Skip to content

Commit

Permalink
Merge pull request #23 from maltoe/fix-installer-architecture-detection
Browse files Browse the repository at this point in the history
Fix architecture detection in installer task
  • Loading branch information
pepicrft authored May 15, 2024
2 parents 7c63482 + e9e9044 commit d066450
Showing 1 changed file with 77 additions and 22 deletions.
99 changes: 77 additions & 22 deletions lib/lightning_css/architectures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,89 @@ defmodule LightningCSS.Architectures do
"""

# Available targets: https://registry.npmjs.org/lightningcss-cli/latest
# credo:disable-for-next-line
#
# For 1.24.1:
# lightningcss-cli-darwin-x64
# lightningcss-cli-linux-x64-gnu
# lightningcss-cli-win32-x64-msvc
# lightningcss-cli-darwin-arm64
# lightningcss-cli-linux-arm64-gnu
# lightningcss-cli-linux-arm-gnueabihf
# lightningcss-cli-linux-arm64-musl
# lightningcss-cli-linux-x64-musl
# lightningcss-cli-freebsd-x64

def target do
case :os.type() do
# Assuming it's an x86 CPU
{:win32, _} ->
wordsize = :erlang.system_info(:wordsize)

if wordsize == 8 do
"win32-x64"
else
"win32-ia32"
end
{:win32, _} -> target(:win32)
{:unix, :darwin} -> target(:darwin)
{:unix, :linux} -> target(:linux)
_ -> unsupported_os()
end
end

defp target(:win32) do
only_64bits()

"win32-x64-msvc"
end

defp target(:darwin) do
only_64bits()

case arch_info() do
{"arm", _} -> "darwin-arm64"
{"x86_64", _} -> "darwin-x64"
_ -> unsupported_arch()
end
end

defp target(:linux) do
{arch, toolchain} = arch_info()

{:unix, osname} ->
arch_str = :erlang.system_info(:system_architecture)
[arch | _] = arch_str |> List.to_string() |> String.split("-")
if arch == "arm" && toolchain == "gnueabihf" do
"linux-arm-gnueabihf"
else
only_64bits()

arch =
case arch do
"amd64" -> "#{osname}-x64"
"x86_64" -> "#{osname}-x64"
"i686" -> "#{osname}-ia32"
"i386" -> "#{osname}-ia32"
"aarch64" -> "#{osname}-arm64"
"arm" when osname == :darwin -> "darwin-arm64"
"arm" -> "#{osname}-arm"
"armv7" <> _ -> "#{osname}-arm"
_ -> raise "lightning_css is not available for architecture: #{arch_str}"
"amd64" -> "x64"
"x86_64" -> "x64"
"arm" -> "arm64"
"arm64" -> "arm64"
_ -> unsupported_arch()
end

unless toolchain in ~w[gnu musl] do
unsupported_arch()
end

"linux-#{arch}-#{toolchain}"
end
end

defp arch_info do
arch_info =
:system_architecture
|> :erlang.system_info()
|> to_string()
|> String.split("-")

{List.first(arch_info), List.last(arch_info)}
end

defp only_64bits do
if :erlang.system_info(:wordsize) != 8 do
raise "lightning_css is not available for a non 64-bit operating system"
end
end

defp unsupported_os do
raise "lightning_css is not available for operating system: #{inspect(:os.type())}"
end

defp unsupported_arch do
raise "lightning_css is not available for architecture: #{:erlang.system_info(:system_architecture)}"
end
end

0 comments on commit d066450

Please sign in to comment.