summaryrefslogtreecommitdiff
path: root/xmake/modules/package/manager/cargo/install_package.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2023-08-09 13:35:49 +0800
committerGitHub <[email protected]>2023-08-09 13:35:49 +0800
commitcc8c1a35e4b1dd6053b86fd279ce263cffc96321 (patch)
treef6682ffaceb907be68b200d4057c711efbed1ee1 /xmake/modules/package/manager/cargo/install_package.lua
parent4182b1a29d085d59e1aca2389a2fc7ec53451885 (diff)
parentef2ba39e335136945cec07d04bd5da4bcd1725a8 (diff)
Merge pull request #4052 from xmake-io/rust
Improve rust to support cross build deps
Diffstat (limited to 'xmake/modules/package/manager/cargo/install_package.lua')
-rw-r--r--xmake/modules/package/manager/cargo/install_package.lua48
1 files changed, 42 insertions, 6 deletions
diff --git a/xmake/modules/package/manager/cargo/install_package.lua b/xmake/modules/package/manager/cargo/install_package.lua
index 23955b1cc..9daa665cd 100644
--- a/xmake/modules/package/manager/cargo/install_package.lua
+++ b/xmake/modules/package/manager/cargo/install_package.lua
@@ -22,6 +22,7 @@
import("core.base.option")
import("core.project.config")
import("lib.detect.find_tool")
+import("private.tools.rust.check_target")
-- install package
--
@@ -52,6 +53,11 @@ function main(name, opt)
require_version = "*"
end
+ -- get target
+ -- e.g. x86_64-pc-windows-msvc, aarch64-unknown-none
+ -- @see https://github.com/xmake-io/xmake/issues/4049
+ local target = check_target(opt.arch, true) and opt.arch or nil
+
-- generate Cargo.toml
local sourcedir = path.join(opt.cachedir, "source")
local cargotoml = path.join(sourcedir, "Cargo.toml")
@@ -86,12 +92,38 @@ function main(name, opt)
tomlfile:close()
end
+ -- generate .cargo/config.toml
+ local configtoml = path.join(sourcedir, ".cargo", "config.toml")
+ if target then
+ io.writefile(configtoml, format([[
+[build]
+target = "%s"
+]], target))
+ end
+
-- generate main.rs
- io.writefile(path.join(sourcedir, "src", "main.rs"), [[
-fn main() {
- println!("Hello, world!");
-}
- ]])
+ local file = io.open(path.join(sourcedir, "src", "main.rs"), "w")
+ if configs.main == false then
+ file:print("#![no_main]")
+ end
+ if configs.std == false then
+ file:print("#![no_std]")
+ end
+ if configs.main == false then
+ file:print([[
+ use core::panic::PanicInfo;
+
+ #[panic_handler]
+ fn panic(_panic: &PanicInfo<'_>) -> ! {
+ loop {}
+ }]])
+ else
+ file:print([[
+ fn main() {
+ println!("Hello, world!");
+ }]])
+ end
+ file:close()
-- do build
local argv = {"build"}
@@ -106,5 +138,9 @@ fn main() {
-- do install
local installdir = opt.installdir
os.tryrm(path.join(installdir, "lib"))
- os.vcp(path.join(sourcedir, "target", opt.mode == "debug" and "debug" or "release", "deps"), path.join(installdir, "lib"))
+ if target then
+ os.vcp(path.join(sourcedir, "target", target, opt.mode == "debug" and "debug" or "release", "deps"), path.join(installdir, "lib"))
+ else
+ os.vcp(path.join(sourcedir, "target", opt.mode == "debug" and "debug" or "release", "deps"), path.join(installdir, "lib"))
+ end
end