Skip to content

Commit 0979db1

Browse files
committed
Add Rustler Precompiled
1 parent 799dd95 commit 0979db1

4 files changed

Lines changed: 166 additions & 3 deletions

File tree

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
name: Build precompiled NIFs
2+
3+
env:
4+
NIF_DIRECTORY: "native/excoding"
5+
6+
on:
7+
push:
8+
branches:
9+
- main
10+
tags:
11+
- '*'
12+
13+
defaults:
14+
run:
15+
# Sets the working dir for "run" scripts.
16+
# Note that this won't change the directory for actions (tasks with "uses").
17+
working-directory: "./native/excoding"
18+
19+
jobs:
20+
build_release:
21+
name: NIF ${{ matrix.nif }} - ${{ matrix.job.target }} (${{ matrix.job.os }})
22+
runs-on: ${{ matrix.job.os }}
23+
strategy:
24+
fail-fast: false
25+
matrix:
26+
nif: ["2.16", "2.15", "2.14"]
27+
job:
28+
- { target: arm-unknown-linux-gnueabihf , os: ubuntu-20.04 , use-cross: true }
29+
- { target: aarch64-unknown-linux-gnu , os: ubuntu-20.04 , use-cross: true }
30+
- { target: aarch64-apple-darwin , os: macos-11 }
31+
- { target: x86_64-apple-darwin , os: macos-11 }
32+
- { target: x86_64-unknown-linux-gnu , os: ubuntu-20.04 }
33+
- { target: x86_64-unknown-linux-musl , os: ubuntu-20.04 , use-cross: true }
34+
- { target: x86_64-pc-windows-gnu , os: windows-2019 }
35+
- { target: x86_64-pc-windows-msvc , os: windows-2019 }
36+
37+
env:
38+
RUSTLER_NIF_VERSION: ${{ matrix.nif }}
39+
steps:
40+
- name: Checkout source code
41+
uses: actions/checkout@v2
42+
43+
- name: Install prerequisites
44+
shell: bash
45+
run: |
46+
case ${{ matrix.job.target }} in
47+
arm-unknown-linux-*) sudo apt-get -y update ; sudo apt-get -y install gcc-arm-linux-gnueabihf ;;
48+
aarch64-unknown-linux-gnu) sudo apt-get -y update ; sudo apt-get -y install gcc-aarch64-linux-gnu ;;
49+
esac
50+
- name: Extract crate information
51+
shell: bash
52+
run: |
53+
echo "PROJECT_NAME=$(sed -n 's/^name = "\(.*\)"/\1/p' Cargo.toml | head -n1)" >> $GITHUB_ENV
54+
# Get the project version from mix.exs
55+
echo "PROJECT_VERSION=$(sed -n 's/^ @version "\(.*\)"/\1/p' ../../mix.exs | head -n1)" >> $GITHUB_ENV
56+
- name: Install Rust toolchain
57+
uses: actions-rs/toolchain@v1
58+
with:
59+
toolchain: stable
60+
target: ${{ matrix.job.target }}
61+
override: true
62+
profile: minimal
63+
64+
- name: Show version information (Rust, cargo, GCC)
65+
shell: bash
66+
run: |
67+
gcc --version || true
68+
rustup -V
69+
rustup toolchain list
70+
rustup default
71+
cargo -V
72+
rustc -V
73+
rustc --print=cfg
74+
- name: Download cross from GitHub releases
75+
uses: giantswarm/install-binary-action@v1.0.0
76+
if: ${{ matrix.job.use-cross }}
77+
with:
78+
binary: "cross"
79+
version: "v0.2.2"
80+
download_url: "https://github.com/cross-rs/cross/releases/download/${version}/cross-x86_64-unknown-linux-gnu.tar.gz"
81+
tarball_binary_path: "${binary}"
82+
smoke_test: "${binary} --version"
83+
84+
- name: Build
85+
shell: bash
86+
run: |
87+
if [ "${{ matrix.job.use-cross }}" == "true" ]; then
88+
cross build --release --target=${{ matrix.job.target }}
89+
else
90+
cargo build --release --target=${{ matrix.job.target }}
91+
fi
92+
- name: Rename lib to the final name
93+
id: rename
94+
shell: bash
95+
run: |
96+
LIB_PREFIX="lib"
97+
case ${{ matrix.job.target }} in
98+
*-pc-windows-*) LIB_PREFIX="" ;;
99+
esac;
100+
# Figure out suffix of lib
101+
# See: https://doc.rust-lang.org/reference/linkage.html
102+
LIB_SUFFIX=".so"
103+
case ${{ matrix.job.target }} in
104+
*-apple-darwin) LIB_SUFFIX=".dylib" ;;
105+
*-pc-windows-*) LIB_SUFFIX=".dll" ;;
106+
esac;
107+
CICD_INTERMEDIATES_DIR=$(mktemp -d)
108+
# Setup paths
109+
LIB_DIR="${CICD_INTERMEDIATES_DIR}/released-lib"
110+
mkdir -p "${LIB_DIR}"
111+
LIB_NAME="${LIB_PREFIX}${{ env.PROJECT_NAME }}${LIB_SUFFIX}"
112+
LIB_PATH="${LIB_DIR}/${LIB_NAME}"
113+
# Copy the release build lib to the result location
114+
cp "target/${{ matrix.job.target }}/release/${LIB_NAME}" "${LIB_DIR}"
115+
# Final paths
116+
# In the end we use ".so" for MacOS in the final build
117+
# See: https://www.erlang.org/doc/man/erlang.html#load_nif-2
118+
LIB_FINAL_SUFFIX="${LIB_SUFFIX}"
119+
case ${{ matrix.job.target }} in
120+
*-apple-darwin) LIB_FINAL_SUFFIX=".so" ;;
121+
esac;
122+
LIB_FINAL_NAME="${LIB_PREFIX}${PROJECT_NAME}-v${PROJECT_VERSION}-nif-${RUSTLER_NIF_VERSION}-${{ matrix.job.target }}${LIB_FINAL_SUFFIX}"
123+
# Copy lib to final name on this directory
124+
cp "${LIB_PATH}" "${LIB_FINAL_NAME}"
125+
tar -cvzf "${LIB_FINAL_NAME}.tar.gz" "${LIB_FINAL_NAME}"
126+
# Passes the path relative to the root of the project.
127+
LIB_FINAL_PATH="${NIF_DIRECTORY}/${LIB_FINAL_NAME}.tar.gz"
128+
# Let subsequent steps know where to find the lib
129+
echo ::set-output name=LIB_FINAL_PATH::${LIB_FINAL_PATH}
130+
echo ::set-output name=LIB_FINAL_NAME::${LIB_FINAL_NAME}.tar.gz
131+
- name: "Artifact upload"
132+
uses: actions/upload-artifact@v2
133+
with:
134+
name: ${{ steps.rename.outputs.LIB_FINAL_NAME }}
135+
path: ${{ steps.rename.outputs.LIB_FINAL_PATH }}
136+
137+
- name: Publish archives and packages
138+
uses: softprops/action-gh-release@v1
139+
with:
140+
files: |
141+
${{ steps.rename.outputs.LIB_FINAL_PATH }}
142+
if: startsWith(github.ref, 'refs/tags/')

