From bbe7745ad8ccde3f39f1c1e468fd2d96ca7cb3f5 Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Mon, 22 Jul 2024 15:21:28 +0200 Subject: [PATCH 01/16] strip -fmodule-mapper for GCC module reusage flag compatibility detection --- xmake/rules/c++/modules/modules_support/gcc/compiler_support.lua | 1 + 1 file changed, 1 insertion(+) 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"}) From af19722ebd25a7c96b3e3b042e4e48cb690b5e63 Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Mon, 22 Jul 2024 18:25:16 +0200 Subject: [PATCH 02/16] improve moduleonly support --- .../c++/modules/packages/my-repo/packages/b/bar2/xmake.lua | 2 +- xmake/actions/package/remote/main.lua | 3 +++ xmake/core/package/package.lua | 7 ++++++- xmake/modules/private/action/require/impl/package.lua | 4 ++-- xmake/rules/c++/modules/xmake.lua | 4 ++-- 5 files changed, 14 insertions(+), 6 deletions(-) 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/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 b4249c6a08a..174e4eb8861 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() @@ -2399,7 +2404,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/xmake.lua b/xmake/rules/c++/modules/xmake.lua index 90ba66c815d..2fed097448b 100644 --- a/xmake/rules/c++/modules/xmake.lua +++ b/xmake/rules/c++/modules/xmake.lua @@ -97,7 +97,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 @@ -160,7 +160,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 From 6bc66f276570973aced27afc6dd56cc6ce5b2d3b Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Mon, 22 Jul 2024 18:25:35 +0200 Subject: [PATCH 03/16] add staticlib2 test --- .../c++/modules/staticlib2/src/bar.cpp | 8 ++++++++ .../c++/modules/staticlib2/src/bar.mpp | 6 ++++++ .../c++/modules/staticlib2/src/foo.cpp | 10 ++++++++++ .../c++/modules/staticlib2/src/foo.mpp | 6 ++++++ .../c++/modules/staticlib2/src/main.cpp | 8 ++++++++ tests/projects/c++/modules/staticlib2/test.lua | 1 + .../projects/c++/modules/staticlib2/xmake.lua | 18 ++++++++++++++++++ 7 files changed, 57 insertions(+) create mode 100644 tests/projects/c++/modules/staticlib2/src/bar.cpp create mode 100644 tests/projects/c++/modules/staticlib2/src/bar.mpp create mode 100644 tests/projects/c++/modules/staticlib2/src/foo.cpp create mode 100644 tests/projects/c++/modules/staticlib2/src/foo.mpp create mode 100644 tests/projects/c++/modules/staticlib2/src/main.cpp create mode 100644 tests/projects/c++/modules/staticlib2/test.lua create mode 100644 tests/projects/c++/modules/staticlib2/xmake.lua 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/staticlib2/test.lua b/tests/projects/c++/modules/staticlib2/test.lua new file mode 100644 index 00000000000..7717f804997 --- /dev/null +++ b/tests/projects/c++/modules/staticlib2/test.lua @@ -0,0 +1 @@ +inherit(".test_base") 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") From 4f2412e3a790cc0e240eb8bc6f0caa54170acbab Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Mon, 22 Jul 2024 18:28:48 +0200 Subject: [PATCH 04/16] improve compilation of modules based on if it is from a package, an other target or an internal module --- .../modules/modules_support/clang/builder.lua | 17 ++++++------- .../modules_support/compiler_support.lua | 9 ++++--- .../modules_support/dependency_scanner.lua | 13 +++++++--- .../modules/modules_support/gcc/builder.lua | 25 +++++++++---------- .../modules/modules_support/msvc/builder.lua | 16 ++++++------ xmake/rules/c++/modules/xmake.lua | 4 --- 6 files changed, 40 insertions(+), 44 deletions(-) diff --git a/xmake/rules/c++/modules/modules_support/clang/builder.lua b/xmake/rules/c++/modules/modules_support/clang/builder.lua index b10a47e06e7..eda23566c0b 100644 --- a/xmake/rules/c++/modules/modules_support/clang/builder.lua +++ b/xmake/rules/c++/modules/modules_support/clang/builder.lua @@ -248,9 +248,14 @@ 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 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 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) @@ -258,14 +263,6 @@ function make_module_buildjobs(target, batchjobs, job_name, deps, opt) 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 - 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 end else os.tryrm(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 72e7b93fd27..2e4d1f5efcc 100644 --- a/xmake/rules/c++/modules/modules_support/compiler_support.lua +++ b/xmake/rules/c++/modules/modules_support/compiler_support.lua @@ -87,9 +87,9 @@ end function cull_objectfiles(target, modules, sourcebatch) -- don't cull for executables - if target:is_binary() then - return - end + -- if target:is_binary() then + -- return + -- end sourcebatch.objectfiles = {} for _, sourcefile in ipairs(sourcebatch.sourcefiles) do @@ -100,8 +100,9 @@ function cull_objectfiles(target, modules, sourcebatch) local fileconfig = target:fileconfig(sourcefile) local public = fileconfig and fileconfig.public local external = fileconfig and fileconfig.external + local from_moduleonly = external and external.moduleonly local private_dep = fileconfig and fileconfig.private_dep - if (not public and not external) or (external and private_dep) then + if not external or from_moduleonly then table.insert(sourcebatch.objectfiles, objectfile) end else diff --git a/xmake/rules/c++/modules/modules_support/dependency_scanner.lua b/xmake/rules/c++/modules/modules_support/dependency_scanner.lua index 66b00f421fc..44a3da980ad 100644 --- a/xmake/rules/c++/modules/modules_support/dependency_scanner.lua +++ b/xmake/rules/c++/modules/modules_support/dependency_scanner.lua @@ -200,7 +200,9 @@ 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 package_data = package["_INFO"] + local moduleonly = not package_data["libfiles"] + package_modules[name] = {file = path.join(modulesdir, modulefile), metadata = metadata, external = {moduleonly = moduleonly}} end return package_modules @@ -427,10 +429,12 @@ function sort_modules_by_dependencies(target, objectfiles, modules) local _, provide, cppfile = compiler_support.get_provided_module(modules[objectfile]) local fileconfig = target:fileconfig(cppfile) local public = fileconfig and fileconfig.public + local external = fileconfig and fileconfig.external + local from_moduleonly = external and external.moduleonly local dont_cull = fileconfig and fileconfig.cull ~= nil and not fileconfig.cull - if not provide or public or dont_cull then + if not provide or public or from_moduleonly or dont_cull then table.insert(result, objectfile) - else + elseif not external and not dont_cull then 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) end else @@ -438,6 +442,7 @@ function sort_modules_by_dependencies(target, objectfiles, modules) end end end + print(target:name(), result) return result end @@ -454,7 +459,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()}, private_dep = private_dep}) 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..cbb8472a038 100644 --- a/xmake/rules/c++/modules/modules_support/gcc/builder.lua +++ b/xmake/rules/c++/modules/modules_support/gcc/builder.lua @@ -215,11 +215,18 @@ 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 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 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 @@ -227,21 +234,13 @@ function make_module_buildjobs(target, batchjobs, job_name, deps, opt) 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 - 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 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 diff --git a/xmake/rules/c++/modules/modules_support/msvc/builder.lua b/xmake/rules/c++/modules/modules_support/msvc/builder.lua index ccdbea141a6..2d79e48ac43 100644 --- a/xmake/rules/c++/modules/modules_support/msvc/builder.lua +++ b/xmake/rules/c++/modules/modules_support/msvc/builder.lua @@ -313,8 +313,14 @@ function make_module_buildjobs(target, batchjobs, job_name, deps, opt) 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 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 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) @@ -322,14 +328,6 @@ function make_module_buildjobs(target, batchjobs, job_name, deps, opt) 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 - 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 end else os.tryrm(opt.objectfile) -- force rebuild for .cpp files diff --git a/xmake/rules/c++/modules/xmake.lua b/xmake/rules/c++/modules/xmake.lua index 2fed097448b..38d1bba4d77 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 @@ -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 From ddf9c7253f9597d2fb96ec24f4f974df8070cbb0 Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Mon, 22 Jul 2024 18:48:42 +0200 Subject: [PATCH 05/16] remove print --- xmake/rules/c++/modules/modules_support/dependency_scanner.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/xmake/rules/c++/modules/modules_support/dependency_scanner.lua b/xmake/rules/c++/modules/modules_support/dependency_scanner.lua index 44a3da980ad..68b25c07742 100644 --- a/xmake/rules/c++/modules/modules_support/dependency_scanner.lua +++ b/xmake/rules/c++/modules/modules_support/dependency_scanner.lua @@ -442,7 +442,6 @@ function sort_modules_by_dependencies(target, objectfiles, modules) end end end - print(target:name(), result) return result end From 261185f6819174884debb649cf7f7678b8130b2a Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Mon, 22 Jul 2024 19:03:17 +0200 Subject: [PATCH 06/16] fix module handling for batchcmds and some cleanups --- .../modules/modules_support/clang/builder.lua | 18 ++++++------- .../modules_support/compiler_support.lua | 6 ----- .../modules_support/dependency_scanner.lua | 3 +-- .../modules/modules_support/gcc/builder.lua | 25 +++++++++---------- .../modules/modules_support/msvc/builder.lua | 18 ++++++------- 5 files changed, 27 insertions(+), 43 deletions(-) diff --git a/xmake/rules/c++/modules/modules_support/clang/builder.lua b/xmake/rules/c++/modules/modules_support/clang/builder.lua index eda23566c0b..174599229d1 100644 --- a/xmake/rules/c++/modules/modules_support/clang/builder.lua +++ b/xmake/rules/c++/modules/modules_support/clang/builder.lua @@ -299,24 +299,20 @@ 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 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 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}) + _compile_objectfile_step(target, bmifile, opt.cppfile, opt.objectfile, {std = (name == "std" or name == "std.compat"), 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 - 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 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 2e4d1f5efcc..ecc08535845 100644 --- a/xmake/rules/c++/modules/modules_support/compiler_support.lua +++ b/xmake/rules/c++/modules/modules_support/compiler_support.lua @@ -86,11 +86,6 @@ 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) @@ -101,7 +96,6 @@ function cull_objectfiles(target, modules, sourcebatch) local public = fileconfig and fileconfig.public local external = fileconfig and fileconfig.external local from_moduleonly = external and external.moduleonly - local private_dep = fileconfig and fileconfig.private_dep if not external or from_moduleonly then table.insert(sourcebatch.objectfiles, objectfile) end diff --git a/xmake/rules/c++/modules/modules_support/dependency_scanner.lua b/xmake/rules/c++/modules/modules_support/dependency_scanner.lua index 68b25c07742..fe6e0fc9f4e 100644 --- a/xmake/rules/c++/modules/modules_support/dependency_scanner.lua +++ b/xmake/rules/c++/modules/modules_support/dependency_scanner.lua @@ -450,7 +450,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) @@ -458,7 +457,7 @@ function get_targetdeps_modules(target) if public then sourcefiles = sourcefiles or {} table.insert(sourcefiles, sourcefile) - target:fileconfig_add(sourcefile, {external = {moduleonly = dep:is_moduleonly()}, 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 cbb8472a038..5866ad04499 100644 --- a/xmake/rules/c++/modules/modules_support/gcc/builder.lua +++ b/xmake/rules/c++/modules/modules_support/gcc/builder.lua @@ -279,11 +279,18 @@ 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 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 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 @@ -291,21 +298,13 @@ function make_module_buildcmds(target, batchcmds, opt) 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 - 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 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/msvc/builder.lua b/xmake/rules/c++/modules/modules_support/msvc/builder.lua index 2d79e48ac43..737721bcdc0 100644 --- a/xmake/rules/c++/modules/modules_support/msvc/builder.lua +++ b/xmake/rules/c++/modules/modules_support/msvc/builder.lua @@ -312,7 +312,6 @@ 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 external and not from_moduleonly then @@ -364,9 +363,14 @@ 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 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 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}) @@ -374,14 +378,6 @@ function make_module_buildcmds(target, batchcmds, opt) 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 - 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 end else batchcmds:rm(opt.objectfile) -- force rebuild for .cpp files From ab6ee69bce2bd78596f4713ec4300301e025a56f Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Mon, 22 Jul 2024 20:19:56 +0200 Subject: [PATCH 07/16] simplify objectfile handling --- .../modules/modules_support/clang/builder.lua | 30 ++++--------------- .../modules_support/compiler_support.lua | 5 ++-- .../modules_support/dependency_scanner.lua | 4 +-- .../modules/modules_support/gcc/builder.lua | 30 ++++--------------- .../modules/modules_support/msvc/builder.lua | 30 ++++--------------- 5 files changed, 23 insertions(+), 76 deletions(-) diff --git a/xmake/rules/c++/modules/modules_support/clang/builder.lua b/xmake/rules/c++/modules/modules_support/clang/builder.lua index 174599229d1..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 @@ -256,13 +252,8 @@ function make_module_buildjobs(target, batchjobs, job_name, deps, opt) _compile_bmi_step(target, bmifile, opt.cppfile, {std = (name == "std" or name == "std.compat")}) end else - 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 + 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 @@ -280,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 @@ -306,13 +293,8 @@ function make_module_buildcmds(target, batchcmds, opt) _compile_bmi_step(target, bmifile, opt.cppfile, {std = (name == "std" or name == "std.compat"), batchcmds = batchcmds}) end else - 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, {std = (name == "std" or name == "std.compat"), 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 + 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 ecc08535845..8e9cf7ab6fe 100644 --- a/xmake/rules/c++/modules/modules_support/compiler_support.lua +++ b/xmake/rules/c++/modules/modules_support/compiler_support.lua @@ -90,13 +90,14 @@ function cull_objectfiles(target, modules, sourcebatch) for _, sourcefile in ipairs(sourcebatch.sourcefiles) do local objectfile = target:objectfile(sourcefile) local module = modules[objectfile] - local _, provide, _ = get_provided_module(module) + local name, 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 from_moduleonly = external and external.moduleonly - if not external or from_moduleonly then + local dont_cull = fileconfig and fileconfig.cull ~= nil and not fileconfig.cull + if (provide and not external) or public or from_moduleonly or dont_cull then table.insert(sourcebatch.objectfiles, objectfile) end else diff --git a/xmake/rules/c++/modules/modules_support/dependency_scanner.lua b/xmake/rules/c++/modules/modules_support/dependency_scanner.lua index fe6e0fc9f4e..c67e18a797a 100644 --- a/xmake/rules/c++/modules/modules_support/dependency_scanner.lua +++ b/xmake/rules/c++/modules/modules_support/dependency_scanner.lua @@ -434,8 +434,8 @@ function sort_modules_by_dependencies(target, objectfiles, modules) local dont_cull = fileconfig and fileconfig.cull ~= nil and not fileconfig.cull if not provide or public or from_moduleonly or dont_cull then table.insert(result, objectfile) - elseif not external and not dont_cull then - 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) + elseif not external then + wprint("%s has been culled because it's not consumed by its target (%s) nor flagged as a public module (add_files(\"xxx.cppm\", {public = true}))", cppfile, target:name()) end else table.insert(result, objectfile) diff --git a/xmake/rules/c++/modules/modules_support/gcc/builder.lua b/xmake/rules/c++/modules/modules_support/gcc/builder.lua index 5866ad04499..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 @@ -227,13 +223,8 @@ function make_module_buildjobs(target, batchjobs, job_name, deps, opt) sourcefile = opt.cppfile end else - 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 + 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)) @@ -259,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 @@ -291,13 +278,8 @@ function make_module_buildcmds(target, batchcmds, opt) sourcefile = opt.cppfile end else - 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 + 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)) diff --git a/xmake/rules/c++/modules/modules_support/msvc/builder.lua b/xmake/rules/c++/modules/modules_support/msvc/builder.lua index 737721bcdc0..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 @@ -320,13 +316,8 @@ function make_module_buildjobs(target, batchjobs, job_name, deps, opt) _compile_bmi_step(target, bmifile, opt.cppfile, opt.objectfile, provide) end else - 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 + 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 @@ -344,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 @@ -371,13 +358,8 @@ function make_module_buildcmds(target, batchcmds, opt) _compile_bmi_step(target, bmifile, opt.cppfile, provide, {batchcmds = batchcmds}) end else - 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 + 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 From d84b42ace51b952a7433c2d966b5a3cc763717dc Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Tue, 23 Jul 2024 22:00:27 +0200 Subject: [PATCH 08/16] remove unnecessary code culling is already done by dependency scanner --- .../modules_support/compiler_support.lua | 23 ------------------- .../modules_support/dependency_scanner.lua | 6 ++--- xmake/rules/c++/modules/xmake.lua | 6 ----- 3 files changed, 3 insertions(+), 32 deletions(-) diff --git a/xmake/rules/c++/modules/modules_support/compiler_support.lua b/xmake/rules/c++/modules/modules_support/compiler_support.lua index 8e9cf7ab6fe..e6cd32bbc34 100644 --- a/xmake/rules/c++/modules/modules_support/compiler_support.lua +++ b/xmake/rules/c++/modules/modules_support/compiler_support.lua @@ -83,29 +83,6 @@ function patch_sourcebatch(target, sourcebatch) end end --- cull sourcebatch objectfiles -function cull_objectfiles(target, modules, sourcebatch) - - sourcebatch.objectfiles = {} - for _, sourcefile in ipairs(sourcebatch.sourcefiles) do - local objectfile = target:objectfile(sourcefile) - local module = modules[objectfile] - local name, 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 from_moduleonly = external and external.moduleonly - local dont_cull = fileconfig and fileconfig.cull ~= nil and not fileconfig.cull - if (provide and not external) or public or from_moduleonly or dont_cull 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 c67e18a797a..7db818cf4a2 100644 --- a/xmake/rules/c++/modules/modules_support/dependency_scanner.lua +++ b/xmake/rules/c++/modules/modules_support/dependency_scanner.lua @@ -430,11 +430,11 @@ function sort_modules_by_dependencies(target, objectfiles, modules) local fileconfig = target:fileconfig(cppfile) local public = fileconfig and fileconfig.public local external = fileconfig and fileconfig.external - local from_moduleonly = external and external.moduleonly + local moduleonly = external and external.moduleonly local dont_cull = fileconfig and fileconfig.cull ~= nil and not fileconfig.cull - if not provide or public or from_moduleonly or dont_cull then + if not provide or public or moduleonly or dont_cull then table.insert(result, objectfile) - elseif not external then + else wprint("%s has been culled because it's not consumed by its target (%s) nor flagged as a public module (add_files(\"xxx.cppm\", {public = true}))", cppfile, target:name()) end else diff --git a/xmake/rules/c++/modules/xmake.lua b/xmake/rules/c++/modules/xmake.lua index 38d1bba4d77..67a4a1497de 100644 --- a/xmake/rules/c++/modules/xmake.lua +++ b/xmake/rules/c++/modules/xmake.lua @@ -114,9 +114,6 @@ 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) else sourcebatch.objectfiles = {} end @@ -175,9 +172,6 @@ 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) else -- avoid duplicate linking of object files of non-module programs sourcebatch.objectfiles = {} From df5cb9cce6cbb757c12f07677edf27b4be219f48 Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Tue, 23 Jul 2024 23:25:48 +0200 Subject: [PATCH 09/16] prompt an error when multiple module with the same names are detected --- .../src/bar.mpp | 2 +- .../src/foo.mpp | 2 +- .../src/main.cpp | 2 +- .../modules/duplicate_name_detection/test.lua | 70 +++++++++++++++++++ .../xmake.lua | 4 +- .../projects/c++/modules/link_order/test.lua | 1 - .../modules_support/dependency_scanner.lua | 11 ++- 7 files changed, 83 insertions(+), 9 deletions(-) rename tests/projects/c++/modules/{link_order => duplicate_name_detection}/src/bar.mpp (60%) rename tests/projects/c++/modules/{link_order => duplicate_name_detection}/src/foo.mpp (60%) rename tests/projects/c++/modules/{link_order => duplicate_name_detection}/src/main.cpp (66%) create mode 100644 tests/projects/c++/modules/duplicate_name_detection/test.lua rename tests/projects/c++/modules/{link_order => duplicate_name_detection}/xmake.lua (83%) delete mode 100644 tests/projects/c++/modules/link_order/test.lua 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..5215735b550 --- /dev/null +++ b/tests/projects/c++/modules/duplicate_name_detection/test.lua @@ -0,0 +1,70 @@ +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.iorun("xmake -rvD") + else + os.iorun("xmake -r") + end + end, catch {function(errors) + errors = errors.errors + if not errors or not errors:find("detected") then + raise("Modules duplicate name detection does not work\n%s", errors.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/link_order/test.lua b/tests/projects/c++/modules/link_order/test.lua deleted file mode 100644 index 7717f804997..00000000000 --- a/tests/projects/c++/modules/link_order/test.lua +++ /dev/null @@ -1 +0,0 @@ -inherit(".test_base") diff --git a/xmake/rules/c++/modules/modules_support/dependency_scanner.lua b/xmake/rules/c++/modules/modules_support/dependency_scanner.lua index 7db818cf4a2..fe544ee7707 100644 --- a/xmake/rules/c++/modules/modules_support/dependency_scanner.lua +++ b/xmake/rules/c++/modules/modules_support/dependency_scanner.lua @@ -175,15 +175,21 @@ end -- generate edges for DAG function _get_edges(nodes, modules) local edges = {} + local module_names = {} + local named_module_names = hashset.new() for _, node in ipairs(nodes) do local module = modules[node] + local module_name, _, _ = compiler_support.get_provided_module(module) + if module_name then + assert(not named_module_names:has(module_name), "duplicate module name detected \"" .. module_name .. "\"") + named_module_names:insert(module_name) + 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,8 +206,7 @@ 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) - local package_data = package["_INFO"] - local moduleonly = not package_data["libfiles"] + local moduleonly = not package:libraryfiles() package_modules[name] = {file = path.join(modulesdir, modulefile), metadata = metadata, external = {moduleonly = moduleonly}} end From e2fb5383a099652e2ac29b139a60e3d723d66113 Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Wed, 24 Jul 2024 02:29:45 +0200 Subject: [PATCH 10/16] fix objectfiles culling --- .../modules_support/dependency_scanner.lua | 71 +++++++++++++------ xmake/rules/c++/modules/xmake.lua | 10 ++- 2 files changed, 56 insertions(+), 25 deletions(-) diff --git a/xmake/rules/c++/modules/modules_support/dependency_scanner.lua b/xmake/rules/c++/modules/modules_support/dependency_scanner.lua index fe544ee7707..f4ac7771299 100644 --- a/xmake/rules/c++/modules/modules_support/dependency_scanner.lua +++ b/xmake/rules/c++/modules/modules_support/dependency_scanner.lua @@ -176,13 +176,17 @@ end function _get_edges(nodes, modules) local edges = {} local module_names = {} + local name_filemap = {} local named_module_names = hashset.new() - for _, node in ipairs(nodes) do + for _, node in ipairs(table.unique(nodes)) do local module = modules[node] - local module_name, _, _ = compiler_support.get_provided_module(module) + local module_name, _, cppfile = compiler_support.get_provided_module(module) if module_name then - assert(not named_module_names:has(module_name), "duplicate module name detected \"" .. module_name .. "\"") + 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 @@ -403,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 @@ -421,33 +426,53 @@ 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 + 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 = fileconfig and fileconfig.public + local external = fileconfig and fileconfig.external + local can_cull = (fileconfig and fileconfig.cull ~= nil) and fileconfig.cull or true + 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 external = fileconfig and fileconfig.external - local moduleonly = external and external.moduleonly - local dont_cull = fileconfig and fileconfig.cull ~= nil and not fileconfig.cull - if not provide or public or moduleonly or dont_cull then - table.insert(result, objectfile) - else - wprint("%s has been culled because it's not consumed by its target (%s) nor flagged as a public module (add_files(\"xxx.cppm\", {public = true}))", cppfile, target:name()) + 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 + wprint("%s has been culled because it's not consumed by its target (%s) nor flagged as a public module (add_files(\"%s\", {public = true}))", name, target:name(), path.filename(cppfile)) end end end - return result + + return build_objectfiles, link_objectfiles end -- get source modulefile for external target deps diff --git a/xmake/rules/c++/modules/xmake.lua b/xmake/rules/c++/modules/xmake.lua index 67a4a1497de..7fa967b0160 100644 --- a/xmake/rules/c++/modules/xmake.lua +++ b/xmake/rules/c++/modules/xmake.lua @@ -107,13 +107,16 @@ 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) -- build headerunits and we need to do it before building modules builder.build_headerunits_for_batchjobs(target, batchjobs, sourcebatch, modules, opt) + + sourcebatch.objectfiles = link_objectfiles else sourcebatch.objectfiles = {} end @@ -165,13 +168,16 @@ 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) -- build modules builder.build_modules_for_batchcmds(target, batchcmds, sourcebatch, modules, opt) + + sourcebatch.objectfiles = link_objectfiles else -- avoid duplicate linking of object files of non-module programs sourcebatch.objectfiles = {} From d7e9d430ccc5de6162f9f361983d1f50a16a179b Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Wed, 24 Jul 2024 02:29:57 +0200 Subject: [PATCH 11/16] improve module culling tests --- tests/projects/c++/modules/culling/test.lua | 70 +++++++++++++++++++- tests/projects/c++/modules/culling2/test.lua | 70 +++++++++++++++++++- 2 files changed, 138 insertions(+), 2 deletions(-) diff --git a/tests/projects/c++/modules/culling/test.lua b/tests/projects/c++/modules/culling/test.lua index 7717f804997..91c19553ca7 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 -r") + 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..91c19553ca7 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 -r") + 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 From 61a90f5cd7c5e9882378127f88c809a890f1a8d8 Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Wed, 24 Jul 2024 02:40:14 +0200 Subject: [PATCH 12/16] fix objectfiles culling --- .../modules/modules_support/dependency_scanner.lua | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/xmake/rules/c++/modules/modules_support/dependency_scanner.lua b/xmake/rules/c++/modules/modules_support/dependency_scanner.lua index f4ac7771299..c2dce12036b 100644 --- a/xmake/rules/c++/modules/modules_support/dependency_scanner.lua +++ b/xmake/rules/c++/modules/modules_support/dependency_scanner.lua @@ -438,9 +438,14 @@ function sort_modules_by_dependencies(target, objectfiles, modules, opt) 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 = fileconfig and fileconfig.public - local external = fileconfig and fileconfig.external - local can_cull = (fileconfig and fileconfig.cull ~= nil) and fileconfig.cull or true + 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 From 14f95cbe61ff9c652810913f54e06bcd49465e58 Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Wed, 24 Jul 2024 02:42:17 +0200 Subject: [PATCH 13/16] improve culling tests --- tests/projects/c++/modules/culling/test.lua | 2 +- tests/projects/c++/modules/culling2/test.lua | 2 +- .../c++/modules/culling3/src/hello.mpp | 10 +++ tests/projects/c++/modules/culling3/test.lua | 69 +++++++++++++++++++ tests/projects/c++/modules/culling3/xmake.lua | 6 ++ 5 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 tests/projects/c++/modules/culling3/src/hello.mpp create mode 100644 tests/projects/c++/modules/culling3/test.lua create mode 100644 tests/projects/c++/modules/culling3/xmake.lua diff --git a/tests/projects/c++/modules/culling/test.lua b/tests/projects/c++/modules/culling/test.lua index 91c19553ca7..bc72c4add2c 100644 --- a/tests/projects/c++/modules/culling/test.lua +++ b/tests/projects/c++/modules/culling/test.lua @@ -7,7 +7,7 @@ function _build() if ci_is_running() then outdata = os.iorun("xmake -rvD") else - outdata = os.iorun("xmake -r") + outdata = os.iorun("xmake -rv") end if outdata then if outdata:find("culled") then diff --git a/tests/projects/c++/modules/culling2/test.lua b/tests/projects/c++/modules/culling2/test.lua index 91c19553ca7..bc72c4add2c 100644 --- a/tests/projects/c++/modules/culling2/test.lua +++ b/tests/projects/c++/modules/culling2/test.lua @@ -7,7 +7,7 @@ function _build() if ci_is_running() then outdata = os.iorun("xmake -rvD") else - outdata = os.iorun("xmake -r") + outdata = os.iorun("xmake -rv") end if outdata then if outdata:find("culled") then 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") From 4f359b270da52a3f2a9c6a6e30a550342896f21e Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Wed, 24 Jul 2024 03:55:17 +0200 Subject: [PATCH 14/16] improve warning when modules get culled --- .../modules_support/dependency_scanner.lua | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/xmake/rules/c++/modules/modules_support/dependency_scanner.lua b/xmake/rules/c++/modules/modules_support/dependency_scanner.lua index c2dce12036b..c8da6793a0e 100644 --- a/xmake/rules/c++/modules/modules_support/dependency_scanner.lua +++ b/xmake/rules/c++/modules/modules_support/dependency_scanner.lua @@ -435,6 +435,7 @@ function sort_modules_by_dependencies(target, objectfiles, modules, opt) 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) @@ -472,11 +473,26 @@ function sort_modules_by_dependencies(target, objectfiles, modules, opt) else objectfiles_sorted_set:remove(objectfile) if name ~= "std" and name ~= "std.compat" then - wprint("%s has been culled because it's not consumed by its target (%s) nor flagged as a public module (add_files(\"%s\", {public = true}))", name, target:name(), path.filename(cppfile)) + 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 build_objectfiles, link_objectfiles end From dcd0c80fbde856820ed204d2b04fd51b3aa8ffae Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 24 Jul 2024 23:37:54 +0800 Subject: [PATCH 15/16] Update test.lua --- .../modules/duplicate_name_detection/test.lua | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/tests/projects/c++/modules/duplicate_name_detection/test.lua b/tests/projects/c++/modules/duplicate_name_detection/test.lua index 5215735b550..34a153037f0 100644 --- a/tests/projects/c++/modules/duplicate_name_detection/test.lua +++ b/tests/projects/c++/modules/duplicate_name_detection/test.lua @@ -3,18 +3,23 @@ import("core.base.semver") import("utils.ci.is_running", {alias = "ci_is_running"}) function _build() - try {function() - if ci_is_running() then - os.iorun("xmake -rvD") - else - os.iorun("xmake -r") - end - end, catch {function(errors) - errors = errors.errors - if not errors or not errors:find("detected") then - raise("Modules duplicate name detection does not work\n%s", errors.errors) - end - end}} + try { + function() + if ci_is_running() then + os.iorun("xmake -rvD") + else + os.iorun("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() From 626b80e46c371a2723f204e2412339e73d6cbece Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 24 Jul 2024 23:48:27 +0800 Subject: [PATCH 16/16] Update test.lua --- tests/projects/c++/modules/duplicate_name_detection/test.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/projects/c++/modules/duplicate_name_detection/test.lua b/tests/projects/c++/modules/duplicate_name_detection/test.lua index 34a153037f0..53033dd7f6c 100644 --- a/tests/projects/c++/modules/duplicate_name_detection/test.lua +++ b/tests/projects/c++/modules/duplicate_name_detection/test.lua @@ -6,9 +6,9 @@ function _build() try { function() if ci_is_running() then - os.iorun("xmake -rvD") + os.run("xmake -rvD") else - os.iorun("xmake -r") + os.run("xmake -r") end end, catch {