From 3cdbc4183fe807d2bf4bbcb19aef1f85e33c1d6d Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Tue, 14 Feb 2023 18:45:42 +0100 Subject: remove useless flags to build modules on clang --- xmake/rules/c++/modules/modules_support/clang.lua | 37 +++++++++++------------ 1 file changed, 18 insertions(+), 19 deletions(-) (limited to 'xmake/rules/c++/modules/modules_support') diff --git a/xmake/rules/c++/modules/modules_support/clang.lua b/xmake/rules/c++/modules/modules_support/clang.lua index 763cfb6cd..54be2c80f 100644 --- a/xmake/rules/c++/modules/modules_support/clang.lua +++ b/xmake/rules/c++/modules/modules_support/clang.lua @@ -116,22 +116,16 @@ end -- load module support for the current target function load(target) - local modulesflag, modulestsflag = get_modulesflag(target) - local builtinmodulemapflag = get_builtinmodulemapflag(target) - local implicitmodulesflag = get_implicitmodulesflag(target) - local noimplicitmodulemapsflag = get_noimplicitmodulemapsflag(target) + local clangmodulesflag, modulestsflag, withoutflag = get_modulesflag(target) -- add module flags - target:add("cxxflags", modulesflag) - if not modulesflag then + if not withoutflag then target:add("cxxflags", modulestsflag) end + -- enable clang modules to emulate std modules if target:policy("build.c++.clang.stdmodules") then - target:add("cxxflags", builtinmodulemapflag, {force = true}) - target:add("cxxflags", implicitmodulesflag, {force = true}) - else - target:add("cxxflags", noimplicitmodulemapsflag, {force = true}) + target:add("cxxflags", clangmodulesflag) end -- fix default visibility for functions and variables [-fvisibility] differs in PCH file vs. current file @@ -698,21 +692,25 @@ function get_bmi_extension() end function get_modulesflag(target) - local modulesflag = _g.modulesflag + local clangmodulesflag = _g.clangmodulesflag local modulestsflag = _g.modulestsflag - if modulesflag == nil and modulestsflag == nil then + local withoutflag = _g.withoutflag + if clangmodulesflag == nil and modulestsflag == nil then local compinst = target:compiler("cxx") if compinst:has_flags("-fmodules", "cxxflags", {flagskey = "clang_modules"}) then - modulesflag = "-fmodules" + clangmodulesflag = "-fmodules" end if compinst:has_flags("-fmodules-ts", "cxxflags", {flagskey = "clang_modules_ts"}) then modulestsflag = "-fmodules-ts" end - assert(modulesflag or modulestsflag, "compiler(clang): does not support c++ module!") - _g.modulesflag = modulesflag or false + local clang_version = _get_clang_version(target) + withoutflag = semver.compare(clang_version, "16.0") >= 0 + assert(withoutflag or modulestsflag, "compiler(clang): does not support c++ module!") + _g.clangmodulesflag = clangmodulesflag or false _g.modulestsflag = modulestsflag or false + _g.withoutflag = withoutflag or false end - return modulesflag or nil, modulestsflag or nil + return clangmodulesflag or nil, modulestsflag or nil, withoutflag or nil end function get_builtinmodulemapflag(target) @@ -814,9 +812,10 @@ function has_headerunitsupport(target) local support_headerunits = _g.support_headerunits if support_headerunits == nil then local compinst = target:compiler("cxx") - local modulesflag, modulestsflag = get_modulesflag(target) - if compinst:has_flags(modulesflag or modulestsflag .. " -std=c++20 -x c++-user-header", "cxxflags", {flagskey = "clang_user_header_unit_support", tryrun = true}) and - compinst:has_flags(modulesflag or modulestsflag .. " -std=c++20 -x c++-system-header", "cxxflags", {flagskey = "clang_system_header_unit_support", tryrun = true}) then + local _, modulestsflag, withoutflag = get_modulesflag(target) + modulestsflag = withoutflag and "" or modulestsflag + if compinst:has_flags(modulestsflag .. " -std=c++20 -x c++-user-header", "cxxflags", {flagskey = "clang_user_header_unit_support", tryrun = true}) and + compinst:has_flags(modulestsflag .. " -std=c++20 -x c++-system-header", "cxxflags", {flagskey = "clang_system_header_unit_support", tryrun = true}) then support_headerunits = true end _g.support_headerunits = support_headerunits or false -- cgit v1.3.1 From 3dff84c9e8fe939d1ed29123797060db56925e26 Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Wed, 15 Feb 2023 18:14:02 +0100 Subject: fix compilation of non .cpp private module with clang --- xmake/rules/c++/modules/modules_support/clang.lua | 2 ++ 1 file changed, 2 insertions(+) (limited to 'xmake/rules/c++/modules/modules_support') diff --git a/xmake/rules/c++/modules/modules_support/clang.lua b/xmake/rules/c++/modules/modules_support/clang.lua index 54be2c80f..3a7ad13b9 100644 --- a/xmake/rules/c++/modules/modules_support/clang.lua +++ b/xmake/rules/c++/modules/modules_support/clang.lua @@ -230,6 +230,8 @@ function _build_modulefile(target, sourcefile, opt) else bmiflags = table.join("-x", "c++-module", "--precompile", compflags, common_args, requiresflags) end + else + compileflags = {"-x", "c++"} end if bmiflags then -- cgit v1.3.1 From 51fad68409dc0ac9c70702397d0d344ca0aaf3f9 Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Wed, 15 Feb 2023 18:14:15 +0100 Subject: fix libc++ link --- xmake/rules/c++/modules/modules_support/clang.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'xmake/rules/c++/modules/modules_support') diff --git a/xmake/rules/c++/modules/modules_support/clang.lua b/xmake/rules/c++/modules/modules_support/clang.lua index 3a7ad13b9..06425ff40 100644 --- a/xmake/rules/c++/modules/modules_support/clang.lua +++ b/xmake/rules/c++/modules/modules_support/clang.lua @@ -111,7 +111,8 @@ end -- enable libc++ function _enable_libcxx(target) target:add("cxxflags", "-stdlib=libc++") - target:add("syslinks", "c++") + target:add("ldflags", "-stdlib=libc++") + target:add("shflags", "-stdlib=libc++") end -- load module support for the current target -- cgit v1.3.1 From 475734e6f4a27a3e3f2cdab3d3a93a5e949b7b0e Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Wed, 15 Feb 2023 18:15:06 +0100 Subject: use -fmodule-file== for named module on clang --- xmake/rules/c++/modules/modules_support/clang.lua | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'xmake/rules/c++/modules/modules_support') diff --git a/xmake/rules/c++/modules/modules_support/clang.lua b/xmake/rules/c++/modules/modules_support/clang.lua index 06425ff40..df3cd066b 100644 --- a/xmake/rules/c++/modules/modules_support/clang.lua +++ b/xmake/rules/c++/modules/modules_support/clang.lua @@ -79,15 +79,21 @@ end -- -fmodule-file=build/.gens/Foo/rules/modules/cache/foo.pcm -- -fmodule-file=build/.gens/Foo/rules/modules/cache/iostream.pcm -- -fmodule-file=build/.gens/Foo/rules/modules/cache/bar.hpp.pcm +-- on LLVM >= 16 +-- -fmodule-file=foo=build/.gens/Foo/rules/modules/cache/foo.pcm +-- -fmodule-file=build/.gens/Foo/rules/modules/cache/iostream.pcm +-- -fmodule-file=build/.gens/Foo/rules/modules/cache/bar.hpp.pcm -- -function _add_module_to_mapper(target, name, bmifile, deps) +function _add_module_to_mapper(target, name, bmifile, deps, namedmodule) local modulemap = _get_modulemap_from_mapper(target, name) if modulemap then return end + local clang_version = _get_clang_version(target) + namedmodule = namedmodule and semver.compare(clang_version, "16.0") >= 0 local modulefileflag = get_modulefileflag(target) - local mapflag = modulefileflag .. bmifile + local mapflag = namedmodule and format("%s%s=%s", modulefileflag, name, bmifile) or modulefileflag .. bmifile modulemap = {flag = mapflag, deps = deps} common.localcache():set2(_mapper_cachekey(target), "modulemap" .. name, modulemap) end @@ -596,7 +602,7 @@ function build_modules_for_batchjobs(target, batchjobs, objectfiles, modules, op target:add("objectfiles", objectfile) if provide then - _add_module_to_mapper(target, name, bmifile, requiresflags) + _add_module_to_mapper(target, name, bmifile, requiresflags, true) end elseif requiresflags then local cxxflags = {} @@ -658,7 +664,7 @@ function build_modules_for_batchcmds(target, batchcmds, objectfiles, modules, op if provide then _batchcmds_compile(batchcmds, target, table.join(flags, {"-x", "c++-module", "--precompile", "-c", path(cppfile), "-o", path(provide.bmi)})) - _add_module_to_mapper(target, name, provide.bmi) + _add_module_to_mapper(target, name, provide.bmi, nil, true) end _batchcmds_compile(batchcmds, target, file, table.join(flags, not provide and {"-x", "c++"} or {}, {"-c", file, "-o", path(objectfile)})) -- cgit v1.3.1 From 4220ee54e0f12461ef2186f8ccf4a7823ece2ee7 Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Wed, 15 Feb 2023 18:15:27 +0100 Subject: fix libc++ detection --- xmake/rules/c++/modules/modules_support/clang.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'xmake/rules/c++/modules/modules_support') diff --git a/xmake/rules/c++/modules/modules_support/clang.lua b/xmake/rules/c++/modules/modules_support/clang.lua index df3cd066b..e8d52dcba 100644 --- a/xmake/rules/c++/modules/modules_support/clang.lua +++ b/xmake/rules/c++/modules/modules_support/clang.lua @@ -157,7 +157,7 @@ function load(target) -- on ubuntu: -- sudo apt install libc++-dev libc++abi-15-dev -- - local flags = table.join(target:get("cxxflags"), get_config("cxxflags") or {}) + local flags = table.join(target:get("cxxflags") or {}, get_config("cxxflags") or {}) target:data_set("cxx.modules.use_libc++", table.contains(flags, "-stdlib=libc++", "clang::-stdlib=libc++")) if target:data("cxx.modules.use_libc++") then _enable_libcxx(target) -- cgit v1.3.1 From f6b9179af9ff16461e023f00ef10535b6e575ced Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Wed, 15 Feb 2023 18:15:42 +0100 Subject: fix headerunit clang support detection --- xmake/rules/c++/modules/modules_support/clang.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'xmake/rules/c++/modules/modules_support') diff --git a/xmake/rules/c++/modules/modules_support/clang.lua b/xmake/rules/c++/modules/modules_support/clang.lua index e8d52dcba..d505d779c 100644 --- a/xmake/rules/c++/modules/modules_support/clang.lua +++ b/xmake/rules/c++/modules/modules_support/clang.lua @@ -823,8 +823,8 @@ function has_headerunitsupport(target) local compinst = target:compiler("cxx") local _, modulestsflag, withoutflag = get_modulesflag(target) modulestsflag = withoutflag and "" or modulestsflag - if compinst:has_flags(modulestsflag .. " -std=c++20 -x c++-user-header", "cxxflags", {flagskey = "clang_user_header_unit_support", tryrun = true}) and - compinst:has_flags(modulestsflag .. " -std=c++20 -x c++-system-header", "cxxflags", {flagskey = "clang_system_header_unit_support", tryrun = true}) then + if compinst:has_flags(modulestsflag .. " -std=c++20 -x c++-user-header", "cxxflags", {snippet = "inline int foo() { return 0; }", flagskey = "clang_user_header_unit_support", tryrun = true}) and + compinst:has_flags(modulestsflag .. " -std=c++20 -x c++-system-header", "cxxflags", {snippet = "inline int foo() { return 0; }", flagskey = "clang_system_header_unit_support", tryrun = true}) then support_headerunits = true end _g.support_headerunits = support_headerunits or false -- cgit v1.3.1 From 0765350a8935fe493e0846543081dc29463ea953 Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Wed, 15 Feb 2023 18:29:10 +0100 Subject: fix clang module vprint --- xmake/rules/c++/modules/modules_support/clang.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'xmake/rules/c++/modules/modules_support') diff --git a/xmake/rules/c++/modules/modules_support/clang.lua b/xmake/rules/c++/modules/modules_support/clang.lua index d505d779c..3d45fd43f 100644 --- a/xmake/rules/c++/modules/modules_support/clang.lua +++ b/xmake/rules/c++/modules/modules_support/clang.lua @@ -246,7 +246,7 @@ function _build_modulefile(target, sourcefile, opt) end compileflags = table.join2(compileflags, compflags, common_args, requiresflags or {}) - vprint(compinst:compcmd(bmifile or sourcefile, objectfile, {compflags = compileflags, rawargs = true})) + vprint(compinst:compcmd(bmiflags and bmifile or sourcefile, objectfile, {compflags = compileflags, rawargs = true})) if not dryrun then -- cgit v1.3.1 From 46e033b7beaba56ed7dba4587ab98992b1d9f646 Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Wed, 15 Feb 2023 18:57:29 +0100 Subject: fix compflags for clang-scan module dependencies scan --- xmake/rules/c++/modules/modules_support/clang.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'xmake/rules/c++/modules/modules_support') diff --git a/xmake/rules/c++/modules/modules_support/clang.lua b/xmake/rules/c++/modules/modules_support/clang.lua index 3d45fd43f..40c91981e 100644 --- a/xmake/rules/c++/modules/modules_support/clang.lua +++ b/xmake/rules/c++/modules/modules_support/clang.lua @@ -303,7 +303,7 @@ function generate_dependencies(target, sourcebatch, opt) if has_clangscandepssupport(target) and not target:policy("build.c++.clang.fallbackscanner") then local clangscandeps = _get_clang_scan_deps(target) local compinst = target:compiler("cxx") - local compflags = compinst:compflags({sourcefile = file, target = target}) + local compflags = compinst:compflags({sourcefile = sourcefile, target = target}) local flags = table.join({"--format=p1689", "--", compinst:program(), "-x", "c++", "-c", sourcefile, "-o", target:objectfile(sourcefile)}, compflags) vprint(table.concat(table.join(clangscandeps, flags), " ")) -- cgit v1.3.1 From d39fdee559df35b35c03ba616558f5614b126a9b Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Wed, 15 Feb 2023 19:04:13 +0100 Subject: remove clang module flags deduplication --- xmake/rules/c++/modules/modules_support/clang.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'xmake/rules/c++/modules/modules_support') diff --git a/xmake/rules/c++/modules/modules_support/clang.lua b/xmake/rules/c++/modules/modules_support/clang.lua index 40c91981e..9d5911775 100644 --- a/xmake/rules/c++/modules/modules_support/clang.lua +++ b/xmake/rules/c++/modules/modules_support/clang.lua @@ -233,7 +233,7 @@ function _build_modulefile(target, sourcefile, opt) bmifile = opt.provide.bmifile if moduleoutputflag then - compileflags = table.join("-x", "c++-module", moduleoutputflag .. bmifile, compflags, common_args, requiresflags) + compileflags = table.join("-x", "c++-module", moduleoutputflag .. bmifile, requiresflags) else bmiflags = table.join("-x", "c++-module", "--precompile", compflags, common_args, requiresflags) end -- cgit v1.3.1