diff options
| author | ruki <[email protected]> | 2025-09-09 13:53:02 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-09-09 13:53:02 +0800 |
| commit | e115e55c064eb6b475914989ea4d5fbaa9f4f83c (patch) | |
| tree | e8f223786841a106c686d244dda6291ed4ad48ed | |
| parent | 30f935da29e7e003b2cea2c26a4be59020fe1b47 (diff) | |
| parent | 736bc53d54e12a508af4a4bec77049a1929984c7 (diff) | |
Merge pull request #6744 from Arthapz/hide-dependency-flags2
feat(C++ modules support) use a file for requires flags when --verbose or --diagnosis is supplied
11 files changed, 137 insertions, 16 deletions
diff --git a/tests/projects/c++/modules/hide_dependency_flags/src/bar.mpp b/tests/projects/c++/modules/hide_dependency_flags/src/bar.mpp new file mode 100644 index 000000000..140924a85 --- /dev/null +++ b/tests/projects/c++/modules/hide_dependency_flags/src/bar.mpp @@ -0,0 +1,8 @@ +export module bar; +import zoo; + +export namespace bar { + int add(int a, int b) { + return zoo::add(a, b); + } +} diff --git a/tests/projects/c++/modules/hide_dependency_flags/src/cat.mpp b/tests/projects/c++/modules/hide_dependency_flags/src/cat.mpp new file mode 100644 index 000000000..652940c08 --- /dev/null +++ b/tests/projects/c++/modules/hide_dependency_flags/src/cat.mpp @@ -0,0 +1,8 @@ +export module cat; + +export namespace cat { + int sub(int a, int b) { + return a - b; + } +} + diff --git a/tests/projects/c++/modules/hide_dependency_flags/src/foo.mpp b/tests/projects/c++/modules/hide_dependency_flags/src/foo.mpp new file mode 100644 index 000000000..5aac74086 --- /dev/null +++ b/tests/projects/c++/modules/hide_dependency_flags/src/foo.mpp @@ -0,0 +1,13 @@ +export module foo; +import bar; +import cat; + +export namespace foo { + int add(int a, int b) { + return bar::add(a, b); + } + int sub(int a, int b) { + return cat::sub(a, b); + } +} + diff --git a/tests/projects/c++/modules/hide_dependency_flags/src/main.cpp b/tests/projects/c++/modules/hide_dependency_flags/src/main.cpp new file mode 100644 index 000000000..1d385dcfa --- /dev/null +++ b/tests/projects/c++/modules/hide_dependency_flags/src/main.cpp @@ -0,0 +1,10 @@ +#include <stdio.h> + +import foo; + +int main(int argc, char** argv) { + printf("add(1, 2): %d\n", foo::add(1, 2)); + printf("sub(1, 2): %d\n", foo::sub(1, 2)); + return 0; +} + diff --git a/tests/projects/c++/modules/hide_dependency_flags/src/zoo.mpp b/tests/projects/c++/modules/hide_dependency_flags/src/zoo.mpp new file mode 100644 index 000000000..60c86ddf6 --- /dev/null +++ b/tests/projects/c++/modules/hide_dependency_flags/src/zoo.mpp @@ -0,0 +1,8 @@ +export module zoo; + +export namespace zoo { + int add(int a, int b) { + return a + b; + } +} + diff --git a/tests/projects/c++/modules/hide_dependency_flags/test.lua b/tests/projects/c++/modules/hide_dependency_flags/test.lua new file mode 100644 index 000000000..7717f8049 --- /dev/null +++ b/tests/projects/c++/modules/hide_dependency_flags/test.lua @@ -0,0 +1 @@ +inherit(".test_base") diff --git a/tests/projects/c++/modules/hide_dependency_flags/xmake.lua b/tests/projects/c++/modules/hide_dependency_flags/xmake.lua new file mode 100644 index 000000000..875146a1d --- /dev/null +++ b/tests/projects/c++/modules/hide_dependency_flags/xmake.lua @@ -0,0 +1,7 @@ +add_rules("mode.release", "mode.debug") +set_languages("c++20") + +target("dependence2") + set_kind("binary") + add_files("src/*.cpp", "src/*.mpp") + set_policy("build.c++.modules.hide_dependencies", true) diff --git a/xmake/core/project/policy.lua b/xmake/core/project/policy.lua index 5c5e376a8..6949a6b94 100644 --- a/xmake/core/project/policy.lua +++ b/xmake/core/project/policy.lua @@ -72,6 +72,8 @@ function policy.policies() ["build.rpath"] = {description = "Enable build rpath.", default = true, type = "boolean"}, -- Enable C++ modules for C++ building, even if no .mpp is involved in the compilation ["build.c++.modules"] = {description = "Enable C++ modules for C++ building.", type = "boolean"}, + -- Hide C++ required files to reduce noise (may reduce build performance) + ["build.c++.modules.hide_dependencies"] = {description = "Hide dependencies from the commandline when build C++ modules.", default = false, type = "boolean"}, -- Enable two phase compilation for C++ modules if supported by the compiler ["build.c++.modules.two_phases"] = {description = "Enable two phase compilation if supported.", default = true, type = "boolean"}, -- Enable std module diff --git a/xmake/rules/c++/modules/clang/builder.lua b/xmake/rules/c++/modules/clang/builder.lua index df65bb213..a43c8aebe 100644 --- a/xmake/rules/c++/modules/clang/builder.lua +++ b/xmake/rules/c++/modules/clang/builder.lua @@ -104,6 +104,24 @@ function _make_headerunitflags(target, headerunit) return flags end + +function _get_mapper_str(target, module, opt) + local mapper_str + if target:policy("build.c++.modules.hide_dependencies") and option.get("diagnosis") then + if not opt.headerunit then + local requires_flagsfile = target:autogenfile(module.sourcefile .. ".requiresflags.txt") + if os.isfile(requires_flagsfile) then + if module.name then + mapper_str = format("\n${dim color.warning}mapper file for %s (%s) --------\n%s\n--------", module.name, module.sourcefile, io.readfile(requires_flagsfile):trim()) + else + mapper_str = format("\n${dim color.warning}mapper file for %s --------\n%s\n--------", module.sourcefile, io.readfile(requires_flagsfile):trim()) + end + end + end + end + return mapper_str +end + -- do compile function _compile(target, flags, module, opt) @@ -119,7 +137,7 @@ function _compile(target, flags, module, opt) if option.get("verbose") then cmd = "\n" .. compinst:compcmd(sourcefile, outputfile, {target = target, compflags = flags, sourcekind = "cxx", rawargs = true}) end - show_progress(target, module, table.join(opt, {cmd = cmd})) + show_progress(target, module, table.join(opt, {cmd = cmd, suffix = _get_mapper_str(target, module, opt)})) -- do compile if not dryrun then @@ -142,7 +160,7 @@ function _batchcmds_compile(batchcmds, target, flags, module, opt) if option.get("verbose") then cmd = "\n" .. compinst:compcmd(sourcefile, outputfile, {target = target, compflags = flags, sourcekind = "cxx", rawargs = true}) end - show_progress(target, module, table.join(opt, {cmd = cmd, batchcmds = batchcmds})) + show_progress(target, module, table.join(opt, {cmd = cmd, batchcmds = batchcmds, suffix = _get_mapper_str(target, module, opt)})) -- do compile batchcmds:compilev(flags, {compiler = compinst, sourcekind = "cxx", verbose = false}) @@ -194,15 +212,28 @@ end function _append_requires_flags(target, module) local cxxflags = {} local requiresflags = _get_requiresflags(target, module) - for _, flag in ipairs(requiresflags) do - -- we need to wrap flag to support flag with space - if type(flag) == "string" and flag:find(" ", 1, true) then - table.insert(cxxflags, {flag}) + local hide_dependencies = target:policy("build.c++.modules.hide_dependencies") + if #requiresflags> 0 then + for _, flag in ipairs(requiresflags) do + -- we need to wrap flag to support flag with space + if type(flag) == "string" and flag:find(" ", 1, true) and not hide_dependencies then + table.insert(cxxflags, {flag}) + else + if hide_dependencies then + table.insert(cxxflags, '"' .. path.unix(flag) .. '"') + else + table.insert(cxxflags, flag) + end + end + end + if hide_dependencies then + local requires_flagsfile = target:autogenfile(module.sourcefile .. ".requiresflags.txt") + io.writefile(requires_flagsfile, path.unix(table.concat(cxxflags, "\n"))) + target:fileconfig_add(module.sourcefile, {force = {cxxflags = {{"@" .. requires_flagsfile}}}}) else - table.insert(cxxflags, flag) + target:fileconfig_add(module.sourcefile, {force = {cxxflags = cxxflags}}) end end - target:fileconfig_add(module.sourcefile, {force = {cxxflags = cxxflags}}) end function append_requires_flags(target, built_modules) diff --git a/xmake/rules/c++/modules/msvc/builder.lua b/xmake/rules/c++/modules/msvc/builder.lua index 44519695d..b59115477 100644 --- a/xmake/rules/c++/modules/msvc/builder.lua +++ b/xmake/rules/c++/modules/msvc/builder.lua @@ -102,6 +102,23 @@ function _make_headerunitflags(target, headerunit, headertype) return flags end +function _get_mapper_str(target, module, opt) + local mapper_str + if target:policy("build.c++.modules.hide_dependencies") and option.get("diagnosis") then + if not opt.headerunit then + local requires_flagsfile = target:autogenfile(module.sourcefile .. ".requiresflags.txt") + if os.isfile(requires_flagsfile) then + if module.name then + mapper_str = format("\n${dim color.warning}mapper file for %s (%s) --------\n%s\n--------", module.name, module.sourcefile, io.readfile(requires_flagsfile):trim()) + else + mapper_str = format("\n${dim color.warning}mapper file for %s --------\n%s\n--------", module.sourcefile, io.readfile(requires_flagsfile):trim()) + end + end + end + end + return mapper_str +end + -- do compile function _compile(target, flags, module, opt) opt = opt or {} @@ -121,7 +138,7 @@ function _compile(target, flags, module, opt) cmd = "\n" .. compinst:compcmd(sourcefile, outputfile, {target = target, compflags = flags, sourcekind = "cxx", rawargs = true}) end end - show_progress(target, module, table.join(opt, {cmd = cmd})) + show_progress(target, module, table.join(opt, {cmd = cmd, suffix = _get_mapper_str(target, module, opt)})) -- do compile if not dryrun then @@ -148,7 +165,7 @@ function _batchcmds_compile(batchcmds, target, flags, module, opt) cmd = "\n" .. compinst:compcmd(sourcefile, outputfile, {target = target, compflags = flags, sourcekind = "cxx", rawargs = true}) end end - show_progress(target, module, table.join(opt, {cmd = cmd, batchcmds = batchcmds})) + show_progress(target, module, table.join(opt, {cmd = cmd, batchcmds = batchcmds, suffit = _get_mapper_str(target, module, opt)})) -- do compile batchcmds:compilev(flags, {compiler = compinst, sourcekind = "cxx", verbose = false}) @@ -215,8 +232,20 @@ function _get_requiresflags(target, module) end function _append_requires_flags(target, module) - local requiresflags = _get_requiresflags(target, module) - target:fileconfig_add(module.sourcefile, {force = {cxxflags = requiresflags}}) + local cxxflags = _get_requiresflags(target, module) + if #cxxflags > 0 then + if target:policy("build.c++.modules.hide_dependencies") then + local _cxxflags = {} + for _, flag in ipairs(cxxflags) do + table.insert(_cxxflags, flag[1] .. ' "' .. path.unix(flag[2]) .. '"') + end + local requires_flagsfile = target:autogenfile(module.sourcefile .. ".requiresflags.txt") + io.writefile(requires_flagsfile, table.concat(_cxxflags, "\n")) + target:fileconfig_add(module.sourcefile, {force = {cxxflags = {{"@" .. requires_flagsfile}}}}) + else + target:fileconfig_add(module.sourcefile, {force = {cxxflags = cxxflags}}) + end + end end function append_requires_flags(target, built_modules) diff --git a/xmake/rules/c++/modules/support.lua b/xmake/rules/c++/modules/support.lua index 29fc92fb0..ef1d1f9dd 100644 --- a/xmake/rules/c++/modules/support.lua +++ b/xmake/rules/c++/modules/support.lua @@ -76,10 +76,14 @@ end -- strip flags not relevent for module reuse function strip_flags(target, flags, opt) - local strippeable_flags, splitted_strippeable_flags = _support(target).strippeable_flags() - - if opt and opt.strip_defines then - table.join2(splitted_strippeable_flags, {"D", "U"}) + local strippeable_flags, splitted_strippeable_flags + if not opt.requiresonly then + strippeable_flags, splitted_strippeable_flags = _support(target).strippeable_flags() + if opt and opt.strip_defines then + table.join2(splitted_strippeable_flags, {"D", "U"}) + end + else + strippeable_flags, splitted_strippeable_flags = _support(target).require_flags() end local splitted_strippeable_flags_set = hashset.new() |
