Skip to content

Commit

Permalink
ported code to use rebar and R14B
Browse files Browse the repository at this point in the history
  • Loading branch information
schacon committed Nov 4, 2010
1 parent fc4b19e commit c1ea72a
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@
*.so
*.o
*.swp
rebar
37 changes: 11 additions & 26 deletions 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
11 changes: 9 additions & 2 deletions geef.c → c_src/geef.c
@@ -1,8 +1,15 @@
#include "erl_nif.h"
#include <stdio.h>
#include <git/commit.h>
#include <git/tag.h>
#include <git/common.h>
#include <git/oid.h>
#include <git/errors.h>
#include <git/index.h>
#include <git/odb.h>
#include <git/oid.h>
#include <git/revwalk.h>
#include <git/repository.h>
#include <git/zlib.h>

#define MAXBUFLEN 1024

Expand All @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions ebin/geef.app
@@ -0,0 +1,14 @@
{application, geef,
[
{description, ""},
{vsn, "0.0.1"},
{modules, [
geef
]},
{registered, []},
{applications, [
kernel,
stdlib
]},
{env, []}
]}.
4 changes: 2 additions & 2 deletions 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(),
Expand Down
14 changes: 0 additions & 14 deletions geef.erl

This file was deleted.

13 changes: 13 additions & 0 deletions 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"}
]}.
21 changes: 21 additions & 0 deletions 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}).

0 comments on commit c1ea72a

Please sign in to comment.