Skip to content

Commit 02c1b56

Browse files
committed
Add Rustler precompiled NIF support
- Switch from Rustler to RustlerPrecompiled in Native module - Add GitHub Actions workflow for building precompiled NIFs - Add .cargo/config.toml for cross-compilation - Pin rustler NIF version to 2.15 in Cargo.toml
1 parent 54f4913 commit 02c1b56

6 files changed

Lines changed: 90 additions & 5 deletions

File tree

.cargo/config.toml

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

.github/workflows/release.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Build precompiled NIFs
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*'
7+
8+
jobs:
9+
build_release:
10+
name: NIF ${{ matrix.nif }} - ${{ matrix.job.target }} (${{ matrix.job.os }})
11+
runs-on: ${{ matrix.job.os }}
12+
permissions:
13+
contents: write
14+
strategy:
15+
fail-fast: false
16+
matrix:
17+
nif: ["2.15"]
18+
job:
19+
- { target: aarch64-unknown-linux-gnu , os: ubuntu-22.04 , use-cross: true }
20+
- { target: aarch64-unknown-linux-musl , os: ubuntu-22.04 , use-cross: true }
21+
- { target: aarch64-apple-darwin , os: macos-14 }
22+
- { target: x86_64-apple-darwin , os: macos-13 }
23+
- { target: x86_64-unknown-linux-gnu , os: ubuntu-22.04 }
24+
- { target: x86_64-unknown-linux-musl , os: ubuntu-22.04 , use-cross: true }
25+
26+
steps:
27+
- name: Checkout source code
28+
uses: actions/checkout@v4
29+
30+
- name: Extract project version
31+
shell: bash
32+
run: |
33+
echo "PROJECT_VERSION=$(sed -n 's/^ @version "\(.*\)"/\1/p' mix.exs | head -n1)" >> $GITHUB_ENV
34+
35+
- name: Install Rust toolchain
36+
uses: dtolnay/rust-toolchain@stable
37+
with:
38+
target: ${{ matrix.job.target }}
39+
40+
- name: Build the project
41+
id: build-crate
42+
uses: philss/rustler-precompiled-action@v1.1.4
43+
with:
44+
project-name: oxide_ex_nif
45+
project-version: ${{ env.PROJECT_VERSION }}
46+
target: ${{ matrix.job.target }}
47+
nif-version: ${{ matrix.nif }}
48+
use-cross: ${{ matrix.job.use-cross }}
49+
project-dir: "native/oxide_ex_nif"
50+
51+
- name: Artifact upload
52+
uses: actions/upload-artifact@v4
53+
with:
54+
name: ${{ steps.build-crate.outputs.file-name }}
55+
path: ${{ steps.build-crate.outputs.file-path }}
56+
57+
- name: Publish archives and packages
58+
uses: softprops/action-gh-release@v2
59+
with:
60+
files: |
61+
${{ steps.build-crate.outputs.file-path }}

lib/oxide/native.ex

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
defmodule Oxide.Native do
22
@moduledoc false
3-
use Rustler, otp_app: :oxide_ex, crate: "oxide_ex_nif"
3+
4+
version = Mix.Project.config()[:version]
5+
6+
use RustlerPrecompiled,
7+
otp_app: :oxide_ex,
8+
crate: "oxide_ex_nif",
9+
base_url: "https://github.com/dannote/oxide_ex/releases/download/v#{version}",
10+
force_build: System.get_env("OXIDE_EX_BUILD") in ["1", "true"],
11+
version: version
412

513
@spec new_scanner(list()) :: reference()
614
def new_scanner(_sources), do: :erlang.nif_error(:nif_not_loaded)

mix.exs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Oxide.MixProject do
22
use Mix.Project
33

4-
@version "0.1.0"
4+
@version "0.2.0"
55
@source_url "https://github.com/dannote/oxide_ex"
66

