summaryrefslogtreecommitdiff
path: root/xmake/rules/c++/modules/build_modulefiles.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2021-10-12 15:26:35 +0800
committerGitHub <[email protected]>2021-10-12 15:26:35 +0800
commit417887dbd608961de809e7955be66fa9a9859176 (patch)
treef38bc795fa15f14b816edd06db84e6118874105b /xmake/rules/c++/modules/build_modulefiles.lua
parent09367e816155b80499ee5965ef686b55df54a590 (diff)
Update build_modulefiles.lua
Diffstat (limited to 'xmake/rules/c++/modules/build_modulefiles.lua')
-rw-r--r--xmake/rules/c++/modules/build_modulefiles.lua85
1 files 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