From c0fc38ebf54e02fb6a1eab612a39b158d4fb0881 Mon Sep 17 00:00:00 2001 From: Benedikt Reinartz Date: Thu, 21 May 2020 12:49:49 +0200 Subject: [PATCH] Update erlang-cargo for partial compiles and take release flag into account --- rustler_mix/lib/rustler/compiler.ex | 23 ++++++++++- rustler_mix/lib/rustler/compiler/server.ex | 45 ---------------------- rustler_mix/mix.lock | 2 +- 3 files changed, 22 insertions(+), 48 deletions(-) delete mode 100644 rustler_mix/lib/rustler/compiler/server.ex diff --git a/rustler_mix/lib/rustler/compiler.ex b/rustler_mix/lib/rustler/compiler.ex index c5b5bb4ba..6ee8e7fed 100644 --- a/rustler_mix/lib/rustler/compiler.ex +++ b/rustler_mix/lib/rustler/compiler.ex @@ -12,9 +12,9 @@ defmodule Rustler.Compiler do crate = ensure_string(Keyword.fetch!(opts, :crate)) config = Config.from(otp_app, module, opts) - artifacts = Server.build() + is_release = config.mode == :release + artifacts = do_compile(crate, is_release) - is_release = Mix.env() in [:prod, :bench] entry = artifacts[crate] is_lib = :cargo_artifact.kind(entry) == :cdylib @@ -77,4 +77,23 @@ defmodule Rustler.Compiler do defp ensure_string(str) when is_binary(str) do str end + + defp do_compile(crate, is_release) do + is_release = Mix.env() in [:prod, :bench] + + cargo_opts = %{ + release: is_release + } + + Mix.shell().info("Starting build in #{File.cwd!()}") + + cargo = :cargo.init(File.cwd!(), cargo_opts) + + artifacts = :cargo.build(cargo, crate) + + # This drops the unique key in favour of the crate name + artifacts = + artifacts + |> Map.new(&{:cargo_artifact.name(&1), &1}) + end end diff --git a/rustler_mix/lib/rustler/compiler/server.ex b/rustler_mix/lib/rustler/compiler/server.ex deleted file mode 100644 index 1808a4a61..000000000 --- a/rustler_mix/lib/rustler/compiler/server.ex +++ /dev/null @@ -1,45 +0,0 @@ -defmodule Rustler.Compiler.Server do - use GenServer - - defp ensure_running() do - case GenServer.start(__MODULE__, [], name: __MODULE__) do - {:ok, _pid} -> :ok - {:error, {:already_started, _pid}} -> :ok - end - end - - def build() do - ensure_running() - GenServer.call(__MODULE__, :compile, :infinity) - end - - @impl true - def init([]) do - {:ok, nil} - end - - @impl true - def handle_call(:compile, _from, nil) do - is_release = Mix.env() in [:prod, :bench] - - cargo_opts = %{ - release: is_release - } - - Mix.shell().info("Starting build in #{File.cwd!()}") - - cargo = :cargo.init(File.cwd!(), cargo_opts) - artifacts = :cargo.build(cargo) - - # This drops the unique key in favour of the crate name - artifacts = - artifacts - |> Map.new(&{:cargo_artifact.name(&1), &1}) - - {:reply, artifacts, artifacts} - end - - def handle_call(:compile, _from, artifacts) do - {:reply, artifacts, artifacts} - end -end diff --git a/rustler_mix/mix.lock b/rustler_mix/mix.lock index 57f4fa9e2..bb376bb33 100644 --- a/rustler_mix/mix.lock +++ b/rustler_mix/mix.lock @@ -1,5 +1,5 @@ %{ - "cargo": {:git, "https://github.com/rusterlium/erlang-cargo.git", "0e612bf1e321c6ae062b98f9079f24b6bedfba01", []}, + "cargo": {:git, "https://github.com/rusterlium/erlang-cargo.git", "7992e2c159255a421d83c2a693b544c20979c841", []}, "earmark": {:hex, :earmark, "1.4.4", "4821b8d05cda507189d51f2caeef370cf1e18ca5d7dfb7d31e9cafe6688106a4", [:mix], [], "hexpm", "1f93aba7340574847c0f609da787f0d79efcab51b044bb6e242cae5aca9d264d"}, "ex_doc": {:hex, :ex_doc, "0.21.3", "857ec876b35a587c5d9148a2512e952e24c24345552259464b98bfbb883c7b42", [:mix], [{:earmark, "~> 1.4", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "0db1ee8d1547ab4877c5b5dffc6604ef9454e189928d5ba8967d4a58a801f161"}, "jsx": {:hex, :jsx, "2.10.0", "77760560d6ac2b8c51fd4c980e9e19b784016aa70be354ce746472c33beb0b1c", [:rebar3], [], "hexpm", "9a83e3704807298016968db506f9fad0f027de37546eb838b3ae1064c3a0ad62"},