From 9656518aa9bde944342d75a13d5d0412568754c5 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 29 May 2024 22:30:28 +0800 Subject: add target to rcflags --- xmake/toolchains/rust/xmake.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/xmake/toolchains/rust/xmake.lua b/xmake/toolchains/rust/xmake.lua index 210e1b727..1b524ae20 100644 --- a/xmake/toolchains/rust/xmake.lua +++ b/xmake/toolchains/rust/xmake.lua @@ -45,6 +45,7 @@ toolchain("rust") end end if arch and #arch:split("%-") > 1 then + toolchain:add("rcflags", "--target=" .. arch) toolchain:add("rcshflags", "--target=" .. arch) toolchain:add("rcldflags", "--target=" .. arch) else -- cgit v1.3.1 From 4ffaa7f613818c915baa56849124acc2227398cb Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 29 May 2024 22:35:23 +0800 Subject: use static lib stub for cargo --- xmake/modules/package/manager/cargo/install_package.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/xmake/modules/package/manager/cargo/install_package.lua b/xmake/modules/package/manager/cargo/install_package.lua index 7c9844c29..97fd1a07e 100644 --- a/xmake/modules/package/manager/cargo/install_package.lua +++ b/xmake/modules/package/manager/cargo/install_package.lua @@ -92,11 +92,17 @@ function main(name, opt) -- https://github.com/rust-lang/cargo/issues/10534#issuecomment-1087631050 local tomlfile = io.open(cargotoml, "a") tomlfile:print("") + tomlfile:print("[lib]") + tomlfile:print("crate-type = [\"staticlib\"]") + tomlfile:print("") tomlfile:print("[workspace]") tomlfile:print("") tomlfile:close() else local tomlfile = io.open(cargotoml, "w") + tomlfile:print("[lib]") + tomlfile:print("crate-type = [\"staticlib\"]") + tomlfile:print("") tomlfile:print("[package]") tomlfile:print("name = \"cargodeps\"") tomlfile:print("version = \"0.1.0\"") @@ -125,7 +131,7 @@ target = "%s" end -- generate main.rs - local file = io.open(path.join(sourcedir, "src", "main.rs"), "w") + local file = io.open(path.join(sourcedir, "src", "lib.rs"), "w") if configs.main == false then file:print("#![no_main]") end -- cgit v1.3.1 From b645fd80b481caf5b1ad48d57d9a6cfd3cf6841c Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 29 May 2024 23:37:32 +0800 Subject: add abort panic --- xmake/rules/rust/xmake.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/xmake/rules/rust/xmake.lua b/xmake/rules/rust/xmake.lua index 2e9295cfd..c53bb4eb3 100644 --- a/xmake/rules/rust/xmake.lua +++ b/xmake/rules/rust/xmake.lua @@ -61,6 +61,9 @@ rule("rust.build") -- set edition local edition = target:values("rust.edition") or "2018" target:add("rcflags", "--edition", edition, {force = true}) + + -- set panic + target:add("rcflags", "-C panic=abort", {force = true}) end) on_build("build.target") -- cgit v1.3.1 From 627ba35bef243770009c5a5b57c980204a9ef1d4 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 29 May 2024 23:50:32 +0800 Subject: improve panic abort for rust no_std --- xmake/rules/rust/xmake.lua | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/xmake/rules/rust/xmake.lua b/xmake/rules/rust/xmake.lua index c53bb4eb3..63c194ced 100644 --- a/xmake/rules/rust/xmake.lua +++ b/xmake/rules/rust/xmake.lua @@ -62,8 +62,22 @@ rule("rust.build") local edition = target:values("rust.edition") or "2018" target:add("rcflags", "--edition", edition, {force = true}) - -- set panic - target:add("rcflags", "-C panic=abort", {force = true}) + -- set abort panic for no_std + -- https://github.com/xmake-io/xmake/issues/4929 + local no_std = false + local sourcebatch = target:sourcebatches()["rust.build"] + if sourcebatch then + for _, sourcefile in ipairs(sourcebatch.sourcefiles) do + local content = io.readfile(sourcefile) + if content and content:find("#![no_std]", 1, true) then + no_std = true + break + end + end + end + if no_std then + target:add("rcflags", "-C panic=abort", {force = true}) + end end) on_build("build.target") -- cgit v1.3.1