diff options
| author | IMXEren <[email protected]> | 2025-07-06 02:43:56 +0530 |
|---|---|---|
| committer | IMXEren <[email protected]> | 2025-07-06 05:00:32 +0530 |
| commit | 8d08f0cc65c7d998265e247660ebf19bc1ba0caa (patch) | |
| tree | 94d6f04dfd653abea1cf1bdf1d7f97fd611336d3 | |
| parent | 5929eeebf059979fd4a8952b74696a2c4ea33750 (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`.
| -rw-r--r-- | xmake/rules/rust/xmake.lua | 22 | ||||
| -rw-r--r-- | xmake/rules/utils/inherit_links/inherit_links.lua | 10 |
2 files changed, 29 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 diff --git a/xmake/rules/utils/inherit_links/inherit_links.lua b/xmake/rules/utils/inherit_links/inherit_links.lua index 8a61a2874..cdac9ace6 100644 --- a/xmake/rules/utils/inherit_links/inherit_links.lua +++ b/xmake/rules/utils/inherit_links/inherit_links.lua @@ -91,6 +91,16 @@ function main(target) -- we need to add includedirs to support import modules for golang _add_export_value(target, "includedirs", path.directory(targetfile)) end + + if target:rule("rust") then + local cratetype = target:values("rust.cratetype") + -- only bin, lib, rlib, dylib can make use of --extern CRATE[=PATH] + if cratetype ~= "staticlib" and cratetype ~= "cdylib" then + local cratename = target:values("rust.cratename") or target:name() + local extern_crate_opt = string.format('--extern %s=%s', cratename, target:targetfile()) + _add_export_value(target, "rcldflags", extern_crate_opt) + end + end end -- we export all links and linkdirs in self/packages/options to the parent target by default |
