Skip to content

Instantly share code, notes, and snippets.

@marirs
Last active April 4, 2024 05:04
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save marirs/f40ab48749ac17ace9b9e850e0bd5bf6 to your computer and use it in GitHub Desktop.
Save marirs/f40ab48749ac17ace9b9e850e0bd5bf6 to your computer and use it in GitHub Desktop.
Rust OpenSSL Cross Compile for Linux on Mac M1
# Install the toolchain
```bash
brew tap SergioBenitez/osxct
brew install x86_64-unknown-linux-gnu
```
# this should get installed in:
# /opt/homebrew/Cellar/x86_64-unknown-linux-gnu/
# Installing the macOS OpenSSL - if not already installed
```bash
brew install openssl@1.1
```
# this should get installed in:
# /opt/homebrew/Cellar/openssl\@1.1/
# Download the OpenSSL Source
```bash
cd /tmp
wget https://www.openssl.org/source/old/1.1.1/openssl-1.1.1w.tar.gz
tar xvf openssl-1.1.1w.tar.gz
cd openssl-1.1.1w
```
# Configure for `Intel x86_64`
```bash
CC="x86_64-unknown-linux-gnu-gcc -fPIE -pie" \
CXX="x86_64-unknown-linux-gnu-g++" \
AS="x86_64-unknown-linux-gnu-as" \
AR="x86_64-unknown-linux-gnu-ar" \
NM="x86_64-unknown-linux-gnu-nm" \
RANLIB="x86_64-unknown-linux-gnu-gcc-ranlib" \
LD="x86_64-unknown-linux-gnu-ld" \
STRIP="x86_64-unknown-linux-gnu-strip" \
./Configure linux-x86_64 no-shared no-async --prefix=/opt/homebrew/Cellar/openssl\@1.1/1.1.1w/x86_64-linux-gnu --openssldir=/opt/homebrew/Cellar/openssl\@1.1/1.1.1w/x86_64-linux-gnu
```
# Configure for `aarch64`
```bash
CC="aarch64-unknown-linux-gnu-gcc -fPIE -pie" \
CXX="aarch64-unknown-linux-gnu-g++" \
AS="aarch64-unknown-linux-gnu-as" \
AR="aarch64-unknown-linux-gnu-ar" \
NM="aarch64-unknown-linux-gnu-nm" \
RANLIB="aarch64-unknown-linux-gnu-gcc-ranlib" \
LD="aarch64-unknown-linux-gnu-ld" \
STRIP="aarch64-unknown-linux-gnu-strip" \
./Configure linux-aarch64 no-shared no-async --prefix=/opt/homebrew/Cellar/openssl\@1.1/1.1.1w/aarch64-linux-gnu --openssldir=/opt/homebrew/Cellar/openssl\@1.1/1.1.1w/aarch64-linux-gnu
```
# Make
```bash
make -j12
```
# Install
```bash
make install
```
# Now you should have a linux x86_64 version of openssl installed in:
# /opt/homebrew/Cellar/openssl\@1.1/1.1.1n/x86_64-linux-gnu
# OR for aarch 64
# /opt/homebrew/Cellar/openssl\@1.1/1.1.1n/aarch64-linux-gnu
### Rust Cross compile in Mac for linux
# Now if you want to cross compile for linux on your mac m1 using rust with openssl or openss-sys
# Cargo.toml - as much as possible try to use "vendored" feature in openssl - that way the openssl will not become
# a dependancy on the target system
```bash
OPENSSL_INCLUDE_DIR=`brew --prefix openssl@1.1`/x86_64-linux-gnu/include OPENSSL_LIB_DIR=`brew --prefix openssl@1.1`/x86_64-linux-gnu/lib OPENSSL_STATIC=1 cargo b --release --target x86_64-unknown-linux-gnu
```
# for aarch64
```bash
OPENSSL_INCLUDE_DIR=`brew --prefix openssl@1.1`/aarch64-linux-gnu/include OPENSSL_LIB_DIR=`brew --prefix openssl@1.1`/aarch64-linux-gnu/lib OPENSSL_STATIC=1 cargo b --release --target aarch64-unknown-linux-gnu
```
# if you want to strip the executable:
```bash
x86_64-unknown-linux-gnu-strip target/release/<file>
```
# for aarch64
```bash
aarch64-unknown-linux-gnu-strip target/release/<file>
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment