From 05a9e1729b264a0b3cfed8a6df1c13e170d9e420 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 30 Mar 2024 23:36:12 +0800 Subject: [PATCH 1/2] improve to link depfiles --- xmake/actions/build/kinds/binary.lua | 30 +++-------------- xmake/actions/build/kinds/linkdepfiles.lua | 39 ++++++++++++++++++++++ xmake/actions/build/kinds/shared.lua | 29 +++------------- xmake/actions/build/kinds/static.lua | 29 +++------------- xmake/rules/platform/windows/def/xmake.lua | 2 ++ 5 files changed, 53 insertions(+), 76 deletions(-) create mode 100644 xmake/actions/build/kinds/linkdepfiles.lua diff --git a/xmake/actions/build/kinds/binary.lua b/xmake/actions/build/kinds/binary.lua index 2f5bad0fcf..76ccbcd9b8 100644 --- a/xmake/actions/build/kinds/binary.lua +++ b/xmake/actions/build/kinds/binary.lua @@ -27,53 +27,31 @@ import("core.project.depend") import("utils.progress") import("private.utils.batchcmds") import("object", {alias = "add_batchjobs_for_object"}) +import("linkdepfiles", {alias = "get_linkdepfiles"}) -- do link target function _do_link_target(target, opt) - - -- load linker instance local linkinst = linker.load(target:kind(), target:sourcekinds(), {target = target}) - - -- get link flags local linkflags = linkinst:linkflags({target = target}) - -- get object files - local objectfiles = target:objectfiles() - -- need build this target? - local depfiles = objectfiles - for _, dep in ipairs(target:orderdeps()) do - if dep:kind() == "static" then - if depfiles == objectfiles then - depfiles = table.copy(objectfiles) - end - table.insert(depfiles, dep:targetfile()) - end - end + local depfiles = get_linkdepfiles(target) local dryrun = option.get("dry-run") local depvalues = {linkinst:program(), linkflags} depend.on_changed(function () - - -- the target file local targetfile = target:targetfile() - - -- is verbose? - local verbose = option.get("verbose") - - -- trace progress info progress.show(opt.progress, "${color.build.target}linking.$(mode) %s", path.filename(targetfile)) - -- trace verbose info + local objectfiles = target:objectfiles() + local verbose = option.get("verbose") if verbose then -- show the full link command with raw arguments, it will expand @xxx.args for msvc/link on windows print(linkinst:linkcmd(objectfiles, targetfile, {linkflags = linkflags, rawargs = true})) end - -- link it if not dryrun then assert(linkinst:link(objectfiles, targetfile, {linkflags = linkflags})) end - end, {dependfile = target:dependfile(), lastmtime = os.mtime(target:targetfile()), changed = target:is_rebuilt(), diff --git a/xmake/actions/build/kinds/linkdepfiles.lua b/xmake/actions/build/kinds/linkdepfiles.lua new file mode 100644 index 0000000000..f96e532fc5 --- /dev/null +++ b/xmake/actions/build/kinds/linkdepfiles.lua @@ -0,0 +1,39 @@ +--!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 linkdepfiles.lua +-- + +-- get link depfiles +function main(target) + local extrafiles = {} + for _, dep in ipairs(target:orderdeps()) do + if dep:kind() == "static" then + table.insert(extrafiles, dep:targetfile()) + end + end + local linkdepfiles = target:data("linkdepfiles") + if linkdepfiles then + table.join2(extrafiles, linkdepfiles) + end + local objectfiles = target:objectfiles() + local depfiles = objectfiles + if #extrafiles > 0 then + depfiles = table.join(objectfiles, extrafiles) + end + return depfiles +end diff --git a/xmake/actions/build/kinds/shared.lua b/xmake/actions/build/kinds/shared.lua index 81cf0ebb58..67671b7ef9 100644 --- a/xmake/actions/build/kinds/shared.lua +++ b/xmake/actions/build/kinds/shared.lua @@ -27,49 +27,28 @@ import("core.project.depend") import("utils.progress") import("private.utils.batchcmds") import("object", {alias = "add_batchjobs_for_object"}) +import("linkdepfiles", {alias = "get_linkdepfiles"}) -- do link target function _do_link_target(target, opt) - - -- load linker instance local linkinst = linker.load(target:kind(), target:sourcekinds(), {target = target}) - - -- get link flags local linkflags = linkinst:linkflags({target = target}) - -- get object files - local objectfiles = target:objectfiles() - -- need build this target? - local depfiles = objectfiles - for _, dep in ipairs(target:orderdeps()) do - if dep:kind() == "static" then - if depfiles == objectfiles then - depfiles = table.copy(objectfiles) - end - table.insert(depfiles, dep:targetfile()) - end - end + local depfiles = get_linkdepfiles(target) local dryrun = option.get("dry-run") local depvalues = {linkinst:program(), linkflags} depend.on_changed(function () - - -- the target file local targetfile = target:targetfile() - - -- is verbose? - local verbose = option.get("verbose") - - -- trace progress info progress.show(opt.progress, "${color.build.target}linking.$(mode) %s", path.filename(targetfile)) - -- trace verbose info + local objectfiles = target:objectfiles() + local verbose = option.get("verbose") if verbose then -- show the full link command with raw arguments, it will expand @xxx.args for msvc/link on windows print(linkinst:linkcmd(objectfiles, targetfile, {linkflags = linkflags, rawargs = true})) end - -- link it if not dryrun then assert(linkinst:link(objectfiles, targetfile, {linkflags = linkflags})) end diff --git a/xmake/actions/build/kinds/static.lua b/xmake/actions/build/kinds/static.lua index 47668770df..11f9c7f6a6 100644 --- a/xmake/actions/build/kinds/static.lua +++ b/xmake/actions/build/kinds/static.lua @@ -27,49 +27,28 @@ import("core.project.depend") import("utils.progress") import("private.utils.batchcmds") import("object", {alias = "add_batchjobs_for_object"}) +import("linkdepfiles", {alias = "get_linkdepfiles"}) -- do link target function _do_link_target(target, opt) - - -- load linker instance local linkinst = linker.load(target:kind(), target:sourcekinds(), {target = target}) - - -- get link flags local linkflags = linkinst:linkflags({target = target}) - -- get object files - local objectfiles = target:objectfiles() - -- need build this target? - local depfiles = objectfiles - for _, dep in ipairs(target:orderdeps()) do - if dep:kind() == "static" then - if depfiles == objectfiles then - depfiles = table.copy(objectfiles) - end - table.insert(depfiles, dep:targetfile()) - end - end + local depfiles = get_linkdepfiles(target) local dryrun = option.get("dry-run") local depvalues = {linkinst:program(), linkflags} depend.on_changed(function () - - -- the target file local targetfile = target:targetfile() - - -- is verbose? - local verbose = option.get("verbose") - - -- trace progress info progress.show(opt.progress, "${color.build.target}archiving.$(mode) %s", path.filename(targetfile)) - -- trace verbose info + local objectfiles = target:objectfiles() + local verbose = option.get("verbose") if verbose then -- show the full link command with raw arguments, it will expand @xxx.args for msvc/link on windows print(linkinst:linkcmd(objectfiles, targetfile, {linkflags = linkflags, rawargs = true})) end - -- link it if not dryrun then assert(linkinst:link(objectfiles, targetfile, {linkflags = linkflags})) end diff --git a/xmake/rules/platform/windows/def/xmake.lua b/xmake/rules/platform/windows/def/xmake.lua index 8aaac52d62..5d1957b0bd 100644 --- a/xmake/rules/platform/windows/def/xmake.lua +++ b/xmake/rules/platform/windows/def/xmake.lua @@ -37,6 +37,8 @@ rule("platform.windows.def") if target:is_plat("windows") then flag = "/def:" .. flag end + -- https://github.com/xmake-io/xmake/pull/4901 + target:data_add("linkdepfiles", sourcefile) target:add("shflags", flag, {force = true}) break; end From 2b1491a54695612af04e79b24b38c2da2f794e16 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 30 Mar 2024 23:43:25 +0800 Subject: [PATCH 2/2] add more rules --- xmake/rules/linker/link_scripts/xmake.lua | 2 ++ xmake/rules/platform/windows/def/xmake.lua | 2 +- xmake/rules/platform/windows/manifest/xmake.lua | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/xmake/rules/linker/link_scripts/xmake.lua b/xmake/rules/linker/link_scripts/xmake.lua index fdd7149885..34bbc3f4fd 100644 --- a/xmake/rules/linker/link_scripts/xmake.lua +++ b/xmake/rules/linker/link_scripts/xmake.lua @@ -42,8 +42,10 @@ rule("linker.link_scripts") if target:has_tool("ld", "gcc", "gxx", "clang", "clangxx") or target:has_tool("sh", "gcc", "gxx", "clang", "clangxx") then target:add(target:is_shared() and "shflags" or "ldflags", "-T " .. scriptfile, {force = true}) + target:data_add("linkdepfiles", scriptfile) elseif target:has_tool("ld", "ld") or target:has_tool("sh", "ld") then target:add(target:is_shared() and "shflags" or "ldflags", "-T " .. scriptfile, {force = true}) + target:data_add("linkdepfiles", scriptfile) end end) diff --git a/xmake/rules/platform/windows/def/xmake.lua b/xmake/rules/platform/windows/def/xmake.lua index 5d1957b0bd..9bdcdc8747 100644 --- a/xmake/rules/platform/windows/def/xmake.lua +++ b/xmake/rules/platform/windows/def/xmake.lua @@ -38,8 +38,8 @@ rule("platform.windows.def") flag = "/def:" .. flag end -- https://github.com/xmake-io/xmake/pull/4901 - target:data_add("linkdepfiles", sourcefile) target:add("shflags", flag, {force = true}) + target:data_add("linkdepfiles", sourcefile) break; end end diff --git a/xmake/rules/platform/windows/manifest/xmake.lua b/xmake/rules/platform/windows/manifest/xmake.lua index 8d24ced973..6bde53a3d5 100644 --- a/xmake/rules/platform/windows/manifest/xmake.lua +++ b/xmake/rules/platform/windows/manifest/xmake.lua @@ -34,6 +34,7 @@ rule("platform.windows.manifest") for _, sourcefile in ipairs(sourcebatch.sourcefiles) do target:add("ldflags", "/manifestinput:" .. path.translate(sourcefile), {force = true}) target:add("shflags", "/manifestinput:" .. path.translate(sourcefile), {force = true}) + target:data_add("linkdepfiles", sourcefile) manifest = true local content = io.readfile(sourcefile) if content then