Skip to content

Commit

Permalink
Deprecate Bundlex.platform/0, improve target detection for crosscompi…
Browse files Browse the repository at this point in the history
…lator
  • Loading branch information
Noarkhh committed Feb 23, 2024
1 parent d5e2a50 commit 8bb966d
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 44 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule MyApp.Mixfile do

defp deps() do
[
{:bundlex, "~> 1.4"}
{:bundlex, "~> 1.5"}
]
end
end
Expand Down
62 changes: 40 additions & 22 deletions lib/bundlex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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!()
Expand Down
35 changes: 18 additions & 17 deletions lib/bundlex/platform.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/bundlex/toolchain/common/unix/os_deps.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")}",
Expand Down
2 changes: 1 addition & 1 deletion lib/mix/tasks/compile.bundlex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 8bb966d

Please sign in to comment.