Skip to content
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

Closed
w568w opened this issue Aug 7, 2023 · 28 comments
Closed

Support for rust cross-compilation #4049

w568w opened this issue Aug 7, 2023 · 28 comments

Comments

@w568w
Copy link
Contributor

w568w commented Aug 7, 2023

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。最简单的例子如下:

// src/main.rs
#![no_main]
#![no_std]

use core::panic::PanicInfo;

#[panic_handler]
fn panic(_panic: &PanicInfo<'_>) -> ! {
    loop {}
}

如果编译面向无 std 支持的架构,xmake 会在编译其 packages 时报错,如:

note: install or modify (m) these packages (pass -y to skip confirm)?
in cargo:
  -> cargo::kernel latest [cargo_toml:"/home/w568w/Projects/rarmos/Cargo.toml"]
please input: y (y/n/m)

  => install cargo::kernel latest .. failed

    Updating crates.io index
   Compiling autocfg v1.1.0
   Compiling stable_deref_trait v1.2.0
   Compiling scopeguard v1.2.0
   Compiling tock-registers v0.8.1
   Compiling as-slice v0.2.1
   Compiling aligned v0.4.1
   Compiling aarch64-cpu v9.3.1
   Compiling lock_api v0.4.10
   Compiling spin v0.9.8
   Compiling rarmos v0.1.0 (/home/w568w/Projects/rarmos/build/.packages/c/cargo_kernel/latest/cache/source)
error[E0463]: can't find crate for `std`
  |
  = note: the `aarch64-unknown-none` target may not support the standard library
  = note: `std` is required by `rarmos` because it does not declare `#![no_std]`
  = help: consider building the standard library from source with `cargo build -Zbuild-std`
error: cannot find macro `println` in this scope
if you want to get more verbose errors, please see:
  -> /home/w568w/Projects/rarmos/build/.packages/c/cargo_kernel/latest/cache/installdir.failed/logs/install.txt
error: install failed!

期待的结果

项目正常编译。

工程配置

xmake.lua

add_requires("cargo::test", {configs = {cargo_toml = path.join(os.projectdir(), "Cargo.toml")}})

target("test")
    set_kind("binary")
    add_files("src/main.rs")
    add_packages("cargo::test")

Cargo.toml

[package]
name = "demo"
version = "0.1.0"
edition = "2021"

[dependencies]
spin = "^0.9"

src/main.rs

#![no_main]
#![no_std]

use core::panic::PanicInfo;

#[panic_handler]
fn panic(_panic: &PanicInfo<'_>) -> ! {
    loop {}
}

.cargo/config.toml(在项目目录下,用于指定默认编译目标)

[build]
target = "aarch64-unknown-none"

附加信息和错误日志

检查后,我发现 xmake 在编译依赖库时会构建一个最小 Crate,并且其 src/main.rs硬编码的:

-- generate main.rs
io.writefile(path.join(sourcedir, "src", "main.rs"), [[
fn main() {
println!("Hello, world!");
}
]])

而这一源码内容显然不包括上面给出的最小示例中所必须的 #![no_std]#![no_main] 等标记。这就导致了编译失败。

@w568w w568w added the bug label Aug 7, 2023
@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


Title: Cannot compile a Rust target with no_std attribute

@w568w
Copy link
Contributor Author

w568w commented Aug 7, 2023

p.s. Rust 中的项目编译配置可以分散于各处:

  • src/main.rs / src/lib.rs目前 xmake 硬编码): 定义当前 Crate 的特性(Attributes)和功能开关(如 #![feature(...)] 用于启用实验特性)
  • .cargo/config.toml目前 xmake 不允许指定,但 Cargo 会自动继承上层目录中的配置):定义 cargo build 时的默认参数
  • cargo build 的命令行参数中(目前 xmake 不允许指定,但可以用 .cargo/config.toml 作为一个临时解决方案)
  • Cargo.toml目前 xmake 允许自行指定):定义 Crate 和 Workspace 的元信息及依赖库

建议对于前三项提供指定或者配置的方法。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


