Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve UX #105

Merged
merged 2 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions lib/bundlex/toolchain/common/unix.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Bundlex.Toolchain.Common.Unix do
@moduledoc false

use Bunch
alias Bundlex.{Native, Toolchain}
alias Bundlex.{Native, Output, Toolchain}
alias Bundlex.Toolchain.Common.Compilers

@spec compiler_commands(
Expand All @@ -14,7 +14,7 @@ defmodule Bundlex.Toolchain.Common.Unix do
) :: [String.t()]
def compiler_commands(native, compile, link, lang, options \\ []) do
includes = native.includes |> paths("-I")
pkg_config_cflags = native.pkg_configs |> pkg_config(:cflags)
pkg_config_cflags = pkg_config(native, :cflags)
compiler_flags = resolve_compiler_flags(native.compiler_flags, native.interface, lang)
output = Toolchain.output_path(native.app, native.name, native.interface)
output_obj = output <> "_obj"
Expand Down Expand Up @@ -113,15 +113,41 @@ defmodule Bundlex.Toolchain.Common.Unix do
defp libs(native) do
lib_dirs = native.lib_dirs |> paths("-L")
libs = native.libs |> Enum.map_join(" ", fn lib -> "-l#{lib}" end)
pkg_config_libs = native.pkg_configs |> pkg_config(:libs)
pkg_config_libs = pkg_config(native, :libs)
"#{pkg_config_libs} #{lib_dirs} #{libs}"
end

defp pkg_config([], _options), do: ""
defp pkg_config(%Native{pkg_configs: []}, _options), do: ""

defp pkg_config(packages, options) do
defp pkg_config(%Native{pkg_configs: packages, app: app}, options) do
options = options |> Bunch.listify() |> Enum.map(&"--#{&1}")
{output, 0} = System.cmd("pkg-config", options ++ packages)
String.trim_trailing(output)
System.put_env("PATH", System.get_env("PATH", "") <> ":/usr/local/bin:/opt/homebrew/bin")

case System.cmd("which", ["pkg-config"]) do
{_path, 0} ->
:ok

{_path, _error} ->
Output.raise("""
pkg-config not found. Bundlex needs pkg-config to find packages in system.
On Mac OS, you can install pkg-config via Homebrew by typing `brew install pkg-config`.
""")
end

Enum.map_join(packages, " ", fn package ->
case System.cmd("pkg-config", options ++ [package], stderr_to_stdout: true) do
{output, 0} ->
String.trim_trailing(output)

{output, error} ->
Output.raise("""
Couldn't find system package #{package} with pkg-config. Check whether it's installed.
Installation instructions may be available in the readme of package #{app}.
Output from pkg-config:
Error: #{error}
#{output}
""")
end
end)
end
end
11 changes: 10 additions & 1 deletion lib/mix/tasks/compile.bundlex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,16 @@ defmodule Mix.Tasks.Compile.Bundlex do
:ok

{:error, {:run_build_script, return_code: ret, command: cmd}} ->
Output.raise("Build script:\n\n#{cmd}\n\nreturned non-zero code: #{ret}")
Output.raise("""
Failed to build the native part of package #{app}. Errors may have been logged above.
Make sure that all required packages are properly installed in your system.
Requirements and installation guide may be found in the readme of package #{app}.

Returned code: #{ret}
Build script:

#{cmd}
""")

{:error, reason} ->
Output.raise("Error running build script, reason #{inspect(reason)}")
Expand Down