From 63424966d5fc07de209704e5eff8b7a1090264bf Mon Sep 17 00:00:00 2001 From: Mateusz Front Date: Wed, 26 Apr 2023 23:03:30 +0200 Subject: [PATCH 1/2] improve errors raised when build process fails --- lib/bundlex/toolchain/common/unix.ex | 39 +++++++++++++++++++++++----- lib/mix/tasks/compile.bundlex.ex | 11 +++++++- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/lib/bundlex/toolchain/common/unix.ex b/lib/bundlex/toolchain/common/unix.ex index 46377fd..92d0b29 100644 --- a/lib/bundlex/toolchain/common/unix.ex +++ b/lib/bundlex/toolchain/common/unix.ex @@ -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( @@ -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" @@ -113,15 +113,40 @@ 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) + + 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 diff --git a/lib/mix/tasks/compile.bundlex.ex b/lib/mix/tasks/compile.bundlex.ex index 7328c13..f2f0f38 100644 --- a/lib/mix/tasks/compile.bundlex.ex +++ b/lib/mix/tasks/compile.bundlex.ex @@ -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)}") From 222d17213fb991357eea6ad822b3a1413b993327 Mon Sep 17 00:00:00 2001 From: Mateusz Front Date: Wed, 26 Apr 2023 23:04:39 +0200 Subject: [PATCH 2/2] add homebrew installation directories to PATH needed for Livebook desktop, which doesn't have these in PATH --- lib/bundlex/toolchain/common/unix.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/bundlex/toolchain/common/unix.ex b/lib/bundlex/toolchain/common/unix.ex index 92d0b29..f9ae14e 100644 --- a/lib/bundlex/toolchain/common/unix.ex +++ b/lib/bundlex/toolchain/common/unix.ex @@ -121,6 +121,7 @@ defmodule Bundlex.Toolchain.Common.Unix do defp pkg_config(%Native{pkg_configs: packages, app: app}, options) do options = options |> Bunch.listify() |> Enum.map(&"--#{&1}") + System.put_env("PATH", System.get_env("PATH", "") <> ":/usr/local/bin:/opt/homebrew/bin") case System.cmd("which", ["pkg-config"]) do {_path, 0} ->