From 75e7ddd9d604fc4c762dbc0e187c4a0d0ae231cc Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 8 Aug 2023 23:40:49 +0800 Subject: [PATCH 01/12] add cargo without std example --- .../rust/cargo_deps_without_std/.cargo/config.toml | 2 ++ tests/projects/rust/cargo_deps_without_std/Cargo.toml | 7 +++++++ tests/projects/rust/cargo_deps_without_std/src/main.rs | 10 ++++++++++ tests/projects/rust/cargo_deps_without_std/xmake.lua | 7 +++++++ 4 files changed, 26 insertions(+) create mode 100644 tests/projects/rust/cargo_deps_without_std/.cargo/config.toml create mode 100644 tests/projects/rust/cargo_deps_without_std/Cargo.toml create mode 100644 tests/projects/rust/cargo_deps_without_std/src/main.rs create mode 100644 tests/projects/rust/cargo_deps_without_std/xmake.lua diff --git a/tests/projects/rust/cargo_deps_without_std/.cargo/config.toml b/tests/projects/rust/cargo_deps_without_std/.cargo/config.toml new file mode 100644 index 00000000000..724febaeccc --- /dev/null +++ b/tests/projects/rust/cargo_deps_without_std/.cargo/config.toml @@ -0,0 +1,2 @@ +[build] +target = "aarch64-unknown-none" diff --git a/tests/projects/rust/cargo_deps_without_std/Cargo.toml b/tests/projects/rust/cargo_deps_without_std/Cargo.toml new file mode 100644 index 00000000000..6761593dcb4 --- /dev/null +++ b/tests/projects/rust/cargo_deps_without_std/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "demo" +version = "0.1.0" +edition = "2021" + +[dependencies] +spin = "^0.9" diff --git a/tests/projects/rust/cargo_deps_without_std/src/main.rs b/tests/projects/rust/cargo_deps_without_std/src/main.rs new file mode 100644 index 00000000000..c7ce4ff1cd6 --- /dev/null +++ b/tests/projects/rust/cargo_deps_without_std/src/main.rs @@ -0,0 +1,10 @@ +// src/main.rs +#![no_main] +#![no_std] + +use core::panic::PanicInfo; + +#[panic_handler] +fn panic(_panic: &PanicInfo<'_>) -> ! { + loop {} +} diff --git a/tests/projects/rust/cargo_deps_without_std/xmake.lua b/tests/projects/rust/cargo_deps_without_std/xmake.lua new file mode 100644 index 00000000000..b9f878c1329 --- /dev/null +++ b/tests/projects/rust/cargo_deps_without_std/xmake.lua @@ -0,0 +1,7 @@ +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") From 1494182a09187cf49edc9a19badc3be287e8a327 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 8 Aug 2023 23:46:13 +0800 Subject: [PATCH 02/12] improve rust deps to support cross build --- .../Cargo.toml | 0 .../src/main.rs | 0 .../rust/cargo_deps_cross_build/xmake.lua | 11 +++++ .../cargo_deps_without_std/.cargo/config.toml | 2 - .../rust/cargo_deps_without_std/xmake.lua | 7 ---- .../package/manager/cargo/configurations.lua | 9 ++-- .../package/manager/cargo/install_package.lua | 42 ++++++++++++++++--- 7 files changed, 53 insertions(+), 18 deletions(-) rename tests/projects/rust/{cargo_deps_without_std => cargo_deps_cross_build}/Cargo.toml (100%) rename tests/projects/rust/{cargo_deps_without_std => cargo_deps_cross_build}/src/main.rs (100%) create mode 100644 tests/projects/rust/cargo_deps_cross_build/xmake.lua delete mode 100644 tests/projects/rust/cargo_deps_without_std/.cargo/config.toml delete mode 100644 tests/projects/rust/cargo_deps_without_std/xmake.lua diff --git a/tests/projects/rust/cargo_deps_without_std/Cargo.toml b/tests/projects/rust/cargo_deps_cross_build/Cargo.toml similarity index 100% rename from tests/projects/rust/cargo_deps_without_std/Cargo.toml rename to 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 similarity index 100% rename from tests/projects/rust/cargo_deps_without_std/src/main.rs rename to 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 00000000000..6327b2e171a --- /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 724febaeccc..00000000000 --- 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 b9f878c1329..00000000000 --- 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 9a8b7f89fdc..a233e7ccf8d 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 23955b1cca5..5bc28498b41 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 From 6ace9d5ab4735767b35d22cefd980e5f447db95c Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 8 Aug 2023 23:52:48 +0800 Subject: [PATCH 03/12] add cross build --- .../rust/cargo_deps_cross_build/xmake.lua | 4 +++- .../package/manager/cargo/configurations.lua | 1 - .../package/manager/cargo/install_package.lua | 5 +++-- xmake/platforms/cross/xmake.lua | 2 +- xmake/toolchains/rust/xmake.lua | 22 ++++++++++++++----- 5 files changed, 23 insertions(+), 11 deletions(-) diff --git a/tests/projects/rust/cargo_deps_cross_build/xmake.lua b/tests/projects/rust/cargo_deps_cross_build/xmake.lua index 6327b2e171a..36a0e73c72a 100644 --- a/tests/projects/rust/cargo_deps_cross_build/xmake.lua +++ b/tests/projects/rust/cargo_deps_cross_build/xmake.lua @@ -1,8 +1,10 @@ +-- rustup target add aarch64-unknown-none +-- xmake f -p cross --cross=aarch64-unknown-none + 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") diff --git a/xmake/modules/package/manager/cargo/configurations.lua b/xmake/modules/package/manager/cargo/configurations.lua index a233e7ccf8d..f151c1802a9 100644 --- a/xmake/modules/package/manager/cargo/configurations.lua +++ b/xmake/modules/package/manager/cargo/configurations.lua @@ -26,7 +26,6 @@ function main() 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 5bc28498b41..60c059f40bf 100644 --- a/xmake/modules/package/manager/cargo/install_package.lua +++ b/xmake/modules/package/manager/cargo/install_package.lua @@ -132,8 +132,9 @@ target = "%s" -- do install local installdir = opt.installdir os.tryrm(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")) + local cross = get_config("cross") + if cross then + os.vcp(path.join(sourcedir, "target", cross, 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 diff --git a/xmake/platforms/cross/xmake.lua b/xmake/platforms/cross/xmake.lua index a5bbdc0d2a9..6257c6459ef 100644 --- a/xmake/platforms/cross/xmake.lua +++ b/xmake/platforms/cross/xmake.lua @@ -34,6 +34,6 @@ platform("cross") set_formats("symbol", "$(name).sym") -- set toolchains - set_toolchains("envs", "cross") + set_toolchains("envs", "rust", "cross") diff --git a/xmake/toolchains/rust/xmake.lua b/xmake/toolchains/rust/xmake.lua index 3329b73b762..0bb6ad36f8e 100644 --- a/xmake/toolchains/rust/xmake.lua +++ b/xmake/toolchains/rust/xmake.lua @@ -18,21 +18,31 @@ -- @file xmake.lua -- --- define toolchain toolchain("rust") - -- set homepage set_homepage("https://www.rust-lang.org/") set_description("Rust Programming Language Compiler") - -- set toolset +-- set_kind("standalone") + set_toolset("rc", "$(env RC)", "rustc") set_toolset("rcld", "$(env RC)", "rustc") set_toolset("rcsh", "$(env RC)", "rustc") set_toolset("rcar", "$(env RC)", "rustc") - -- on load + on_check(function (toolchain) + import("lib.detect.find_tool") + return find_tool("rustc") + end) + on_load(function (toolchain) - toolchain:set("rcshflags", "") - toolchain:set("rcldflags", "") + -- for cross-compilation, e.g. xmake f -p cross --cross=aarch64-unknown-none + local cross = toolchain:cross() + if toolchain:is_cross() and cross then + toolchain:add("rcshflags", "--target=" .. cross) + toolchain:add("rcldflags", "--target=" .. cross) + else + toolchain:set("rcshflags", "") + toolchain:set("rcldflags", "") + end end) From 04ff2a200ba3be2334827b3fe0385038aee9c090 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 8 Aug 2023 23:55:11 +0800 Subject: [PATCH 04/12] disable rust toolchain --- xmake/platforms/cross/xmake.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/platforms/cross/xmake.lua b/xmake/platforms/cross/xmake.lua index 6257c6459ef..a5bbdc0d2a9 100644 --- a/xmake/platforms/cross/xmake.lua +++ b/xmake/platforms/cross/xmake.lua @@ -34,6 +34,6 @@ platform("cross") set_formats("symbol", "$(name).sym") -- set toolchains - set_toolchains("envs", "rust", "cross") + set_toolchains("envs", "cross") From 0b82a790bad9538e09102afc31fc29c1e9bac826 Mon Sep 17 00:00:00 2001 From: w568w <1278297578@qq.com> Date: Wed, 9 Aug 2023 00:26:01 +0800 Subject: [PATCH 05/12] use `cargo metadata` to fetch the name set of cargo libraries --- .../rust/cargo_deps_cross_build/Cargo.toml | 3 + .../package/manager/cargo/find_package.lua | 59 ++++++++++++------- 2 files changed, 42 insertions(+), 20 deletions(-) diff --git a/tests/projects/rust/cargo_deps_cross_build/Cargo.toml b/tests/projects/rust/cargo_deps_cross_build/Cargo.toml index 6761593dcb4..fdc6c313201 100644 --- a/tests/projects/rust/cargo_deps_cross_build/Cargo.toml +++ b/tests/projects/rust/cargo_deps_cross_build/Cargo.toml @@ -5,3 +5,6 @@ edition = "2021" [dependencies] spin = "^0.9" + +[target.'cfg(target_arch = "aarch64")'.dependencies] +aarch64-cpu = "^9.3" diff --git a/xmake/modules/package/manager/cargo/find_package.lua b/xmake/modules/package/manager/cargo/find_package.lua index ec351d84e43..b01ce53f249 100644 --- a/xmake/modules/package/manager/cargo/find_package.lua +++ b/xmake/modules/package/manager/cargo/find_package.lua @@ -22,6 +22,7 @@ import("core.base.option") import("core.base.semver") import("core.base.hashset") +import("core.base.json") import("core.project.config") import("core.project.target") import("lib.detect.find_tool") @@ -82,30 +83,48 @@ function _get_libname(name) end -- get the name set of libraries -function _get_names_of_libraries(name, configs) +function _get_names_of_libraries(name, opt, configs) local names = hashset.new() if configs.cargo_toml then - local dependencies = false - local cargo_file = io.open(configs.cargo_toml) - for line in cargo_file:lines() do - line = line:trim() - if not dependencies and line == "[dependencies]" then - dependencies = true - elseif dependencies then - if not line:startswith("[") then - local splitinfo = line:split("=", {plain = true}) - if splitinfo and #splitinfo > 1 then - name = splitinfo[1]:trim() - if #name > 0 then - names:insert(_get_libname(name)) - end - end - else - break + local cargo = assert(find_tool("cargo"), "cargo not found! Please ensure Rust has been installed") + local cargo_args = {"metadata", "--format-version", "1", "--manifest-path", configs.cargo_toml, "--color", "never"} + if opt.arch then + table.insert(cargo_args, "--filter-platform") + table.insert(cargo_args, opt.arch) + end + if configs.features then + table.insert(cargo_args, "--features") + table.insert(cargo_args, table.concat(configs.features, ",")) + end + if configs.default_features == false then + table.insert(cargo_args, "--no-default-features") + end + + local output = os.iorunv(cargo.program, cargo_args) + local metadata = json.decode(output) + + -- fetch the direct dependencies list regradless of the target platform + table.insert(cargo_args, "--no-deps") + output = os.iorunv(cargo.program, cargo_args) + local metadata_no_deps = json.decode(output) + -- FIXME: should consider the case of multiple packages in a workspace! + local direct_deps = metadata_no_deps.packages[1].dependencies + + -- get the intersection of the direct dependencies and all dependencies for the target platform + local function _get_deps(name, deps) + for _, dep in ipairs(deps) do + if dep.name == name then + return dep end end + return nil + end + for _, dep in ipairs(direct_deps) do + local dep_metadata = _get_deps(dep.name, metadata.packages) + if dep_metadata then + names:insert(_get_libname(dep.name)) + end end - cargo_file:close() else names:insert(_get_libname(name)) end @@ -124,7 +143,7 @@ function main(name, opt) local configs = opt.configs or {} -- get names of libraries - local names = _get_names_of_libraries(name, configs) + local names = _get_names_of_libraries(name, opt, configs) assert(not names:empty()) local frameworkdirs From d56fd4e78dd1d5a64409df3b8c889a54a4f740eb Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 9 Aug 2023 00:45:21 +0800 Subject: [PATCH 06/12] use arch to select target --- .../rust/cargo_deps_cross_build/xmake.lua | 3 ++- .../package/manager/cargo/install_package.lua | 18 +++++++++++++----- xmake/toolchains/rust/xmake.lua | 17 +++++------------ xmake/toolchains/xcode/load_macosx.lua | 3 ++- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/tests/projects/rust/cargo_deps_cross_build/xmake.lua b/tests/projects/rust/cargo_deps_cross_build/xmake.lua index 36a0e73c72a..e02e14f822e 100644 --- a/tests/projects/rust/cargo_deps_cross_build/xmake.lua +++ b/tests/projects/rust/cargo_deps_cross_build/xmake.lua @@ -1,6 +1,7 @@ -- rustup target add aarch64-unknown-none --- xmake f -p cross --cross=aarch64-unknown-none +-- xmake f -a aarch64-unknown-none +set_arch("aarch64-unknown-none") add_rules("mode.release", "mode.debug") add_requires("cargo::test", {configs = { std = false, diff --git a/xmake/modules/package/manager/cargo/install_package.lua b/xmake/modules/package/manager/cargo/install_package.lua index 60c059f40bf..7d58553e63a 100644 --- a/xmake/modules/package/manager/cargo/install_package.lua +++ b/xmake/modules/package/manager/cargo/install_package.lua @@ -52,6 +52,15 @@ 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 + local arch = opt.arch + if arch and #arch:split("%-") > 1 then + target = arch + end + -- generate Cargo.toml local sourcedir = path.join(opt.cachedir, "source") local cargotoml = path.join(sourcedir, "Cargo.toml") @@ -88,11 +97,11 @@ function main(name, opt) -- generate .cargo/config.toml local configtoml = path.join(sourcedir, ".cargo", "config.toml") - if configs.build_target then + if target then io.writefile(configtoml, format([[ [build] target = "%s" -]], configs.build_target)) +]], target)) end -- generate main.rs @@ -132,9 +141,8 @@ target = "%s" -- do install local installdir = opt.installdir os.tryrm(path.join(installdir, "lib")) - local cross = get_config("cross") - if cross then - os.vcp(path.join(sourcedir, "target", cross, 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 diff --git a/xmake/toolchains/rust/xmake.lua b/xmake/toolchains/rust/xmake.lua index 0bb6ad36f8e..e997eaa21d3 100644 --- a/xmake/toolchains/rust/xmake.lua +++ b/xmake/toolchains/rust/xmake.lua @@ -23,24 +23,17 @@ toolchain("rust") set_homepage("https://www.rust-lang.org/") set_description("Rust Programming Language Compiler") --- set_kind("standalone") - set_toolset("rc", "$(env RC)", "rustc") set_toolset("rcld", "$(env RC)", "rustc") set_toolset("rcsh", "$(env RC)", "rustc") set_toolset("rcar", "$(env RC)", "rustc") - on_check(function (toolchain) - import("lib.detect.find_tool") - return find_tool("rustc") - end) - on_load(function (toolchain) - -- for cross-compilation, e.g. xmake f -p cross --cross=aarch64-unknown-none - local cross = toolchain:cross() - if toolchain:is_cross() and cross then - toolchain:add("rcshflags", "--target=" .. cross) - toolchain:add("rcldflags", "--target=" .. cross) + -- e.g. x86_64-pc-windows-msvc, aarch64-unknown-none + local arch = toolchain:arch() + if arch and #arch:split("%-") > 1 then + toolchain:add("rcshflags", "--target=" .. arch) + toolchain:add("rcldflags", "--target=" .. arch) else toolchain:set("rcshflags", "") toolchain:set("rcldflags", "") diff --git a/xmake/toolchains/xcode/load_macosx.lua b/xmake/toolchains/xcode/load_macosx.lua index 69afce0e9a2..fb7c3d61569 100644 --- a/xmake/toolchains/xcode/load_macosx.lua +++ b/xmake/toolchains/xcode/load_macosx.lua @@ -78,7 +78,8 @@ function main(toolchain) -- init flags for c/c++ toolchain:add("ldflags", "-stdlib=libc++") toolchain:add("shflags", "-stdlib=libc++") - toolchain:add("syslinks", "z") + toolchain:add("ldflags", "-lz") + toolchain:add("shflags", "-lz") -- init flags for objc/c++ (with ldflags and shflags) -- we can use `add_mxflags("-fno-objc-arc")` to override it in xmake.lua From b156158b59dd2ed5cd825d6ac98b5222c75d19fd Mon Sep 17 00:00:00 2001 From: w568w <1278297578@qq.com> Date: Wed, 9 Aug 2023 11:06:06 +0800 Subject: [PATCH 07/12] fix local function style and extract the target check function --- .../package/manager/cargo/find_package.lua | 22 +++--- .../package/manager/cargo/install_package.lua | 7 +- .../private/tools/rust/check_target.lua | 75 +++++++++++++++++++ 3 files changed, 89 insertions(+), 15 deletions(-) create mode 100644 xmake/modules/private/tools/rust/check_target.lua diff --git a/xmake/modules/package/manager/cargo/find_package.lua b/xmake/modules/package/manager/cargo/find_package.lua index b01ce53f249..0dcb835bb4e 100644 --- a/xmake/modules/package/manager/cargo/find_package.lua +++ b/xmake/modules/package/manager/cargo/find_package.lua @@ -27,6 +27,7 @@ import("core.project.config") import("core.project.target") import("lib.detect.find_tool") import("lib.detect.find_file") +import("private.tools.rust.check_target") -- get cargo registry directory function _get_cargo_registrydir() @@ -82,13 +83,22 @@ function _get_libname(name) return "lib" .. name:gsub("-", "_") end +function _get_package_from_deps(name, deps) + for _, dep in ipairs(deps) do + if dep.name == name then + return dep + end + end + return nil +end + -- get the name set of libraries function _get_names_of_libraries(name, opt, configs) local names = hashset.new() if configs.cargo_toml then local cargo = assert(find_tool("cargo"), "cargo not found! Please ensure Rust has been installed") local cargo_args = {"metadata", "--format-version", "1", "--manifest-path", configs.cargo_toml, "--color", "never"} - if opt.arch then + if check_target(opt.arch, true) then table.insert(cargo_args, "--filter-platform") table.insert(cargo_args, opt.arch) end @@ -111,16 +121,8 @@ function _get_names_of_libraries(name, opt, configs) local direct_deps = metadata_no_deps.packages[1].dependencies -- get the intersection of the direct dependencies and all dependencies for the target platform - local function _get_deps(name, deps) - for _, dep in ipairs(deps) do - if dep.name == name then - return dep - end - end - return nil - end for _, dep in ipairs(direct_deps) do - local dep_metadata = _get_deps(dep.name, metadata.packages) + local dep_metadata = _get_package_from_deps(dep.name, metadata.packages) if dep_metadata then names:insert(_get_libname(dep.name)) end diff --git a/xmake/modules/package/manager/cargo/install_package.lua b/xmake/modules/package/manager/cargo/install_package.lua index 7d58553e63a..9daa665cd0a 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 -- @@ -55,11 +56,7 @@ function main(name, opt) -- get target -- e.g. x86_64-pc-windows-msvc, aarch64-unknown-none -- @see https://github.com/xmake-io/xmake/issues/4049 - local target - local arch = opt.arch - if arch and #arch:split("%-") > 1 then - target = arch - end + local target = check_target(opt.arch, true) and opt.arch or nil -- generate Cargo.toml local sourcedir = path.join(opt.cachedir, "source") diff --git a/xmake/modules/private/tools/rust/check_target.lua b/xmake/modules/private/tools/rust/check_target.lua new file mode 100644 index 00000000000..1c213306ed9 --- /dev/null +++ b/xmake/modules/private/tools/rust/check_target.lua @@ -0,0 +1,75 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author w568w +-- @file check_target.lua +-- + +-- imports +import("lib.detect.find_tool") + +-- get rustc supported targets +-- +-- @return the supported target list. If rustc not found, return nil +function _get_rustc_supported_target() + local rustc = find_tool("rustc") + if not rustc then + return nil + end + local output = os.iorunv(rustc.program, {"--print", "target-list"}) + return output:split('\n') +end + +-- check whether the target is supported by rustc +-- +-- @param arch the target name, e.g. x86_64-unknown-linux-gnu +-- @param precise whether to check the target precisely (i.e. check by rustc), otherwise only by syntax +-- +-- @return true if arch != nil and the target is supported by rustc, otherwise false +function main(arch, precise) + print("arch: " .. arch) + + if not arch then + return false + end + + -- 1: check by syntax + local result = false + local archs = arch:split("%-") + if #archs >= 2 then + result = true + end + + -- 2: check by rustc + if not precise then + return result + end + result = false + local rustc_supported_target = _get_rustc_supported_target() + if rustc_supported_target then + for _, v in ipairs(rustc_supported_target) do + if v == arch then + result = true + break + end + end + if not result then + utils.warning("The arch \"%s\" is NOT supported by rustc, will be IGNORED and may cause compilation errors. please check it again", arch) + end + end + + return result +end From 3ba512001d7785b81e6f4e3b72ffbcf11973c3d7 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 9 Aug 2023 11:06:10 +0800 Subject: [PATCH 08/12] Update find_package.lua --- .../package/manager/cargo/find_package.lua | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/xmake/modules/package/manager/cargo/find_package.lua b/xmake/modules/package/manager/cargo/find_package.lua index b01ce53f249..801f266c618 100644 --- a/xmake/modules/package/manager/cargo/find_package.lua +++ b/xmake/modules/package/manager/cargo/find_package.lua @@ -83,14 +83,14 @@ function _get_libname(name) end -- get the name set of libraries -function _get_names_of_libraries(name, opt, configs) +function _get_names_of_libraries(name, target, configs) local names = hashset.new() if configs.cargo_toml then local cargo = assert(find_tool("cargo"), "cargo not found! Please ensure Rust has been installed") local cargo_args = {"metadata", "--format-version", "1", "--manifest-path", configs.cargo_toml, "--color", "never"} - if opt.arch then + if target then table.insert(cargo_args, "--filter-platform") - table.insert(cargo_args, opt.arch) + table.insert(cargo_args, target) end if configs.features then table.insert(cargo_args, "--features") @@ -102,7 +102,7 @@ function _get_names_of_libraries(name, opt, configs) local output = os.iorunv(cargo.program, cargo_args) local metadata = json.decode(output) - + -- fetch the direct dependencies list regradless of the target platform table.insert(cargo_args, "--no-deps") output = os.iorunv(cargo.program, cargo_args) @@ -111,16 +111,14 @@ function _get_names_of_libraries(name, opt, configs) local direct_deps = metadata_no_deps.packages[1].dependencies -- get the intersection of the direct dependencies and all dependencies for the target platform - local function _get_deps(name, deps) - for _, dep in ipairs(deps) do - if dep.name == name then - return dep + for _, dep in ipairs(direct_deps) do + local dep_metadata + for _, pkg in ipairs(metadata.packages) do + if pkg.name == dep.name then + dep_metadata = pkg + break end end - return nil - end - for _, dep in ipairs(direct_deps) do - local dep_metadata = _get_deps(dep.name, metadata.packages) if dep_metadata then names:insert(_get_libname(dep.name)) end @@ -142,8 +140,17 @@ function main(name, opt) opt = opt or {} local configs = opt.configs or {} + -- get target + -- e.g. x86_64-pc-windows-msvc, aarch64-unknown-none + -- @see https://github.com/xmake-io/xmake/issues/4049 + local target + local arch = opt.arch + if arch and #arch:split("%-") > 1 then + target = arch + end + -- get names of libraries - local names = _get_names_of_libraries(name, opt, configs) + local names = _get_names_of_libraries(name, target, configs) assert(not names:empty()) local frameworkdirs From 12f6131319ee6256e33cc9d50110e791f43552ed Mon Sep 17 00:00:00 2001 From: w568w <1278297578@qq.com> Date: Wed, 9 Aug 2023 11:16:15 +0800 Subject: [PATCH 09/12] update warning messages in `check_target()` --- xmake/modules/private/tools/rust/check_target.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xmake/modules/private/tools/rust/check_target.lua b/xmake/modules/private/tools/rust/check_target.lua index 1c213306ed9..75d68ccf147 100644 --- a/xmake/modules/private/tools/rust/check_target.lua +++ b/xmake/modules/private/tools/rust/check_target.lua @@ -51,6 +51,8 @@ function main(arch, precise) local archs = arch:split("%-") if #archs >= 2 then result = true + else + utils.warning("The arch \"%s\" is NOT a valid target triple, will be IGNORED and may cause compilation errors. please check it again", arch) end -- 2: check by rustc From da732687d5747e2140668ab0016a74315d1b40ae Mon Sep 17 00:00:00 2001 From: w568w <1278297578@qq.com> Date: Wed, 9 Aug 2023 11:23:29 +0800 Subject: [PATCH 10/12] update error messages to be consistent with existing styles --- xmake/modules/package/manager/cargo/find_package.lua | 2 +- xmake/modules/private/tools/rust/check_target.lua | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/xmake/modules/package/manager/cargo/find_package.lua b/xmake/modules/package/manager/cargo/find_package.lua index 82d6f6bdbf2..bc42b714859 100644 --- a/xmake/modules/package/manager/cargo/find_package.lua +++ b/xmake/modules/package/manager/cargo/find_package.lua @@ -87,7 +87,7 @@ end function _get_names_of_libraries(name, target, configs) local names = hashset.new() if configs.cargo_toml then - local cargo = assert(find_tool("cargo"), "cargo not found! Please ensure Rust has been installed") + local cargo = assert(find_tool("cargo"), "cargo not found!") local cargo_args = {"metadata", "--format-version", "1", "--manifest-path", configs.cargo_toml, "--color", "never"} if check_target(target, true) then table.insert(cargo_args, "--filter-platform") diff --git a/xmake/modules/private/tools/rust/check_target.lua b/xmake/modules/private/tools/rust/check_target.lua index 75d68ccf147..e08e830da5f 100644 --- a/xmake/modules/private/tools/rust/check_target.lua +++ b/xmake/modules/private/tools/rust/check_target.lua @@ -52,7 +52,7 @@ function main(arch, precise) if #archs >= 2 then result = true else - utils.warning("The arch \"%s\" is NOT a valid target triple, will be IGNORED and may cause compilation errors. please check it again", arch) + utils.warning("the arch \"%s\" is NOT a valid target triple, will be IGNORED and may cause compilation errors, please check it again", arch) end -- 2: check by rustc @@ -69,7 +69,7 @@ function main(arch, precise) end end if not result then - utils.warning("The arch \"%s\" is NOT supported by rustc, will be IGNORED and may cause compilation errors. please check it again", arch) + utils.warning("the arch \"%s\" is NOT supported by rustc, will be IGNORED and may cause compilation errors, please check it again", arch) end end From b618dfe97c7bef82a3a317117fc364d0366b2137 Mon Sep 17 00:00:00 2001 From: w568w <1278297578@qq.com> Date: Wed, 9 Aug 2023 11:26:49 +0800 Subject: [PATCH 11/12] minor style updates --- xmake/modules/package/manager/cargo/find_package.lua | 3 ++- xmake/modules/private/tools/rust/check_target.lua | 5 ++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/xmake/modules/package/manager/cargo/find_package.lua b/xmake/modules/package/manager/cargo/find_package.lua index bc42b714859..98237adda6d 100644 --- a/xmake/modules/package/manager/cargo/find_package.lua +++ b/xmake/modules/package/manager/cargo/find_package.lua @@ -89,7 +89,8 @@ function _get_names_of_libraries(name, target, configs) if configs.cargo_toml then local cargo = assert(find_tool("cargo"), "cargo not found!") local cargo_args = {"metadata", "--format-version", "1", "--manifest-path", configs.cargo_toml, "--color", "never"} - if check_target(target, true) then + local target = check_target(target, true) and target or nil + if target then table.insert(cargo_args, "--filter-platform") table.insert(cargo_args, target) end diff --git a/xmake/modules/private/tools/rust/check_target.lua b/xmake/modules/private/tools/rust/check_target.lua index e08e830da5f..fdc06458b2a 100644 --- a/xmake/modules/private/tools/rust/check_target.lua +++ b/xmake/modules/private/tools/rust/check_target.lua @@ -40,7 +40,6 @@ end -- -- @return true if arch != nil and the target is supported by rustc, otherwise false function main(arch, precise) - print("arch: " .. arch) if not arch then return false @@ -52,7 +51,7 @@ function main(arch, precise) if #archs >= 2 then result = true else - utils.warning("the arch \"%s\" is NOT a valid target triple, will be IGNORED and may cause compilation errors, please check it again", arch) + wprint("the arch \"%s\" is NOT a valid target triple, will be IGNORED and may cause compilation errors, please check it again", arch) end -- 2: check by rustc @@ -69,7 +68,7 @@ function main(arch, precise) end end if not result then - utils.warning("the arch \"%s\" is NOT supported by rustc, will be IGNORED and may cause compilation errors, please check it again", arch) + wprint("the arch \"%s\" is NOT supported by rustc, will be IGNORED and may cause compilation errors, please check it again", arch) end end From 415f8da47b8b182a7072190dec91061ed6563c88 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 9 Aug 2023 11:49:34 +0800 Subject: [PATCH 12/12] Update find_package.lua --- xmake/modules/package/manager/cargo/find_package.lua | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/xmake/modules/package/manager/cargo/find_package.lua b/xmake/modules/package/manager/cargo/find_package.lua index 98237adda6d..58064d9a9b4 100644 --- a/xmake/modules/package/manager/cargo/find_package.lua +++ b/xmake/modules/package/manager/cargo/find_package.lua @@ -84,12 +84,13 @@ function _get_libname(name) end -- get the name set of libraries -function _get_names_of_libraries(name, target, configs) +function _get_names_of_libraries(name, opt) + local configs = opt.configs or {} local names = hashset.new() if configs.cargo_toml then local cargo = assert(find_tool("cargo"), "cargo not found!") local cargo_args = {"metadata", "--format-version", "1", "--manifest-path", configs.cargo_toml, "--color", "never"} - local target = check_target(target, true) and target or nil + local target = check_target(opt.arch, true) and opt.arch or nil if target then table.insert(cargo_args, "--filter-platform") table.insert(cargo_args, target) @@ -143,7 +144,7 @@ function main(name, opt) local configs = opt.configs or {} -- get names of libraries - local names = _get_names_of_libraries(name, opt.arch, configs) + local names = _get_names_of_libraries(name, opt) assert(not names:empty()) local frameworkdirs