From bed2e19ede630717e4a6c41fdbc727f63d877427 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Wed, 24 Jul 2013 23:41:22 +0200 Subject: [PATCH] Pass the args as a list to the compiler Instead of converting into a string and hoping the shell doesn't choke. --- mix.exs | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/mix.exs b/mix.exs index d8636c4..697e703 100644 --- a/mix.exs +++ b/mix.exs @@ -6,8 +6,8 @@ defmodule Mix.Tasks.Compile.Nif do file = Keyword.fetch!(config, :file) paths = Keyword.fetch!(config, :paths) - flags = Keyword.fetch!(config, :flags) - exts = Keyword.fetch!(config, :exts) + flags = Keyword.get(config, :flags, []) + exts = Keyword.get(config, :exts, [:c, :cpp]) compiler = Keyword.get(config, :compilers, ["cc", "gcc", "clang"]) |> find_compiler @@ -23,12 +23,14 @@ defmodule Mix.Tasks.Compile.Nif do [] -> :noop _ -> - cmd = - [compiler, "-shared", "-fpic", "-o #{file}", to_compile, flags] - |> List.flatten - |> Enum.join(" ") - - IO.puts System.cmd(cmd) + Mix.shell.info("* Compiling #{file}") + args = ["-shared", "-fpic", "-o", file, to_compile, flags] |> List.flatten + port = Port.open({:spawn_executable, compiler}, + [:stream, :binary, :use_stdio, :stderr_to_stdout, :hide, + :exit_status, {:args, args}]) + if do_cmd(port) != 0 do + raise Mix.Error, message: "Error compiling #{file}" + end end end @@ -41,6 +43,16 @@ defmodule Mix.Tasks.Compile.Nif do end end + defp do_cmd(port) do + receive do + {^port, {:data, data}} -> + IO.write(data) + do_cmd(port) + {^port, {:exit_status, status}} -> + status + end + end + end defmodule Geef.Mixfile do