From 8bc7fb3a527c5b4dd0455eae3463754d5630249b Mon Sep 17 00:00:00 2001 From: Isaac Yonemoto Date: Sun, 20 Oct 2024 11:00:17 -0500 Subject: [PATCH] Adds build modes (#496) * adds release modes and a test for it * tests release modes * changes to 0.13.3 --- lib/zig/_module.ex | 3 +++ lib/zig/command.ex | 17 ++++++++++++++++- mix.exs | 2 +- test/corner_cases/release_mode_test.exs | 14 ++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 test/corner_cases/release_mode_test.exs diff --git a/lib/zig/_module.ex b/lib/zig/_module.ex index 7d4473b7..121558fa 100644 --- a/lib/zig/_module.ex +++ b/lib/zig/_module.ex @@ -36,6 +36,7 @@ defmodule Zig.Module do :c, :dir, :easy_c, + release_mode: :debug, language: Elixir, nifs: {:auto, []}, ignore: [], @@ -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()], @@ -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() diff --git a/lib/zig/command.ex b/lib/zig/command.ex index b3c49c5b..3bbe89a1 100644 --- a/lib/zig/command.ex +++ b/lib/zig/command.ex @@ -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)) @@ -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: # diff --git a/mix.exs b/mix.exs index ec07d7e7..6cba406b 100644 --- a/mix.exs +++ b/mix.exs @@ -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), diff --git a/test/corner_cases/release_mode_test.exs b/test/corner_cases/release_mode_test.exs new file mode 100644 index 00000000..378e6cbb --- /dev/null +++ b/test/corner_cases/release_mode_test.exs @@ -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 \ No newline at end of file