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

Cross compilation (of libc) on MacOS fails #66

Open
raphaelcohn opened this issue Feb 19, 2021 · 3 comments
Open

Cross compilation (of libc) on MacOS fails #66

raphaelcohn opened this issue Feb 19, 2021 · 3 comments
Labels
S-needs-investigation Status: needs investigation upstream An issue with the standard library itself or its dependencies.

Comments

@raphaelcohn
Copy link

I'm cross compiling for a linux musl target on MacOS, using the latest version of the homebrew musl-cross package.
This works fine for normal work I do.
However, when attempting to do a standard build, the libc rust crate fails with error: could not find native static library c, perhaps an -L flag is missing?. Rightly so, as it has been linked added as a dependency to rustc command line (long trace below).

This is because libc is odd; it doesn't emit use cargo:rustc-link-search in it's build script, build.rs. A patch to do this back in 2015 was rejected because it's 'fixed upstream' (rust-lang/libc#17) in rust itself (rust-lang/rust@52862e4). I have to say, I didn't follow this reasoning.

This is a place where one would like to inject a custom flag - I know where and how to search for the libc - but I can only do so by modifying RUSTFLAGS, which affects everything else being built. Of course, I could write a rustc wrapper script, do a bit of argument sense checking, and then call rustc, but that's... work. (As an aside, when doing controlled, reproducible builds, I do this a lot, unsetting PATH and then providing shim wrappers for tools until I build up a profile of what's needed to do a reproducible build, especially if build tools call date and the like).

This seems related to #14 and other issues.

   Compiling alloc v0.0.0 (/Users/raph/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/alloc)
     Running `CARGO=/Users/raph/.rustup/toolchains/nightly-x86_64-apple-darwin/bin/cargo CARGO_CRATE_NAME=alloc CARGO_MANIFEST_DIR=/Users/raph/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/alloc CARGO_PKG_AUTHORS='The Rust Project Developers' CARGO_PKG_DESCRIPTION='' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE='' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=alloc CARGO_PKG_REPOSITORY='' CARGO_PKG_VERSION=0.0.0 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=0 CARGO_PKG_VERSION_PATCH=0 CARGO_PKG_VERSION_PRE='' DYLD_FALLBACK_LIBRARY_PATH='/Volumes/Source/lemonrock/linux-support/rust-custom-target.workspace/target/release/deps:/Users/raph/.rustup/toolchains/nightly-x86_64-apple-darwin/lib:/Users/raph/.rustup/toolchains/nightly-x86_64-apple-darwin/lib:/Users/raph/lib:/usr/local/lib:/usr/lib' RUSTC_BOOTSTRAP=1 rustc --crate-name alloc --edition=2018 /Users/raph/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C embed-bitcode=no -C metadata=d600ad6f2ddde8a8 -C extra-filename=-d600ad6f2ddde8a8 --out-dir /Volumes/Source/lemonrock/linux-support/rust-custom-target.workspace/target/x86_64-unknown-linux-musl-tiger-lake/release/deps --target /Volumes/Source/lemonrock/linux-support/rust-custom-target.workspace/x86_64-unknown-linux-musl-tiger-lake.json -C linker=x86_64-linux-musl-cc -Z force-unstable-if-unmarked -L dependency=/Volumes/Source/lemonrock/linux-support/rust-custom-target.workspace/target/x86_64-unknown-linux-musl-tiger-lake/release/deps -L dependency=/Volumes/Source/lemonrock/linux-support/rust-custom-target.workspace/target/release/deps --extern compiler_builtins=/Volumes/Source/lemonrock/linux-support/rust-custom-target.workspace/target/x86_64-unknown-linux-musl-tiger-lake/release/deps/libcompiler_builtins-e59aa6ff3d802cf9.rmeta --extern core=/Volumes/Source/lemonrock/linux-support/rust-custom-target.workspace/target/x86_64-unknown-linux-musl-tiger-lake/release/deps/libcore-0e79ae0d5145d2a2.rmeta --cap-lints warn -Z verbose=yes -Z print-link-args=yes -L native=c`
