diff options
| author | ruki <[email protected]> | 2021-11-10 23:05:38 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-11-10 23:05:38 +0800 |
| commit | bc404bc6481de0c934aa5ff4523924688b72a2d4 (patch) | |
| tree | 13d21c0ca6bd6881ae110809e815d37665c4fd88 /xmake/modules | |
| parent | b6ed301feb4b223a8c0faa9dd979a722b6e04555 (diff) | |
fix rpath for rust
Diffstat (limited to 'xmake/modules')
| -rw-r--r-- | xmake/modules/core/tools/rustc.lua | 52 | ||||
| -rw-r--r-- | xmake/modules/detect/tools/rustc/has_flags.lua | 28 |
2 files changed, 60 insertions, 20 deletions
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 |
