Skip to content

Commit ba33091

Browse files
committed
Merge branch 'master' of ssh://github.com/moai/luamongo
2 parents 03184a3 + 7d3a57b commit ba33091

18 files changed

+213
-160
lines changed

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014-2015 Francisco Zamora-Martinez (pakozm@gmail.com)
4+
Copyright (c) 2009-2015 Zipline Games, Inc.
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

Makefile

Lines changed: 90 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,106 @@
1+
UNAME:= $(shell uname)
2+
PLAT= DetectOS
3+
14
CC= g++
2-
CFLAGS= -Wall -g -O2 -shared -fPIC -I/usr/include/mongo `pkg-config --cflags lua5.2` `pkg-config --cflags libmongo-client`
5+
LUAFLAGS= $(shell pkg-config --cflags $(LUAPKG))
6+
LUAPKG:= $(shell ( luajit -e 'print("luajit")' 2> /dev/null ) || lua5.2 -e 'print("lua5.2")' 2> /dev/null || lua5.1 -e 'print("lua5.1")' 2> /dev/null || lua -e 'print("lua" .. string.match(_VERSION, "%d+.%d+"))' 2> /dev/null)
37
AR= ar rcu
48
RANLIB= ranlib
59
RM= rm -f
6-
LIBS=`pkg-config --libs lua5.2` -lmongoclient -lssl -lboost_thread -lboost_filesystem
7-
OUTLIB=mongo.so
10+
OUTLIB= mongo.so
11+
OBJS = main.o mongo_bsontypes.o mongo_dbclient.o mongo_replicaset.o mongo_connection.o mongo_cursor.o mongo_gridfile.o mongo_gridfs.o mongo_gridfschunk.o mongo_query.o utils.o mongo_cxx_extension.o mongo_gridfilebuilder.o
812

9-
LDFLAGS= $(LIBS)
13+
# macports
14+
ifneq ("$(wildcard /opt/local/include/mongo/client/dbclient.h)","")
15+
LUA:= $(shell echo $(LUAPKG) | sed 's/[0-9].[0-9]//g')
16+
VER:= $(shell echo $(LUAPKG) | sed 's/.*\([0-9].[0-9]\)/\1/g')
17+
LUAFLAGS:= $(shell pkg-config --cflags '$(LUA) >= $(VER)')
18+
MONGO_INCLUDE_DIR= /opt/local/include/mongo/
19+
MONGO_LIB_DIR= /opt/local/lib
20+
CFLAGS:= -Wall -g -O2 -fPIC $(LUAFLAGS) -I$(MONGO_INCLUDE_DIR)
21+
LIBS:= $(shell pkg-config --libs "$(LUA) >= $(VER)") -lmongoclient -lssl -lboost_thread-mt -lboost_filesystem-mt -flat_namespace -bundle -L$(MONGO_LIB_DIR) -rdynamic
22+
endif
1023

11-
OBJS = main.o mongo_bsontypes.o mongo_dbclient.o mongo_replicaset.o mongo_connection.o mongo_cursor.o mongo_gridfile.o mongo_gridfs.o mongo_gridfschunk.o mongo_query.o utils.o mongo_cxx_extension.o mongo_gridfilebuilder.o
24+
# homebrew
25+
ifneq ("$(wildcard /usr/local/include/mongo/client/dbclient.h)","")
26+
LUA:= $(shell echo $(LUAPKG) | sed 's/[0-9].[0-9]//g')
27+
VER:= $(shell echo $(LUAPKG) | sed 's/.*\([0-9].[0-9]\)/\1/g')
28+
LUAFLAGS:= $(shell pkg-config --cflags '$(LUA) >= $(VER)')
29+
MONGO_INCLUDE_DIR= /usr/local/include/mongo/
30+
MONGO_LIB_DIR= /usr/local/lib
31+
CFLAGS:= -Wall -g -O2 -fPIC $(LUAFLAGS) -I$(MONGO_INCLUDE_DIR)
32+
LIBS:= $(shell pkg-config --libs "$(LUA) >= $(VER)") -lmongoclient -lssl -lboost_thread-mt -lboost_filesystem-mt -flat_namespace -bundle -L$(MONGO_LIB_DIR) -rdynamic
33+
endif
1234

