Skip to content

Commit 0d2657c

Browse files
authored
Merge pull request #6402 from SirLynix/rust-cross
cargo: fix package cross-compilation
2 parents a3df7af + ef19045 commit 0d2657c

File tree

4 files changed

+113
-53
lines changed

4 files changed

+113
-53
lines changed

xmake/modules/package/manager/cargo/find_package.lua

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,12 @@ function main(name, opt)
136136
local librarydir_host = path.join(opt.installdir, "lib", "host")
137137
local libfiles = os.files(path.join(librarydir, "*.rlib"))
138138

139+
local frameworkdirs = {}
140+
table.insert(frameworkdirs, librarydir)
141+
if os.isdir(librarydir_host) then
142+
table.insert(frameworkdirs, librarydir_host)
143+
end
144+
139145
-- @see https://github.com/xmake-io/xmake/issues/4228
140146
local libfiles_native
141147
local plat = opt.plat
@@ -165,9 +171,7 @@ function main(name, opt)
165171
libraryname = "lib" .. libraryname
166172
end
167173
if names:has(libraryname) and not libraries_set[libraryname] then
168-
frameworkdirs = frameworkdirs or {}
169174
frameworks = frameworks or {}
170-
table.insert(frameworkdirs, path.directory(libraryfile))
171175
table.insert(frameworks, libraryfile)
172176
libraries_set[libraryname] = true
173177
end
@@ -176,7 +180,7 @@ function main(name, opt)
176180
if frameworks and frameworkdirs then
177181
result = result or {}
178182
result.libfiles = libfiles
179-
result.frameworkdirs = frameworkdirs and table.unique(frameworkdirs) or nil
183+
result.frameworkdirs = frameworkdirs
180184
result.frameworks = frameworks
181185
result.version = opt.require_version
182186
end

xmake/modules/package/manager/cargo/install_package.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import("core.base.option")
2323
import("core.project.config")
2424
import("lib.detect.find_tool")
2525
import("private.tools.rust.check_target")
26+
import("private.tools.rust.target_triple")
2627

2728
-- translate local path in dependencies
2829
-- @see https://github.com/xmake-io/xmake/issues/4222
@@ -78,7 +79,10 @@ function main(name, opt)
7879
-- get target
7980
-- e.g. x86_64-pc-windows-msvc, aarch64-unknown-none
8081
-- @see https://github.com/xmake-io/xmake/issues/4049
81-
local target = check_target(opt.arch, true) and opt.arch or nil
82+
local target = #opt.arch:split("%-") >= 2 and check_target(opt.arch, true) and opt.arch
83+
if not target then
84+
target = target_triple(opt.plat, opt.arch)
85+
end
8286

8387
-- generate Cargo.toml
8488
local sourcedir = path.join(opt.cachedir, "source")
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
--!A cross-platform build utility based on Lua
2+
--
3+
-- Licensed under the Apache License, Version 2.0 (the "License");
4+
-- you may not use this file except in compliance with the License.
5+
-- You may obtain a copy of the License at
6+
--
7+
-- http://www.apache.org/licenses/LICENSE-2.0
8+
--
9+
-- Unless required by applicable law or agreed to in writing, software
10+
-- distributed under the License is distributed on an "AS IS" BASIS,
11+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
-- See the License for the specific language governing permissions and
13+
-- limitations under the License.
14+
--
15+
-- Copyright (C) 2015-present, TBOOX Open Source Group.
16+
--
17+
-- @author SirLynix
18+
-- @file target_triple.lua
19+
--
20+
21+
-- get arch part
22+
function _translate_arch(arch, opt)
23+
local maps =
24+
{
25+
["aarch64"] = "aarch64"
26+
, ["armv5te"] = "arm"
27+
, ["armeabi"] = "arm"
28+
, ["armeabi-v7a"] = "armv7"
29+
, ["armv7-a"] = "armv7"
30+
, ["arm64"] = "aarch64"
31+
, ["arm64-v8a"] = "aarch64"
32+
, ["i386"] = "i686"
33+
, ["i686"] = "i686"
34+
, ["x86"] = "i686"
35+
, ["x86_64"] = "x86_64"
36+
, ["x64"] = "x86_64"
37+
, ["wasm32"] = "wasm32"
38+
, ["wasm64"] = "wasm64"
39+
}
40+
return maps[arch]
41+
end
42+
43+
-- get platform part
44+
function _translate_plat(plat, arch, opt)
45+
if plat == "windows" then
46+
return "-pc-windows-msvc"
47+
elseif plat == "mingw" then
48+
return "-pc-windows-gnu"
49+
elseif plat == "linux" then
50+
return "-unknown-linux-gnu"
51+
elseif plat == "macosx" then
52+
return "-apple-darwin"
53+
elseif plat == "android" then
54+
if arch == "armeabi-v7a" or arch == "armeabi" or arch == "armv7-a" or arch == "armv5te" then
55+
return "-linux-androideabi"
56+
else
57+
return "-linux-android"
58+
end
59+
elseif plat == "iphoneos" or plat == "appletvos" or plat == "watchos" then
60+
local suffix = opt and opt.apple_sim and "-sim" or ""
61+
if plat == "iphoneos" then
62+
return "-apple-ios" .. suffix
63+
elseif plat == "appletvos" then
64+
return "-apple-tvos" .. suffix
65+
elseif plat == "watchos" then
66+
return "-apple-watchos" .. suffix
67+
end
68+
elseif plat == "bsd" then
69+
return "-unknown-freebsd"
70+
elseif plat == "wasm" then
71+
return "-unknown-unknown"
72+
end
73+
end
74+
75+
-- gets the rustc compatible target triple (e.g. x86_64-pc-windows-msvc) for a set plat/arch
76+
--
77+
-- @param plat the target plat, e.g. windows, android, macosx, ...
78+
-- @param arch the target name, e.g. arm64, x86_64, wasm64, ...
79+
-- @param opt the options, e.g. {apple_sim = true)
80+
--
81+
-- @return a valid rustc triple if plat and arch are recognized, nil otherwise
82+
function main(plat, arch, opt)
83+
84+
local target_arch = _translate_arch(arch, opt)
85+
if not target_arch then
86+
return
87+
end
88+
89+
local target_plat = _translate_plat(plat, arch, opt)
90+
if not target_plat then
91+
return
92+
end
93+
94+
return target_arch .. target_plat
95+
end

