Skip to content

Commit

Permalink
Adds build modes (#496)
Browse files Browse the repository at this point in the history
* adds release modes and a test for it

* tests release modes

* changes to 0.13.3
  • Loading branch information
ityonemo authored Oct 20, 2024
1 parent 43c37a6 commit 8bc7fb3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/zig/_module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ defmodule Zig.Module do
:c,
:dir,
:easy_c,
release_mode: :debug,
language: Elixir,
nifs: {:auto, []},
ignore: [],
Expand Down Expand Up @@ -68,6 +69,7 @@ defmodule Zig.Module do
c: C.opts() | C.t(),
dir: Path.t(),
easy_c: Path.t(),
release_mode: release_modes(),
language: Elixir | :erlang,
nifs: {:auto, Nif.opts()} | Nif.opts() | [Nif.t()],
ignore: [atom()],
Expand All @@ -83,6 +85,7 @@ defmodule Zig.Module do
attributes: keyword
}

@type release_modes :: :debug | :safe | :fast | :small
@type packagespec() :: {name :: atom(), {path :: Path.t(), deps :: [atom]}}
# NB: this is going to become more complex for security reasons.
@type precompiledspec() :: Path.t()
Expand Down
17 changes: 16 additions & 1 deletion lib/zig/command.ex
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ defmodule Zig.Command do

lib_dir = Path.join(so_dir, "lib")

run_zig("build --prefix #{so_dir}", cd: staging_directory, stderr_to_stdout: true)
run_zig("build -Doptimize=#{release_mode(module)} --prefix #{so_dir}", cd: staging_directory, stderr_to_stdout: true)

src_lib_name = Path.join(lib_dir, src_lib_name(module.module))
dst_lib_name = Path.join(lib_dir, dst_lib_name(module.module))
Expand All @@ -124,6 +124,21 @@ defmodule Zig.Command do
run_zig("targets", [])
end

@release_modes %{
debug: "Debug",
fast: "ReleaseFast",
small: "ReleaseSmall",
safe: "ReleaseSafe"
}

def release_mode(%{release_mode: :env}) do
System.fetch_env!("ZIGLER_RELEASE_MODE")
end

def release_mode(module) do
Map.fetch!(@release_modes, module.release_mode)
end

def executable_path do
# executable_path resolves zig executable in the following fashion:
#
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Zigler.MixProject do

[
app: :zigler,
version: "0.13.2",
version: "0.13.3",
elixir: "~> 1.14",
start_permanent: env == :prod,
elixirc_paths: elixirc_paths(env),
Expand Down
14 changes: 14 additions & 0 deletions test/corner_cases/release_mode_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule ZiglerTest.CornerCases.ReleaseMode do
use ExUnit.Case, async: true
use Zig, otp_app: :zigler, release_mode: :safe

~Z"""
pub fn add(a: u32, b: u32) u32 {
return a + b;
}
"""

test "release mode safe works" do
assert 48 = add(47, 1)
end
end

0 comments on commit 8bc7fb3

Please sign in to comment.