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

"Could not find directory of OpenSSL installation" in a docker build #1684

Closed
Teajey opened this issue Sep 10, 2022 · 2 comments
Closed

"Could not find directory of OpenSSL installation" in a docker build #1684

Teajey opened this issue Sep 10, 2022 · 2 comments

Comments

@Teajey
Copy link

Teajey commented Sep 10, 2022

I'm trying to build a binary for AWS Lambda here and have been stuck trying to get openssl-sys to build for a long time. I'm getting the error "Could not find directory of OpenSSL installation" even though I have libssl-dev installed, and even I tried throwing openssl in there for good measure.

I've tried exploring the resulting file system of the docker container to see where I could point OPENSSL_DIR, but I can't find anything containing an include and a lib directory like the build asks for.

OpenSSL is just being installed here in a very standard way, I don't see why it can't be found.

Thanks!


My Dockerfile:

FROM rust:1.63.0

RUN apt update
RUN apt install -y musl-tools pkg-config libssl-dev openssl

RUN rustup override set nightly-2022-08-09

RUN rustup target add x86_64-unknown-linux-musl

WORKDIR /build

COPY Cargo.lock .
COPY Cargo.toml .
RUN mkdir src
RUN echo "fn main() {}" > src/main.rs

RUN cargo build --release --target x86_64-unknown-linux-musl

My Cargo.toml:

[package]
name = "rust-dbucket"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
aws-config = "0.47.0"
aws-sdk-dynamodb = "0.17.0"
aws-sdk-secretsmanager = "0.17.0"
aws-smithy-client = { version = "0.47.0", features = ["test-util"] }
aws-smithy-http = "0.47.0"
base64 = "0.13.0"
chrono = "0.4.22"
color-eyre = "0.6.2"
derive_more = "0.99.17"
enum-iterator = "1.2.0"
futures = "0.3.24"
insta = "1.19.1"
itertools = "0.10.3"
juniper = "0.15.10"
juniper_rocket = "0.8.2"
lambda-web = { version = "0.2.0", features = ["rocket05"] }
reqwest = { version = "0.11.11", features = ["json"] }
rocket = "0.5.0-rc.2"
# Change back to crates.io version when https://github.com/lawliet89/rocket_cors/pull/108 is published
rocket_cors = { git = "https://github.com/lawliet89/rocket_cors.git", rev = "54fae070"  }
roxmltree = "0.15.0"
sentry = "0.27.0"
serde = { version = "1.0.144", features = ["derive"] }
serde_dynamo = { version = "4.0.6", features = ["aws-sdk-dynamodb+0_17"] }
serde_json = "1.0.85"
tokio = "1.20.1"

The error that I'm seeing:

