Default in Ubuntu cargo build --target x86_64-unknown-linux-musl
will failed to compile openssl-sys
:
error: failed to run custom build command for `openssl-sys v0.9.54`
--- 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.54
Though you have libssl-dev
installed.
How to fix?
Build openssl
Download openssl and build it.
ln -s /usr/include/x86_64-linux-gnu/asm /usr/include/x86_64-linux-musl/asm && ln -s /usr/include/asm-generic /usr/include/x86_64-linux-musl/asm-generic && ln -s /usr/include/linux /usr/include/x86_64-linux-musl/linux
mkdir /musl
wget https://github.com/openssl/openssl/archive/OpenSSL_1_1_1f.tar.gz
tar zxvf OpenSSL_1_1_1f.tar.gz
cd openssl-OpenSSL_1_1_1f/
CC="musl-gcc -fPIE -pie" ./Configure no-shared no-async --prefix=/musl --openssldir=/musl/ssl linux-x86_64
make depend
make -j$(nproc)
make install
After openssl
is installed, you should export some ENVs and then build agent, and all will be ok.
export PKG_CONFIG_ALLOW_CROSS=1
export OPENSSL_STATIC=true
export OPENSSL_DIR=/musl
cargo build --target x86_64-unknown-linux-musl ...
Update
- you may need
apt install musl-tools
, thanks @vijayet1
References
- https://github.com/sfackler/rust-openssl/issues/183
- https://github.com/getsentry/rust-musl-cross/blob/master/Dockerfile
- https://gitlab.com/rust_musl_docker/image/-/blob/master/BaseDockerfile.template
- https://github.com/rust-lang/cargo/issues/713#issuecomment-59597433
- https://github.com/getsentry/rust-musl-cross