From dea0eef8dcef0164293e042cc63afb18d8440859 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Mon, 22 Jul 2013 03:06:14 +0200 Subject: [PATCH] Compile the NIF directly Instead of going through `rebar compile`, do it ourselves. --- mix.exs | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/mix.exs b/mix.exs index e3ef40c..42eebb2 100644 --- a/mix.exs +++ b/mix.exs @@ -1,8 +1,29 @@ -defmodule Mix.Tasks.Compile.Rebar do - @shortdoc "Runs `rebar compile` so we build the NIF" +defmodule Mix.Tasks.Compile.Nif do + @shortdoc "Compile a NIF" def run(_) do - Mix.shell.info System.cmd("rebar compile") + config = Keyword.fetch!(Mix.project, :nif) + + file = Keyword.fetch!(config, :file) + paths = Keyword.fetch!(config, :paths) + flags = Keyword.fetch!(config, :flags) + exts = Keyword.fetch!(config, :exts) + + # Create the directory (e.g. "priv/") + file |> Path.dirname |> File.mkdir_p! + + # Figure out whether we should compile the NIF. Compare the source + # files with the target binary. If the target is newer, recompile. + to_compile = Mix.Utils.extract_files(paths, exts) + stale = Mix.Utils.extract_stale(to_compile, [file]) + case stale do + [] -> + :noop + _ -> + filesarg = Enum.join(to_compile, " ") + cmd = "gcc -shared -fpic -o #{file} #{filesarg} #{flags}" + IO.puts System.cmd(cmd) + end end end @@ -12,10 +33,18 @@ defmodule Geef.Mixfile do def project do [ app: :geef, version: "0.0.1", - compilers: [:rebar, :elixir, :app], + compilers: [:nif, :elixir, :app], + nif: nif, deps: deps ] end + def nif do + [ paths: ["c_src"], + exts: [:c], + file: "priv/geef.so", + flags: "-lgit2" ] + end + # Configuration for the OTP application def application do []