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 compile from gnu to musl not working on Fedora 28 #980

Closed
dwaynebradley opened this issue Aug 23, 2018 · 4 comments
Closed

Cross compile from gnu to musl not working on Fedora 28 #980

dwaynebradley opened this issue Aug 23, 2018 · 4 comments

Comments

@dwaynebradley
Copy link

Here is what I'm seeing when trying to target x86_64-unknown-linux-musl on my Fedora 28 VM:

[dwayne@fedora-i3 openfmb-rust-sample-code]$ cargo build --target=x86_64-unknown-linux-musl
   Compiling openssl-sys v0.9.35
error: failed to run custom build command for `openssl-sys v0.9.35`
process didn't exit successfully: `/home/dwayne/git/openfmb-rust-sample-code/target/debug/build/openssl-sys-4d591416ba02a8e5/build-script-main` (exit code: 101)
--- stdout
cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_LIB_DIR
cargo:rerun-if-env-changed=OPENSSL_LIB_DIR
cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_INCLUDE_DIR
cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR
cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_DIR
cargo:rerun-if-env-changed=OPENSSL_DIR
run pkg_config fail: "Cross compilation detected. Use PKG_CONFIG_ALLOW_CROSS=1 to override"

--- stderr
thread 'main' panicked at '

Could not find directory of OpenSSL installation, and this `-sys` crate cannot
proceed without this knowledge. If OpenSSL is installed and this crate had
trouble finding it,  you can set the `OPENSSL_DIR` environment variable for the
compilation process.

Make sure you also have the development packages of openssl installed.
For example, `libssl-dev` on Ubuntu or `openssl-devel` on Fedora.

If you're in a situation where you think the directory *should* be found
automatically, please open a bug at https://github.com/sfackler/rust-openssl
and include information about your system as well as this message.

    $HOST = x86_64-unknown-linux-gnu
    $TARGET = x86_64-unknown-linux-musl
    openssl-sys = 0.9.35

', /home/dwayne/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/openssl-sys-0.9.35/build/main.rs:265:9
note: Run with `RUST_BACKTRACE=1` for a backtrace.

I also tried to use the PKG_CONFIG_ALLOW_CROSS=1 option as well but then I get this:

[dwayne@fedora-i3 openfmb-rust-sample-code]$ PKG_CONFIG_ALLOW_CROSS=1 cargo build --target=x86_64-unknown-linux-musl
   Compiling openssl-sys v0.9.35
error: failed to run custom build command for `openssl-sys v0.9.35`
process didn't exit successfully: `/home/dwayne/git/openfmb-rust-sample-code/target/debug/build/openssl-sys-4d591416ba02a8e5/build-script-main` (exit code: 101)
--- stdout
cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_LIB_DIR
cargo:rerun-if-env-changed=OPENSSL_LIB_DIR
cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_INCLUDE_DIR
cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR
cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_DIR
cargo:rerun-if-env-changed=OPENSSL_DIR
cargo:rustc-link-lib=ssl
cargo:rustc-link-lib=crypto
OPT_LEVEL = Some("0")
TARGET = Some("x86_64-unknown-linux-musl")
HOST = Some("x86_64-unknown-linux-gnu")
TARGET = Some("x86_64-unknown-linux-musl")
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
HOST = Some("x86_64-unknown-linux-gnu")
CROSS_COMPILE = None
TARGET = Some("x86_64-unknown-linux-musl")
HOST = Some("x86_64-unknown-linux-gnu")
CFLAGS_x86_64-unknown-linux-musl = None
CFLAGS_x86_64_unknown_linux_musl = None
TARGET_CFLAGS = None
CFLAGS = None
DEBUG = Some("true")
running: "musl-gcc" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-m64" "-static" "-Wall" "-Wextra" "-E" "/home/dwayne/git/openfmb-rust-sample-code/target/x86_64-unknown-linux-musl/debug/build/openssl-sys-8788530f8302f9f8/out/expando.c"
cargo:warning=/home/dwayne/git/openfmb-rust-sample-code/target/x86_64-unknown-linux-musl/debug/build/openssl-sys-8788530f8302f9f8/out/expando.c:1:10: fatal error: openssl/opensslv.h: No such file or directory
cargo:warning= #include <openssl/opensslv.h>
cargo:warning=          ^~~~~~~~~~~~~~~~~~~~
cargo:warning=compilation terminated.
exit code: 1

