summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-11-10 23:05:38 +0800
committerruki <[email protected]>2021-11-10 23:05:38 +0800
commitbc404bc6481de0c934aa5ff4523924688b72a2d4 (patch)
tree13d21c0ca6bd6881ae110809e815d37665c4fd88
parentb6ed301feb4b223a8c0faa9dd979a722b6e04555 (diff)
fix rpath for rust
-rw-r--r--xmake/languages/rust/xmake.lua14
-rw-r--r--xmake/modules/core/tools/rustc.lua52
-rw-r--r--xmake/modules/detect/tools/rustc/has_flags.lua28
-rw-r--r--xmake/rules/rust/xmake.lua17
-rw-r--r--xmake/rules/utils/inherit_links/inherit_links.lua16
5 files changed, 100 insertions, 27 deletions
diff --git a/xmake/languages/rust/xmake.lua b/xmake/languages/rust/xmake.lua
index dc6ae3d15..fef2af363 100644
--- a/xmake/languages/rust/xmake.lua
+++ b/xmake/languages/rust/xmake.lua
@@ -45,13 +45,27 @@ language("rust")
, "target.symbols"
, "toolchain.linkdirs"
, "toolchain.rpathdirs"
+ , "config.links"
+ , "target.links"
+ , "toolchain.links"
+ , "config.syslinks"
+ , "target.syslinks"
+ , "toolchain.syslinks"
}
, shared = {
"config.linkdirs"
, "target.linkdirs"
+ , "target.rpathdirs"
, "target.strip"
, "target.symbols"
, "toolchain.linkdirs"
+ , "toolchain.rpathdirs"
+ , "config.links"
+ , "target.links"
+ , "toolchain.links"
+ , "config.syslinks"
+ , "target.syslinks"
+ , "toolchain.syslinks"
}
, static = {
"target.strip"
diff --git a/xmake/modules/core/tools/rustc.lua b/xmake/modules/core/tools/rustc.lua
index a30a389af..fa358e897 100644
--- a/xmake/modules/core/tools/rustc.lua
+++ b/xmake/modules/core/tools/rustc.lua
@@ -25,18 +25,6 @@ import("core.project.project")
-- init it
function init(self)
-
- -- init arflags
- self:set("rcarflags", "--crate-type=lib")
-
- -- init shflags
- self:set("rcshflags", "--crate-type=dylib")
-
- -- init ldflags
- self:set("rcldflags", "--crate-type=bin")
-
- -- init the file formats
- self:set("formats", { static = "lib$(name).rlib" })
end
-- make the optimize flag
@@ -67,9 +55,47 @@ function nf_linkdir(self, dir)
return {"-L" .. dir}
end
+-- make the link flag
+function nf_link(self, lib)
+ return "-l" .. lib
+end
+
+-- make the syslink flag
+function nf_syslink(self, lib)
+ return nf_link(self, lib)
+end
+
+-- make the rpathdir flag
+function nf_rpathdir(self, dir)
+ dir = path.translate(dir)
+ if self:has_flags({"-C", "link-arg=-Wl,-rpath=$ORIGIN"}, "ldflags") then
+ return {"-C", "link-arg=-Wl,-rpath=" .. (dir:gsub("@[%w_]+", function (name)
+ local maps = {["@loader_path"] = "$ORIGIN", ["@executable_path"] = "$ORIGIN"}
+ return maps[name]
+ end))}
+ elseif self:has_flags({"-C", "link-arg=-Xlinker", "-C", "link-arg=-rpath", "-C", "link-arg=-Xlinker", "-C", "link-arg=@loader_path"}, "ldflags") then
+ return {"-C", "link-arg=-Xlinker",
+ "-C", "link-arg=-rpath",
+ "-C", "link-arg=-Xlinker",
+ "-C", "link-arg=" .. (dir:gsub("%$ORIGIN", "@loader_path"))}
+ end
+end
+
-- make the build arguments list
function buildargv(self, sourcefiles, targetkind, targetfile, flags)
- return self:program(), table.join(flags, "-o", targetfile, sourcefiles)
+ -- add rpath for dylib (macho), e.g. -install_name @rpath/file.dylib
+ local flags_extra = {}
+ if targetkind == "shared" and is_plat("macosx", "iphoneos", "watchos") then
+ table.insert(flags_extra, "-C")
+ table.insert(flags_extra, "link-arg=-Xlinker")
+ table.insert(flags_extra, "-C")
+ table.insert(flags_extra, "link-arg=-install_name")
+ table.insert(flags_extra, "-C")
+ table.insert(flags_extra, "link-arg=-Xlinker")
+ table.insert(flags_extra, "-C")
+ table.insert(flags_extra, "link-arg=@rpath/" .. path.filename(targetfile))
+ end
+ return self:program(), table.join(flags, flags_extra, "-o", targetfile, sourcefiles)
end
-- build the target file
diff --git a/xmake/modules/detect/tools/rustc/has_flags.lua b/xmake/modules/detect/tools/rustc/has_flags.lua
index 904a3c85a..e1696e454 100644
--- a/xmake/modules/detect/tools/rustc/has_flags.lua
+++ b/xmake/modules/detect/tools/rustc/has_flags.lua
@@ -21,6 +21,16 @@
-- imports
import("core.cache.detectcache")
+-- is linker?
+function _islinker(flags, opt)
+ local flags_str = table.concat(flags, " ")
+ if flags_str:startswith("-C linkarg=") then
+ return true
+ end
+ local toolkind = opt.toolkind or ""
+ return toolkind == "ld" or toolkind == "sh" or toolkind:endswith("ld") or toolkind:endswith("sh")
+end
+
-- try running
function _try_running(...)
@@ -64,7 +74,7 @@ function _check_from_arglist(flags, opt)
end
-- try running to check flags
-function _check_try_running(flags, opt)
+function _check_try_running(flags, opt, islinker)
-- make an stub source file
local sourcefile = path.join(os.tmpdir(), "detect", "rustc_has_flags.rs")
@@ -72,14 +82,15 @@ function _check_try_running(flags, opt)
io.writefile(sourcefile, "fn main() {\n}")
end
- -- check it
+ -- check flags for linker
+ if islinker then
+ return _try_running(opt.program, table.join("--crate-type=bin", flags, "-o", os.tmpfile(), sourcefile), opt)
+ end
+
+ -- check flags for compiler
local objectfile = os.tmpfile() .. ".o"
local ok, errors = _try_running(opt.program, table.join("--emit", "obj", flags, "-o", objectfile, sourcefile))
-
- -- remove files
os.tryrm(objectfile)
-
- -- ok?
return ok, errors
end
@@ -91,12 +102,15 @@ end
--
function main(flags, opt)
+ -- is linker?
+ local islinker = _islinker(flags, opt)
+
-- attempt to check it from the argument list
if _check_from_arglist(flags, opt) then
return true
end
-- try running to check it
- return _check_try_running(flags, opt)
+ return _check_try_running(flags, opt, islinker)
end
diff --git a/xmake/rules/rust/xmake.lua b/xmake/rules/rust/xmake.lua
index a8ccae429..b823ef753 100644
--- a/xmake/rules/rust/xmake.lua
+++ b/xmake/rules/rust/xmake.lua
@@ -21,6 +21,23 @@
-- define rule: rust.build
rule("rust.build")
set_sourcekinds("rc")
+ on_load(function (target)
+ if target:is_static() then
+ target:set("extension", ".rlib")
+ target:add("arflags", "--crate-type=lib")
+ elseif target:is_shared() then
+ target:add("shflags", "--crate-type=dylib")
+ -- fix cannot satisfy dependencies so `std` only shows up once
+ -- https://github.com/rust-lang/rust/issues/19680
+ --
+ -- but it will link dynamic @rpath/libstd-xxx.dylib,
+ -- so we can no longer modify and set other rpath paths
+ target:add("shflags", "-C prefer-dynamic")
+ elseif target:is_binary() then
+ target:add("ldflags", "--crate-type=bin")
+ end
+ target:data_set("inherit.links.deplink", false)
+ end)
on_build("build.target")
-- define rule: rust
diff --git a/xmake/rules/utils/inherit_links/inherit_links.lua b/xmake/rules/utils/inherit_links/inherit_links.lua
index d5567b605..805f535d8 100644
--- a/xmake/rules/utils/inherit_links/inherit_links.lua
+++ b/xmake/rules/utils/inherit_links/inherit_links.lua
@@ -46,7 +46,6 @@ function _add_export_value(target, name, value)
end
end
--- main entry
function main(target)
-- disable inherit.links for `add_deps()`?
@@ -59,12 +58,15 @@ function main(target)
if targetkind == "shared" or targetkind == "static" then
local targetfile = target:targetfile()
- -- we need move target link to head
- _add_export_value(target, "links", target:linkname())
- local links = target:get("links", {rawref = true})
- if links and type(links) == "table" and #links > 1 then
- table.insert(links, 1, links[#links])
- table.remove(links, #links)
+ -- rust maybe will disable inherit links, only inherit linkdirs
+ if target:data("inherit.links.deplink") ~= false then
+ -- we need move target link to head
+ _add_export_value(target, "links", target:linkname())
+ local links = target:get("links", {rawref = true})
+ if links and type(links) == "table" and #links > 1 then
+ table.insert(links, 1, links[#links])
+ table.remove(links, #links)
+ end
end
_add_export_value(target, "linkdirs", path.directory(targetfile))