Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cross compile a rust project with openssl?

My project is written in Rust (Ubuntu 16.04, CMake 3.5.1).

Previously it used grpcio = "0.4.0" as a dependency in Cargo.toml and I could successfully cross-compile (i.e., create a static binary) it (using cross). However, after I changed my dependency to grpcio = { version = "0.4.2", features = ["openssl"] } I can't cross-compile it anymore: it says that it couldn't compile grpcio-sys = "0.4.2" and displays a few CMake errors (and I can't continue to use 0.4.0 because it doesn't support features=["openssl"]):

  1. Couldn't find some enviroments flags for OpenSSL (even though I installed sudo apt-get install libssl-dev) for Ubuntu 16.04.
  2. gRPC_PROTOBUF_PROVIDER is "module" but PROTOBUF_ROOT_DIR is wrong.

How can I fix it? This post says I should avoid OpenSSL completely. And here's another post about cross compiling with OpenSSL.

like image 599
James Larkin Avatar asked Feb 19 '19 21:02

James Larkin


2 Answers

It took me a while to find, but I believe nowadays there's an easy option for OpenSSL cross-compilation - enabling the vendored feature.

This causes OpenSSL to be compiled from source as part of your project's build (and therefore against the same target architecture as the rest of the project) instead of expecting it to already be installed on your system.

You can propagate the feature into your own project to optionally depend on vendored by adding something like this to your Cargo.toml:

[features]
...

# If compiling on a system without OpenSSL installed, or cross-compiling for a different
# architecture, enable this feature to compile OpenSSL as part of the build.
# See https://docs.rs/openssl/#vendored for more.
static_ssl = ['openssl/vendored']

[dependencies]
...

[dependencies.openssl]
optional = true
version = ...

Enabling the static_ssl feature when building your project will then compile OpenSSL at the same time.

For grpcio in particular see their documentation, which indicates they provide an openssl-vendored feature for this purpose. So you can add grpcio = { version = "0.7", features = ["openssl-vendored"] } to your Cargo.toml to unconditionally compile OpenSSL, or use the same pattern as above (just don't mark the grpcio package as optional).

This post goes into some more details about different ways of compiling with OpenSSL.

like image 55
dimo414 Avatar answered Nov 14 '22 06:11

dimo414


I was searching for a similar problem when trying to compile to Turris Omnia router (armv71). I found a great docker image that solved my problem.

Take a look at: https://github.com/messense/rust-musl-cross.

like image 38
Michal Borek Avatar answered Nov 14 '22 06:11

Michal Borek