error: could not compile `libc`

Caused by:
  process didn't exit successfully: `CARGO=/Users/raph/.rustup/toolchains/nightly-x86_64-apple-darwin/bin/cargo CARGO_CRATE_NAME=libc CARGO_MANIFEST_DIR=/Volumes/Source/lemonrock/linux-support/rust-custom-target.workspace/vendor/libc-0.2.79 CARGO_PKG_AUTHORS='The Rust Project Developers' CARGO_PKG_DESCRIPTION='Raw FFI bindings to platform libraries like libc.
  ' CARGO_PKG_HOMEPAGE='https://github.com/rust-lang/libc' CARGO_PKG_LICENSE='MIT OR Apache-2.0' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=libc CARGO_PKG_REPOSITORY='https://github.com/rust-lang/libc' CARGO_PKG_VERSION=0.2.79 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=2 CARGO_PKG_VERSION_PATCH=79 CARGO_PKG_VERSION_PRE='' DYLD_FALLBACK_LIBRARY_PATH='/Volumes/Source/lemonrock/linux-support/rust-custom-target.workspace/target/release/deps:/Users/raph/.rustup/toolchains/nightly-x86_64-apple-darwin/lib:/Users/raph/.rustup/toolchains/nightly-x86_64-apple-darwin/lib:/Users/raph/lib:/usr/local/lib:/usr/lib' OUT_DIR=/Volumes/Source/lemonrock/linux-support/rust-custom-target.workspace/target/x86_64-unknown-linux-musl-tiger-lake/release/build/libc-19f238fd5225b055/out RUSTC_BOOTSTRAP=1 rustc --crate-name libc /Volumes/Source/lemonrock/linux-support/rust-custom-target.workspace/vendor/libc-0.2.79/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C embed-bitcode=no --cfg 'feature="align"' --cfg 'feature="rustc-dep-of-std"' --cfg 'feature="rustc-std-workspace-core"' -C metadata=a26b5536f1af61b7 -C extra-filename=-a26b5536f1af61b7 --out-dir /Volumes/Source/lemonrock/linux-support/rust-custom-target.workspace/target/x86_64-unknown-linux-musl-tiger-lake/release/deps --target /Volumes/Source/lemonrock/linux-support/rust-custom-target.workspace/x86_64-unknown-linux-musl-tiger-lake.json -C linker=x86_64-linux-musl-cc -Z force-unstable-if-unmarked -L dependency=/Volumes/Source/lemonrock/linux-support/rust-custom-target.workspace/target/x86_64-unknown-linux-musl-tiger-lake/release/deps -L dependency=/Volumes/Source/lemonrock/linux-support/rust-custom-target.workspace/target/release/deps --extern rustc_std_workspace_core=/Volumes/Source/lemonrock/linux-support/rust-custom-target.workspace/target/x86_64-unknown-linux-musl-tiger-lake/release/deps/librustc_std_workspace_core-a9fcb53369c5d212.rmeta --cap-lints warn -Z verbose=yes -Z print-link-args=yes -L native=c --cfg freebsd11 --cfg libc_priv_mod_use --cfg libc_union --cfg libc_const_size_of --cfg libc_align --cfg libc_core_cvoid --cfg libc_packedN --cfg libc_cfg_target_vendor --cfg libc_thread_local` (exit code: 1)
@12101111
Copy link

error: could not find native static library `c`, perhaps an -L flag is missing?

error: aborting due to previous error

error: could not compile `libc`

