From e94c67aea65b7b64246a79b7f125c11f3268371d Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 24 Aug 2022 22:37:40 +0800 Subject: fix modules for gcc --- xmake/rules/c++/modules/modules_support/common.lua | 7 +++++-- xmake/rules/c++/modules/modules_support/gcc.lua | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/xmake/rules/c++/modules/modules_support/common.lua b/xmake/rules/c++/modules/modules_support/common.lua index 580d52dd9..734a2d070 100644 --- a/xmake/rules/c++/modules/modules_support/common.lua +++ b/xmake/rules/c++/modules/modules_support/common.lua @@ -390,17 +390,20 @@ function fallback_generate_dependencies(target, jsonfile, sourcefile) if not module_name then module_name = line:match("export%s+module%s+(.+)%s*;") end + if not module_name then + module_name = line:match("module%s+(.+)%s*;") + end local module_depname = line:match("import%s+(.+)%s*;") -- we need parse module interface dep in cxx/impl_unit.cpp, e.g. hello.mpp and hello_impl.cpp -- @see https://github.com/xmake-io/xmake/pull/2664#issuecomment-1213167314 if not module_depname and not has_module_extension(sourcefile) then - module_depname = line:match("module%s+(.+)%s*;") + module_depname = module_name end if module_depname then local module_dep = {} -- partition? import :xxx; if module_depname:startswith(":") then - module_depname = module_name .. module_depname + module_depname = (module_name or "") .. module_depname elseif module_depname:startswith("\"") then module_depname = module_depname:sub(2, -2) module_dep["lookup-method"] = "include-quote" diff --git a/xmake/rules/c++/modules/modules_support/gcc.lua b/xmake/rules/c++/modules/modules_support/gcc.lua index 9660466bd..20a8437aa 100644 --- a/xmake/rules/c++/modules/modules_support/gcc.lua +++ b/xmake/rules/c++/modules/modules_support/gcc.lua @@ -63,6 +63,8 @@ function load(target) os.rm(_get_module_mapper()) end target:add("cxxflags", modulemapperflag .. _get_module_mapper(), {force = true, expand = false}) + -- fix cxxabi issue, @see https://github.com/xmake-io/xmake/issues/2716#issuecomment-1225057760 + target:add("cxxflags", "-D_GLIBCXX_USE_CXX11_ABI=0") end -- get includedirs for stl headers -- cgit v1.3.1 From ee55b56ef385023a290e495f6626ab7aeea2c5b8 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 24 Aug 2022 22:45:24 +0800 Subject: improve module deps --- .../c++/modules/headerunits_person/src/Person.mpp | 20 ++++++++++++++++++++ .../c++/modules/headerunits_person/src/test.cpp | 11 +++++++++++ .../c++/modules/headerunits_person/test.lua | 1 + .../c++/modules/headerunits_person/xmake.lua | 4 ++++ tests/projects/c++/modules/partitions/test.lua | 1 + tests/projects/c++/modules/partitions2/src/math.cpp | 15 +++++++++++++++ tests/projects/c++/modules/partitions2/src/math.mpp | 7 +++++++ .../c++/modules/partitions2/src/math_helpers.cpp | 7 +++++++ tests/projects/c++/modules/partitions2/src/test.cpp | 7 +++++++ tests/projects/c++/modules/partitions2/xmake.lua | 4 ++++ xmake/rules/c++/modules/modules_support/common.lua | 21 +++++++++++---------- 11 files changed, 88 insertions(+), 10 deletions(-) create mode 100644 tests/projects/c++/modules/headerunits_person/src/Person.mpp create mode 100644 tests/projects/c++/modules/headerunits_person/src/test.cpp create mode 100644 tests/projects/c++/modules/headerunits_person/test.lua create mode 100644 tests/projects/c++/modules/headerunits_person/xmake.lua create mode 100644 tests/projects/c++/modules/partitions/test.lua create mode 100644 tests/projects/c++/modules/partitions2/src/math.cpp create mode 100644 tests/projects/c++/modules/partitions2/src/math.mpp create mode 100644 tests/projects/c++/modules/partitions2/src/math_helpers.cpp create mode 100644 tests/projects/c++/modules/partitions2/src/test.cpp create mode 100644 tests/projects/c++/modules/partitions2/xmake.lua diff --git a/tests/projects/c++/modules/headerunits_person/src/Person.mpp b/tests/projects/c++/modules/headerunits_person/src/Person.mpp new file mode 100644 index 000000000..e2dbf4044 --- /dev/null +++ b/tests/projects/c++/modules/headerunits_person/src/Person.mpp @@ -0,0 +1,20 @@ +module; + +#include // + +export module person; + +import ; + +export class Person { +public: + Person(std::string firstName, std::string lastName) + : m_firstName{std::move(firstName)}, m_lastName{std::move(lastName)} {} + + const std::string &getFirstName() const { return m_firstName; } + const std::string &getLastName() const { return m_lastName; } + +private: + std::string m_firstName; + std::string m_lastName; +}; diff --git a/tests/projects/c++/modules/headerunits_person/src/test.cpp b/tests/projects/c++/modules/headerunits_person/src/test.cpp new file mode 100644 index 000000000..7c01dc782 --- /dev/null +++ b/tests/projects/c++/modules/headerunits_person/src/test.cpp @@ -0,0 +1,11 @@ +import person; +import ; +import ; // For operator<< for std::string + +using namespace std; + +int main() +{ + Person person{ "Kole", "Webb" }; + cout << person.getLastName() << ", " << person.getFirstName() << endl; +} diff --git a/tests/projects/c++/modules/headerunits_person/test.lua b/tests/projects/c++/modules/headerunits_person/test.lua new file mode 100644 index 000000000..c18e5a1d0 --- /dev/null +++ b/tests/projects/c++/modules/headerunits_person/test.lua @@ -0,0 +1 @@ +inherit(".test_headerunits") diff --git a/tests/projects/c++/modules/headerunits_person/xmake.lua b/tests/projects/c++/modules/headerunits_person/xmake.lua new file mode 100644 index 000000000..c9290833b --- /dev/null +++ b/tests/projects/c++/modules/headerunits_person/xmake.lua @@ -0,0 +1,4 @@ +set_languages("c++20") +target("hello") + set_kind("binary") + add_files("src/*.cpp", "src/*.mpp") diff --git a/tests/projects/c++/modules/partitions/test.lua b/tests/projects/c++/modules/partitions/test.lua new file mode 100644 index 000000000..c18e5a1d0 --- /dev/null +++ b/tests/projects/c++/modules/partitions/test.lua @@ -0,0 +1 @@ +inherit(".test_headerunits") diff --git a/tests/projects/c++/modules/partitions2/src/math.cpp b/tests/projects/c++/modules/partitions2/src/math.cpp new file mode 100644 index 000000000..535d693eb --- /dev/null +++ b/tests/projects/c++/modules/partitions2/src/math.cpp @@ -0,0 +1,15 @@ +module math; + +import :details; + +double Math::superLog(double z, double b) +{ + // Implementation omitted... + return someHelperFunction(z); +} + +double Math::lerchZeta(double lambda, double alpha, double s) +{ + // Implementation omitted... + return someHelperFunction(s); +} diff --git a/tests/projects/c++/modules/partitions2/src/math.mpp b/tests/projects/c++/modules/partitions2/src/math.mpp new file mode 100644 index 000000000..e9308b2af --- /dev/null +++ b/tests/projects/c++/modules/partitions2/src/math.mpp @@ -0,0 +1,7 @@ +export module math; // math module declaration + +export namespace Math +{ + double superLog(double z, double b); + double lerchZeta(double lambda, double alpha, double s); +} diff --git a/tests/projects/c++/modules/partitions2/src/math_helpers.cpp b/tests/projects/c++/modules/partitions2/src/math_helpers.cpp new file mode 100644 index 000000000..9d2eaaa27 --- /dev/null +++ b/tests/projects/c++/modules/partitions2/src/math_helpers.cpp @@ -0,0 +1,7 @@ +module math:details; // math:details implementation partition + +double someHelperFunction(double a) +{ + // Implementation omitted... + return 42; +} diff --git a/tests/projects/c++/modules/partitions2/src/test.cpp b/tests/projects/c++/modules/partitions2/src/test.cpp new file mode 100644 index 000000000..a2ff757b1 --- /dev/null +++ b/tests/projects/c++/modules/partitions2/src/test.cpp @@ -0,0 +1,7 @@ +import math; + +int main() +{ + auto a{ Math::lerchZeta(1, 2, 3) }; + auto b{ Math::superLog(1, 2) }; +} diff --git a/tests/projects/c++/modules/partitions2/xmake.lua b/tests/projects/c++/modules/partitions2/xmake.lua new file mode 100644 index 000000000..c9290833b --- /dev/null +++ b/tests/projects/c++/modules/partitions2/xmake.lua @@ -0,0 +1,4 @@ +set_languages("c++20") +target("hello") + set_kind("binary") + add_files("src/*.cpp", "src/*.mpp") diff --git a/xmake/rules/c++/modules/modules_support/common.lua b/xmake/rules/c++/modules/modules_support/common.lua index 734a2d070..1e3231231 100644 --- a/xmake/rules/c++/modules/modules_support/common.lua +++ b/xmake/rules/c++/modules/modules_support/common.lua @@ -381,29 +381,30 @@ function fallback_generate_dependencies(target, jsonfile, sourcefile) local rule = {outputs = {jsonfile}} rule["primary-output"] = target:objectfile(sourcefile) - local module_name + local module_name_export + local module_name_private local module_deps = {} local sourcecode = io.readfile(sourcefile) sourcecode = sourcecode:gsub("//.-\n", "\n") sourcecode = sourcecode:gsub("/%*.-%*/", "") for _, line in ipairs(sourcecode:split("\n", {plain = true})) do - if not module_name then - module_name = line:match("export%s+module%s+(.+)%s*;") + if not module_name_export then + module_name_export = line:match("export%s+module%s+(.+)%s*;") end - if not module_name then - module_name = line:match("module%s+(.+)%s*;") + if not module_name_private then + module_name_private = line:match("module%s+(.+)%s*;") end local module_depname = line:match("import%s+(.+)%s*;") -- we need parse module interface dep in cxx/impl_unit.cpp, e.g. hello.mpp and hello_impl.cpp -- @see https://github.com/xmake-io/xmake/pull/2664#issuecomment-1213167314 if not module_depname and not has_module_extension(sourcefile) then - module_depname = module_name + module_depname = module_name_private end if module_depname then local module_dep = {} -- partition? import :xxx; if module_depname:startswith(":") then - module_depname = (module_name or "") .. module_depname + module_depname = (module_name_export or module_name_private or "") .. module_depname elseif module_depname:startswith("\"") then module_depname = module_depname:sub(2, -2) module_dep["lookup-method"] = "include-quote" @@ -420,11 +421,11 @@ function fallback_generate_dependencies(target, jsonfile, sourcefile) end end - if module_name then - table.insert(rule.outputs, module_name .. bmi_extension(target)) + if module_name_export then + table.insert(rule.outputs, module_name_export .. bmi_extension(target)) local provide = {} - provide["logical-name"] = module_name + provide["logical-name"] = module_name_export provide["source-path"] = path.absolute(sourcefile, project.directory()) rule.provides = {} -- cgit v1.3.1 From f029d068cd8d7f508cafd7fc050c1738350b114c Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 24 Aug 2022 22:45:42 +0800 Subject: format code --- tests/projects/c++/modules/headerunits_person/src/Person.mpp | 2 +- tests/projects/c++/modules/headerunits_person/src/test.cpp | 4 ++-- tests/projects/c++/modules/partitions2/src/math.cpp | 8 ++++---- tests/projects/c++/modules/partitions2/src/math.mpp | 4 ++-- tests/projects/c++/modules/partitions2/src/math_helpers.cpp | 4 ++-- tests/projects/c++/modules/partitions2/src/test.cpp | 4 ++-- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/projects/c++/modules/headerunits_person/src/Person.mpp b/tests/projects/c++/modules/headerunits_person/src/Person.mpp index e2dbf4044..e5b8105e8 100644 --- a/tests/projects/c++/modules/headerunits_person/src/Person.mpp +++ b/tests/projects/c++/modules/headerunits_person/src/Person.mpp @@ -1,6 +1,6 @@ module; -#include // +#include export module person; diff --git a/tests/projects/c++/modules/headerunits_person/src/test.cpp b/tests/projects/c++/modules/headerunits_person/src/test.cpp index 7c01dc782..d12206ef7 100644 --- a/tests/projects/c++/modules/headerunits_person/src/test.cpp +++ b/tests/projects/c++/modules/headerunits_person/src/test.cpp @@ -6,6 +6,6 @@ using namespace std; int main() { - Person person{ "Kole", "Webb" }; - cout << person.getLastName() << ", " << person.getFirstName() << endl; + Person person{ "Kole", "Webb" }; + cout << person.getLastName() << ", " << person.getFirstName() << endl; } diff --git a/tests/projects/c++/modules/partitions2/src/math.cpp b/tests/projects/c++/modules/partitions2/src/math.cpp index 535d693eb..3a10c8203 100644 --- a/tests/projects/c++/modules/partitions2/src/math.cpp +++ b/tests/projects/c++/modules/partitions2/src/math.cpp @@ -4,12 +4,12 @@ import :details; double Math::superLog(double z, double b) { - // Implementation omitted... - return someHelperFunction(z); + // Implementation omitted... + return someHelperFunction(z); } double Math::lerchZeta(double lambda, double alpha, double s) { - // Implementation omitted... - return someHelperFunction(s); + // Implementation omitted... + return someHelperFunction(s); } diff --git a/tests/projects/c++/modules/partitions2/src/math.mpp b/tests/projects/c++/modules/partitions2/src/math.mpp index e9308b2af..9e2552513 100644 --- a/tests/projects/c++/modules/partitions2/src/math.mpp +++ b/tests/projects/c++/modules/partitions2/src/math.mpp @@ -2,6 +2,6 @@ export module math; // math module declaration export namespace Math { - double superLog(double z, double b); - double lerchZeta(double lambda, double alpha, double s); + double superLog(double z, double b); + double lerchZeta(double lambda, double alpha, double s); } diff --git a/tests/projects/c++/modules/partitions2/src/math_helpers.cpp b/tests/projects/c++/modules/partitions2/src/math_helpers.cpp index 9d2eaaa27..f7089bb20 100644 --- a/tests/projects/c++/modules/partitions2/src/math_helpers.cpp +++ b/tests/projects/c++/modules/partitions2/src/math_helpers.cpp @@ -2,6 +2,6 @@ module math:details; // math:details implementation partition double someHelperFunction(double a) { - // Implementation omitted... - return 42; + // Implementation omitted... + return 42; } diff --git a/tests/projects/c++/modules/partitions2/src/test.cpp b/tests/projects/c++/modules/partitions2/src/test.cpp index a2ff757b1..8f7f81b2c 100644 --- a/tests/projects/c++/modules/partitions2/src/test.cpp +++ b/tests/projects/c++/modules/partitions2/src/test.cpp @@ -2,6 +2,6 @@ import math; int main() { - auto a{ Math::lerchZeta(1, 2, 3) }; - auto b{ Math::superLog(1, 2) }; + auto a{ Math::lerchZeta(1, 2, 3) }; + auto b{ Math::superLog(1, 2) }; } -- cgit v1.3.1 From a77e6df407c6b411d73f3c129db02526c3cdac32 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 24 Aug 2022 23:34:08 +0800 Subject: remove repeat moduledeps --- xmake/rules/c++/modules/modules_support/common.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/xmake/rules/c++/modules/modules_support/common.lua b/xmake/rules/c++/modules/modules_support/common.lua index 1e3231231..92c92d465 100644 --- a/xmake/rules/c++/modules/modules_support/common.lua +++ b/xmake/rules/c++/modules/modules_support/common.lua @@ -384,6 +384,7 @@ function fallback_generate_dependencies(target, jsonfile, sourcefile) local module_name_export local module_name_private local module_deps = {} + local module_deps_set = hashset.new() local sourcecode = io.readfile(sourcefile) sourcecode = sourcecode:gsub("//.-\n", "\n") sourcecode = sourcecode:gsub("/%*.-%*/", "") @@ -400,7 +401,7 @@ function fallback_generate_dependencies(target, jsonfile, sourcefile) if not module_depname and not has_module_extension(sourcefile) then module_depname = module_name_private end - if module_depname then + if module_depname and not module_deps_set:has(module_depname) then local module_dep = {} -- partition? import :xxx; if module_depname:startswith(":") then @@ -418,6 +419,7 @@ function fallback_generate_dependencies(target, jsonfile, sourcefile) end module_dep["logical-name"] = module_depname table.insert(module_deps, module_dep) + module_deps_set:insert(module_depname) end end -- cgit v1.3.1 From 1403886d0b46331539c695511e78bf40b4ff08e6 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 24 Aug 2022 23:35:04 +0800 Subject: fix tests --- tests/projects/c++/modules/partitions2/src/math_helpers.cpp | 7 ------- tests/projects/c++/modules/partitions2/src/math_helpers.mpp | 7 +++++++ 2 files changed, 7 insertions(+), 7 deletions(-) delete mode 100644 tests/projects/c++/modules/partitions2/src/math_helpers.cpp create mode 100644 tests/projects/c++/modules/partitions2/src/math_helpers.mpp diff --git a/tests/projects/c++/modules/partitions2/src/math_helpers.cpp b/tests/projects/c++/modules/partitions2/src/math_helpers.cpp deleted file mode 100644 index f7089bb20..000000000 --- a/tests/projects/c++/modules/partitions2/src/math_helpers.cpp +++ /dev/null @@ -1,7 +0,0 @@ -module math:details; // math:details implementation partition - -double someHelperFunction(double a) -{ - // Implementation omitted... - return 42; -} diff --git a/tests/projects/c++/modules/partitions2/src/math_helpers.mpp b/tests/projects/c++/modules/partitions2/src/math_helpers.mpp new file mode 100644 index 000000000..a92cb5e4a --- /dev/null +++ b/tests/projects/c++/modules/partitions2/src/math_helpers.mpp @@ -0,0 +1,7 @@ +export module math:details; // math:details implementation partition + +double someHelperFunction(double a) +{ + // Implementation omitted... + return 42; +} -- cgit v1.3.1 From ab8143a85b50dab08e1a3573f3456fb9573a699a Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 24 Aug 2022 23:35:13 +0800 Subject: add test --- tests/projects/c++/modules/partitions2/test.lua | 1 + 1 file changed, 1 insertion(+) create mode 100644 tests/projects/c++/modules/partitions2/test.lua diff --git a/tests/projects/c++/modules/partitions2/test.lua b/tests/projects/c++/modules/partitions2/test.lua new file mode 100644 index 000000000..c18e5a1d0 --- /dev/null +++ b/tests/projects/c++/modules/partitions2/test.lua @@ -0,0 +1 @@ +inherit(".test_headerunits") -- cgit v1.3.1