Current section
Files
Jump to
Current section
Files
lib/mix/compilers/firebird_nif.ex
defmodule Mix.Tasks.Compile.FirebirdNif do
@moduledoc """
Compiles the Firebird sync NIF from Rust source.
The NIF provides synchronous WASM execution via dirty CPU schedulers,
reducing per-call overhead from ~40μs (Wasmex async) to ~6μs.
Requires Rust toolchain (rustc + cargo). Install via:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
"""
use Mix.Task
@impl true
def run(_args) do
native_dir = Path.join(File.cwd!(), "native/firebird_nif")
priv_dir = Path.join(File.cwd!(), "priv/native")
if File.dir?(native_dir) do
# Check if we need to rebuild
so_path = Path.join(priv_dir, "libfirebird_nif.so")
src_path = Path.join(native_dir, "src/lib.rs")
needs_build =
!File.exists?(so_path) or
(File.exists?(src_path) and
File.stat!(src_path).mtime > File.stat!(so_path).mtime)
if needs_build do
Mix.shell().info("Compiling Firebird sync NIF...")
File.mkdir_p!(priv_dir)
case System.cmd("cargo", ["build", "--release"],
cd: native_dir,
env: [
{"PATH", System.get_env("PATH", "") <> ":#{System.get_env("HOME")}/.cargo/bin"}
],
stderr_to_stdout: true
) do
{_output, 0} ->
# Copy the built library
release_dir = Path.join(native_dir, "target/release")
lib_name = "libfirebird_nif.so"
src = Path.join(release_dir, lib_name)
if File.exists?(src) do
File.cp!(src, so_path)
Mix.shell().info(" → #{so_path}")
end
:ok
{output, code} ->
Mix.shell().error("Firebird NIF compilation failed (exit #{code}):")
Mix.shell().error(output)
# Don't fail the build - SyncNif is optional
:ok
end
else
:ok
end
else
:ok
end
end
end