From 0b82a790bad9538e09102afc31fc29c1e9bac826 Mon Sep 17 00:00:00 2001 From: w568w <1278297578@qq.com> Date: Wed, 9 Aug 2023 00:26:01 +0800 Subject: use `cargo metadata` to fetch the name set of cargo libraries --- .../rust/cargo_deps_cross_build/Cargo.toml | 3 ++ .../modules/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 6761593dc..fdc6c3132 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 ec351d84e..b01ce53f2 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 -- cgit v1.3.1 From b156158b59dd2ed5cd825d6ac98b5222c75d19fd Mon Sep 17 00:00:00 2001 From: w568w <1278297578@qq.com> Date: Wed, 9 Aug 2023 11:06:06 +0800 Subject: fix local function style and extract the target check function --- .../modules/package/manager/cargo/find_package.lua | 22 ++++--- .../package/manager/cargo/install_package.lua | 7 +- xmake/modules/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 b01ce53f2..0dcb835bb 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 7d58553e6..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 -- @@ -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 000000000..1c213306e --- /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 -- cgit v1.3.1 From 3ba512001d7785b81e6f4e3b72ffbcf11973c3d7 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 9 Aug 2023 11:06:10 +0800 Subject: Update find_package.lua --- .../modules/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 b01ce53f2..801f266c6 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 -- cgit v1.3.1 From 12f6131319ee6256e33cc9d50110e791f43552ed Mon Sep 17 00:00:00 2001 From: w568w <1278297578@qq.com> Date: Wed, 9 Aug 2023 11:16:15 +0800 Subject: 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 1c213306e..75d68ccf1 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 -- cgit v1.3.1 From da732687d5747e2140668ab0016a74315d1b40ae Mon Sep 17 00:00:00 2001 From: w568w <1278297578@qq.com> Date: Wed, 9 Aug 2023 11:23:29 +0800 Subject: 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 82d6f6bdb..bc42b7148 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 75d68ccf1..e08e830da 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 -- cgit v1.3.1 From b618dfe97c7bef82a3a317117fc364d0366b2137 Mon Sep 17 00:00:00 2001 From: w568w <1278297578@qq.com> Date: Wed, 9 Aug 2023 11:26:49 +0800 Subject: 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 bc42b7148..98237adda 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 e08e830da..fdc06458b 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 -- cgit v1.3.1 From 415f8da47b8b182a7072190dec91061ed6563c88 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 9 Aug 2023 11:49:34 +0800 Subject: 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 98237adda..58064d9a9 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 -- cgit v1.3.1