diff options
| author | w568w <[email protected]> | 2023-08-09 00:26:01 +0800 |
|---|---|---|
| committer | w568w <[email protected]> | 2023-08-09 00:26:01 +0800 |
| commit | 0b82a790bad9538e09102afc31fc29c1e9bac826 (patch) | |
| tree | fcc300be34a984423bfa0e1e387f817a94b7fd41 | |
| parent | d56fd4e78dd1d5a64409df3b8c889a54a4f740eb (diff) | |
use `cargo metadata` to fetch the name set of cargo libraries
| -rw-r--r-- | tests/projects/rust/cargo_deps_cross_build/Cargo.toml | 3 | ||||
| -rw-r--r-- | xmake/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 |
