From 2a231e4654c2569c347e27351a28ea6c4e95cf0c Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 10 Oct 2021 23:03:20 +0800 Subject: fix module tests --- tests/projects/c++/modules/class/src/hello.mpp | 3 +-- tests/projects/c++/modules/class/src/hello_impl.cpp | 7 +++---- tests/projects/c++/modules/dependence/src/hello_impl.cpp | 6 +++--- tests/projects/c++/modules/hello/src/hello.mpp | 7 +++---- tests/projects/c++/modules/impl_unit/src/hello_impl.cpp | 6 +++--- tests/projects/c++/modules/inline_and_template/src/hello.mpp | 6 +++--- tests/projects/c++/modules/inline_and_template/src/say.mpp | 6 +++--- 7 files changed, 19 insertions(+), 22 deletions(-) diff --git a/tests/projects/c++/modules/class/src/hello.mpp b/tests/projects/c++/modules/class/src/hello.mpp index 9e4ecd960..268964aa3 100644 --- a/tests/projects/c++/modules/class/src/hello.mpp +++ b/tests/projects/c++/modules/class/src/hello.mpp @@ -5,8 +5,7 @@ export namespace hello { public: say(int data); void hello(); - private: int data_; }; -} \ No newline at end of file +} diff --git a/tests/projects/c++/modules/class/src/hello_impl.cpp b/tests/projects/c++/modules/class/src/hello_impl.cpp index 6d0008fbb..b8b572f27 100644 --- a/tests/projects/c++/modules/class/src/hello_impl.cpp +++ b/tests/projects/c++/modules/class/src/hello_impl.cpp @@ -1,14 +1,13 @@ -module hello; - +module; #include - using namespace std; +module hello; + namespace hello { say::say(int data) : data_(data) { } - void say::hello() { cout << "hello, say class: " << data_ << endl; } diff --git a/tests/projects/c++/modules/dependence/src/hello_impl.cpp b/tests/projects/c++/modules/dependence/src/hello_impl.cpp index 5dbc009f4..064afebad 100644 --- a/tests/projects/c++/modules/dependence/src/hello_impl.cpp +++ b/tests/projects/c++/modules/dependence/src/hello_impl.cpp @@ -1,6 +1,7 @@ -module hello; - +module; #include + +module hello; import mod; void inner() { @@ -13,7 +14,6 @@ namespace hello { void say_hello() { ::inner(); } say::say(int data) : data_{data} { - } void say::hello() { diff --git a/tests/projects/c++/modules/hello/src/hello.mpp b/tests/projects/c++/modules/hello/src/hello.mpp index 9bbd036f0..124bd72bc 100644 --- a/tests/projects/c++/modules/hello/src/hello.mpp +++ b/tests/projects/c++/modules/hello/src/hello.mpp @@ -1,11 +1,10 @@ -export module hello; - +module; #include -using namespace std; +export module hello; export namespace hello { void say(const char* str) { printf("%s\n", str); } -} \ No newline at end of file +} 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 9cee17cfd..425c7aba4 100644 --- a/tests/projects/c++/modules/impl_unit/src/hello_impl.cpp +++ b/tests/projects/c++/modules/impl_unit/src/hello_impl.cpp @@ -1,9 +1,9 @@ -module hello; - +module; #include - using namespace std; +module hello; + namespace hello { void say_hi() { cout << "hello hi!" << endl; 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 c36d54783..94d40ddaa 100644 --- a/tests/projects/c++/modules/inline_and_template/src/hello.mpp +++ b/tests/projects/c++/modules/inline_and_template/src/hello.mpp @@ -1,9 +1,9 @@ -export module hello; - +module; #include +export module hello; export namespace hello { inline void say_hello() { std::printf("hello world!\n"); } -} \ No newline at end of file +} 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 b4d05a5f4..ab3290cf9 100644 --- a/tests/projects/c++/modules/inline_and_template/src/say.mpp +++ b/tests/projects/c++/modules/inline_and_template/src/say.mpp @@ -1,11 +1,11 @@ -export module say; - +module; #include +export module say; export class say { public: template void hello() { std::printf("hello, say class: %d\n", N); } -}; \ No newline at end of file +}; -- cgit v1.3.1 From 1cb2cb91f9ae2c251b2f1ba1870874570e11516f Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 12 Oct 2021 00:50:59 +0800 Subject: add module cache for clang --- xmake/rules/c++/modules/build_modulefiles.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/xmake/rules/c++/modules/build_modulefiles.lua b/xmake/rules/c++/modules/build_modulefiles.lua index 4e26f1eca..aa61fc696 100644 --- a/xmake/rules/c++/modules/build_modulefiles.lua +++ b/xmake/rules/c++/modules/build_modulefiles.lua @@ -24,6 +24,9 @@ import("core.tool.compiler") -- build module files using clang function _build_modulefiles_clang(target, sourcebatch, opt) + -- the module cache directory + local cachedir = path.join(target:autogendir(), "rules", "modules", "cache") + -- attempt to compile the module files as cxx sourcebatch.sourcekind = "cxx" sourcebatch.objectfiles = sourcebatch.objectfiles or {} @@ -35,7 +38,8 @@ function _build_modulefiles_clang(target, sourcebatch, opt) end -- compile module files to *.pcm - opt = table.join(opt, {configs = {force = {cxxflags = {opt.modulesflag, "--precompile", "-x c++-module"}}}}) + opt = table.join(opt, {configs = {force = {cxxflags = {opt.modulesflag, + "--precompile", "-x c++-module", "-fmodules-cache-path=" .. cachedir}}}}) import("private.action.build.object").build(target, sourcebatch, opt) -- compile *.pcm to object files @@ -53,7 +57,7 @@ function _build_modulefiles_clang(target, sourcebatch, opt) import("private.action.build.object").build(target, sourcebatch, opt) -- add module files - target:add("cxxflags", opt.modulesflag) + target:add("cxxflags", opt.modulesflag, "-fmodules-cache-path=" .. cachedir) for _, modulefile in ipairs(modulefiles) do target:add("cxxflags", "-fmodule-file=" .. modulefile) end -- cgit v1.3.1 From 09367e816155b80499ee5965ef686b55df54a590 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 12 Oct 2021 00:51:20 +0800 Subject: add module cache for clang --- xmake/rules/c++/modules/build_modulefiles.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/rules/c++/modules/build_modulefiles.lua b/xmake/rules/c++/modules/build_modulefiles.lua index aa61fc696..430c4397f 100644 --- a/xmake/rules/c++/modules/build_modulefiles.lua +++ b/xmake/rules/c++/modules/build_modulefiles.lua @@ -52,7 +52,7 @@ function _build_modulefiles_clang(target, sourcebatch, opt) sourcebatch.dependfiles[idx] = target:dependfile(objectfile) table.insert(modulefiles, modulefile) end - opt.configs = {cxxflags = {opt.modulesflag}} + opt.configs = {cxxflags = {opt.modulesflag, "-fmodules-cache-path=" .. cachedir}} opt.quiet = true import("private.action.build.object").build(target, sourcebatch, opt) -- cgit v1.3.1 From 417887dbd608961de809e7955be66fa9a9859176 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 12 Oct 2021 15:26:35 +0800 Subject: Update build_modulefiles.lua --- xmake/rules/c++/modules/build_modulefiles.lua | 85 ++++++++++++++++++--------- 1 file changed, 56 insertions(+), 29 deletions(-) diff --git a/xmake/rules/c++/modules/build_modulefiles.lua b/xmake/rules/c++/modules/build_modulefiles.lua index 430c4397f..7ad566f5c 100644 --- a/xmake/rules/c++/modules/build_modulefiles.lua +++ b/xmake/rules/c++/modules/build_modulefiles.lua @@ -24,6 +24,16 @@ import("core.tool.compiler") -- build module files using clang function _build_modulefiles_clang(target, sourcebatch, opt) + -- get modules flag + local modulesflag + local compinst = compiler.load("cxx", {target = target}) + if compinst:has_flags("-fmodules") then + modulesflag = "-fmodules" + elseif compinst:has_flags("-fmodules-ts") then + modulesflag = "-fmodules-ts" + end + assert(modulesflag, "compiler(clang): does not support c++ module!") + -- the module cache directory local cachedir = path.join(target:autogendir(), "rules", "modules", "cache") @@ -38,7 +48,7 @@ function _build_modulefiles_clang(target, sourcebatch, opt) end -- compile module files to *.pcm - opt = table.join(opt, {configs = {force = {cxxflags = {opt.modulesflag, + opt = table.join(opt, {configs = {force = {cxxflags = {modulesflag, "--precompile", "-x c++-module", "-fmodules-cache-path=" .. cachedir}}}}) import("private.action.build.object").build(target, sourcebatch, opt) @@ -52,7 +62,7 @@ function _build_modulefiles_clang(target, sourcebatch, opt) sourcebatch.dependfiles[idx] = target:dependfile(objectfile) table.insert(modulefiles, modulefile) end - opt.configs = {cxxflags = {opt.modulesflag, "-fmodules-cache-path=" .. cachedir}} + opt.configs = {cxxflags = {modulesflag, "-fmodules-cache-path=" .. cachedir}} opt.quiet = true import("private.action.build.object").build(target, sourcebatch, opt) @@ -98,6 +108,41 @@ end -- build module files using msvc function _build_modulefiles_msvc(target, sourcebatch, opt) + -- get modules flag + local modulesflag + local compinst = compiler.load("cxx", {target = target}) + if compinst:has_flags("/experimental:module") then + modulesflag = "/experimental:module" + end + assert(modulesflag, "compiler(msvc): does not support c++ module!") + + -- get output flag + local outputflag + if compinst:has_flags("/ifcOutput") then + outputflag = "/ifcOutput" + elseif compinst:has_flags("/module:output") then + outputflag = "/module:output" + end + assert(outputflag, "compiler(msvc): does not support c++ module!") + + -- get interface flag + local interfaceflag + if compinst:has_flags("/interface") then + interfaceflag = "/interface" + elseif compinst:has_flags("/module:interface") then + interfaceflag = "/module:interface" + end + assert(interfaceflag, "compiler(msvc): does not support c++ module!") + + -- get reference flag + local referenceflag + if compinst:has_flags("/reference") then + referenceflag = "/reference" + elseif compinst:has_flags("/module:interface") then + referenceflag = "/module:reference" + end + assert(referenceflag, "compiler(msvc): does not support c++ module!") + -- attempt to compile the module files as cxx local modulefiles = {} opt = table.join(opt, {configs = {}}) @@ -111,7 +156,7 @@ function _build_modulefiles_msvc(target, sourcebatch, opt) -- compile module file to *.pcm local singlebatch = {sourcekind = "cxx", sourcefiles = {sourcefile}, objectfiles = {objectfile}, dependfiles = {dependfile}} - opt.configs.cxxflags = {"/experimental:module /module:interface /module:output " .. os.args(modulefile), "/TP"} + opt.configs.cxxflags = {modulesflag, interfaceflag, outputflag .. " " .. os.args(modulefile), "/TP"} import("private.action.build.object").build(target, singlebatch, opt) table.insert(modulefiles, modulefile) table.insert(sourcebatch.objectfiles, objectfile) @@ -120,38 +165,20 @@ function _build_modulefiles_msvc(target, sourcebatch, opt) -- add module files for _, modulefile in ipairs(modulefiles) do - target:add("cxxflags", "/experimental:module /module:reference " .. os.args(modulefile)) + target:add("cxxflags", modulesflag, referenceflag .. " " .. os.args(modulefile)) end end -- build module files function main(target, sourcebatch, opt) - - -- do compile - local modulesflag = nil local _, toolname = target:tool("cxx") - local compinst = compiler.load("cxx") - 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 - modulesflag = "-fmodules-ts" - end + if toolname:find("clang", 1, true) then + _build_modulefiles_clang(target, sourcebatch, opt) + elseif toolname:find("gcc", 1, true) then + _build_modulefiles_gcc(target, sourcebatch, opt) elseif toolname == "cl" then - if compinst:has_flags("/experimental:module") then - modulesflag = "/experimental:module" - end - end - if modulesflag then - opt.modulesflag = modulesflag - if toolname:find("clang", 1, true) then - _build_modulefiles_clang(target, sourcebatch, opt) - elseif toolname:find("gcc", 1, true) 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 + _build_modulefiles_msvc(target, sourcebatch, opt) + else + raise("compiler(%s): does not support c++ module!", toolname) end end -- cgit v1.3.1 From 71ea9007f37d54c0224c43c1a3a1f2f3854c07c0 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 12 Oct 2021 15:27:04 +0800 Subject: Update hello.mpp --- tests/projects/c++/modules/dependence/src/hello.mpp | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/tests/projects/c++/modules/dependence/src/hello.mpp b/tests/projects/c++/modules/dependence/src/hello.mpp index 3f803afaf..abac9fcb0 100644 --- a/tests/projects/c++/modules/dependence/src/hello.mpp +++ b/tests/projects/c++/modules/dependence/src/hello.mpp @@ -1,27 +1,14 @@ export module hello; export namespace hello { -#ifdef _MSC_VER - int data__; -#else extern int data__; -#endif void say_hello(); class say { public: say(int data); void hello(); - private: int data_; }; } -/* -#ifndef _MSC_VER -export namespace { - void anonymous() { - } -} -#endif -*/ \ No newline at end of file -- cgit v1.3.1 From 3cafed12222db81c39361adc4e2425fb33b5b170 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 12 Oct 2021 21:57:46 +0800 Subject: Update build_modulefiles.lua --- xmake/rules/c++/modules/build_modulefiles.lua | 31 ++++++++++++--------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/xmake/rules/c++/modules/build_modulefiles.lua b/xmake/rules/c++/modules/build_modulefiles.lua index 7ad566f5c..a41ab2f39 100644 --- a/xmake/rules/c++/modules/build_modulefiles.lua +++ b/xmake/rules/c++/modules/build_modulefiles.lua @@ -73,36 +73,33 @@ function _build_modulefiles_clang(target, sourcebatch, opt) end end --- TODO -- build module files using gcc function _build_modulefiles_gcc(target, sourcebatch, opt) - --[[ + -- get modules flag + local modulesflag + local compinst = compiler.load("cxx", {target = target}) + if compinst:has_flags("-fmodules-ts") then + modulesflag = "-fmodules-ts" + end + assert(modulesflag, "compiler(gcc): does not support c++ module!") + -- attempt to compile the module files as cxx - local modulefiles = {} - opt = table.join(opt, {configs = {}}) sourcebatch.sourcekind = "cxx" sourcebatch.objectfiles = sourcebatch.objectfiles or {} sourcebatch.dependfiles = sourcebatch.dependfiles or {} for _, sourcefile in ipairs(sourcebatch.sourcefiles) do local objectfile = target:objectfile(sourcefile) - local dependfile = target:dependfile(objectfile) - local modulefile = objectfile .. ".pcm" - - -- compile module file to *.pcm - local singlebatch = {sourcekind = "cxx", sourcefiles = {sourcefile}, objectfiles = {objectfile}, dependfiles = {dependfile}} - 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) - table.insert(sourcebatch.dependfiles, dependfile) + table.insert(sourcebatch.dependfiles, target:dependfile(objectfile)) end + -- compile module files to object files + opt = table.join(opt, {configs = {force = {cxxflags = {modulesflag, "-x c++"}}}}) + import("private.action.build.object").build(target, sourcebatch, opt) + -- add module files - for _, modulefile in ipairs(modulefiles) do - target:add("cxxflags", "-fmodules", "-fmodule-file=" .. modulefile) - end]] - raise("compiler(gcc): not implemented for c++ module!") + target:add("cxxflags", modulesflag) end -- build module files using msvc -- cgit v1.3.1 From 6d94f1182d93b04f854f90599e3878fd5826939e Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 13 Oct 2021 00:40:31 +0800 Subject: fix modules test --- tests/projects/c++/modules/class/src/hello_impl.cpp | 2 +- tests/projects/c++/modules/impl_unit/src/hello_impl.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/projects/c++/modules/class/src/hello_impl.cpp b/tests/projects/c++/modules/class/src/hello_impl.cpp index b8b572f27..5dd555a7a 100644 --- a/tests/projects/c++/modules/class/src/hello_impl.cpp +++ b/tests/projects/c++/modules/class/src/hello_impl.cpp @@ -1,9 +1,9 @@ module; #include -using namespace std; module hello; +using namespace std; namespace hello { say::say(int data) : data_(data) { 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 425c7aba4..eccfed5d3 100644 --- a/tests/projects/c++/modules/impl_unit/src/hello_impl.cpp +++ b/tests/projects/c++/modules/impl_unit/src/hello_impl.cpp @@ -1,8 +1,8 @@ module; #include -using namespace std; module hello; +using namespace std; namespace hello { void say_hi() { -- cgit v1.3.1