You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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:
7
6
8
7
```sh
9
8
alias rust-musl-builder='docker run --rm -it -v "$(pwd)":/home/rust/src ekidd/rust-musl-builder'
10
9
rust-musl-builder cargo build --release
11
10
```
12
11
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.
18
13
19
14
## Deploying your Rust application
20
15
21
16
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!
22
17
23
18
## How it works
24
19
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:
28
21
29
22
- The standard `musl-libc` libraries.
30
23
- 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).
32
25
-`libz`, which is needed by `libpq`.
33
26
34
27
This library also sets up the environment variables needed to compile popular Rust crates using these libraries.
@@ -46,14 +39,28 @@ fn main() {
46
39
}
47
40
```
48
41
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.
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
+
49
59
## Making static releases with Travis CI and GitHub
50
60
51
61
These instructions are inspired by [rust-cross][].
52
62
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:
57
64
58
65
```yaml
59
66
language: rust
@@ -78,13 +85,9 @@ deploy:
78
85
tags: true
79
86
```
80
87
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`.
83
89
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.
88
91
89
92
For a working example, see [faradayio/cage][cage].
90
93
@@ -94,49 +97,26 @@ For a working example, see [faradayio/cage][cage].
94
97
95
98
## Making tiny Docker images with Alpine Linux and Rust binaries
96
99
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
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).
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.
126
108
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.
130
110
131
111
## Development notes
132
112
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
135
114
136
115
## Other ways to build portable Rust binaries
137
116
138
117
- [messense/rust-musl-cross](https://github.com/messense/rust-musl-cross) shows how to build binaries for many different architectures.
139
118
- [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.
0 commit comments