Skip to content

-C target-feature=crt-static does not enable cdylib for musl when passed as CLI argument to cargo rustc #10143

Open
@davidhewitt

Description

@davidhewitt

Problem

Try building a cdylib crate with cargo rustc for x86_64-unknown-linux-musl target, using -C target-feature=-crt-static on the command line. This will fail.

e.g. the namespace_package example in setuptools_rust (https://github.com/PyO3/setuptools-rust/tree/main/examples/namespace_package):

$ cd setuptools-rust/examples/namespace_package
$ cargo rustc --lib --target x86_64-unknown-linux-musl -- -C target-feature=-crt-static
error: cannot produce cdylib for `namespace_package_rust v0.1.0 (/home/david/dev/setuptools-rust/examples/namespace_package)` as the target `x86_64-unknown-linux-musl` does not support these crate types

But it does work, as long as the flag is sent in RUSTFLAGS:

$ cd setuptools-rust/examples/namespace_package
$ RUSTFLAGS="-C target-feature=-crt-static" cargo rustc --lib --target x86_64-unknown-linux-musl
    # (compilation output)
    Finished dev [unoptimized + debuginfo] target(s) in 4.09s

Steps

No response

Possible Solution(s)

Looking at src/cargo/core/compiler/build_context/target_info.rs, it looks to me like cargo uses rustc --print=file-names to check whether cdylib crate type is supported:

# Without -C target-feature=-crt-static, `rustc` does not support `cdylib` for this target
$ rustc <(echo "") --crate-type=cdylib --print=file-names --target=x86_64-unknown-linux-musl --crate-name ___
warning: dropping unsupported crate type `cdylib` for target `x86_64-unknown-linux-musl`

warning: 1 warning emitted

# With -C target-feature=-crt-static, `rustc` is happy to crate a cdylib
$ rustc <(echo "") --crate-type=cdylib --print=file-names --target=x86_64-unknown-linux-musl --crate-name ___ -C target-feature=-crt-static
lib___.so

Also in target_info.rs, in TargetInfo::new it looks like the contents of RUSTFLAGS are sent to the rustc --print=file-names invocation but the CLI args passed to cargo rustc are not. That would be consistent with my observations above.

I think a fix is to include these CLI arguments? I am unsure of the possible consequences which is why I didn't author a PR to thread them through and instead wrote a bug report.

Notes

No response

Version

cargo 1.56.0 (4ed5d137b 2021-10-04)
release: 1.56.0
commit-hash: 4ed5d137baff5eccf1bae5a7b2ae4b57efad4a7d
commit-date: 2021-10-04

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bugCommand-rustcS-triageStatus: This issue is waiting on initial triage.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions