Skip to content

Commit

Permalink
Add library_dirs and update target arch rules (#489)
Browse files Browse the repository at this point in the history
* Update target

* Add library_dirs option for C
  • Loading branch information
acalejos authored Oct 20, 2024
1 parent 12e33d8 commit 43c37a6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
4 changes: 4 additions & 0 deletions lib/zig/_c.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ 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()
}

@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()
Expand All @@ -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)
Expand Down
12 changes: 5 additions & 7 deletions lib/zig/target.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions lib/zig/templates/build.zig.eex
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>}});
Expand Down

0 comments on commit 43c37a6

Please sign in to comment.