The prod build runs MIX_ENV=prod without make/gcc/net-snmp. The NIF is pre-built by the towerops-nif Nix package and injected via build.nix preBuild. Skip the mix compiler task in prod to avoid 'could not be found' CI errors.
53 lines
1.1 KiB
Elixir
53 lines
1.1 KiB
Elixir
defmodule Mix.Tasks.Compile.ToweropsNif do
|
|
@moduledoc """
|
|
Compiles the towerops_nif C NIF.
|
|
|
|
In dev/test, runs `make -C c_src` to build `priv/towerops_nif.so`.
|
|
In prod, the NIF is pre-built by Nix and injected via build.nix preBuild,
|
|
so this task is a no-op.
|
|
"""
|
|
|
|
use Mix.Task.Compiler
|
|
|
|
@impl true
|
|
def run(_args) do
|
|
if skip_compilation?() do
|
|
{:ok, []}
|
|
else
|
|
do_compile()
|
|
end
|
|
end
|
|
|
|
defp skip_compilation? do
|
|
Mix.env() == :prod or is_nil(System.find_executable("make"))
|
|
end
|
|
|
|
defp do_compile do
|
|
{result, _exit_code} = System.cmd("make", [], cd: "c_src", stderr_to_stdout: true, into: IO.stream())
|
|
|
|
case result do
|
|
"" ->
|
|
{:ok, []}
|
|
|
|
_ ->
|
|
# Output was produced, so the build ran
|
|
{:ok, []}
|
|
end
|
|
rescue
|
|
e ->
|
|
Mix.shell().error("Failed to compile C NIF: #{inspect(e)}")
|
|
{:error, []}
|
|
end
|
|
|
|
@impl true
|
|
def clean do
|
|
if skip_compilation?() do
|
|
:ok
|
|
else
|
|
{_result, _exit_code} = System.cmd("make", ["clean"], cd: "c_src", stderr_to_stdout: true)
|
|
:ok
|
|
end
|
|
rescue
|
|
_ -> :ok
|
|
end
|
|
end
|