From f91fc985a24094fd90a825f68c9ca63f7aa83ce0 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 17 Oct 2021 22:20:28 +0800 Subject: add unit_unit tests --- tests/projects/c/unit_build/src/bar/test4.c | 8 ++++++++ tests/projects/c/unit_build/src/bar/test5.c | 8 ++++++++ tests/projects/c/unit_build/src/foo/test1.c | 8 ++++++++ tests/projects/c/unit_build/src/foo/test2.c | 8 ++++++++ tests/projects/c/unit_build/src/header.h | 10 ++++++++++ tests/projects/c/unit_build/src/main.c | 7 +++++++ tests/projects/c/unit_build/src/test.cpp | 4 ++++ tests/projects/c/unit_build/src/test2.c | 8 ++++++++ tests/projects/c/unit_build/src/test6.c | 8 ++++++++ tests/projects/c/unit_build/src/test7.c | 8 ++++++++ tests/projects/c/unit_build/src/test8.c | 8 ++++++++ tests/projects/c/unit_build/test.lua | 6 ++++++ tests/projects/c/unit_build/xmake.lua | 8 ++++++++ xmake/rules/c++/unit_build/xmake.lua | 21 +++++++++++++++++++++ 14 files changed, 120 insertions(+) create mode 100644 tests/projects/c/unit_build/src/bar/test4.c create mode 100644 tests/projects/c/unit_build/src/bar/test5.c create mode 100644 tests/projects/c/unit_build/src/foo/test1.c create mode 100644 tests/projects/c/unit_build/src/foo/test2.c create mode 100644 tests/projects/c/unit_build/src/header.h create mode 100644 tests/projects/c/unit_build/src/main.c create mode 100644 tests/projects/c/unit_build/src/test.cpp create mode 100644 tests/projects/c/unit_build/src/test2.c create mode 100644 tests/projects/c/unit_build/src/test6.c create mode 100644 tests/projects/c/unit_build/src/test7.c create mode 100644 tests/projects/c/unit_build/src/test8.c create mode 100644 tests/projects/c/unit_build/test.lua create mode 100644 tests/projects/c/unit_build/xmake.lua create mode 100644 xmake/rules/c++/unit_build/xmake.lua diff --git a/tests/projects/c/unit_build/src/bar/test4.c b/tests/projects/c/unit_build/src/bar/test4.c new file mode 100644 index 000000000..a92803eb7 --- /dev/null +++ b/tests/projects/c/unit_build/src/bar/test4.c @@ -0,0 +1,8 @@ + +// main.cpp +#include "header.h" + +int test4() +{ + return 0; +} diff --git a/tests/projects/c/unit_build/src/bar/test5.c b/tests/projects/c/unit_build/src/bar/test5.c new file mode 100644 index 000000000..2a3e7066c --- /dev/null +++ b/tests/projects/c/unit_build/src/bar/test5.c @@ -0,0 +1,8 @@ + +// main.cpp +#include "header.h" + +int test5() +{ + return 0; +} diff --git a/tests/projects/c/unit_build/src/foo/test1.c b/tests/projects/c/unit_build/src/foo/test1.c new file mode 100644 index 000000000..e9d6c83d9 --- /dev/null +++ b/tests/projects/c/unit_build/src/foo/test1.c @@ -0,0 +1,8 @@ + +// main.cpp +#include "header.h" + +int test() +{ + return 0; +} diff --git a/tests/projects/c/unit_build/src/foo/test2.c b/tests/projects/c/unit_build/src/foo/test2.c new file mode 100644 index 000000000..b4ce69b0f --- /dev/null +++ b/tests/projects/c/unit_build/src/foo/test2.c @@ -0,0 +1,8 @@ + +// main.cpp +#include "header.h" + +int test3() +{ + return 0; +} diff --git a/tests/projects/c/unit_build/src/header.h b/tests/projects/c/unit_build/src/header.h new file mode 100644 index 000000000..29bfc5870 --- /dev/null +++ b/tests/projects/c/unit_build/src/header.h @@ -0,0 +1,10 @@ +// header.h +#ifndef HEADER_H +#define HEADER_H + +#include +#include +#include + +#endif + diff --git a/tests/projects/c/unit_build/src/main.c b/tests/projects/c/unit_build/src/main.c new file mode 100644 index 000000000..f0408cdf8 --- /dev/null +++ b/tests/projects/c/unit_build/src/main.c @@ -0,0 +1,7 @@ +#include "header.h" + +int main(int argc, char** argv) +{ + printf("hello xmake!\n"); + return 0; +} diff --git a/tests/projects/c/unit_build/src/test.cpp b/tests/projects/c/unit_build/src/test.cpp new file mode 100644 index 000000000..1cedddad9 --- /dev/null +++ b/tests/projects/c/unit_build/src/test.cpp @@ -0,0 +1,4 @@ +int test_cpp() +{ + return 0; +} diff --git a/tests/projects/c/unit_build/src/test2.c b/tests/projects/c/unit_build/src/test2.c new file mode 100644 index 000000000..596359cd2 --- /dev/null +++ b/tests/projects/c/unit_build/src/test2.c @@ -0,0 +1,8 @@ + +// main.cpp +#include "header.h" + +int test2() +{ + return 0; +} diff --git a/tests/projects/c/unit_build/src/test6.c b/tests/projects/c/unit_build/src/test6.c new file mode 100644 index 000000000..efbd7c6c1 --- /dev/null +++ b/tests/projects/c/unit_build/src/test6.c @@ -0,0 +1,8 @@ + +// main.cpp +#include "header.h" + +int test6() +{ + return 0; +} diff --git a/tests/projects/c/unit_build/src/test7.c b/tests/projects/c/unit_build/src/test7.c new file mode 100644 index 000000000..24aeff619 --- /dev/null +++ b/tests/projects/c/unit_build/src/test7.c @@ -0,0 +1,8 @@ + +// main.cpp +#include "header.h" + +int test7() +{ + return 0; +} diff --git a/tests/projects/c/unit_build/src/test8.c b/tests/projects/c/unit_build/src/test8.c new file mode 100644 index 000000000..6ad5680a6 --- /dev/null +++ b/tests/projects/c/unit_build/src/test8.c @@ -0,0 +1,8 @@ + +// main.cpp +#include "header.h" + +int test8() +{ + return 0; +} diff --git a/tests/projects/c/unit_build/test.lua b/tests/projects/c/unit_build/test.lua new file mode 100644 index 000000000..b76241be2 --- /dev/null +++ b/tests/projects/c/unit_build/test.lua @@ -0,0 +1,6 @@ +-- main entry +function main(t) + + -- build project + t:build() +end diff --git a/tests/projects/c/unit_build/xmake.lua b/tests/projects/c/unit_build/xmake.lua new file mode 100644 index 000000000..1df67c0ec --- /dev/null +++ b/tests/projects/c/unit_build/xmake.lua @@ -0,0 +1,8 @@ +target("test") + set_kind("binary") + add_includedirs("src") + add_rules("c++.unit_build", {batchsize = 2}) + add_files("src/*.c", "src/*.cpp") + add_files("src/foo/*.c", {unit_group = "foo"}) + add_files("src/bar/*.c", {unit_group = "bar"}) + diff --git a/xmake/rules/c++/unit_build/xmake.lua b/xmake/rules/c++/unit_build/xmake.lua new file mode 100644 index 000000000..c6d0ea0bd --- /dev/null +++ b/xmake/rules/c++/unit_build/xmake.lua @@ -0,0 +1,21 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file xmake.lua +-- + +rule("c++.unit_build") -- cgit v1.3.1 From 9dbe8c62ebbfb7fe0fb3335790af184b2b7860a1 Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 17 Oct 2021 23:56:16 +0800 Subject: add unit_build rule --- xmake/core/project/target.lua | 4 -- xmake/rules/c++/unit_build/unit_build.lua | 108 ++++++++++++++++++++++++++++++ xmake/rules/c++/unit_build/xmake.lua | 24 +++++++ 3 files changed, 132 insertions(+), 4 deletions(-) create mode 100644 xmake/rules/c++/unit_build/unit_build.lua diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index d6bba579a..08ab720c0 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -1688,11 +1688,7 @@ function _instance:sourcebatches() end end end - - -- cache it self._SOURCEBATCHES = sourcebatches - - -- ok? return sourcebatches, modified end diff --git a/xmake/rules/c++/unit_build/unit_build.lua b/xmake/rules/c++/unit_build/unit_build.lua new file mode 100644 index 000000000..030639f1a --- /dev/null +++ b/xmake/rules/c++/unit_build/unit_build.lua @@ -0,0 +1,108 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file unit_build.lua +-- + +-- imports +import("core.project.depend") + +function _merge_unitfile(target, sourcefile_unit, sourcefiles, opt) + local dependfile = target:dependfile(sourcefile_unit) + depend.on_changed(function () + + -- trace + vprint("generating.unitfile %s", sourcefile_unit) + + -- do merge + local unitfile = io.open(sourcefile_unit, "w") + for _, sourcefile in ipairs(sourcefiles) do + sourcefile = path.absolute(sourcefile) + sourcefile_unit = path.absolute(sourcefile_unit) + sourcefile = path.relative(sourcefile, path.directory(sourcefile_unit)) + unitfile:print("#include \"%s\"", sourcefile) + end + unitfile:close() + + end, {dependfile = dependfile, files = sourcefiles}) +end + +function generate_unitfiles(target, sourcebatch, opt) + local unitbatch = target:data("unit_build.unitbatch." .. sourcebatch.rulename) + if unitbatch then + for _, sourcefile_unit in ipairs(sourcebatch.sourcefiles) do + local sourceinfo = unitbatch[sourcefile_unit] + if sourceinfo then + local sourcefiles = sourceinfo.sourcefiles + if sourcefiles then + _merge_unitfile(target, sourcefile_unit, sourcefiles, opt) + end + end + end + end +end + +function main(target, sourcebatch) + + -- get unit batch sources + local extraconf = target:extraconf("rules", "c++.unit_build") + local batchsize = extraconf.batchsize + local id = 1 + local count = 0 + local unitbatch = {} + local sourcedir = path.join(target:autogendir({root = true}), "unit_build") + for idx, sourcefile in pairs(sourcebatch.sourcefiles) do + local sourcefile_unit + local objectfile = sourcebatch.objectfiles[idx] + local dependfile = sourcebatch.dependfiles[idx] + local fileconfig = target:fileconfig(sourcefile) + if fileconfig and fileconfig.unit_group then + sourcefile_unit = path.join(sourcedir, "unit_" .. fileconfig.unit_group .. path.extension(sourcefile)) + else + if batchsize and count > batchsize then + id = id + 1 + end + sourcefile_unit = path.join(sourcedir, "unit_" .. hash.uuid(tostring(id)):split("-", {plain = true})[1] .. path.extension(sourcefile)) + count = count + 1 + end + local sourceinfo = unitbatch[sourcefile_unit] + if not sourceinfo then + sourceinfo = {} + sourceinfo.objectfile = target:objectfile(sourcefile_unit) + sourceinfo.dependfile = target:dependfile(sourceinfo.objectfile) + unitbatch[sourcefile_unit] = sourceinfo + end + sourceinfo.sourcefiles = sourceinfo.sourcefiles or {} + table.insert(sourceinfo.sourcefiles, sourcefile) + end + + -- use unit batch + local sourcefiles = {} + local objectfiles = {} + local dependfiles = {} + for sourcefile_unit, sourceinfo in pairs(unitbatch) do + table.insert(sourcefiles, sourcefile_unit) + table.insert(objectfiles, sourceinfo.objectfile) + table.insert(dependfiles, sourceinfo.dependfile) + end + sourcebatch.sourcefiles = sourcefiles + sourcebatch.objectfiles = objectfiles + sourcebatch.dependfiles = dependfiles + + -- save unit batch + target:data_set("unit_build.unitbatch." .. sourcebatch.rulename, unitbatch) +end diff --git a/xmake/rules/c++/unit_build/xmake.lua b/xmake/rules/c++/unit_build/xmake.lua index c6d0ea0bd..b0eb7b39a 100644 --- a/xmake/rules/c++/unit_build/xmake.lua +++ b/xmake/rules/c++/unit_build/xmake.lua @@ -19,3 +19,27 @@ -- rule("c++.unit_build") + after_load(function (target) + import("unit_build") + local sourcebatches = target:sourcebatches() + if sourcebatches then + for _, rulename in ipairs({"c.build", "c++.build"}) do + local sourcebatch = sourcebatches[rulename] + if sourcebatch then + unit_build(target, sourcebatch) + end + end + end + end) + before_build(function (target, opt) + import("unit_build") + local sourcebatches = target:sourcebatches() + if sourcebatches then + for _, rulename in ipairs({"c.build", "c++.build"}) do + local sourcebatch = sourcebatches[rulename] + if sourcebatch then + unit_build.generate_unitfiles(target, sourcebatch, opt) + end + end + end + end) -- cgit v1.3.1 From d31f3b2ef667e8c0b22cebbe9ab6f7579905d3ff Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 18 Oct 2021 21:01:29 +0800 Subject: improve unit_build --- xmake/rules/c++/unit_build/unit_build.lua | 39 +++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/xmake/rules/c++/unit_build/unit_build.lua b/xmake/rules/c++/unit_build/unit_build.lua index 030639f1a..7fd50b596 100644 --- a/xmake/rules/c++/unit_build/unit_build.lua +++ b/xmake/rules/c++/unit_build/unit_build.lua @@ -56,14 +56,25 @@ function generate_unitfiles(target, sourcebatch, opt) end end +-- use unit build +-- +-- e.g. +-- add_rules("c++.unit_build", {batchsize = 2}) +-- add_files("src/*.c", "src/*.cpp", {unit_ignored = true}) +-- add_files("src/foo/*.c", {unit_group = "foo"}) +-- add_files("src/bar/*.c", {unit_group = "bar"}) +-- function main(target, sourcebatch) -- get unit batch sources local extraconf = target:extraconf("rules", "c++.unit_build") - local batchsize = extraconf.batchsize + local batchsize = extraconf and extraconf.batchsize local id = 1 local count = 0 local unitbatch = {} + local sourcefiles = {} + local objectfiles = {} + local dependfiles = {} local sourcedir = path.join(target:autogendir({root = true}), "unit_build") for idx, sourcefile in pairs(sourcebatch.sourcefiles) do local sourcefile_unit @@ -72,6 +83,11 @@ function main(target, sourcebatch) local fileconfig = target:fileconfig(sourcefile) if fileconfig and fileconfig.unit_group then sourcefile_unit = path.join(sourcedir, "unit_" .. fileconfig.unit_group .. path.extension(sourcefile)) + elseif fileconfig and fileconfig.unit_ignored then + -- we do not add these files to unit file + table.insert(sourcefiles, sourcefile) + table.insert(objectfiles, objectfile) + table.insert(dependfiles, dependfile) else if batchsize and count > batchsize then id = id + 1 @@ -79,21 +95,20 @@ function main(target, sourcebatch) sourcefile_unit = path.join(sourcedir, "unit_" .. hash.uuid(tostring(id)):split("-", {plain = true})[1] .. path.extension(sourcefile)) count = count + 1 end - local sourceinfo = unitbatch[sourcefile_unit] - if not sourceinfo then - sourceinfo = {} - sourceinfo.objectfile = target:objectfile(sourcefile_unit) - sourceinfo.dependfile = target:dependfile(sourceinfo.objectfile) - unitbatch[sourcefile_unit] = sourceinfo + if sourcefile_unit then + local sourceinfo = unitbatch[sourcefile_unit] + if not sourceinfo then + sourceinfo = {} + sourceinfo.objectfile = target:objectfile(sourcefile_unit) + sourceinfo.dependfile = target:dependfile(sourceinfo.objectfile) + unitbatch[sourcefile_unit] = sourceinfo + end + sourceinfo.sourcefiles = sourceinfo.sourcefiles or {} + table.insert(sourceinfo.sourcefiles, sourcefile) end - sourceinfo.sourcefiles = sourceinfo.sourcefiles or {} - table.insert(sourceinfo.sourcefiles, sourcefile) end -- use unit batch - local sourcefiles = {} - local objectfiles = {} - local dependfiles = {} for sourcefile_unit, sourceinfo in pairs(unitbatch) do table.insert(sourcefiles, sourcefile_unit) table.insert(objectfiles, sourceinfo.objectfile) -- cgit v1.3.1 From 69ddff2a27199172659c9c823dabde8c4b004ae3 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 18 Oct 2021 21:01:43 +0800 Subject: update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09b60b8ae..89d1a64aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ * Add gcc-8, gcc-9, gcc-10, gcc-11 toolchains * [#1623](https://github.com/xmake-io/xmake/issues/1632): Support find_package from cmake * [#1747](https://github.com/xmake-io/xmake/issues/1747): Add `set_kind("headeronly")` for target to install files for headeronly library +* [#1019](https://github.com/xmake-io/xmake/issues/1019): Support Unity build ### Changes @@ -1110,6 +1111,7 @@ * 添加 gcc-8, gcc-9, gcc-10, gcc-11 工具链 * [#1623](https://github.com/xmake-io/xmake/issues/1632): 支持 find_package 从 cmake 查找包 * [#1747](https://github.com/xmake-io/xmake/issues/1747): 添加 `set_kind("headeronly")` 更好的处理 headeronly 库的安装 +* [#1019](https://github.com/xmake-io/xmake/issues/1019): 支持 Unity build ### 改进 -- cgit v1.3.1 From 4e3967d123432b2352b450ed892fcb1b8f3b37d2 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 18 Oct 2021 21:02:26 +0800 Subject: rename to unity build --- tests/projects/c/unit_build/src/bar/test4.c | 8 -- tests/projects/c/unit_build/src/bar/test5.c | 8 -- tests/projects/c/unit_build/src/foo/test1.c | 8 -- tests/projects/c/unit_build/src/foo/test2.c | 8 -- tests/projects/c/unit_build/src/header.h | 10 --- tests/projects/c/unit_build/src/main.c | 7 -- tests/projects/c/unit_build/src/test.cpp | 4 - tests/projects/c/unit_build/src/test2.c | 8 -- tests/projects/c/unit_build/src/test6.c | 8 -- tests/projects/c/unit_build/src/test7.c | 8 -- tests/projects/c/unit_build/src/test8.c | 8 -- tests/projects/c/unit_build/test.lua | 6 -- tests/projects/c/unit_build/xmake.lua | 8 -- tests/projects/c/unity_build/src/bar/test4.c | 8 ++ tests/projects/c/unity_build/src/bar/test5.c | 8 ++ tests/projects/c/unity_build/src/foo/test1.c | 8 ++ tests/projects/c/unity_build/src/foo/test2.c | 8 ++ tests/projects/c/unity_build/src/header.h | 10 +++ tests/projects/c/unity_build/src/main.c | 7 ++ tests/projects/c/unity_build/src/test.cpp | 4 + tests/projects/c/unity_build/src/test2.c | 8 ++ tests/projects/c/unity_build/src/test6.c | 8 ++ tests/projects/c/unity_build/src/test7.c | 8 ++ tests/projects/c/unity_build/src/test8.c | 8 ++ tests/projects/c/unity_build/test.lua | 6 ++ tests/projects/c/unity_build/xmake.lua | 8 ++ xmake/rules/c++/unit_build/unit_build.lua | 123 --------------------------- xmake/rules/c++/unit_build/xmake.lua | 45 ---------- xmake/rules/c++/unity_build/unity_build.lua | 123 +++++++++++++++++++++++++++ xmake/rules/c++/unity_build/xmake.lua | 45 ++++++++++ 30 files changed, 267 insertions(+), 267 deletions(-) delete mode 100644 tests/projects/c/unit_build/src/bar/test4.c delete mode 100644 tests/projects/c/unit_build/src/bar/test5.c delete mode 100644 tests/projects/c/unit_build/src/foo/test1.c delete mode 100644 tests/projects/c/unit_build/src/foo/test2.c delete mode 100644 tests/projects/c/unit_build/src/header.h delete mode 100644 tests/projects/c/unit_build/src/main.c delete mode 100644 tests/projects/c/unit_build/src/test.cpp delete mode 100644 tests/projects/c/unit_build/src/test2.c delete mode 100644 tests/projects/c/unit_build/src/test6.c delete mode 100644 tests/projects/c/unit_build/src/test7.c delete mode 100644 tests/projects/c/unit_build/src/test8.c delete mode 100644 tests/projects/c/unit_build/test.lua delete mode 100644 tests/projects/c/unit_build/xmake.lua create mode 100644 tests/projects/c/unity_build/src/bar/test4.c create mode 100644 tests/projects/c/unity_build/src/bar/test5.c create mode 100644 tests/projects/c/unity_build/src/foo/test1.c create mode 100644 tests/projects/c/unity_build/src/foo/test2.c create mode 100644 tests/projects/c/unity_build/src/header.h create mode 100644 tests/projects/c/unity_build/src/main.c create mode 100644 tests/projects/c/unity_build/src/test.cpp create mode 100644 tests/projects/c/unity_build/src/test2.c create mode 100644 tests/projects/c/unity_build/src/test6.c create mode 100644 tests/projects/c/unity_build/src/test7.c create mode 100644 tests/projects/c/unity_build/src/test8.c create mode 100644 tests/projects/c/unity_build/test.lua create mode 100644 tests/projects/c/unity_build/xmake.lua delete mode 100644 xmake/rules/c++/unit_build/unit_build.lua delete mode 100644 xmake/rules/c++/unit_build/xmake.lua create mode 100644 xmake/rules/c++/unity_build/unity_build.lua create mode 100644 xmake/rules/c++/unity_build/xmake.lua diff --git a/tests/projects/c/unit_build/src/bar/test4.c b/tests/projects/c/unit_build/src/bar/test4.c deleted file mode 100644 index a92803eb7..000000000 --- a/tests/projects/c/unit_build/src/bar/test4.c +++ /dev/null @@ -1,8 +0,0 @@ - -// main.cpp -#include "header.h" - -int test4() -{ - return 0; -} diff --git a/tests/projects/c/unit_build/src/bar/test5.c b/tests/projects/c/unit_build/src/bar/test5.c deleted file mode 100644 index 2a3e7066c..000000000 --- a/tests/projects/c/unit_build/src/bar/test5.c +++ /dev/null @@ -1,8 +0,0 @@ - -// main.cpp -#include "header.h" - -int test5() -{ - return 0; -} diff --git a/tests/projects/c/unit_build/src/foo/test1.c b/tests/projects/c/unit_build/src/foo/test1.c deleted file mode 100644 index e9d6c83d9..000000000 --- a/tests/projects/c/unit_build/src/foo/test1.c +++ /dev/null @@ -1,8 +0,0 @@ - -// main.cpp -#include "header.h" - -int test() -{ - return 0; -} diff --git a/tests/projects/c/unit_build/src/foo/test2.c b/tests/projects/c/unit_build/src/foo/test2.c deleted file mode 100644 index b4ce69b0f..000000000 --- a/tests/projects/c/unit_build/src/foo/test2.c +++ /dev/null @@ -1,8 +0,0 @@ - -// main.cpp -#include "header.h" - -int test3() -{ - return 0; -} diff --git a/tests/projects/c/unit_build/src/header.h b/tests/projects/c/unit_build/src/header.h deleted file mode 100644 index 29bfc5870..000000000 --- a/tests/projects/c/unit_build/src/header.h +++ /dev/null @@ -1,10 +0,0 @@ -// header.h -#ifndef HEADER_H -#define HEADER_H - -#include -#include -#include - -#endif - diff --git a/tests/projects/c/unit_build/src/main.c b/tests/projects/c/unit_build/src/main.c deleted file mode 100644 index f0408cdf8..000000000 --- a/tests/projects/c/unit_build/src/main.c +++ /dev/null @@ -1,7 +0,0 @@ -#include "header.h" - -int main(int argc, char** argv) -{ - printf("hello xmake!\n"); - return 0; -} diff --git a/tests/projects/c/unit_build/src/test.cpp b/tests/projects/c/unit_build/src/test.cpp deleted file mode 100644 index 1cedddad9..000000000 --- a/tests/projects/c/unit_build/src/test.cpp +++ /dev/null @@ -1,4 +0,0 @@ -int test_cpp() -{ - return 0; -} diff --git a/tests/projects/c/unit_build/src/test2.c b/tests/projects/c/unit_build/src/test2.c deleted file mode 100644 index 596359cd2..000000000 --- a/tests/projects/c/unit_build/src/test2.c +++ /dev/null @@ -1,8 +0,0 @@ - -// main.cpp -#include "header.h" - -int test2() -{ - return 0; -} diff --git a/tests/projects/c/unit_build/src/test6.c b/tests/projects/c/unit_build/src/test6.c deleted file mode 100644 index efbd7c6c1..000000000 --- a/tests/projects/c/unit_build/src/test6.c +++ /dev/null @@ -1,8 +0,0 @@ - -// main.cpp -#include "header.h" - -int test6() -{ - return 0; -} diff --git a/tests/projects/c/unit_build/src/test7.c b/tests/projects/c/unit_build/src/test7.c deleted file mode 100644 index 24aeff619..000000000 --- a/tests/projects/c/unit_build/src/test7.c +++ /dev/null @@ -1,8 +0,0 @@ - -// main.cpp -#include "header.h" - -int test7() -{ - return 0; -} diff --git a/tests/projects/c/unit_build/src/test8.c b/tests/projects/c/unit_build/src/test8.c deleted file mode 100644 index 6ad5680a6..000000000 --- a/tests/projects/c/unit_build/src/test8.c +++ /dev/null @@ -1,8 +0,0 @@ - -// main.cpp -#include "header.h" - -int test8() -{ - return 0; -} diff --git a/tests/projects/c/unit_build/test.lua b/tests/projects/c/unit_build/test.lua deleted file mode 100644 index b76241be2..000000000 --- a/tests/projects/c/unit_build/test.lua +++ /dev/null @@ -1,6 +0,0 @@ --- main entry -function main(t) - - -- build project - t:build() -end diff --git a/tests/projects/c/unit_build/xmake.lua b/tests/projects/c/unit_build/xmake.lua deleted file mode 100644 index 1df67c0ec..000000000 --- a/tests/projects/c/unit_build/xmake.lua +++ /dev/null @@ -1,8 +0,0 @@ -target("test") - set_kind("binary") - add_includedirs("src") - add_rules("c++.unit_build", {batchsize = 2}) - add_files("src/*.c", "src/*.cpp") - add_files("src/foo/*.c", {unit_group = "foo"}) - add_files("src/bar/*.c", {unit_group = "bar"}) - diff --git a/tests/projects/c/unity_build/src/bar/test4.c b/tests/projects/c/unity_build/src/bar/test4.c new file mode 100644 index 000000000..a92803eb7 --- /dev/null +++ b/tests/projects/c/unity_build/src/bar/test4.c @@ -0,0 +1,8 @@ + +// main.cpp +#include "header.h" + +int test4() +{ + return 0; +} diff --git a/tests/projects/c/unity_build/src/bar/test5.c b/tests/projects/c/unity_build/src/bar/test5.c new file mode 100644 index 000000000..2a3e7066c --- /dev/null +++ b/tests/projects/c/unity_build/src/bar/test5.c @@ -0,0 +1,8 @@ + +// main.cpp +#include "header.h" + +int test5() +{ + return 0; +} diff --git a/tests/projects/c/unity_build/src/foo/test1.c b/tests/projects/c/unity_build/src/foo/test1.c new file mode 100644 index 000000000..e9d6c83d9 --- /dev/null +++ b/tests/projects/c/unity_build/src/foo/test1.c @@ -0,0 +1,8 @@ + +// main.cpp +#include "header.h" + +int test() +{ + return 0; +} diff --git a/tests/projects/c/unity_build/src/foo/test2.c b/tests/projects/c/unity_build/src/foo/test2.c new file mode 100644 index 000000000..b4ce69b0f --- /dev/null +++ b/tests/projects/c/unity_build/src/foo/test2.c @@ -0,0 +1,8 @@ + +// main.cpp +#include "header.h" + +int test3() +{ + return 0; +} diff --git a/tests/projects/c/unity_build/src/header.h b/tests/projects/c/unity_build/src/header.h new file mode 100644 index 000000000..29bfc5870 --- /dev/null +++ b/tests/projects/c/unity_build/src/header.h @@ -0,0 +1,10 @@ +// header.h +#ifndef HEADER_H +#define HEADER_H + +#include +#include +#include + +#endif + diff --git a/tests/projects/c/unity_build/src/main.c b/tests/projects/c/unity_build/src/main.c new file mode 100644 index 000000000..f0408cdf8 --- /dev/null +++ b/tests/projects/c/unity_build/src/main.c @@ -0,0 +1,7 @@ +#include "header.h" + +int main(int argc, char** argv) +{ + printf("hello xmake!\n"); + return 0; +} diff --git a/tests/projects/c/unity_build/src/test.cpp b/tests/projects/c/unity_build/src/test.cpp new file mode 100644 index 000000000..1cedddad9 --- /dev/null +++ b/tests/projects/c/unity_build/src/test.cpp @@ -0,0 +1,4 @@ +int test_cpp() +{ + return 0; +} diff --git a/tests/projects/c/unity_build/src/test2.c b/tests/projects/c/unity_build/src/test2.c new file mode 100644 index 000000000..596359cd2 --- /dev/null +++ b/tests/projects/c/unity_build/src/test2.c @@ -0,0 +1,8 @@ + +// main.cpp +#include "header.h" + +int test2() +{ + return 0; +} diff --git a/tests/projects/c/unity_build/src/test6.c b/tests/projects/c/unity_build/src/test6.c new file mode 100644 index 000000000..efbd7c6c1 --- /dev/null +++ b/tests/projects/c/unity_build/src/test6.c @@ -0,0 +1,8 @@ + +// main.cpp +#include "header.h" + +int test6() +{ + return 0; +} diff --git a/tests/projects/c/unity_build/src/test7.c b/tests/projects/c/unity_build/src/test7.c new file mode 100644 index 000000000..24aeff619 --- /dev/null +++ b/tests/projects/c/unity_build/src/test7.c @@ -0,0 +1,8 @@ + +// main.cpp +#include "header.h" + +int test7() +{ + return 0; +} diff --git a/tests/projects/c/unity_build/src/test8.c b/tests/projects/c/unity_build/src/test8.c new file mode 100644 index 000000000..6ad5680a6 --- /dev/null +++ b/tests/projects/c/unity_build/src/test8.c @@ -0,0 +1,8 @@ + +// main.cpp +#include "header.h" + +int test8() +{ + return 0; +} diff --git a/tests/projects/c/unity_build/test.lua b/tests/projects/c/unity_build/test.lua new file mode 100644 index 000000000..b76241be2 --- /dev/null +++ b/tests/projects/c/unity_build/test.lua @@ -0,0 +1,6 @@ +-- main entry +function main(t) + + -- build project + t:build() +end diff --git a/tests/projects/c/unity_build/xmake.lua b/tests/projects/c/unity_build/xmake.lua new file mode 100644 index 000000000..447960db5 --- /dev/null +++ b/tests/projects/c/unity_build/xmake.lua @@ -0,0 +1,8 @@ +target("test") + set_kind("binary") + add_includedirs("src") + add_rules("c++.unity_build", {batchsize = 2}) + add_files("src/*.c", "src/*.cpp") + add_files("src/foo/*.c", {unity_group = "foo"}) + add_files("src/bar/*.c", {unity_group = "bar"}) + diff --git a/xmake/rules/c++/unit_build/unit_build.lua b/xmake/rules/c++/unit_build/unit_build.lua deleted file mode 100644 index 7fd50b596..000000000 --- a/xmake/rules/c++/unit_build/unit_build.lua +++ /dev/null @@ -1,123 +0,0 @@ ---!A cross-platform build utility based on Lua --- --- Licensed under the Apache License, Version 2.0 (the "License"); --- you may not use this file except in compliance with the License. --- You may obtain a copy of the License at --- --- http://www.apache.org/licenses/LICENSE-2.0 --- --- Unless required by applicable law or agreed to in writing, software --- distributed under the License is distributed on an "AS IS" BASIS, --- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. --- See the License for the specific language governing permissions and --- limitations under the License. --- --- Copyright (C) 2015-present, TBOOX Open Source Group. --- --- @author ruki --- @file unit_build.lua --- - --- imports -import("core.project.depend") - -function _merge_unitfile(target, sourcefile_unit, sourcefiles, opt) - local dependfile = target:dependfile(sourcefile_unit) - depend.on_changed(function () - - -- trace - vprint("generating.unitfile %s", sourcefile_unit) - - -- do merge - local unitfile = io.open(sourcefile_unit, "w") - for _, sourcefile in ipairs(sourcefiles) do - sourcefile = path.absolute(sourcefile) - sourcefile_unit = path.absolute(sourcefile_unit) - sourcefile = path.relative(sourcefile, path.directory(sourcefile_unit)) - unitfile:print("#include \"%s\"", sourcefile) - end - unitfile:close() - - end, {dependfile = dependfile, files = sourcefiles}) -end - -function generate_unitfiles(target, sourcebatch, opt) - local unitbatch = target:data("unit_build.unitbatch." .. sourcebatch.rulename) - if unitbatch then - for _, sourcefile_unit in ipairs(sourcebatch.sourcefiles) do - local sourceinfo = unitbatch[sourcefile_unit] - if sourceinfo then - local sourcefiles = sourceinfo.sourcefiles - if sourcefiles then - _merge_unitfile(target, sourcefile_unit, sourcefiles, opt) - end - end - end - end -end - --- use unit build --- --- e.g. --- add_rules("c++.unit_build", {batchsize = 2}) --- add_files("src/*.c", "src/*.cpp", {unit_ignored = true}) --- add_files("src/foo/*.c", {unit_group = "foo"}) --- add_files("src/bar/*.c", {unit_group = "bar"}) --- -function main(target, sourcebatch) - - -- get unit batch sources - local extraconf = target:extraconf("rules", "c++.unit_build") - local batchsize = extraconf and extraconf.batchsize - local id = 1 - local count = 0 - local unitbatch = {} - local sourcefiles = {} - local objectfiles = {} - local dependfiles = {} - local sourcedir = path.join(target:autogendir({root = true}), "unit_build") - for idx, sourcefile in pairs(sourcebatch.sourcefiles) do - local sourcefile_unit - local objectfile = sourcebatch.objectfiles[idx] - local dependfile = sourcebatch.dependfiles[idx] - local fileconfig = target:fileconfig(sourcefile) - if fileconfig and fileconfig.unit_group then - sourcefile_unit = path.join(sourcedir, "unit_" .. fileconfig.unit_group .. path.extension(sourcefile)) - elseif fileconfig and fileconfig.unit_ignored then - -- we do not add these files to unit file - table.insert(sourcefiles, sourcefile) - table.insert(objectfiles, objectfile) - table.insert(dependfiles, dependfile) - else - if batchsize and count > batchsize then - id = id + 1 - end - sourcefile_unit = path.join(sourcedir, "unit_" .. hash.uuid(tostring(id)):split("-", {plain = true})[1] .. path.extension(sourcefile)) - count = count + 1 - end - if sourcefile_unit then - local sourceinfo = unitbatch[sourcefile_unit] - if not sourceinfo then - sourceinfo = {} - sourceinfo.objectfile = target:objectfile(sourcefile_unit) - sourceinfo.dependfile = target:dependfile(sourceinfo.objectfile) - unitbatch[sourcefile_unit] = sourceinfo - end - sourceinfo.sourcefiles = sourceinfo.sourcefiles or {} - table.insert(sourceinfo.sourcefiles, sourcefile) - end - end - - -- use unit batch - for sourcefile_unit, sourceinfo in pairs(unitbatch) do - table.insert(sourcefiles, sourcefile_unit) - table.insert(objectfiles, sourceinfo.objectfile) - table.insert(dependfiles, sourceinfo.dependfile) - end - sourcebatch.sourcefiles = sourcefiles - sourcebatch.objectfiles = objectfiles - sourcebatch.dependfiles = dependfiles - - -- save unit batch - target:data_set("unit_build.unitbatch." .. sourcebatch.rulename, unitbatch) -end diff --git a/xmake/rules/c++/unit_build/xmake.lua b/xmake/rules/c++/unit_build/xmake.lua deleted file mode 100644 index b0eb7b39a..000000000 --- a/xmake/rules/c++/unit_build/xmake.lua +++ /dev/null @@ -1,45 +0,0 @@ ---!A cross-platform build utility based on Lua --- --- Licensed under the Apache License, Version 2.0 (the "License"); --- you may not use this file except in compliance with the License. --- You may obtain a copy of the License at --- --- http://www.apache.org/licenses/LICENSE-2.0 --- --- Unless required by applicable law or agreed to in writing, software --- distributed under the License is distributed on an "AS IS" BASIS, --- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. --- See the License for the specific language governing permissions and --- limitations under the License. --- --- Copyright (C) 2015-present, TBOOX Open Source Group. --- --- @author ruki --- @file xmake.lua --- - -rule("c++.unit_build") - after_load(function (target) - import("unit_build") - local sourcebatches = target:sourcebatches() - if sourcebatches then - for _, rulename in ipairs({"c.build", "c++.build"}) do - local sourcebatch = sourcebatches[rulename] - if sourcebatch then - unit_build(target, sourcebatch) - end - end - end - end) - before_build(function (target, opt) - import("unit_build") - local sourcebatches = target:sourcebatches() - if sourcebatches then - for _, rulename in ipairs({"c.build", "c++.build"}) do - local sourcebatch = sourcebatches[rulename] - if sourcebatch then - unit_build.generate_unitfiles(target, sourcebatch, opt) - end - end - end - end) diff --git a/xmake/rules/c++/unity_build/unity_build.lua b/xmake/rules/c++/unity_build/unity_build.lua new file mode 100644 index 000000000..57bff99a0 --- /dev/null +++ b/xmake/rules/c++/unity_build/unity_build.lua @@ -0,0 +1,123 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file unity_build.lua +-- + +-- imports +import("core.project.depend") + +function _merge_unityfile(target, sourcefile_unity, sourcefiles, opt) + local dependfile = target:dependfile(sourcefile_unity) + depend.on_changed(function () + + -- trace + vprint("generating.unityfile %s", sourcefile_unity) + + -- do merge + local unityfile = io.open(sourcefile_unity, "w") + for _, sourcefile in ipairs(sourcefiles) do + sourcefile = path.absolute(sourcefile) + sourcefile_unity = path.absolute(sourcefile_unity) + sourcefile = path.relative(sourcefile, path.directory(sourcefile_unity)) + unityfile:print("#include \"%s\"", sourcefile) + end + unityfile:close() + + end, {dependfile = dependfile, files = sourcefiles}) +end + +function generate_unityfiles(target, sourcebatch, opt) + local unity_batch = target:data("unity_build.unity_batch." .. sourcebatch.rulename) + if unity_batch then + for _, sourcefile_unity in ipairs(sourcebatch.sourcefiles) do + local sourceinfo = unity_batch[sourcefile_unity] + if sourceinfo then + local sourcefiles = sourceinfo.sourcefiles + if sourcefiles then + _merge_unityfile(target, sourcefile_unity, sourcefiles, opt) + end + end + end + end +end + +-- use unity build +-- +-- e.g. +-- add_rules("c++.unity_build", {batchsize = 2}) +-- add_files("src/*.c", "src/*.cpp", {unity_ignored = true}) +-- add_files("src/foo/*.c", {unity_group = "foo"}) +-- add_files("src/bar/*.c", {unity_group = "bar"}) +-- +function main(target, sourcebatch) + + -- get unit batch sources + local extraconf = target:extraconf("rules", "c++.unity_build") + local batchsize = extraconf and extraconf.batchsize + local id = 1 + local count = 0 + local unity_batch = {} + local sourcefiles = {} + local objectfiles = {} + local dependfiles = {} + local sourcedir = path.join(target:autogendir({root = true}), "unity_build") + for idx, sourcefile in pairs(sourcebatch.sourcefiles) do + local sourcefile_unity + local objectfile = sourcebatch.objectfiles[idx] + local dependfile = sourcebatch.dependfiles[idx] + local fileconfig = target:fileconfig(sourcefile) + if fileconfig and fileconfig.unity_group then + sourcefile_unity = path.join(sourcedir, "unity_" .. fileconfig.unity_group .. path.extension(sourcefile)) + elseif fileconfig and fileconfig.unity_ignored then + -- we do not add these files to unit file + table.insert(sourcefiles, sourcefile) + table.insert(objectfiles, objectfile) + table.insert(dependfiles, dependfile) + else + if batchsize and count > batchsize then + id = id + 1 + end + sourcefile_unity = path.join(sourcedir, "unity_" .. hash.uuid(tostring(id)):split("-", {plain = true})[1] .. path.extension(sourcefile)) + count = count + 1 + end + if sourcefile_unity then + local sourceinfo = unity_batch[sourcefile_unity] + if not sourceinfo then + sourceinfo = {} + sourceinfo.objectfile = target:objectfile(sourcefile_unity) + sourceinfo.dependfile = target:dependfile(sourceinfo.objectfile) + unity_batch[sourcefile_unity] = sourceinfo + end + sourceinfo.sourcefiles = sourceinfo.sourcefiles or {} + table.insert(sourceinfo.sourcefiles, sourcefile) + end + end + + -- use unit batch + for sourcefile_unity, sourceinfo in pairs(unity_batch) do + table.insert(sourcefiles, sourcefile_unity) + table.insert(objectfiles, sourceinfo.objectfile) + table.insert(dependfiles, sourceinfo.dependfile) + end + sourcebatch.sourcefiles = sourcefiles + sourcebatch.objectfiles = objectfiles + sourcebatch.dependfiles = dependfiles + + -- save unit batch + target:data_set("unity_build.unity_batch." .. sourcebatch.rulename, unity_batch) +end diff --git a/xmake/rules/c++/unity_build/xmake.lua b/xmake/rules/c++/unity_build/xmake.lua new file mode 100644 index 000000000..838f8a097 --- /dev/null +++ b/xmake/rules/c++/unity_build/xmake.lua @@ -0,0 +1,45 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file xmake.lua +-- + +rule("c++.unity_build") + after_load(function (target) + import("unity_build") + local sourcebatches = target:sourcebatches() + if sourcebatches then + for _, rulename in ipairs({"c.build", "c++.build"}) do + local sourcebatch = sourcebatches[rulename] + if sourcebatch then + unity_build(target, sourcebatch) + end + end + end + end) + before_build(function (target, opt) + import("unity_build") + local sourcebatches = target:sourcebatches() + if sourcebatches then + for _, rulename in ipairs({"c.build", "c++.build"}) do + local sourcebatch = sourcebatches[rulename] + if sourcebatch then + unity_build.generate_unitfiles(target, sourcebatch, opt) + end + end + end + end) -- cgit v1.3.1 From 26de32e94fea1b849395ccf035aeb111b71084f3 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 18 Oct 2021 21:02:32 +0800 Subject: rename to unity build --- xmake/rules/c++/unity_build/unity_build.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/rules/c++/unity_build/unity_build.lua b/xmake/rules/c++/unity_build/unity_build.lua index 57bff99a0..82338f506 100644 --- a/xmake/rules/c++/unity_build/unity_build.lua +++ b/xmake/rules/c++/unity_build/unity_build.lua @@ -84,7 +84,7 @@ function main(target, sourcebatch) if fileconfig and fileconfig.unity_group then sourcefile_unity = path.join(sourcedir, "unity_" .. fileconfig.unity_group .. path.extension(sourcefile)) elseif fileconfig and fileconfig.unity_ignored then - -- we do not add these files to unit file + -- we do not add these files to unity file table.insert(sourcefiles, sourcefile) table.insert(objectfiles, objectfile) table.insert(dependfiles, dependfile) -- cgit v1.3.1 From afcfa7fcd5dd767ecd683cdd5bacea120cfc6270 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 18 Oct 2021 21:04:08 +0800 Subject: improve unity build --- xmake/rules/c++/unity_build/unity_build.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/rules/c++/unity_build/unity_build.lua b/xmake/rules/c++/unity_build/unity_build.lua index 82338f506..e28e5aff3 100644 --- a/xmake/rules/c++/unity_build/unity_build.lua +++ b/xmake/rules/c++/unity_build/unity_build.lua @@ -83,7 +83,7 @@ function main(target, sourcebatch) local fileconfig = target:fileconfig(sourcefile) if fileconfig and fileconfig.unity_group then sourcefile_unity = path.join(sourcedir, "unity_" .. fileconfig.unity_group .. path.extension(sourcefile)) - elseif fileconfig and fileconfig.unity_ignored then + elseif (fileconfig and fileconfig.unity_ignored) or (batchsize == 0) then -- we do not add these files to unity file table.insert(sourcefiles, sourcefile) table.insert(objectfiles, objectfile) -- cgit v1.3.1 From 2f8bf766dfeb9103e5ab9a1ccc7936a5bc04aa9c Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 18 Oct 2021 21:05:07 +0800 Subject: fix errors --- xmake/rules/c++/unity_build/xmake.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/rules/c++/unity_build/xmake.lua b/xmake/rules/c++/unity_build/xmake.lua index 838f8a097..b03a383ae 100644 --- a/xmake/rules/c++/unity_build/xmake.lua +++ b/xmake/rules/c++/unity_build/xmake.lua @@ -38,7 +38,7 @@ rule("c++.unity_build") for _, rulename in ipairs({"c.build", "c++.build"}) do local sourcebatch = sourcebatches[rulename] if sourcebatch then - unity_build.generate_unitfiles(target, sourcebatch, opt) + unity_build.generate_unityfiles(target, sourcebatch, opt) end end end -- cgit v1.3.1 From 105117735be22a14c32d136681b5f08916625fb4 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 18 Oct 2021 22:17:25 +0800 Subject: add c.unity_build --- tests/projects/c/unity_build/xmake.lua | 2 +- xmake/rules/c++/unity_build/unity_build.lua | 2 +- xmake/rules/c++/unity_build/xmake.lua | 38 +++++++++++++++++++++-------- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/tests/projects/c/unity_build/xmake.lua b/tests/projects/c/unity_build/xmake.lua index 447960db5..89166a312 100644 --- a/tests/projects/c/unity_build/xmake.lua +++ b/tests/projects/c/unity_build/xmake.lua @@ -1,7 +1,7 @@ target("test") set_kind("binary") add_includedirs("src") - add_rules("c++.unity_build", {batchsize = 2}) + add_rules("c.unity_build", {batchsize = 2}) add_files("src/*.c", "src/*.cpp") add_files("src/foo/*.c", {unity_group = "foo"}) add_files("src/bar/*.c", {unity_group = "bar"}) diff --git a/xmake/rules/c++/unity_build/unity_build.lua b/xmake/rules/c++/unity_build/unity_build.lua index e28e5aff3..96b53001e 100644 --- a/xmake/rules/c++/unity_build/unity_build.lua +++ b/xmake/rules/c++/unity_build/unity_build.lua @@ -67,7 +67,7 @@ end function main(target, sourcebatch) -- get unit batch sources - local extraconf = target:extraconf("rules", "c++.unity_build") + local extraconf = target:extraconf("rules", sourcebatch.sourcekind == "cxx" and "c++.unity_build" or "c.unity_build") local batchsize = extraconf and extraconf.batchsize local id = 1 local count = 0 diff --git a/xmake/rules/c++/unity_build/xmake.lua b/xmake/rules/c++/unity_build/xmake.lua index b03a383ae..e0dfa9660 100644 --- a/xmake/rules/c++/unity_build/xmake.lua +++ b/xmake/rules/c++/unity_build/xmake.lua @@ -18,16 +18,36 @@ -- @file xmake.lua -- +rule("c.unity_build") + after_load(function (target) + import("unity_build") + local sourcebatches = target:sourcebatches() + if sourcebatches then + local sourcebatch = sourcebatches["c.build"] + if sourcebatch then + unity_build(target, sourcebatch) + end + end + end) + before_build(function (target, opt) + import("unity_build") + local sourcebatches = target:sourcebatches() + if sourcebatches then + local sourcebatch = sourcebatches["c.build"] + if sourcebatch then + unity_build.generate_unityfiles(target, sourcebatch, opt) + end + end + end) + rule("c++.unity_build") after_load(function (target) import("unity_build") local sourcebatches = target:sourcebatches() if sourcebatches then - for _, rulename in ipairs({"c.build", "c++.build"}) do - local sourcebatch = sourcebatches[rulename] - if sourcebatch then - unity_build(target, sourcebatch) - end + local sourcebatch = sourcebatches["c++.build"] + if sourcebatch then + unity_build(target, sourcebatch) end end end) @@ -35,11 +55,9 @@ rule("c++.unity_build") import("unity_build") local sourcebatches = target:sourcebatches() if sourcebatches then - for _, rulename in ipairs({"c.build", "c++.build"}) do - local sourcebatch = sourcebatches[rulename] - if sourcebatch then - unity_build.generate_unityfiles(target, sourcebatch, opt) - end + local sourcebatch = sourcebatches["c++.build"] + if sourcebatch then + unity_build.generate_unityfiles(target, sourcebatch, opt) end end end) -- cgit v1.3.1 From 7214daac014811ca73103d0e8d22bdc2b1b54470 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 18 Oct 2021 22:18:01 +0800 Subject: add c++ unity build test --- tests/projects/c++/unity_build/src/bar/test4.cpp | 8 +++++++ tests/projects/c++/unity_build/src/bar/test5.cpp | 8 +++++++ tests/projects/c++/unity_build/src/bar/test6.cpp | 8 +++++++ tests/projects/c++/unity_build/src/foo/test.cpp | 8 +++++++ tests/projects/c++/unity_build/src/foo/test2.cpp | 8 +++++++ tests/projects/c++/unity_build/src/header.h | 27 ++++++++++++++++++++++++ tests/projects/c++/unity_build/src/header2.h | 0 tests/projects/c++/unity_build/src/main.cpp | 8 +++++++ tests/projects/c++/unity_build/src/test.c | 4 ++++ tests/projects/c++/unity_build/src/test7.cpp | 8 +++++++ tests/projects/c++/unity_build/src/test8.cpp | 8 +++++++ tests/projects/c++/unity_build/test.lua | 6 ++++++ tests/projects/c++/unity_build/xmake.lua | 9 ++++++++ 13 files changed, 110 insertions(+) create mode 100644 tests/projects/c++/unity_build/src/bar/test4.cpp create mode 100644 tests/projects/c++/unity_build/src/bar/test5.cpp create mode 100644 tests/projects/c++/unity_build/src/bar/test6.cpp create mode 100644 tests/projects/c++/unity_build/src/foo/test.cpp create mode 100644 tests/projects/c++/unity_build/src/foo/test2.cpp create mode 100644 tests/projects/c++/unity_build/src/header.h create mode 100644 tests/projects/c++/unity_build/src/header2.h create mode 100644 tests/projects/c++/unity_build/src/main.cpp create mode 100644 tests/projects/c++/unity_build/src/test.c create mode 100644 tests/projects/c++/unity_build/src/test7.cpp create mode 100644 tests/projects/c++/unity_build/src/test8.cpp create mode 100644 tests/projects/c++/unity_build/test.lua create mode 100644 tests/projects/c++/unity_build/xmake.lua diff --git a/tests/projects/c++/unity_build/src/bar/test4.cpp b/tests/projects/c++/unity_build/src/bar/test4.cpp new file mode 100644 index 000000000..a92803eb7 --- /dev/null +++ b/tests/projects/c++/unity_build/src/bar/test4.cpp @@ -0,0 +1,8 @@ + +// main.cpp +#include "header.h" + +int test4() +{ + return 0; +} diff --git a/tests/projects/c++/unity_build/src/bar/test5.cpp b/tests/projects/c++/unity_build/src/bar/test5.cpp new file mode 100644 index 000000000..2a3e7066c --- /dev/null +++ b/tests/projects/c++/unity_build/src/bar/test5.cpp @@ -0,0 +1,8 @@ + +// main.cpp +#include "header.h" + +int test5() +{ + return 0; +} diff --git a/tests/projects/c++/unity_build/src/bar/test6.cpp b/tests/projects/c++/unity_build/src/bar/test6.cpp new file mode 100644 index 000000000..efbd7c6c1 --- /dev/null +++ b/tests/projects/c++/unity_build/src/bar/test6.cpp @@ -0,0 +1,8 @@ + +// main.cpp +#include "header.h" + +int test6() +{ + return 0; +} diff --git a/tests/projects/c++/unity_build/src/foo/test.cpp b/tests/projects/c++/unity_build/src/foo/test.cpp new file mode 100644 index 000000000..e9d6c83d9 --- /dev/null +++ b/tests/projects/c++/unity_build/src/foo/test.cpp @@ -0,0 +1,8 @@ + +// main.cpp +#include "header.h" + +int test() +{ + return 0; +} diff --git a/tests/projects/c++/unity_build/src/foo/test2.cpp b/tests/projects/c++/unity_build/src/foo/test2.cpp new file mode 100644 index 000000000..596359cd2 --- /dev/null +++ b/tests/projects/c++/unity_build/src/foo/test2.cpp @@ -0,0 +1,8 @@ + +// main.cpp +#include "header.h" + +int test2() +{ + return 0; +} diff --git a/tests/projects/c++/unity_build/src/header.h b/tests/projects/c++/unity_build/src/header.h new file mode 100644 index 000000000..affda55ca --- /dev/null +++ b/tests/projects/c++/unity_build/src/header.h @@ -0,0 +1,27 @@ +// header.h +#ifndef HEADER_H +#define HEADER_H + +#include +#include +#include +#include +#include +#include +//#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#endif + diff --git a/tests/projects/c++/unity_build/src/header2.h b/tests/projects/c++/unity_build/src/header2.h new file mode 100644 index 000000000..e69de29bb diff --git a/tests/projects/c++/unity_build/src/main.cpp b/tests/projects/c++/unity_build/src/main.cpp new file mode 100644 index 000000000..27d41ee50 --- /dev/null +++ b/tests/projects/c++/unity_build/src/main.cpp @@ -0,0 +1,8 @@ +#include "header.h" + +int main(int argc, char** argv) +{ + std::string s("xmake"); + printf("hello %s!\n", s.c_str()); + return 0; +} diff --git a/tests/projects/c++/unity_build/src/test.c b/tests/projects/c++/unity_build/src/test.c new file mode 100644 index 000000000..7f0aa2bf4 --- /dev/null +++ b/tests/projects/c++/unity_build/src/test.c @@ -0,0 +1,4 @@ + +void test_c(void) +{ +} diff --git a/tests/projects/c++/unity_build/src/test7.cpp b/tests/projects/c++/unity_build/src/test7.cpp new file mode 100644 index 000000000..24aeff619 --- /dev/null +++ b/tests/projects/c++/unity_build/src/test7.cpp @@ -0,0 +1,8 @@ + +// main.cpp +#include "header.h" + +int test7() +{ + return 0; +} diff --git a/tests/projects/c++/unity_build/src/test8.cpp b/tests/projects/c++/unity_build/src/test8.cpp new file mode 100644 index 000000000..6ad5680a6 --- /dev/null +++ b/tests/projects/c++/unity_build/src/test8.cpp @@ -0,0 +1,8 @@ + +// main.cpp +#include "header.h" + +int test8() +{ + return 0; +} diff --git a/tests/projects/c++/unity_build/test.lua b/tests/projects/c++/unity_build/test.lua new file mode 100644 index 000000000..b76241be2 --- /dev/null +++ b/tests/projects/c++/unity_build/test.lua @@ -0,0 +1,6 @@ +-- main entry +function main(t) + + -- build project + t:build() +end diff --git a/tests/projects/c++/unity_build/xmake.lua b/tests/projects/c++/unity_build/xmake.lua new file mode 100644 index 000000000..ce38ae9ec --- /dev/null +++ b/tests/projects/c++/unity_build/xmake.lua @@ -0,0 +1,9 @@ +target("test") + set_kind("binary") + add_includedirs("src") + add_rules("c++.unity_build", {batchsize = 2}) + add_files("src/*.c", "src/*.cpp") + add_files("src/foo/*.cpp", {unity_group = "foo"}) + add_files("src/bar/*.cpp", {unity_group = "bar"}) + + -- cgit v1.3.1 From bc6e13ac0a28839ed45aec376fcd10acdc6488a6 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 18 Oct 2021 22:18:56 +0800 Subject: fix links --- xmake/rules/c++/unity_build/unity_build.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/xmake/rules/c++/unity_build/unity_build.lua b/xmake/rules/c++/unity_build/unity_build.lua index 96b53001e..4574011f2 100644 --- a/xmake/rules/c++/unity_build/unity_build.lua +++ b/xmake/rules/c++/unity_build/unity_build.lua @@ -109,7 +109,8 @@ function main(target, sourcebatch) end -- use unit batch - for sourcefile_unity, sourceinfo in pairs(unity_batch) do + for _, sourcefile_unity in ipairs(table.orderkeys(unity_batch)) do + local sourceinfo = unity_batch[sourcefile_unity] table.insert(sourcefiles, sourcefile_unity) table.insert(objectfiles, sourceinfo.objectfile) table.insert(dependfiles, sourceinfo.dependfile) -- cgit v1.3.1 From 7f5c18288cf287abe44e57046a80ef7db0c0b0da Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 18 Oct 2021 22:59:43 +0800 Subject: improve single batch --- xmake/rules/c++/unity_build/unity_build.lua | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/xmake/rules/c++/unity_build/unity_build.lua b/xmake/rules/c++/unity_build/unity_build.lua index 4574011f2..d73a3adf4 100644 --- a/xmake/rules/c++/unity_build/unity_build.lua +++ b/xmake/rules/c++/unity_build/unity_build.lua @@ -83,7 +83,7 @@ function main(target, sourcebatch) local fileconfig = target:fileconfig(sourcefile) if fileconfig and fileconfig.unity_group then sourcefile_unity = path.join(sourcedir, "unity_" .. fileconfig.unity_group .. path.extension(sourcefile)) - elseif (fileconfig and fileconfig.unity_ignored) or (batchsize == 0) then + elseif (fileconfig and fileconfig.unity_ignored) or (batchsize and batchsize <= 1) then -- we do not add these files to unity file table.insert(sourcefiles, sourcefile) table.insert(objectfiles, objectfile) @@ -111,9 +111,15 @@ function main(target, sourcebatch) -- use unit batch for _, sourcefile_unity in ipairs(table.orderkeys(unity_batch)) do local sourceinfo = unity_batch[sourcefile_unity] - table.insert(sourcefiles, sourcefile_unity) - table.insert(objectfiles, sourceinfo.objectfile) - table.insert(dependfiles, sourceinfo.dependfile) + if #sourceinfo.sourcefiles > 1 then + table.insert(sourcefiles, sourcefile_unity) + table.insert(objectfiles, sourceinfo.objectfile) + table.insert(dependfiles, sourceinfo.dependfile) + else + table.insert(sourcefiles, sourceinfo.sourcefiles[1]) + table.insert(objectfiles, sourceinfo.objectfile) + table.insert(dependfiles, sourceinfo.dependfile) + end end sourcebatch.sourcefiles = sourcefiles sourcebatch.objectfiles = objectfiles -- cgit v1.3.1 From 1ec16d7882188dd817ccb23be43359aa4d045d24 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 19 Oct 2021 00:06:00 +0800 Subject: add uniqueid --- tests/projects/c++/unity_build/src/bar/test4.cpp | 6 +++++- tests/projects/c++/unity_build/src/bar/test5.cpp | 7 ++++++- tests/projects/c++/unity_build/xmake.lua | 2 +- xmake/rules/c++/unity_build/unity_build.lua | 11 +++++++++++ 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/tests/projects/c++/unity_build/src/bar/test4.cpp b/tests/projects/c++/unity_build/src/bar/test4.cpp index a92803eb7..8fe732964 100644 --- a/tests/projects/c++/unity_build/src/bar/test4.cpp +++ b/tests/projects/c++/unity_build/src/bar/test4.cpp @@ -2,7 +2,11 @@ // main.cpp #include "header.h" +namespace MY_UNITY_ID { + int i = 42; +} + int test4() { - return 0; + return MY_UNITY_ID::i; } diff --git a/tests/projects/c++/unity_build/src/bar/test5.cpp b/tests/projects/c++/unity_build/src/bar/test5.cpp index 2a3e7066c..afff0b46c 100644 --- a/tests/projects/c++/unity_build/src/bar/test5.cpp +++ b/tests/projects/c++/unity_build/src/bar/test5.cpp @@ -2,7 +2,12 @@ // main.cpp #include "header.h" +namespace MY_UNITY_ID { + int i = 42; +} + int test5() { - return 0; + return MY_UNITY_ID::i; } + diff --git a/tests/projects/c++/unity_build/xmake.lua b/tests/projects/c++/unity_build/xmake.lua index ce38ae9ec..a881c5c36 100644 --- a/tests/projects/c++/unity_build/xmake.lua +++ b/tests/projects/c++/unity_build/xmake.lua @@ -1,7 +1,7 @@ target("test") set_kind("binary") add_includedirs("src") - add_rules("c++.unity_build", {batchsize = 2}) + add_rules("c++.unity_build", {batchsize = 2, uniqueid = "MY_UNITY_ID"}) add_files("src/*.c", "src/*.cpp") add_files("src/foo/*.cpp", {unity_group = "foo"}) add_files("src/bar/*.cpp", {unity_group = "bar"}) diff --git a/xmake/rules/c++/unity_build/unity_build.lua b/xmake/rules/c++/unity_build/unity_build.lua index d73a3adf4..2d8e69d5d 100644 --- a/xmake/rules/c++/unity_build/unity_build.lua +++ b/xmake/rules/c++/unity_build/unity_build.lua @@ -29,12 +29,21 @@ function _merge_unityfile(target, sourcefile_unity, sourcefiles, opt) vprint("generating.unityfile %s", sourcefile_unity) -- do merge + local uniqueid = target:data("unity_build.uniqueid") local unityfile = io.open(sourcefile_unity, "w") for _, sourcefile in ipairs(sourcefiles) do sourcefile = path.absolute(sourcefile) sourcefile_unity = path.absolute(sourcefile_unity) sourcefile = path.relative(sourcefile, path.directory(sourcefile_unity)) + if uniqueid then + unityfile:print("#ifndef %s", uniqueid) + unityfile:print("#define %s %s", uniqueid, "unity_" .. hash.uuid():split("-", {plain = true})[1]) + unityfile:print("#endif") + end unityfile:print("#include \"%s\"", sourcefile) + if uniqueid then + unityfile:print("#undef %s", uniqueid) + end end unityfile:close() @@ -69,6 +78,7 @@ function main(target, sourcebatch) -- get unit batch sources local extraconf = target:extraconf("rules", sourcebatch.sourcekind == "cxx" and "c++.unity_build" or "c.unity_build") local batchsize = extraconf and extraconf.batchsize + local uniqueid = extraconf and extraconf.uniqueid local id = 1 local count = 0 local unity_batch = {} @@ -126,5 +136,6 @@ function main(target, sourcebatch) sourcebatch.dependfiles = dependfiles -- save unit batch + target:data_set("unity_build.uniqueid", uniqueid) target:data_set("unity_build.unity_batch." .. sourcebatch.rulename, unity_batch) end -- cgit v1.3.1 From 7e6fba5fe7662b4e58ce150aeab17c70b6b12254 Mon Sep 17 00:00:00 2001 From: ruki Date: Mon, 18 Oct 2021 19:07:39 +0800 Subject: Update unity_build.lua --- xmake/rules/c++/unity_build/unity_build.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/xmake/rules/c++/unity_build/unity_build.lua b/xmake/rules/c++/unity_build/unity_build.lua index 2d8e69d5d..608f79fed 100644 --- a/xmake/rules/c++/unity_build/unity_build.lua +++ b/xmake/rules/c++/unity_build/unity_build.lua @@ -36,9 +36,7 @@ function _merge_unityfile(target, sourcefile_unity, sourcefiles, opt) sourcefile_unity = path.absolute(sourcefile_unity) sourcefile = path.relative(sourcefile, path.directory(sourcefile_unity)) if uniqueid then - unityfile:print("#ifndef %s", uniqueid) unityfile:print("#define %s %s", uniqueid, "unity_" .. hash.uuid():split("-", {plain = true})[1]) - unityfile:print("#endif") end unityfile:print("#include \"%s\"", sourcefile) if uniqueid then -- cgit v1.3.1