Skip to content

Commit

Permalink
fixes C file translation
Browse files Browse the repository at this point in the history
  • Loading branch information
ityonemo committed Aug 15, 2024
1 parent 08cfccb commit 1921922
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
39 changes: 22 additions & 17 deletions lib/zig/_c.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/zig/_module.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 1921922

Please sign in to comment.