-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Cross-compiling or statically linking sqlx
fails because of OpenSSL
#670
Comments
For an immediate workaround, look to: |
I don't mind adding support for rustls. It's just non-trivial because of a limitation with Cargo. See: #575 (comment) |
There's a very promising fix proposed in sfackler/rust-openssl#1337 (comment). I'm going to try adding this to rust-musl-builder and see what happens. |
`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.
- `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. We remove a few `OPENSSL`-related variables that were added 4 years ago, and that no longer appear to be needed.
`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. We remove a few `OPENSSL`-related variables that were added 4 years ago, and that no longer appear to be needed.
I have updated
...as proposed at sfackler/rust-openssl#1337 (comment). This makes Thank you for your help figuring this out! I'm delighted to be able to build entirely static Linux binaries using |
Hello! Thank you for such an awesome Rust SQL client. We've been trying it out at work, and it looks super-promising. We'd love to use it to replace
diesel
andtokio-postgres
in some of our open source tools.However, many of our open source tools like
falconeri
anddbcrossbar
are shipped as statically-linked binaries using my rust-musl-builder. (rust-musl-builder
seems to be popular for other Rust projects, with something like 740,000 downloads. So if I can figure out how to makesqlx
work for me, that would hopefully help out some other people, too.)The problem: Compile-time OpenSSL connections cause linker headaches when cross-compiling
In order to produce a statically-linked binary using
sqlx
, we need to compile it twice, for two different environments:sqlx-macros
is compiled against GNU Libc, and Rust requires it to be a shared library that can be loaded as a "plugin" for the compiler.sqlx
is compiled againstmusl-libc
, which is a 100% static C library that will allow a Rust binary to work on any Linux distribution with a modern kernel. This is super-useful for distributing one Linux binary that works everywhere. In this case,sqlx
must be compiled as a static library, not a shared one.The problem occurs because:
sqlx-rt
always links against OpenSSL, in both the host and target environments.openssl-sys
library can be configured to link either statically or dynamically usingOPENSSL_STATIC
, but we're forced to choose one.Here are three proposed solutions. I'd be happy to submit a PR for any of them.
Proposed solution 1: Fix
openssl-sys
to support both static and dynamic versions of OpenSSL at the same timeThis is possible, but it might be complicated. The only reason to do this would be to link against OpenSSL in a macro while cross-compiling. But OpenSSL is native C code, and it makes everything related to cross-compiling excessively difficult.
Proposed solution 2: Modify
sqlx-macros
to have anoffline-only
featureI could submit a PR that modified
sqlx-macros
to support anoffline-only
feature that required an up-to-datesqlx-data.json
file, and that had no ability to connect to the database. This would only be useful when cross-compiling. This might be a relatively simple solution for people who can pass--features
tocargo build
when cross compiling, and who can set up appropriate features.Proposed solution 3: Add support for
rustls
tosqlx-rt
If solution (2) seems unappealing, another option would be to allow using
sqlx-rt
without any C dependencies at all. To do this, we could use a native Rust library likerustls
. I've used this in other Rust projects, and it seems to be well behaved and easy to set up.If we had support for
rustls
, cross-compiling would magically work, even for people who just have a stock Rust install. There would be no need for tools like rust-musl-builder.I'd also be happy to submit a PR for this approach.
Next steps
I'd be delighted to send you a PR + documentation. And I could add an
sqlx
-specific test torust-musl-builder
to make sure that every release ofrust-musl-builder
still supports cross-compiling withsqlx
. This would make it very easy for Rust developers to build static Rust binaries usingsqlx
.Once again, thank you very much for such an awesome Rust SQL interface! I really love the way it allows us to use natural SQL while still preserving the benefits of Rust's type checking.
The text was updated successfully, but these errors were encountered: