diff --git a/.gitignore b/.gitignore index dbd4f00..7ce0a41 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.so *.o *.swp +rebar diff --git a/Makefile b/Makefile index 9ead2d5..b6af23f 100644 --- a/Makefile +++ b/Makefile @@ -1,30 +1,15 @@ -OTPROOT=/Users/schacon/projects/otp -INCLUDES = -I$(OTPROOT)/erts/emulator/beam/ +REBAR=$(shell which rebar || echo ./rebar) -# OS X flags. -GCCFLAGS = -O3 -fPIC -bundle -flat_namespace -undefined suppress -fno-common -Wall -m32 +all: compile -LIBS = -lz -lgit2 +./rebar: + erl -noshell -s inets start \ + -eval 'httpc:request(get, {"http://hg.basho.com/rebar/downloads/rebar", []}, [], [{stream, "./rebar"}])' \ + -s init stop + chmod +x ./rebar -# Linux Flags -#GCCFLAGS = -O3 -fPIC -shared -fno-common -Wall - -CFLAGS = $(GCCFLAGS) $(INCLUDES) -LDFLAGS = $(GCCFLAGS) $(LIBS) - -OBJECTS = geef.o - -DRIVER = geef.so -BEAM = geef.beam - -all: $(DRIVER) $(BEAM) - -clean: - rm -f *.o *.beam $(DRIVER) - -$(DRIVER): $(OBJECTS) - gcc -o $@ $^ $(LDFLAGS) - -$(BEAM): geef.erl - erlc $^ +compile: $(REBAR) + @$(REBAR) compile +clean: $(REBAR) + @$(REBAR) clean diff --git a/geef.c b/c_src/geef.c similarity index 87% rename from geef.c rename to c_src/geef.c index da18d29..9f63cbe 100644 --- a/geef.c +++ b/c_src/geef.c @@ -1,8 +1,15 @@ #include "erl_nif.h" #include +#include +#include #include -#include +#include +#include #include +#include +#include +#include +#include #define MAXBUFLEN 1024 @@ -19,7 +26,7 @@ geef_hex_to_raw(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) git_oid_mkstr(&oid, sha); ErlNifBinary ibin; - enif_alloc_binary(env, 20, &ibin); + enif_alloc_binary(20, &ibin); memcpy(ibin.data, (&oid)->id, 20); return enif_make_binary(env, &ibin); diff --git a/ebin/geef.app b/ebin/geef.app new file mode 100644 index 0000000..a7b9978 --- /dev/null +++ b/ebin/geef.app @@ -0,0 +1,14 @@ +{application, geef, + [ + {description, ""}, + {vsn, "0.0.1"}, + {modules, [ + geef + ]}, + {registered, []}, + {applications, [ + kernel, + stdlib + ]}, + {env, []} + ]}. diff --git a/geef b/geef index fad5766..7485a95 100755 --- a/geef +++ b/geef @@ -1,6 +1,6 @@ -#!/Users/schacon/projects/otp/bin/escript +#!/usr/local/Cellar/erlang/R14B/bin/escript %% -*- erlang -*- -%%! debug verbose -noshell +%%! debug verbose -noshell -pa ebin main([Command|_Args]) -> geef:start(), diff --git a/geef.erl b/geef.erl deleted file mode 100644 index ba7420c..0000000 --- a/geef.erl +++ /dev/null @@ -1,14 +0,0 @@ --module(geef). --export([start/0, hex_to_raw/1, object_exists/2]). - -start() -> - erlang:load_nif("geef", 0). - -hex_to_raw(_Val) -> - nif_error(?LINE). - -object_exists(_Val, _Val2) -> - nif_error(?LINE). - -nif_error(Line) -> - exit({nif_not_loaded,module,?MODULE,line,Line}). diff --git a/rebar.config b/rebar.config new file mode 100644 index 0000000..97e4a61 --- /dev/null +++ b/rebar.config @@ -0,0 +1,13 @@ +{so_name, "geef.so"}. + +{port_envs, [ + {"DRV_LDFLAGS", "$DRV_LDFLAGS -lz -lgit2"}, + + %% OS X Leopard flags for 64-bit + {"darwin9.*-64$", "CXXFLAGS", "-m64"}, + {"darwin9.*-64$", "LDFLAGS", "-arch x86_64"}, + + %% OS X Snow Leopard flags for 32-bit + {"darwin10.*-32$", "CXXFLAGS", "-m32"}, + {"darwin10.*-32$", "LDFLAGS", "-arch i386"} +]}. diff --git a/src/geef.erl b/src/geef.erl new file mode 100644 index 0000000..7335c47 --- /dev/null +++ b/src/geef.erl @@ -0,0 +1,21 @@ +-module(geef). +-export([start/0, hex_to_raw/1, object_exists/2]). + +start() -> + case code:priv_dir(geef) of + {error, bad_name} -> + SoName = filename:join("priv", geef); + Dir -> + SoName = filename:join(Dir, geef) + end, + io:format("Hey There: ~s~n", [SoName]), + erlang:load_nif(SoName, 0). + +hex_to_raw(_Val) -> + nif_error(?LINE). + +object_exists(_Val, _Val2) -> + nif_error(?LINE). + +nif_error(Line) -> + exit({nif_not_loaded,module,?MODULE,line,Line}).