diff options
| author | ruki <[email protected]> | 2022-12-19 14:00:49 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-12-19 14:00:49 +0800 |
| commit | ba059f5723f290bea5682cfac0534739e66cf219 (patch) | |
| tree | 88dcc1b15188e156101623c5c4aea71229a09b98 | |
| parent | ce7eccec1148a259ac8d73e098ceb1974deb1f5e (diff) | |
| parent | f84a1a0e45b85cbccd66e985f195bcfe031e2b67 (diff) | |
Merge pull request #3182 from xmake-io/modules
improve modules for project generator
| -rw-r--r-- | xmake/modules/private/utils/batchcmds.lua | 24 | ||||
| -rw-r--r-- | xmake/rules/c++/modules/modules_support/clang.lua | 29 | ||||
| -rw-r--r-- | xmake/rules/c++/modules/modules_support/gcc.lua | 25 | ||||
| -rw-r--r-- | xmake/rules/c++/modules/modules_support/msvc.lua | 16 |
4 files changed, 64 insertions, 30 deletions
diff --git a/xmake/modules/private/utils/batchcmds.lua b/xmake/modules/private/utils/batchcmds.lua index 797403b34..5948fe0cf 100644 --- a/xmake/modules/private/utils/batchcmds.lua +++ b/xmake/modules/private/utils/batchcmds.lua @@ -253,7 +253,26 @@ function batchcmds:compile(sourcefiles, objectfile, opt) sourcekind = language.sourcekind_of(tostring(sourcefiles)) end local compiler_inst = compiler.load(sourcekind, opt) - local program, argv = compiler_inst:compargv(sourcefiles, path(objectfile), opt) + local _, argv = compiler_inst:compargv(sourcefiles, path(objectfile), opt) + + -- add compilation command and bind run environments of compiler + self:mkdir(path.directory(objectfile)) + self:compilev(argv, table.join({sourcekind = sourcekind, compiler = compiler_inst}, opt)) +end + +-- add command: compiler.compilev +function batchcmds:compilev(argv, opt) + + -- bind target if exists + opt = opt or {} + opt.target = self._TARGET + + -- load compiler and get compilation command + local compiler_inst = opt.compiler + if not compiler_inst then + local sourcekind = opt.sourcekind + compiler_inst = compiler.load(sourcekind, opt) + end -- we need translate path for the project generator for idx, item in ipairs(argv) do @@ -269,8 +288,7 @@ function batchcmds:compile(sourcefiles, objectfile, opt) end -- add compilation command and bind run environments of compiler - self:mkdir(path.directory(objectfile)) - self:vrunv(program, argv, {envs = table.join(compiler_inst:runenvs(), opt.envs)}) + self:vrunv(compiler_inst:program(), argv, {envs = table.join(compiler_inst:runenvs(), opt.envs)}) end -- add command: linker.link diff --git a/xmake/rules/c++/modules/modules_support/clang.lua b/xmake/rules/c++/modules/modules_support/clang.lua index 5fa8845f4..3c18f6554 100644 --- a/xmake/rules/c++/modules/modules_support/clang.lua +++ b/xmake/rules/c++/modules/modules_support/clang.lua @@ -133,6 +133,14 @@ function _get_toolchain_includedirs_for_stlheaders(target, includedirs, clang) os.tryrm(tmpfile) end +-- do compile for batchcmds +-- @note we need use batchcmds:compilev to translate paths in compflags for generator, e.g. -Ixx +function _batchcmds_compile(batchcmds, target, flags) + local compinst = target:compiler("cxx") + local compflags = compinst:compflags({target = target}) + batchcmds:compilev(table.join(compflags or {}, flags), {compiler = compinst, sourcekind = "cxx"}) +end + -- build module file function _build_modulefile(target, sourcefile, opt) local objectfile = opt.objectfile @@ -300,7 +308,6 @@ end -- generate target stl header units for batchcmds function generate_stl_headerunits_for_batchcmds(target, batchcmds, headerunits, opt) - local compinst = target:compiler("cxx") local stlcachedir = common.stlmodules_cachedir(target) local modulecachepathflag = get_modulecachepathflag(target) assert(has_headerunitsupport(target), "compiler(clang): does not support c++ header units!") @@ -313,11 +320,11 @@ function generate_stl_headerunits_for_batchcmds(target, batchcmds, headerunits, -- don't build same header unit at the same time if not common.memcache():get2(headerunit.name, "building") then common.memcache():set2(headerunit.name, "building", true) - local args = { + local flags = { path(stlcachedir, function (p) return modulecachepathflag .. p end), "-c", "-o", path(bmifile), "-x", "c++-system-header", headerunit.name} batchcmds:show_progress(opt.progress, "${color.build.object}compiling.headerunit.$(mode) %s", headerunit.name) - batchcmds:vrunv(compinst:program(), table.join(compinst:compflags({target = target}), args)) + _batchcmds_compile(batchcmds, target, flags) end -- libc++ have a builtin module mapper if not target:data_set("cxx.modules.use_libc++") then @@ -385,7 +392,6 @@ end -- generate target user header units for batchcmds function generate_user_headerunits_for_batchcmds(target, batchcmds, headerunits, opt) - local compinst = target:compiler("cxx") assert(has_headerunitsupport(target), "compiler(clang): does not support c++ header units!") -- get cachedirs @@ -411,15 +417,15 @@ function generate_user_headerunits_for_batchcmds(target, batchcmds, headerunits, local bmifile = (outputdir and path.join(outputdir, bmifilename) or bmifilename) batchcmds:mkdir(path.directory(objectfile)) - local args = {path(cachedir, function (p) return modulecachepathflag .. p end), "-c", "-o", path(bmifile)} + local flags = {path(cachedir, function (p) return modulecachepathflag .. p end), "-c", "-o", path(bmifile)} if headerunit.type == ":quote" then - table.join2(args, {"-I", path(headerunit.path):directory(), "-x", "c++-user-header", path(headerunit.path)}) + table.join2(flags, {"-I", path(headerunit.path):directory(), "-x", "c++-user-header", path(headerunit.path)}) elseif headerunit.type == ":angle" then - table.join2(args, {"-x", "c++-system-header", headerunit.name}) + table.join2(flags, {"-x", "c++-system-header", headerunit.name}) end batchcmds:show_progress(opt.progress, "${color.build.object}compiling.headerunit.$(mode) %s", headerunit.name) - batchcmds:vrunv(compinst:program(), table.join(compinst:compflags({target = target}), args)) + _batchcmds_compile(batchcmds, target, flags) batchcmds:add_depfiles(headerunit.path) _add_module_to_mapper(target, headerunit.name, bmifile) @@ -504,7 +510,6 @@ end -- build module files for batchcmds function build_modules_for_batchcmds(target, batchcmds, objectfiles, modules, opt) - local compinst = target:compiler("cxx") local cachedir = common.modules_cachedir(target) local modulecachepathflag = get_modulecachepathflag(target) @@ -540,10 +545,12 @@ function build_modules_for_batchcmds(target, batchcmds, objectfiles, modules, op batchcmds:show_progress(opt.progress, "${color.build.object}compiling.module.$(mode) %s", name or cppfile) batchcmds:mkdir(path.directory(objectfile)) if provide then - batchcmds:vrunv(compinst:program(), table.join(compinst:compflags({target = target}), flags, {"-x", "c++-module", "--precompile", "-c", path(cppfile), "-o", path(provide.bmi)})) + _batchcmds_compile(batchcmds, target, table.join(flags, + {"-x", "c++-module", "--precompile", "-c", path(cppfile), "-o", path(provide.bmi)})) _add_module_to_mapper(target, name, provide.bmi) end - batchcmds:vrunv(compinst:program(), table.join(compinst:compflags({target = target}), flags, not provide and {"-x", "c++"} or {}, {"-c", file, "-o", path(objectfile)})) + _batchcmds_compile(batchcmds, target, table.join(flags, + not provide and {"-x", "c++"} or {}, {"-c", file, "-o", path(objectfile)})) target:add("objectfiles", objectfile) elseif requiresflags then target:fileconfig_add(cppfile, {force = {cxxflags = requiresflags}}) diff --git a/xmake/rules/c++/modules/modules_support/gcc.lua b/xmake/rules/c++/modules/modules_support/gcc.lua index dfa68de45..d3abfbd75 100644 --- a/xmake/rules/c++/modules/modules_support/gcc.lua +++ b/xmake/rules/c++/modules/modules_support/gcc.lua @@ -102,6 +102,14 @@ function _get_toolchain_includedirs_for_stlheaders(includedirs, gcc) os.tryrm(tmpfile) end +-- do compile for batchcmds +-- @note we need use batchcmds:compilev to translate paths in compflags for generator, e.g. -Ixx +function _batchcmds_compile(batchcmds, target, flags) + local compinst = target:compiler("cxx") + local compflags = compinst:compflags({target = target}) + batchcmds:compilev(table.join(compflags or {}, flags), {compiler = compinst, sourcekind = "cxx"}) +end + -- build module file function _build_modulefile(target, sourcefile, opt) local objectfile = opt.objectfile @@ -272,7 +280,6 @@ end -- generate target stl header units for batchcmds function generate_stl_headerunits_for_batchcmds(target, batchcmds, headerunits, opt) - local compinst = target:compiler("cxx") local mapper_file = _get_module_mapper(target) local stlcachedir = common.stlmodules_cachedir(target) @@ -282,9 +289,9 @@ function generate_stl_headerunits_for_batchcmds(target, batchcmds, headerunits, for _, headerunit in ipairs(headerunits) do local bmifile = path.join(stlcachedir, headerunit.name .. get_bmi_extension()) if not os.isfile(bmifile) then - local args = {"-c", "-x", "c++-system-header", headerunit.name} + local flags = {"-c", "-x", "c++-system-header", headerunit.name} batchcmds:show_progress(opt.progress, "${color.build.object}compiling.headerunit.$(mode) %s", headerunit.name) - batchcmds:vrunv(compinst:program(), table.join(compinst:compflags({target = target}), args)) + _batchcmds_compile(batchcmds, target, flags) end batchcmds:add_depfiles(headerunit.path) _add_module_to_mapper(mapper_file, headerunit.path, path.absolute(bmifile, projectdir)) @@ -347,7 +354,6 @@ end -- generate target user header units for batchcmds function generate_user_headerunits_for_batchcmds(target, batchcmds, headerunits, opt) - local compinst = target:compiler("cxx") local mapper_file = _get_module_mapper(target) local cachedir = common.modules_cachedir(target) @@ -369,19 +375,19 @@ function generate_user_headerunits_for_batchcmds(target, batchcmds, headerunits, local bmifile = (outputdir and path.join(outputdir, bmifilename) or bmifilename) batchcmds:mkdir(path.directory(objectfile)) - local args = {"-c"} + local flags = {"-c"} local headerunit_path if headerunit.type == ":quote" then - table.join2(args, {"-I", path(path.relative(headerunit.path, projectdir)):directory(), "-x", "c++-user-header", headerunit.name}) + table.join2(flags, {"-I", path(path.relative(headerunit.path, projectdir)):directory(), "-x", "c++-user-header", headerunit.name}) headerunit_path = path.join(".", path.relative(headerunit.path, projectdir)) elseif headerunit.type == ":angle" then - table.join2(args, {"-x", "c++-system-header", headerunit.name}) + table.join2(flags, {"-x", "c++-system-header", headerunit.name}) -- if path is relative then its a subtarget path headerunit_path = path.is_absolute(headerunit.path) and headerunit.path or path.join(".", headerunit.path) end batchcmds:show_progress(opt.progress, "${color.build.object}compiling.headerunit.$(mode) %s", headerunit.name) - batchcmds:vrunv(compinst:program(), table.join(compinst:compflags({target = target}), args)) + _batchcmds_compile(batchcmds, target, flags) batchcmds:add_depfiles(headerunit.path) _add_module_to_mapper(mapper_file, headerunit_path, path.absolute(bmifile, projectdir)) @@ -460,7 +466,6 @@ end -- build module files for batchcmds function build_modules_for_batchcmds(target, batchcmds, objectfiles, modules, opt) - local compinst = target:compiler("cxx") local modulemapperflag = get_modulemapperflag(target) local mapper_file = _get_module_mapper(target) @@ -502,7 +507,7 @@ function build_modules_for_batchcmds(target, batchcmds, objectfiles, modules, op local flags = {"-x", "c++", "-c", path(cppfile), "-o", path(objectfile)} batchcmds:show_progress(opt.progress, "${color.build.object}compiling.module.$(mode) %s", name or cppfile) batchcmds:mkdir(path.directory(objectfile)) - batchcmds:vrunv(compinst:program(), table.join(compinst:compflags({target = target}), flags)) + _batchcmds_compile(batchcmds, target, flags) batchcmds:add_depfiles(cppfile) target:add("objectfiles", objectfile) if provide then diff --git a/xmake/rules/c++/modules/modules_support/msvc.lua b/xmake/rules/c++/modules/modules_support/msvc.lua index e5c37ea38..8154742dd 100644 --- a/xmake/rules/c++/modules/modules_support/msvc.lua +++ b/xmake/rules/c++/modules/modules_support/msvc.lua @@ -69,6 +69,14 @@ function _compile(target, flags) os.vrunv(compinst:program(), winos.cmdargv(table.join(compinst:compflags({target = target}), flags)), {envs = msvc:runenvs()}) end +-- do compile for batchcmds +-- @note we need use batchcmds:compilev to translate paths in compflags for generator, e.g. -Ixx +function _batchcmds_compile(batchcmds, target, flags) + local compinst = target:compiler("cxx") + local compflags = compinst:compflags({target = target}) + batchcmds:compilev(table.join(compflags or {}, flags), {compiler = compinst, sourcekind = "cxx"}) +end + -- add an objectfile to the linker flags -- -- e.g @@ -246,11 +254,9 @@ end -- generate header unit module bmi for batchcmds function generate_headerunit_for_batchcmds(target, name, flags, objectfile, batchcmds, opt) - local compinst = target:compiler("cxx") - local msvc = target:toolchain("msvc") local common_flags = {"-TP", "-c"} batchcmds:show_progress(opt.progress, "${color.build.object}compiling.headerunit.$(mode) %s", name) - batchcmds:vrunv(compinst:program(), table.join(compinst:compflags({target = target}), common_flags, flags), {envs = msvc:runenvs()}) + _batchcmds_compile(batchcmds, target, table.join(common_flags, flags)) _add_objectfile_to_link_arguments(target, objectfile) end @@ -520,8 +526,6 @@ end -- build module files for batchcmds function build_modules_for_batchcmds(target, batchcmds, objectfiles, modules, opt) - local compinst = target:compiler("cxx") - local msvc = target:toolchain("msvc") -- get flags local ifcoutputflag = get_ifcoutputflag(target) @@ -570,7 +574,7 @@ function build_modules_for_batchcmds(target, batchcmds, objectfiles, modules, op batchcmds:show_progress(opt.progress, "${color.build.object}compiling.module.$(mode) %s", name or cppfile) batchcmds:mkdir(path.directory(objectfile)) - batchcmds:vrunv(compinst:program(), table.join(compinst:compflags({target = target}), table.join(flags, requiresflags or {})), {envs = msvc:runenvs()}) + _batchcmds_compile(batchcmds, target, table.join(flags, requiresflags or {})) batchcmds:add_depfiles(cppfile) _add_objectfile_to_link_arguments(target, path(objectfile)) if provide then |