77
def project do
@@ -35,7 +35,7 @@ defmodule Oxide.MixProject do
3535
"Tailwind CSS" => "https://tailwindcss.com"
3636
},
3737
files:
38-
~w(lib native/oxide_ex_nif/src native/oxide_ex_nif/Cargo.toml Cargo.toml Cargo.lock .formatter.exs mix.exs README.md LICENSE)
38+
~w(lib native/oxide_ex_nif/src native/oxide_ex_nif/Cargo.toml Cargo.toml Cargo.lock .formatter.exs mix.exs README.md LICENSE checksum-*.exs)
3939
]
4040
end
4141

@@ -63,7 +63,8 @@ defmodule Oxide.MixProject do
6363

6464
defp deps do
6565
[
66-
{:rustler, "~> 0.36 or ~> 0.37"},
66+
{:rustler_precompiled, "~> 0.8"},
67+
{:rustler, "~> 0.36 or ~> 0.37", optional: true},
6768
{:ex_doc, "~> 0.35", only: :dev, runtime: false},
6869
{:dialyxir, "~> 1.4", only: [:dev, :test], runtime: false},
6970
{:credo, "~> 1.7", only: [:dev, :test], runtime: false},

mix.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
%{
22
"bunt": {:hex, :bunt, "1.0.0", "081c2c665f086849e6d57900292b3a161727ab40431219529f13c4ddcf3e7a44", [:mix], [], "hexpm", "dc5f86aa08a5f6fa6b8096f0735c4e76d54ae5c9fa2c143e5a1fc7c1cd9bb6b5"},
3+
"castore": {:hex, :castore, "1.0.17", "4f9770d2d45fbd91dcf6bd404cf64e7e58fed04fadda0923dc32acca0badffa2", [:mix], [], "hexpm", "12d24b9d80b910dd3953e165636d68f147a31db945d2dcb9365e441f8b5351e5"},
34
"credo": {:hex, :credo, "1.7.17", "f92b6aa5b26301eaa5a35e4d48ebf5aa1e7094ac00ae38f87086c562caf8a22f", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "1eb5645c835f0b6c9b5410f94b5a185057bcf6d62a9c2b476da971cde8749645"},
45
"dialyxir": {:hex, :dialyxir, "1.4.7", "dda948fcee52962e4b6c5b4b16b2d8fa7d50d8645bbae8b8685c3f9ecb7f5f4d", [:mix], [{:erlex, ">= 0.2.8", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "b34527202e6eb8cee198efec110996c25c5898f43a4094df157f8d28f27d9efe"},
56
"earmark_parser": {:hex, :earmark_parser, "1.4.44", "f20830dd6b5c77afe2b063777ddbbff09f9759396500cdbe7523efd58d7a339c", [:mix], [], "hexpm", "4778ac752b4701a5599215f7030989c989ffdc4f6df457c5f36938cc2d2a2750"},
@@ -14,4 +15,5 @@
1415
"makeup_erlang": {:hex, :makeup_erlang, "1.0.3", "4252d5d4098da7415c390e847c814bad3764c94a814a0b4245176215615e1035", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "953297c02582a33411ac6208f2c6e55f0e870df7f80da724ed613f10e6706afd"},
1516
"nimble_parsec": {:hex, :nimble_parsec, "1.4.2", "8efba0122db06df95bfaa78f791344a89352ba04baedd3849593bfce4d0dc1c6", [:mix], [], "hexpm", "4b21398942dda052b403bbe1da991ccd03a053668d147d53fb8c4e0efe09c973"},
1617
"rustler": {:hex, :rustler, "0.37.3", "5f4e6634d43b26f0a69834dd1d3ed4e1710b022a053bf4a670220c9540c92602", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "a6872c6f53dcf00486d1e7f9e046e20e01bf1654bdacc4193016c2e8002b32a2"},
18+
"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"},
1719
}

native/oxide_ex_nif/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ path = "src/lib.rs"
1111
crate-type = ["cdylib"]
1212

1313
[dependencies]
14-
rustler = "0.37"
14+
rustler = { version = "0.37", default-features = false, features = ["derive", "nif_version_2_15"] }
1515
tailwindcss-oxide = { git = "https://github.com/tailwindlabs/tailwindcss.git", tag = "v4.1.8" }

0 commit comments

Comments
 (0)