p.s. Project compilation configurations in Rust can be scattered around:

  • src/main.rs / src/lib.rs (currently hard-coded in xmake): Define the current Crate’s features (Attributes) and function switches (such as #![feature(...) ] to enable experimental features)
  • .cargo/config.toml (currently xmake does not allow specifying, but will automatically inherit the configuration in the upper directory): define the default parameters when cargo build
  • Cargo.toml (Currently xmake allows you to specify by yourself): Define the meta information and dependent libraries of Crate and Workspace

It is recommended to provide a method of specifying or configuring for the first and second items.

@waruqi
Copy link
Member

waruqi commented Aug 8, 2023

而这一源码内容显然不包括上面给出的最小示例中所必须的 #![no_std] 和 #![no_main] 等标记。这就导致了编译失败。

我试着加了一下,也不行么

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


And this source code content obviously does not include the #![no_std] and #![no_main] tags that are necessary in the minimal example given above. This caused the compilation to fail.

I tried adding it, doesn't work?

@w568w
Copy link
Contributor Author

w568w commented Aug 8, 2023

我试着加了一下,也不行么

嗯…可能是我理解能力有问题,这是陈述句还是疑问句?

如果是疑问句,可以看一看 Cargo 构建的错误提示吗?

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


I tried to add it, but it doesn’t work

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?

@waruqi
Copy link
Member

waruqi commented Aug 8, 2023

我试着加了一下,也不行么

嗯…可能是我理解能力有问题,这是陈述句还是疑问句?

如果是疑问句,可以看一看 Cargo 构建的错误提示吗?

就是写死的那个 main.rs 里面,我也加上 #![no_std] 这些。。还是报你遇到的 std 错误。。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


I tried to add it, but it doesn't work

Hmm...Maybe I have a problem with my comprehension. Is this a declarative sentence or a question sentence?

If it is a question sentence, can you take a look at the error message of the Cargo build?

In the hard-coded main.rs, I also added #![no_std]. . Or report the std error you encountered. .

@waruqi
Copy link
Member

waruqi commented Aug 8, 2023

也可能是我这引入的包的问题,你给下你这完整 demo project 呢

error[E0463]: can't find crate for `core`
  |
  = note: the `aarch64-unknown-none` target may not be installed
  = help: consider downloading the target with `rustup target add aarch64-u
nknown-none`

error[E0463]: can't find crate for `compiler_builtins`

error[E0463]: can't find crate for `core`

@w568w
Copy link
Contributor Author

w568w commented Aug 8, 2023

也可能是我这引入的包的问题,你给下你这完整 demo project 呢

这就是完整的工程了。

= note: the `aarch64-unknown-none` target may not be installed
= help: consider downloading the target with `rustup target add aarch64-u
nknown-none`

这个的含义是需要安装对应的目标工具链,执行一下 $ rustup target add aarch64-unknown-none 试试?

@waruqi
Copy link
Member

waruqi commented Aug 8, 2023

[lock_api 0.4.10] error[E0463]: can't find crate for std

还是有

ruki-2:cargo_deps_with_toml ruki$ xmake f -cvD
checking for platform ... macosx
checking for architecture ... x86_64
checking for Xcode directory ... /Applications/Xcode.app
checking for SDK version of Xcode for macosx (x86_64) ... 13.0
checking for Minimal target version of Xcode for macosx (x86_64) ... 12.6
checking for zig ... /usr/local/bin/zig
checking for unzip ... /usr/bin/unzip
checking for git ... /usr/bin/git
checking for gzip ... /usr/bin/gzip
checking for tar ... /usr/bin/tar
finding test from cargo ..
checking for cargo::test ... no
note: install or modify (m) these packages (pass -y to skip confirm)?
in cargo:
  -> cargo::test latest [cargo_toml:"/Users/ruki/projects/personal/xmake/t
..)
please input: y (y/n/m)

installing test from cargo ..
checking for cargo ... /Users/ruki/.cargo/bin//cargo
/Users/ruki/.cargo/bin//cargo build --release -vv
    Updating crates.io index
   Compiling autocfg v1.1.0
   Compiling scopeguard v1.2.0
     Running `CARGO=/Users/ruki/.cargo/bin/cargo CARGO_CRATE_NAME=autocfg C
ARGO_MANIFEST_DIR=/Users/ruki/.cargo/registry/src/github.com-1ecc6299db9ec8
23/autocfg-1.1.0 CARGO_PKG_AUTHORS='Josh Stone <cuviper@gmail.com>' CARGO_P
KG_DESCRIPTION='Automatic cfg for Rust compiler features' CARGO_PKG_HOMEPAG
E='' CARGO_PKG_LICENSE='Apache-2.0 OR MIT' CARGO_PKG_LICENSE_FILE='' CARGO_
PKG_NAME=autocfg CARGO_PKG_REPOSITORY='https://github.com/cuviper/autocfg'
CARGO_PKG_RUST_VERSION='' CARGO_PKG_VERSION=1.1.0 CARGO_PKG_VERSION_MAJOR=1
 CARGO_PKG_VERSION_MINOR=1 CARGO_PKG_VERSION_PATCH=0 CARGO_PKG_VERSION_PRE=
'' DYLD_FALLBACK_LIBRARY_PATH='/Users/ruki/projects/personal/xmake/tests/pr
ojects/rust/cargo_deps_with_toml/build/.packages/c/cargo_test/latest/cache/
source/target/release/deps:/Users/ruki/.rustup/toolchains/stable-x86_64-app
le-darwin/lib:/Users/ruki/lib:/usr/local/lib:/usr/lib' rustc --crate-name a
utocfg /Users/ruki/.cargo/registry/src/github.com-1ecc6299db9ec823/autocfg-
1.1.0/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifa
cts,future-incompat --crate-type lib --emit=dep-info,metadata,link -C embed
-bitcode=no -C debug-assertions=off -C metadata=7eebf15bbfa02fbc -C extra-f
ilename=-7eebf15bbfa02fbc --out-dir /Users/ruki/projects/personal/xmake/tes
ts/projects/rust/cargo_deps_with_toml/build/.packages/c/cargo_test/latest/c
ache/source/target/release/deps -L dependency=/Users/ruki/projects/personal
/xmake/tests/projects/rust/cargo_deps_with_toml/build/.packages/c/cargo_tes
t/latest/cache/source/target/release/deps --cap-lints warn`
     Running `CARGO=/Users/ruki/.cargo/bin/cargo CARGO_CRATE_NAME=scopeguar
d CARGO_MANIFEST_DIR=/Users/ruki/.cargo/registry/src/github.com-1ecc6299db9
ec823/scopeguard-1.2.0 CARGO_PKG_AUTHORS=bluss CARGO_PKG_DESCRIPTION='A RAI
I scope guard that will run a given closure when it goes out of scope,
even if the code between panics (assuming unwinding panic).

Defines the macros `defer'\!'`, `defer_on_unwind'\!'`, `defer_on_success'\!
'` as
shorthands for guards with one of the implemented strategies.
' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE='MIT OR Apache-2.0' CARGO_PKG_LIC
ENSE_FILE='' CARGO_PKG_NAME=scopeguard CARGO_PKG_REPOSITORY='https://github
.com/bluss/scopeguard' CARGO_PKG_RUST_VERSION='' CARGO_PKG_VERSION=1.2.0 CA
RGO_PKG_VERSION_MAJOR=1 CARGO_PKG_VERSION_MINOR=2 CARGO_PKG_VERSION_PATCH=0
 CARGO_PKG_VERSION_PRE='' DYLD_FALLBACK_LIBRARY_PATH='/Users/ruki/projects/
personal/xmake/tests/projects/rust/cargo_deps_with_toml/build/.packages/c/c
argo_test/latest/cache/source/target/release/deps:/Users/ruki/.rustup/toolc
hains/stable-x86_64-apple-darwin/lib:/Users/ruki/lib:/usr/local/lib:/usr/li
b' rustc --crate-name scopeguard /Users/ruki/.cargo/registry/src/github.com
-1ecc6299db9ec823/scopeguard-1.2.0/src/lib.rs --error-format=json --json=di
agnostic-rendered-ansi,artifacts,future-incompat --crate-type lib --emit=de
p-info,metadata,link -C opt-level=3 -C embed-bitcode=no -C metadata=f95fc36
92f454656 -C extra-filename=-f95fc3692f454656 --out-dir /Users/ruki/project
s/personal/xmake/tests/projects/rust/cargo_deps_with_toml/build/.packages/c
/cargo_test/latest/cache/source/target/aarch64-unknown-none/release/deps --
target aarch64-unknown-none -L dependency=/Users/ruki/projects/personal/xma
ke/tests/projects/rust/cargo_deps_with_toml/build/.packages/c/cargo_test/la
test/cache/source/target/aarch64-unknown-none/release/deps -L dependency=/U
sers/ruki/projects/personal/xmake/tests/projects/rust/cargo_deps_with_toml/
build/.packages/c/cargo_test/latest/cache/source/target/release/deps --cap-
lints warn`
   Compiling lock_api v0.4.10
     Running `CARGO=/Users/ruki/.cargo/bin/cargo CARGO_CRATE_NAME=build_scr
ipt_build CARGO_MANIFEST_DIR=/Users/ruki/.cargo/registry/src/github.com-1ec
c6299db9ec823/lock_api-0.4.10 CARGO_PKG_AUTHORS='Amanieu d'\''Antras <amani
eu@gmail.com>' CARGO_PKG_DESCRIPTION='Wrappers to create fully-featured Mut
ex and RwLock types. Compatible with no_std.' CARGO_PKG_HOMEPAGE='' CARGO_P
KG_LICENSE='MIT OR Apache-2.0' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=loc
k_api CARGO_PKG_REPOSITORY='https://github.com/Amanieu/parking_lot' CARGO_P
KG_RUST_VERSION='' CARGO_PKG_VERSION=0.4.10 CARGO_PKG_VERSION_MAJOR=0 CARGO
_PKG_VERSION_MINOR=4 CARGO_PKG_VERSION_PATCH=10 CARGO_PKG_VERSION_PRE='' DY
LD_FALLBACK_LIBRARY_PATH='/Users/ruki/projects/personal/xmake/tests/project
s/rust/cargo_deps_with_toml/build/.packages/c/cargo_test/latest/cache/sourc
e/target/release/deps:/Users/ruki/.rustup/toolchains/stable-x86_64-apple-da
rwin/lib:/Users/ruki/lib:/usr/local/lib:/usr/lib' rustc --crate-name build_
script_build --edition=2018 /Users/ruki/.cargo/registry/src/github.com-1ecc
6299db9ec823/lock_api-0.4.10/build.rs --error-format=json --json=diagnostic
-rendered-ansi,artifacts,future-incompat --crate-type bin --emit=dep-info,l
ink -C embed-bitcode=no -C debug-assertions=off --cfg 'feature="atomic_usiz
e"' --cfg 'feature="default"' -C metadata=d0d417093c2ce8b7 -C extra-filenam
e=-d0d417093c2ce8b7 --out-dir /Users/ruki/projects/personal/xmake/tests/pro
jects/rust/cargo_deps_with_toml/build/.packages/c/cargo_test/latest/cache/s
ource/target/release/build/lock_api-d0d417093c2ce8b7 -L dependency=/Users/r
uki/projects/personal/xmake/tests/projects/rust/cargo_deps_with_toml/build/
.packages/c/cargo_test/latest/cache/source/target/release/deps --extern aut
ocfg=/Users/ruki/projects/personal/xmake/tests/projects/rust/cargo_deps_wit
h_toml/build/.packages/c/cargo_test/latest/cache/source/target/release/deps
/libautocfg-7eebf15bbfa02fbc.rlib --cap-lints warn`
     Running `/Users/ruki/projects/personal/xmake/tests/projects/rust/cargo
_deps_with_toml/build/.packages/c/cargo_test/latest/cache/source/target/rel
ease/build/lock_api-d0d417093c2ce8b7/build-script-build`
[lock_api 0.4.10] error[E0463]: can't find crate for `std`
[lock_api 0.4.10]   |
[lock_api 0.4.10]   = note: the `aarch64-unknown-none` target may not suppo
rt the standard library
[lock_api 0.4.10]   = note: `std` is required by `probe0` because it does n
ot declare `#![no_std]`
[lock_api 0.4.10]
[lock_api 0.4.10] error: aborting due to previous error
[lock_api 0.4.10]
[lock_api 0.4.10] For more information about this error, try `rustc --expla
in E0463`.
[lock_api 0.4.10] cargo:rustc-cfg=has_const_fn_trait_bound
     Running `CARGO=/Users/ruki/.cargo/bin/cargo CARGO_CRATE_NAME=lock_api
CARGO_MANIFEST_DIR=/Users/ruki/.cargo/registry/src/github.com-1ecc6299db9ec
823/lock_api-0.4.10 CARGO_PKG_AUTHORS='Amanieu d'\''Antras <amanieu@gmail.c
om>' CARGO_PKG_DESCRIPTION='Wrappers to create fully-featured Mutex and RwL
ock types. Compatible with no_std.' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE
='MIT OR Apache-2.0' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=lock_api CARG
O_PKG_REPOSITORY='https://github.com/Amanieu/parking_lot' CARGO_PKG_RUST_VE
RSION='' CARGO_PKG_VERSION=0.4.10 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSI
ON_MINOR=4 CARGO_PKG_VERSION_PATCH=10 CARGO_PKG_VERSION_PRE='' DYLD_FALLBAC
K_LIBRARY_PATH='/Users/ruki/projects/personal/xmake/tests/projects/rust/car
go_deps_with_toml/build/.packages/c/cargo_test/latest/cache/source/target/r
elease/deps:/Users/ruki/.rustup/toolchains/stable-x86_64-apple-darwin/lib:/
Users/ruki/lib:/usr/local/lib:/usr/lib' OUT_DIR=/Users/ruki/projects/person
al/xmake/tests/projects/rust/cargo_deps_with_toml/build/.packages/c/cargo_t
est/latest/cache/source/target/aarch64-unknown-none/release/build/lock_api-
e05f2f8dd531a8a6/out rustc --crate-name lock_api --edition=2018 /Users/ruki
/.cargo/registry/src/github.com-1ecc6299db9ec823/lock_api-0.4.10/src/lib.rs
 --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incom
pat --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C embed-
bitcode=no --cfg 'feature="atomic_usize"' --cfg 'feature="default"' -C meta
data=37fa8c1ae6810b0e -C extra-filename=-37fa8c1ae6810b0e --out-dir /Users/
ruki/projects/personal/xmake/tests/projects/rust/cargo_deps_with_toml/build
/.packages/c/cargo_test/latest/cache/source/target/aarch64-unknown-none/rel
ease/deps --target aarch64-unknown-none -L dependency=/Users/ruki/projects/
personal/xmake/tests/projects/rust/cargo_deps_with_toml/build/.packages/c/c
argo_test/latest/cache/source/target/aarch64-unknown-none/release/deps -L d
ependency=/Users/ruki/projects/personal/xmake/tests/projects/rust/cargo_dep
s_with_toml/build/.packages/c/cargo_test/latest/cache/source/target/release
/deps --extern scopeguard=/Users/ruki/projects/personal/xmake/tests/project
s/rust/cargo_deps_with_toml/build/.packages/c/cargo_test/latest/cache/sourc
e/target/aarch64-unknown-none/release/deps/libscopeguard-f95fc3692f454656.r
meta --cap-lints warn --cfg has_const_fn_trait_bound`
   Compiling spin v0.9.8
     Running `CARGO=/Users/ruki/.cargo/bin/cargo CARGO_CRATE_NAME=spin CARG
O_MANIFEST_DIR=/Users/ruki/.cargo/registry/src/github.com-1ecc6299db9ec823/
spin-0.9.8 CARGO_PKG_AUTHORS='Mathijs van de Nes <git@mathijs.vd-nes.nl>:Jo
hn Ericson <git@JohnEricson.me>:Joshua Barretto <joshua.s.barretto@gmail.co
m>' CARGO_PKG_DESCRIPTION='Spin-based synchronization primitives' CARGO_PKG
_HOMEPAGE='' CARGO_PKG_LICENSE=MIT CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME
=spin CARGO_PKG_REPOSITORY='https://github.com/mvdnes/spin-rs.git' CARGO_PK
G_RUST_VERSION=1.38 CARGO_PKG_VERSION=0.9.8 CARGO_PKG_VERSION_MAJOR=0 CARGO
_PKG_VERSION_MINOR=9 CARGO_PKG_VERSION_PATCH=8 CARGO_PKG_VERSION_PRE='' DYL
D_FALLBACK_LIBRARY_PATH='/Users/ruki/projects/personal/xmake/tests/projects
/rust/cargo_deps_with_toml/build/.packages/c/cargo_test/latest/cache/source
/target/release/deps:/Users/ruki/.rustup/toolchains/stable-x86_64-apple-dar
win/lib:/Users/ruki/lib:/usr/local/lib:/usr/lib' rustc --crate-name spin /U
sers/ruki/.cargo/registry/src/github.com-1ecc6299db9ec823/spin-0.9.8/src/li
b.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-i
ncompat --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C em
bed-bitcode=no --cfg 'feature="barrier"' --cfg 'feature="default"' --cfg 'f
eature="lazy"' --cfg 'feature="lock_api"' --cfg 'feature="lock_api_crate"'
--cfg 'feature="mutex"' --cfg 'feature="once"' --cfg 'feature="rwlock"' --c
fg 'feature="spin_mutex"' -C metadata=c7d88c3614f0502b -C extra-filename=-c
7d88c3614f0502b --out-dir /Users/ruki/projects/personal/xmake/tests/project
s/rust/cargo_deps_with_toml/build/.packages/c/cargo_test/latest/cache/sourc
e/target/aarch64-unknown-none/release/deps --target aarch64-unknown-none -L
 dependency=/Users/ruki/projects/personal/xmake/tests/projects/rust/cargo_d
eps_with_toml/build/.packages/c/cargo_test/latest/cache/source/target/aarch
64-unknown-none/release/deps -L dependency=/Users/ruki/projects/personal/xm
ake/tests/projects/rust/cargo_deps_with_toml/build/.packages/c/cargo_test/l
atest/cache/source/target/release/deps --extern lock_api_crate=/Users/ruki/
projects/personal/xmake/tests/projects/rust/cargo_deps_with_toml/build/.pac
kages/c/cargo_test/latest/cache/source/target/aarch64-unknown-none/release/
deps/liblock_api-37fa8c1ae6810b0e.rmeta --cap-lints warn`
   Compiling demo v0.1.0 (/Users/ruki/projects/personal/xmake/tests/project
s/rust/cargo_deps_with_toml/build/.packages/c/cargo_test/latest/cache/sourc
e)
     Running `CARGO=/Users/ruki/.cargo/bin/cargo CARGO_BIN_NAME=demo CARGO_
CRATE_NAME=demo CARGO_MANIFEST_DIR=/Users/ruki/projects/personal/xmake/test
s/projects/rust/cargo_deps_with_toml/build/.packages/c/cargo_test/latest/ca
che/source CARGO_PKG_AUTHORS='' CARGO_PKG_DESCRIPTION='' CARGO_PKG_HOMEPAGE
='' CARGO_PKG_LICENSE='' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=demo CARG
O_PKG_REPOSITORY='' CARGO_PKG_RUST_VERSION='' CARGO_PKG_VERSION=0.1.0 CARGO
_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=1 CARGO_PKG_VERSION_PATCH=0 CA
RGO_PKG_VERSION_PRE='' CARGO_PRIMARY_PACKAGE=1 DYLD_FALLBACK_LIBRARY_PATH='
/Users/ruki/projects/personal/xmake/tests/projects/rust/cargo_deps_with_tom
l/build/.packages/c/cargo_test/latest/cache/source/target/release/deps:/Use
rs/ruki/.rustup/toolchains/stable-x86_64-apple-darwin/lib:/Users/ruki/lib:/
usr/local/lib:/usr/lib' rustc --crate-name demo --edition=2021 src/main.rs
--error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incomp
at --crate-type bin --emit=dep-info,link -C opt-level=3 -C embed-bitcode=no
 -C metadata=3ae63228ff56fc6f -C extra-filename=-3ae63228ff56fc6f --out-dir
 /Users/ruki/projects/personal/xmake/tests/projects/rust/cargo_deps_with_to
ml/build/.packages/c/cargo_test/latest/cache/source/target/aarch64-unknown-
none/release/deps --target aarch64-unknown-none -L dependency=/Users/ruki/p
rojects/personal/xmake/tests/projects/rust/cargo_deps_with_toml/build/.pack
ages/c/cargo_test/latest/cache/source/target/aarch64-unknown-none/release/d
eps -L dependency=/Users/ruki/projects/personal/xmake/tests/projects/rust/c
argo_deps_with_toml/build/.packages/c/cargo_test/latest/cache/source/target
/release/deps --extern spin=/Users/ruki/projects/personal/xmake/tests/proje
cts/rust/cargo_deps_with_toml/build/.packages/c/cargo_test/latest/cache/sou
rce/target/aarch64-unknown-none/release/deps/libspin-c7d88c3614f0502b.rlib`
    Finished release [optimized] target(s) in 4.16s
> copy /Users/ruki/projects/personal/xmake/tests/projects/rust/cargo_deps_w
ith_toml/build/.packages/c/cargo_test/latest/cache/source/target/release/de
ps to /Users/ruki/projects/personal/xmake/tests/projects/rust/cargo_deps_wi
th_toml/build/.packages/c/cargo_test/latest/c8acd4e5a21e4a1d9fb7cd223f415f9
5/lib
finding test from cargo ..
checking for cargo::test ... no

@w568w
Copy link
Contributor Author

w568w commented Aug 8, 2023

还是有

嗯……这个确实看不出问题了,是否有可能是刚才编译时编译到了错误的目标,所以现在 lock_api 的生成脚本 {临时源码目录}/target/release/build/lock_api-*/build-script-build 不太正常?试试先彻底 clean 当前项目:$ cargo clean && xmake c && rm -r build

而且从最后的

Finished release [optimized] target(s) in 4.16s

来看,编译实际上是成功了……我也暂时确定不了原因了。


我刚才自行从源码编译了一下 xmake,做了如下修改:

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 {}
 }
     ]])
 

