如何为x86_64-unknown-linux-musl构建依赖于curl的可执行文件

时间:2017-01-18 15:33:01

标签: rust musl

我在amd64 Debian机器上,我正在尝试构建一个x86_64-unknown-linux-musl可执行文件。我在Cargo.toml中有这个:

[dependencies]
curl = "0.4"

当我跑cargo build --target=x86_64-unknown-linux-musl时,我明白了 这样:

error: failed to run custom build command for `libz-sys v1.0.10`
process didn't exit successfully: `/tmp/foo/target/debug/build/libz-sys-c20da5f29c41e515/build-script-build` (exit code: 101)
--- stdout
OPT_LEVEL = Some("0")
PROFILE = Some("debug")
TARGET = Some("x86_64-unknown-linux-musl")
debug=true opt-level=0
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
running: "./configure" "--prefix=/tmp/foo/target/x86_64-unknown-linux-musl/debug/build/libz-sys-e109627694e9981e/out"
Compiler error reporting is too harsh for ./configure (perhaps remove -Werror).
** ./configure aborting.

--- stderr
thread 'main' panicked at 'failed to run successfully: exit code: 1', /home/tshepang/.cargo/registry/src/github.com-1ecc6299db9ec823/libz-sys-1.0.10/build.rs:189

当我重新运行时:

error: failed to run custom build command for `openssl-sys v0.9.6`
process didn't exit successfully: `/tmp/foo/target/debug/build/openssl-sys-ac9c042b062dad1d/build-script-build` (exit code: 101)
--- 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.

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.6

当我本地构建时,一切正常,即cargo build --target=x86_64-unknown-linux-gnu

搜索一下,我了解了一个环境变量PKG_CONFIG_ALLOW_CROSS:

PKG_CONFIG_ALLOW_CROSS=true cargo build --target=x86_64-unknown-linux-musl

在这样做的过程中,我还发现我错过了Debian软件包 命名为 libcurl4-openssl-dev

正在运行ldd target/target/x86_64-unknown-linux-musl/debug/foo 表明可执行文件是动态链接的,然后搜索 此外,我了解了另一个环境变量, PKG_CONFIG_ALL_STATIC:

PKG_CONFIG_ALL_STATIC=true PKG_CONFIG_ALLOW_CROSS=true cargo build --target=x86_64-unknown-linux-musl

这揭示了一大堆失踪的deps,所有这些(幸运的是) 有Debian依赖。但安装所有这些并没有帮助,因为, 最后,我仍然坐着一个没有的可执行文件 静态联系。

2 个答案:

答案 0 :(得分:3)

我放弃并最终使用cross

cargo install cross
cross build --target=x86_64-unknown-linux-musl

这太简单了,你会在target/x86_64-unknown-linux-musl/debug找到可执行文件。

答案 1 :(得分:2)

curl箱子(直接或间接)依赖于两个箱子libz-sys和openssl-sys。 一个名字以" -sys"结尾的箱子。通常是一组FFI(外部函数接口)绑定到本机C库。

建立这样一个" -sys" crate需要链接到本机库。如果您的目标是x86_64-unknown-linux-musl,那么您必须链接到针对musl构建的本机库,而不是glic。但是,您在发行版的存储库中找到的大多数软件包都提供了针对glibc构建的库。

解决方案是自己构建所需的库,链接到musl而不是glibc。

我无法访问Debian安装,但在Ubuntu 16.04上,这对于OpenSSL来说是这样的:

# this package provides the "musl-gcc" wrapper
apt-get install musl-tools 
# you will also need these, if they are not installed yet
apt-get install pkg-config xutils-dev build-essential

# Download and build OpenSSL against musl
VERS=1.0.2j
export CC=musl-gcc
export MUSL_PREFIX=/usr/local/musl
export C_INCLUDE_PATH="$MUSL_PREFIX/include/"
curl -O https://www.openssl.org/source/openssl-$VERS.tar.gz
tar xvzf openssl-$VERS.tar.gz
cd openssl-$VERS

./config --prefix "$MUSL_PREFIX"
make depend
make
sudo make install

export OPENSSL_DIR=/usr/local/musl/
export OPENSSL_STATIC=1

一旦你有一个相同的libz(我还没有尝试构建它),你应该能够建立你的箱子:

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

,生成的二进制文件将位于target/x86_64-unknown-linux-musl/debug/<binary_name>

cross工具基本上可以做到这一点,但是在Docker容器中,以保持主机清洁。

此版本生成的二进制文件应该是静态链接的,甚至不依赖于glibc。这也意味着它会更大*并且如果在其中一个中发现安全问题,您将需要自己升级任何依赖项(尤其是OpenSSL)。

*您可能希望在已发布的二进制文件上使用strip