From 8bb966de2672411a700aaa7da5ec2537c492e2b4 Mon Sep 17 00:00:00 2001 From: noarkhh Date: Fri, 23 Feb 2024 16:18:03 +0100 Subject: [PATCH] Deprecate Bundlex.platform/0, improve target detection for crosscompilator --- README.md | 2 +- lib/bundlex.ex | 62 +++++++++++++------- lib/bundlex/platform.ex | 35 +++++------ lib/bundlex/toolchain/common/unix/os_deps.ex | 4 +- lib/mix/tasks/compile.bundlex.ex | 2 +- mix.exs | 2 +- 6 files changed, 63 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index e693846f..5ec701d6 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ defmodule MyApp.Mixfile do defp deps() do [ - {:bundlex, "~> 1.4"} + {:bundlex, "~> 1.5"} ] end end diff --git a/lib/bundlex.ex b/lib/bundlex.ex index ebf242a2..ce04ea70 100644 --- a/lib/bundlex.ex +++ b/lib/bundlex.ex @@ -9,7 +9,7 @@ defmodule Bundlex do @type platform_t :: :linux | :macosx | :windows32 | :windows64 | :nerves | :custom @typedoc """ - A map containing four fields that describe the platform. + A map containing four fields that describe the target platform. It consists of: * architecture - e.g. `x86_64` or `arm64` @@ -26,35 +26,53 @@ defmodule Bundlex do } @doc """ - A function returning information about the target platform (unknown in case of crosscompilation). + A function returning information about the target platform. In case of cross-compilation the + information can be provided by setting appropriate environment variables. """ @spec get_target() :: target() - if Mix.target() == :host do - def get_target() do - [architecture, vendor, os | maybe_abi] = - :erlang.system_info(:system_architecture) |> List.to_string() |> String.split("-") + case System.fetch_env("CROSSCOMPILE") do + :error -> + def get_target() do + [architecture, vendor, os | maybe_abi] = + :erlang.system_info(:system_architecture) |> List.to_string() |> String.split("-") - %{ - architecture: architecture, - vendor: vendor, - os: os, - abi: List.first(maybe_abi) || :unknown - } - end - else - def get_target() do - %{ - architecture: :unknown, - vendor: :unknown, - os: :unknown, - abi: :unknown - } - end + %{ + architecture: architecture, + vendor: vendor, + os: os, + abi: List.first(maybe_abi) || :unknown + } + end + + {:ok, _} -> + def get_target() do + unquote( + Enum.map( + [ + {:architecture, "TARGET_ARCH"}, + {:vendor, "TARGET_VENDOR"}, + {:os, "TARGET_OS"}, + {:abi, "TARGET_ABI"} + ], + fn {key, env} -> + value = + case System.fetch_env(env) do + {:ok, value} -> value + :error -> :unknown + end + + {key, value} + end + ) + ) + |> Enum.into(%{}) + end end @doc """ Returns current platform name. """ + @deprecated "Use Bundlex.get_target/0 instead" @spec platform() :: platform_t() def platform() do Platform.get_target!() diff --git a/lib/bundlex/platform.ex b/lib/bundlex/platform.ex index 0c6982f5..539e6122 100644 --- a/lib/bundlex/platform.ex +++ b/lib/bundlex/platform.ex @@ -41,24 +41,25 @@ defmodule Bundlex.Platform do Otherwise raises Mix error. """ @spec get_target!() :: name_t - mix_target = Mix.target() - - if mix_target == :host do - def get_target!(), do: get_host!() - else - def get_target!() do - case System.fetch_env("NERVES_APP") do - {:ok, _app} -> - :nerves - - :error -> - Output.warn( - "Custom MIX_TARGET #{inspect(unquote(mix_target))} is not directly supported. Make sure necessary environment variables are set correctly." - ) - - :custom + + case System.fetch_env("CROSSCOMPILE") do + :error -> + def get_target!(), do: get_host!() + + {:ok, _} -> + def get_target!() do + case System.fetch_env("NERVES_APP") do + {:ok, _app} -> + :nerves + + :error -> + Output.warn( + "Cross-compiling without using Nerves. Make sure necessary environment variables are set correctly." + ) + + :custom + end end - end end @spec get_host!() :: name_t diff --git a/lib/bundlex/toolchain/common/unix/os_deps.ex b/lib/bundlex/toolchain/common/unix/os_deps.ex index 060c9c43..14e3d662 100644 --- a/lib/bundlex/toolchain/common/unix/os_deps.ex +++ b/lib/bundlex/toolchain/common/unix/os_deps.ex @@ -2,7 +2,7 @@ defmodule Bundlex.Toolchain.Common.Unix.OSDeps do @moduledoc false require Logger - alias Bundlex.Output + alias Bundlex.{Output, Platform} @spec resolve_os_deps(Bundlex.Native.t()) :: Bundlex.Native.t() def resolve_os_deps(native) do @@ -147,7 +147,7 @@ defmodule Bundlex.Toolchain.Common.Unix.OSDeps do # TODO: pass the platform via arguments # $ORIGIN must be escaped so that it's not treated as an ENV variable - rpath_root = if Bundlex.platform() == :macosx, do: "@loader_path", else: "\\$ORIGIN" + rpath_root = if Platform.get_target!() == :macosx, do: "@loader_path", else: "\\$ORIGIN" [ "-L#{Path.join(dep_path, "lib")}", diff --git a/lib/mix/tasks/compile.bundlex.ex b/lib/mix/tasks/compile.bundlex.ex index f2f0f387..e1a76939 100644 --- a/lib/mix/tasks/compile.bundlex.ex +++ b/lib/mix/tasks/compile.bundlex.ex @@ -23,7 +23,7 @@ defmodule Mix.Tasks.Compile.Bundlex do commands = [] app = MixHelper.get_app!() - platform = Bundlex.platform() + platform = Platform.get_target!() project = with {:ok, project} <- Project.get(app) do diff --git a/mix.exs b/mix.exs index 18fefa2f..40e34e3d 100644 --- a/mix.exs +++ b/mix.exs @@ -1,7 +1,7 @@ defmodule Bundlex.Mixfile do use Mix.Project - @version "1.4.6" + @version "1.5.0" @github_url "https://github.com/membraneframework/bundlex" def project do