lib/excoding.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@ defmodule Excoding do
3434
iex> Excoding.decode(<<0xA5, 0xA4, 0xF9>>, "windows-1255")
3535
"¥₪ש"
3636
"""
37-
use Rustler,
37+
version = Mix.Project.config()[:version]
38+
39+
use RustlerPrecompiled,
3840
otp_app: :excoding,
3941
crate: "excoding",
42+
base_url: "https://github.com/moogle19/excoding/releases/download/v#{version}",
43+
force_build: System.get_env("RUSTLER_PRECOMPILATION_EXAMPLE_BUILD") in ["1", "true"],
4044
mode: if(Mix.env() == :prod, do: :release, else: :debug)
4145

4246
@doc """

native/excoding/.cargo/config

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
[target.'cfg(target_os = "macos")']
22
rustflags = [
3-
"-C", "link-arg=-undefined",
4-
"-C", "link-arg=dynamic_lookup",
3+
"-C",
4+
"link-arg=-undefined",
5+
"-C",
6+
"link-arg=dynamic_lookup",
57
]
8+
9+
# See https://github.com/rust-lang/rust/issues/59302
10+
[target.x86_64-unknown-linux-musl]
11+
rustflags = [
12+
"-C",
13+
"target-feature=-crt-static",
14+
]
15+
16+
# Provides a small build size, but takes more time to build.
17+
[profile.release]
18+
lto = true

native/excoding/Cross.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[build.env]
2+
passthrough = [
3+
"RUSTLER_NIF_VERSION",
4+
]

0 commit comments

Comments
 (0)