From 091e754eb4d5328385fed59d5c4f92e09355ff51 Mon Sep 17 00:00:00 2001 From: ruki Date: Fri, 15 Oct 2021 23:18:52 +0800 Subject: load module deps --- xmake/rules/c++/modules/build_modules/clang.lua | 95 +++++++++++++++++ xmake/rules/c++/modules/build_modules/gcc.lua | 63 +++++++++++ .../c++/modules/build_modules/module_parser.lua | 92 ++++++++++++++++ xmake/rules/c++/modules/build_modules/msvc.lua | 118 +++++++++++++++++++++ xmake/rules/c++/modules/clang.lua | 95 ----------------- xmake/rules/c++/modules/gcc.lua | 63 ----------- xmake/rules/c++/modules/moduledeps.lua | 64 ----------- xmake/rules/c++/modules/msvc.lua | 113 -------------------- xmake/rules/c++/modules/xmake.lua | 8 +- 9 files changed, 372 insertions(+), 339 deletions(-) create mode 100644 xmake/rules/c++/modules/build_modules/clang.lua create mode 100644 xmake/rules/c++/modules/build_modules/gcc.lua create mode 100644 xmake/rules/c++/modules/build_modules/module_parser.lua create mode 100644 xmake/rules/c++/modules/build_modules/msvc.lua delete mode 100644 xmake/rules/c++/modules/clang.lua delete mode 100644 xmake/rules/c++/modules/gcc.lua delete mode 100644 xmake/rules/c++/modules/moduledeps.lua delete mode 100644 xmake/rules/c++/modules/msvc.lua diff --git a/xmake/rules/c++/modules/build_modules/clang.lua b/xmake/rules/c++/modules/build_modules/clang.lua new file mode 100644 index 000000000..7037c0767 --- /dev/null +++ b/xmake/rules/c++/modules/build_modules/clang.lua @@ -0,0 +1,95 @@ +--!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("private.action.build.object", {alias = "objectbuilder"}) + +-- 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!") + + -- the module cache directory + 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 + + -- compile module files to object files + local rootjob = opt.rootjob + local count = 0 + local sourcefiles_total = #sourcebatch.sourcefiles + for i = 1, sourcefiles_total do + local sourcefile = sourcebatch.sourcefiles[i] + batchjobs:addjob(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, {rootjob = rootjob}) + end + +end + diff --git a/xmake/rules/c++/modules/build_modules/gcc.lua b/xmake/rules/c++/modules/build_modules/gcc.lua new file mode 100644 index 000000000..d5ffbc57b --- /dev/null +++ b/xmake/rules/c++/modules/build_modules/gcc.lua @@ -0,0 +1,63 @@ +--!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"}) + +-- 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 + + -- compile module files to object files + local rootjob = opt.rootjob + for i = 1, #sourcebatch.sourcefiles do + local sourcefile = sourcebatch.sourcefiles[i] + batchjobs:addjob(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, {rootjob = rootjob}) + end + + -- add module flags + target:add("cxxflags", modulesflag) +end + diff --git a/xmake/rules/c++/modules/build_modules/module_parser.lua b/xmake/rules/c++/modules/build_modules/module_parser.lua new file mode 100644 index 000000000..a3991a5e1 --- /dev/null +++ b/xmake/rules/c++/modules/build_modules/module_parser.lua @@ -0,0 +1,92 @@ +--!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("utils.progress") + +-- 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 +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 progress + progress.show(opt.progress, "${color.build.target}generating.deps %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 + 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 + +-- 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) + 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 + moduledeps = moduledeps or {} + moduledeps[moduleinfo.name] = moduleinfo + end + end + end + return moduledeps +end diff --git a/xmake/rules/c++/modules/build_modules/msvc.lua b/xmake/rules/c++/modules/build_modules/msvc.lua new file mode 100644 index 000000000..15be07a41 --- /dev/null +++ b/xmake/rules/c++/modules/build_modules/msvc.lua @@ -0,0 +1,118 @@ +--!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") + +-- 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") 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") 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") 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!") + + -- 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 objectfile = target:objectfile(sourcefile) + local dependfile = target:dependfile(objectfile) + local modulefile = (cachedir and path.join(cachedir, path.basename(sourcefile)) or objectfile) .. ".ifc" + table.insert(modulefiles, modulefile) + table.insert(sourcebatch.objectfiles, objectfile) + table.insert(sourcebatch.dependfiles, dependfile) + end + + -- load moduledeps + local moduledeps = module_parser.load(target, sourcebatch, opt) + print(moduledeps) + + -- compile module files to object files + local rootjob = opt.rootjob + local count = 0 + local sourcefiles_total = #sourcebatch.sourcefiles + for i = 1, sourcefiles_total do + local sourcefile = sourcebatch.sourcefiles[i] + batchjobs:addjob(sourcefile, function (index, total) + local opt2 = table.join(opt, {configs = {force = {cxxflags = {interfaceflag, + outputflag .. " " .. os.args(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 .. " " .. os.args(modulefile)) + end + end + end, {rootjob = rootjob}) + end + + -- add module flags + target:add("cxxflags", modulesflag) + if cachedir then + target:add("cxxflags", "/ifcSearchDir " .. os.args(cachedir)) + end +end + diff --git a/xmake/rules/c++/modules/clang.lua b/xmake/rules/c++/modules/clang.lua deleted file mode 100644 index 7037c0767..000000000 --- a/xmake/rules/c++/modules/clang.lua +++ /dev/null @@ -1,95 +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("private.action.build.object", {alias = "objectbuilder"}) - --- 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!") - - -- the module cache directory - 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 - - -- compile module files to object files - local rootjob = opt.rootjob - local count = 0 - local sourcefiles_total = #sourcebatch.sourcefiles - for i = 1, sourcefiles_total do - local sourcefile = sourcebatch.sourcefiles[i] - batchjobs:addjob(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, {rootjob = rootjob}) - end - -end - diff --git a/xmake/rules/c++/modules/gcc.lua b/xmake/rules/c++/modules/gcc.lua deleted file mode 100644 index d5ffbc57b..000000000 --- a/xmake/rules/c++/modules/gcc.lua +++ /dev/null @@ -1,63 +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"}) - --- 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 - - -- compile module files to object files - local rootjob = opt.rootjob - for i = 1, #sourcebatch.sourcefiles do - local sourcefile = sourcebatch.sourcefiles[i] - batchjobs:addjob(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, {rootjob = rootjob}) - end - - -- add module flags - target:add("cxxflags", modulesflag) -end - diff --git a/xmake/rules/c++/modules/moduledeps.lua b/xmake/rules/c++/modules/moduledeps.lua deleted file mode 100644 index 9ae2b3cab..000000000 --- a/xmake/rules/c++/modules/moduledeps.lua +++ /dev/null @@ -1,64 +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 moduledeps.lua --- - --- imports -import("core.project.depend") -import("utils.progress") - --- generate module deps for the given file -function _generate_moduledeps(target, sourcefile, opt) - local dependfile = target:dependfile(sourcefile) - depend.on_changed(function () - - -- trace progress - progress.show(opt.progress, "${color.build.target}generating.deps %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 - module_deps = module_deps or {} - table.insert(module_deps, module_depname) - end - end - - -- save depend data - if module_name then - io.save(dependfile, {name = module_name, deps = module_deps, file = sourcefile}) - end - - end, {dependfile = dependfile, files = {sourcefile}}) -end - --- generate module deps -function generate(target, sourcebatch, opt) - for _, sourcefile in ipairs(sourcebatch.sourcefiles) do - _generate_moduledeps(target, sourcefile, opt) - end -end - diff --git a/xmake/rules/c++/modules/msvc.lua b/xmake/rules/c++/modules/msvc.lua deleted file mode 100644 index 6d100b96f..000000000 --- a/xmake/rules/c++/modules/msvc.lua +++ /dev/null @@ -1,113 +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"}) - --- 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") 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") 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") 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!") - - -- 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 objectfile = target:objectfile(sourcefile) - local dependfile = target:dependfile(objectfile) - local modulefile = (cachedir and path.join(cachedir, path.basename(sourcefile)) or objectfile) .. ".ifc" - table.insert(modulefiles, modulefile) - table.insert(sourcebatch.objectfiles, objectfile) - table.insert(sourcebatch.dependfiles, dependfile) - end - - -- compile module files to object files - local rootjob = opt.rootjob - local count = 0 - local sourcefiles_total = #sourcebatch.sourcefiles - for i = 1, sourcefiles_total do - local sourcefile = sourcebatch.sourcefiles[i] - batchjobs:addjob(sourcefile, function (index, total) - local opt2 = table.join(opt, {configs = {force = {cxxflags = {interfaceflag, - outputflag .. " " .. os.args(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 .. " " .. os.args(modulefile)) - end - end - end, {rootjob = rootjob}) - end - - -- add module flags - target:add("cxxflags", modulesflag) - if cachedir then - target:add("cxxflags", "/ifcSearchDir " .. os.args(cachedir)) - end -end - diff --git a/xmake/rules/c++/modules/xmake.lua b/xmake/rules/c++/modules/xmake.lua index 0e1e99d5d..ad36226eb 100644 --- a/xmake/rules/c++/modules/xmake.lua +++ b/xmake/rules/c++/modules/xmake.lua @@ -26,7 +26,7 @@ rule("c++.build.modules") if sourcebatches then local sourcebatch = sourcebatches["c++.build.modules"] if sourcebatch then - import("moduledeps").generate(target, sourcebatch, opt) + import("build_modules.module_parser").generate(target, sourcebatch, opt) end end end) @@ -38,11 +38,11 @@ rule("c++.build.modules") -- build module files with batchjobs local _, toolname = target:tool("cxx") if toolname:find("clang", 1, true) then - import("clang").build_with_batchjobs(target, batchjobs, sourcebatch, opt) + import("build_modules.clang").build_with_batchjobs(target, batchjobs, sourcebatch, opt) elseif toolname:find("gcc", 1, true) then - import("gcc").build_with_batchjobs(target, batchjobs, sourcebatch, opt) + import("build_modules.gcc").build_with_batchjobs(target, batchjobs, sourcebatch, opt) elseif toolname == "cl" then - import("msvc").build_with_batchjobs(target, batchjobs, sourcebatch, opt) + import("build_modules.msvc").build_with_batchjobs(target, batchjobs, sourcebatch, opt) else raise("compiler(%s): does not support c++ module!", toolname) end -- cgit v1.3.1