但编译项目时仍报错(和你的略有不同),日志如下:

note: install or modify (m) these packages (pass -y to skip confirm)?
in cargo:
  -> cargo::test latest [cargo_toml:"/home/w568w/Projects/rarmos/Cargo.toml"]
please input: y (y/n/m)

checking for cargo ... /usr/bin/cargo
/usr/bin/cargo build --release -v

... 省略,无报错 ...

Finished release [optimized] target(s) in 2.50s

> copy /home/w568w/Projects/rarmos/build/.packages/c/cargo_test/latest/cache/source/target/release/deps to /home/w568w/Projects/rarmos/build/.packages/c/cargo_test/latest/5e6547d0654e4584b80110922bf0699c/lib
checking for cargo::test ... no

error: fetch cargo::test-latest failed!
  => install cargo::test latest .. failed

fetch cargo::test-latest failed!
if you want to get more verbose errors, please see:
  -> /home/w568w/Projects/rarmos/build/.packages/c/cargo_test/latest/cache/installdir.failed/logs/install.txt
error: install failed!

初步判断问题在于 xmake 默认的复制来源是 {临时源码目录}/target/release/deps,而 Cargo 仅会在该目录储存编译到当前主机架构的结果,实际的目标编译结果在 {临时源码目录}/target/{目标架构名}/release/deps(即 target/aarch64-unknown-none/release/deps)中,所以这里可能也需要传递给 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

