From 9e5040cb79e14597373b5bbeba4bbd6fecd847d2 Mon Sep 17 00:00:00 2001 From: Gly Date: Wed, 13 Sep 2023 13:10:46 +0200 Subject: add modules in built source for xmake project --- xmake/plugins/project/clang/compile_commands.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/plugins/project/clang/compile_commands.lua b/xmake/plugins/project/clang/compile_commands.lua index 68ba71c48..90b80e0b9 100644 --- a/xmake/plugins/project/clang/compile_commands.lua +++ b/xmake/plugins/project/clang/compile_commands.lua @@ -38,7 +38,7 @@ end function _sourcebatch_is_built(sourcebatch) -- we can only use rulename to filter them because sourcekind may be bound to multiple rules local rulename = sourcebatch.rulename - if rulename == "c.build" or rulename == "c++.build" + if rulename == "c.build" or rulename == "c++.build" or rulename == "c++.build.modules" or rulename == "asm.build" or rulename == "cuda.build" or rulename == "objc.build" or rulename == "objc++.build" then return true -- cgit v1.3.1 From 1d55d00c22cde3f5b7cf7b7ee7a42fb477298856 Mon Sep 17 00:00:00 2001 From: Gly Date: Wed, 13 Sep 2023 13:12:15 +0200 Subject: in "c++.build.modules" rule, add required flags to modules --- xmake/rules/c++/modules/xmake.lua | 48 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/xmake/rules/c++/modules/xmake.lua b/xmake/rules/c++/modules/xmake.lua index ab90f67a7..9155f1a6f 100644 --- a/xmake/rules/c++/modules/xmake.lua +++ b/xmake/rules/c++/modules/xmake.lua @@ -57,6 +57,54 @@ rule("c++.build.modules") target:data_set("cxx.has_modules", true) end end) + before_buildcmd_files(function(target, batchcmds, sourcebatch, opt) + if target:data("cxx.has_modules") then + import("modules_support.common") + + local target_clone = target:clone() + + common.patch_sourcebatch(target_clone, sourcebatch, opt) + + local modules = common.get_module_dependencies(target, sourcebatch, opt) + + -- we need to generate modules to get their dependencies available for `get_requiresflags` + common.generate_headerunits_for_batchcmds(target_clone, batchcmds, sourcebatch, modules, opt) + common.build_modules_for_batchcmds(target_clone, batchcmds, sourcebatch, modules, opt) + + -- for each module, we add required flags + local module_support = common.modules_support(target) + for _, objectfile in ipairs(sourcebatch.objectfiles) do + local module = modules[objectfile] + if module and module.requires then + local requiresflags = module_support.get_requiresflags(target, module.requires) + + local sourcefile = nil + for _, mod in pairs(module.provides) do + if mod.sourcefile then + sourcefile = mod.sourcefile + break + end + end + if sourcefile then + target:fileconfig_add(sourcefile, {force = {cxxflags = requiresflags}}) + end + end + end + + -- debugging values + local compinst = target:compiler("cxx") + for _, sourcefile in ipairs(sourcebatch.sourcefiles) do + local compflags = compinst:compflags({sourcefile = sourcefile, target = target}) + print("source: ", sourcefile, "compflags: ", compflags) + end + local compflags = compinst:compflags({sourcefile = "src/main.cpp", target = target}) + print("source: ", "src/main.cpp", "compflags: ", compflags) + + else + -- avoid duplicate linking of object files of non-module programs + sourcebatch.objectfiles = {} + end + end) -- build modules rule("c++.build.modules.builder") -- cgit v1.3.1 From 9e0fdd520ddabedc8ae4d2c17cbd66db698afaa5 Mon Sep 17 00:00:00 2001 From: Gly Date: Wed, 13 Sep 2023 19:07:22 +0200 Subject: Remove before_buildcmd_files from c++.build.modules --- xmake/rules/c++/modules/xmake.lua | 48 --------------------------------------- 1 file changed, 48 deletions(-) diff --git a/xmake/rules/c++/modules/xmake.lua b/xmake/rules/c++/modules/xmake.lua index 9155f1a6f..ab90f67a7 100644 --- a/xmake/rules/c++/modules/xmake.lua +++ b/xmake/rules/c++/modules/xmake.lua @@ -57,54 +57,6 @@ rule("c++.build.modules") target:data_set("cxx.has_modules", true) end end) - before_buildcmd_files(function(target, batchcmds, sourcebatch, opt) - if target:data("cxx.has_modules") then - import("modules_support.common") - - local target_clone = target:clone() - - common.patch_sourcebatch(target_clone, sourcebatch, opt) - - local modules = common.get_module_dependencies(target, sourcebatch, opt) - - -- we need to generate modules to get their dependencies available for `get_requiresflags` - common.generate_headerunits_for_batchcmds(target_clone, batchcmds, sourcebatch, modules, opt) - common.build_modules_for_batchcmds(target_clone, batchcmds, sourcebatch, modules, opt) - - -- for each module, we add required flags - local module_support = common.modules_support(target) - for _, objectfile in ipairs(sourcebatch.objectfiles) do - local module = modules[objectfile] - if module and module.requires then - local requiresflags = module_support.get_requiresflags(target, module.requires) - - local sourcefile = nil - for _, mod in pairs(module.provides) do - if mod.sourcefile then - sourcefile = mod.sourcefile - break - end - end - if sourcefile then - target:fileconfig_add(sourcefile, {force = {cxxflags = requiresflags}}) - end - end - end - - -- debugging values - local compinst = target:compiler("cxx") - for _, sourcefile in ipairs(sourcebatch.sourcefiles) do - local compflags = compinst:compflags({sourcefile = sourcefile, target = target}) - print("source: ", sourcefile, "compflags: ", compflags) - end - local compflags = compinst:compflags({sourcefile = "src/main.cpp", target = target}) - print("source: ", "src/main.cpp", "compflags: ", compflags) - - else - -- avoid duplicate linking of object files of non-module programs - sourcebatch.objectfiles = {} - end - end) -- build modules rule("c++.build.modules.builder") -- cgit v1.3.1 From d978d57e5a5c1ea55bf328888fa38f7a4a9dad8e Mon Sep 17 00:00:00 2001 From: Gly Date: Wed, 13 Sep 2023 19:09:13 +0200 Subject: add requires flags to module in build_modules_for_batchcmds --- xmake/rules/c++/modules/modules_support/clang.lua | 2 ++ xmake/rules/c++/modules/modules_support/msvc.lua | 2 ++ 2 files changed, 4 insertions(+) diff --git a/xmake/rules/c++/modules/modules_support/clang.lua b/xmake/rules/c++/modules/modules_support/clang.lua index 32cdc4d4f..1254aa29a 100644 --- a/xmake/rules/c++/modules/modules_support/clang.lua +++ b/xmake/rules/c++/modules/modules_support/clang.lua @@ -711,6 +711,8 @@ function build_modules_for_batchcmds(target, batchcmds, objectfiles, modules, op _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, {namedmodule = true}) + -- add requiresflags to module. it will be used for project generation + target:fileconfig_add(cppfile, {force = {cxxflags = requiresflags}}) end _batchcmds_compile(batchcmds, target, file, table.join(flags, not provide and {"-x", "c++"} or {}, {"-c", file, "-o", path(objectfile)})) diff --git a/xmake/rules/c++/modules/modules_support/msvc.lua b/xmake/rules/c++/modules/modules_support/msvc.lua index 0643117d0..e90ae3df3 100644 --- a/xmake/rules/c++/modules/modules_support/msvc.lua +++ b/xmake/rules/c++/modules/modules_support/msvc.lua @@ -557,6 +557,8 @@ function build_modules_for_batchcmds(target, batchcmds, objectfiles, modules, op _add_objectfile_to_link_arguments(target, path.translate(objectfile)) if provide then _add_module_to_mapper(target, referenceflag, name, name, objectfile, provide.bmi, requiresflags) + -- add requiresflags to module. it will be used for project generation + target:fileconfig_add(cppfile, {force = {cxxflags = requiresflags}}) end depmtime = math.max(depmtime, os.mtime(provide and provide.bmi or objectfile)) elseif requiresflags then -- cgit v1.3.1 From e61af464265134482e9f1058b9e60d0e4b14b341 Mon Sep 17 00:00:00 2001 From: Gabin Lefranc Date: Wed, 13 Sep 2023 19:57:07 +0200 Subject: use c++.build.modules.builder instead of c++.build.modules --- xmake/plugins/project/clang/compile_commands.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/plugins/project/clang/compile_commands.lua b/xmake/plugins/project/clang/compile_commands.lua index 90b80e0b9..b8776955f 100644 --- a/xmake/plugins/project/clang/compile_commands.lua +++ b/xmake/plugins/project/clang/compile_commands.lua @@ -38,7 +38,7 @@ end function _sourcebatch_is_built(sourcebatch) -- we can only use rulename to filter them because sourcekind may be bound to multiple rules local rulename = sourcebatch.rulename - if rulename == "c.build" or rulename == "c++.build" or rulename == "c++.build.modules" + if rulename == "c.build" or rulename == "c++.build" or rulename == "c++.build.modules.builder" or rulename == "asm.build" or rulename == "cuda.build" or rulename == "objc.build" or rulename == "objc++.build" then return true -- cgit v1.3.1 From 1a677008c229118aee6c5d7011d9999ecdbdaca5 Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 15 Sep 2023 11:09:02 +0800 Subject: Update compile_commands.lua --- xmake/plugins/project/clang/compile_commands.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/plugins/project/clang/compile_commands.lua b/xmake/plugins/project/clang/compile_commands.lua index b8776955f..68ba71c48 100644 --- a/xmake/plugins/project/clang/compile_commands.lua +++ b/xmake/plugins/project/clang/compile_commands.lua @@ -38,7 +38,7 @@ end function _sourcebatch_is_built(sourcebatch) -- we can only use rulename to filter them because sourcekind may be bound to multiple rules local rulename = sourcebatch.rulename - if rulename == "c.build" or rulename == "c++.build" or rulename == "c++.build.modules.builder" + if rulename == "c.build" or rulename == "c++.build" or rulename == "asm.build" or rulename == "cuda.build" or rulename == "objc.build" or rulename == "objc++.build" then return true -- cgit v1.3.1 From 49955f31fb09851dd6619f67f70ecc6a0b067272 Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 15 Sep 2023 11:09:16 +0800 Subject: Update clang.lua --- xmake/rules/c++/modules/modules_support/clang.lua | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/xmake/rules/c++/modules/modules_support/clang.lua b/xmake/rules/c++/modules/modules_support/clang.lua index 1254aa29a..8bd94eb2b 100644 --- a/xmake/rules/c++/modules/modules_support/clang.lua +++ b/xmake/rules/c++/modules/modules_support/clang.lua @@ -235,7 +235,8 @@ end -- do compile for batchcmds -- @note we need to use batchcmds:compilev to translate paths in compflags for generator, e.g. -Ixx -function _batchcmds_compile(batchcmds, target, sourcefile, flags) +function _batchcmds_compile(batchcmds, target, flags, sourcefile) + opt = opt or {} local compinst = target:compiler("cxx") local compflags = compinst:compflags({sourcefile = sourcefile, target = target}) batchcmds:compilev(table.join(compflags or {}, flags), {compiler = compinst, sourcekind = "cxx"}) @@ -709,13 +710,13 @@ function build_modules_for_batchcmds(target, batchcmds, objectfiles, modules, op batchcmds:mkdir(path.directory(objectfile)) if provide then _batchcmds_compile(batchcmds, target, table.join(flags, - {"-x", "c++-module", "--precompile", "-c", path(cppfile), "-o", path(provide.bmi)})) + {"-x", "c++-module", "--precompile", "-c", path(cppfile), "-o", path(provide.bmi)}), cppfile) _add_module_to_mapper(target, name, provide.bmi, {namedmodule = true}) -- add requiresflags to module. it will be used for project generation target:fileconfig_add(cppfile, {force = {cxxflags = requiresflags}}) end - _batchcmds_compile(batchcmds, target, file, table.join(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)}), file) target:add("objectfiles", objectfile) elseif requiresflags then local cxxflags = {} -- cgit v1.3.1 From 63532f552099a0e3f29c7ebc497b6c15ab5bfcfe Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 15 Sep 2023 11:09:38 +0800 Subject: Update clang.lua --- xmake/rules/c++/modules/modules_support/clang.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/xmake/rules/c++/modules/modules_support/clang.lua b/xmake/rules/c++/modules/modules_support/clang.lua index 8bd94eb2b..265b0247e 100644 --- a/xmake/rules/c++/modules/modules_support/clang.lua +++ b/xmake/rules/c++/modules/modules_support/clang.lua @@ -236,7 +236,6 @@ end -- do compile for batchcmds -- @note we need to use batchcmds:compilev to translate paths in compflags for generator, e.g. -Ixx function _batchcmds_compile(batchcmds, target, flags, sourcefile) - opt = opt or {} local compinst = target:compiler("cxx") local compflags = compinst:compflags({sourcefile = sourcefile, target = target}) batchcmds:compilev(table.join(compflags or {}, flags), {compiler = compinst, sourcekind = "cxx"}) -- cgit v1.3.1 From f17932e4261b76b9f5e6b3ab4564711332f7c077 Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 15 Sep 2023 11:17:12 +0800 Subject: Update gcc.lua --- xmake/rules/c++/modules/modules_support/gcc.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/rules/c++/modules/modules_support/gcc.lua b/xmake/rules/c++/modules/modules_support/gcc.lua index 4fa92e4df..51d62d78d 100644 --- a/xmake/rules/c++/modules/modules_support/gcc.lua +++ b/xmake/rules/c++/modules/modules_support/gcc.lua @@ -514,7 +514,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_compile(batchcmds, target, flags) + _batchcmds_compile(batchcmds, target, flags, cppfile) batchcmds:add_depfiles(cppfile) target:add("objectfiles", objectfile) if provide then -- cgit v1.3.1 From 2b207492f61df550cf726ad059052b2d8062dbc8 Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 15 Sep 2023 11:38:46 +0800 Subject: Update gcc.lua --- xmake/rules/c++/modules/modules_support/gcc.lua | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/xmake/rules/c++/modules/modules_support/gcc.lua b/xmake/rules/c++/modules/modules_support/gcc.lua index 51d62d78d..f08eb4199 100644 --- a/xmake/rules/c++/modules/modules_support/gcc.lua +++ b/xmake/rules/c++/modules/modules_support/gcc.lua @@ -511,13 +511,13 @@ function build_modules_for_batchcmds(target, batchcmds, objectfiles, modules, op break end end - 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_compile(batchcmds, target, flags, cppfile) - batchcmds:add_depfiles(cppfile) - target:add("objectfiles", objectfile) - if provide then + if provide or common.has_module_extension(cppfile) then + 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_compile(batchcmds, target, flags, cppfile) + batchcmds:add_depfiles(cppfile) + target:add("objectfiles", objectfile) _add_module_to_mapper(mapper_file, name, path.absolute(provide.bmi, projectdir)) end depmtime = math.max(depmtime, os.mtime(provide and provide.bmi or objectfile)) -- cgit v1.3.1 From 3f10a225581a76e8219c779518697507c9ab38de Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 15 Sep 2023 11:54:05 +0800 Subject: Update gcc.lua --- xmake/rules/c++/modules/modules_support/gcc.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/rules/c++/modules/modules_support/gcc.lua b/xmake/rules/c++/modules/modules_support/gcc.lua index f08eb4199..762810f08 100644 --- a/xmake/rules/c++/modules/modules_support/gcc.lua +++ b/xmake/rules/c++/modules/modules_support/gcc.lua @@ -113,7 +113,7 @@ end -- @note we need to use batchcmds:compilev to translate paths in compflags for generator, e.g. -Ixx function _batchcmds_compile(batchcmds, target, flags, sourcefile) local compinst = target:compiler("cxx") - local compflags = compinst:compflags({sourcefile = sourcefile}, {target = target}) + local compflags = compinst:compflags({sourcefile = sourcefile, target = target}) batchcmds:compilev(table.join(compflags or {}, flags), {compiler = compinst, sourcekind = "cxx"}) end -- cgit v1.3.1