Skip to content

Commit

Permalink
Makefile for folks who like make.
Browse files Browse the repository at this point in the history
  • Loading branch information
banker committed Mar 20, 2012
1 parent 8cb5a25 commit 8180914
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 0 deletions.
172 changes: 172 additions & 0 deletions Makefile
@@ -0,0 +1,172 @@
# MongoDB C Driver Makefile
#
# Copyright 2009, 2010 10gen Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Version
MONGO_MAJOR=0
MONGO_MINOR=4
BSON_MAJOR=$(MONGO_MAJOR)
BSON_MINOR=$(MONGO_MINOR)

# Library names
MONGO_LIBNAME=libmongoc
BSON_LIBNAME=libbson

# TODO: add replica set test, cpp test, platform tests
TESTS=test/auth_test test/bson_test test/bson_subobject_test test/count_delete \
test/cursors_test test/endian_swap_test test/errors_test test/examples_test \
test/functions_test test/gridfs_test test/helpers_test test/json_test \
test/oid_test test/resize_test test/simple_test test/sizes_test test/update_test \
test/validate_test
MONGO_OBJECTS=src/bson.o src/encoding.o src/gridfs.o src/md5.o src/mongo.o \
src/numbers.o src/env_posix.o
BSON_OBJECTS=src/bson.o src/numbers.o

# Compile flags
ALL_DEFINES=$(DEFINES)
ALL_DEFINES+=-DMONGO_USE_GETADDRINFO
CC:=$(shell sh -c 'type $(CC) >/dev/null 2>/dev/null && echo $(CC) || echo gcc')

# Endianness check
endian := $(shell sh -c 'echo "ab" | od -x | grep "6261" >/dev/null && echo little || echo big')
ifeq ($(endian),big)
ALL_DEFINES+=-DMONGO_BIG_ENDIAN
endif

# Posix settings

# Int64 type check
int64:=$(shell ./check_int64.sh $(CC) stdint.h && echo stdint)
ifeq ($(int64),stdint)
ALL_DEFINES+=-DMONGO_HAVE_STDINT
else
int64:=$(shell ./check_int64.sh $(CC) unistd.h && echo unistd)
ifeq ($(int64),unistd)
ALL_DEFINES+=-DMONGO_HAVE_UNISTD
endif
endif
$(shell rm header_check.tmp tmp.c)

TEST_DEFINES=$(ALL_DEFINES)
TEST_DEFINES+=-DTEST_SERVER="\"127.0.0.1\""

OPTIMIZATION?=-O3
WARNINGS?=-Wall
DEBUG?=-ggdb
ALL_CFLAGS=$(CFLAGS) $(OPTIMIZATION) $(WARNINGS) $(DEBUG) $(ALL_DEFINES)
ALL_LDFLAGS=$(LDFLAGS)

# Shared libraries
DYLIBSUFFIX=so
STLIBSUFFIX=a
MONGO_DYLIBNAME=$(MONGO_LIBNAME).$(DYLIBSUFFIX)
MONGO_DYLIB_MINOR_NAME=$(MONGO_LIBNAME).$(DYLIBSUFFIX).$(MONGO_MAJOR).$(MONGO_MINOR)
MONGO_DYLIB_MAJOR_NAME=$(MONGO_LIBNAME).$(DYLIBSUFFIX).$(MONGO_MAJOR)
MONGO_DYLIB_MAKE_CMD=$(CC) -shared -Wl,-soname,$(MONGO_DYLIB_MINOR_NAME) -o $(MONGO_DYLIBNAME) $(ALL_LDFLAGS)

BSON_DYLIBNAME=$(BSON_LIBNAME).$(DYLIBSUFFIX)
BSON_DYLIB_MINOR_NAME=$(BSON_LIBNAME).$(DYLIBSUFFIX).$(BSON_MAJOR).$(BSON_MINOR)
BSON_DYLIB_MAJOR_NAME=$(BSON_LIBNAME).$(DYLIBSUFFIX).$(BSON_MAJOR)
BSON_DYLIB_MAKE_CMD=$(CC) -shared -Wl,-soname,$(BSON_DYLIBNAME_VERSIONED) -o $(BSON_DYLIBNAME) $(ALL_LDFLAGS)

# Static libraries
MONGO_STLIBNAME=$(MONGO_LIBNAME).$(STLIBSUFFIX)
BSON_STLIBNAME=$(BSON_LIBNAME).$(STLIBSUFFIX)

# Overrides
kernel_name := $(shell sh -c 'uname -s 2>/dev/null || echo not')
ifeq ($(kernel_name),SunOS)
ALL_LDFLAGS+=-ldl -lnsl -lsocket
INSTALL_CMD=cp -r
MONGO_DYLIB_MAKE_CMD=$(CC) -G -o $(MONGO_DYLIBNAME) -h $(MONGO_DYLIB_MINOR_NAME) $(ALL_LDFLAGS)
BSON_DYLIB_MAKE_CMD=$(CC) -G -o $(BSON_DYLIBNAME) -h $(BSON_DYLIB_MINOR_NAME) $(ALL_LDFLAGS)
endif
ifeq ($(kernel_name),Darwin)
DYLIBSUFFIX=dylib
MONGO_DYLIB_MINOR_NAME=$(MONGO_LIBNAME).$(DYLIBSUFFIX).$(MONGO_MAJOR).$(MONGO_MINOR)
MONGO_DYLIB_MAJOR_NAME=$(MONGO_LIBNAME).$(DYLIBSUFFIX).$(MONGO_MAJOR)
MONGO_DYLIB_MAKE_CMD=$(CC) -shared -Wl,-install_name,$(MONGO_DYLIB_MINOR_NAME) -o $(MONGO_DYLIBNAME)