但新的问题是最终编译项目时使用的是 rustcrustc 并不会尊重 .cargo/config.toml 的配置,所以必须追加额外的 target 参数:

add_rcflags("--target=aarch64-unknown-none", {force = true})

再编辑:但是还不够……因为我实际的项目中依据不同的编译目标包含了条件依赖

[target.'cfg(target_arch = "aarch64")'.dependencies]
aarch64-cpu = "^9.3"

而 xmake 简单地忽略了将这些依赖增加到最终编译命令的可能性:

function _get_names_of_libraries(name, 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

因此,还必须追加参数……

add_rcflags("--extern aarch64_cpu=/home/w568w/Projects/rarmos/build/.packages/c/cargo_test/latest/5e6547d0654e4584b80110922bf0699c/lib/libaarch64_cpu-856e6420af7a70c9.rlib", {force = true})

……然后终于编译成功了。

@w568w
Copy link
Contributor Author

w568w commented Aug 8, 2023

@waruqi 我的建议是不要用字符串文本匹配的方式解析 Cargo.toml,TOML 的格式非常丰富多变(尤其是 Cargo.toml)。相反地,可以依赖于 cargo metadata 这样的命令来读取现有包的参数,它会按 Rust 实际的方式直接解析当前 Crate,输出正确的 JSON 格式的包元信息。

p.s. 我对 Lua 不甚熟悉……但如果有时间的话(不是最近),我可以学习一下然后来帮助贡献该部分。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


@waruqi My suggestion is not to parse Cargo.toml with strings, TOML is very rich and varied. Instead, one can rely on commands like cargo metadata to read the parameters of an existing package, which will be The actual way of Rust is to directly parse the current Crate and output the correct package meta-information in JSON format.

@waruqi
Copy link
Member

waruqi commented Aug 8, 2023

这个就是 rust 的 cross build 么。。其实早期 交叉编译是支持的,后来好像 rustc 大改过,break 了很多东西,所以这边重新适配了,所以暂时把交叉编译支持给去掉了。。等后面有空看看 再加上。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


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.

@waruqi waruqi changed the title 无法编译带 no_std 标记的 Rust 目标 / Cannot compile a Rust target with no_std attribute Support for rust cross-compilation Aug 8, 2023
@waruqi waruqi added feature request and removed bug labels Aug 8, 2023
@waruqi waruqi added this to the v2.8.2 milestone Aug 8, 2023
@w568w
Copy link
Contributor Author

w568w commented Aug 8, 2023

这个就是 rust 的 cross build 么。。

其实和 cross build 也不完全一样,只能说有重合。cross build 因为一般都是面向嵌入式设备或者内核环境,其架构一般是 Tier 2 支持,不具有 std 库,所以常常会遇到必须使用 no_std 的情况。

但做非交叉编译时也可能遇到不得不用 no_std 的情况,例如在 64 位 Windows 上的 Legacy 编译目标 x86_64-pc-windows-msvc 或是在 ARM Redox 系统中的编译目标 aarch64-unknown-redox 就都是不具有 std 支持的。


有关上面的条件编译问题,我会另开一个 issue。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


Is this the cross build of rust? .

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 std library, so it is often necessary to use no_std.

However, when doing non-cross compilation, you may also encounter situations where you have to use no_std, such as the Legacy compilation target x86_64-pc-windows-msvc on 64-bit Windows or the compilation target aarch64- unknown-redox does not have std support.

@waruqi
Copy link
Member

waruqi commented Aug 8, 2023

初步搞了下,可以先试试,#4052

xmake update -s github:xmake-io/xmake#rust

加了一个测试 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 + xmake 或者命令配置切换 xmake f -a aarch64-unknown-none; xmake 都可以。。

.cargo/config.toml 不用加,默认会自动根据 target 生成一个切 target 。。

但做非交叉编译时也可能遇到不得不用 no_std 的情况,例如在 64 位 Windows 上的 Legacy 编译目标 x86_64-pc-windows-msvc 或是在 ARM Redox 系统中的编译目标 aarch64-unknown-redox 就都是不具有 std 支持的。

目前提供了 std/main configs ,可以单独配置。。

@waruqi
Copy link
Member

waruqi commented Aug 8, 2023

@waruqi 我的建议是不要用字符串文本匹配的方式解析 Cargo.toml,TOML 的格式非常丰富多变(尤其是 Cargo.toml)。相反地,可以依赖于 cargo metadata 这样的命令来读取现有包的参数,它会按 Rust 实际的方式直接解析当前 Crate,输出正确的 JSON 格式的包元信息。

p.s. 我对 Lua 不甚熟悉……但如果有时间的话(不是最近),我可以学习一下然后来帮助贡献该部分。

这个后两天有空了 再看看

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


@waruqi My suggestion is not to parse Cargo.toml with string literal matching, TOML has a very rich and varied format (especially Cargo.toml). Instead, one can rely on commands like cargo metadata to read the parameters of an existing package, which will The actual way of Rust is to directly parse the current Crate and output the correct package meta-information in JSON format.

p.s. I'm new to Lua...but if I have time (not recently), I can learn it and help contribute to that part.

I'm free in the next two days and I'll check it out

@w568w
Copy link
Contributor Author

w568w commented Aug 8, 2023

初步搞了下,可以先试试,#4052

测试了可以正常编译。感谢!:tada:

我也趁热打铁,争取最近交个 _get_names_of_libraries 的 Patch 上来。

@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


After preliminary work, you can try it first, #4052

Tested that it can be compiled normally. grateful! 🎉

I'm also striking while the iron is hot, trying to submit a patch for _get_names_of_libraries recently.

@waruqi
Copy link
Member

waruqi commented Aug 9, 2023

merge 了。。

@waruqi waruqi closed this as completed Aug 9, 2023
@Issues-translate-bot
Copy link

Bot detected the issue body's language is not English, translate it automatically.


merged. .

@Yangff
Copy link

Yangff commented May 26, 2024

虽然 {临时源码目录}/target/release/deps 下面只有当前主机的依赖,但是这些依赖也是必须的。例如,它们包含了宏的编译结果。

这两个依赖都需要复制并且在使用 rustc 的时候通过 -L指定路径,否则会造成类似这样的错误

PS 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 
-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
error[E0463]: can't find crate for `patternsleuth`
 --> deps\first\patternsleuth_bind\src\lib.rs:5:5
  |
5 | use patternsleuth::resolvers::{
  |     ^^^^^^^^^^^^^ can't find crate

error: cannot determine resolution for the macro `impl_collector`
  --> deps\first\patternsleuth_bind\src\lib.rs:19:1
   |
19 | impl_collector! {
   | ^^^^^^^^^^^^^^
   |
   = note: import resolution is stuck, try simplifying macro imports

error[E0463]: can't find crate for `patternsleuth`
   --> deps\first\patternsleuth_bind\src\lib.rs:101:15
    |
101 |     let exe = patternsleuth::process::internal::read_image()?;
    |               ^^^^^^^^^^^^^ can't find crate

error[E0433]: failed to resolve: use of undeclared type `UE4SSResolution`
   --> deps\first\patternsleuth_bind\src\lib.rs:105:34
    |
105 |     let resolution = exe.resolve(UE4SSResolution::resolver())?;
    |                                  ^^^^^^^^^^^^^^^ use of undeclared type `UE4SSResolution`

error: aborting due to 4 previous errors

正确的调用为

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

方能正确编译出结果

@Yangff
Copy link

Yangff commented May 26, 2024

    if target then
        os.vcp(path.join(sourcedir, "target", target, opt.mode == "debug" and "debug" or "release", "deps"), path.join(installdir, "lib"))
        -- copy everything host target to lib
        os.vcp(path.join(sourcedir, "target", opt.mode == "debug" and "debug" or "release", "deps", "*"), path.join(installdir, "lib"))
    else
        os.vcp(path.join(sourcedir, "target", opt.mode == "debug" and "debug" or "release", "deps"), path.join(installdir, "lib"))
    end

这样可以workaround一下但是感觉分开两个文件夹会更好

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants