diff --git a/.hgignore b/.hgignore index 704b32a..2160db5 100644 --- a/.hgignore +++ b/.hgignore @@ -2,3 +2,6 @@ syntax: glob dist/ src/geoipport libgeoip-* +priv/ +ebin/ +c_src/*.o diff --git a/README b/README index 11e1383..70d1406 100644 --- a/README +++ b/README @@ -2,6 +2,7 @@ who: matt@genges.com what: erlang port for libgeoip. supports GeoIPCity and GeoLiteCity DBs. when: made December 2008. published December 2008. +when2: Last updated October 2010. Rebar-ized and added license. where: everywhere why: I don't like having to do external service calls for geo lookups. @@ -24,12 +25,16 @@ benchmark versus egeoip: egeoip: 5498 lookups per second resulting in libgeoip-erlang being about 28% faster than egeoip. -installation: build it. - edit src/Makefile if erlang isn't in /usr/local/lib/erlang - put everything in dist/ where you need it to be +Building Instructions: + > rebar compile + You may need to edit rebar.config if your maxmind headers and + objects aren't in /opt/local. + +Note: You must have the libgeoip directory name *in* your path for libgeoip to +find the correct port to run. usage [APPLICATION]: -% erl -pz ./libgeoip-erlang/libgeoip-VER/ebin/ +> erl -pz ../ligeoip-erlang/ebin -pz ../libgeoip-erlang/priv Eshell V5.6.5 (abort with ^G) 1> application:start(libgeoip_app). @@ -44,7 +49,7 @@ db_set usage [DIRECT]: -% erl -pz ./libgeoip-erlang/libgeoip-1.0/ebin/ +> erl -pz ../libgeoip-erlang/ebin -pz ../libgeoip-erlang/priv Eshell V5.6.5 (abort with ^G) 1> libgeoip:start_link("/usr/local/maxmind/data/GeoLiteCity.dat"). diff --git a/c_src/Makefile b/c_src/Makefile new file mode 100644 index 0000000..0574efb --- /dev/null +++ b/c_src/Makefile @@ -0,0 +1,23 @@ +.PHONY: dist clean + +## - if you have multiple versions, pick the highest one +OTP_LIB_DIR:= $(shell erl -noshell -eval 'io:format("~s", [code:lib_dir()])' -s erlang halt) +EI=$(shell ls -d $(OTP_LIB_DIR)/erl_interface* | sort -n |tail -1) +LIBGEOIP=../priv/libgeoip + +## - default macports install location for libgeoip +GEOIP_LIBS=/opt/local + +dist: $(LIBGEOIP) + +$(LIBGEOIP): geoipport.c erl_comm.c geohash.c Makefile + @mkdir -p ../priv + gcc -Wall -Os -L$(GEOIP_LIBS)/lib -I$(GEOIP_LIBS)/include \ +-L$(EI)/lib/ -I$(EI)/include/ \ +-o $(LIBGEOIP) erl_comm.c geoipport.c geohash.c -lerl_interface -lei -lGeoIP \ +-lpthread + +## - Comments are fun + +clean: + rm -f $(LIBGEOIP) diff --git a/src/erl_comm.c b/c_src/erl_comm.c similarity index 100% rename from src/erl_comm.c rename to c_src/erl_comm.c diff --git a/src/geohash.c b/c_src/geohash.c similarity index 96% rename from src/geohash.c rename to c_src/geohash.c index 16320ac..7e58f85 100644 --- a/src/geohash.c +++ b/c_src/geohash.c @@ -2,6 +2,7 @@ | direct JS -> C port of encodeGeoHash (ugly formatting and all) from: | http://github.com/davetroy/geohash-js/tree/master/geohash.js | no decoding or adjacency finding implemented (or required) yet + | geohash.js is MIT licensed as is this translation of it. */ int BITS[] = {16, 8, 4, 2, 1}; diff --git a/c_src/geoipport b/c_src/geoipport new file mode 100755 index 0000000..c63b91a Binary files /dev/null and b/c_src/geoipport differ diff --git a/src/geoipport.c b/c_src/geoipport.c similarity index 100% rename from src/geoipport.c rename to c_src/geoipport.c diff --git a/src/libgeoip.hrl b/include/libgeoip.hrl similarity index 100% rename from src/libgeoip.hrl rename to include/libgeoip.hrl diff --git a/rebar.config b/rebar.config new file mode 100644 index 0000000..764e7c6 --- /dev/null +++ b/rebar.config @@ -0,0 +1,6 @@ +{port_envs, [{"CFLAGS", "-Wall -I/opt/local/include "}, + {"LDFLAGS", "-L/opt/local/lib "}]}. + +% This is a hack because I can't make rebar compile non-linked-in drivers. +{port_pre_script, {"gmake -C c_src", "priv/libgeoip"}}. +{port_cleanup_script, "gmake -C c_src clean"}. diff --git a/src/Makefile b/src/Makefile deleted file mode 100644 index 5999407..0000000 --- a/src/Makefile +++ /dev/null @@ -1,50 +0,0 @@ -.PHONY: dist clean - -## - if you have multiple versions, pick the highest one -OTP_LIB_DIR:= $(shell erl -noshell -eval 'io:format("~s", [code:lib_dir()])' -s erlang halt) -EI=$(shell ls -d $(OTP_LIB_DIR)/erl_interface* | sort -n |tail -1) -VER=1.0.1 -DIST=../libgeoip-$(VER) -LIBGEOIP=geoipport - -## - default macports install location for libgeoip -GEOIP_LIBS=/opt/local - -.SUFFIXES: .beam .erl -ERL_FILES=$(wildcard *.erl) -DIST_ERL=$(ERL_FILES:%erl=$(DIST)/ebin/%beam) - - -dist: $(DIST) $(DIST_ERL) $(LIBGEOIP) $(DIST)/ebin/libgeoip_app.app \ -$(DIST)/priv/$(LIBGEOIP) $(DIST)/include/libgeoip.hrl - -## - Setting up directory structure - -$(DIST): - mkdir -p $(DIST)/{src,ebin,priv,include} - -$(DIST)/priv/$(LIBGEOIP): $(LIBGEOIP) - cp $(LIBGEOIP) $(DIST)/priv - -$(DIST)/include/libgeoip.hrl: libgeoip.hrl - cp libgeoip.hrl $(DIST)/include - -$(DIST)/ebin/libgeoip_app.app: libgeoip_app.app - cp $< $(dir $@) - -## - Compiling erl and the port - -$(DIST)/ebin/%.beam: %.erl Makefile - erlc -W +debug_info -o $(dir $@) $< - cp $< $(DIST)/src - -$(LIBGEOIP): geoipport.c erl_comm.c geohash.c Makefile - gcc -Wall -Os -L$(GEOIP_LIBS)/lib -I$(GEOIP_LIBS)/include \ --L$(EI)/lib/ -I$(EI)/include/ \ --o $(LIBGEOIP) erl_comm.c geoipport.c geohash.c -lerl_interface -lei -lGeoIP \ --lpthread - -## - Comments are fun - -clean: - rm -rf $(LIBGEOIP) $(DIST) diff --git a/src/libgeoip_app.app b/src/libgeoip.app.src similarity index 56% rename from src/libgeoip_app.app rename to src/libgeoip.app.src index f2f9b09..42c1808 100644 --- a/src/libgeoip_app.app +++ b/src/libgeoip.app.src @@ -1,7 +1,7 @@ -{application, libgeoip_app, +{application, libgeoip, [{description, "libgeoip port"}, - {vsn, "1.0.1"}, - {modules, [libgeoip_app, libgeoip_sup, libgeoip]}, + {vsn, "1.0.2"}, + {modules, []}, {registered, [libgeoip]}, {applications, [kernel, stdlib]}, {mod, {libgeoip_app,[]}} diff --git a/src/libgeoip.erl b/src/libgeoip.erl index 988a79b..1a7b6df 100644 --- a/src/libgeoip.erl +++ b/src/libgeoip.erl @@ -23,7 +23,8 @@ init(DBName) -> {error, bad_name} -> ""; Found -> Found ++ "/" end, - GeoIP = open_port({spawn, Path ++ "geoipport"}, [{packet, 2}, binary]), + io:format("Path is: ~p~n", [Path]), + GeoIP = open_port({spawn, Path ++ "libgeoip"}, [{packet, 2}, binary]), process_flag(trap_exit, true), case DBName of [] -> ok;