diff --git a/tests/projects/c++/modules/culling/test.lua b/tests/projects/c++/modules/culling/test.lua index 7717f804997..bc72c4add2c 100644 --- a/tests/projects/c++/modules/culling/test.lua +++ b/tests/projects/c++/modules/culling/test.lua @@ -1 +1,69 @@ -inherit(".test_base") +import("lib.detect.find_tool") +import("core.base.semver") +import("utils.ci.is_running", {alias = "ci_is_running"}) + +function _build() + local outdata + if ci_is_running() then + outdata = os.iorun("xmake -rvD") + else + outdata = os.iorun("xmake -rv") + end + if outdata then + if outdata:find("culled") then + raise("Modules culling does not work\n%s", outdata) + end + end +end + +function can_build() + if is_subhost("windows") then + return true + elseif is_subhost("msys") then + return true + elseif is_host("linux") then + local gcc = find_tool("gcc", {version = true}) + if gcc and gcc.version and semver.compare(gcc.version, "11.0") >= 0 then + return true + end + local clang = find_tool("clang", {version = true}) + if clang and clang.version and semver.compare(clang.version, "14.0") >= 0 then + return true + end + end +end + +function main(t) + if is_subhost("windows") then + local clang = find_tool("clang", {version = true}) + if clang and clang.version and semver.compare(clang.version, "17.0") >= 0 then + os.exec("xmake f --toolchain=clang -c --yes") + _build() + os.exec("xmake clean -a") + os.exec("xmake f --toolchain=clang --runtimes=c++_shared -c --yes") + _build() + end + + os.exec("xmake clean -a") + os.exec("xmake f -c --yes") + _build() + elseif is_subhost("msys") then + os.exec("xmake f -c -p mingw --yes") + _build() + elseif is_host("linux") then + local gcc = find_tool("gcc", {version = true}) + if gcc and gcc.version and semver.compare(gcc.version, "11.0") >= 0 then + os.exec("xmake f -c --yes") + _build() + end + local clang = find_tool("clang", {version = true}) + if clang and clang.version and semver.compare(clang.version, "14.0") >= 0 then + os.exec("xmake clean -a") + os.exec("xmake f --toolchain=clang -c --yes") + _build() + os.exec("xmake clean -a") + os.exec("xmake f --toolchain=clang --runtimes=c++_shared -c --yes") + _build() + end + end +end diff --git a/tests/projects/c++/modules/culling2/test.lua b/tests/projects/c++/modules/culling2/test.lua index 7717f804997..bc72c4add2c 100644 --- a/tests/projects/c++/modules/culling2/test.lua +++ b/tests/projects/c++/modules/culling2/test.lua @@ -1 +1,69 @@ -inherit(".test_base") +import("lib.detect.find_tool") +import("core.base.semver") +import("utils.ci.is_running", {alias = "ci_is_running"}) + +function _build() + local outdata + if ci_is_running() then + outdata = os.iorun("xmake -rvD") + else + outdata = os.iorun("xmake -rv") + end + if outdata then + if outdata:find("culled") then + raise("Modules culling does not work\n%s", outdata) + end + end +end + +function can_build() + if is_subhost("windows") then + return true + elseif is_subhost("msys") then + return true + elseif is_host("linux") then + local gcc = find_tool("gcc", {version = true}) + if gcc and gcc.version and semver.compare(gcc.version, "11.0") >= 0 then + return true + end + local clang = find_tool("clang", {version = true}) + if clang and clang.version and semver.compare(clang.version, "14.0") >= 0 then + return true + end + end +end + +function main(t) + if is_subhost("windows") then + local clang = find_tool("clang", {version = true}) + if clang and clang.version and semver.compare(clang.version, "17.0") >= 0 then + os.exec("xmake f --toolchain=clang -c --yes") + _build() + os.exec("xmake clean -a") + os.exec("xmake f --toolchain=clang --runtimes=c++_shared -c --yes") + _build() + end + + os.exec("xmake clean -a") + os.exec("xmake f -c --yes") + _build() + elseif is_subhost("msys") then + os.exec("xmake f -c -p mingw --yes") + _build() + elseif is_host("linux") then + local gcc = find_tool("gcc", {version = true}) + if gcc and gcc.version and semver.compare(gcc.version, "11.0") >= 0 then + os.exec("xmake f -c --yes") + _build() + end + local clang = find_tool("clang", {version = true}) + if clang and clang.version and semver.compare(clang.version, "14.0") >= 0 then + os.exec("xmake clean -a") + os.exec("xmake f --toolchain=clang -c --yes") + _build() + os.exec("xmake clean -a") + os.exec("xmake f --toolchain=clang --runtimes=c++_shared -c --yes") + _build() + end + end +end diff --git a/tests/projects/c++/modules/culling3/src/hello.mpp b/tests/projects/c++/modules/culling3/src/hello.mpp new file mode 100644 index 00000000000..124bd72bc37 --- /dev/null +++ b/tests/projects/c++/modules/culling3/src/hello.mpp @@ -0,0 +1,10 @@ +module; +#include + +export module hello; + +export namespace hello { + void say(const char* str) { + printf("%s\n", str); + } +} diff --git a/tests/projects/c++/modules/culling3/test.lua b/tests/projects/c++/modules/culling3/test.lua new file mode 100644 index 00000000000..d3865ecdce8 --- /dev/null +++ b/tests/projects/c++/modules/culling3/test.lua @@ -0,0 +1,69 @@ +import("lib.detect.find_tool") +import("core.base.semver") +import("utils.ci.is_running", {alias = "ci_is_running"}) + +function _build() + local outdata + if ci_is_running() then + outdata = os.iorun("xmake -rvD") + else + outdata = os.iorun("xmake -rv") + end + if outdata then + if not outdata:find("culled") then + raise("Modules culling does not work\n%s", outdata) + end + end +end + +function can_build() + if is_subhost("windows") then + return true + elseif is_subhost("msys") then + return true + elseif is_host("linux") then + local gcc = find_tool("gcc", {version = true}) + if gcc and gcc.version and semver.compare(gcc.version, "11.0") >= 0 then + return true + end + local clang = find_tool("clang", {version = true}) + if clang and clang.version and semver.compare(clang.version, "14.0") >= 0 then + return true + end + end +end + +function main(t) + if is_subhost("windows") then + local clang = find_tool("clang", {version = true}) + if clang and clang.version and semver.compare(clang.version, "17.0") >= 0 then + os.exec("xmake f --toolchain=clang -c --yes") + _build() + os.exec("xmake clean -a") + os.exec("xmake f --toolchain=clang --runtimes=c++_shared -c --yes") + _build() + end + + os.exec("xmake clean -a") + os.exec("xmake f -c --yes") + _build() + elseif is_subhost("msys") then + os.exec("xmake f -c -p mingw --yes") + _build() + elseif is_host("linux") then + local gcc = find_tool("gcc", {version = true}) + if gcc and gcc.version and semver.compare(gcc.version, "11.0") >= 0 then + os.exec("xmake f -c --yes") + _build() + end + local clang = find_tool("clang", {version = true}) + if clang and clang.version and semver.compare(clang.version, "14.0") >= 0 then + os.exec("xmake clean -a") + os.exec("xmake f --toolchain=clang -c --yes") + _build() + os.exec("xmake clean -a") + os.exec("xmake f --toolchain=clang --runtimes=c++_shared -c --yes") + _build() + end + end +end diff --git a/tests/projects/c++/modules/culling3/xmake.lua b/tests/projects/c++/modules/culling3/xmake.lua new file mode 100644 index 00000000000..7ec9385f3a5 --- /dev/null +++ b/tests/projects/c++/modules/culling3/xmake.lua @@ -0,0 +1,6 @@ +add_rules("mode.release", "mode.debug") +set_languages("c++20") + +target("culling") + set_kind("static") + add_files("src/*.mpp") diff --git a/tests/projects/c++/modules/link_order/src/bar.mpp b/tests/projects/c++/modules/duplicate_name_detection/src/bar.mpp similarity index 60% rename from tests/projects/c++/modules/link_order/src/bar.mpp rename to tests/projects/c++/modules/duplicate_name_detection/src/bar.mpp index dc7c14ec815..a51969b8f18 100644 --- a/tests/projects/c++/modules/link_order/src/bar.mpp +++ b/tests/projects/c++/modules/duplicate_name_detection/src/bar.mpp @@ -1,4 +1,4 @@ -export module duplicate; +export module foo; export int value() { return 1; diff --git a/tests/projects/c++/modules/link_order/src/foo.mpp b/tests/projects/c++/modules/duplicate_name_detection/src/foo.mpp similarity index 60% rename from tests/projects/c++/modules/link_order/src/foo.mpp rename to tests/projects/c++/modules/duplicate_name_detection/src/foo.mpp index eba3a4223a7..d97e5f08512 100644 --- a/tests/projects/c++/modules/link_order/src/foo.mpp +++ b/tests/projects/c++/modules/duplicate_name_detection/src/foo.mpp @@ -1,4 +1,4 @@ -export module duplicate; +export module foo; export int value() { return 0; diff --git a/tests/projects/c++/modules/link_order/src/main.cpp b/tests/projects/c++/modules/duplicate_name_detection/src/main.cpp similarity index 66% rename from tests/projects/c++/modules/link_order/src/main.cpp rename to tests/projects/c++/modules/duplicate_name_detection/src/main.cpp index cd9524d8b2a..50ddf1fdd11 100644 --- a/tests/projects/c++/modules/link_order/src/main.cpp +++ b/tests/projects/c++/modules/duplicate_name_detection/src/main.cpp @@ -1,4 +1,4 @@ -import duplicate; +import foo; int main() { return value(); diff --git a/tests/projects/c++/modules/duplicate_name_detection/test.lua b/tests/projects/c++/modules/duplicate_name_detection/test.lua new file mode 100644 index 00000000000..53033dd7f6c --- /dev/null +++ b/tests/projects/c++/modules/duplicate_name_detection/test.lua @@ -0,0 +1,75 @@ +import("lib.detect.find_tool") +import("core.base.semver") +import("utils.ci.is_running", {alias = "ci_is_running"}) + +function _build() + try { + function() + if ci_is_running() then + os.run("xmake -rvD") + else + os.run("xmake -r") + end + end, + catch { + function (errors) + errors = tostring(errors) + if not errors:find("duplicate module name detected", 1, true) then + raise("Modules duplicate name detection does not work\n%s", errors) + end + end + } + } +end + +function can_build() + if is_subhost("windows") then + return true + elseif is_subhost("msys") then + return true + elseif is_host("linux") then + local gcc = find_tool("gcc", {version = true}) + if gcc and gcc.version and semver.compare(gcc.version, "11.0") >= 0 then + return true + end + local clang = find_tool("clang", {version = true}) + if clang and clang.version and semver.compare(clang.version, "14.0") >= 0 then + return true + end + end +end + +function main(t) + if is_subhost("windows") then + local clang = find_tool("clang", {version = true}) + if clang and clang.version and semver.compare(clang.version, "17.0") >= 0 then + os.exec("xmake f --toolchain=clang -c --yes") + _build() + os.exec("xmake clean -a") + os.exec("xmake f --toolchain=clang --runtimes=c++_shared -c --yes") + _build() + end + + os.exec("xmake clean -a") + os.exec("xmake f -c --yes") + _build() + elseif is_subhost("msys") then + os.exec("xmake f -c -p mingw --yes") + _build() + elseif is_host("linux") then + local gcc = find_tool("gcc", {version = true}) +if gcc and gcc.version and semver.compare(gcc.version, "11.0") >= 0 then + os.exec("xmake f -c --yes") + _build() + end + local clang = find_tool("clang", {version = true}) + if clang and clang.version and semver.compare(clang.version, "14.0") >= 0 then + os.exec("xmake clean -a") + os.exec("xmake f --toolchain=clang -c --yes") + _build() + os.exec("xmake clean -a") + os.exec("xmake f --toolchain=clang --runtimes=c++_shared -c --yes") + _build() + end + end +end diff --git a/tests/projects/c++/modules/link_order/xmake.lua b/tests/projects/c++/modules/duplicate_name_detection/xmake.lua similarity index 83% rename from tests/projects/c++/modules/link_order/xmake.lua rename to tests/projects/c++/modules/duplicate_name_detection/xmake.lua index 4cb2af87f45..f4539695c15 100644 --- a/tests/projects/c++/modules/link_order/xmake.lua +++ b/tests/projects/c++/modules/duplicate_name_detection/xmake.lua @@ -9,12 +9,12 @@ target("bar") set_kind("moduleonly") add_files("src/bar.mpp") -target("link_order_1") +target("duplicate_name_detection_1") set_kind("binary") add_deps("foo", "bar") add_files("src/main.cpp") -target("link_order_2") +target("duplicate_name_detection_2") set_kind("binary") add_deps("bar", "foo") add_files("src/main.cpp") diff --git a/tests/projects/c++/modules/packages/my-repo/packages/b/bar2/xmake.lua b/tests/projects/c++/modules/packages/my-repo/packages/b/bar2/xmake.lua index ffa208234fb..8d7f969bc60 100644 --- a/tests/projects/c++/modules/packages/my-repo/packages/b/bar2/xmake.lua +++ b/tests/projects/c++/modules/packages/my-repo/packages/b/bar2/xmake.lua @@ -1,5 +1,5 @@ package("bar2") - set_kind("library") + set_kind("library", {moduleonly = true}) set_sourcedir(path.join(os.scriptdir(), "src")) on_install(function(package) diff --git a/tests/projects/c++/modules/staticlib2/src/bar.cpp b/tests/projects/c++/modules/staticlib2/src/bar.cpp new file mode 100644 index 00000000000..0b538509622 --- /dev/null +++ b/tests/projects/c++/modules/staticlib2/src/bar.cpp @@ -0,0 +1,8 @@ +module bar; + +namespace bar { + int hello() { + return 2; + } +} + diff --git a/tests/projects/c++/modules/staticlib2/src/bar.mpp b/tests/projects/c++/modules/staticlib2/src/bar.mpp new file mode 100644 index 00000000000..df67d52f0cb --- /dev/null +++ b/tests/projects/c++/modules/staticlib2/src/bar.mpp @@ -0,0 +1,6 @@ +export module bar; + +export namespace bar { + int hello(); +} + diff --git a/tests/projects/c++/modules/staticlib2/src/foo.cpp b/tests/projects/c++/modules/staticlib2/src/foo.cpp new file mode 100644 index 00000000000..25437128390 --- /dev/null +++ b/tests/projects/c++/modules/staticlib2/src/foo.cpp @@ -0,0 +1,10 @@ +module foo; + +import bar; + +namespace foo { + int hello() { + return bar::hello(); + } +} + diff --git a/tests/projects/c++/modules/staticlib2/src/foo.mpp b/tests/projects/c++/modules/staticlib2/src/foo.mpp new file mode 100644 index 00000000000..2f85bb9fe90 --- /dev/null +++ b/tests/projects/c++/modules/staticlib2/src/foo.mpp @@ -0,0 +1,6 @@ +export module foo; + +export namespace foo { + int hello(); +} + diff --git a/tests/projects/c++/modules/staticlib2/src/main.cpp b/tests/projects/c++/modules/staticlib2/src/main.cpp new file mode 100644 index 00000000000..69e91b1262e --- /dev/null +++ b/tests/projects/c++/modules/staticlib2/src/main.cpp @@ -0,0 +1,8 @@ +#include +import foo; + +int main() { + printf("%d\n", foo::hello()); + return 0; +} + diff --git a/tests/projects/c++/modules/link_order/test.lua b/tests/projects/c++/modules/staticlib2/test.lua similarity index 100% rename from tests/projects/c++/modules/link_order/test.lua rename to tests/projects/c++/modules/staticlib2/test.lua diff --git a/tests/projects/c++/modules/staticlib2/xmake.lua b/tests/projects/c++/modules/staticlib2/xmake.lua new file mode 100644 index 00000000000..64f0c4b0586 --- /dev/null +++ b/tests/projects/c++/modules/staticlib2/xmake.lua @@ -0,0 +1,18 @@ +add_rules("mode.release", "mode.debug") +set_languages("c++20") + +target("bar") + set_kind("static") + add_files("src/bar.mpp", {public = true}) + add_files("src/bar.cpp") + +target("foo") + set_kind("static") + add_deps("bar") + add_files("src/foo.mpp", {public = true}) + add_files("src/foo.cpp") + +target("hello") + set_kind("binary") + add_deps("foo") + add_files("src/main.cpp") diff --git a/xmake/actions/package/remote/main.lua b/xmake/actions/package/remote/main.lua index 028c1f9660e..f73126618cc 100644 --- a/xmake/actions/package/remote/main.lua +++ b/xmake/actions/package/remote/main.lua @@ -54,6 +54,8 @@ function _package_remote(target) file:print(" set_kind(\"binary\")") elseif target:is_headeronly() then file:print(" set_kind(\"library\", {headeronly = true})") + elseif target:is_moduleonly() then + file:print(" set_kind(\"library\", {moduleonly = true})") end local homepage = option.get("homepage") if homepage then @@ -122,6 +124,7 @@ function _package_target(target) , static = _package_remote , shared = _package_remote , headeronly = _package_remote + , moduleonly = _package_remote } local kind = target:kind() local script = scripts[kind] diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index fd5d5243354..fdfa59a4230 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -572,6 +572,11 @@ function _instance:is_headeronly() return self:is_library() and self:extraconf("kind", "library", "headeronly") end +-- is module only? +function _instance:is_moduleonly() + return self:is_library() and self:extraconf("kind", "library", "moduleonly") +end + -- is top level? user top requires in xmake.lua function _instance:is_toplevel() local requireinfo = self:requireinfo() @@ -2404,7 +2409,7 @@ function _instance:_generate_build_configs(configs, opt) end -- check links for library - if self:is_library() and not self:is_headeronly() then + if self:is_library() and not self:is_headeronly() and not self:is_moduleonly() then local links = table.wrap(configs.links) local ldflags = table.wrap(configs.ldflags) local frameworks = table.wrap(configs.frameworks) diff --git a/xmake/modules/private/action/require/impl/package.lua b/xmake/modules/private/action/require/impl/package.lua index 8d64b7891ca..1c7eaf9e51c 100644 --- a/xmake/modules/private/action/require/impl/package.lua +++ b/xmake/modules/private/action/require/impl/package.lua @@ -553,9 +553,9 @@ function _init_requireinfo(requireinfo, package, opt) requireinfo.configs.asan = project.policy("build.sanitizer.address") end end - -- but we will ignore some configs for buildhash in the headeronly and host/binary package + -- but we will ignore some configs for buildhash in the headeronly, moduleonly and host/binary package -- @note on_test still need these configs, @see https://github.com/xmake-io/xmake/issues/4124 - if package:is_headeronly() or (package:is_binary() and not package:is_cross()) then + if package:is_headeronly() or package:is_moduleonly() or (package:is_binary() and not package:is_cross()) then requireinfo.ignored_configs_for_buildhash = {"runtimes", "toolchains", "lto", "asan", "pic"} end end diff --git a/xmake/rules/c++/modules/modules_support/clang/builder.lua b/xmake/rules/c++/modules/modules_support/clang/builder.lua index b10a47e06e7..91534a11e3a 100644 --- a/xmake/rules/c++/modules/modules_support/clang/builder.lua +++ b/xmake/rules/c++/modules/modules_support/clang/builder.lua @@ -207,11 +207,7 @@ function make_module_buildjobs(target, batchjobs, job_name, deps, opt) job = batchjobs:newjob(name or opt.cppfile, function(index, total, jobopt) local mapped_bmi if provide and compiler_support.memcache():get2(target:name() .. name, "reuse") then - if not target:is_binary() then - return - else - mapped_bmi = get_from_target_mapper(target, name).bmi - end + mapped_bmi = get_from_target_mapper(target, name).bmi end local build, dependinfo @@ -248,24 +244,16 @@ function make_module_buildjobs(target, batchjobs, job_name, deps, opt) local fileconfig = target:fileconfig(opt.cppfile) local public = fileconfig and fileconfig.public local external = fileconfig and fileconfig.external - local private_dep = fileconfig and fileconfig.private_dep + local from_moduleonly = external and external.moduleonly local bmifile = mapped_bmi or bmifile - if target:is_binary() then - if mapped_bmi then - progress.show(jobopt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.objectfile.$(mode) %s", target:name(), name or opt.cppfile) - _compile_objectfile_step(target, bmifile, opt.cppfile, opt.objectfile) - else - progress.show(jobopt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.module.$(mode) %s", target:name(), name or opt.cppfile) - _compile_one_step(target, bmifile, opt.cppfile, opt.objectfile, {std = (name == "std" or name == "std.compat")}) - end - else - if (not public and not external) or (external and private_dep) then - progress.show(jobopt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.module.$(mode) %s", target:name(), name or opt.cppfile) - _compile_one_step(target, bmifile, opt.cppfile, opt.objectfile, {std = (name == "std" or name == "std.compat")}) - else + if external and not from_moduleonly then + if not mapped_bmi then progress.show(jobopt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.bmi.$(mode) %s", target:name(), name or opt.cppfile) _compile_bmi_step(target, bmifile, opt.cppfile, {std = (name == "std" or name == "std.compat")}) end + else + progress.show(jobopt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.module.$(mode) %s", target:name(), name or opt.cppfile) + _compile_one_step(target, bmifile, opt.cppfile, opt.objectfile, {std = (name == "std" or name == "std.compat")}) end else os.tryrm(opt.objectfile) -- force rebuild for .cpp files @@ -283,11 +271,7 @@ function make_module_buildcmds(target, batchcmds, opt) local mapped_bmi if provide and compiler_support.memcache():get2(target:name() .. name, "reuse") then - if not target:is_binary() then - return - else - mapped_bmi = get_from_target_mapper(target, name).bmi - end + mapped_bmi = get_from_target_mapper(target, name).bmi end -- append requires flags @@ -302,24 +286,15 @@ function make_module_buildcmds(target, batchcmds, opt) local fileconfig = target:fileconfig(opt.cppfile) local public = fileconfig and fileconfig.public local external = fileconfig and fileconfig.external - local private_dep = fileconfig and fileconfig.private_dep local bmifile = mapped_bmi or bmifile - if target:is_binary() then - if mapped_bmi then - batchcmds:show_progress(opt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.objectfile.$(mode) %s", target:name(), name or opt.cppfile) - _compile_objectfile_step(target, bmifile, opt.cppfile, opt.objectfile, {batchcmds = batchcmds}) - else - batchcmds:show_progress(opt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.module.$(mode) %s", target:name(), name or opt.cppfile) - _compile_one_step(target, bmifile, opt.cppfile, opt.objectfile, {std = (name == "std" or name == "std.compat"), batchcmds = batchcmds}) - end - else - if (not public and not external) or (external and private_dep) then - batchcmds:show_progress(opt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.module.$(mode) %s", target:name(), name or opt.cppfile) - _compile_one_step(target, bmifile, opt.cppfile, opt.objectfile, {std = (name == "std" or name == "std.compat"), batchcmds = batchcmds}) - else + if external and not from_moduleonly then + if not mapped_bmi then batchcmds:show_progress(opt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.bmi.$(mode) %s", target:name(), name or opt.cppfile) _compile_bmi_step(target, bmifile, opt.cppfile, {std = (name == "std" or name == "std.compat"), batchcmds = batchcmds}) end + else + batchcmds:show_progress(opt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.module.$(mode) %s", target:name(), name or opt.cppfile) + _compile_one_step(target, bmifile, opt.cppfile, opt.objectfile, {std = (name == "std" or name == "std.compat"), batchcmds = batchcmds}) end else batchcmds:rm(opt.objectfile) -- force rebuild for .cpp files diff --git a/xmake/rules/c++/modules/modules_support/compiler_support.lua b/xmake/rules/c++/modules/modules_support/compiler_support.lua index a005dc80d4f..7c4e665d099 100644 --- a/xmake/rules/c++/modules/modules_support/compiler_support.lua +++ b/xmake/rules/c++/modules/modules_support/compiler_support.lua @@ -83,33 +83,6 @@ function patch_sourcebatch(target, sourcebatch) end end --- cull sourcebatch objectfiles -function cull_objectfiles(target, modules, sourcebatch) - - -- don't cull for executables - if target:is_binary() then - return - end - - sourcebatch.objectfiles = {} - for _, sourcefile in ipairs(sourcebatch.sourcefiles) do - local objectfile = target:objectfile(sourcefile) - local module = modules[objectfile] - local _, provide, _ = get_provided_module(module) - if provide then - local fileconfig = target:fileconfig(sourcefile) - local public = fileconfig and fileconfig.public - local external = fileconfig and fileconfig.external - local private_dep = fileconfig and fileconfig.private_dep - if (not public and not external) or (external and private_dep) then - table.insert(sourcebatch.objectfiles, objectfile) - end - else - table.insert(sourcebatch.objectfiles, objectfile) - end - end -end - -- get bmi extension function get_bmi_extension(target) return _compiler_support(target).get_bmi_extension() diff --git a/xmake/rules/c++/modules/modules_support/dependency_scanner.lua b/xmake/rules/c++/modules/modules_support/dependency_scanner.lua index 66b00f421fc..c8da6793a0e 100644 --- a/xmake/rules/c++/modules/modules_support/dependency_scanner.lua +++ b/xmake/rules/c++/modules/modules_support/dependency_scanner.lua @@ -175,15 +175,25 @@ end -- generate edges for DAG function _get_edges(nodes, modules) local edges = {} - for _, node in ipairs(nodes) do + local module_names = {} + local name_filemap = {} + local named_module_names = hashset.new() + for _, node in ipairs(table.unique(nodes)) do local module = modules[node] + local module_name, _, cppfile = compiler_support.get_provided_module(module) + if module_name then + if named_module_names:has(module_name) then + raise("duplicate module name detected \"" .. module_name .. "\"\n -> " .. cppfile .. "\n -> " .. name_filemap[module_name]) + end + named_module_names:insert(module_name) + name_filemap[module_name] = cppfile + end if module.requires then for required_name, _ in table.orderpairs(module.requires) do for _, required_node in ipairs(nodes) do local name, _, _ = compiler_support.get_provided_module(modules[required_node]) if name and name == required_name then table.insert(edges, {required_node, node}) - break end end end @@ -200,7 +210,8 @@ function _get_package_modules(target, package, opt) for _, metafile in ipairs(metafiles) do package_modules = package_modules or {} local modulefile, name, metadata = _parse_meta_info(target, metafile) - package_modules[name] = {file = path.join(modulesdir, modulefile), metadata = metadata} + local moduleonly = not package:libraryfiles() + package_modules[name] = {file = path.join(modulesdir, modulefile), metadata = metadata, external = {moduleonly = moduleonly}} end return package_modules @@ -396,8 +407,9 @@ function get_all_packages_modules(target, opt) end -- topological sort -function sort_modules_by_dependencies(target, objectfiles, modules) - local result = {} +function sort_modules_by_dependencies(target, objectfiles, modules, opt) + local build_objectfiles = {} + local link_objectfiles = {} local edges = _get_edges(objectfiles, modules) local dag = graph.new(true) for _, e in ipairs(edges) do @@ -414,31 +426,74 @@ function sort_modules_by_dependencies(target, objectfiles, modules) table.insert(names, name or cppfile) raise("circular modules dependency detected!\n%s", table.concat(names, "\n -> import ")) end - local objectfiles_sorted = dag:topological_sort() - for _, objectfile in ipairs(objectfiles_sorted) do - table.insert(result, objectfile) - end + local objectfiles_sorted = table.reverse(dag:topological_sort()) local objectfiles_sorted_set = hashset.from(objectfiles_sorted) for _, objectfile in ipairs(objectfiles) do if not objectfiles_sorted_set:has(objectfile) then - if target:policy("build.c++.modules.culling") then - -- cull unreferenced non-public named module but add non-module files and implementation modules - local _, provide, cppfile = compiler_support.get_provided_module(modules[objectfile]) - local fileconfig = target:fileconfig(cppfile) + table.insert(objectfiles_sorted, objectfile) + objectfiles_sorted_set:insert(objectfile) + end + end + local culleds + for _, objectfile in ipairs(objectfiles_sorted) do + local name, provide, cppfile = compiler_support.get_provided_module(modules[objectfile]) + local fileconfig = target:fileconfig(cppfile) + local public + local external + local can_cull = true + if fileconfig then + public = fileconfig.public + external = fileconfig.external + can_cull = fileconfig.cull == nil and true or fileconfig.cull + end + can_cull = can_cull and target:policy("build.c++.modules.culling") + local insert = true + if provide then + insert = public or (not external or external.moduleonly) + if insert and not public and can_cull then + insert = false + local edges = dag:adjacent_edges(objectfile) local public = fileconfig and fileconfig.public - local dont_cull = fileconfig and fileconfig.cull ~= nil and not fileconfig.cull - if not provide or public or dont_cull then - table.insert(result, objectfile) - else - wprint("%s has been culled because it's not consumed by its target nor flagged as a public module (add_files(\"xxx.cppm\", {public = true}))", cppfile) + if edges then + for _, edge in ipairs(edges) do + if edge:to() ~= objectfile and objectfiles_sorted_set:has(edge:to()) then + insert = true + break + end + end end - else - table.insert(result, objectfile) end end + if insert then + table.insert(build_objectfiles, objectfile) + table.insert(link_objectfiles, objectfile) + elseif external and not external.from_moduleonly then + table.insert(build_objectfiles, objectfile) + else + objectfiles_sorted_set:remove(objectfile) + if name ~= "std" and name ~= "std.compat" then + culleds = culleds or {} + culleds[target:name()] = culleds[target:name()] or {} + table.insert(culleds[target:name()], format("%s -> %s", name, cppfile)) + end + end + end + + if culleds then + if option.get("verbose") then + local culled_strs = {} + for target_name, m in pairs(culleds) do + table.insert(culled_strs, format("%s:\n %s", target_name, table.concat(m, "\n "))) + end + wprint("some modules have got culled, because it is not consumed by its target nor flagged as a public module with add_files(\"xxx.mpp\", {public = true})\n %s", + table.concat(culled_strs, "\n ")) + else + wprint("some modules have got culled, use verbose (-v) mode to more informations") + end end - return result + + return build_objectfiles, link_objectfiles end -- get source modulefile for external target deps @@ -446,7 +501,6 @@ function get_targetdeps_modules(target) local sourcefiles for _, dep in ipairs(target:orderdeps()) do local sourcebatch = dep:sourcebatches()["c++.build.modules.builder"] - local private_dep = target:extraconf("deps", dep:name(), "private") or not target:extraconf("deps", dep:name(), "public") if sourcebatch and sourcebatch.sourcefiles then for _, sourcefile in ipairs(sourcebatch.sourcefiles) do local fileconfig = dep:fileconfig(sourcefile) @@ -454,7 +508,7 @@ function get_targetdeps_modules(target) if public then sourcefiles = sourcefiles or {} table.insert(sourcefiles, sourcefile) - target:fileconfig_add(sourcefile, {external = true, private_dep = private_dep}) + target:fileconfig_add(sourcefile, {external = {moduleonly = dep:is_moduleonly()}}) end end end diff --git a/xmake/rules/c++/modules/modules_support/gcc/builder.lua b/xmake/rules/c++/modules/modules_support/gcc/builder.lua index d4f5a01f1b5..a0a43db5a58 100644 --- a/xmake/rules/c++/modules/modules_support/gcc/builder.lua +++ b/xmake/rules/c++/modules/modules_support/gcc/builder.lua @@ -187,11 +187,7 @@ function make_module_buildjobs(target, batchjobs, job_name, deps, opt) job = batchjobs:newjob(name or opt.cppfile, function(index, total, jobopt) local mapped_bmi if provide and compiler_support.memcache():get2(target:name() .. name, "reuse") then - if not target:is_binary() then - return - else - mapped_bmi = get_from_target_mapper(target, name).bmi - end + mapped_bmi = get_from_target_mapper(target, name).bmi end -- generate and append module mapper file @@ -215,33 +211,27 @@ function make_module_buildjobs(target, batchjobs, job_name, deps, opt) local fileconfig = target:fileconfig(opt.cppfile) local public = fileconfig and fileconfig.public local external = fileconfig and fileconfig.external - local private_dep = fileconfig and fileconfig.private_dep + local from_moduleonly = external and external.moduleonly local bmifile = mapped_bmi or bmifile local flags = {"-x", "c++"} local sourcefile - if target:is_binary() then - if mapped_bmi then - progress.show(jobopt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.objectfile.$(mode) %s", target:name(), name or opt.cppfile) - sourcefile = bmifile - else - progress.show(jobopt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.module.$(mode) %s", target:name(), name or opt.cppfile) - sourcefile = opt.cppfile - end - else - if (not public and not external) or (external and private_dep) then - progress.show(jobopt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.module.$(mode) %s", target:name(), name or opt.cppfile) - sourcefile = opt.cppfile - else + if external and not from_moduleonly then + if not mapped_bmi then progress.show(jobopt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.bmi.$(mode) %s", target:name(), name or opt.cppfile) local module_onlyflag = compiler_support.get_moduleonlyflag(target) table.insert(flags, module_onlyflag) sourcefile = opt.cppfile end + else + progress.show(jobopt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.module.$(mode) %s", target:name(), name or opt.cppfile) + sourcefile = opt.cppfile end if option.get("diagnosis") then print("mapper file --------\n%s--------", io.readfile(module_mapper)) end - _compile(target, flags, sourcefile, opt.objectfile) + if sourcefile then + _compile(target, flags, sourcefile, opt.objectfile) + end os.tryrm(module_mapper) else os.tryrm(opt.objectfile) -- force rebuild for .cpp files @@ -260,11 +250,7 @@ function make_module_buildcmds(target, batchcmds, opt) local mapped_bmi if provide and compiler_support.memcache():get2(target:name() .. name, "reuse") then - if not target:is_binary() then - return - else - mapped_bmi = get_from_target_mapper(target, name).bmi - end + mapped_bmi = get_from_target_mapper(target, name).bmi end -- generate and append module mapper file @@ -280,33 +266,27 @@ function make_module_buildcmds(target, batchcmds, opt) local fileconfig = target:fileconfig(opt.cppfile) local public = fileconfig and fileconfig.public local external = fileconfig and fileconfig.external - local private_dep = fileconfig and fileconfig.private_dep + local from_moduleonly = external and external.moduleonly local bmifile = mapped_bmi or bmifile local flags = {"-x", "c++"} local sourcefile - if target:is_binary() then - if mapped_bmi then - batchcmds:show_progress(opt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.objectfile.$(mode) %s", target:name(), name or opt.cppfile) - sourcefile = bmifile - else - batchcmds:show_progress(opt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.module.$(mode) %s", target:name(), name or opt.cppfile) - sourcefile = opt.cppfile - end - else - if (not public and not external) or (external and private_dep) then - batchcmds:show_progress(opt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.module.$(mode) %s", target:name(), name or opt.cppfile) - sourcefile = opt.cppfile - else + if external and not from_moduleonly then + if not mapped_bmi then batchcmds:show_progress(opt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.bmi.$(mode) %s", target:name(), name or opt.cppfile) local module_onlyflag = compiler_support.get_moduleonlyflag(target) table.insert(flags, module_onlyflag) sourcefile = opt.cppfile end + else + batchcmds:show_progress(opt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.module.$(mode) %s", target:name(), name or opt.cppfile) + sourcefile = opt.cppfile end if option.get("diagnosis") then batchcmds:print("mapper file: %s", io.readfile(module_mapper)) end - _batchcmds_compile(batchcmds, target, flags, sourcefile, opt.objectfile) + if sourcefile then + _batchcmds_compile(batchcmds, target, flags, sourcefile, opt.objectfile) + end batchcmds:rm(module_mapper) else batchcmds:rm(opt.objectfile) -- force rebuild for .cpp files diff --git a/xmake/rules/c++/modules/modules_support/gcc/compiler_support.lua b/xmake/rules/c++/modules/modules_support/gcc/compiler_support.lua index 2253c986687..25e047f8a2c 100644 --- a/xmake/rules/c++/modules/modules_support/gcc/compiler_support.lua +++ b/xmake/rules/c++/modules/modules_support/gcc/compiler_support.lua @@ -77,6 +77,7 @@ function strip_flags(target, flags) "-w", "-cxx-isystem", "-Q", + "-fmodule-mapper", } if not target:policy("build.c++.modules.tryreuse.discriminate_on_defines") then table.join2(strippable_flags, {"-D", "-U"}) diff --git a/xmake/rules/c++/modules/modules_support/msvc/builder.lua b/xmake/rules/c++/modules/modules_support/msvc/builder.lua index ccdbea141a6..89d9b6b606b 100644 --- a/xmake/rules/c++/modules/modules_support/msvc/builder.lua +++ b/xmake/rules/c++/modules/modules_support/msvc/builder.lua @@ -271,11 +271,7 @@ function make_module_buildjobs(target, batchjobs, job_name, deps, opt) local mapped_bmi if provide and compiler_support.memcache():get2(target:name() .. name, "reuse") then - if not target:is_binary() then - return - else - mapped_bmi = get_from_target_mapper(target, name).bmi - end + mapped_bmi = get_from_target_mapper(target, name).bmi end local build, dependinfo @@ -312,24 +308,16 @@ function make_module_buildjobs(target, batchjobs, job_name, deps, opt) local fileconfig = target:fileconfig(opt.cppfile) local public = fileconfig and fileconfig.public local external = fileconfig and fileconfig.external - local private_dep = fileconfig and fileconfig.private_dep + local from_moduleonly = external and external.moduleonly local bmifile = mapped_bmi or bmifile - if target:is_binary() then - if mapped_bmi then - progress.show(jobopt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.objectfile.$(mode) %s", target:name(), name or opt.cppfile) - _compile_objectfile_step(target, bmifile, opt.cppfile, opt.objectfile, provide) - else - progress.show(jobopt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.module.$(mode) %s", target:name(), name or opt.cppfile) - _compile_one_step(target, bmifile, opt.cppfile, opt.objectfile, provide) - end - else - if (not public and not external) or (external and private_dep) then - progress.show(jobopt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.module.$(mode) %s", target:name(), name or opt.cppfile) - _compile_one_step(target, bmifile, opt.cppfile, opt.objectfile, provide) - else + if external and not from_moduleonly then + if not mapped_bmi then progress.show(jobopt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.bmi.$(mode) %s", target:name(), name or opt.cppfile) _compile_bmi_step(target, bmifile, opt.cppfile, opt.objectfile, provide) end + else + progress.show(jobopt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.module.$(mode) %s", target:name(), name or opt.cppfile) + _compile_one_step(target, bmifile, opt.cppfile, opt.objectfile, provide) end else os.tryrm(opt.objectfile) -- force rebuild for .cpp files @@ -347,11 +335,7 @@ function make_module_buildcmds(target, batchcmds, opt) local mapped_bmi if provide and compiler_support.memcache():get2(target:name() .. name, "reuse") then - if not target:is_binary() then - return - else - mapped_bmi = get_from_target_mapper(target, name).bmi - end + mapped_bmi = get_from_target_mapper(target, name).bmi end -- append requires flags @@ -366,24 +350,16 @@ function make_module_buildcmds(target, batchcmds, opt) local fileconfig = target:fileconfig(opt.cppfile) local public = fileconfig and fileconfig.public local external = fileconfig and fileconfig.external - local private_dep = fileconfig and fileconfig.private_dep + local from_moduleonly = external and external.moduleonly local bmifile = mapped_bmi or bmifile - if target:is_binary() then - if mapped_bmi then - batchcmds:show_progress(opt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.objectfile.$(mode) %s", target:name(), name or opt.cppfile) - _compile_objectfile_step(target, bmifile, opt.cppfile, opt.objectfile, provide, {batchcmds = batchcmds}) - else - batchcmds:show_progress(opt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.module.$(mode) %s", target:name(), name or opt.cppfile) - _compile_one_step(target, bmifile, opt.cppfile, opt.objectfile, provide, {batchcmds = batchcmds}) - end - else - if (not public and not external) or (external and private_dep) then - batchcmds:show_progress(opt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.module.$(mode) %s", target:name(), name or opt.cppfile) - _compile_one_step(target, bmifile, opt.cppfile, opt.objectfile, provide, {batchcmds = batchcmds}) - else + if external and not from_moduleonly then + if not mapped_bmi then batchcmds:show_progress(opt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.bmi.$(mode) %s", target:name(), name or opt.cppfile) _compile_bmi_step(target, bmifile, opt.cppfile, provide, {batchcmds = batchcmds}) end + else + batchcmds:show_progress(opt.progress, "${color.build.target}<%s> ${clear}${color.build.object}compiling.module.$(mode) %s", target:name(), name or opt.cppfile) + _compile_one_step(target, bmifile, opt.cppfile, opt.objectfile, provide, {batchcmds = batchcmds}) end else batchcmds:rm(opt.objectfile) -- force rebuild for .cpp files diff --git a/xmake/rules/c++/modules/xmake.lua b/xmake/rules/c++/modules/xmake.lua index d658815aa8b..4874082cfb3 100644 --- a/xmake/rules/c++/modules/xmake.lua +++ b/xmake/rules/c++/modules/xmake.lua @@ -87,8 +87,6 @@ rule("c++.build.modules.builder") local std_modules = compiler_support.get_stdmodules(target) if std_modules then table.join2(sourcebatch.sourcefiles, std_modules) - target:fileconfig_set(std_modules[1], {external = true}) - target:fileconfig_set(std_modules[2], {external = true}) end -- extract packages modules dependencies @@ -97,7 +95,7 @@ rule("c++.build.modules.builder") -- append to sourcebatch for _, package_module_data in table.orderpairs(package_modules_data) do table.insert(sourcebatch.sourcefiles, package_module_data.file) - target:fileconfig_set(package_module_data.file, {external = true, defines = package_module_data.metadata.defines}) + target:fileconfig_set(package_module_data.file, {external = package_module_data.external, defines = package_module_data.metadata.defines}) end end @@ -109,7 +107,8 @@ rule("c++.build.modules.builder") if not target:is_moduleonly() then -- avoid building non referenced modules - sourcebatch.objectfiles = dependency_scanner.sort_modules_by_dependencies(target, sourcebatch.objectfiles, modules) + local build_objectfiles, link_objectfiles = dependency_scanner.sort_modules_by_dependencies(target, sourcebatch.objectfiles, modules) + sourcebatch.objectfiles = build_objectfiles -- build modules builder.build_modules_for_batchjobs(target, batchjobs, sourcebatch, modules, opt) @@ -117,8 +116,7 @@ rule("c++.build.modules.builder") -- build headerunits and we need to do it before building modules builder.build_headerunits_for_batchjobs(target, batchjobs, sourcebatch, modules, opt) - -- cull external modules objectfile - compiler_support.cull_objectfiles(target, modules, sourcebatch) + sourcebatch.objectfiles = link_objectfiles else sourcebatch.objectfiles = {} end @@ -150,8 +148,6 @@ rule("c++.build.modules.builder") local std_modules = compiler_support.get_stdmodules(target) if std_modules then table.join2(sourcebatch.sourcefiles, std_modules) - target:fileconfig_set(std_modules[1], {external = true}) - target:fileconfig_set(std_modules[2], {external = true}) end -- extract packages modules dependencies @@ -160,7 +156,7 @@ rule("c++.build.modules.builder") -- append to sourcebatch for _, package_module_data in table.orderpairs(package_modules_data) do table.insert(sourcebatch.sourcefiles, package_module_data.file) - target:fileconfig_set(package_module_data.file, {external = true, defines = package_module_data.metadata.defines}) + target:fileconfig_set(package_module_data.file, {external = package_module_data.external, defines = package_module_data.metadata.defines}) end end @@ -172,7 +168,8 @@ rule("c++.build.modules.builder") if not target:is_moduleonly() then -- avoid building non referenced modules - sourcebatch.objectfiles = dependency_scanner.sort_modules_by_dependencies(target, sourcebatch.objectfiles, modules) + local build_objectfiles, link_objectfiles = dependency_scanner.sort_modules_by_dependencies(target, sourcebatch.objectfiles, modules) + sourcebatch.objectfiles = build_objectfiles -- build headerunits builder.build_headerunits_for_batchcmds(target, batchcmds, sourcebatch, modules, opt) @@ -180,8 +177,7 @@ rule("c++.build.modules.builder") -- build modules builder.build_modules_for_batchcmds(target, batchcmds, sourcebatch, modules, opt) - -- cull external modules objectfile - compiler_support.cull_objectfiles(target, modules, sourcebatch) + sourcebatch.objectfiles = link_objectfiles else -- avoid duplicate linking of object files of non-module programs sourcebatch.objectfiles = {}