summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorw568w <[email protected]>2023-08-09 11:06:06 +0800
committerw568w <[email protected]>2023-08-09 11:06:06 +0800
commitb156158b59dd2ed5cd825d6ac98b5222c75d19fd (patch)
tree78d004dde19d9fa937f6e2219bf50db88dc6d1fa
parent0b82a790bad9538e09102afc31fc29c1e9bac826 (diff)
fix local function style and extract the target check function
-rw-r--r--xmake/modules/package/manager/cargo/find_package.lua22
-rw-r--r--xmake/modules/package/manager/cargo/install_package.lua7
-rw-r--r--xmake/modules/private/tools/rust/check_target.lua75
3 files changed, 89 insertions, 15 deletions
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