summaryrefslogtreecommitdiff
path: root/xmake/rules/rust/xmake.lua
diff options
context:
space:
mode:
authorIMXEren <[email protected]>2025-07-06 02:43:56 +0530
committerIMXEren <[email protected]>2025-07-06 05:00:32 +0530
commit8d08f0cc65c7d998265e247660ebf19bc1ba0caa (patch)
tree94d6f04dfd653abea1cf1bdf1d7f97fd611336d3 /xmake/rules/rust/xmake.lua
parent5929eeebf059979fd4a8952b74696a2c4ea33750 (diff)
fix: rust static and shared library failing to find extern crate in windows-msvc
1. rustc requires lib*.rlib, *.dll but produces *.rlib, *.dll. 2. shared library are `foo.dll`, `foo.dll.lib` and it links for `foo.dll`.
Diffstat (limited to 'xmake/rules/rust/xmake.lua')
-rw-r--r--xmake/rules/rust/xmake.lua22
1 files changed, 19 insertions, 3 deletions
diff --git a/xmake/rules/rust/xmake.lua b/xmake/rules/rust/xmake.lua
index dfdf1d047..56d54fb38 100644
--- a/xmake/rules/rust/xmake.lua
+++ b/xmake/rules/rust/xmake.lua
@@ -32,6 +32,9 @@ rule("rust.cxxbridge")
rule("rust.build")
set_sourcekinds("rc")
on_load(function (target)
+ -- set cratename
+ local cratename = target:values("rust.cratename") or target:name()
+ target:set("basename", cratename)
-- set cratetype
local cratetype = target:values("rust.cratetype")
if cratetype == "staticlib" then
@@ -43,19 +46,32 @@ rule("rust.build")
target:add("shflags", "--crate-type=cdylib", {force = true})
target:add("shflags", "-C prefer-dynamic", {force = true})
elseif target:is_static() then
+ cratetype = "lib"
+ target:set("values", "rust.cratetype", cratetype)
+ if is_plat("windows") then
+ target:set("prefixname", "lib")
+ end
target:set("extension", ".rlib")
- target:add("arflags", "--crate-type=lib", {force = true})
+ target:add("arflags", "--crate-type=" .. cratetype, {force = true})
target:data_set("inherit.links.deplink", false)
elseif target:is_shared() then
- target:add("shflags", "--crate-type=dylib", {force = true})
+ cratetype = "dylib"
+ target:set("values", "rust.cratetype", cratetype)
+ target:add("shflags", "--crate-type=" .. cratetype, {force = true})
-- 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", {force = true})
+ -- don't add links and instead use --extern CRATE[=PATH]
+ -- also, it'll automatically link from linkdirs (-L) even if no --extern
+ -- https://github.com/xmake-io/xmake/issues/6604
+ target:data_set("inherit.links.deplink", false)
elseif target:is_binary() then
- target:add("ldflags", "--crate-type=bin", {force = true})
+ cratetype = "bin"
+ target:set("values", "rust.cratetype", cratetype)
+ target:add("ldflags", "--crate-type=" .. cratetype, {force = true})
end
-- set edition