cross-rs / cross

“Zero setup” cross compilation and “cross testing” of Rust crates
Apache License 2.0
6.4k stars 359 forks source link

unable to cross-compile for mips-unknown-linux-musl, anything that uses openssl (e.g. http client, mqtt-client) #1382

Closed ramabu closed 7 months ago

ramabu commented 8 months ago

Checklist

Describe your issue

Trying to compile and run https://www.emqx.com/en/blog/how-to-use-mqtt-in-rust for mips-unknown-linux-musl (compiling and running local/native works fine).

I am guessing that my toolchain is missing something - but cannot seem to find directions/viable ways to fix the gap.

What target(s) are you cross-compiling for?

mips-unknown-linux-musl

Which operating system is the host (e.g computer cross is on) running?

What architecture is the host?

What container engine is cross using?

cross version

cross 0.2.5 (cargo 1.74.0 (ecb9851af 2023-10-18)

Example

cross build --release --target mips-unknown-linux-musl Compiling libc v0.2.150 Compiling proc-macro2 v1.0.70 Compiling unicode-ident v1.0.12 Compiling futures-core v0.3.29 Compiling autocfg v1.1.0 Compiling pkg-config v0.3.27 Compiling vcpkg v0.2.15 Compiling futures-channel v0.3.29 Compiling crossbeam-utils v0.8.16 Compiling futures-task v0.3.29 Compiling cfg-if v1.0.0 Compiling futures-util v0.3.29 Compiling futures-sink v0.3.29 Compiling pin-utils v0.1.0 Compiling memchr v2.6.4 Compiling futures-io v0.3.29 Compiling pin-project-lite v0.2.13 Compiling thiserror v1.0.50 Compiling event-listener v2.5.3 Compiling log v0.4.20 Compiling futures-timer v3.0.2 Compiling slab v0.4.9 Compiling concurrent-queue v2.4.0 Compiling crossbeam-channel v0.5.8 Compiling quote v1.0.33 Compiling async-channel v1.9.0 Compiling syn v2.0.39 Compiling cc v1.0.83 Compiling cmake v0.1.50 Compiling openssl-sys v0.9.97 Compiling paho-mqtt-sys v0.9.0 (https://github.com/eclipse/paho.mqtt.rust.git?branch=master#cf953b93) error: failed to run custom build command for openssl-sys v0.9.97

Caused by: process didn't exit successfully: /target/release/build/openssl-sys-0ecd353b0c5734ba/build-script-main (exit status: 101) --- stdout cargo:rerun-if-env-changed=MIPS_UNKNOWN_LINUX_MUSL_OPENSSL_LIB_DIR MIPS_UNKNOWN_LINUX_MUSL_OPENSSL_LIB_DIR unset cargo:rerun-if-env-changed=OPENSSL_LIB_DIR OPENSSL_LIB_DIR unset cargo:rerun-if-env-changed=MIPS_UNKNOWN_LINUX_MUSL_OPENSSL_INCLUDE_DIR MIPS_UNKNOWN_LINUX_MUSL_OPENSSL_INCLUDE_DIR unset cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR OPENSSL_INCLUDE_DIR unset cargo:rerun-if-env-changed=MIPS_UNKNOWN_LINUX_MUSL_OPENSSL_DIR MIPS_UNKNOWN_LINUX_MUSL_OPENSSL_DIR unset cargo:rerun-if-env-changed=OPENSSL_DIR OPENSSL_DIR unset cargo:rerun-if-env-changed=OPENSSL_NO_PKG_CONFIG cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_mips-unknown-linux-musl cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_mips_unknown_linux_musl cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_ALLOW_CROSS cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS cargo:rerun-if-env-changed=PKG_CONFIG_mips-unknown-linux-musl cargo:rerun-if-env-changed=PKG_CONFIG_mips_unknown_linux_musl cargo:rerun-if-env-changed=TARGET_PKG_CONFIG cargo:rerun-if-env-changed=PKG_CONFIG cargo:rerun-if-env-changed=OPENSSL_STATIC cargo:rerun-if-env-changed=OPENSSL_DYNAMIC cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC cargo:rerun-if-env-changed=PKG_CONFIG_PATH_mips-unknown-linux-musl cargo:rerun-if-env-changed=PKG_CONFIG_PATH_mips_unknown_linux_musl cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_PATH cargo:rerun-if-env-changed=PKG_CONFIG_PATH cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_mips-unknown-linux-musl cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_mips_unknown_linux_musl cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_LIBDIR cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_mips-unknown-linux-musl cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_mips_unknown_linux_musl cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_SYSROOT_DIR cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR run pkg_config fail: PKG_CONFIG_ALLOW_SYSTEM_CFLAGS="1" "pkg-config" "--libs" "--cflags" "openssl" did not exit successfully: exit status: 1 error: could not find system library 'openssl' required by the 'openssl-sys' crate

--- stderr Package openssl was not found in the pkg-config search path. Perhaps you should add the directory containing `openssl.pc' to the PKG_CONFIG_PATH environment variable No package 'openssl' found

--- stderr thread 'main' panicked at /cargo/registry/src/index.crates.io-6f17d22bba15001f/openssl-sys-0.9.97/build/find_normal.rs:190:5:

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 = mips-unknown-linux-musl openssl-sys = 0.9.97

note: run with RUST_BACKTRACE=1 environment variable to display a backtrace warning: build failed, waiting for other jobs to finish...

Additional information / notes

No response

Emilgardis commented 8 months ago

we don't have openssl installed in our images, you'll have to install it yourself or use vendored

You can maybe use system packages using debian (ubuntu doesn't distribute mips anymore).

I'd recommend you to use the vendored feature

ramabu commented 8 months ago

specifically for mips-unknown-linux-musl, I had to download openssl, compile it, and set OPENSSL_LIB_DIR/OPENSSL_INCLUDE_DIR properly for this to happen, as I could not apt install openssl-devel nor libssl-dev (not found)

I basically followed the instructions in the first answer here, changed as follows:

` cd /tmp

apt update apt install wget

wget https://www.openssl.org/source/openssl-1.1.1t.tar.gz tar xzf openssl-1.1.1t.tar.gz export MACHINE=mips export ARCH=mips export CC=mips-unknown-linux-musl-gcc cd openssl-1.1.1t && ./config && make

cd $PROJECT_DIR

export OPENSSL_LIB_DIR=/tmp/openssl-1.1.1t/ export OPENSSL_INCLUDE_DIR=/tmp/openssl-1.1.1t/include

cargo build --target mips-unknown-linux-musl --release `

ramabu commented 7 months ago

Pasting the Dockerfile I used for a custom cross image

FROM  ghcr.io/cross-rs/mips-unknown-linux-musl:latest
## Base rust 'cross' image for compiling on mips-unknown-linux-musl (gl-inet)
ENV OPENSSL_V="1.1.1t"
ENV OPENSSL_LIB_DIR=/tmp/openssl-${OPENSSL_V}
ENV OPENSSL_INCLUDE_DIR=/tmp/openssl-${OPENSSL_V}/include

RUN set -o errexit \
    && apt-get update \
    && apt install --yes wget clang g++-multilib \
    && cd /tmp \
    && wget https://www.openssl.org/source/openssl-${OPENSSL_V}.tar.gz \
    && tar xzf openssl-${OPENSSL_V}.tar.gz \
    && export MACHINE=mips \
    && export ARCH=mips-linux-muslsf \
    && export CC=${ARCH}-gcc \
    && cd /tmp/openssl-${OPENSSL_V} \
    && ./config \
    && make

RUN export OPENSSL_V="1.1.1t" \
    && export OPENSSL_LIB_DIR=/tmp/openssl-${OPENSSL_V} \
    && export OPENSSL_DIR=/tmp/openssl-${OPENSSL_V} \
    && export OPENSSL_INCLUDE_DIR=/tmp/openssl-${OPENSSL_V}/include
ramabu commented 7 months ago

Got this working and attached a working description