Skip to content

Unable to cross compile for musl #1627

Closed
@detly

Description

@detly

This happens the same way under cross with a custom docker image, or bare cargo on an Ubuntu env. To simplify things, I'll just give the local command (for Ubuntu 21.10). This is a GNU host, but what I want is a statically-linked-with-musl-libc binary.

Assume I've done sudo apt install pkg-config libssl-dev musl-tools.

⚬ OPENSSL_LIB_DIR=/usr/lib/x86_64-linux-gnu/ OPENSSL_INCLUDE_DIR=/usr/include/ OPENSSL_STATIC=1 cargo build --target x86_64-unknown-linux-musl
   Compiling openssl-sys v0.9.72
   Compiling socket2 v0.4.4
   Compiling signal-hook-registry v1.4.0
   Compiling atty v0.2.14
   Compiling mio v0.8.2
   Compiling idna v0.2.3
   Compiling syn v1.0.92
   Compiling nix v0.23.1
   Compiling regex v1.5.5
The following warnings were emitted during compilation:

warning: build/expando.c:2:10: fatal error: openssl/opensslconf.h: No such file or directory
warning:     2 | #include <openssl/opensslconf.h>
warning:       |          ^~~~~~~~~~~~~~~~~~~~~~~
warning: compilation terminated.

error: failed to run custom build command for `openssl-sys v0.9.72`

Caused by:
  process didn't exit successfully: `/home/jason/Code/[...]/target/debug/build/openssl-sys-51dce0970b29e049/build-script-main` (exit status: 101)
  --- stdout
  cargo:rustc-cfg=const_fn
  cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_LIB_DIR
  X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_LIB_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_LIB_DIR
  OPENSSL_LIB_DIR = /usr/lib/x86_64-linux-gnu/
  cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_INCLUDE_DIR
  X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_INCLUDE_DIR unset
  cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR
  OPENSSL_INCLUDE_DIR = /usr/include/
  cargo:rustc-link-search=native=/usr/lib/x86_64-linux-gnu/
  cargo:include=/usr/include/
  cargo:rerun-if-changed=build/expando.c
  OPT_LEVEL = Some("0")
  TARGET = Some("x86_64-unknown-linux-musl")
  HOST = Some("x86_64-unknown-linux-gnu")
  CC_x86_64-unknown-linux-musl = None
  CC_x86_64_unknown_linux_musl = None
  TARGET_CC = None
  CC = None
  CROSS_COMPILE = None
  CFLAGS_x86_64-unknown-linux-musl = None
  CFLAGS_x86_64_unknown_linux_musl = None
  TARGET_CFLAGS = None
  CFLAGS = None
  CRATE_CC_NO_DEFAULTS = 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" "/usr/include/" "-Wall" "-Wextra" "-E" "build/expando.c"
  cargo:warning=build/expando.c:2:10: fatal error: openssl/opensslconf.h: No such file or directory
  cargo:warning=    2 | #include <openssl/opensslconf.h>
  cargo:warning=      |          ^~~~~~~~~~~~~~~~~~~~~~~
  cargo:warning=compilation terminated.
  exit status: 1

  --- stderr
  thread 'main' panicked at '
  Header expansion error:
  Error { kind: ToolExecError, message: "Command \"musl-gcc\" \"-O0\" \"-ffunction-sections\" \"-fdata-sections\" \"-fPIC\" \"-g\" \"-fno-omit-frame-pointer\" \"-m64\" \"-I\" \"/usr/include/\" \"-Wall\" \"-Wextra\" \"-E\" \"build/expando.c\" with args \"musl-gcc\" did not execute successfully (status code exit status: 1)." }

  Failed to find OpenSSL development headers.

  You can try fixing this setting the `OPENSSL_DIR` environment variable
  pointing to your OpenSSL installation or installing OpenSSL headers package
  specific to your distribution:

      # On Ubuntu
      sudo apt-get install libssl-dev
      # On Arch Linux
      sudo pacman -S openssl
      # On Fedora
      sudo dnf install openssl-devel

  See rust-openssl README for more information:

      https://github.com/sfackler/rust-openssl#linux
  ', /home/jason/.cargo/registry/src/github.com-1ecc6299db9ec823/openssl-sys-0.9.72/build/main.rs:162:13
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
warning: build failed, waiting for other jobs to finish...
error: build failed

Fundamentally the problem seems to be:

  • opensslconf.h is at /usr/include/x86_64-linux-gnu/openssl/opensslconf.h
  • other OpenSSL headers are at /usr/include/openssl

So there is no way to set OPENSSL_INCLUDE_DIR to a value that satisfies this, and pkg-config doesn't seem to help.

This seems similar to #603, but (a) I would be happy with a statically linked OpenSSL, and I don't think they're doing that and (b) I can't even do this via cross with a custom image containing OpenSSL, I get the same error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions