Skip to content

Instantly share code, notes, and snippets.

@LostKobrakai

LostKobrakai/crisscross.ex Secret

Created Nov 6, 2020
Embed
What would you like to do?
defmodule CrissCross do
@moduledoc """
CrissCross allows for simple cross compilation using hex precompiled beam packages.
This does work only for projects, which don't have any further native dependencies besides the Erlang Runtime System (ERTS).
## Available OSs
* Ubuntu 14.04
* Ubuntu 16.04
* Ubuntu 18.04
* Ubuntu 20.04
## Usage
In your `mix.exs` add the following:
def project do
[
…,
releases: releases()
]
end
def releases do
[
ubuntu_14: [
include_erts: fn -> CrissCross.ubuntu(14, "OTP-23.1.2") end
],
ubuntu_16: [
include_erts: fn -> CrissCross.ubuntu(16, "OTP-23.1.2") end
]
]
end
"""
@base "_build/erts"
def ubuntu(os_version, otp_version) do
os = validate_os_version!(os_version)
file = Path.join([@base, os, "#{otp_version}.tar.gz"])
root = Path.dirname(file)
File.mkdir_p!(root)
dir = Path.rootname(file, ".tar.gz")
Mix.shell().info("Cross compiling for #{os} using #{otp_version}.")
unless File.dir?(dir) do
Mix.shell().info("Downloading precompiled beam…")
{:ok, :saved_to_file} =
:httpc.request(
:get,
{~c"https://repo.hex.pm/builds/otp/#{os}/#{otp_version}.tar.gz", []},
[],
body_format: :binary,
stream: ~c"#{file}"
)
Mix.shell().info("Extracting files…")
extract_tar(~c"#{file}", ~c"#{root}")
File.rm!(file)
end
[erts] = Path.wildcard(Path.join(dir, "erts-*"))
erts
end
defp validate_os_version!(14), do: "ubuntu-14.04"
defp validate_os_version!(16), do: "ubuntu-16.04"
defp validate_os_version!(18), do: "ubuntu-18.04"
defp validate_os_version!(20), do: "ubuntu-20.04"
defp validate_os_version!(other) do
raise "Invalid ubuntu version: #{other}"
end
defp extract_tar(filename, root) do
:erl_tar.extract(filename, [:compressed, cwd: root])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment