From aff3f765da3cd344bd0f8903ccd0d73b04fd9721 Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Mon, 12 Dec 2022 19:21:56 +0100 Subject: [PATCH 1/9] update test --- .../c++/modules/stdmodules/src/my_module.cpp | 2 +- .../c++/modules/stdmodules/src/my_module.mpp | 2 +- .../projects/c++/modules/stdmodules/test.lua | 1 + .../c++/modules/stdmodules/test/test.cpp | 2 +- .../projects/c++/modules/stdmodules/xmake.lua | 21 +++++++++++++++++-- 5 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 tests/projects/c++/modules/stdmodules/test.lua diff --git a/tests/projects/c++/modules/stdmodules/src/my_module.cpp b/tests/projects/c++/modules/stdmodules/src/my_module.cpp index 5264da82169..3d4711ceb78 100644 --- a/tests/projects/c++/modules/stdmodules/src/my_module.cpp +++ b/tests/projects/c++/modules/stdmodules/src/my_module.cpp @@ -1,6 +1,6 @@ module my_module; -#ifdef _MSC_VER +#ifdef MSVC_MODULES import std.core; #else import std; diff --git a/tests/projects/c++/modules/stdmodules/src/my_module.mpp b/tests/projects/c++/modules/stdmodules/src/my_module.mpp index 5c0e44bc9a5..38a8a90af44 100644 --- a/tests/projects/c++/modules/stdmodules/src/my_module.mpp +++ b/tests/projects/c++/modules/stdmodules/src/my_module.mpp @@ -1,6 +1,6 @@ export module my_module; -#ifdef _MSC_VER +#ifdef MSVC_MODULES import std.core; #else import std; diff --git a/tests/projects/c++/modules/stdmodules/test.lua b/tests/projects/c++/modules/stdmodules/test.lua new file mode 100644 index 00000000000..0657ead56c5 --- /dev/null +++ b/tests/projects/c++/modules/stdmodules/test.lua @@ -0,0 +1 @@ +inherit(".test_msvc") diff --git a/tests/projects/c++/modules/stdmodules/test/test.cpp b/tests/projects/c++/modules/stdmodules/test/test.cpp index a12813c248a..3deeff07e4e 100644 --- a/tests/projects/c++/modules/stdmodules/test/test.cpp +++ b/tests/projects/c++/modules/stdmodules/test/test.cpp @@ -1,4 +1,4 @@ -#ifdef _MSC_VER +#ifdef MSVC_MODULES import std.core; #else import std; diff --git a/tests/projects/c++/modules/stdmodules/xmake.lua b/tests/projects/c++/modules/stdmodules/xmake.lua index ffe2d015b98..176e8b5a660 100644 --- a/tests/projects/c++/modules/stdmodules/xmake.lua +++ b/tests/projects/c++/modules/stdmodules/xmake.lua @@ -1,13 +1,30 @@ add_rules("mode.debug", "mode.release") -set_languages("c++20") +set_languages("c++23") add_cxxflags("clang::-stdlib=libc++") target("mod") - set_kind("shared") + set_kind("static") add_files("src/*.cpp", "src/*.mpp") target("test") set_kind("binary") add_files("test/*.cpp") add_deps("mod") + +if is_plat("windows") then + target("mod-msvcifc") + set_languages("c++20") + set_kind("static") + add_files("src/*.cpp", "src/*.mpp") + set_values("c++.msvc.enable_std_ifc", true) + add_defines("MSVC_MODULES") + + target("test-msvcifc") + set_languages("c++20") + set_kind("binary") + add_files("test/*.cpp") + add_deps("mod-msvcifc") + set_values("c++.msvc.enable_std_ifc", true) + add_defines("MSVC_MODULES") +end From e4d4e9b9f42eb9443438c632a72add9560e517ac Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Mon, 12 Dec 2022 19:22:18 +0100 Subject: [PATCH 2/9] add support for standard C++23 modules on msvc --- .../c++/modules/modules_support/clang.lua | 6 ++ .../c++/modules/modules_support/common.lua | 1 + .../rules/c++/modules/modules_support/gcc.lua | 6 ++ .../c++/modules/modules_support/msvc.lua | 55 ++++++++++++++++++- 4 files changed, 65 insertions(+), 3 deletions(-) diff --git a/xmake/rules/c++/modules/modules_support/clang.lua b/xmake/rules/c++/modules/modules_support/clang.lua index de71032c746..5fa8845f450 100644 --- a/xmake/rules/c++/modules/modules_support/clang.lua +++ b/xmake/rules/c++/modules/modules_support/clang.lua @@ -557,6 +557,12 @@ function build_modules_for_batchcmds(target, batchcmds, objectfiles, modules, op _flush_mapper(target) end +-- not supported atm +function get_stdmodules(target) + local modules = {} + return modules +end + function get_bmi_extension() return ".pcm" end diff --git a/xmake/rules/c++/modules/modules_support/common.lua b/xmake/rules/c++/modules/modules_support/common.lua index ac555f778ab..01fa1bc886c 100644 --- a/xmake/rules/c++/modules/modules_support/common.lua +++ b/xmake/rules/c++/modules/modules_support/common.lua @@ -515,6 +515,7 @@ function get_module_dependencies(target, sourcebatch, opt) if changed or modules == nil then local moduleinfos = load_moduleinfos(target, sourcebatch) modules = _parse_dependencies_data(target, moduleinfos) + modules = table.join(modules or {}, modules_support(target).get_stdmodules(target)) if modules then _check_circular_dependencies(modules) end diff --git a/xmake/rules/c++/modules/modules_support/gcc.lua b/xmake/rules/c++/modules/modules_support/gcc.lua index 796b0e0332d..dfa68de4555 100644 --- a/xmake/rules/c++/modules/modules_support/gcc.lua +++ b/xmake/rules/c++/modules/modules_support/gcc.lua @@ -514,6 +514,12 @@ function build_modules_for_batchcmds(target, batchcmds, objectfiles, modules, op batchcmds:set_depmtime(depmtime) end +-- not supported atm +function get_stdmodules(target) + local modules = {} + return modules +end + function get_bmi_extension() return ".gcm" end diff --git a/xmake/rules/c++/modules/modules_support/msvc.lua b/xmake/rules/c++/modules/modules_support/msvc.lua index 5e56b1241f9..198447e4369 100644 --- a/xmake/rules/c++/modules/modules_support/msvc.lua +++ b/xmake/rules/c++/modules/modules_support/msvc.lua @@ -127,9 +127,17 @@ function load(target) local modulesflag = get_modulesflag(target) target:add("cxxflags", modulesflag) + -- enable std modules if c++23 by defaults + if target:values("c++.msvc.enable_std_import") == nil then + local languages = table.join({}, target:get("languages")) + local disable_on = { "c++11", "cxx11", "c++14", "cxx14", "c++17", "cxx17", "c++20", "cxx20"} + local iscpp23 = table.find_if(languages, function(_, v) for _, flag in pairs(disable_on) do if v:find(flag) then return false end end return true end) + target:set("values", "c++.msvc.enable_std_import", iscpp23) + end + -- add stdifcdir in case of if the user ask for it local stdifcdirflag = get_stdifcdirflag(target) - if stdifcdirflag then + if stdifcdirflag and target:values("c++.msvc.enable_std_ifc") then local msvc = target:toolchain("msvc") if msvc then local vcvars = msvc:config("vcvars") @@ -141,13 +149,19 @@ function load(target) arch = "x86" end if arch then + local mode = is_mode("release") and "Release" or "Debug" local stdifcdir = path.join(vcvars.VCInstallDir, "Tools", "MSVC", vcvars.VCToolsVersion, "ifc", arch) if os.isdir(stdifcdir) then target:add("cxxflags", {stdifcdirflag, winos.short_path(stdifcdir)}, {force = true, expand = false}) + target:add("linkdirs", path.join(stdifcdir, mode)) + target:add("links", "std") end end end end + + -- can't enable c++23 std modules and msvc specifics std modules + target:set("values", "c++.msvc.enable_std_import", false) end end @@ -441,6 +455,13 @@ function build_modules_for_batchjobs(target, batchjobs, objectfiles, modules, op _flush_mapper(target) end, {rootjob = opt.rootjob}) + if target:values("c++.msvc.enable_std_import") then + for objectfile, module in pairs(get_stdmodules(target)) do + table.insert(objectfiles, objectfile) + modules[objectfile] = module + end + end + local modulesjobs = {} for _, objectfile in ipairs(objectfiles) do local module = modules[objectfile] @@ -490,7 +511,7 @@ function build_modules_for_batchjobs(target, batchjobs, objectfiles, modules, op name = name or module.cppfile, flags = _flags, progress = (index * 100) / total}) - target:add("objectfiles", objectfile) + _add_objectfile_to_link_arguments(target, objectfile) elseif requiresflags then requiresflags = get_requiresflags(target, module.requires) target:fileconfig_add(cppfile, {force = {cxxflags = table.join(flags, requiresflags)}}) @@ -555,7 +576,7 @@ function build_modules_for_batchcmds(target, batchcmds, objectfiles, modules, op batchcmds:mkdir(path.directory(objectfile)) batchcmds:vrunv(compinst:program(), table.join(compinst:compflags({target = target}), table.join(flags, requiresflags or {})), {envs = msvc:runenvs()}) batchcmds:add_depfiles(cppfile) - target:add("objectfiles", objectfile) + _add_objectfile_to_link_arguments(target, objectfile) if provide then _add_module_to_mapper(target, referenceflag, name, name, objectfile, provide.bmi, requiresflags) end @@ -571,6 +592,34 @@ function build_modules_for_batchcmds(target, batchcmds, objectfiles, modules, op _flush_mapper(target) end +function get_stdmodules(target) + local modules = {} + + -- build c++23 standard modules if needed + if target:values("c++.msvc.enable_std_import") then + local msvc = target:toolchain("msvc") + if msvc then + local vcvars = msvc:config("vcvars") + if vcvars.VCInstallDir and vcvars.VCToolsVersion then + local stdmodulesdir = path.join(vcvars.VCInstallDir, "Tools", "MSVC", vcvars.VCToolsVersion, "modules") + assert(stdmodulesdir, "Can't enable C++23 std modules, directory missing !") + + local stlcachedir = common.stlmodules_cachedir(target) + local modulefile = path.join(stdmodulesdir, "std.ixx") + local bmifile = path.join(stlcachedir, "std.ixx" .. get_bmi_extension()) + local objfile = bmifile .. ".obj" + modules[objfile] = {provides = {std = {interface = true, sourcefile = modulefile, bmi = bmifile}}} + stlcachedir = common.stlmodules_cachedir(target) + modulefile = path.join(stdmodulesdir, "std.compat.ixx") + bmifile = path.join(stlcachedir, "std.compat.ixx" .. get_bmi_extension()) + objfile = bmifile .. ".obj" + modules[objfile] = {provides = {["std.compat"] = {interface = true, sourcefile = modulefile, bmi = bmifile}}, requires = {std = {unique = false, method = "by-name"}}} + end + end + end + return modules +end + function get_bmi_extension() return ".ifc" end From d0f88a03ac9aa31df054cf3b91b660c1e696dcaa Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Mon, 12 Dec 2022 19:49:22 +0100 Subject: [PATCH 3/9] fix compilation on c++23 std modules unsupported msvc --- xmake/rules/c++/modules/modules_support/msvc.lua | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/xmake/rules/c++/modules/modules_support/msvc.lua b/xmake/rules/c++/modules/modules_support/msvc.lua index 198447e4369..bd645c69469 100644 --- a/xmake/rules/c++/modules/modules_support/msvc.lua +++ b/xmake/rules/c++/modules/modules_support/msvc.lua @@ -132,7 +132,15 @@ function load(target) local languages = table.join({}, target:get("languages")) local disable_on = { "c++11", "cxx11", "c++14", "cxx14", "c++17", "cxx17", "c++20", "cxx20"} local iscpp23 = table.find_if(languages, function(_, v) for _, flag in pairs(disable_on) do if v:find(flag) then return false end end return true end) - target:set("values", "c++.msvc.enable_std_import", iscpp23) + local stdmodulesdir + local msvc = target:toolchain("msvc") + if msvc then + local vcvars = msvc:config("vcvars") + if vcvars.VCInstallDir and vcvars.VCToolsVersion then + stdmodulesdir = path.join(vcvars.VCInstallDir, "Tools", "MSVC", vcvars.VCToolsVersion, "modules") + end + end + target:set("values", "c++.msvc.enable_std_import", iscpp23 and os.isdir(stdmodulesdir)) end -- add stdifcdir in case of if the user ask for it From f8d5eba57c7252f504a6782d3a7bf9ddd6cae74c Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Tue, 13 Dec 2022 18:40:51 +0100 Subject: [PATCH 4/9] fix module objectfile handling --- xmake/rules/c++/modules/modules_support/msvc.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xmake/rules/c++/modules/modules_support/msvc.lua b/xmake/rules/c++/modules/modules_support/msvc.lua index bd645c69469..31b65b15445 100644 --- a/xmake/rules/c++/modules/modules_support/msvc.lua +++ b/xmake/rules/c++/modules/modules_support/msvc.lua @@ -79,7 +79,7 @@ function _add_objectfile_to_link_arguments(target, objectfile) if table.contains(cache, objectfile) then return end - table.insert(cache, path.translate(objectfile)) + table.insert(cache, objectfile) common.localcache():set(cachekey, cache) common.localcache():save(cachekey) end @@ -519,7 +519,7 @@ function build_modules_for_batchjobs(target, batchjobs, objectfiles, modules, op name = name or module.cppfile, flags = _flags, progress = (index * 100) / total}) - _add_objectfile_to_link_arguments(target, objectfile) + _add_objectfile_to_link_arguments(target, path(objectfile)) elseif requiresflags then requiresflags = get_requiresflags(target, module.requires) target:fileconfig_add(cppfile, {force = {cxxflags = table.join(flags, requiresflags)}}) @@ -584,7 +584,7 @@ function build_modules_for_batchcmds(target, batchcmds, objectfiles, modules, op batchcmds:mkdir(path.directory(objectfile)) batchcmds:vrunv(compinst:program(), table.join(compinst:compflags({target = target}), table.join(flags, requiresflags or {})), {envs = msvc:runenvs()}) batchcmds:add_depfiles(cppfile) - _add_objectfile_to_link_arguments(target, objectfile) + _add_objectfile_to_link_arguments(target, path(objectfile)) if provide then _add_module_to_mapper(target, referenceflag, name, name, objectfile, provide.bmi, requiresflags) end From f314e4eb068e04e4b311feffffaf427ea25ef9bd Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Tue, 13 Dec 2022 22:12:22 +0100 Subject: [PATCH 5/9] fix tests --- .../projects/c++/modules/stdmodules/xmake.lua | 33 +++++++++++------ tests/projects/c++/modules/test_msvc.lua | 37 ++++++++++++++++++- 2 files changed, 57 insertions(+), 13 deletions(-) diff --git a/tests/projects/c++/modules/stdmodules/xmake.lua b/tests/projects/c++/modules/stdmodules/xmake.lua index 176e8b5a660..92fa4b8f192 100644 --- a/tests/projects/c++/modules/stdmodules/xmake.lua +++ b/tests/projects/c++/modules/stdmodules/xmake.lua @@ -1,28 +1,39 @@ add_rules("mode.debug", "mode.release") -set_languages("c++23") add_cxxflags("clang::-stdlib=libc++") -target("mod") - set_kind("static") - add_files("src/*.cpp", "src/*.mpp") +option("stdifcsupport") + set_default(false) + set_showmenu(true) +option("stdimportsupport") + set_default(false) + set_showmenu(true) +option_end() -target("test") - set_kind("binary") - add_files("test/*.cpp") - add_deps("mod") +if has_config("stdimportsupport") then + target("mod") + set_languages("c++latest") + set_kind("static") + add_files("src/*.cpp", "src/*.mpp") + + target("test") + set_kind("binary") + set_languages("c++latest") + add_files("test/*.cpp") + add_deps("mod") +end -if is_plat("windows") then +if has_config("stdifcsupport") then target("mod-msvcifc") - set_languages("c++20") set_kind("static") + set_languages("c++latest", "clatest") add_files("src/*.cpp", "src/*.mpp") set_values("c++.msvc.enable_std_ifc", true) add_defines("MSVC_MODULES") target("test-msvcifc") - set_languages("c++20") set_kind("binary") + set_languages("c++latest", "clatest") add_files("test/*.cpp") add_deps("mod-msvcifc") set_values("c++.msvc.enable_std_ifc", true) diff --git a/tests/projects/c++/modules/test_msvc.lua b/tests/projects/c++/modules/test_msvc.lua index d51d3e3ef13..1cdb4228d82 100644 --- a/tests/projects/c++/modules/test_msvc.lua +++ b/tests/projects/c++/modules/test_msvc.lua @@ -1,5 +1,34 @@ import("lib.detect.find_tool") import("core.base.semver") +import("core.tool.toolchain") + +function stdifcdir_support() + if is_subhost("windows") then + local stdifcdir + local msvc = toolchain.load("msvc") + if msvc and msvc:check() then + local vcvars = msvc:config("vcvars") + if vcvars and vcvars.VCInstallDir and vcvars.VCToolsVersion then + stdifcdir = path.join(vcvars.VCInstallDir, "Tools", "MSVC", vcvars.VCToolsVersion, "ifc") + end + end + return os.isdir(stdifcdir or "") + end +end + +function stdimport_support() + if is_subhost("windows") then + local stdmodulesdir + local msvc = toolchain.load("msvc") + if msvc and msvc:check() then + local vcvars = msvc:config("vcvars") + if vcvars and vcvars.VCInstallDir and vcvars.VCToolsVersion then + stdifcdir = path.join(vcvars.VCInstallDir, "Tools", "MSVC", vcvars.VCToolsVersion, "modules") + end + end + return os.isdir(stdmodulesdir or "") + end +end function _build() local ci = (os.getenv("CI") or os.getenv("GITHUB_ACTIONS") or ""):lower() @@ -12,7 +41,11 @@ end function main(t) if is_subhost("windows") then - os.exec("xmake f -c") - _build() + local stdifcdir_flag = stdifcdir_support() and "--stdifcsupport=y" + local stdimport_flag = stdimport_support() and "--stdimportsupport=y" + if stdifcdir_flag or stdimport then + os.execv("xmake", table.join({"f", "-c"}, stdifcdir_flag or {}, stdimport_flag or {})) + _build() + end end end From 5dce8f69e3c7b3130bafb631c67529f4c0474955 Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Tue, 13 Dec 2022 22:12:35 +0100 Subject: [PATCH 6/9] enable arm64 std ifc modules support --- tests/projects/c++/modules/test_msvc.lua | 2 +- xmake/rules/c++/modules/modules_support/msvc.lua | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/projects/c++/modules/test_msvc.lua b/tests/projects/c++/modules/test_msvc.lua index 1cdb4228d82..6295a3ed7e6 100644 --- a/tests/projects/c++/modules/test_msvc.lua +++ b/tests/projects/c++/modules/test_msvc.lua @@ -23,7 +23,7 @@ function stdimport_support() if msvc and msvc:check() then local vcvars = msvc:config("vcvars") if vcvars and vcvars.VCInstallDir and vcvars.VCToolsVersion then - stdifcdir = path.join(vcvars.VCInstallDir, "Tools", "MSVC", vcvars.VCToolsVersion, "modules") + stdmodulesdir = path.join(vcvars.VCInstallDir, "Tools", "MSVC", vcvars.VCToolsVersion, "modules") end end return os.isdir(stdmodulesdir or "") diff --git a/xmake/rules/c++/modules/modules_support/msvc.lua b/xmake/rules/c++/modules/modules_support/msvc.lua index 31b65b15445..017047f9f5e 100644 --- a/xmake/rules/c++/modules/modules_support/msvc.lua +++ b/xmake/rules/c++/modules/modules_support/msvc.lua @@ -155,6 +155,8 @@ function load(target) arch = "x64" elseif target:is_arch("x86", "i386") then arch = "x86" + elseif target:is_arch("arm64") then + arch = "arm64" end if arch then local mode = is_mode("release") and "Release" or "Debug" From edcda9c35c05a90c1e6e110b559de69d78d1cc06 Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Wed, 14 Dec 2022 16:37:06 +0100 Subject: [PATCH 7/9] remove old msvc modules preview --- .../c++/modules/stdmodules/src/my_module.cpp | 4 -- .../c++/modules/stdmodules/src/my_module.mpp | 4 -- .../c++/modules/stdmodules/test/test.cpp | 4 -- .../projects/c++/modules/stdmodules/xmake.lua | 43 +++--------- tests/projects/c++/modules/test_msvc.lua | 43 +++--------- .../c++/modules/modules_support/msvc.lua | 67 ++++++------------- 6 files changed, 39 insertions(+), 126 deletions(-) diff --git a/tests/projects/c++/modules/stdmodules/src/my_module.cpp b/tests/projects/c++/modules/stdmodules/src/my_module.cpp index 3d4711ceb78..b87808a6e41 100644 --- a/tests/projects/c++/modules/stdmodules/src/my_module.cpp +++ b/tests/projects/c++/modules/stdmodules/src/my_module.cpp @@ -1,9 +1,5 @@ module my_module; -#ifdef MSVC_MODULES -import std.core; -#else import std; -#endif auto my_sum(size_t a, size_t b) -> size_t { return a + b; } diff --git a/tests/projects/c++/modules/stdmodules/src/my_module.mpp b/tests/projects/c++/modules/stdmodules/src/my_module.mpp index 38a8a90af44..bbebb27662b 100644 --- a/tests/projects/c++/modules/stdmodules/src/my_module.mpp +++ b/tests/projects/c++/modules/stdmodules/src/my_module.mpp @@ -1,9 +1,5 @@ export module my_module; -#ifdef MSVC_MODULES -import std.core; -#else import std; -#endif export auto my_sum(size_t a, size_t b) -> size_t; diff --git a/tests/projects/c++/modules/stdmodules/test/test.cpp b/tests/projects/c++/modules/stdmodules/test/test.cpp index 3deeff07e4e..7c6f7b4df15 100644 --- a/tests/projects/c++/modules/stdmodules/test/test.cpp +++ b/tests/projects/c++/modules/stdmodules/test/test.cpp @@ -1,8 +1,4 @@ -#ifdef MSVC_MODULES -import std.core; -#else import std; -#endif import my_module; diff --git a/tests/projects/c++/modules/stdmodules/xmake.lua b/tests/projects/c++/modules/stdmodules/xmake.lua index 92fa4b8f192..85e738d144b 100644 --- a/tests/projects/c++/modules/stdmodules/xmake.lua +++ b/tests/projects/c++/modules/stdmodules/xmake.lua @@ -2,40 +2,13 @@ add_rules("mode.debug", "mode.release") add_cxxflags("clang::-stdlib=libc++") -option("stdifcsupport") - set_default(false) - set_showmenu(true) -option("stdimportsupport") - set_default(false) - set_showmenu(true) -option_end() +set_languages("c++latest") -if has_config("stdimportsupport") then - target("mod") - set_languages("c++latest") - set_kind("static") - add_files("src/*.cpp", "src/*.mpp") +target("mod") + set_kind("static") + add_files("src/*.cpp", "src/*.mpp") - target("test") - set_kind("binary") - set_languages("c++latest") - add_files("test/*.cpp") - add_deps("mod") -end - -if has_config("stdifcsupport") then - target("mod-msvcifc") - set_kind("static") - set_languages("c++latest", "clatest") - add_files("src/*.cpp", "src/*.mpp") - set_values("c++.msvc.enable_std_ifc", true) - add_defines("MSVC_MODULES") - - target("test-msvcifc") - set_kind("binary") - set_languages("c++latest", "clatest") - add_files("test/*.cpp") - add_deps("mod-msvcifc") - set_values("c++.msvc.enable_std_ifc", true) - add_defines("MSVC_MODULES") -end +target("test") + set_kind("binary") + add_files("test/*.cpp") + add_deps("mod") diff --git a/tests/projects/c++/modules/test_msvc.lua b/tests/projects/c++/modules/test_msvc.lua index 6295a3ed7e6..a436b581465 100644 --- a/tests/projects/c++/modules/test_msvc.lua +++ b/tests/projects/c++/modules/test_msvc.lua @@ -2,34 +2,6 @@ import("lib.detect.find_tool") import("core.base.semver") import("core.tool.toolchain") -function stdifcdir_support() - if is_subhost("windows") then - local stdifcdir - local msvc = toolchain.load("msvc") - if msvc and msvc:check() then - local vcvars = msvc:config("vcvars") - if vcvars and vcvars.VCInstallDir and vcvars.VCToolsVersion then - stdifcdir = path.join(vcvars.VCInstallDir, "Tools", "MSVC", vcvars.VCToolsVersion, "ifc") - end - end - return os.isdir(stdifcdir or "") - end -end - -function stdimport_support() - if is_subhost("windows") then - local stdmodulesdir - local msvc = toolchain.load("msvc") - if msvc and msvc:check() then - local vcvars = msvc:config("vcvars") - if vcvars and vcvars.VCInstallDir and vcvars.VCToolsVersion then - stdmodulesdir = path.join(vcvars.VCInstallDir, "Tools", "MSVC", vcvars.VCToolsVersion, "modules") - end - end - return os.isdir(stdmodulesdir or "") - end -end - function _build() local ci = (os.getenv("CI") or os.getenv("GITHUB_ACTIONS") or ""):lower() if ci == "true" then @@ -41,11 +13,16 @@ end function main(t) if is_subhost("windows") then - local stdifcdir_flag = stdifcdir_support() and "--stdifcsupport=y" - local stdimport_flag = stdimport_support() and "--stdimportsupport=y" - if stdifcdir_flag or stdimport then - os.execv("xmake", table.join({"f", "-c"}, stdifcdir_flag or {}, stdimport_flag or {})) - _build() + local msvc = toolchain.load("msvc") + if msvc and msvc:check() then + local vcvars = msvc:config("vcvars") + if vcvars and vcvars.VCInstallDir and vcvars.VCToolsVersion and semver.compare(vcvars.VCToolsVersion, "14.35") then + local stdmodulesdir = path.join(vcvars.VCInstallDir, "Tools", "MSVC", vcvars.VCToolsVersion, "modules") + if os.isdir(stdmodulesdir) then + os.exec("xmake f -c") + _build() + end + end end end end diff --git a/xmake/rules/c++/modules/modules_support/msvc.lua b/xmake/rules/c++/modules/modules_support/msvc.lua index 017047f9f5e..8a099de176f 100644 --- a/xmake/rules/c++/modules/modules_support/msvc.lua +++ b/xmake/rules/c++/modules/modules_support/msvc.lua @@ -25,6 +25,7 @@ import("core.project.project") import("core.project.depend") import("core.project.config") import("core.base.hashset") +import("core.base.semver") import("utils.progress") import("private.action.build.object", {alias = "objectbuilder"}) import("common") @@ -130,49 +131,28 @@ function load(target) -- enable std modules if c++23 by defaults if target:values("c++.msvc.enable_std_import") == nil then local languages = table.join({}, target:get("languages")) - local disable_on = { "c++11", "cxx11", "c++14", "cxx14", "c++17", "cxx17", "c++20", "cxx20"} - local iscpp23 = table.find_if(languages, function(_, v) for _, flag in pairs(disable_on) do if v:find(flag) then return false end end return true end) + local iscpp23 = false + for _, language in pairs(languages) do + if language:find("c++") or language:find("cxx") then + iscpp23 = true + for _, version in pairs({"11", "14", "17", "20"}) do + if language:find(version) then + iscpp23 = false + break + end + end + end + end local stdmodulesdir local msvc = target:toolchain("msvc") if msvc then local vcvars = msvc:config("vcvars") - if vcvars.VCInstallDir and vcvars.VCToolsVersion then + if vcvars.VCInstallDir and vcvars.VCToolsVersion and semver.compare(vcvars.VCToolsVersion, "14.35") then stdmodulesdir = path.join(vcvars.VCInstallDir, "Tools", "MSVC", vcvars.VCToolsVersion, "modules") end end target:set("values", "c++.msvc.enable_std_import", iscpp23 and os.isdir(stdmodulesdir)) end - - -- add stdifcdir in case of if the user ask for it - local stdifcdirflag = get_stdifcdirflag(target) - if stdifcdirflag and target:values("c++.msvc.enable_std_ifc") then - local msvc = target:toolchain("msvc") - if msvc then - local vcvars = msvc:config("vcvars") - if vcvars.VCInstallDir and vcvars.VCToolsVersion then - local arch - if target:is_arch("x64", "x86_64") then - arch = "x64" - elseif target:is_arch("x86", "i386") then - arch = "x86" - elseif target:is_arch("arm64") then - arch = "arm64" - end - if arch then - local mode = is_mode("release") and "Release" or "Debug" - local stdifcdir = path.join(vcvars.VCInstallDir, "Tools", "MSVC", vcvars.VCToolsVersion, "ifc", arch) - if os.isdir(stdifcdir) then - target:add("cxxflags", {stdifcdirflag, winos.short_path(stdifcdir)}, {force = true, expand = false}) - target:add("linkdirs", path.join(stdifcdir, mode)) - target:add("links", "std") - end - end - end - end - - -- can't enable c++23 std modules and msvc specifics std modules - target:set("values", "c++.msvc.enable_std_import", false) - end end -- provide toolchain include dir for stl headerunit when p1689 is not supported @@ -550,6 +530,13 @@ function build_modules_for_batchcmds(target, batchcmds, objectfiles, modules, op local referenceflag = get_referenceflag(target) local internalpartitionflag = get_internalpartitionflag(target) + if target:values("c++.msvc.enable_std_import") then + for objectfile, module in pairs(get_stdmodules(target)) do + table.insert(objectfiles, objectfile) + modules[objectfile] = module + end + end + -- build modules local depmtime = 0 for _, objectfile in ipairs(objectfiles) do @@ -738,18 +725,6 @@ function get_exportheaderflag(target) return exportheaderflag or nil end -function get_stdifcdirflag(target) - local stdifcdirflag = _g.stdifcdirflag - if stdifcdirflag == nil then - local compinst = target:compiler("cxx") - if compinst:has_flags("-stdIfcDir", "cxxflags", {flagskey = "cl_std_ifc_dir"}) then - stdifcdirflag = "-stdIfcDir" - end - _g.stdifcdirflag = stdifcdirflag or false - end - return stdifcdirflag or nil -end - function get_scandependenciesflag(target) local scandependenciesflag = _g.scandependenciesflag if scandependenciesflag == nil then From 47d2cdfee41bd746b7d41e941c9da2a679cc65ea Mon Sep 17 00:00:00 2001 From: Arthur LAURENT Date: Fri, 16 Dec 2022 12:33:32 +0100 Subject: [PATCH 8/9] improve modules_support/msvc.lua --- .../c++/modules/modules_support/msvc.lua | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/xmake/rules/c++/modules/modules_support/msvc.lua b/xmake/rules/c++/modules/modules_support/msvc.lua index 8a099de176f..6d7504e6b82 100644 --- a/xmake/rules/c++/modules/modules_support/msvc.lua +++ b/xmake/rules/c++/modules/modules_support/msvc.lua @@ -130,16 +130,15 @@ function load(target) -- enable std modules if c++23 by defaults if target:values("c++.msvc.enable_std_import") == nil then - local languages = table.join({}, target:get("languages")) - local iscpp23 = false - for _, language in pairs(languages) do - if language:find("c++") or language:find("cxx") then - iscpp23 = true - for _, version in pairs({"11", "14", "17", "20"}) do - if language:find(version) then - iscpp23 = false - break - end + local languages = target:get("languages") + local isatleastcpp23 = false + for _, language in ipairs(languages) do + if language:startswith("c++") or language:startswith("cxx") then + isatleastcpp23 = true + local version = tonumber(language:match("%d+")) + if not version or version <= 20 then + isatleastcpp23 = false + break end end end @@ -151,7 +150,7 @@ function load(target) stdmodulesdir = path.join(vcvars.VCInstallDir, "Tools", "MSVC", vcvars.VCToolsVersion, "modules") end end - target:set("values", "c++.msvc.enable_std_import", iscpp23 and os.isdir(stdmodulesdir)) + target:set("values", "c++.msvc.enable_std_import", isatleastcpp23 and os.isdir(stdmodulesdir)) end end From 1ba0ad7cbb139e04ebe29ea4fcede2fa57376433 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 17 Dec 2022 11:32:03 +0800 Subject: [PATCH 9/9] Update msvc.lua --- xmake/rules/c++/modules/modules_support/msvc.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/xmake/rules/c++/modules/modules_support/msvc.lua b/xmake/rules/c++/modules/modules_support/msvc.lua index 6d7504e6b82..e5c37ea38dc 100644 --- a/xmake/rules/c++/modules/modules_support/msvc.lua +++ b/xmake/rules/c++/modules/modules_support/msvc.lua @@ -129,7 +129,7 @@ function load(target) target:add("cxxflags", modulesflag) -- enable std modules if c++23 by defaults - if target:values("c++.msvc.enable_std_import") == nil then + if target:data("c++.msvc.enable_std_import") == nil then local languages = target:get("languages") local isatleastcpp23 = false for _, language in ipairs(languages) do @@ -150,7 +150,7 @@ function load(target) stdmodulesdir = path.join(vcvars.VCInstallDir, "Tools", "MSVC", vcvars.VCToolsVersion, "modules") end end - target:set("values", "c++.msvc.enable_std_import", isatleastcpp23 and os.isdir(stdmodulesdir)) + target:data_set("c++.msvc.enable_std_import", isatleastcpp23 and os.isdir(stdmodulesdir)) end end @@ -444,7 +444,7 @@ function build_modules_for_batchjobs(target, batchjobs, objectfiles, modules, op _flush_mapper(target) end, {rootjob = opt.rootjob}) - if target:values("c++.msvc.enable_std_import") then + if target:data("c++.msvc.enable_std_import") then for objectfile, module in pairs(get_stdmodules(target)) do table.insert(objectfiles, objectfile) modules[objectfile] = module @@ -529,7 +529,7 @@ function build_modules_for_batchcmds(target, batchcmds, objectfiles, modules, op local referenceflag = get_referenceflag(target) local internalpartitionflag = get_internalpartitionflag(target) - if target:values("c++.msvc.enable_std_import") then + if target:data("c++.msvc.enable_std_import") then for objectfile, module in pairs(get_stdmodules(target)) do table.insert(objectfiles, objectfile) modules[objectfile] = module @@ -592,7 +592,7 @@ function get_stdmodules(target) local modules = {} -- build c++23 standard modules if needed - if target:values("c++.msvc.enable_std_import") then + if target:data("c++.msvc.enable_std_import") then local msvc = target:toolchain("msvc") if msvc then local vcvars = msvc:config("vcvars") @@ -818,4 +818,4 @@ function get_internalpartitionflag(target) _g.internalpartitionflag = internalpartitionflag or false end return internalpartitionflag or nil -end \ No newline at end of file +end