Skip to content

Commit fcc6921

Browse files
committed
Add working example and test case for diesel
Fixes #27, or at least works around it. This deals with the issues mentioned in sgrif/pq-sys#18, and it relies heavily on ideas from @golddranks and @clux.
1 parent 4ab9c34 commit fcc6921

File tree

12 files changed

+354
-66
lines changed

12 files changed

+354
-66
lines changed

.gitignore

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

Dockerfile

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ RUN apt-get update && \
1919
curl \
2020
file \
2121
git \
22+
musl-dev \
2223
musl-tools \
24+
libpq-dev \
25+
libssl-dev \
26+
pkgconf \
2327
sudo \
2428
xutils-dev \
2529
&& \
@@ -58,7 +62,7 @@ RUN echo "Building OpenSSL" && \
5862
VERS=1.0.2l && \
5963
curl -O https://www.openssl.org/source/openssl-$VERS.tar.gz && \
6064
tar xvzf openssl-$VERS.tar.gz && cd openssl-$VERS && \
61-
env CC=musl-gcc ./config --prefix=/usr/local/musl && \
65+
env CC=musl-gcc ./Configure no-shared no-zlib -fPIC --prefix=/usr/local/musl linux-x86_64 && \
6266
env C_INCLUDE_PATH=/usr/local/musl/include/ make depend && \
6367
make && sudo make install && \
6468
cd .. && rm -rf openssl-$VERS.tar.gz openssl-$VERS && \
@@ -76,17 +80,19 @@ RUN echo "Building OpenSSL" && \
7680
tar xzf postgres.tar.gz && \
7781
cd postgresql-$VERS && \
7882
CC=musl-gcc CPPFLAGS=-I/usr/local/musl/include LDFLAGS=-L/usr/local/musl/lib ./configure --with-openssl --without-readline --prefix=/usr/local/musl && \
79-
cd src/interfaces/libpq && \
80-
make all-static-lib && sudo make install-lib-static && \
81-
cd ../../../.. && rm -rf postgres.tar.gz postgresql-$VERS
83+
cd src/interfaces/libpq && make all-static-lib && sudo make install-lib-static && cd ../../.. && \
84+
cd src/bin/pg_config && make && sudo make install && cd ../.. && \
85+
cd .. && rm -rf postgres.tar.gz postgresql-$VERS
8286

8387
ENV OPENSSL_DIR=/usr/local/musl/ \
8488
OPENSSL_INCLUDE_DIR=/usr/local/musl/include/ \
8589
DEP_OPENSSL_INCLUDE=/usr/local/musl/include/ \
8690
OPENSSL_LIB_DIR=/usr/local/musl/lib/ \
8791
OPENSSL_STATIC=1 \
88-
PQ_LIB_DIR=/usr/local/musl/lib \
89-
PQ_LIB_STATIC=1
92+
PQ_LIB_STATIC_X86_64_UNKNOWN_LINUX_MUSL=1 \
93+
PG_CONFIG_X86_64_UNKNOWN_LINUX_GNU=/usr/bin/pg_config \
94+
PKG_CONFIG_ALLOW_CROSS=true \
95+
PKG_CONFIG_ALL_STATIC=true
9096

9197
# (Please feel free to submit pull requests for musl-libc builds of other C
9298
# libraries needed by the most popular and common Rust crates, to avoid

README.md

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,26 @@
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-
Do you want to compile a completely static Rust binary with no external
6-
dependencies? If so, try:
5+
Do you want to compile a completely static Rust binary with no external dependencies? If so, try:
76

87
```sh
98
alias rust-musl-builder='docker run --rm -it -v "$(pwd)":/home/rust/src ekidd/rust-musl-builder'
109
rust-musl-builder cargo build --release
1110
```
1211

13-
This command assumes that `$(pwd)` is readable and writable by uid 1000,
14-
gid 1000. It will output binaries in
15-
`target/x86_64-unknown-linux-musl/release`. At the moment, it doesn't
16-
attempt to cache libraries between builds, so this is best reserved for
17-
making final release builds.
12+
This command assumes that `$(pwd)` is readable and writable by uid 1000, gid 1000. It will output binaries in `target/x86_64-unknown-linux-musl/release`. At the moment, it doesn't attempt to cache libraries between builds, so this is best reserved for making final release builds.
1813

1914
## Deploying your Rust application
2015

2116
With a bit of luck, you should be able to just copy your application binary from `target/x86_64-unknown-linux-musl/release`, and install it directly on any reasonably modern x86_64 Linux machine. In particular, you should be able make static release binaries using TravisCI and GitHub, or you can copy your Rust application into an [Alpine Linux container][]. See below for details!
2217

2318
## How it works
2419

25-
`rust-musl-builder` uses [musl-libc][], [musl-gcc][], and the new
26-
[rustup][] `target` support. It includes static versions of several
27-
libraries:
20+
`rust-musl-builder` uses [musl-libc][], [musl-gcc][], and the new [rustup][] `target` support. It includes static versions of several libraries:
2821

