diff options
| author | ruki <[email protected]> | 2025-01-25 00:57:44 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-01-25 00:57:44 +0800 |
| commit | d1b1492b07d370f9cf06414490fc489c1dda6ed8 (patch) | |
| tree | c339b2365597111f6f6f7d9ab7b42498af16c2c5 /xmake/modules/core/tools/cl/parse_include.lua | |
| parent | d52893b18d6f837b10a61ac21b52da26238b201f (diff) | |
move parse_deps.lua
Diffstat (limited to 'xmake/modules/core/tools/cl/parse_include.lua')
| -rw-r--r-- | xmake/modules/core/tools/cl/parse_include.lua | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/xmake/modules/core/tools/cl/parse_include.lua b/xmake/modules/core/tools/cl/parse_include.lua new file mode 100644 index 000000000..700f9823f --- /dev/null +++ b/xmake/modules/core/tools/cl/parse_include.lua @@ -0,0 +1,104 @@ +--!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 parse_include.lua +-- + +-- imports +import("core.project.project") +import("core.base.hashset") +import("core.tool.toolchain") +import("core.cache.detectcache") +import("lib.detect.find_tool") +import("private.tools.vstool") + +-- probe include note prefix from cl +function probe_include_note_from_cl() + local key = "cldeps.parse_include.note" + local note = detectcache:get(key) + if not note then + local cl = find_tool("cl") + if cl then + local projectdir = os.tmpfile() .. ".cldeps" + local sourcefile = path.join(projectdir, "main.c") + local headerfile = path.join(projectdir, "foo.h") + local objectfile = sourcefile .. ".obj" + local outdata = try { function() + local runenvs = toolchain.load("msvc"):runenvs() + local argv = {"-nologo", "-showIncludes", "-c", "-Fo" .. objectfile, sourcefile} + io.writefile(headerfile, "\n") + io.writefile(sourcefile, [[ + #include "foo.h" + int main (int argc, char** argv) { + return 0; + } + ]]) + return vstool.iorunv(cl.program, argv, {envs = runenvs, curdir = projectdir}) + end} + if outdata then + for _, line in ipairs(outdata:split('\n', {plain = true})) do + note = line:match("^(.-:.-: )") + if note then + break + end + end + end + os.tryrm(projectdir) + end + detectcache:set(key, note) + detectcache:save() + end + return note +end + +-- get include notes prefix, e.g. "Note: including file: " +-- +-- @note we cannot get better solution to distinguish between `includes` and `error infos` +-- +function get_include_notes() + local notes = _g.notes + if not notes then + notes = {} + local note = probe_include_note_from_cl() + if note then + table.insert(notes, note) + end + table.join2(notes, { + "Note: including file: ", -- en + "注意: 包含文件: ", -- zh + "Remarque : inclusion du fichier : ", -- fr + "メモ: インクルード ファイル: " -- jp + }) + _g.notes = notes + end + return notes +end + +-- main entry +function main(line) + local notes = get_include_notes() + for idx, note in ipairs(notes) do + if line:startswith(note) then + -- optimization: move this note to head + if idx ~= 1 then + table.insert(notes, 1, note) + end + return line:sub(#note):trim() + end + end +end + |
