From 885d00da8caf74aeed758d030b9a1b50d9078e1f Mon Sep 17 00:00:00 2001 From: Ângelo Andrade Cirino Date: Sat, 21 Aug 2021 15:12:42 -0300 Subject: Little modifications to modules build --- xmake/rules/c++/modules/build_modulefiles.lua | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/xmake/rules/c++/modules/build_modulefiles.lua b/xmake/rules/c++/modules/build_modulefiles.lua index 4e26f1eca..75593f0cc 100644 --- a/xmake/rules/c++/modules/build_modulefiles.lua +++ b/xmake/rules/c++/modules/build_modulefiles.lua @@ -55,7 +55,7 @@ function _build_modulefiles_clang(target, sourcebatch, opt) -- add module files target:add("cxxflags", opt.modulesflag) for _, modulefile in ipairs(modulefiles) do - target:add("cxxflags", "-fmodule-file=" .. modulefile) + target:add("cxxflags", "-fmodules", "-fimplicit-modules", "-fimplicit-module-maps", "-fmodule-file=" .. modulefile) end end @@ -63,7 +63,6 @@ end -- build module files using gcc function _build_modulefiles_gcc(target, sourcebatch, opt) - --[[ -- attempt to compile the module files as cxx local modulefiles = {} opt = table.join(opt, {configs = {}}) @@ -77,7 +76,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", "-fmodule-output=" .. modulefile, "-x c++"} + opt.configs.cxxflags = {"-fmodules-ts", "-x c++"} import("private.action.build.object").build(target, singlebatch, opt) table.insert(modulefiles, modulefile) table.insert(sourcebatch.objectfiles, objectfile) @@ -86,9 +85,8 @@ function _build_modulefiles_gcc(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", "-fmodules-ts", "-fmodule-file=" .. modulefile) + end end -- build module files using msvc @@ -125,9 +123,9 @@ function main(target, sourcebatch, opt) -- do compile local modulesflag = nil - local _, toolname = target:tool("cxx") + local toolname = target:tool("cxx") local compinst = compiler.load("cxx") - if toolname:find("clang", 1, true) or toolname:find("gcc", 1, true) then + if toolname:find("clang", 1, true) or toolname:find("gcc", 1, true) or toolname:find("g++", 1, true) then if compinst:has_flags("-fmodules") then modulesflag = "-fmodules" elseif compinst:has_flags("-fmodules-ts") then @@ -142,7 +140,7 @@ function main(target, sourcebatch, opt) opt.modulesflag = modulesflag if toolname:find("clang", 1, true) then _build_modulefiles_clang(target, sourcebatch, opt) - elseif toolname:find("gcc", 1, true) then + elseif toolname:find("gcc", 1, true) or toolname:find("g++", 1, true) then _build_modulefiles_gcc(target, sourcebatch, opt) elseif toolname == "cl" then _build_modulefiles_msvc(target, sourcebatch, opt) -- cgit v1.3.1 From 1b2a5da03aea8f9082204c345e852ee69bd6cec4 Mon Sep 17 00:00:00 2001 From: Ângelo Andrade Cirino Date: Mon, 17 Jan 2022 15:49:50 -0300 Subject: Solved building submodules with clang --- xmake/rules/c++/modules/build_modules/clang.lua | 11 +++++++++-- xmake/rules/c++/modules/build_modules/module_parser.lua | 9 +++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/xmake/rules/c++/modules/build_modules/clang.lua b/xmake/rules/c++/modules/build_modules/clang.lua index 523c61ad9..49ae5b2de 100644 --- a/xmake/rules/c++/modules/build_modules/clang.lua +++ b/xmake/rules/c++/modules/build_modules/clang.lua @@ -89,7 +89,7 @@ function build_with_batchjobs(target, batchjobs, sourcebatch, opt) local sourcefiles_total = #sourcebatch.sourcefiles for i = 1, sourcefiles_total do local sourcefile = sourcebatch.sourcefiles[i] - local moduledep = assert(moduledeps_files[sourcefile], "moduledep(%s) not found!", sourcefile) + local moduledep = moduledeps_files[sourcefile] or {} moduledep.job = batchjobs:newjob(sourcefile, function (index, total) -- compile module files to *.pcm @@ -100,6 +100,14 @@ function build_with_batchjobs(target, batchjobs, sourcebatch, opt) opt2.objectfile = modulefiles[i] opt2.dependfile = target:dependfile(opt2.objectfile) opt2.sourcekind = assert(sourcebatch.sourcekind, "%s: sourcekind not found!", sourcefile) + if moduledep["deps"] then + for _, modulefile in ipairs(modulefiles) do + local found = modulefile:find(moduledep["name"]) + if found == nil then + target:add("cxxflags", "-fmodule-file=" .. modulefile, {force = true}) + end + end + end objectbuilder.build_object(target, sourcefile, opt2) -- compile *.pcm to object files @@ -136,4 +144,3 @@ function build_with_batchjobs(target, batchjobs, sourcebatch, opt) end end end - diff --git a/xmake/rules/c++/modules/build_modules/module_parser.lua b/xmake/rules/c++/modules/build_modules/module_parser.lua index b97b7e1d0..881f2b48f 100644 --- a/xmake/rules/c++/modules/build_modules/module_parser.lua +++ b/xmake/rules/c++/modules/build_modules/module_parser.lua @@ -84,15 +84,16 @@ function load(target, sourcebatch, opt) generate(target, sourcebatch, opt) -- load deps - local moduledeps + local moduledeps = moduledeps or {} for _, sourcefile in ipairs(sourcebatch.sourcefiles) do local dependfile = _get_dependfile_of_modulesource(target, sourcefile) if os.isfile(dependfile) then local data = io.load(dependfile) if data then - local moduleinfo = data.moduleinfo - moduledeps = moduledeps or {} - moduledeps[moduleinfo.name] = moduleinfo + if data.moduleinfo then + local moduleinfo = data.moduleinfo + moduledeps[moduleinfo.name] = moduleinfo + end end end end -- cgit v1.3.1 From cc30e7792843762b6862cdc603e7d1862cf691dc Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 19 Jan 2022 09:55:15 +0800 Subject: Update clang.lua --- xmake/rules/c++/modules/build_modules/clang.lua | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/xmake/rules/c++/modules/build_modules/clang.lua b/xmake/rules/c++/modules/build_modules/clang.lua index 49ae5b2de..4cde11419 100644 --- a/xmake/rules/c++/modules/build_modules/clang.lua +++ b/xmake/rules/c++/modules/build_modules/clang.lua @@ -66,16 +66,13 @@ function build_with_batchjobs(target, batchjobs, sourcebatch, opt) local cachedir = path.join(target:autogendir(), "rules", "modules", "cache") -- we need patch objectfiles to sourcebatch for linking module objects - local modulefiles = {} sourcebatch.sourcekind = "cxx" sourcebatch.objectfiles = sourcebatch.objectfiles or {} sourcebatch.dependfiles = sourcebatch.dependfiles or {} for _, sourcefile in ipairs(sourcebatch.sourcefiles) do - local modulefile = path.join(cachedir, path.basename(sourcefile) .. ".pcm") local objectfile = target:objectfile(sourcefile) table.insert(sourcebatch.objectfiles, objectfile) table.insert(sourcebatch.dependfiles, target:dependfile(objectfile)) - table.insert(modulefiles, modulefile) end -- load moduledeps @@ -86,10 +83,18 @@ function build_with_batchjobs(target, batchjobs, sourcebatch, opt) -- compile module files to object files local count = 0 + local modulefiles = {} local sourcefiles_total = #sourcebatch.sourcefiles for i = 1, sourcefiles_total do local sourcefile = sourcebatch.sourcefiles[i] local moduledep = moduledeps_files[sourcefile] or {} + + -- make module file path, @note we need process submodule name, e.g. module.submodule.mpp -> module.submodule.pcm + -- @see https://github.com/xmake-io/xmake/pull/1982 + local modulefile = path.join(cachedir, (moduledep.name or path.basename(sourcefile)) .. ".pcm") + table.insert(modulefiles, modulefile) + + -- make build job moduledep.job = batchjobs:newjob(sourcefile, function (index, total) -- compile module files to *.pcm @@ -100,14 +105,6 @@ function build_with_batchjobs(target, batchjobs, sourcebatch, opt) opt2.objectfile = modulefiles[i] opt2.dependfile = target:dependfile(opt2.objectfile) opt2.sourcekind = assert(sourcebatch.sourcekind, "%s: sourcekind not found!", sourcefile) - if moduledep["deps"] then - for _, modulefile in ipairs(modulefiles) do - local found = modulefile:find(moduledep["name"]) - if found == nil then - target:add("cxxflags", "-fmodule-file=" .. modulefile, {force = true}) - end - end - end objectbuilder.build_object(target, sourcefile, opt2) -- compile *.pcm to object files -- cgit v1.3.1 From f4056bea6f7bf0cc10cc54ead8305d448d5d1fae Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 19 Jan 2022 09:55:33 +0800 Subject: Update module_parser.lua --- xmake/rules/c++/modules/build_modules/module_parser.lua | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/xmake/rules/c++/modules/build_modules/module_parser.lua b/xmake/rules/c++/modules/build_modules/module_parser.lua index 881f2b48f..5f7bee717 100644 --- a/xmake/rules/c++/modules/build_modules/module_parser.lua +++ b/xmake/rules/c++/modules/build_modules/module_parser.lua @@ -84,14 +84,15 @@ function load(target, sourcebatch, opt) generate(target, sourcebatch, opt) -- load deps - local moduledeps = moduledeps or {} + local moduledeps for _, sourcefile in ipairs(sourcebatch.sourcefiles) do local dependfile = _get_dependfile_of_modulesource(target, sourcefile) if os.isfile(dependfile) then local data = io.load(dependfile) if data then - if data.moduleinfo then - local moduleinfo = data.moduleinfo + local moduleinfo = data.moduleinfo + if moduleinfo then + moduledeps = moduledeps or {} moduledeps[moduleinfo.name] = moduleinfo end end -- cgit v1.3.1