BSON_DYLIB_MINOR_NAME=$(BSON_LIBNAME).$(DYLIBSUFFIX).$(BSON_MAJOR).$(BSON_MINOR)
BSON_DYLIB_MAJOR_NAME=$(BSON_LIBNAME).$(DYLIBSUFFIX).$(BSON_MAJOR)
BSON_DYLIB_NAME_CMD=$(CC) -shared -Wl,-install_name,$(BSON_DYLIB_MINOR_NAME) -o $(BSON_DYLIBNAME)
endif

# Installation
ifeq ($(kernel_name),SunOS)
INSTALL?=cp -r
endif
INSTALL?= cp -a
INSTALL_INCLUDE_PATH?=/usr/local/include
INSTALL_LIBRARY_PATH?=/usr/local/lib

# TARGETS
all: $(MONGO_DYLIBNAME) $(BSON_DYLIBNAME) $(MONGO_STLIBNAME) $(BSON_STLIBNAME)

# Dependency targets. Run 'make deps' to generate these.
bson.o: src/bson.c src/bson.h src/platform.h src/encoding.h
encoding.o: src/encoding.c src/bson.h src/platform.h src/encoding.h
env_default.o: src/env_default.c src/env.h src/mongo.h src/bson.h \
src/platform.h
env_posix.o: src/env_posix.c src/env.h src/mongo.h src/bson.h \
src/platform.h
gridfs.o: src/gridfs.c src/gridfs.h src/mongo.h src/bson.h src/platform.h
md5.o: src/md5.c src/md5.h
mongo.o: src/mongo.c src/mongo.h src/bson.h src/platform.h src/md5.h \
src/env.h
numbers.o: src/numbers.c

$(MONGO_DYLIBNAME): $(MONGO_OBJECTS)
$(MONGO_DYLIB_MAKE_CMD)

$(MONGO_STLIBNAME): $(MONGO_OBJECTS)
$(AR) -rs $@ $(MONGO_OBJECTS)

$(BSON_DYLIBNAME): $(BSON_OBJECTS)
$(BSON_DYLIB_MAKE_CMD)

$(BSON_STLIBNAME): $(BSON_OBJECTS)
$(AR) -rs $@ $(BSON_OBJECTS)

install:
mkdir -p $(INSTALL_INCLUDE_PATH) $(INSTALL_LIBRARY_PATH)
$(INSTALL) src/mongo.h src/bson.h $(INSTALL_INCLUDE_PATH)
$(INSTALL) $(MONGO_DYLIBNAME) $(INSTALL_LIBRARY_PATH)/$(MONGO_DYLIB_MINOR_NAME)
$(INSTALL) $(BSON_DYLIBNAME) $(INSTALL_LIBRARY_PATH)/$(BSON_DYLIB_MINOR_NAME)
cd $(INSTALL_LIBRARY_PATH) && ln -sf $(MONGO_DYLIB_MINOR_NAME) $(MONGO_DYLIB_MAJOR_NAME)
cd $(INSTALL_LIBRARY_PATH) && ln -sf $(BSON_DYLIB_MINOR_NAME) $(BSON_DYLIB_MAJOR_NAME)
$(INSTALL) $(MONGO_STLIBNAME) $(INSTALL_LIBRARY_PATH)
$(INSTALL) $(BSON_STLIBNAME) $(INSTALL_LIBRARY_PATH)

# TODO: rename tests and activate this.
#test: $(TESTS)
# for test in $(TESTS) ; do ./$$test ; done

clean:
rm -rf $(MONGO_DYLIBNAME) $(MONGO_STLIBNAME) $(BSON_DYLIBNAME) $(BSON_STLIBNAME) src/*.o test/*_test

deps:
$(CC) -MM -DMONGO_HAVE_STDINT src/*.c

32bit:
$(MAKE) CFLAGS="-m32" LDFLAGS="-pg"

%_test: %_test.c $(MONGO_STLIBNAME)
$(CC) -o $@ -Isrc $(TEST_DEFINES) $(ALL_LDFLAGS) $< $(MONGO_STLIBNAME)

%.o: %.c
$(CC) -o $@ -c $(ALL_CFLAGS) $<

.PHONY: clean
6 changes: 6 additions & 0 deletions check_int64.sh
@@ -0,0 +1,6 @@
#!/bin/bash
# $1 is C compiler. $2 is header file
cat <<EOF >tmp.c && $1 -o header_check.tmp tmp.c
#include <$2>
int main() { int64_t i=0; return 0; }
EOF

0 comments on commit 8180914

Please sign in to comment.