diff options
| author | Ângelo Andrade Cirino <[email protected]> | 2022-01-10 10:23:17 -0300 |
|---|---|---|
| committer | Ângelo Andrade Cirino <[email protected]> | 2022-01-10 10:23:17 -0300 |
| commit | ed0c0c5e0249aa966ea7061b30710abdd0b09d87 (patch) | |
| tree | 2a51c869a3aea1a093ed569e749bdf0465634a6c /xmake/rules/c++/modules/build_modules/module_parser.lua | |
| parent | 885d00da8caf74aeed758d030b9a1b50d9078e1f (diff) | |
| parent | 2431dd7e6142ff98b55741cfd4de4d2e69f4f8b5 (diff) | |
Merged from upstream/master
Diffstat (limited to 'xmake/rules/c++/modules/build_modules/module_parser.lua')
| -rw-r--r-- | xmake/rules/c++/modules/build_modules/module_parser.lua | 118 |
1 files changed, 118 insertions, 0 deletions
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..b97b7e1d0 --- /dev/null +++ b/xmake/rules/c++/modules/build_modules/module_parser.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 module_parser.lua +-- + +-- imports +import("core.project.depend") + +-- 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 + +-- 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 + moduledeps = moduledeps or {} + moduledeps[moduleinfo.name] = moduleinfo + end + end + end + return moduledeps +end + +-- build module deps +function build(moduledeps) + local moduledeps_files = {} + for _, moduledep in pairs(moduledeps) do + if moduledep.deps then + for _, depname in ipairs(moduledep.deps) do + local dep = moduledeps[depname] + if dep then + dep.parents = dep.parents or {} + table.insert(dep.parents, moduledep) + end + end + end + moduledeps_files[moduledep.file] = moduledep + end + return moduledeps_files +end |