Caused by:
  process didn't exit successfully: `CARGO=/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/bin/cargo CARGO_CRATE_NAME=libc CARGO_MANIFEST_DIR=/home/han/.cargo/registry/src/gitpro.ttaallkk.top-1ecc6299db9ec823/libc-0.2.88 CARGO_PKG_AUTHORS='The Rust Project Developers' CARGO_PKG_DESCRIPTION='Raw FFI bindings to platform libraries like libc.
  ' CARGO_PKG_HOMEPAGE='https://github.com/rust-lang/libc' CARGO_PKG_LICENSE='MIT OR Apache-2.0' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=libc CARGO_PKG_REPOSITORY='https://github.com/rust-lang/libc' CARGO_PKG_VERSION=0.2.88 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=2 CARGO_PKG_VERSION_PATCH=88 CARGO_PKG_VERSION_PRE='' LD_LIBRARY_PATH='/usr/obj/rust/debug/deps:/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib:/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib' OUT_DIR=/usr/obj/rust/x86_64-unknown-linux-musl/debug/build/libc-4cf2a7bffddd2e5d/out RUSTC_BOOTSTRAP=1 rustc --crate-name libc /home/han/.cargo/registry/src/gitpro.ttaallkk.top-1ecc6299db9ec823/libc-0.2.88/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 --cfg 'feature="align"' --cfg 'feature="rustc-dep-of-std"' --cfg 'feature="rustc-std-workspace-core"' -C metadata=d244570cd2e0e60e -C extra-filename=-d244570cd2e0e60e --out-dir /usr/obj/rust/x86_64-unknown-linux-musl/debug/deps --target x86_64-unknown-linux-musl -Z force-unstable-if-unmarked -L dependency=/usr/obj/rust/x86_64-unknown-linux-musl/debug/deps -L dependency=/usr/obj/rust/debug/deps --extern rustc_std_workspace_core=/usr/obj/rust/x86_64-unknown-linux-musl/debug/deps/librustc_std_workspace_core-597cd0340bdb6a9d.rmeta --cap-lints warn -Clinker=ld.lld -Ctarget-feature=-crt-static --cfg freebsd11 --cfg libc_priv_mod_use --cfg libc_union --cfg libc_const_size_of --cfg libc_align --cfg libc_core_cvoid --cfg libc_packedN --cfg libc_cfg_target_vendor --cfg libc_thread_local` (exit status: 1)

It also failed on a musl system

@12101111
Copy link

12101111 commented Jul 5, 2021

A workaround for this is:

export RUSTFLAGS="-Lnative=/usr/lib"

(replace /usr/lib with the path to libc.a)

And a fix would be like rust-lang/libc#1327 plus some adjustment in rustbuild like rust-lang/rust#85600 to copy libc.a into self-contained directory.

@Follpvosten
Copy link

This also happens for me on x86_64-unknown-linux-gnu; you can clearly see that it tries to compile core and std from the wrong toolchain.

bors added a commit to rust-lang-ci/rust that referenced this issue Nov 3, 2021
Provide standalone libc.a in self-contained for musl and wasi

This is a prerequisites of rust-lang/libc#2272, which in turn fix:

- rust-lang/wg-cargo-std-aware#66
- rust-lang#89626
bors added a commit to rust-lang/libc that referenced this issue Nov 4, 2021
Use link modifiers -bundle on musl and wasi target

Implement rust-lang/rust#72274

Fix:

- rust-lang/wg-cargo-std-aware#66
- rust-lang/rust#89626

TODO:

- [x] Implement rustbuild side change: rust-lang/rust#90527
bors added a commit to rust-lang/libc that referenced this issue Nov 4, 2021
Use link modifiers -bundle on musl and wasi target

Implement rust-lang/rust#72274

Fix:

- rust-lang/wg-cargo-std-aware#66
- rust-lang/rust#89626

TODO:

- [x] Implement rustbuild side change: rust-lang/rust#90527
bors added a commit to rust-lang/libc that referenced this issue Nov 4, 2021
Use link modifiers -bundle on musl and wasi target

Implement rust-lang/rust#72274

Fix:

- rust-lang/wg-cargo-std-aware#66
- rust-lang/rust#89626

TODO:

- [x] Implement rustbuild side change: rust-lang/rust#90527
@ehuss ehuss added upstream An issue with the standard library itself or its dependencies. S-needs-investigation Status: needs investigation labels May 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-needs-investigation Status: needs investigation upstream An issue with the standard library itself or its dependencies.
Projects
None yet
Development

No branches or pull requests

4 participants