Skip to content

Commit 2b19e77

Browse files
committed
Add support for static linking of sqlx
`sqlx` requires linking against OpenSSL twice: - `sqlx-macros` needs to link against OpenSSL as a shared libary for use at compile time. - `sqlx` needs to link against OpenSSL a static library for use at runtime. You can find the details here. launchbadge/sqlx#670 sfackler/rust-openssl#1337 We go with the fix proposed by sfackler, and add a test program that we can use to verify everything works correctly.
1 parent 9f6efa5 commit 2b19e77

File tree

13 files changed

+1386
-11
lines changed

13 files changed

+1386
-11
lines changed

Dockerfile

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ RUN apt-get update && \
6767
curl -fLO https://github.com/EmbarkStudios/cargo-deny/releases/download/$CARGO_DENY_VERSION/cargo-deny-$CARGO_DENY_VERSION-x86_64-unknown-linux-musl.tar.gz && \
6868
tar xf cargo-deny-$CARGO_DENY_VERSION-x86_64-unknown-linux-musl.tar.gz && \
6969
mv cargo-deny-$CARGO_DENY_VERSION-x86_64-unknown-linux-musl/cargo-deny /usr/local/bin/ && \
70-
rm -rf cargo-deny-$CARGO_DENY_VERSION-x86_64-unknown-linux-musl cargo-deny-$CARGO_DENY_VERSION-x86_64-unknown-linux-musl.tar.gz
70+
rm -rf cargo-deny-$CARGO_DENY_VERSION-x86_64-unknown-linux-musl cargo-deny-$CARGO_DENY_VERSION-x86_64-unknown-linux-musl.tar.gz
7171

7272
# Static linking for C++ code
7373
RUN sudo ln -s "/usr/bin/g++" "/usr/bin/musl-g++"
@@ -142,11 +142,8 @@ RUN echo "Building libpq" && \
142142
cd ../../bin/pg_config && make && sudo make install && \
143143
rm -r /tmp/*
144144

145-
ENV OPENSSL_DIR=/usr/local/musl/ \
146-
OPENSSL_INCLUDE_DIR=/usr/local/musl/include/ \
147-
DEP_OPENSSL_INCLUDE=/usr/local/musl/include/ \
148-
OPENSSL_LIB_DIR=/usr/local/musl/lib/ \
149-
OPENSSL_STATIC=1 \
145+
ENV X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_DIR=/usr/local/musl/ \
146+
X86_64_UNKNOWN_LINUX_MUSL_OPENSSL_STATIC=1 \
150147
PQ_LIB_STATIC_X86_64_UNKNOWN_LINUX_MUSL=1 \
151148
PG_CONFIG_X86_64_UNKNOWN_LINUX_GNU=/usr/bin/pg_config \
152149
PKG_CONFIG_ALLOW_CROSS=true \

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
[![Docker Image](https://img.shields.io/docker/pulls/ekidd/rust-musl-builder.svg?maxAge=2592000)](https://hub.docker.com/r/ekidd/rust-musl-builder/)
44

5+
[Source on GitHub](https://github.com/emk/rust-musl-builder)
6+
57
## What is this?
68

79
Do you want to compile a completely static Rust binary with no external dependencies? If so, try:
@@ -13,7 +15,7 @@ rust-musl-builder cargo build --release
1315

1416
This command assumes that `$(pwd)` is readable and writable by uid 1000, gid 1000. At the moment, it doesn't attempt to cache libraries between builds, so this is best reserved for making final release builds.
1517

16-
For a more realistic example, see the `Dockerfile` for [examples/using-diesel](./examples/using-diesel).
18+
For a more realistic example, see the `Dockerfile` for [examples/using-diesel](./examples/using-diesel) [examples/using-sqlx](./examples/using-sqlx).
1719

1820
## Deploying your Rust application
1921

@@ -36,7 +38,7 @@ In general, we provide the following tagged Docker images:
3638
Rust, please file an issue.
3739

3840
At a minimum, each of these images should be able to
39-
compile [examples/using-diesel](./examples/using-diesel).
41+
compile [examples/using-diesel](./examples/using-diesel) and [examples/using-sqlx](./examples/using-sqlx).
4042

4143
[comp]: https://rust-lang.github.io/rustup-components-history/index.html
4244

@@ -94,8 +96,6 @@ This image also supports the following extra goodies:
9496
If your application uses OpenSSL, you will also need to take a few extra steps to make sure that it can find OpenSSL's list of trusted certificates, which is stored in different locations on different Linux distributions. You can do this using [`openssl-probe`](https://crates.io/crates/openssl-probe) as follows:
9597

9698
```rust
97-
extern crate openssl_probe;
98-
9999
fn main() {
100100
openssl_probe::init_ssl_cert_env_vars();
101101
//... your code

examples/using-diesel/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ diesel = { version = "1", features = ["postgres", "sqlite"] }
99
libsqlite3-sys = { version = "*", features = ["bundled"] }
1010
# Needed for Postgres.
1111
openssl = "*"
12+
openssl-probe = "0.1.2"

examples/using-diesel/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ where
4646
}
4747

4848
fn main() {
49+
openssl_probe::init_ssl_cert_env_vars();
50+
4951
println!("Hello, world!");
5052

5153
// Only run our database example if we have a database. Otherwise, we just

examples/using-sqlx/.dockerignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target

0 commit comments

Comments
 (0)