#16 41.19 error: failed to run custom build command for `openssl-sys v0.9.75`
#16 41.19 
#16 41.19 Caused by:
#16 41.19   process didn't exit successfully: `/build/target/release/build/openssl-sys-bae4e2ea767ca796/build-script-main` (exit status: 101)
#16 41.19   --- stdout
#16 41.19   cargo:rustc-cfg=const_fn
#16 41.19   cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_LIB_DIR
#16 41.19   X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_LIB_DIR unset
#16 41.19   cargo:rerun-if-env-changed=OPENSSL_LIB_DIR
#16 41.19   OPENSSL_LIB_DIR unset
#16 41.19   cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_INCLUDE_DIR
#16 41.19   X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_INCLUDE_DIR unset
#16 41.19   cargo:rerun-if-env-changed=OPENSSL_INCLUDE_DIR
#16 41.19   OPENSSL_INCLUDE_DIR unset
#16 41.19   cargo:rerun-if-env-changed=X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_DIR
#16 41.19   X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_DIR unset
#16 41.19   cargo:rerun-if-env-changed=OPENSSL_DIR
#16 41.19   OPENSSL_DIR unset
#16 41.19   cargo:rerun-if-env-changed=OPENSSL_NO_PKG_CONFIG
#16 41.19   cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_x86_64-unknown-linux-musl
#16 41.19   cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS_x86_64_unknown_linux_musl
#16 41.19   cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_ALLOW_CROSS
#16 41.19   cargo:rerun-if-env-changed=PKG_CONFIG_ALLOW_CROSS
#16 41.19   cargo:rerun-if-env-changed=PKG_CONFIG_x86_64-unknown-linux-musl
#16 41.19   cargo:rerun-if-env-changed=PKG_CONFIG_x86_64_unknown_linux_musl
#16 41.19   cargo:rerun-if-env-changed=TARGET_PKG_CONFIG
#16 41.19   cargo:rerun-if-env-changed=PKG_CONFIG
#16 41.19   cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-musl
#16 41.19   cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_musl
#16 41.19   cargo:rerun-if-env-changed=TARGET_PKG_CONFIG_SYSROOT_DIR
#16 41.19   cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
#16 41.19   run pkg_config fail: "pkg-config has not been configured to support cross-compilation.\n\nInstall a sysroot for the target platform and configure it via\nPKG_CONFIG_SYSROOT_DIR and PKG_CONFIG_PATH, or install a\ncross-compiling wrapper for pkg-config and set it via\nPKG_CONFIG environment variable."
#16 41.19 
#16 41.19   --- stderr
#16 41.19   thread 'main' panicked at '
#16 41.19 
#16 41.19   Could not find directory of OpenSSL installation, and this `-sys` crate cannot
#16 41.19   proceed without this knowledge. If OpenSSL is installed and this crate had
#16 41.19   trouble finding it,  you can set the `OPENSSL_DIR` environment variable for the
#16 41.19   compilation process.
#16 41.19 
#16 41.19   Make sure you also have the development packages of openssl installed.
#16 41.19   For example, `libssl-dev` on Ubuntu or `openssl-devel` on Fedora.
#16 41.19 
#16 41.19   If you're in a situation where you think the directory *should* be found
#16 41.19   automatically, please open a bug at https://github.com/sfackler/rust-openssl
#16 41.19   and include information about your system as well as this message.
#16 41.19 
#16 41.19   $HOST = x86_64-unknown-linux-gnu
#16 41.19   $TARGET = x86_64-unknown-linux-musl
#16 41.19   openssl-sys = 0.9.75
#16 41.19 
#16 41.19   ', /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/openssl-sys-0.9.75/build/find_normal.rs:191:5
#16 41.19   note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
#16 41.19 warning: build failed, waiting for other jobs to finish...
------
executor failed running [/bin/sh -c cargo build --release --target x86_64-unknown-linux-musl]: exit code: 101
@Teajey
Copy link
Author

Teajey commented Sep 10, 2022

Thanks to this article I've got it working. I had to build OpenSSL for musl. I hate to say that I've kind of blindly copied the build script without really understanding it but there ya go.

Here's what my Dockerfile looks like now:

FROM rust:1.63.0

RUN apt update
RUN apt install -y musl-tools

# ------------------------------- Build OpenSSL for the `musl` build target
RUN \
  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

WORKDIR /musl

RUN wget https://github.com/openssl/openssl/archive/OpenSSL_1_1_1f.tar.gz
RUN tar zxvf OpenSSL_1_1_1f.tar.gz 
WORKDIR /musl/openssl-OpenSSL_1_1_1f/

RUN CC="musl-gcc -fPIE -pie" ./Configure no-shared no-async --prefix=/musl --openssldir=/musl/ssl linux-x86_64
RUN make depend
RUN make -j$(nproc)
RUN make install
# -------------------------------

WORKDIR /build

RUN rustup override set nightly-2022-08-09

RUN rustup target add x86_64-unknown-linux-musl

COPY Cargo.lock .
COPY Cargo.toml .
RUN mkdir src
RUN echo "fn main() {}" > src/main.rs

ENV OPENSSL_DIR=/musl

RUN cargo build --release --target x86_64-unknown-linux-musl

@Teajey Teajey closed this as completed Sep 10, 2022
@polarathene
Copy link

polarathene commented Feb 22, 2024

While the issue is old, this is for anyone else that lands here 😅


FWIW, since you're using Docker anyway you could just use the alpine version of the Rust image. You don't need to build openssl for musl as above that way :)

run pkg_config fail: "pkg-config has not been configured to support cross-compilation.\n\nInstall a sysroot for the target platform and configure it via\nPKG_CONFIG_SYSROOT_DIR and PKG_CONFIG_PATH, or install a\ncross-compiling wrapper for pkg-config and set it via\nPKG_CONFIG environment variable."

That error might be relevant, along with your cross compiling if you don't actually need it.

Since you're using the same arch + platform and just want to target -musl instead of -gnu for a static build, perhaps look at the advice in this issue: #1627 (comment)


UPDATE: In the linked issue I direct you to, I additionally document the vendored feature for the openssl crate, that'll build from source provided you have the required dependencies to do so successfully. I provided several examples that should help (including cross compiling to -musl target from a glibc build host like you were trying)👍

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