Skip to content

Commit 1dc41b7

Browse files
committed
Add precompilation ci
1 parent faa4348 commit 1dc41b7

5 files changed

Lines changed: 209 additions & 4 deletions

File tree

.github/workflows/release.yml

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

lib/ex_srtp/backend/rust_crypto.ex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
defmodule ExSRTP.Backend.RustCrypto.Native do
22
@moduledoc false
3-
4-
use Rustler, otp_app: :ex_srtp, crate: "ex_srtp"
3+
version = Mix.Project.config()[:version]
4+
5+
use RustlerPrecompiled,
6+
otp_app: :ex_srtp,
7+
crate: "ex_srtp",
8+
base_url: "https://github.com/elixir-streaming/ex_srtp/releases/download/v#{version}",
9+
force_build: System.get_env("EXSRTP_BUILD_NIF") in ["1", "true"],
10+
version: version
511

612
def init(_policy), do: :erlang.nif_error(:nif_not_loaded)
713

mix.exs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ defmodule ExSRTP.MixProject do
2929
[
3030
{:ex_rtp, "~> 0.4.0"},
3131
{:ex_rtcp, "~> 0.4.0"},
32-
{:ex_doc, "~> 0.30", only: :dev, runtime: false},
33-
{:rustler, "~> 0.37", runtime: false}
32+
{:rustler, "~> 0.37", runtime: false},
33+
{:rustler_precompiled, "~> 0.8"},
34+
{:ex_doc, "~> 0.30", only: :dev, runtime: false}
3435
]
3536
end
3637

mix.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
%{
2+
"castore": {:hex, :castore, "1.0.17", "4f9770d2d45fbd91dcf6bd404cf64e7e58fed04fadda0923dc32acca0badffa2", [:mix], [], "hexpm", "12d24b9d80b910dd3953e165636d68f147a31db945d2dcb9365e441f8b5351e5"},
23
"earmark_parser": {:hex, :earmark_parser, "1.4.44", "f20830dd6b5c77afe2b063777ddbbff09f9759396500cdbe7523efd58d7a339c", [:mix], [], "hexpm", "4778ac752b4701a5599215f7030989c989ffdc4f6df457c5f36938cc2d2a2750"},
34
"ex_doc": {:hex, :ex_doc, "0.39.3", "519c6bc7e84a2918b737aec7ef48b96aa4698342927d080437f61395d361dcee", [:mix], [{:earmark_parser, "~> 1.4.44", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "0590955cf7ad3b625780ee1c1ea627c28a78948c6c0a9b0322bd976a079996e1"},
45
"ex_rtcp": {:hex, :ex_rtcp, "0.4.0", "f9e515462a9581798ff6413583a25174cfd2101c94a2ebee871cca7639886f0a", [:mix], [], "hexpm", "28956602cf210d692fcdaf3f60ca49681634e1deb28ace41246aee61ee22dc3b"},
@@ -9,4 +10,5 @@
910
"makeup_erlang": {:hex, :makeup_erlang, "1.0.2", "03e1804074b3aa64d5fad7aa64601ed0fb395337b982d9bcf04029d68d51b6a7", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "af33ff7ef368d5893e4a267933e7744e46ce3cf1f61e2dccf53a111ed3aa3727"},
1011
"nimble_parsec": {:hex, :nimble_parsec, "1.4.2", "8efba0122db06df95bfaa78f791344a89352ba04baedd3849593bfce4d0dc1c6", [:mix], [], "hexpm", "4b21398942dda052b403bbe1da991ccd03a053668d147d53fb8c4e0efe09c973"},
1112
"rustler": {:hex, :rustler, "0.37.1", "721434020c7f6f8e1cdc57f44f75c490435b01de96384f8ccb96043f12e8a7e0", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "24547e9b8640cf00e6a2071acb710f3e12ce0346692e45098d84d45cdb54fd79"},
13+
"rustler_precompiled": {:hex, :rustler_precompiled, "0.8.4", "700a878312acfac79fb6c572bb8b57f5aae05fe1cf70d34b5974850bbf2c05bf", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: false]}, {:rustler, "~> 0.23", [hex: :rustler, repo: "hexpm", optional: true]}], "hexpm", "3b33d99b540b15f142ba47944f7a163a25069f6d608783c321029bc1ffb09514"},
1214
}

native/ex_srtp/.cargo/config

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[target.'cfg(target_os = "macos")']
2+
rustflags = ["-C", "link-arg=-undefined", "-C", "link-arg=dynamic_lookup"]
3+
4+
[target.x86_64-unknown-linux-musl]
5+
rustflags = ["-C", "target-feature=-crt-static"]
6+
7+
[profile.release]
8+
lto = true

0 commit comments

Comments
 (0)