-
-
Notifications
You must be signed in to change notification settings - Fork 817
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for rust cross-compilation #4049
Comments
Title: Cannot compile a Rust target with |
p.s. Rust 中的项目编译配置可以分散于各处:
建议对于前三项提供指定或者配置的方法。 |
p.s. Project compilation configurations in Rust can be scattered around:
It is recommended to provide a method of specifying or configuring for the first and second items. |
我试着加了一下,也不行么 |
I tried adding it, doesn't work? |
嗯…可能是我理解能力有问题,这是陈述句还是疑问句? 如果是疑问句,可以看一看 Cargo 构建的错误提示吗? |
Hmm...Maybe I have a problem with my comprehension. Is this a statement or a question? If it is a question sentence, can you take a look at the error message of the Cargo build? |
就是写死的那个 main.rs 里面,我也加上 |
In the hard-coded main.rs, I also added |
也可能是我这引入的包的问题,你给下你这完整 demo project 呢
|
这就是完整的工程了。
这个的含义是需要安装对应的目标工具链,执行一下 |
还是有
|
嗯……这个确实看不出问题了,是否有可能是刚才编译时编译到了错误的目标,所以现在 而且从最后的
来看,编译实际上是成功了……我也暂时确定不了原因了。 我刚才自行从源码编译了一下 diff --git a/xmake/modules/package/manager/cargo/install_package.lua b/xmake/modules/package/manager/cargo/install_package.lua
index 23955b1cc..14ec05f28 100644
--- a/xmake/modules/package/manager/cargo/install_package.lua
+++ b/xmake/modules/package/manager/cargo/install_package.lua
@@ -88,8 +88,14 @@ function main(name, opt)
-- generate main.rs
io.writefile(path.join(sourcedir, "src", "main.rs"), [[
-fn main() {
- println!("Hello, world!");
+#![no_main]
+#![no_std]
+
+use core::panic::PanicInfo;
+
+#[panic_handler]
+fn panic(_panic: &PanicInfo<'_>) -> ! {
+ loop {}
}
]])
但编译项目时仍报错(和你的略有不同),日志如下:
初步判断问题在于 xmake 默认的复制来源是 编辑:再作如下修改后正常了: diff --git a/xmake/modules/package/manager/cargo/install_package.lua b/xmake/modules/package/manager/cargo/install_package.lua
index 23955b1cc..a2190e409 100644
--- a/xmake/modules/package/manager/cargo/install_package.lua
+++ b/xmake/modules/package/manager/cargo/install_package.lua
@@ -106,5 +106,5 @@ 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"))
+ os.vcp(path.join(sourcedir, "target", "aarch64-unknown-none", opt.mode == "debug" and "debug" or "release", "deps"), path.join(installdir, "lib"))
end 但新的问题是最终编译项目时使用的是 add_rcflags("--target=aarch64-unknown-none", {force = true}) 再编辑:但是还不够……因为我实际的项目中依据不同的编译目标包含了条件依赖: [target.'cfg(target_arch = "aarch64")'.dependencies]
aarch64-cpu = "^9.3" 而 xmake 简单地忽略了将这些依赖增加到最终编译命令的可能性: xmake/xmake/modules/package/manager/cargo/find_package.lua Lines 85 to 93 in e8dbb83
因此,还必须追加参数…… add_rcflags("--extern aarch64_cpu=/home/w568w/Projects/rarmos/build/.packages/c/cargo_test/latest/5e6547d0654e4584b80110922bf0699c/lib/libaarch64_cpu-856e6420af7a70c9.rlib", {force = true}) ……然后终于编译成功了。 |
@waruqi 我的建议是不要用字符串文本匹配的方式解析 p.s. 我对 Lua 不甚熟悉……但如果有时间的话(不是最近),我可以学习一下然后来帮助贡献该部分。 |
@waruqi My suggestion is not to parse |
这个就是 rust 的 cross build 么。。其实早期 交叉编译是支持的,后来好像 rustc 大改过,break 了很多东西,所以这边重新适配了,所以暂时把交叉编译支持给去掉了。。等后面有空看看 再加上。 |
Is this the cross build of rust? . In fact, cross-compilation was supported in the early days. Later, it seems that rustc has been greatly improved and broke a lot of things, so it has been re-adapted here, so the cross-compilation support is temporarily removed. . Check it out later when you have time. |
no_std
标记的 Rust 目标 / Cannot compile a Rust target with no_std
attribute
其实和 cross build 也不完全一样,只能说有重合。cross build 因为一般都是面向嵌入式设备或者内核环境,其架构一般是 Tier 2 支持,不具有 但做非交叉编译时也可能遇到不得不用 有关上面的条件编译问题,我会另开一个 issue。 |
In fact, it is not exactly the same as cross build, it can only be said that there is overlap. Because cross build is generally oriented to embedded devices or kernel environments, its architecture is generally supported by Tier 2 and does not have the However, when doing non-cross compilation, you may also encounter situations where you have to use |
初步搞了下,可以先试试,#4052
加了一个测试 example https://github.com/xmake-io/xmake/tree/rust/tests/projects/rust/cargo_deps_cross_build set_arch("aarch64-unknown-none")
add_rules("mode.release", "mode.debug")
add_requires("cargo::test", {configs = {
std = false,
main = false,
cargo_toml = path.join(os.projectdir(), "Cargo.toml")}})
target("test")
set_kind("binary")
add_files("src/main.rs")
add_packages("cargo::test") 可以用 set_arch 固定 target + .cargo/config.toml 不用加,默认会自动根据 target 生成一个切 target 。。
目前提供了 std/main configs ,可以单独配置。。 |
这个后两天有空了 再看看 |
I'm free in the next two days and I'll check it out |
测试了可以正常编译。感谢!:tada: 我也趁热打铁,争取最近交个 |
Tested that it can be compiled normally. grateful! 🎉 I'm also striking while the iron is hot, trying to submit a patch for |
merge 了。。 |
merged. . |
虽然 这两个依赖都需要复制并且在使用
正确的调用为 C:\Users\yangf\Documents\Projects\RE-UE4SS> rustc
-C debuginfo=2 -C opt-level=3
-C linker=C:\Users\yangf\Documents\Projects\RE-UE4SS/tools/zig/zig-c++
--target=x86_64-unknown-linux-gnu --edition=2021
-L dependency=C:\Users\yangf\Documents\Projects\RE-UE4SS\Intermediates\.packages\c\cargo_patternsleuth_bind\latest\ca0d54df84b4464a9d56cf13e471daea\lib
**-L dependency=C:\Users\yangf\Documents\Projects\RE-UE4SS\Intermediates\.packages\c\cargo_patternsleuth_bind\latest\cache\source\target\release\deps**
-C debuginfo=2
--extern patternsleuth=C:\Users\yangf\Documents\Projects\RE-UE4SS\Intermediates\.packages\c\cargo_patternsleuth_bind\latest\cache\source\target\x86_64-unknown-linux-gnu\release\deps\libpatternsleuth-fe8a45f2bce330fe.rlib
--crate-type=staticlib
-o C:\Users\yangf\Documents\Projects\RE-UE4SS\Binaries\Game__Shipping__Linux\patternsleuth_bind\libpatternsleuth_bind.a deps\first\patternsleuth_bind\src\lib.rs 方能正确编译出结果 |
这样可以workaround一下但是感觉分开两个文件夹会更好 |
Xmake 版本
2.8.1+20230711
操作系统版本和架构
Archlinux x86_64 (Kernel: Linux 6.4.8-arch1-1)
描述问题
Rust 在编译到不具备 std 库支持(例如部分 Tier 2 级别支持的架构。大部分嵌入式架构都为 Tier 2 支持)的架构时,必须使用
no_std
环境。这意味着添加#![no_std]
和#![no_main]
标记,并追加自行编写的 Panic 处理函数panic_handler
。最简单的例子如下:如果编译面向无 std 支持的架构,xmake 会在编译其 packages 时报错,如:
期待的结果
项目正常编译。
工程配置
xmake.lua
Cargo.toml
src/main.rs
.cargo/config.toml(在项目目录下,用于指定默认编译目标)
附加信息和错误日志
检查后,我发现 xmake 在编译依赖库时会构建一个最小 Crate,并且其
src/main.rs
是硬编码的:xmake/xmake/modules/package/manager/cargo/install_package.lua
Lines 89 to 94 in 0ffb589
而这一源码内容显然不包括上面给出的最小示例中所必须的
#![no_std]
和#![no_main]
等标记。这就导致了编译失败。The text was updated successfully, but these errors were encountered: