Skip to content

Commit

Permalink
Fix crosscompile (#126)
Browse files Browse the repository at this point in the history
* fix invalid expression error

* fix 'custom' family
  • Loading branch information
mat-hek authored Mar 29, 2024
1 parent a03b048 commit d79c328
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 71 deletions.
27 changes: 9 additions & 18 deletions lib/bundlex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,16 @@ defmodule Bundlex do
}
end

{:ok, _} ->
{:ok, _crosscompile} ->
target =
Map.new(
[
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
)
[
architecture: "TARGET_ARCH",
vendor: "TARGET_VENDOR",
os: "TARGET_OS",
abi: "TARGET_ABI"
]
|> Map.new(fn {key, env} -> {key, System.get_env(env, "unknown")} end)
|> Macro.escape()

def get_target() do
unquote(target)
Expand Down
17 changes: 5 additions & 12 deletions lib/bundlex/build_script.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule Bundlex.BuildScript do
end

def run(%__MODULE__{commands: commands}, platform) do
family = platform |> family!()
family = Platform.family(platform)
cmd = commands |> join_commands(family)

case cmd |> Mix.shell().cmd() do
Expand All @@ -44,7 +44,7 @@ defmodule Bundlex.BuildScript do

@spec store(t, Platform.name_t(), String.t()) :: {:ok, {String.t(), String.t()}}
def store(%__MODULE__{commands: commands}, platform, name \\ "bundlex") do
family = platform |> family!()
family = Platform.family(platform)
script_name = name <> @script_ext[family]
script_prefix = @script_prefix[family]
script = script_prefix <> (commands |> join_commands(family))
Expand All @@ -55,18 +55,11 @@ defmodule Bundlex.BuildScript do
end
end

defp join_commands(commands, :unix) do
Enum.map_join(commands, " && \\\n", &"(#{&1})") <> "\n"
end

defp join_commands(commands, :windows) do
Enum.join(commands, "\r\n") <> "\r\n"
end

defp family!(:windows32), do: :windows
defp family!(:windows64), do: :windows
defp family!(:macosx), do: :unix
defp family!(:linux), do: :unix
defp family!(:freebsd), do: :unix
defp family!(:nerves), do: :unix
defp join_commands(commands, _other) do
Enum.map_join(commands, " && \\\n", &"(#{&1})") <> "\n"
end
end
82 changes: 41 additions & 41 deletions lib/bundlex/platform.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,48 @@ defmodule Bundlex.Platform do

case System.fetch_env("CROSSCOMPILE") do
:error ->
def get_target!(), do: get_host!()
def get_target!() do
case :os.type() do
{:win32, _} ->
{:ok, reg} = :win32reg.open([:read])

:ok =
:win32reg.change_key(
reg,
~c"\\hklm\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"
)

{:ok, build} = :win32reg.value(reg, ~c"BuildLabEx")

platform_name =
if build |> to_string |> String.contains?("amd64") do
:windows64
else
:windows32
end

:ok = :win32reg.close(reg)

platform_name

{:unix, :linux} ->
:linux

{:unix, :freebsd} ->
:freebsd

{:unix, :darwin} ->
:macosx

other ->
# TODO add detection for more platforms
Output.raise(
"Unable to detect current platform. Erlang returned #{inspect(other)} which I don't know how to handle."
)
end
end

{:ok, _} ->
{:ok, _crosscompile} ->
def get_target!() do
case System.fetch_env("NERVES_APP") do
{:ok, _app} ->
Expand All @@ -62,45 +101,6 @@ defmodule Bundlex.Platform do
end
end

@spec get_host!() :: name_t
defp get_host!() do
case :os.type() do
{:win32, _} ->
{:ok, reg} = :win32reg.open([:read])

:ok =
:win32reg.change_key(reg, ~c"\\hklm\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion")

{:ok, build} = :win32reg.value(reg, ~c"BuildLabEx")

platform_name =
if build |> to_string |> String.contains?("amd64") do
:windows64
else
:windows32
end

:ok = :win32reg.close(reg)

platform_name

{:unix, :linux} ->
:linux

{:unix, :freebsd} ->
:freebsd

{:unix, :darwin} ->
:macosx

other ->
# TODO add detection for more platforms
Output.raise(
"Unable to detect current platform. Erlang returned #{inspect(other)} which I don't know how to handle."
)
end
end

@spec family(name_t) :: family_name_t
def family(:windows32), do: :windows
def family(:windows64), do: :windows
Expand Down

0 comments on commit d79c328

Please sign in to comment.