|
| 1 | +--!A cross-platform build utility based on Lua |
| 2 | +-- |
| 3 | +-- Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +-- you may not use this file except in compliance with the License. |
| 5 | +-- You may obtain a copy of the License at |
| 6 | +-- |
| 7 | +-- http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +-- |
| 9 | +-- Unless required by applicable law or agreed to in writing, software |
| 10 | +-- distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +-- See the License for the specific language governing permissions and |
| 13 | +-- limitations under the License. |
| 14 | +-- |
| 15 | +-- Copyright (C) 2015-present, TBOOX Open Source Group. |
| 16 | +-- |
| 17 | +-- @author ruki |
| 18 | +-- @file parse_deps.lua |
| 19 | +-- |
| 20 | + |
| 21 | +-- imports |
| 22 | +import("core.project.config") |
| 23 | +import("core.project.project") |
| 24 | +import("core.base.hashset") |
| 25 | + |
| 26 | +-- normailize path of a dependecy |
| 27 | +function _normailize_dep(dep, projectdir) |
| 28 | + -- escape characters, e.g. \#Qt.Widget_pch.h -> #Qt.Widget_pch.h |
| 29 | + -- @see https://github.com/xmake-io/xmake/issues/4134 |
| 30 | + -- https://github.com/xmake-io/xmake/issues/4273 |
| 31 | + if not is_host("windows") then |
| 32 | + dep = dep:gsub("\\(.)", "%1") |
| 33 | + end |
| 34 | + if path.is_absolute(dep) then |
| 35 | + dep = path.translate(dep) |
| 36 | + else |
| 37 | + dep = path.absolute(dep, projectdir) |
| 38 | + end |
| 39 | + if dep:startswith(projectdir) then |
| 40 | + return path.relative(dep, projectdir) |
| 41 | + else |
| 42 | + -- we also need to check header files outside project |
| 43 | + -- https://github.com/xmake-io/xmake/issues/1154 |
| 44 | + return dep |
| 45 | + end |
| 46 | +end |
| 47 | + |
| 48 | +-- parse depsfiles from string |
| 49 | +-- |
| 50 | +-- parse_deps(io.readfile(depfile, {continuation = "\\"})) |
| 51 | +-- |
| 52 | +-- eg. |
| 53 | +-- |
| 54 | +-- build/.objs/foo/linux/x86_64/release/src/foo.c.o: src/foo.c |
| 55 | +-- build/.objs/foo/linux/x86_64/release/src/foo.c.o: src/foo.h |
| 56 | +-- |
| 57 | +-- build/.objs/tests/linux/x86_64/release/src/main.c.o: src/main.c |
| 58 | +-- build/.objs/tests/linux/x86_64/release/src/main.c.o: src/foo.h |
| 59 | +-- build/.objs/tests/linux/x86_64/release/src/main.c.o: src/bar.h |
| 60 | +-- build/.objs/tests/linux/x86_64/release/src/main.c.o: src/zoo.h |
| 61 | +-- |
| 62 | +function main(depsdata, opt) |
| 63 | + local results = hashset.new() |
| 64 | + local projectdir = os.projectdir() |
| 65 | + local line = depsdata:rtrim() -- maybe there will be an empty newline at the end. so we trim it first |
| 66 | + local plain = {plain = true} |
| 67 | + for _, includefile in ipairs(line:split('\n', plain)) do -- it will trim all internal spaces without `{strict = true}` |
| 68 | + includefile = includefile:split(": ", plain)[2] |
| 69 | + if includefile and #includefile > 0 then |
| 70 | + includefile = _normailize_dep(includefile, projectdir) |
| 71 | + if includefile then |
| 72 | + results:insert(includefile) |
| 73 | + end |
| 74 | + end |
| 75 | + end |
| 76 | + return results:to_array() |
| 77 | +end |
0 commit comments