xmake/toolchains/rust/xmake.lua

Lines changed: 6 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -28,57 +28,14 @@ toolchain("rust")
2828
set_toolset("rcar", "$(env RC)", "rustc")
2929

3030
on_load(function (toolchain)
31-
local target
32-
if toolchain:is_arch("x86_64", "x64") then
33-
target = "x86_64"
34-
elseif toolchain:is_arch("i386", "x86", "i686") then
35-
target = "i686"
36-
elseif toolchain:is_arch("arm64", "aarch64", "arm64-v8a") then
37-
target = "aarch64"
38-
elseif toolchain:is_arch("armeabi-v7a", "armv7-a") then
39-
target = "armv7"
40-
elseif toolchain:is_arch("armeabi", "armv5te") then
41-
target = "arm"
42-
elseif toolchain:is_arch("wasm32") then
43-
target = "wasm32"
44-
elseif toolchain:is_arch("wasm64") then
45-
target = "wasm64"
46-
end
31+
import("private.tools.rust.target_triple")
4732

48-
if target then
49-
if toolchain:is_plat("windows") then
50-
target = target .. "-pc-windows-msvc"
51-
elseif toolchain:is_plat("mingw") then
52-
target = target .. "-pc-windows-gnu"
53-
elseif toolchain:is_plat("linux") then
54-
target = target .. "-unknown-linux-gnu"
55-
elseif toolchain:is_plat("macosx") then
56-
target = target .. "-apple-darwin"
57-
elseif toolchain:is_plat("android") then
58-
target = target .. "-linux-"
59-
if toolchain:is_arch("armeabi-v7a", "armeabi", "armv7-a", "armv5te") then
60-
target = target .. "androideabi"
61-
else
62-
target = target .. "android"
63-
end
64-
elseif toolchain:is_plat("iphoneos", "appletvos", "watchos") then
65-
if toolchain:is_plat("iphoneos") then
66-
target = target .. "-apple-ios"
67-
elseif toolchain:is_plat("appletvos") then
68-
target = target .. "-apple-tvos"
69-
elseif toolchain:is_plat("watchos") then
70-
target = target .. "-apple-watchos"
71-
end
72-
if toolchain:config("appledev") == "simulator" then
73-
target = target .. "-sim"
74-
end
75-
elseif toolchain:is_plat("bsd") then
76-
target = target .. "-unknown-freebsd"
77-
elseif toolchain:is_plat("wasm") then
78-
target = target .. "-unknown-unknown"
79-
end
33+
local opt = {}
34+
if toolchain:config("appledev") == "simulator" then
35+
opt.apple_sim = true
8036
end
81-
37+
38+
local target = target_triple(toolchain:plat(), toolchain:arch(), opt)
8239
if target then
8340
toolchain:add("rcflags", "--target=" .. target)
8441
else

0 commit comments

Comments
 (0)