--- stderr
thread 'main' panicked at '
Header expansion error:
Error { kind: ToolExecError, message: "Command \"musl-gcc\" \"-O0\" \"-ffunction-sections\" \"-fdata-sections\" \"-fPIC\" \"-g\" \"-m64\" \"-static\" \"-Wall\" \"-Wextra\" \"-E\" \"/home/dwayne/git/openfmb-rust-sample-code/target/x86_64-unknown-linux-musl/debug/build/openssl-sys-8788530f8302f9f8/out/expando.c\" with args \"musl-gcc\" did not execute successfully (status code exit code: 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/dwayne/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/openssl-sys-0.9.35/build/main.rs:428:13
note: Run with `RUST_BACKTRACE=1` for a backtrace.

I have made sure that openssl-devel is installed:


[dwayne@fedora-i3 ~]$ sudo dnf install openssl-devel
Last metadata expiration check: 0:53:11 ago on Thu 23 Aug 2018 02:41:36 PM EDT.
Package openssl-devel-1:1.1.0h-3.fc28.x86_64 is already installed, skipping.
Dependencies resolved.
Nothing to do.
Complete!

pkg_config is installed as well:

[dwayne@fedora-i3 ~]$ pkg-config --list-package-names
mit-krb5-gssapi
mit-krb5
libxcrypt
jemalloc
krb5-gssapi
libpcre2-32
zlib
libselinux
openssl
kdb
libelf
fontutil
libpcre2-8
libsepol
libcrypt
kadm-server
libpcre2-16
kadm-client
libpcre2-posix
libpcap
com_err
libverto
krb5
libssl
libcrypto
gssrpc
shared-mime-info
gnome-keybindings
dracut
bash-completion
xbitmaps
libquvi-scripts-0.9
udev
systemd
[dwayne@fedora-i3 ~]$ pkg-config --list-all
mit-krb5-gssapi                mit-krb5-gssapi - Kerberos implementation of the GSSAPI
mit-krb5                       mit-krb5 - An implementation of Kerberos network authentication
libxcrypt                      libxcrypt - Extended crypt library for DES, MD5, Blowfish and others
jemalloc                       jemalloc - A general purpose malloc(3) implementation that emphasizes fragmentation avoidance and scalable concurrency support.
krb5-gssapi                    krb5-gssapi - Kerberos implementation of the GSSAPI
libpcre2-32                    libpcre2-32 - PCRE2 - Perl compatible regular expressions C library (2nd API) with 32 bit character support
zlib                           zlib - zlib compression library
libselinux                     libselinux - SELinux utility library
openssl                        OpenSSL - Secure Sockets Layer and cryptography libraries and tools
kdb                            kdb - Kerberos database access libraries
libelf                         libelf - elfutils libelf library to read and write ELF files
fontutil                       FontUtil - Font utilities dirs
libpcre2-8                     libpcre2-8 - PCRE2 - Perl compatible regular expressions C library (2nd API) with 8 bit character support
libsepol                       libsepol - SELinux policy library
libcrypt                       libxcrypt - Extended crypt library for DES, MD5, Blowfish and others
kadm-server                    kadm-server - Kerberos administration server library
libpcre2-16                    libpcre2-16 - PCRE2 - Perl compatible regular expressions C library (2nd API) with 16 bit character support
kadm-client                    kadm-client - Kerberos administration client library
libpcre2-posix                 libpcre2-posix - Posix compatible interface to libpcre2-8
libpcap                        libpcap - Platform-independent network traffic capture library
com_err                        com_err - Common error description library
libverto                       libverto - Event loop abstraction interface
krb5                           krb5 - An implementation of Kerberos network authentication
libssl                         OpenSSL-libssl - Secure Sockets Layer and cryptography libraries
libcrypto                      OpenSSL-libcrypto - OpenSSL cryptography library
gssrpc                         gssrpc - GSSAPI RPC implementation
shared-mime-info               shared-mime-info - Freedesktop common MIME database
gnome-keybindings              gnome-keybindings - Keybindings configuration for GNOME applications
dracut                         dracut - dracut
bash-completion                bash-completion - programmable completion for the bash shell
xbitmaps                       X bitmaps - Bitmaps that are shared between X applications
libquvi-scripts-0.9            libquvi-scripts - Scripts for libquvi
udev                           udev - udev
systemd                        systemd - systemd System and Service Manager

Anything else that I could try?

@sfackler
Copy link
Owner

You need to have a copy of OpenSSL that's cross compiled to MUSL as well, and point openssl-sys to it. Alternatively, you can use the vendored Cargo feature to have openssl-sys build a copy for you.

@dwaynebradley
Copy link
Author

Thanks! That helped. I'm still learning my way around Rust. I had to add the following to my Cargo.toml file:

[dependencies]
nats = "*"
protobuf = { version = "~2.0" }
# Add openssl-sys as a direct dependency so it can be cross compiled to
# x86_64-unknown-linux-musl using the "vendored" feature below
openssl-sys = "*"

[features]
# Force openssl-sys to staticly link in the openssl library. Necessary when
# cross compiling to x86_64-unknown-linux-musl.
vendored = ["openssl-sys/vendored"]

This way I can compile to a single executable with no library dependencies using:

cargo build --target=x86_64-unknown-linux-musl --features vendored

[dwayne@fedora-i3 debug]$ ldd openfmb_rust_sample_code
	not a dynamic executable

If there is a better, more "rust" way, of doing this, I'm all ears!

@sfackler
Copy link
Owner

That's the way to do it!

@dwaynebradley
Copy link
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants