Skip to content

Commit

Permalink
Merge pull request #10 from lytedev/master
Browse files Browse the repository at this point in the history
Add RustlerPrecompiled and setup associated GitHub actions and add document for creating releases
  • Loading branch information
benhaney authored Aug 14, 2022
2 parents 5268b98 + 48e9d3c commit 981a390
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 2 deletions.
157 changes: 157 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
name: Build precompiled NIFs

env:
NIF_DIRECTORY: "native/jsonrs"
SKIP_JSONRS_BUILD: "true"

on:
push:
branches:
- main
tags:
- '*'

defaults:
run:
working-directory: "./native/jsonrs"

jobs:
build_release:
name: NIF ${{ matrix.nif }} - ${{ matrix.job.target }} (${{ matrix.job.os }})
runs-on: ${{ matrix.job.os }}
strategy:
fail-fast: false
matrix:
nif: ["2.16", "2.15", "2.14"]
job:
- target: arm-unknown-linux-gnueabihf
os: ubuntu-20.04
use-cross: true
- target: aarch64-unknown-linux-gnu
os: ubuntu-20.04
use-cross: true
- target: aarch64-apple-darwin
os: macos-11
- target: x86_64-apple-darwin
os: macos-11
- target: x86_64-unknown-linux-gnu
os: ubuntu-20.04
- target: x86_64-unknown-linux-musl
os: ubuntu-20.04
use-cross: true
- target: x86_64-pc-windows-gnu
os: windows-2019
- target: x86_64-pc-windows-msvc
os: windows-2019

env:
RUSTLER_NIF_VERSION: ${{ matrix.nif }}

steps:
- name: Checkout source code
uses: actions/checkout@v2

- name: Install prerequisites
shell: bash
run: |
case ${{ matrix.job.target }} in
arm-unknown-linux-*) sudo apt-get -y update ; sudo apt-get -y install gcc-arm-linux-gnueabihf ;;
aarch64-unknown-linux-gnu) sudo apt-get -y update ; sudo apt-get -y install gcc-aarch64-linux-gnu ;;
esac
- name: Extract crate information
shell: bash
run: |
echo "PROJECT_NAME=$(sed -n 's/^name = "\(.*\)"/\1/p' Cargo.toml | head -n1)" >> $GITHUB_ENV
# Get the project version from mix.exs
echo "PROJECT_VERSION=$(sed -n 's/^ @version "\(.*\)"/\1/p' ../../mix.exs | head -n1)" >> $GITHUB_ENV
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: ${{ matrix.job.target }}
override: true
profile: minimal

- name: Show version
shell: bash
run: |
gcc --version || true
rustup -V
rustup toolchain list
rustup default
cargo -V
rustc -V
rustc --print=cfg
- name: Download cross
uses: giantswarm/install-binary-action@v1.0.0
if: ${{ matrix.job.use-cross }}
with:
binary: "cross"
version: "v0.2.2"
download_url: "https://github.com/cross-rs/cross/releases/download/${version}/cross-x86_64-unknown-linux-gnu.tar.gz"
tarball_binary_path: "${binary}"
smoke_test: "${binary} --version"

- name: Build lib
shell: bash
run: |
if [ "${{ matrix.job.use-cross }}" == "true" ]; then
cross build --release --target=${{ matrix.job.target }}
else
cargo build --release --target=${{ matrix.job.target }}
fi
- name: Rename lib
id: rename
shell: bash
run: |
LIB_PREFIX="lib"
case ${{ matrix.job.target }} in
*-pc-windows-*) LIB_PREFIX="" ;;
esac;
# Figure out suffix of lib
# See: https://doc.rust-lang.org/reference/linkage.html
LIB_SUFFIX=".so"
case ${{ matrix.job.target }} in
*-apple-darwin) LIB_SUFFIX=".dylib" ;;
*-pc-windows-*) LIB_SUFFIX=".dll" ;;
esac;
CICD_INTERMEDIATES_DIR=$(mktemp -d)
# Setup paths
LIB_DIR="${CICD_INTERMEDIATES_DIR}/released-lib"
mkdir -p "${LIB_DIR}"
LIB_NAME="${LIB_PREFIX}${{ env.PROJECT_NAME }}${LIB_SUFFIX}"
LIB_PATH="${LIB_DIR}/${LIB_NAME}"
# Copy the release build lib to the result location
cp "target/${{ matrix.job.target }}/release/${LIB_NAME}" "${LIB_DIR}"
# Final paths
# In the end we use ".so" for MacOS in the final build
# See: https://www.erlang.org/doc/man/erlang.html#load_nif-2
LIB_FINAL_SUFFIX="${LIB_SUFFIX}"
case ${{ matrix.job.target }} in
*-apple-darwin) LIB_FINAL_SUFFIX=".so" ;;
esac;
LIB_FINAL_NAME="${LIB_PREFIX}${PROJECT_NAME}-v${PROJECT_VERSION}-nif-${RUSTLER_NIF_VERSION}-${{ matrix.job.target }}${LIB_FINAL_SUFFIX}"
# Copy lib to final name on this directory
cp "${LIB_PATH}" "${LIB_FINAL_NAME}"
tar -cvzf "${LIB_FINAL_NAME}.tar.gz" "${LIB_FINAL_NAME}"
# Passes the path relative to the root of the project.
LIB_FINAL_PATH="${NIF_DIRECTORY}/${LIB_FINAL_NAME}.tar.gz"
# Let subsequent steps know where to find the lib
echo ::set-output name=LIB_FINAL_PATH::${LIB_FINAL_PATH}
echo ::set-output name=LIB_FINAL_NAME::${LIB_FINAL_NAME}.tar.gz
- name: "Upload artifact"
uses: actions/upload-artifact@v2
with:
name: ${{ steps.rename.outputs.LIB_FINAL_NAME }}
path: ${{ steps.rename.outputs.LIB_FINAL_PATH }}

- name: Publish archives and packages
uses: softprops/action-gh-release@v1
with:
files: |
${{ steps.rename.outputs.LIB_FINAL_PATH }}
if: startsWith(github.ref, 'refs/tags/')
10 changes: 10 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Publishing Jsonrs

From: https://hexdocs.pm/rustler_precompiled/precompilation_guide.html#recommended-flow

- Bump dep version in `mix.exs`
- `git tag v${mix_version}` the git tag must be `v` prepended to the Mix project version
- `git push --tags`
- Wait for the associated GitHub actions to finish
- Run `mix rustler_precompiled.download Jsonrs --all`
- Release the packge to hex.pm `mix hex.publish` (making sure to include the correct files (**untested!**)
26 changes: 26 additions & 0 deletions checksum-Elixir.Jsonrs.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
%{
"jsonrs-v0.2.0-nif-2.14-x86_64-pc-windows-gnu.dll.tar.gz" => "sha256:091a1de3b8e91f1d4757429fa25e900a7c4772ee75c5683e8b9baf74b47e3d83",
"jsonrs-v0.2.0-nif-2.14-x86_64-pc-windows-msvc.dll.tar.gz" => "sha256:88c41a8fcda4b4d41e8312773ca36d8caa47716d1547f6789e4b32399f366759",
"jsonrs-v0.2.0-nif-2.15-x86_64-pc-windows-gnu.dll.tar.gz" => "sha256:9e75646ead58b5bab7137ca231268de764691c2d8d9f6294d9b9871443cbcbc9",
"jsonrs-v0.2.0-nif-2.15-x86_64-pc-windows-msvc.dll.tar.gz" => "sha256:53e754d46534570d44675e77c110872f52a9e5a21ef3a83a1ad3c198a139252a",
"jsonrs-v0.2.0-nif-2.16-x86_64-pc-windows-gnu.dll.tar.gz" => "sha256:668e847f5fc4c4033a929add644a4c98bdf207d914306125210754cce8991f54",
"jsonrs-v0.2.0-nif-2.16-x86_64-pc-windows-msvc.dll.tar.gz" => "sha256:68a6ed5eda6e0a3073107495a255670902a0c9bc11e3514950f548cf89913d5a",
"libjsonrs-v0.2.0-nif-2.14-aarch64-apple-darwin.so.tar.gz" => "sha256:203fa811ac4363c4593cff8a78213f31886582ee912491a78ad578b66a6e1694",
"libjsonrs-v0.2.0-nif-2.14-aarch64-unknown-linux-gnu.so.tar.gz" => "sha256:aaaa1e54468bcecbdf8f7dcbad572215b2540dc3f44029cb7ea6b6873b109175",
"libjsonrs-v0.2.0-nif-2.14-arm-unknown-linux-gnueabihf.so.tar.gz" => "sha256:37e2bf004679408990a1896dc80e597403b84864ed6ccdb236acd60c6aa913e9",
"libjsonrs-v0.2.0-nif-2.14-x86_64-apple-darwin.so.tar.gz" => "sha256:123f78da4898e14f6c633d23a87987be7f88b9d9efad4930d32cb6f66efcb984",
"libjsonrs-v0.2.0-nif-2.14-x86_64-unknown-linux-gnu.so.tar.gz" => "sha256:deaf2c62d3520f36022b2b1248543dd708046cbb7020dc2ffbc96de4a12d28f0",
"libjsonrs-v0.2.0-nif-2.14-x86_64-unknown-linux-musl.so.tar.gz" => "sha256:fe988a3a96bf6a0f0994c834c519f1b1adb1a89b2ff8142995f3b1caa8e0ea68",
"libjsonrs-v0.2.0-nif-2.15-aarch64-apple-darwin.so.tar.gz" => "sha256:985586c3f586a85fee57bfee4e8fe1bd9aef8f614d952caf8dd140a6ac7862b5",
"libjsonrs-v0.2.0-nif-2.15-aarch64-unknown-linux-gnu.so.tar.gz" => "sha256:8b33a4ec99610e906b322debae691b05b77860f86c0b850180fbd87e4afa79b4",
"libjsonrs-v0.2.0-nif-2.15-arm-unknown-linux-gnueabihf.so.tar.gz" => "sha256:1b4b12534ccac3029ec6bc548b0cfb1cd269c93450199e43c379b292cb5afa87",
"libjsonrs-v0.2.0-nif-2.15-x86_64-apple-darwin.so.tar.gz" => "sha256:0d7f3bf8dcb334857da0f3da0e53ffa81b961408a11a4e712a7e7e08141da94a",
"libjsonrs-v0.2.0-nif-2.15-x86_64-unknown-linux-gnu.so.tar.gz" => "sha256:55666fc597508e679ddc34928e10e1b4dc891e9dd5976d1804e2f94c80d34626",
"libjsonrs-v0.2.0-nif-2.15-x86_64-unknown-linux-musl.so.tar.gz" => "sha256:b5cc455d816c9212fa64cc72cddd6ec293e4eb53039d467b94e853482407ad80",
"libjsonrs-v0.2.0-nif-2.16-aarch64-apple-darwin.so.tar.gz" => "sha256:4b26386837e9dad99377f7a694a717a4777eae738fce39b61565cd581ec9c782",
"libjsonrs-v0.2.0-nif-2.16-aarch64-unknown-linux-gnu.so.tar.gz" => "sha256:4bf18417dc2948351ae3c371aa087b3ea7e70f206c5aa4b9e4e0c4f30acd82a0",
"libjsonrs-v0.2.0-nif-2.16-arm-unknown-linux-gnueabihf.so.tar.gz" => "sha256:99e6fca2d05ddfaaf4149b2bd78126cd74b6423d9ca3dee7848e9834c35cd291",
"libjsonrs-v0.2.0-nif-2.16-x86_64-apple-darwin.so.tar.gz" => "sha256:e70f0a442b40ac5c3bb2b8e6b1bda010c3f25fa39b0c9826b540cb1e2d6f4ccd",
"libjsonrs-v0.2.0-nif-2.16-x86_64-unknown-linux-gnu.so.tar.gz" => "sha256:51cff72a837ac36dfd46eba0ca1a33239ca78fe40266a0431c1fb4b9005fe711",
"libjsonrs-v0.2.0-nif-2.16-x86_64-unknown-linux-musl.so.tar.gz" => "sha256:7a389221df15c0d040d2536dca7789ea91c7a72c3ce4569d095eecf87097862c",
}
8 changes: 7 additions & 1 deletion lib/jsonrs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ defmodule Jsonrs do
@moduledoc """
A JSON library powered by Rust's Serde through a NIF
"""
use Rustler, otp_app: :jsonrs

version = Mix.Project.config()[:version]

use RustlerPrecompiled, otp_app: :jsonrs,
base_url: "https://github.com/lytedev/jsonrs/releases/download/v#{version}",
force_build: not(System.get_env("SKIP_JSONRS_BUILD") in ["1", "true"]),
version: version

@spec nif_encode!(term) :: String.t()
defp nif_encode!(_input), do: :erlang.nif_error(:nif_not_loaded)
Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ defmodule Jsonrs.MixProject do
defp deps do
[
{:rustler, "~> 0.25.0"},
{:rustler_precompiled, "~> 0.5"},
{:ex_doc, "~> 0.27", only: :dev, runtime: false},
]
end
Expand All @@ -50,7 +51,7 @@ defmodule Jsonrs.MixProject do
maintainers: ["Ben Haney"],
licenses: ["Unlicense"],
links: %{"GitHub" => "https://github.com/benhaney/jsonrs"},
files: ["lib", "mix.exs", "README*", "native/jsonrs/src", "native/jsonrs/.cargo", "native/jsonrs/README*", "native/jsonrs/Cargo*"]
files: ["lib", "mix.exs", "README*", "native/jsonrs/src", "native/jsonrs/.cargo", "native/jsonrs/README*", "native/jsonrs/Cargo*", "native", "checksum-*.exs"]
]
end

