From 43c37a64d9236646f495186906c1f4f718eb0705 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Alejos?= Date: Sat, 19 Oct 2024 21:20:57 -0400 Subject: [PATCH] Add library_dirs and update target arch rules (#489) * Update target * Add library_dirs option for C --- lib/zig/_c.ex | 4 ++++ lib/zig/target.ex | 12 +++++------- lib/zig/templates/build.zig.eex | 4 ++++ 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/zig/_c.ex b/lib/zig/_c.ex index 02a07843..be526a13 100644 --- a/lib/zig/_c.ex +++ b/lib/zig/_c.ex @@ -5,12 +5,14 @@ defmodule Zig.C do # c-interoperability with zigler defstruct include_dirs: [], + library_dirs: [], src: [], link_lib: [], link_libcpp: false @type t :: %__MODULE__{ include_dirs: [Path.t()], + library_dirs: [Path.t()], link_lib: [Path.t()], link_libcpp: boolean, src: src_opts() @@ -18,6 +20,7 @@ defmodule Zig.C do @type opts :: [ include_dirs: Path.t() | [Path.t()], + library_dirs: Path.t() | [Path.t()], link_lib: Path.t() | [Path.t()], link_libcpp: boolean, src: src_opts() @@ -33,6 +36,7 @@ defmodule Zig.C do struct!(__MODULE__, include_dirs: normalize_filelist(opts, :include_dirs, module_dir), + library_dirs: normalize_filelist(opts, :library_dirs, module_dir), link_lib: normalize_filelist(opts, :link_lib, module_dir), link_libcpp: Keyword.get(opts, :link_libcpp, false), src: normalized_srclist(opts, module_dir) diff --git a/lib/zig/target.ex b/lib/zig/target.ex index ce00799e..854acae2 100644 --- a/lib/zig/target.ex +++ b/lib/zig/target.ex @@ -21,14 +21,12 @@ defmodule Zig.Target do # obtains the target from the @spec resolve() :: nil | t def resolve do - unless function_exported?(Mix, :target, 0) and Mix.target() === :host do - arch = System.get_env("TARGET_ARCH") - os = System.get_env("TARGET_OS") - abi = System.get_env("TARGET_ABI") + arch = System.get_env("TARGET_ARCH") + os = System.get_env("TARGET_OS") + abi = System.get_env("TARGET_ABI") - if arch && os && abi do - %__MODULE__{arch: arch, os: os, abi: abi} - end + if arch && os && abi do + %__MODULE__{arch: arch, os: os, abi: abi} end end diff --git a/lib/zig/templates/build.zig.eex b/lib/zig/templates/build.zig.eex index 33ea2a98..c3ecade9 100644 --- a/lib/zig/templates/build.zig.eex +++ b/lib/zig/templates/build.zig.eex @@ -83,6 +83,10 @@ pub fn build(b: *std.Build) void { nif.addIncludePath(.{.cwd_relative = "<%= include_dir %>"}); <% end %> + <%= for library_dir <- @c.library_dirs do %> + nif.addLibraryPath(.{.cwd_relative = "<%= library_dir %>"}); + <% end %> + <%= for {src, src_opts} <- @c.src do %> <% ccompileparams = Enum.map_join(src_opts, ", ", &~s("#{&1}")) %> nif.addCSourceFile(.{.file = .{.cwd_relative = "<%= src %>"}, .flags = &[_][]const u8{<%= ccompileparams %>}});