xref: /leveldb-1.20/build_detect_platform (revision c8c5866a)
1#!/bin/sh
2
3# Detects OS we're compiling on and generates build_config.mk,
4# which in turn gets read while processing Makefile.
5
6# build_config.mk will set the following variables:
7# - PORT_CFLAGS will either set:
8#               -DLEVELDB_PLATFORM_POSIX if cstatomic is present
9#               -DLEVELDB_PLATFORM_NOATOMIC if it is not
10# - PLATFORM_CFLAGS with compiler flags for the platform
11# - PLATFORM_LDFLAGS with linker flags for the platform
12
13# Delete existing build_config.mk
14rm -f build_config.mk
15
16if test -z "$CXX"; then
17    CXX=g++
18fi
19
20# Detect OS
21case `uname -s` in
22    Darwin)
23        PLATFORM=OS_MACOSX
24        echo "PLATFORM_CFLAGS=-DOS_MACOSX" >> build_config.mk
25        echo "PLATFORM_LDFLAGS="  >> build_config.mk
26        ;;
27    Linux)
28        PLATFORM=OS_LINUX
29        echo "PLATFORM_CFLAGS=-pthread -DOS_LINUX" >> build_config.mk
30        echo "PLATFORM_LDFLAGS=-lpthread"  >> build_config.mk
31        ;;
32    SunOS)
33        PLATFORM=OS_SOLARIS
34        echo "PLATFORM_CFLAGS=-D_REENTRANT -DOS_SOLARIS"  >> build_config.mk
35        echo "PLATFORM_LDFLAGS=-lpthread -lrt" >> build_config.mk
36        ;;
37    FreeBSD)
38        PLATFORM=OS_FREEBSD
39        echo "PLATFORM_CFLAGS=-D_REENTRANT -DOS_FREEBSD"  >> build_config.mk
40        echo "PLATFORM_LDFLAGS=-lpthread" >> build_config.mk
41        ;;
42    *)
43        echo "Unknown platform!"
44        exit 1
45esac
46
47echo "PLATFORM=$PLATFORM" >> build_config.mk
48
49# On GCC, use libc's memcmp, not GCC's memcmp
50PORT_CFLAGS="-fno-builtin-memcmp"
51
52# Detect C++0x -- this determines whether we'll use port_noatomic.h
53# or port_posix.h by:
54# 1. Rrying to compile with -std=c++0x and including <cstdatomic>.
55# 2. If $CXX returns error code, we know to use port_posix.h
56$CXX $CFLAGS -std=c++0x -x c++ - -o /dev/null 2>/dev/null  <<EOF
57  #include <cstdatomic>
58  int main() {}
59EOF
60if [ "$?" = 0 ]; then
61    PORT_CFLAGS="$PORT_CFLAGS -DLEVELDB_PLATFORM_POSIX -DLEVELDB_CSTDATOMIC_PRESENT -std=c++0x"
62else
63    PORT_CFLAGS="$PORT_CFLAGS -DLEVELDB_PLATFORM_POSIX"
64fi
65
66# Test whether Snappy library is installed
67# http://code.google.com/p/snappy/
68$CXX $CFLAGS -x c++ - -o /dev/null 2>/dev/null  <<EOF
69  #include <snappy.h>
70  int main() {}
71EOF
72if [ "$?" = 0 ]; then
73    echo "SNAPPY=1" >> build_config.mk
74else
75    echo "SNAPPY=0" >> build_config.mk
76fi
77
78echo "PORT_CFLAGS=$PORT_CFLAGS" >> build_config.mk
79