From 1921922fd17b44d9677f3a7d7ea852c0760f99f1 Mon Sep 17 00:00:00 2001 From: isaac yonemoto Date: Thu, 15 Aug 2024 10:08:55 -0500 Subject: [PATCH] fixes C file translation --- lib/zig/_c.ex | 39 ++++++++++++++++++++++----------------- lib/zig/_module.ex | 2 +- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/lib/zig/_c.ex b/lib/zig/_c.ex index f1db12aa..02a07843 100644 --- a/lib/zig/_c.ex +++ b/lib/zig/_c.ex @@ -25,55 +25,60 @@ defmodule Zig.C do @type src_opts :: term - def new(opts, module_file) do + def new(opts, module_opts) do + module_dir = cond do + dir = module_opts[:dir] -> dir + file = module_opts[:file] -> Path.dirname(file) + end + struct!(__MODULE__, - include_dirs: normalize_filelist(opts, :include_dirs, module_file), - link_lib: normalize_filelist(opts, :link_lib, module_file), + include_dirs: normalize_filelist(opts, :include_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_file) + src: normalized_srclist(opts, module_dir) ) end - defp normalize_filelist(opts, key, module_file) do + defp normalize_filelist(opts, key, module_dir) do opts |> Keyword.get(key) |> List.wrap() - |> Enum.map(&solve_relative(&1, module_file)) + |> Enum.map(&solve_relative(&1, module_dir)) end - defp normalized_srclist(opts, module_file) do + defp normalized_srclist(opts, module_dir) do opts |> Keyword.get(:src) |> List.wrap() - |> Enum.flat_map(&normalize_src(&1, module_file)) + |> Enum.flat_map(&normalize_src(&1, module_dir)) end defp solve_relative({:system, _} = system, _), do: system - defp solve_relative(file, module_file) do - Path.expand(file, module_file) + defp solve_relative(file, module_dir) do + Path.expand(file, module_dir) end - defp normalize_src(file, module_file) when is_binary(file) do - maybe_with_wildcard(file, module_file, []) + defp normalize_src(file, module_dir) when is_binary(file) do + maybe_with_wildcard(file, module_dir, []) end - defp normalize_src({file, opts}, module_file) do - maybe_with_wildcard(file, module_file, opts) + defp normalize_src({file, opts}, module_dir) do + maybe_with_wildcard(file, module_dir, opts) end - defp maybe_with_wildcard(file, module_file, opts) do + defp maybe_with_wildcard(file, module_dir, opts) do if String.ends_with?(file, "/*") do file - |> solve_relative(module_file) |> Path.dirname() + |> solve_relative(module_dir) |> then(fn wildcard_dir -> wildcard_dir |> File.ls!() |> Enum.map(&{Path.join(wildcard_dir, &1), opts}) end) else - [{solve_relative(file, module_file), opts}] + [{solve_relative(file, module_dir), opts}] end end end diff --git a/lib/zig/_module.ex b/lib/zig/_module.ex index 9ab8f364..7d4473b7 100644 --- a/lib/zig/_module.ex +++ b/lib/zig/_module.ex @@ -124,7 +124,7 @@ defmodule Zig.Module do defp normalize_options(opts) do opts |> obtain_version - |> Keyword.update(:c, %C{}, &C.new(&1, Keyword.fetch!(opts, :file))) + |> Keyword.update(:c, %C{}, &C.new(&1, opts)) |> Keyword.update(:callbacks, [], &normalize_callbacks(&1, opts)) end