diff options
| author | ruki <[email protected]> | 2023-08-08 23:46:13 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2023-08-08 23:46:13 +0800 |
| commit | 1494182a09187cf49edc9a19badc3be287e8a327 (patch) | |
| tree | 813eca4bc9baf33b3f305cce48c408612c4784e1 | |
| parent | 75e7ddd9d604fc4c762dbc0e187c4a0d0ae231cc (diff) | |
improve rust deps to support cross build
| -rw-r--r-- | tests/projects/rust/cargo_deps_cross_build/Cargo.toml (renamed from tests/projects/rust/cargo_deps_without_std/Cargo.toml) | 0 | ||||
| -rw-r--r-- | tests/projects/rust/cargo_deps_cross_build/src/main.rs (renamed from tests/projects/rust/cargo_deps_without_std/src/main.rs) | 0 | ||||
| -rw-r--r-- | tests/projects/rust/cargo_deps_cross_build/xmake.lua | 11 | ||||
| -rw-r--r-- | tests/projects/rust/cargo_deps_without_std/.cargo/config.toml | 2 | ||||
| -rw-r--r-- | tests/projects/rust/cargo_deps_without_std/xmake.lua | 7 | ||||
| -rw-r--r-- | xmake/modules/package/manager/cargo/configurations.lua | 9 | ||||
| -rw-r--r-- | xmake/modules/package/manager/cargo/install_package.lua | 42 |
7 files changed, 53 insertions, 18 deletions
diff --git a/tests/projects/rust/cargo_deps_without_std/Cargo.toml b/tests/projects/rust/cargo_deps_cross_build/Cargo.toml index 6761593dc..6761593dc 100644 --- a/tests/projects/rust/cargo_deps_without_std/Cargo.toml +++ b/tests/projects/rust/cargo_deps_cross_build/Cargo.toml diff --git a/tests/projects/rust/cargo_deps_without_std/src/main.rs b/tests/projects/rust/cargo_deps_cross_build/src/main.rs index c7ce4ff1c..c7ce4ff1c 100644 --- a/tests/projects/rust/cargo_deps_without_std/src/main.rs +++ b/tests/projects/rust/cargo_deps_cross_build/src/main.rs diff --git a/tests/projects/rust/cargo_deps_cross_build/xmake.lua b/tests/projects/rust/cargo_deps_cross_build/xmake.lua new file mode 100644 index 000000000..6327b2e17 --- /dev/null +++ b/tests/projects/rust/cargo_deps_cross_build/xmake.lua @@ -0,0 +1,11 @@ +add_rules("mode.release", "mode.debug") +add_requires("cargo::test", {configs = { + std = false, + main = false, + build_target = "aarch64-unknown-none", + cargo_toml = path.join(os.projectdir(), "Cargo.toml")}}) + +target("test") + set_kind("binary") + add_files("src/main.rs") + add_packages("cargo::test") diff --git a/tests/projects/rust/cargo_deps_without_std/.cargo/config.toml b/tests/projects/rust/cargo_deps_without_std/.cargo/config.toml deleted file mode 100644 index 724febaec..000000000 --- a/tests/projects/rust/cargo_deps_without_std/.cargo/config.toml +++ /dev/null @@ -1,2 +0,0 @@ -[build] -target = "aarch64-unknown-none" diff --git a/tests/projects/rust/cargo_deps_without_std/xmake.lua b/tests/projects/rust/cargo_deps_without_std/xmake.lua deleted file mode 100644 index b9f878c13..000000000 --- a/tests/projects/rust/cargo_deps_without_std/xmake.lua +++ /dev/null @@ -1,7 +0,0 @@ -add_rules("mode.release", "mode.debug") -add_requires("cargo::test", {configs = {cargo_toml = path.join(os.projectdir(), "Cargo.toml")}}) - -target("test") - set_kind("binary") - add_files("src/main.rs") - add_packages("cargo::test") diff --git a/xmake/modules/package/manager/cargo/configurations.lua b/xmake/modules/package/manager/cargo/configurations.lua index 9a8b7f89f..a233e7ccf 100644 --- a/xmake/modules/package/manager/cargo/configurations.lua +++ b/xmake/modules/package/manager/cargo/configurations.lua @@ -22,8 +22,11 @@ function main() return { - features = {description = "set the features of dependency."}, - default_features = {description = "enables or disables any defaults provided by the dependency.", default = true}, - cargo_toml = {description = "set Cargo.toml file path"} + features = {description = "Set the features of dependency."}, + default_features = {description = "Enable or disable any defaults provided by the dependency.", default = true}, + std = {description = "Enable or disable std module."}, + main = {description = "Enable or disable main entry function."}, + build_target = {description = "Set build target. e.g. aarch64-unknown-none"}, + cargo_toml = {description = "Set Cargo.toml file path"} } end diff --git a/xmake/modules/package/manager/cargo/install_package.lua b/xmake/modules/package/manager/cargo/install_package.lua index 23955b1cc..5bc28498b 100644 --- a/xmake/modules/package/manager/cargo/install_package.lua +++ b/xmake/modules/package/manager/cargo/install_package.lua @@ -86,12 +86,38 @@ function main(name, opt) tomlfile:close() end + -- generate .cargo/config.toml + local configtoml = path.join(sourcedir, ".cargo", "config.toml") + if configs.build_target then + io.writefile(configtoml, format([[ +[build] +target = "%s" +]], configs.build_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 +132,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 configs.build_target then + os.vcp(path.join(sourcedir, "target", configs.build_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 |