Expand Down
2 changes: 2 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
%{
"castore": {:hex, :castore, "0.1.18", "deb5b9ab02400561b6f5708f3e7660fc35ca2d51bfc6a940d2f513f89c2975fc", [:mix], [], "hexpm", "61bbaf6452b782ef80b33cdb45701afbcf0a918a45ebe7e73f1130d661e66a06"},
"earmark": {:hex, :earmark, "1.4.2", "3aa0bd23bc4c61cf2f1e5d752d1bb470560a6f8539974f767a38923bb20e1d7f", [:mix], [], "hexpm"},
"earmark_parser": {:hex, :earmark_parser, "1.4.26", "f4291134583f373c7d8755566122908eb9662df4c4b63caa66a0eabe06569b0a", [:mix], [], "hexpm", "48d460899f8a0c52c5470676611c01f64f3337bad0b26ddab43648428d94aabc"},
"ex_doc": {:hex, :ex_doc, "0.28.4", "001a0ea6beac2f810f1abc3dbf4b123e9593eaa5f00dd13ded024eae7c523298", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "bf85d003dd34911d89c8ddb8bda1a958af3471a274a4c2150a9c01c78ac3f8ed"},
Expand All @@ -8,5 +9,6 @@
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
"nimble_parsec": {:hex, :nimble_parsec, "1.2.3", "244836e6e3f1200c7f30cb56733fd808744eca61fd182f731eac4af635cc6d0b", [:mix], [], "hexpm", "c8d789e39b9131acf7b99291e93dae60ab48ef14a7ee9d58c6964f59efb570b0"},
"rustler": {:hex, :rustler, "0.25.0", "32526b51af7e58a740f61941bf923486ce6415a91c3934cc16c281aa201a2240", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:toml, "~> 0.6", [hex: :toml, repo: "hexpm", optional: false]}], "hexpm", "6b43a11a37fe79c6234d88c4102ab5dfede7a6a764dc5c7b539956cfa02f3cf4"},
"rustler_precompiled": {:hex, :rustler_precompiled, "0.5.1", "93df423bd7b14b67dcacf994443d132d300623f80756974cac4febeab40af74a", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: false]}, {:rustler, "~> 0.23", [hex: :rustler, repo: "hexpm", optional: true]}], "hexpm", "3f8cbc8e92eef4e1a71bf441b568b868b16a3730f63f5b803c68073017e30b13"},
"toml": {:hex, :toml, "0.6.2", "38f445df384a17e5d382befe30e3489112a48d3ba4c459e543f748c2f25dd4d1", [:mix], [], "hexpm", "d013e45126d74c0c26a38d31f5e8e9b83ea19fc752470feb9a86071ca5a672fa"},
}
9 changes: 9 additions & 0 deletions native/jsonrs/.cargo/config
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ rustflags = [
"-C", "link-arg=-undefined",
"-C", "link-arg=dynamic_lookup",
]

# See https://github.com/rust-lang/rust/issues/59302
[target.x86_64-unknown-linux-musl]
rustflags = [
"-C", "target-feature=-crt-static"
]

[profile.release]
lto = true
4 changes: 4 additions & 0 deletions native/jsonrs/Cross.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[build.env]
passthrough = [
"RUSTLER_NIF_VERSION"
]

0 comments on commit 981a390

Please sign in to comment.