13-
UNAME = `uname`
14-
PLAT = DetectOS
35+
ifeq ("$(LIBS)", "")
36+
MONGOFLAGS:= $(shell pkg-config --cflags libmongo-client)
37+
CFLAGS:= -Wall -g -O2 -shared -fPIC -I/usr/include/mongo $(LUAFLAGS) $(MONGOFLAGS)
38+
LIBS:= $(shell pkg-config --libs $(LUAPKG)) -lmongoclient -lssl -lboost_thread -lboost_filesystem -lrt
39+
endif
1540

16-
all: $(PLAT)
41+
LDFLAGS:= $(LIBS)
1742

18-
DetectOS:
43+
all: check $(PLAT)
44+
45+
DetectOS: check
1946
@make $(UNAME)
2047

21-
Linux:
22-
@make -f Makefile.linux
48+
Linux: luamongo
49+
50+
Darwin: checkdarwin luamongo
51+
52+
check:
53+
@if [ -z $(LUAPKG) ]; then echo "Impossible to detect Lua version, you need LuaJIT, Lua 5.1 or Lua 5.2 installed!"; exit 1; fi
54+
@if [ -z $(LUAFLAGS) ]; then echo "Unable to configure with pkg-config, luamongo needs developer version of $(LUAPKG). You can force other Lua version by declaring variable LUAPKG=lua5.1 or LUAPKG=lua5.2"; exit 1; fi
2355

24-
Darwin:
25-
@make -f Makefile.darwin
56+
checkdarwin:
57+
ifeq ("$(MONGO_LIB_DIR)", "")
58+
@echo "To build luamongo on Darwin, you must have either ports or homebrew (preferred) installed!"
59+
exit 1
60+
endif
61+
62+
echo: check
63+
@echo "CC = $(CC)"
64+
@echo "CFLAGS = $(CFLAGS)"
65+
@echo "AR = $(AR)"
66+
@echo "RANLIB = $(RANLIB)"
67+
@echo "RM = $(RM)"
68+
@echo "LDFLAGS = $(LDFLAGS)"
2669

2770
clean:
2871
$(RM) $(OBJS) $(OUTLIB)
2972

30-
.PHONY: all
73+
############################################################################~
74+
75+
76+
luamongo: $(OBJS)
77+
$(CC) $(CFLAGS) $(OBJS) -o $(OUTLIB) $(LDFLAGS)
78+
79+
main.o: main.cpp utils.h
80+
$(CC) -c -o $@ $< $(CFLAGS)
81+
mongo_dbclient.o: mongo_dbclient.cpp common.h utils.h
82+
$(CC) -c -o $@ $< $(CFLAGS)
83+
mongo_connection.o: mongo_connection.cpp common.h utils.h
84+
$(CC) -c -o $@ $< $(CFLAGS)
85+
mongo_cursor.o: mongo_cursor.cpp common.h utils.h
86+
$(CC) -c -o $@ $< $(CFLAGS)
87+
mongo_gridfile.o: mongo_gridfile.cpp common.h utils.h
88+
$(CC) -c -o $@ $< $(CFLAGS)
89+
mongo_gridfs.o: mongo_gridfs.cpp common.h utils.h
90+
$(CC) -c -o $@ $< $(CFLAGS)
91+
mongo_gridfschunk.o: mongo_gridfschunk.cpp common.h utils.h
92+
$(CC) -c -o $@ $< $(CFLAGS)
93+
mongo_query.o: mongo_query.cpp common.h utils.h
94+
$(CC) -c -o $@ $< $(CFLAGS)
95+
mongo_replicaset.o: mongo_replicaset.cpp common.h utils.h
96+
$(CC) -c -o $@ $< $(CFLAGS)
97+
mongo_bsontypes.o: mongo_bsontypes.cpp common.h
98+
$(CC) -c -o $@ $< $(CFLAGS)
99+
utils.o: utils.cpp common.h utils.h
100+
$(CC) -c -o $@ $< $(CFLAGS)
101+
mongo_cxx_extension.o: mongo_cxx_extension.cpp mongo_cxx_extension.h
102+
$(CC) -c -o $@ $< $(CFLAGS)
103+
mongo_gridfilebuilder.o: mongo_gridfilebuilder.cpp mongo_cxx_extension.h common.h utils.h
104+
$(CC) -c -o $@ $< $(CFLAGS)
105+
106+
.PHONY: all check checkdarwin clean DetectOS Linux Darwin echo

Makefile.darwin

Lines changed: 0 additions & 78 deletions
This file was deleted.

Makefile.linux

Lines changed: 0 additions & 56 deletions
This file was deleted.

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
# A Lua driver for mongodb
22

