summaryrefslogtreecommitdiff
path: root/xmake/rules/c++/modules/build_modules
diff options
context:
space:
mode:
authorruki <[email protected]>2022-08-03 17:20:55 +0800
committerGitHub <[email protected]>2022-08-03 17:20:55 +0800
commitd1de4d49e4ae33ea5364bc996a64622fa59af9eb (patch)
treeff6903a74a1151035253f53fa8cb805f10eb817f /xmake/rules/c++/modules/build_modules
parent67a3575689b9761677b48bfb4588712f117924fb (diff)
parent728126bd7816bbd5fb6b93cedc7163a425343fba (diff)
Merge pull request #2623 from Arthapz/cxx20-modules-support
[Draft] Improve C++20 modules support
Diffstat (limited to 'xmake/rules/c++/modules/build_modules')
-rw-r--r--xmake/rules/c++/modules/build_modules/clang.lua128
-rw-r--r--xmake/rules/c++/modules/build_modules/gcc.lua84
-rw-r--r--xmake/rules/c++/modules/build_modules/module_parser.lua149
-rw-r--r--xmake/rules/c++/modules/build_modules/msvc.lua178
4 files changed, 0 insertions, 539 deletions
diff --git a/xmake/rules/c++/modules/build_modules/clang.lua b/xmake/rules/c++/modules/build_modules/clang.lua
deleted file mode 100644
index 120e1d5d0..000000000
--- a/xmake/rules/c++/modules/build_modules/clang.lua
+++ /dev/null
@@ -1,128 +0,0 @@
---!A cross-platform build utility based on Lua
---
--- Licensed under the Apache License, Version 2.0 (the "License");
--- you may not use this file except in compliance with the License.
--- You may obtain a copy of the License at
---
--- http://www.apache.org/licenses/LICENSE-2.0
---
--- Unless required by applicable law or agreed to in writing, software
--- distributed under the License is distributed on an "AS IS" BASIS,
--- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--- See the License for the specific language governing permissions and
--- limitations under the License.
---
--- Copyright (C) 2015-present, TBOOX Open Source Group.
---
--- @author ruki
--- @file clang.lua
---
-
--- imports
-import("core.tool.compiler")
-import("core.project.config")
-import("private.action.build.object", {alias = "objectbuilder"})
-import("module_parser")
-
--- load parent target with modules files
-function load_parent(target, 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!")
-
- -- add module flags
- target:add("cxxflags", modulesflag)
-
- -- add the module cache directory
- local cachedir = path.join(config.buildir(), ".gens", "rules", "modules", "cache")
- target:add("cxxflags", "-fmodules-cache-path=" .. cachedir, {force = true})
- target:add("cxxflags", "-fimplicit-modules", "-fimplicit-module-maps", "-fprebuilt-module-path=" .. cachedir, {force = true})
-end
-
--- build module files
-function build_with_batchjobs(target, batchjobs, 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!")
-
- -- get the module cache directory, @note we must use same cache directory for each targets
- -- @see https://github.com/xmake-io/xmake/issues/2194
- --
- local cachedir = path.join(config.buildir(), ".gens", "rules", "modules", "cache")
-
- -- we need patch objectfiles to sourcebatch for linking module objects
- sourcebatch.sourcekind = "cxx"
- sourcebatch.objectfiles = sourcebatch.objectfiles or {}
- sourcebatch.dependfiles = sourcebatch.dependfiles or {}
- for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
- local objectfile = target:objectfile(sourcefile)
- table.insert(sourcebatch.objectfiles, objectfile)
- table.insert(sourcebatch.dependfiles, target:dependfile(objectfile))
- end
-
- -- load moduledeps
- local moduledeps, moduledeps_files = module_parser.load(target, 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 moduleinfo = 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, (moduleinfo.name or path.basename(sourcefile)) .. ".pcm")
- table.insert(modulefiles, modulefile)
-
- -- make build job
- moduleinfo.job = batchjobs:newjob(sourcefile, function (index, total)
-
- -- compile module files to *.pcm
- local opt2 = table.join(opt, {configs = {force = {cxxflags = {modulesflag,
- "-fimplicit-modules", "-fimplicit-module-maps", "-fprebuilt-module-path=" .. cachedir,
- "--precompile", "-x c++-module", "-fmodules-cache-path=" .. cachedir}}}})
- opt2.progress = (index * 100) / total
- opt2.objectfile = modulefiles[i]
- opt2.dependfile = target:dependfile(opt2.objectfile)
- opt2.sourcekind = assert(sourcebatch.sourcekind, "%s: sourcekind not found!", sourcefile)
- objectbuilder.build_object(target, sourcefile, opt2)
-
- -- compile *.pcm to object files
- opt2.configs = {force = {cxxflags = {modulesflag, "-fmodules-cache-path=" .. cachedir,
- "-fimplicit-modules", "-fimplicit-module-maps", "-fprebuilt-module-path=" .. cachedir}}}
- opt2.quiet = true
- opt2.objectfile = sourcebatch.objectfiles[i]
- opt2.dependfile = sourcebatch.dependfiles[i]
- objectbuilder.build_object(target, modulefiles[i], opt2)
-
- -- add module flags to other c++ files after building all modules
- count = count + 1
- if count == sourcefiles_total then
- target:add("cxxflags", modulesflag, "-fmodules-cache-path=" .. cachedir, {force = true})
- -- FIXME It is invalid for the module implementation unit
- --target:add("cxxflags", "-fimplicit-modules", "-fimplicit-module-maps", "-fprebuilt-module-path=" .. cachedir, {force = true})
- for _, modulefile in ipairs(modulefiles) do
- target:add("cxxflags", "-fmodule-file=" .. modulefile, {force = true})
- end
- end
- end)
- end
-
- -- build batchjobs
- module_parser.build_batchjobs(moduledeps, batchjobs, opt.rootjob)
-end
diff --git a/xmake/rules/c++/modules/build_modules/gcc.lua b/xmake/rules/c++/modules/build_modules/gcc.lua
deleted file mode 100644
index 3bf8b8fcc..000000000
--- a/xmake/rules/c++/modules/build_modules/gcc.lua
+++ /dev/null
@@ -1,84 +0,0 @@
---!A cross-platform build utility based on Lua
---
--- Licensed under the Apache License, Version 2.0 (the "License");
--- you may not use this file except in compliance with the License.
--- You may obtain a copy of the License at
---
--- http://www.apache.org/licenses/LICENSE-2.0
---
--- Unless required by applicable law or agreed to in writing, software
--- distributed under the License is distributed on an "AS IS" BASIS,
--- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--- See the License for the specific language governing permissions and
--- limitations under the License.
---
--- Copyright (C) 2015-present, TBOOX Open Source Group.
---
--- @author ruki
--- @file gcc.lua
---
-
--- imports
-import("core.tool.compiler")
-import("private.action.build.object", {alias = "objectbuilder"})
-import("module_parser")
-
--- load parent target with modules files
-function load_parent(target, opt)
- -- get modules flag
- local modulesflag
- local compinst = compiler.load("cxx", {target = target})
- if compinst:has_flags("-fmodules-ts") then
- modulesflag = "-fmodules-ts"
- end
- assert(modulesflag, "compiler(gcc): does not support c++ module!")
-
- -- add module flags
- target:add("cxxflags", modulesflag)
-end
-
--- build module files
-function build_with_batchjobs(target, batchjobs, sourcebatch, opt)
-
- -- get modules flag
- local modulesflag
- local compinst = compiler.load("cxx", {target = target})
- if compinst:has_flags("-fmodules-ts") then
- modulesflag = "-fmodules-ts"
- end
- assert(modulesflag, "compiler(gcc): does not support c++ module!")
-
- -- we need patch objectfiles to sourcebatch for linking module objects
- sourcebatch.sourcekind = "cxx"
- sourcebatch.objectfiles = sourcebatch.objectfiles or {}
- sourcebatch.dependfiles = sourcebatch.dependfiles or {}
- for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
- local objectfile = target:objectfile(sourcefile)
- table.insert(sourcebatch.objectfiles, objectfile)
- table.insert(sourcebatch.dependfiles, target:dependfile(objectfile))
- end
-
- -- load moduledeps
- local moduledeps, moduledeps_files = module_parser.load(target, sourcebatch, opt)
-
- -- compile module files to object files
- for i = 1, #sourcebatch.sourcefiles do
- local sourcefile = sourcebatch.sourcefiles[i]
- local moduleinfo = assert(moduledeps_files[sourcefile], "moduleinfo(%s) not found!", sourcefile)
- moduleinfo.job = batchjobs:newjob(sourcefile, function (index, total)
- local opt2 = table.join(opt, {configs = {force = {cxxflags = {"-x c++"}}}})
- opt2.progress = (index * 100) / total
- opt2.objectfile = sourcebatch.objectfiles[i]
- opt2.dependfile = sourcebatch.dependfiles[i]
- opt2.sourcekind = assert(sourcebatch.sourcekind, "%s: sourcekind not found!", sourcefile)
- objectbuilder.build_object(target, sourcefile, opt2)
- end)
- end
-
- -- add module flags
- target:add("cxxflags", modulesflag)
-
- -- build batchjobs
- module_parser.build_batchjobs(moduledeps, batchjobs, opt.rootjob)
-end
-
diff --git a/xmake/rules/c++/modules/build_modules/module_parser.lua b/xmake/rules/c++/modules/build_modules/module_parser.lua
deleted file mode 100644
index 91a09aadb..000000000
--- a/xmake/rules/c++/modules/build_modules/module_parser.lua
+++ /dev/null
@@ -1,149 +0,0 @@
---!A cross-platform build utility based on Lua
---
--- Licensed under the Apache License, Version 2.0 (the "License");
--- you may not use this file except in compliance with the License.
--- You may obtain a copy of the License at
---
--- http://www.apache.org/licenses/LICENSE-2.0
---
--- Unless required by applicable law or agreed to in writing, software
--- distributed under the License is distributed on an "AS IS" BASIS,
--- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--- See the License for the specific language governing permissions and
--- limitations under the License.
---
--- Copyright (C) 2015-present, TBOOX Open Source Group.
---
--- @author ruki
--- @file module_parser.lua
---
-
--- imports
-import("core.project.depend")
-import("core.base.hashset")
-
--- get depend file of module source file
-function _get_dependfile_of_modulesource(target, sourcefile)
- return target:dependfile(sourcefile)
-end
-
--- get depend file of module object file, compiler will rewrite it
-function _get_dependfile_of_moduleobject(target, sourcefile)
- local objectfile = target:objectfile(sourcefile)
- return target:dependfile(objectfile)
-end
-
--- generate module deps for the given file
-function _generate_moduledeps(target, sourcefile, opt)
- local dependfile = _get_dependfile_of_modulesource(target, sourcefile)
- depend.on_changed(function ()
-
- -- trace
- vprint("generating.moduledeps %s", sourcefile)
-
- -- generating deps
- local module_name
- local module_deps
- local sourcecode = io.readfile(sourcefile)
- sourcecode = sourcecode:gsub("//.-\n", "\n")
- sourcecode = sourcecode:gsub("/%*.-%*/", "")
- for _, line in ipairs(sourcecode:split("\n", {plain = true})) do
- if not module_name then
- module_name = line:match("export%s+module%s+(.+)%s*;")
- end
- local module_depname = line:match("import%s+(.+)%s*;")
- if module_depname then
- -- partition? import :xxx;
- if module_depname:startswith(":") then
- module_depname = module_name .. module_depname
- end
- module_deps = module_deps or {}
- table.insert(module_deps, module_depname)
- end
- end
-
- -- save depend data
- if module_name then
- local dependinfo = {moduleinfo = {name = module_name, deps = module_deps, file = sourcefile}}
- return dependinfo
- end
-
- end, {dependfile = dependfile, files = {sourcefile}})
-end
-
--- build batch jobs with deps
-function _build_batchjobs_with_deps(moduledeps, batchjobs, rootjob, jobrefs, moduleinfo)
- local targetjob_ref = jobrefs[moduleinfo.name]
- if targetjob_ref then
- batchjobs:add(targetjob_ref, rootjob)
- else
- local modulejob = batchjobs:add(moduleinfo.job, rootjob)
- if modulejob then
- jobrefs[moduleinfo.name] = modulejob
- for _, depname in ipairs(moduleinfo.deps) do
- local dep = moduledeps[depname]
- if dep then -- maybe nil, e.g. `import <string>;`
- _build_batchjobs_with_deps(moduledeps, batchjobs, modulejob, jobrefs, dep)
- end
- end
- end
- end
-end
-
--- generate module deps
-function generate(target, sourcebatch, opt)
- for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
- _generate_moduledeps(target, sourcefile, opt)
- end
-end
-
--- load module deps
-function load(target, sourcebatch, opt)
-
- -- do generate first
- generate(target, sourcebatch, opt)
-
- -- load deps
- 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
- local moduleinfo = data.moduleinfo
- if moduleinfo then
- moduledeps = moduledeps or {}
- moduledeps[moduleinfo.name] = moduleinfo
- end
- end
- end
- end
-
- -- get moduledeps with file map
- local moduledeps_files = {}
- for _, moduleinfo in pairs(moduledeps) do
- moduledeps_files[moduleinfo.file] = moduleinfo
- end
- return moduledeps, moduledeps_files
-end
-
--- build batch jobs
-function build_batchjobs(moduledeps, batchjobs, rootjob)
- local depset = hashset.new()
- for _, moduleinfo in pairs(moduledeps) do
- assert(moduleinfo.job)
- for _, depname in ipairs(moduleinfo.deps) do
- depset:insert(depname)
- end
- end
- local moduledeps_root = {}
- for _, moduleinfo in pairs(moduledeps) do
- if not depset:has(moduleinfo.name) then
- table.insert(moduledeps_root, moduleinfo)
- end
- end
- local jobrefs = {}
- for _, moduleinfo in pairs(moduledeps_root) do
- _build_batchjobs_with_deps(moduledeps, batchjobs, rootjob, jobrefs, moduleinfo)
- end
-end
diff --git a/xmake/rules/c++/modules/build_modules/msvc.lua b/xmake/rules/c++/modules/build_modules/msvc.lua
deleted file mode 100644
index 0a8976468..000000000
--- a/xmake/rules/c++/modules/build_modules/msvc.lua
+++ /dev/null
@@ -1,178 +0,0 @@
---!A cross-platform build utility based on Lua
---
--- Licensed under the Apache License, Version 2.0 (the "License");
--- you may not use this file except in compliance with the License.
--- You may obtain a copy of the License at
---
--- http://www.apache.org/licenses/LICENSE-2.0
---
--- Unless required by applicable law or agreed to in writing, software
--- distributed under the License is distributed on an "AS IS" BASIS,
--- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--- See the License for the specific language governing permissions and
--- limitations under the License.
---
--- Copyright (C) 2015-present, TBOOX Open Source Group.
---
--- @author ruki
--- @file msvc.lua
---
-
--- imports
-import("core.tool.compiler")
-import("private.action.build.object", {alias = "objectbuilder"})
-import("module_parser")
-
--- load parent target with modules files
-function load_parent(target, opt)
-
- -- get modules flag
- local modulesflag
- local compinst = compiler.load("cxx", {target = target})
- if compinst:has_flags("/experimental:module", "cxxflags") then
- modulesflag = "/experimental:module"
- end
- assert(modulesflag, "compiler(msvc): does not support c++ module!")
-
- -- add module flags
- target:add("cxxflags", modulesflag)
-
- -- get output flag
- if compinst:has_flags("/ifcOutput", "cxxflags") then
- for _, dep in ipairs(target:orderdeps()) do
- local sourcebatches = dep:sourcebatches()
- if sourcebatches and sourcebatches["c++.build.modules"] then
- local cachedir = path.join(dep:autogendir(), "rules", "modules", "cache")
- target:add("cxxflags", {"/ifcSearchDir", cachedir}, {force = true, expand = false})
- end
- end
- end
-end
-
--- build module files
-function build_with_batchjobs(target, batchjobs, sourcebatch, opt)
-
- -- get modules flag
- local modulesflag
- local compinst = compiler.load("cxx", {target = target})
- if compinst:has_flags("/experimental:module", "cxxflags") then
- modulesflag = "/experimental:module"
- end
- assert(modulesflag, "compiler(msvc): does not support c++ module!")
-
- -- get output flag
- local cachedir
- local outputflag
- if compinst:has_flags("/ifcOutput", "cxxflags") then
- outputflag = "/ifcOutput"
- cachedir = path.join(target:autogendir(), "rules", "modules", "cache")
- if not os.isdir(cachedir) then
- os.mkdir(cachedir)
- end
- elseif compinst:has_flags("/module:output", "cxxflags") then
- outputflag = "/module:output"
- end
- assert(outputflag, "compiler(msvc): does not support c++ module!")
-
- -- get interface flag
- local interfaceflag
- if compinst:has_flags("/interface", "cxxflags") then
- interfaceflag = "/interface"
- elseif compinst:has_flags("/module:interface", "cxxflags") then
- interfaceflag = "/module:interface"
- end
- assert(interfaceflag, "compiler(msvc): does not support c++ module!")
-
- -- get reference flag
- local referenceflag
- if compinst:has_flags("/reference", "cxxflags") then
- referenceflag = "/reference"
- elseif compinst:has_flags("/module:interface", "cxxflags") then
- referenceflag = "/module:reference"
- end
- assert(referenceflag, "compiler(msvc): does not support c++ module!")
-
- -- get stdifcdir flag
- local stdifcdirflag
- if compinst:has_flags("/stdIfcDir", "cxxflags") then
- stdifcdirflag = "/stdIfcDir"
- elseif compinst:has_flags("/module:stdIfcDir", "cxxflags") then
- stdifcdirflag = "/module:stdIfcDir"
- end
- assert(stdifcdirflag, "compiler(msvc): does not support c++ module!")
-
- -- we need patch objectfiles to sourcebatch for linking module objects
- sourcebatch.sourcekind = "cxx"
- sourcebatch.objectfiles = sourcebatch.objectfiles or {}
- sourcebatch.dependfiles = sourcebatch.dependfiles or {}
- for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
- local objectfile = target:objectfile(sourcefile)
- local dependfile = target:dependfile(objectfile)
- table.insert(sourcebatch.objectfiles, objectfile)
- table.insert(sourcebatch.dependfiles, dependfile)
- end
-
- -- load moduledeps
- local moduledeps, moduledeps_files = module_parser.load(target, sourcebatch, opt)
-
- -- get modulefiles
- local modulefiles = {}
- for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
- local moduleinfo = moduledeps_files[sourcefile] or {}
- -- make module file path, @note we need process submodule name, e.g. module.submodule.mpp -> module.submodule.ifc
- -- @see https://github.com/xmake-io/xmake/issues/2614
- local modulefile = (cachedir and path.join(cachedir, moduleinfo.name or path.basename(sourcefile)) or objectfile) .. ".ifc"
- table.insert(modulefiles, modulefile)
- end
-
- -- compile module files to object files
- local count = 0
- local sourcefiles_total = #sourcebatch.sourcefiles
- for i = 1, sourcefiles_total do
- local sourcefile = sourcebatch.sourcefiles[i]
- local moduleinfo = assert(moduledeps_files[sourcefile], "moduleinfo(%s) not found!", sourcefile)
- moduleinfo.job = batchjobs:newjob(sourcefile, function (index, total)
- local opt2 = table.join(opt, {configs = {force = {cxxflags = {
- interfaceflag,
- {outputflag, modulefiles[i]},
- "/TP"}}}})
- opt2.progress = (index * 100) / total
- opt2.objectfile = sourcebatch.objectfiles[i]
- opt2.dependfile = sourcebatch.dependfiles[i]
- opt2.sourcekind = assert(sourcebatch.sourcekind, "%s: sourcekind not found!", sourcefile)
- objectbuilder.build_object(target, sourcefile, opt2)
-
- -- add module flags to other c++ files after building all modules
- count = count + 1
- if count == sourcefiles_total and not cachedir then
- for _, modulefile in ipairs(modulefiles) do
- target:add("cxxflags", {referenceflag, modulefile}, {force = true, expand = false})
- end
- end
- end)
- end
-
- -- add module flags
- target:add("cxxflags", modulesflag)
- if cachedir then
- target:add("cxxflags", {"/ifcSearchDir", cachedir}, {force = true, expand = false})
- end
- if stdifcdirflag then
- for _, toolchain_inst in ipairs(target:toolchains()) do
- if toolchain_inst:name() == "msvc" then
- local vcvars = toolchain_inst:config("vcvars")
- if vcvars.VCInstallDir and vcvars.VCToolsVersion then
- local stdifcdir = path.join(vcvars.VCInstallDir, "Tools", "MSVC", vcvars.VCToolsVersion, "ifc", target:is_arch("x64") and "x64" or "x86")
- if os.isdir(stdifcdir) then
- target:add("cxxflags", {stdifcdirflag, winos.short_path(stdifcdir)}, {force = true, expand = false})
- end
- end
- break
- end
- end
- end
-
- -- build batchjobs
- module_parser.build_batchjobs(moduledeps, batchjobs, opt.rootjob)
-end
-