2922
- The standard `musl-libc` libraries.
3023
- OpenSSL, which is needed by many Rust applications.
31-
- `libpq`, which is needed for applications that use `diesel` with PostgreSQL.
24+
- `libpq`, which is needed for applications that use `diesel` with PostgreSQL. Note that this may be broken under Rust 1.21.0 and later (see https://github.com/emk/rust-musl-builder/issues/27).
3225
- `libz`, which is needed by `libpq`.
3326

3427
This library also sets up the environment variables needed to compile popular Rust crates using these libraries.
@@ -46,14 +39,28 @@ fn main() {
4639
}
4740
```
4841

42+
## Making Diesel work
43+
44+
In addition to setting up OpenSSL, you'll need to add the following lines to your `Cargo.toml`:
45+
46+
```toml
47+
[dependencies]
48+
# This is needed to make sure that Cargo statically links against
49+
# `libssl`. This should happen automatically, but it doesn't.
50+
openssl-sys = "0.9"
51+
52+
[patch.crates-io]
53+
# This is needed to handle cross-compilation of libpq.
54+
pq-sys = { git = 'https://github.com/golddranks/pq-sys' }
55+
```
56+
57+
See [this PR](https://github.com/sgrif/pq-sys/pull/18) for a discussion of the issues involved in cross-compiling `diesel` and `diesel_codegen`.
58+
4959
## Making static releases with Travis CI and GitHub
5060

5161
These instructions are inspired by [rust-cross][].
5262

53-
First, read the [Travis CI: GitHub Releases Uploading][uploading] page, and
54-
run `travis setup releases` as instructed. Then add the following lines to
55-
your existing `.travis.yml` file, replacing `myapp` with the name of your
56-
package:
63+
First, read the [Travis CI: GitHub Releases Uploading][uploading] page, and run `travis setup releases` as instructed. Then add the following lines to your existing `.travis.yml` file, replacing `myapp` with the name of your package:
5764

5865
```yaml
5966
language: rust
@@ -78,13 +85,9 @@ deploy:
7885
tags: true
7986
```
8087
81-
Next, copy [`build-release`](./examples/build-release) into your project
82-
and run `chmod +x build-release`.
88+
Next, copy [`build-release`](./examples/build-release) into your project and run `chmod +x build-release`.
8389

84-
When you push a new tag to your project, `build-release` will automatically
85-
build new Linux binaries using `rust-musl-builder`, and new Mac binaries
86-
with Cargo, and it will upload both to the GitHub releases page for your
87-
repository.
90+
When you push a new tag to your project, `build-release` will automatically build new Linux binaries using `rust-musl-builder`, and new Mac binaries with Cargo, and it will upload both to the GitHub releases page for your repository.
8891

8992
For a working example, see [faradayio/cage][cage].
9093

@@ -94,49 +97,26 @@ For a working example, see [faradayio/cage][cage].
9497

9598
## Making tiny Docker images with Alpine Linux and Rust binaries
9699

97-
Docker now supports [multistage builds][multistage], which make it easy to
98-
build your Rust application with `rust-musl-builder` and deploy it using
99-
[Alpine Linux][]. For a working example, see
100-
[`Dockerfile.multistage-example`](./Dockerfile.multistage-example).
100+
Docker now supports [multistage builds][multistage], which make it easy to build your Rust application with `rust-musl-builder` and deploy it using [Alpine Linux][]. For a working example, see [`examples/using-diesel/Dockerfile`](./examples/using-diesel/Dockerfile).
101101

102102
[multistage]: https://docs.docker.com/engine/userguide/eng-image/multistage-build/
103103
[Alpine Linux]: https://alpinelinux.org/
104104

105105
## Adding more C libraries
106106

107-
If you're using Docker crates which require specific C libraries to be
108-
installed, you can create a `Dockerfile` based on this one, and use
109-
`musl-gcc` to compile the libraries you need. For example:
110-
111-
```Dockerfile
112-
FROM ekidd/rust-musl-builder
113-
114-
# EXAMPLE ONLY! libz is already included.
115-
RUN VERS=1.2.11 && \
116-
cd /home/rust/libs && \
117-
curl -LO http://zlib.net/zlib-$VERS.tar.gz && \
118-
tar xzf zlib-$VERS.tar.gz && cd zlib-$VERS && \
119-
CC=musl-gcc ./configure --static --prefix=/usr/local/musl && \
120-
make && sudo make install && \
121-
cd .. && rm -rf zlib-$VERS.tar.gz zlib-$VERS
122-
```
123-
124-
This usually involves a bit of experimentation for each new library, but it
125-
seems to work well for most simple, standalone libraries.
107+
If you're using Docker crates which require specific C libraries to be installed, you can create a `Dockerfile` based on this one, and use `musl-gcc` to compile the libraries you need. For an example, see [`examples/adding-a-library/Dockerfile`](./examples/adding-a-library/Dockerfile). This usually involves a bit of experimentation for each new library, but it seems to work well for most simple, standalone libraries.
126108

127-
If you need an especially common library, please feel free to submit a pull
128-
request adding it to the main `Dockerfile`! We'd like to support popular
129-
Rust crates out of the box.
109+
If you need an especially common library, please feel free to submit a pull request adding it to the main `Dockerfile`! We'd like to support popular Rust crates out of the box.
130110

131111
## Development notes
132112

133-
After modifying the image, run `./test-image` to make sure that everything
134-
works.
113+
After modifying the image, run `./test-image` to make sure that everything works.xs
135114

136115
## Other ways to build portable Rust binaries
137116

138117
- [messense/rust-musl-cross](https://github.com/messense/rust-musl-cross) shows how to build binaries for many different architectures.
139118
- [japaric/rust-cross](https://github.com/japaric/rust-cross) has extensive instructions on how to cross-compile Rust applications.
119+
- [clux/muslrust](https://github.com/clux/muslrust) also supports libcurl.
140120

141121
## License
142122

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
target
File renamed without changes.

examples/using-diesel/.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)