3+
## Compilation
4+
5+
The makefile automatically detects which platform and Lua version are you
6+
using, so for compilation you just need to do:
7+
8+
```
9+
$ make
10+
```
11+
12+
You can force the platform compilation by using `$ make Linux` or `$ make Darwin`.
13+
Additionally, you can force the Lua version by doing:
14+
15+
```
16+
$ make LUAPKG=lua5.2
17+
```
18+
19+
where `lua5.2` can be replaced by `lua5.1` and `luajit`.
20+
21+
22+
## Installation
23+
24+
Copy the library file `mongo.so` to any of the paths in LUA_CPATH environment
25+
variable or Lua string `package.path`.
26+
327
## Wiki Documentation
428

529
* <a href="https://github.com/moai/luamongo/wiki/Bsontypes">BsonTypes</a>

common.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44
#define LUAMONGO_VERSION_STRING "_VERSION"
55

66
#define LUAMONGO_ROOT "mongo"
7+
#if LUA_VERSION_NUM < 502
8+
#define LUAMONGO_CONNECTION "mongo.Connection"
9+
#define LUAMONGO_REPLICASET "mongo.ReplicaSet"
10+
#define LUAMONGO_CURSOR "mongo.Cursor"
11+
#define LUAMONGO_QUERY "mongo.Query"
12+
#define LUAMONGO_GRIDFS "mongo.GridFS"
13+
#define LUAMONGO_GRIDFILE "mongo.GridFile"
14+
#define LUAMONGO_GRIDFSCHUNK "mongo.GridFSChunk"
15+
#define LUAMONGO_GRIDFILEBUILDER "mongo.GridFileBuilder"
16+
// not an actual class, pseudo-base for error messages
17+
#define LUAMONGO_DBCLIENT "mongo.DBClient"
18+
#else
719
#define LUAMONGO_CONNECTION "Connection"
820
#define LUAMONGO_REPLICASET "ReplicaSet"
921
#define LUAMONGO_CURSOR "Cursor"
@@ -12,9 +24,9 @@
1224
#define LUAMONGO_GRIDFILE "GridFile"
1325
#define LUAMONGO_GRIDFSCHUNK "GridFSChunk"
1426
#define LUAMONGO_GRIDFILEBUILDER "GridFileBuilder"
15-
1627
// not an actual class, pseudo-base for error messages
1728
#define LUAMONGO_DBCLIENT "DBClient"
29+
#endif
1830

1931
#define LUAMONGO_ERR_CONNECTION_FAILED "Connection failed: %s"
2032
#define LUAMONGO_ERR_REPLICASET_FAILED "ReplicaSet.New failed: %s"

mongo_bsontypes.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,11 @@ int mongo_bsontypes_register(lua_State *L) {
375375
{NULL, NULL}
376376
};
377377

378-
//luaL_register(L, LUAMONGO_ROOT, bsontype_methods);
378+
#if LUA_VERSION_NUM < 502
379+
luaL_register(L, LUAMONGO_ROOT, bsontype_methods);
380+
#else
379381
luaL_newlib(L, bsontype_methods);
382+
#endif
380383

381384
return 1;
382385
}

mongo_connection.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,11 @@ int mongo_connection_register(lua_State *L) {
120120

121121
lua_pop(L,1);
122122

123-
//luaL_register(L, LUAMONGO_CONNECTION, connection_class_methods);
123+
#if LUA_VERSION_NUM < 502
124+
luaL_register(L, LUAMONGO_CONNECTION, connection_class_methods);
125+
#else
124126
luaL_newlib(L, connection_class_methods);
127+
#endif
125128

126129
return 1;
127130
}

mongo_cursor.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,11 @@ int mongo_cursor_register(lua_State *L) {
194194

195195
lua_pop(L,1);
196196

197-
//luaL_register(L, LUAMONGO_CURSOR, cursor_class_methods);
197+
#if LUA_VERSION_NUM < 502
198+
luaL_register(L, LUAMONGO_CURSOR, cursor_class_methods);
199+
#else
198200
luaL_newlib(L, cursor_class_methods);
199-
201+
#endif
202+
200203
return 1;
201204
}

mongo_cxx_extension.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
* IN THE SOFTWARE.
2121
*/
2222

23+
#include <iostream>
24+
#include <stdlib.h>
2325
#include <client/redef_macros.h>
2426
#include <client/dbclient.h>
2527
#include <client/gridfs.h>

0 commit comments

Comments
 (0)