Skip to content

Commit

Permalink
Switched compiler to elixir_make
Browse files Browse the repository at this point in the history
  • Loading branch information
riverrun committed Dec 2, 2016
1 parent 172c0a7 commit 0d25352
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 123 deletions.
6 changes: 5 additions & 1 deletion Makefile
Expand Up @@ -26,6 +26,10 @@ NIF_SRC=\
all: $(LIB_NAME)

$(LIB_NAME): $(NIF_SRC)
mkdir -p priv
$(CC) $(CFLAGS) -shared $(LDFLAGS) $^ -o $@

.PHONY: all
clean:
rm -f $(LIB_NAME)

.PHONY: all clean
22 changes: 18 additions & 4 deletions Makefile.win
@@ -1,15 +1,29 @@
BCRYPT_PATH = c_src
!IF [where /Q Makefile.auto.win]
# The file doesn't exist, so don't include it.
!ELSE
!INCLUDE Makefile.auto.win
!IF [del /Q /F Makefile.auto.win] == 0
!ENDIF
!ENDIF

BCRYPT_PATH = c_src
NMAKE = nmake /$(MAKEFLAGS)
CFLAGS = /O2 /EHsc /I"$(BCRYPT_PATH)"

NIF_SRC=\
c_src\bcrypt_nif.c\
c_src\blowfish.c

LIB_NAME = priv\bcrypt_nif.dll
all: priv\bcrypt_nif.dll

all: $(LIB_NAME)
Makefile.auto.win:
erl -eval "io:format(\"~s~n\", [lists:concat([\"ERTS_INCLUDE_PATH=\", code:root_dir(), \"/erts-\", erlang:system_info(version), \"/include\"])])" -s init stop -noshell > $@

$(LIB_NAME):
!IFDEF ERTS_INCLUDE_PATH
priv\bcrypt_nif.dll:
if NOT EXIST "priv" mkdir "priv"
$(CC) $(CFLAGS) /I"$(ERTS_INCLUDE_PATH)" /LD /MD /Fe$@ $(NIF_SRC)
!ELSE
priv\bcrypt_nif.dll: Makefile.auto.win
$(NMAKE) /F Makefile.win priv\bcrypt_nif.dll
!ENDIF
122 changes: 6 additions & 116 deletions mix.exs
@@ -1,118 +1,7 @@
defmodule Mix.Tasks.Compile.Comeonin do
@shortdoc "Compiles Comeonin"

def run(_) do
File.rm_rf!("priv")
File.mkdir("priv")
{exec, args} = case :os.type do
{:win32, _} ->
erts_include_path = "#{:code.root_dir()}/erts-#{:erlang.system_info(:version)}/include"
# note, the && must come immediately after the include path, no whitespace
{"cmd", ["/c set ERTS_INCLUDE_PATH=#{erts_include_path}&& nmake /F Makefile.win"]}
{:unix, os} when os in [:freebsd, :openbsd] ->
{"gmake", ["all"]}
_ ->
{"make", ["all"]}
end

if System.find_executable(exec) do
build(exec, args)
Mix.Project.build_structure
:ok
else
nocompiler_error(exec)
end
end

def build(exec, args) do
{result, error_code} = System.cmd(exec, args, stderr_to_stdout: true)
IO.binwrite result
if error_code != 0, do: build_error(exec)
end

defp nocompiler_error("nmake") do
raise Mix.Error, message: nocompiler_message("nmake") <> windows_message()
end
defp nocompiler_error(exec) do
raise Mix.Error, message: nocompiler_message(exec) <> nix_message()
end

defp build_error("nmake") do
raise Mix.Error, message: build_message() <> windows_message()
end
defp build_error(_) do
raise Mix.Error, message: build_message() <> nix_message()
end

defp nocompiler_message(exec) do
"""
Could not find the program `#{exec}`.
You will need to install the C compiler `#{exec}` to be able to build
Comeonin.
"""
end

defp build_message do
"""
Could not compile Comeonin.
Please make sure that you are using Erlang / OTP version 18.0 or later
and that you have a C compiler installed.
"""
end

defp windows_message do
~S"""
One option is to install a recent version of
[Visual C++ Build Tools](http://landinghub.visualstudio.com/visual-cpp-build-tools)
either manually or using [Chocolatey](https://chocolatey.org/) -
`choco install VisualCppBuildTools`.
After installing Visual C++ Build Tools, look in the `Program Files (x86)`
folder and search for `Microsoft Visual Studio`. Note down the full path
of the folder with the highest version number.
Open the `run` command and type in the following command (make sure that
the path and version number are correct):
`cmd /K "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64`
This should open up a command prompt with the necessary environment variables set,
and from which you will be able to run the commands `mix compile`, `mix deps.compile`
and `mix test`.
See https://github.com/riverrun/comeonin/wiki/Requirements for more
information.
"""
end

defp nix_message do
"""
Please follow the directions below for the operating system you are
using:
Mac OS X: You need to have gcc and make installed. Try running the
commands `gcc --version` and / or `make --version`. If these programs
are not installed, you will be prompted to install them.
Linux: You need to have gcc and make installed. If you are using
Ubuntu or any other Debian-based system, install the packages
`build-essential`. Also install `erlang-dev` package if not
included in your Erlang/OTP version.
See https://github.com/riverrun/comeonin/wiki/Requirements for more
information.
"""
end
end

defmodule Comeonin.Mixfile do
use Mix.Project

@version "2.6.1"
@version "3.0.0"

@description """
Password hashing (bcrypt, pbkdf2_sha512) library for Elixir.
Expand All @@ -126,7 +15,7 @@ defmodule Comeonin.Mixfile do
description: @description,
package: package(),
source_url: "https://github.com/riverrun/comeonin",
compilers: [:comeonin] ++ Mix.compilers,
compilers: [:elixir_make] ++ Mix.compilers,
deps: deps(),
dialyzer: [plt_file: ".dialyzer/local.plt"]]
end
Expand All @@ -136,8 +25,9 @@ defmodule Comeonin.Mixfile do
end

defp deps do
[{:earmark, "~> 0.2", only: :dev},
{:ex_doc, "~> 0.12", only: :dev},
[{:elixir_make, "~> 0.4"},
{:earmark, "~> 1.0", only: :dev},
{:ex_doc, "~> 0.14", only: :dev},
{:dialyxir, "~> 0.3.5", only: :dev}]
end

Expand All @@ -146,6 +36,6 @@ defmodule Comeonin.Mixfile do
maintainers: ["David Whitlock"],
licenses: ["BSD"],
links: %{"GitHub" => "https://github.com/riverrun/comeonin",
"Docs" => "http://hexdocs.pm/comeonin"}]
"Docs" => "http://hexdocs.pm/comeonin"}]
end
end
5 changes: 3 additions & 2 deletions mix.lock
@@ -1,5 +1,6 @@
%{"comeonin_i18n": {:hex, :comeonin_i18n, "0.1.0"},
"dialyxir": {:hex, :dialyxir, "0.3.5", "eaba092549e044c76f83165978979f60110dc58dd5b92fd952bf2312f64e9b14", [:mix], []},
"earmark": {:hex, :earmark, "0.2.1", "ba6d26ceb16106d069b289df66751734802777a3cbb6787026dd800ffeb850f3", [:mix], []},
"ex_doc": {:hex, :ex_doc, "0.12.0", "b774aabfede4af31c0301aece12371cbd25995a21bb3d71d66f5c2fe074c603f", [:mix], [{:earmark, "~> 0.2", [hex: :earmark, optional: false]}]},
"earmark": {:hex, :earmark, "1.0.3", "89bdbaf2aca8bbb5c97d8b3b55c5dd0cff517ecc78d417e87f1d0982e514557b", [:mix], []},
"elixir_make": {:hex, :elixir_make, "0.4.0", "992f38fabe705bb45821a728f20914c554b276838433349d4f2341f7a687cddf", [:mix], []},
"ex_doc": {:hex, :ex_doc, "0.14.5", "c0433c8117e948404d93ca69411dd575ec6be39b47802e81ca8d91017a0cf83c", [:mix], [{:earmark, "~> 1.0", [hex: :earmark, optional: false]}]},
"gettext": {:hex, :gettext, "0.7.0"}}

0 comments on commit 0d25352

Please sign in to comment.