From 2e3cad4e9c01e65f9c5da4b32be05ff4754f497b Mon Sep 17 00:00:00 2001 From: Ângelo Andrade Cirino Date: Thu, 15 Jul 2021 00:10:11 -0300 Subject: Corrected the support for C++ modules builds --- xmake/rules/c++/modules/build_modulefiles.lua | 43 ++++++++++++++++++--------- 1 file changed, 29 insertions(+), 14 deletions(-) (limited to 'xmake/rules/c++/modules/build_modulefiles.lua') diff --git a/xmake/rules/c++/modules/build_modulefiles.lua b/xmake/rules/c++/modules/build_modulefiles.lua index 992344059..9d0eaf276 100644 --- a/xmake/rules/c++/modules/build_modulefiles.lua +++ b/xmake/rules/c++/modules/build_modulefiles.lua @@ -35,7 +35,7 @@ function _build_modulefiles_clang(target, sourcebatch, opt) end -- compile module files to *.pcm - opt = table.join(opt, {configs = {force = {cxxflags = {"-fmodules-ts", "--precompile", "-x c++-module"}}}}) + opt = table.join(opt, {configs = {force = {cxxflags = {modulesflag, "--precompile", "-x c++-module"}}}}) import("private.action.build.object").build(target, sourcebatch, opt) -- compile *.pcm to object files @@ -48,12 +48,12 @@ function _build_modulefiles_clang(target, sourcebatch, opt) sourcebatch.dependfiles[idx] = target:dependfile(objectfile) table.insert(modulefiles, modulefile) end - opt.configs = {cxxflags = {"-fmodules-ts"}} + opt.configs = {cxxflags = {modulesflag}} opt.quiet = true import("private.action.build.object").build(target, sourcebatch, opt) -- add module files - target:add("cxxflags", "-fmodules-ts") + target:add("cxxflags", modulesflag) for _, modulefile in ipairs(modulefiles) do target:add("cxxflags", "-fmodule-file=" .. modulefile) end @@ -77,7 +77,7 @@ function _build_modulefiles_gcc(target, sourcebatch, opt) -- compile module file to *.pcm local singlebatch = {sourcekind = "cxx", sourcefiles = {sourcefile}, objectfiles = {objectfile}, dependfiles = {dependfile}} - opt.configs.cxxflags = {"-fmodules-ts", "-fmodule-output=" .. modulefile, "-x c++"} + opt.configs.cxxflags = {"-fmodules", "-fmodule-output=" .. modulefile, "-x c++"} import("private.action.build.object").build(target, singlebatch, opt) table.insert(modulefiles, modulefile) table.insert(sourcebatch.objectfiles, objectfile) @@ -86,7 +86,7 @@ function _build_modulefiles_gcc(target, sourcebatch, opt) -- add module files for _, modulefile in ipairs(modulefiles) do - target:add("cxxflags", "-fmodules-ts", "-fmodule-file=" .. modulefile) + target:add("cxxflags", "-fmodules", "-fmodule-file=" .. modulefile) end]] raise("compiler(gcc): not implemented for c++ module!") end @@ -124,15 +124,30 @@ end function main(target, sourcebatch, opt) -- do compile + local modulesflag = "-fmodules" + local _, toolname = target:tool("cxx") local compinst = compiler.load("cxx") - if compinst:name() == "clang" and compinst:has_flags("-fmodules-ts") then - _build_modulefiles_clang(target, sourcebatch, opt) - elseif compinst:name() == "gcc" and compinst:has_flags("-fmodules-ts") then - _build_modulefiles_gcc(target, sourcebatch, opt) - elseif compinst:name() == "cl" and compinst:has_flags("/experimental:module") then - _build_modulefiles_msvc(target, sourcebatch, opt) - else - raise("compiler(%s): does not support c++ module!", compinst:name()) + if toolname == "clang" or toolname == "gcc" then + if compinst:has_flags("-fmodules") then + modulesflag = "-fmodules" + elseif compinst:has_flags("-fmodules-ts") then + modulesflag = "-fmodules-ts" + end + elseif toolname == "cl" then + if compinst:has_flags("/experimental:module") then + modulesflag = "/experimental:module" + end + end + if modulesflag then + opt.modulesflag = modulesflag + if toolname == "clang" then + _build_modulefiles_clang(target, sourcebatch, opt) + elseif toolname == "gcc" then + _build_modulefiles_gcc(target, sourcebatch, opt) + elseif toolname == "cl" then + _build_modulefiles_msvc(target, sourcebatch, opt) + else + raise("compiler(%s): does not support c++ module!", toolname) + end end end - -- cgit v1.3.1 From c1b1b841353e32d2df356b8765d1189bef3d2f11 Mon Sep 17 00:00:00 2001 From: Ângelo Andrade Cirino Date: Thu, 15 Jul 2021 10:49:02 -0300 Subject: Additional changes to modules support and corrected the tests --- tests/projects/c++/modules/class/src/hello_impl.cpp | 5 ++--- tests/projects/c++/modules/class/xmake.lua | 3 +-- tests/projects/c++/modules/dependence/src/hello.mpp | 3 ++- tests/projects/c++/modules/dependence/src/hello_impl.cpp | 4 ++-- tests/projects/c++/modules/dependence/xmake.lua | 4 +--- tests/projects/c++/modules/hello/src/hello.mpp | 5 +++-- tests/projects/c++/modules/hello/xmake.lua | 3 +-- tests/projects/c++/modules/impl_unit/src/hello_impl.cpp | 4 ++-- tests/projects/c++/modules/impl_unit/xmake.lua | 3 +-- tests/projects/c++/modules/inline_and_template/src/hello.mpp | 4 ++-- tests/projects/c++/modules/inline_and_template/src/say.mpp | 4 ++-- tests/projects/c++/modules/inline_and_template/xmake.lua | 4 +--- xmake/rules/c++/modules/build_modulefiles.lua | 10 +++++----- 13 files changed, 25 insertions(+), 31 deletions(-) (limited to 'xmake/rules/c++/modules/build_modulefiles.lua') diff --git a/tests/projects/c++/modules/class/src/hello_impl.cpp b/tests/projects/c++/modules/class/src/hello_impl.cpp index b929c453b..6d0008fbb 100644 --- a/tests/projects/c++/modules/class/src/hello_impl.cpp +++ b/tests/projects/c++/modules/class/src/hello_impl.cpp @@ -1,8 +1,7 @@ -// module; -#include module hello; -// import std.core; +#include + using namespace std; namespace hello { diff --git a/tests/projects/c++/modules/class/xmake.lua b/tests/projects/c++/modules/class/xmake.lua index a3bc3a394..6d6eaddee 100644 --- a/tests/projects/c++/modules/class/xmake.lua +++ b/tests/projects/c++/modules/class/xmake.lua @@ -1,5 +1,4 @@ +set_languages("c++20") target("class") set_kind("binary") add_files("src/*.cpp", "src/*.mpp") - - diff --git a/tests/projects/c++/modules/dependence/src/hello.mpp b/tests/projects/c++/modules/dependence/src/hello.mpp index e29bd7b95..3f803afaf 100644 --- a/tests/projects/c++/modules/dependence/src/hello.mpp +++ b/tests/projects/c++/modules/dependence/src/hello.mpp @@ -17,10 +17,11 @@ export namespace hello { int data_; }; } - +/* #ifndef _MSC_VER export namespace { void anonymous() { } } #endif +*/ \ No newline at end of file diff --git a/tests/projects/c++/modules/dependence/src/hello_impl.cpp b/tests/projects/c++/modules/dependence/src/hello_impl.cpp index 37797c31c..5dbc009f4 100644 --- a/tests/projects/c++/modules/dependence/src/hello_impl.cpp +++ b/tests/projects/c++/modules/dependence/src/hello_impl.cpp @@ -1,6 +1,6 @@ -// module; -#include module hello; + +#include import mod; void inner() { diff --git a/tests/projects/c++/modules/dependence/xmake.lua b/tests/projects/c++/modules/dependence/xmake.lua index 14d6ca17c..6d64b49b1 100644 --- a/tests/projects/c++/modules/dependence/xmake.lua +++ b/tests/projects/c++/modules/dependence/xmake.lua @@ -1,6 +1,4 @@ +set_languages("c++20") target("dependence") set_kind("binary") add_files("src/*.cpp", "src/*.mpp") - set_languages("c++11") - - diff --git a/tests/projects/c++/modules/hello/src/hello.mpp b/tests/projects/c++/modules/hello/src/hello.mpp index fbd7fd6ad..9bbd036f0 100644 --- a/tests/projects/c++/modules/hello/src/hello.mpp +++ b/tests/projects/c++/modules/hello/src/hello.mpp @@ -1,6 +1,7 @@ -// module; -#include export module hello; + +#include + using namespace std; export namespace hello { diff --git a/tests/projects/c++/modules/hello/xmake.lua b/tests/projects/c++/modules/hello/xmake.lua index 6c3bff19a..2315cf287 100644 --- a/tests/projects/c++/modules/hello/xmake.lua +++ b/tests/projects/c++/modules/hello/xmake.lua @@ -1,5 +1,4 @@ +set_languages("c++20") target("hello") set_kind("binary") add_files("src/*.cpp", "src/*.mpp") - - diff --git a/tests/projects/c++/modules/impl_unit/src/hello_impl.cpp b/tests/projects/c++/modules/impl_unit/src/hello_impl.cpp index 1c9968a51..9cee17cfd 100644 --- a/tests/projects/c++/modules/impl_unit/src/hello_impl.cpp +++ b/tests/projects/c++/modules/impl_unit/src/hello_impl.cpp @@ -1,7 +1,7 @@ -// module; -#include module hello; +#include + using namespace std; namespace hello { diff --git a/tests/projects/c++/modules/impl_unit/xmake.lua b/tests/projects/c++/modules/impl_unit/xmake.lua index 3d19e0908..abe41d20a 100644 --- a/tests/projects/c++/modules/impl_unit/xmake.lua +++ b/tests/projects/c++/modules/impl_unit/xmake.lua @@ -1,5 +1,4 @@ +set_languages("c++20") target("impl_unit") set_kind("binary") add_files("src/*.cpp", "src/*.mpp") - - diff --git a/tests/projects/c++/modules/inline_and_template/src/hello.mpp b/tests/projects/c++/modules/inline_and_template/src/hello.mpp index 1796f0b11..c36d54783 100644 --- a/tests/projects/c++/modules/inline_and_template/src/hello.mpp +++ b/tests/projects/c++/modules/inline_and_template/src/hello.mpp @@ -1,7 +1,7 @@ -// module; -#include export module hello; +#include + export namespace hello { inline void say_hello() { std::printf("hello world!\n"); diff --git a/tests/projects/c++/modules/inline_and_template/src/say.mpp b/tests/projects/c++/modules/inline_and_template/src/say.mpp index d09e14d28..b4d05a5f4 100644 --- a/tests/projects/c++/modules/inline_and_template/src/say.mpp +++ b/tests/projects/c++/modules/inline_and_template/src/say.mpp @@ -1,7 +1,7 @@ -// module; -#include export module say; +#include + export class say { public: template diff --git a/tests/projects/c++/modules/inline_and_template/xmake.lua b/tests/projects/c++/modules/inline_and_template/xmake.lua index c0722cccc..4810edb85 100644 --- a/tests/projects/c++/modules/inline_and_template/xmake.lua +++ b/tests/projects/c++/modules/inline_and_template/xmake.lua @@ -1,6 +1,4 @@ +set_languages("c++20") target("inline_and_template") set_kind("binary") add_files("src/*.cpp", "src/*.mpp") - set_languages("c++11") - - diff --git a/xmake/rules/c++/modules/build_modulefiles.lua b/xmake/rules/c++/modules/build_modulefiles.lua index 9d0eaf276..a36ce49b3 100644 --- a/xmake/rules/c++/modules/build_modulefiles.lua +++ b/xmake/rules/c++/modules/build_modulefiles.lua @@ -35,7 +35,7 @@ function _build_modulefiles_clang(target, sourcebatch, opt) end -- compile module files to *.pcm - opt = table.join(opt, {configs = {force = {cxxflags = {modulesflag, "--precompile", "-x c++-module"}}}}) + opt = table.join(opt, {configs = {force = {cxxflags = {opt.modulesflag, "--precompile", "-x c++-module"}}}}) import("private.action.build.object").build(target, sourcebatch, opt) -- compile *.pcm to object files @@ -48,12 +48,12 @@ function _build_modulefiles_clang(target, sourcebatch, opt) sourcebatch.dependfiles[idx] = target:dependfile(objectfile) table.insert(modulefiles, modulefile) end - opt.configs = {cxxflags = {modulesflag}} + opt.configs = {cxxflags = {opt.modulesflag}} opt.quiet = true import("private.action.build.object").build(target, sourcebatch, opt) -- add module files - target:add("cxxflags", modulesflag) + target:add("cxxflags", opt.modulesflag) for _, modulefile in ipairs(modulefiles) do target:add("cxxflags", "-fmodule-file=" .. modulefile) end @@ -140,9 +140,9 @@ function main(target, sourcebatch, opt) end if modulesflag then opt.modulesflag = modulesflag - if toolname == "clang" then + if string.find(toolname, "clang") then _build_modulefiles_clang(target, sourcebatch, opt) - elseif toolname == "gcc" then + elseif string.find(toolname, "gcc") then _build_modulefiles_gcc(target, sourcebatch, opt) elseif toolname == "cl" then _build_modulefiles_msvc(target, sourcebatch, opt) -- cgit v1.3.1 From 880eeb4bc1633eccb52d4df19404441f6076d770 Mon Sep 17 00:00:00 2001 From: Ângelo Andrade Cirino Date: Thu, 15 Jul 2021 10:57:09 -0300 Subject: A little change to the toolname conditional --- xmake/rules/c++/modules/build_modulefiles.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'xmake/rules/c++/modules/build_modulefiles.lua') diff --git a/xmake/rules/c++/modules/build_modulefiles.lua b/xmake/rules/c++/modules/build_modulefiles.lua index a36ce49b3..99b5aeaaa 100644 --- a/xmake/rules/c++/modules/build_modulefiles.lua +++ b/xmake/rules/c++/modules/build_modulefiles.lua @@ -140,9 +140,9 @@ function main(target, sourcebatch, opt) end if modulesflag then opt.modulesflag = modulesflag - if string.find(toolname, "clang") then + if toolname:find("clang", 1, true) then _build_modulefiles_clang(target, sourcebatch, opt) - elseif string.find(toolname, "gcc") then + elseif toolname:find("gcc", 1, true) then _build_modulefiles_gcc(target, sourcebatch, opt) elseif toolname == "cl" then _build_modulefiles_msvc(target, sourcebatch, opt) -- cgit v1.3.1 From 942c32f448d9b8a850438aa2af1de2415b7ae872 Mon Sep 17 00:00:00 2001 From: Ângelo Andrade Cirino Date: Thu, 15 Jul 2021 11:19:54 -0300 Subject: Better toolname conditionals and modulesflag shouldn't be initialized --- xmake/rules/c++/modules/build_modulefiles.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'xmake/rules/c++/modules/build_modulefiles.lua') diff --git a/xmake/rules/c++/modules/build_modulefiles.lua b/xmake/rules/c++/modules/build_modulefiles.lua index 99b5aeaaa..4e26f1eca 100644 --- a/xmake/rules/c++/modules/build_modulefiles.lua +++ b/xmake/rules/c++/modules/build_modulefiles.lua @@ -124,10 +124,10 @@ end function main(target, sourcebatch, opt) -- do compile - local modulesflag = "-fmodules" + local modulesflag = nil local _, toolname = target:tool("cxx") local compinst = compiler.load("cxx") - if toolname == "clang" or toolname == "gcc" then + if toolname:find("clang", 1, true) or toolname:find("gcc", 1, true) then if compinst:has_flags("-fmodules") then modulesflag = "-fmodules" elseif compinst:has_flags("-fmodules-ts") then -- cgit v1.3.1