Conditional compilation cargo problem

Hi folks,

I'm trying to have conditional compilation related to whether or not I'm performing a musl build.

This is what the relevant part of Cargo.toml looks like:

[target.'cfg(target_env = "musl")'.dependencies]
async-compression = { version = "0.3", features = ["tokio", "gzip"] }
[target.'cfg(not(target_env = "musl"))'.dependencies]
async-compression = { version = "0.3", features = ["tokio", "gzip", "bzip2"] }

So I'd like to have different features for the async-compression crate depending on whether or not I'm doing a musl build.

The code in question looks like this:

#[cfg(not(target_env = "musl"))]
async fn create_bz2_file_writer(
    path: &Path,
    append: bool,
) -> Result<Pin<Box<dyn AsyncWrite + Send>>> {
    use async_compression::{tokio::write::BzEncoder, Level};

    let file = create_file(path, append).await?;

    Ok(Box::pin(BzEncoder::with_quality(file, Level::Best)))

#[cfg(target_env = "musl")]
async fn create_bz2_file_writer(
    _path: &Path,
    _append: bool,
) -> Result<Pin<Box<dyn AsyncWrite + Send>>> {
    Err(eyre!("Bzip2 is not available in musl environment!"))

I just include it for completeness since that part of conditional compilation is working if I switch the features manually in my Cargo.toml.

If I try to build with cargo build --target=x86_64-unknown-linux-musl then the build fails with the following error:

❯ cargo build --target=x86_64-unknown-linux-musl
   Compiling bzip2-sys v0.1.11+1.0.8
   Compiling object v0.26.1
   Compiling socket2 v0.4.1
   Compiling terminal_size v0.1.17
error: failed to run custom build command for `bzip2-sys v0.1.11+1.0.8`

Caused by:
  process didn't exit successfully: `.../target/debug/build/bzip2-sys-523eaee88aeff94b/build-script-build` (exit status: 1)
  --- stdout
  TARGET = Some("x86_64-unknown-linux-musl")
  OPT_LEVEL = Some("0")
  HOST = Some("x86_64-apple-darwin")
  CC_x86_64-unknown-linux-musl = None
  CC_x86_64_unknown_linux_musl = None
  TARGET_CC = None
  CC = None
  CFLAGS_x86_64-unknown-linux-musl = None
  CFLAGS_x86_64_unknown_linux_musl = None
  CFLAGS = None
  DEBUG = Some("true")
  CARGO_CFG_TARGET_FEATURE = Some("fxsr,sse,sse2")
  running: "musl-gcc" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "-m64" "-I" "bzip2-1.0.8" "-D_FILE_OFFSET_BITS=64" "-DBZ_NO_STDIO" "-o" ".../target/x86_64-unknown-linux-musl/debug/build/bzip2-sys-dd8e25eee8d0c662/out/lib/bzip2-1.0.8/blocksort.o" "-c" "bzip2-1.0.8/blocksort.c"

  --- stderr

  error occurred: Failed to find tool. Is `musl-gcc` installed?

warning: build failed, waiting for other jobs to finish...
error: build failed

But this shouldn't be the case since the things that fail to compile are transitive dependencies of the "bzip2" feature that shouldn't be active anymore, assuming that everything works as I expect.

The documentation seems to indicate that I'm doing this in a valid fashion. I also tried to specify the full target, but to no avail.

Do you have any idea what I'm doing wrong? Or did I actually find a bug?

Try setting the resolver to version 2.

name = "my-package"
version = "1.0.0"
resolver = "2"

See more here.

1 Like

That did it, thank you so much1

Couldn't the old resolver emit a warning about this being unsupported. It kind of understands (because it's not complaining that it doesn't) but it just isn't working as expected.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.