From abfc51a2af8d606583450068745e726436a65877 Mon Sep 17 00:00:00 2001 From: Vignesh Rajagopalan Date: Fri, 27 Nov 2015 04:36:13 +0530 Subject: [PATCH] Add a hacky os x build solution --- mix.exs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/mix.exs b/mix.exs index faf09a1..5c04fd7 100644 --- a/mix.exs +++ b/mix.exs @@ -5,7 +5,7 @@ defmodule GraphQL.Parser.Mixfile do [app: :graphql_parser, version: "0.0.1", elixir: "~> 1.1", - compilers: [:libgraphqlparser, :elixir, :app], + compilers: [:libgraphqlparser, :graphqlparsernif, :elixir, :app], build_embedded: Mix.env == :prod, start_permanent: Mix.env == :prod, deps: deps] @@ -20,8 +20,22 @@ defmodule GraphQL.Parser.Mixfile do end end +defmodule GraphQL.Parser.MixHelper do + def check_exit_status({res, exit_status_code}) do + if exit_status_code != 0 do + raise Mix.Error, message: """ + Build command exited with status code: #{exit_status_code}. + Make sure you're running `mix compile` from project's root. + """ + end + + IO.binwrite res # verbose + end +end + defmodule Mix.Tasks.Compile.Libgraphqlparser do use Mix.Task + import GraphQL.Parser.MixHelper @shortdoc "Compiles libgraphqlparser" @@ -49,15 +63,40 @@ defmodule Mix.Tasks.Compile.Libgraphqlparser do end end end +end - defp check_exit_status({res, exit_status_code}) do - if exit_status_code != 0 do - raise Mix.Error, message: """ - Build command exited with status code: #{exit_status_code}. - Make sure you're running `mix compile` from project's root. - """ - end +defmodule Mix.Tasks.Compile.Graphqlparsernif do + use Mix.Task + import GraphQL.Parser.MixHelper - IO.binwrite res # verbose + @shortdoc "Compiles the GraphQL.Parser NIF library" + + def run(_) do + + compiler_inputs = [ + "-undefined", "dynamic_lookup", "-dynamiclib", + "-I", "/usr/local/include/graphqlparser", + "-I", "#{:code.root_dir ++ '/erts-' ++ :erlang.system_info(:version) ++ '/include' |> to_string}", + "-L", "/usr/local/lib", + "-l", "graphqlparser", + "src/graphqlparser_nif.c", + "-o", "graphqlparser.so" + ] + + try do + System.cmd("gcc", compiler_inputs, stderr_to_stdout: true) + |> check_exit_status + + rescue + e in Mix.Error -> + raise e + e in ErlangError -> + case e.original do + :enoent -> + raise Mix.Error, message: """ + Please check if `cmake` and `make` are installed + """ + end + end end end