summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSineStriker <[email protected]>2025-04-21 01:24:27 +0800
committerruki <[email protected]>2025-04-23 11:30:30 +0800
commite16400105330feffae48c766eafc743717436ae1 (patch)
treeb6f67c23f4c3d26d9623b2b265797ea4a12cdb57
parent93dda3ce130c3a71a3d8a7bb4776af6e82bcf1c5 (diff)
Add `byproduct` API to target
-rw-r--r--xmake/actions/package/oldpkg/main.lua8
-rw-r--r--xmake/core/project/target.lua59
-rw-r--r--xmake/core/tool/linker.lua1
-rw-r--r--xmake/modules/private/action/build/link_objects.lua9
-rw-r--r--xmake/modules/target/action/clean/main.lua10
-rw-r--r--xmake/modules/target/action/install/main.lua4
-rw-r--r--xmake/plugins/pack/batchcmds.lua11
-rw-r--r--xmake/rules/utils/inherit_links/inherit_links.lua5
8 files changed, 35 insertions, 72 deletions
diff --git a/xmake/actions/package/oldpkg/main.lua b/xmake/actions/package/oldpkg/main.lua
index 394bb7683..1ce78ca7b 100644
--- a/xmake/actions/package/oldpkg/main.lua
+++ b/xmake/actions/package/oldpkg/main.lua
@@ -48,11 +48,9 @@ function _package_library(target)
-- copy *.lib for shared/windows (*.dll) target
-- @see https://github.com/xmake-io/xmake/issues/787
- if target:has_implib() then
- local target_implib = target:artifactfile("lib")
- if os.isfile(target_implib) then
- os.vcp(target_implib, format("%s/%s.pkg/$(plat)/$(arch)/lib/$(mode)/", outputdir, targetname))
- end
+ local target_implib = target:byproduct("implib")
+ if target_implib and os.isfile(target_implib) then
+ os.vcp(target_implib, format("%s/%s.pkg/$(plat)/$(arch)/lib/$(mode)/", outputdir, targetname))
end
-- copy headers
diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua
index e0756f6c5..5a6da9a12 100644
--- a/xmake/core/project/target.lua
+++ b/xmake/core/project/target.lua
@@ -1602,14 +1602,16 @@ end
-- get the target directory
function _instance:targetdir()
+
+ -- the target directory
local targetdir = self:get("targetdir")
if not targetdir then
return self:_default_targetdir()
end
-- executable, windows shared library
- if self:is_binary() or self:has_implib() then
- local subdir = self:extraconf("targetdir", targetdir, "bin")
+ if self:is_binary() or (self:is_shared() and self:is_plat("windows", "mingw")) then
+ local subdir = self:extraconf("targetdir", targetdir, "bindir")
if subdir then
targetdir = path.join(targetdir, subdir)
end
@@ -1618,7 +1620,7 @@ function _instance:targetdir()
-- static library, non-windows shared library
if self:is_static() or self:is_shared() then
- local subdir = self:extraconf("targetdir", targetdir, "lib")
+ local subdir = self:extraconf("targetdir", targetdir, "libdir")
if subdir then
targetdir = path.join(targetdir, subdir)
end
@@ -1629,56 +1631,31 @@ function _instance:targetdir()
end
-- get the build artifact output directory
-function _instance:artifactdir(type)
+function _instance:_targetdir_extra(kind)
local targetdir = self:get("targetdir")
if not targetdir then
return self:_default_targetdir()
end
- local subdir = self:extraconf("targetdir", targetdir, type)
+ local subdir = self:extraconf("targetdir", targetdir, kind)
if subdir then
return path.join(targetdir, subdir)
end
return targetdir
end
--- get the build artifact output file
+-- get the build byproduct file
--
--- supported artifact types:
--- 1. bin: executable(.exe, Unix Executables), windows shared library(.dll)
--- 2. lib: static library(.lib, .a), windows DLL implib(.lib, .dll.a), non-windows shared library(.so, .dylib)
+-- supported byproduct kinds:
+-- 1. implib: windows DLL implib(.lib, .dll.a)
--
-- otherwise returns nil
--
-function _instance:artifactfile(type)
- if type == "bin" then
- -- executable, windows shared library
- if self:is_binary() or self:has_implib() then
- return self:targetfile()
- end
-
- return nil
- end
-
- if type == "lib" then
- if self:is_static() then
- -- static library
- return self:targetfile()
- end
- if self:is_shared() then
- if self:is_plat("windows") then
- -- msvc shared library implib
- return path.join(self:artifactdir("lib"), path.basename(self:filename()) .. ".lib")
-
- elseif self:is_plat("mingw")then
- -- mingw shared library implib
- return path.join(self:artifactdir("lib"), path.basename(self:filename()) .. ".dll.a")
- else
- -- unix shared library
- return self:targetfile()
- end
+function _instance:byproduct(kind)
+ if kind == "implib" then
+ if self:is_shared() and self:is_plat("windows", "mingw") then
+ return path.join(self:_targetdir_extra("libdir"), path.basename(self:filename()) .. (self:is_plat("mingw") and ".dll.a" or ".lib"))
end
-
return nil
end
@@ -2563,14 +2540,6 @@ function _instance:has_runtime(...)
end
end
--- has implib artifact file?
---
--- returns true if the target is a windows shared library
---
-function _instance:has_implib()
- return self:is_shared() and self:is_plat("windows", "mingw")
-end
-
-- get the given toolchain
function _instance:toolchain(name)
local toolchains_map = self:_memcache():get("toolchains_map")
diff --git a/xmake/core/tool/linker.lua b/xmake/core/tool/linker.lua
index 802a7c65f..d91da81d8 100644
--- a/xmake/core/tool/linker.lua
+++ b/xmake/core/tool/linker.lua
@@ -228,7 +228,6 @@ function linker:link(objectfiles, targetfile, opt)
local linkflags = opt.linkflags or self:linkflags(opt)
opt = table.copy(opt)
opt.target = self:target()
-
profiler:enter(self:name(), "link", targetfile)
local ok, errors = sandbox.load(self:_tool().link, self:_tool(), table.wrap(objectfiles), self:_targetkind(), targetfile, linkflags, opt)
profiler:leave(self:name(), "link", targetfile)
diff --git a/xmake/modules/private/action/build/link_objects.lua b/xmake/modules/private/action/build/link_objects.lua
index 0ff51e17d..d10ae1ef4 100644
--- a/xmake/modules/private/action/build/link_objects.lua
+++ b/xmake/modules/private/action/build/link_objects.lua
@@ -47,18 +47,13 @@ function _do_link_target(target, opt)
local objectfiles = target:objectfiles()
local verbose = option.get("verbose")
- local target_implib = nil
- if target:has_implib() then
- target_implib = target:artifactfile("lib")
- end
-
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, implib = target_implib, rawargs = true}))
+ print(linkinst:linkcmd(objectfiles, targetfile, {linkflags = linkflags, implib = target:byproduct("implib"), rawargs = true}))
end
if not dryrun then
- assert(linkinst:link(objectfiles, targetfile, {linkflags = linkflags, implib = target_implib}))
+ assert(linkinst:link(objectfiles, targetfile, {linkflags = linkflags, implib = target:byproduct("implib")}))
end
end, {dependfile = target:dependfile(),
lastmtime = os.mtime(target:targetfile()),
diff --git a/xmake/modules/target/action/clean/main.lua b/xmake/modules/target/action/clean/main.lua
index b919c4d63..8d828248d 100644
--- a/xmake/modules/target/action/clean/main.lua
+++ b/xmake/modules/target/action/clean/main.lua
@@ -39,14 +39,18 @@ function main(target)
-- @see https://github.com/xmake-io/xmake/issues/3052
if target:is_shared() then
if target:is_plat("windows") then
- local expfile = path.join(target:artifactdir("lib"), path.basename(targetfile) .. ".exp")
+ local libfile = target:byproduct("implib")
+ local expfile = path.join(path.directory(libfile), path.basename(targetfile) .. ".exp")
+ if os.isfile(libfile) then
+ remove_files(libfile)
+ end
if os.isfile(expfile) then
remove_files(expfile)
end
end
- if target:has_implib() then
- local libfile = target:artifactfile("lib")
+ if target:is_plat("mingw") then
+ local libfile = target:byproduct("implib")
if os.isfile(libfile) then
remove_files(libfile)
end
diff --git a/xmake/modules/target/action/install/main.lua b/xmake/modules/target/action/install/main.lua
index 321591ea2..fa598b20c 100644
--- a/xmake/modules/target/action/install/main.lua
+++ b/xmake/modules/target/action/install/main.lua
@@ -232,12 +232,12 @@ function _install_shared(target, opt)
os.mkdir(bindir)
local targetfile = target:targetfile()
- if target:has_implib() then
+ if target:is_plat("windows", "mingw") then
-- install *.lib for shared/windows (*.dll) target
-- @see https://github.com/xmake-io/xmake/issues/714
os.vcp(target:targetfile(), bindir)
local libdir = _get_target_libdir(target, opt)
- local target_implib = target:artifact("lib")
+ local target_implib = target:byproduct("implib")
if os.isfile(target_implib) then
os.mkdir(libdir)
os.vcp(target_implib, libdir)
diff --git a/xmake/plugins/pack/batchcmds.lua b/xmake/plugins/pack/batchcmds.lua
index 4128a2cd4..ab5a33aa0 100644
--- a/xmake/plugins/pack/batchcmds.lua
+++ b/xmake/plugins/pack/batchcmds.lua
@@ -303,13 +303,10 @@ function _on_target_installcmd_shared(target, batchcmds_, opt)
-- install *.lib for shared/windows (*.dll) target
-- @see https://github.com/xmake-io/xmake/issues/714
-
- if target:has_implib() then
- local target_implib = target:artifactfile("lib")
- if os.isfile(target_implib) then
- batchcmds_:mkdir(libdir)
- batchcmds_:cp(target_implib, path.join(libdir, path.filename(target_implib)))
- end
+ local target_implib = target:byproduct("implib")
+ if target_implib and os.isfile(target_implib) then
+ batchcmds_:mkdir(libdir)
+ batchcmds_:cp(target_implib, path.join(libdir, path.filename(target_implib)))
end
_install_target_headers(target, batchcmds_, opt)
diff --git a/xmake/rules/utils/inherit_links/inherit_links.lua b/xmake/rules/utils/inherit_links/inherit_links.lua
index 3f56dc46c..74d63495c 100644
--- a/xmake/rules/utils/inherit_links/inherit_links.lua
+++ b/xmake/rules/utils/inherit_links/inherit_links.lua
@@ -80,8 +80,9 @@ function main(target)
end
end
- if target:has_implib() then
- _add_export_value(target, "linkdirs", target:artifactdir("lib"))
+ local target_implib = target:byproduct("implib")
+ if target_implib then
+ _add_export_value(target, "linkdirs", path.directory(target_implib))
else
_add_export_value(target, "linkdirs", path.directory(targetfile))
end