From 27c5f41e7abd848381b286330b85155d8a2e9175 Mon Sep 17 00:00:00 2001 From: OpportunityLiu Date: Sun, 2 Jun 2019 21:59:36 +0800 Subject: improve deps parsing --- xmake/modules/core/tools/gcc.lua | 86 +++++------------------------- xmake/modules/core/tools/nvcc.lua | 35 ++++++++++-- xmake/modules/private/tools/parse_deps.lua | 79 +++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 76 deletions(-) create mode 100644 xmake/modules/private/tools/parse_deps.lua diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index 7e888090f..dc7c3c9f5 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -24,6 +24,7 @@ import("core.project.config") import("core.project.project") import("core.language.language") import("detect.tools.find_ccache") +import("private.tools.parse_deps") -- init it function init(self) @@ -327,60 +328,6 @@ function link(self, objectfiles, targetkind, targetfile, flags) os.runv(linkargv(self, objectfiles, targetkind, targetfile, flags)) end --- get include deps --- --- strcpy.o: src/tbox/libc/string/strcpy.c src/tbox/libc/string/string.h \ --- src/tbox/libc/string/prefix.h src/tbox/libc/string/../prefix.h \ --- src/tbox/libc/string/../../prefix.h \ --- src/tbox/libc/string/../../prefix/prefix.h \ --- src/tbox/libc/string/../../prefix/config.h \ --- src/tbox/libc/string/../../prefix/../config.h \ --- build/iphoneos/x86_64/release/tbox.config.h \ --- -function _get_include_deps(outdata) - - -- translate it - local results = {} - local uniques = {} - local spacech = false - for _, line in ipairs(outdata:split("\n")) do - local p = line:find(':', 1, true) - if p then - line = line:sub(p + 1) - end - if line:endswith('\\') then - line = line:sub(1, -2) - end - line = line:trim() - spacech = false - if line:find(' ', 1, true) then - line = line:gsub("\\ ", "__spacechar__") - spacech = true - end - for _, includefile in ipairs(line:split("%s+")) do - if spacech then - includefile = includefile:gsub("__spacechar__", " ") - end - includefile = includefile:trim() - if #includefile > 0 then - - -- get the relative - includefile = path.relative(includefile, project.directory()) - - -- save it if belong to the project - if path.absolute(includefile):startswith(os.projectdir()) then - - -- insert it and filter repeat - if not uniques[includefile] then - table.insert(results, includefile) - uniques[includefile] = true - end - end - end - end - end - return results -end -- make the complie arguments list for the precompiled header function _compargv1_pch(self, pcheaderfile, pcoutputfile, flags) @@ -496,11 +443,6 @@ function _compile1(self, sourcefile, objectfile, dependinfo, flags) end end - -- remove the temporary dependent file - if depfile then - os.tryrm(depfile) - end - -- raise compiling errors raise(#lines > 0 and table.concat(lines, "\n") or "") end @@ -508,7 +450,6 @@ function _compile1(self, sourcefile, objectfile, dependinfo, flags) finally { function (ok, outdata, errdata) - -- show warnings? if ok and errdata and #errdata > 0 and (option.get("diagnosis") or option.get("warning")) then local lines = errdata:split('\n') @@ -517,21 +458,22 @@ function _compile1(self, sourcefile, objectfile, dependinfo, flags) cprint("${color.warning}%s", warnings) end end + + -- generate the dependent includes + if depfile and os.isfile(depfile) then + local depdata = io.readfile(depfile) + if dependinfo and self:kind() ~= "as" and depdata then + local files = dependinfo.files or {} + table.join2(files, parse_deps(depdata)) + dependinfo.files = table.unique(files) + end + + -- remove the temporary dependent file + os.tryrm(depfile) + end end } } - - -- generate the dependent includes - if depfile and os.isfile(depfile) then - local depdata = io.readfile(depfile) - if dependinfo and self:kind() ~= "as" and depdata then - dependinfo.files = dependinfo.files or {} - table.join2(dependinfo.files, _get_include_deps(depdata)) - end - - -- remove the temporary dependent file - os.tryrm(depfile) - end end -- make the complie arguments list diff --git a/xmake/modules/core/tools/nvcc.lua b/xmake/modules/core/tools/nvcc.lua index 77cb4ce48..02996cc34 100644 --- a/xmake/modules/core/tools/nvcc.lua +++ b/xmake/modules/core/tools/nvcc.lua @@ -24,6 +24,7 @@ import("core.project.config") import("core.project.project") import("core.language.language") import("detect.tools.find_ccache") +import("private.tools.parse_deps") -- init it function init(self) @@ -58,7 +59,7 @@ function nf_symbol(self, level) -- the maps local maps = { - debug = "-g" + debug = "-g -G" } -- make it @@ -127,8 +128,8 @@ end function nf_optimize(self, level) -- the maps - local maps = - { + local maps = + { none = "-O0" , fast = "-O1" , faster = "-O2" @@ -138,7 +139,7 @@ function nf_optimize(self, level) } -- make it - return maps[level] + return maps[level] end -- make the language flag @@ -281,9 +282,22 @@ function _compile1(self, sourcefile, objectfile, dependinfo, flags) os.mkdir(path.directory(objectfile)) -- compile it + local depfile = dependinfo and os.tmpfile() or nil try { function () + -- support `-M -MF depfile.d`? + if depfile and _g._HAS_M_MF == nil then + _g._HAS_M_MF = self:has_flags({"-M", "-MF", os.nuldev()}, "cuflags") or false + end + + -- generate includes file + if depfile and _g._HAS_M_MF then + local compflags = table.join(flags, "-M", "-MF", depfile) + -- since -MD is not supported, run nvcc twice + os.iorunv(_compargv1(self, sourcefile, objectfile, compflags)) + end + local outdata, errdata = os.iorunv(_compargv1(self, sourcefile, objectfile, flags)) return (outdata or "") .. (errdata or "") end, @@ -322,6 +336,19 @@ function _compile1(self, sourcefile, objectfile, dependinfo, flags) if warnings and #warnings > 0 and (option.get("verbose") or option.get("warning")) then cprint("${color.warning}%s", table.concat(table.slice(warnings:split('\n'), 1, 8), '\n')) end + + -- generate the dependent includes + if depfile and os.isfile(depfile) then + local depdata = io.readfile(depfile) + if dependinfo and self:kind() ~= "as" and depdata then + local files = dependinfo.files or {} + table.join2(files, parse_deps(depdata)) + dependinfo.files = table.unique(files) + end + + -- remove the temporary dependent file + os.tryrm(depfile) + end end } } diff --git a/xmake/modules/private/tools/parse_deps.lua b/xmake/modules/private/tools/parse_deps.lua new file mode 100644 index 000000000..76f2acd50 --- /dev/null +++ b/xmake/modules/private/tools/parse_deps.lua @@ -0,0 +1,79 @@ +--!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 - 2019, TBOOX Open Source Group. +-- +-- @author ruki +-- @file gcc.lua +-- + +-- imports +import("core.project.project") + +-- a placeholder for spaces in path +local spacePlaceHolder = "\001*\002" + +-- normailize path of a dependecy +function _normailize_dep(dep) + dep = dep:trim() + if #dep == 0 then + return nil + end + dep = path.relative(dep, project.directory()) + dep = path.absolute(dep, project.directory()) + -- save it if belong to the project + if dep:startswith(os.projectdir()) then + -- get the relative + dep = path.relative(dep, project.directory()) + return dep + end +end + +-- parse deps file (*.d) +-- +-- eg. +-- strcpy.o: src/tbox/libc/string/strcpy.c src/tbox/libc/string/string.h \ +-- src/tbox/libc/string/prefix.h src/tbox/libc/string/../prefix.h \ +-- src/tbox/libc/string/../../prefix.h \ +-- src/tbox/libc/string/../../prefix/prefix.h \ +-- src/tbox/libc/string/../../prefix/config.h \ +-- src/tbox/libc/string/../../prefix/../config.h \ +-- build/iphoneos/x86_64/release/tbox.config.h \ +-- +function main(depsdata) + if not depsdata or #depsdata == 0 then + return {} + end + + local results = {} + + -- translate it + local data = depsdata:gsub("\\\n", "") + + for _, line in ipairs(data:split("\n")) do + local p = line:find(':', 1, true) + if p ~= nil then + line = line:sub(p + 1) + line = line:gsub("\\ ", spacePlaceHolder) + for _, includefile in ipairs(line:split("%s+")) do + includefile = includefile:gsub(spacePlaceHolder, " ") + includefile = _normailize_dep(includefile) + if includefile then + table.insert(results, includefile) + end + end + end + end + return table.unique(results) +end \ No newline at end of file -- cgit v1.3.1 From 4469331515012fe65fd30067af0eb8225fd6406e Mon Sep 17 00:00:00 2001 From: OpportunityLiu Date: Sun, 2 Jun 2019 22:07:11 +0800 Subject: move file --- xmake/modules/core/tools/gcc.lua | 2 +- xmake/modules/core/tools/nvcc.lua | 2 +- xmake/modules/private/tools/gcc/parse_deps.lua | 79 ++++++++++++++++++++++++++ xmake/modules/private/tools/parse_deps.lua | 79 -------------------------- 4 files changed, 81 insertions(+), 81 deletions(-) create mode 100644 xmake/modules/private/tools/gcc/parse_deps.lua delete mode 100644 xmake/modules/private/tools/parse_deps.lua diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua index dc7c3c9f5..8396c1746 100644 --- a/xmake/modules/core/tools/gcc.lua +++ b/xmake/modules/core/tools/gcc.lua @@ -24,7 +24,7 @@ import("core.project.config") import("core.project.project") import("core.language.language") import("detect.tools.find_ccache") -import("private.tools.parse_deps") +import("private.tools.gcc.parse_deps") -- init it function init(self) diff --git a/xmake/modules/core/tools/nvcc.lua b/xmake/modules/core/tools/nvcc.lua index 02996cc34..cc9a14f42 100644 --- a/xmake/modules/core/tools/nvcc.lua +++ b/xmake/modules/core/tools/nvcc.lua @@ -24,7 +24,7 @@ import("core.project.config") import("core.project.project") import("core.language.language") import("detect.tools.find_ccache") -import("private.tools.parse_deps") +import("private.tools.gcc.parse_deps") -- init it function init(self) diff --git a/xmake/modules/private/tools/gcc/parse_deps.lua b/xmake/modules/private/tools/gcc/parse_deps.lua new file mode 100644 index 000000000..76f2acd50 --- /dev/null +++ b/xmake/modules/private/tools/gcc/parse_deps.lua @@ -0,0 +1,79 @@ +--!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 - 2019, TBOOX Open Source Group. +-- +-- @author ruki +-- @file gcc.lua +-- + +-- imports +import("core.project.project") + +-- a placeholder for spaces in path +local spacePlaceHolder = "\001*\002" + +-- normailize path of a dependecy +function _normailize_dep(dep) + dep = dep:trim() + if #dep == 0 then + return nil + end + dep = path.relative(dep, project.directory()) + dep = path.absolute(dep, project.directory()) + -- save it if belong to the project + if dep:startswith(os.projectdir()) then + -- get the relative + dep = path.relative(dep, project.directory()) + return dep + end +end + +-- parse deps file (*.d) +-- +-- eg. +-- strcpy.o: src/tbox/libc/string/strcpy.c src/tbox/libc/string/string.h \ +-- src/tbox/libc/string/prefix.h src/tbox/libc/string/../prefix.h \ +-- src/tbox/libc/string/../../prefix.h \ +-- src/tbox/libc/string/../../prefix/prefix.h \ +-- src/tbox/libc/string/../../prefix/config.h \ +-- src/tbox/libc/string/../../prefix/../config.h \ +-- build/iphoneos/x86_64/release/tbox.config.h \ +-- +function main(depsdata) + if not depsdata or #depsdata == 0 then + return {} + end + + local results = {} + + -- translate it + local data = depsdata:gsub("\\\n", "") + + for _, line in ipairs(data:split("\n")) do + local p = line:find(':', 1, true) + if p ~= nil then + line = line:sub(p + 1) + line = line:gsub("\\ ", spacePlaceHolder) + for _, includefile in ipairs(line:split("%s+")) do + includefile = includefile:gsub(spacePlaceHolder, " ") + includefile = _normailize_dep(includefile) + if includefile then + table.insert(results, includefile) + end + end + end + end + return table.unique(results) +end \ No newline at end of file diff --git a/xmake/modules/private/tools/parse_deps.lua b/xmake/modules/private/tools/parse_deps.lua deleted file mode 100644 index 76f2acd50..000000000 --- a/xmake/modules/private/tools/parse_deps.lua +++ /dev/null @@ -1,79 +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 - 2019, TBOOX Open Source Group. --- --- @author ruki --- @file gcc.lua --- - --- imports -import("core.project.project") - --- a placeholder for spaces in path -local spacePlaceHolder = "\001*\002" - --- normailize path of a dependecy -function _normailize_dep(dep) - dep = dep:trim() - if #dep == 0 then - return nil - end - dep = path.relative(dep, project.directory()) - dep = path.absolute(dep, project.directory()) - -- save it if belong to the project - if dep:startswith(os.projectdir()) then - -- get the relative - dep = path.relative(dep, project.directory()) - return dep - end -end - --- parse deps file (*.d) --- --- eg. --- strcpy.o: src/tbox/libc/string/strcpy.c src/tbox/libc/string/string.h \ --- src/tbox/libc/string/prefix.h src/tbox/libc/string/../prefix.h \ --- src/tbox/libc/string/../../prefix.h \ --- src/tbox/libc/string/../../prefix/prefix.h \ --- src/tbox/libc/string/../../prefix/config.h \ --- src/tbox/libc/string/../../prefix/../config.h \ --- build/iphoneos/x86_64/release/tbox.config.h \ --- -function main(depsdata) - if not depsdata or #depsdata == 0 then - return {} - end - - local results = {} - - -- translate it - local data = depsdata:gsub("\\\n", "") - - for _, line in ipairs(data:split("\n")) do - local p = line:find(':', 1, true) - if p ~= nil then - line = line:sub(p + 1) - line = line:gsub("\\ ", spacePlaceHolder) - for _, includefile in ipairs(line:split("%s+")) do - includefile = includefile:gsub(spacePlaceHolder, " ") - includefile = _normailize_dep(includefile) - if includefile then - table.insert(results, includefile) - end - end - end - end - return table.unique(results) -end \ No newline at end of file -- cgit v1.3.1 From 448ec7e23eb0beaf4bd1d395656ae55b1f06c5f8 Mon Sep 17 00:00:00 2001 From: OpportunityLiu Date: Mon, 3 Jun 2019 11:28:40 +0800 Subject: fix split --- xmake/modules/private/tools/gcc/parse_deps.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/modules/private/tools/gcc/parse_deps.lua b/xmake/modules/private/tools/gcc/parse_deps.lua index 76f2acd50..ec740f220 100644 --- a/xmake/modules/private/tools/gcc/parse_deps.lua +++ b/xmake/modules/private/tools/gcc/parse_deps.lua @@ -66,7 +66,7 @@ function main(depsdata) if p ~= nil then line = line:sub(p + 1) line = line:gsub("\\ ", spacePlaceHolder) - for _, includefile in ipairs(line:split("%s+")) do + for _, includefile in ipairs(line:split("%s")) do includefile = includefile:gsub(spacePlaceHolder, " ") includefile = _normailize_dep(includefile) if includefile then -- cgit v1.3.1 From 9c7bcd8a7f9578101e71f25479daf9b5879d5b0b Mon Sep 17 00:00:00 2001 From: OpportunityLiu Date: Mon, 3 Jun 2019 15:24:52 +0800 Subject: code style --- xmake/modules/core/tools/nvcc.lua | 2 +- xmake/modules/private/tools/gcc/parse_deps.lua | 8 ++++---- xmake/modules/private/tools/nvcc/parse_deps.lua | 21 +++++++++++++++++++++ 3 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 xmake/modules/private/tools/nvcc/parse_deps.lua diff --git a/xmake/modules/core/tools/nvcc.lua b/xmake/modules/core/tools/nvcc.lua index cc9a14f42..066e86cc9 100644 --- a/xmake/modules/core/tools/nvcc.lua +++ b/xmake/modules/core/tools/nvcc.lua @@ -24,7 +24,7 @@ import("core.project.config") import("core.project.project") import("core.language.language") import("detect.tools.find_ccache") -import("private.tools.gcc.parse_deps") +import("private.tools.nvcc.parse_deps") -- init it function init(self) diff --git a/xmake/modules/private/tools/gcc/parse_deps.lua b/xmake/modules/private/tools/gcc/parse_deps.lua index ec740f220..04e97b17b 100644 --- a/xmake/modules/private/tools/gcc/parse_deps.lua +++ b/xmake/modules/private/tools/gcc/parse_deps.lua @@ -15,14 +15,14 @@ -- Copyright (C) 2015 - 2019, TBOOX Open Source Group. -- -- @author ruki --- @file gcc.lua +-- @file parse_deps.lua -- -- imports import("core.project.project") -- a placeholder for spaces in path -local spacePlaceHolder = "\001*\002" +local space_placeholder = "\001*\002" -- normailize path of a dependecy function _normailize_dep(dep) @@ -65,9 +65,9 @@ function main(depsdata) local p = line:find(':', 1, true) if p ~= nil then line = line:sub(p + 1) - line = line:gsub("\\ ", spacePlaceHolder) + line = line:gsub("\\ ", space_placeholder) for _, includefile in ipairs(line:split("%s")) do - includefile = includefile:gsub(spacePlaceHolder, " ") + includefile = includefile:gsub(space_placeholder, " ") includefile = _normailize_dep(includefile) if includefile then table.insert(results, includefile) diff --git a/xmake/modules/private/tools/nvcc/parse_deps.lua b/xmake/modules/private/tools/nvcc/parse_deps.lua new file mode 100644 index 000000000..0f9c2504c --- /dev/null +++ b/xmake/modules/private/tools/nvcc/parse_deps.lua @@ -0,0 +1,21 @@ +--!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 - 2019, TBOOX Open Source Group. +-- +-- @author OpportunityLiu +-- @file parse_deps.lua +-- + +inherit("private.tools.gcc.parse_deps") \ No newline at end of file -- cgit v1.3.1 From 18ac66eebce733b1112b419f9896cdfa2ede395a Mon Sep 17 00:00:00 2001 From: OpportunityLiu Date: Mon, 3 Jun 2019 21:01:54 +0800 Subject: fix placeholder --- xmake/modules/private/tools/gcc/parse_deps.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xmake/modules/private/tools/gcc/parse_deps.lua b/xmake/modules/private/tools/gcc/parse_deps.lua index 04e97b17b..4e9b42140 100644 --- a/xmake/modules/private/tools/gcc/parse_deps.lua +++ b/xmake/modules/private/tools/gcc/parse_deps.lua @@ -11,7 +11,7 @@ -- 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 - 2019, TBOOX Open Source Group. -- -- @author ruki @@ -22,7 +22,7 @@ import("core.project.project") -- a placeholder for spaces in path -local space_placeholder = "\001*\002" +local space_placeholder = "\001" -- normailize path of a dependecy function _normailize_dep(dep) -- cgit v1.3.1