rust-lang/wg-cargo-std-aware

Can't build std if I specify target json file: std does not see networking

vi opened this issue · 5 comments

vi commented

Building hello world for x86_64-unknown-linux-gnu works.

But if I save x86_64-unknown-linux-gnu's target specification to json and build using that json, std fails to find socket-related things.

$ cargo --version
cargo 1.48.0-nightly (8777a6b1e 2020-09-15)
$ rustc --version
rustc 1.48.0-nightly (285fc7d70 2020-09-16)

$ rustc +nightly -Z unstable-options --print target-spec-json > mytarget.json
$ gvim mytarget.json # change `is-builin` to `false`. It doesn't matter actually.
$ cargo build --release -Zbuild-std=panic_abort,std -Zbuild-std-features=panic_immediate_abort --target=./mytarget.json
...
   Compiling std v0.0.0 (/nix/rust/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std)
error[E0432]: unresolved imports `crate::sys_common::net::getsockopt`, `crate::sys_common::net::setsockopt`, `crate::sys_common::net::sockaddr_to_addr`
 --> /nix/rust/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys/unix/net.rs:8:30
  |
8 | use crate::sys_common::net::{getsockopt, setsockopt, sockaddr_to_addr};
  |                              ^^^^^^^^^^  ^^^^^^^^^^  ^^^^^^^^^^^^^^^^ no `sockaddr_to_addr` in `sys::unix::net`
  |                              |           |
  |                              |           no `setsockopt` in `sys::unix::net`
  |                              no `getsockopt` in `sys::unix::net`

error[E0432]: unresolved import `crate::sys_common::net::LookupHost`
... (a lot of other missing things related to networking) ...

I except building using custom target json that happens to be the same as built-in json to be exactly the same as building specifying built-in target directly.

This looks like it's an issue with libstd and various cfg options. I don't think it's a bug with Cargo's support here?

ehuss commented

This isn't directly related to Cargo, but it is related to rust-lang/rust#74033, with the intent of helping with build-std support. JSON spec support isn't in a great place right now, unfortunately.

This particular issue is due to the target-name sniffing done here. If you rename the JSON file to something like x86_64-unknown-linux-gnu.json, this should work.

The intent is to fix the build scripts that inspect target, just haven't gotten around to it, yet. There should probably be an issue somewhere to track that, I don't think there is, yet. This is as good a place as any, I think, even though this is an issue with the standard library.

Currently messing around trying to cross-compile a rust binary for QNX and I also hit this issue. I moved forward by just changing the name of my custom target to something its not, which did find these symbols.

Should we inspect the value of the llvm-target instead of the target name itself? Not sure if there are any cases where the target name and llvm-target name don't lineup intentionally

This should probably not use llvm-target. I suspect it should instead use the CARGO_CFG_TARGET_* variables, see https://doc.rust-lang.org/cargo/reference/environment-variables.html. Those values can be controlled from within the target spec json. In particular, using CARGO_CFG_TARGET_OS should do the trick for the vast majority of those.

ehuss commented

Closing as resolved by rust-lang/rust#120232.