Don't use c++0x.
[novacoin.git] / src / leveldb / build_detect_platform
1 #!/bin/sh
2 #
3 # Detects OS we're compiling on and outputs a file specified by the first
4 # argument, which in turn gets read while processing Makefile.
5 #
6 # The output will set the following variables:
7 #   CC                          C Compiler path
8 #   CXX                         C++ Compiler path
9 #   PLATFORM_LDFLAGS            Linker flags
10 #   PLATFORM_LIBS               Libraries flags
11 #   PLATFORM_SHARED_EXT         Extension for shared libraries
12 #   PLATFORM_SHARED_LDFLAGS     Flags for building shared library
13 #                               This flag is embedded just before the name
14 #                               of the shared library without intervening spaces
15 #   PLATFORM_SHARED_CFLAGS      Flags for compiling objects for shared library
16 #   PLATFORM_CCFLAGS            C compiler flags
17 #   PLATFORM_CXXFLAGS           C++ compiler flags.  Will contain:
18 #   PLATFORM_SHARED_VERSIONED   Set to 'true' if platform supports versioned
19 #                               shared libraries, empty otherwise.
20 #
21 # The PLATFORM_CCFLAGS and PLATFORM_CXXFLAGS might include the following:
22 #
23 #       -DLEVELDB_ATOMIC_PRESENT     if <atomic> is present
24 #       -DLEVELDB_PLATFORM_POSIX     for Posix-based platforms
25 #       -DSNAPPY                     if the Snappy library is present
26 #
27
28 OUTPUT=$1
29 PREFIX=$2
30 if test -z "$OUTPUT" || test -z "$PREFIX"; then
31   echo "usage: $0 <output-filename> <directory_prefix>" >&2
32   exit 1
33 fi
34
35 # Delete existing output, if it exists
36 rm -f $OUTPUT
37 touch $OUTPUT
38
39 if test -z "$CC"; then
40     CC=cc
41 fi
42
43 if test -z "$CXX"; then
44     CXX=g++
45 fi
46
47 if test -z "$TMPDIR"; then
48     TMPDIR=/tmp
49 fi
50
51 # Detect OS
52 if test -z "$TARGET_OS"; then
53     TARGET_OS=`uname -s`
54 fi
55
56 COMMON_FLAGS=
57 CROSS_COMPILE=
58 PLATFORM_CCFLAGS=
59 PLATFORM_CXXFLAGS=
60 PLATFORM_LDFLAGS=
61 PLATFORM_LIBS=
62 PLATFORM_SHARED_EXT="so"
63 PLATFORM_SHARED_LDFLAGS="-shared -Wl,-soname -Wl,"
64 PLATFORM_SHARED_CFLAGS="-fPIC"
65 PLATFORM_SHARED_VERSIONED=true
66
67 MEMCMP_FLAG=
68 if [ "$CXX" = "g++" ]; then
69     # Use libc's memcmp instead of GCC's memcmp.  This results in ~40%
70     # performance improvement on readrandom under gcc 4.4.3 on Linux/x86.
71     MEMCMP_FLAG="-fno-builtin-memcmp"
72 fi
73
74 case "$TARGET_OS" in
75     CYGWIN_*)
76         PLATFORM=OS_LINUX
77         COMMON_FLAGS="$MEMCMP_FLAG -lpthread -DOS_LINUX -DCYGWIN"
78         PLATFORM_LDFLAGS="-lpthread"
79         PORT_FILE=port/port_posix.cc
80         ;;
81     Darwin)
82         PLATFORM=OS_MACOSX
83         COMMON_FLAGS="$MEMCMP_FLAG -DOS_MACOSX"
84         PLATFORM_SHARED_EXT=dylib
85         [ -z "$INSTALL_PATH" ] && INSTALL_PATH=`pwd`
86         PLATFORM_SHARED_LDFLAGS="-dynamiclib -install_name $INSTALL_PATH/"
87         PORT_FILE=port/port_posix.cc
88         ;;
89     Linux)
90         PLATFORM=OS_LINUX
91         COMMON_FLAGS="$MEMCMP_FLAG -pthread -DOS_LINUX"
92         PLATFORM_LDFLAGS="-pthread"
93         PORT_FILE=port/port_posix.cc
94         ;;
95     SunOS)
96         PLATFORM=OS_SOLARIS
97         COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_SOLARIS"
98         PLATFORM_LIBS="-lpthread -lrt"
99         PORT_FILE=port/port_posix.cc
100         ;;
101     FreeBSD)
102         PLATFORM=OS_FREEBSD
103         COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_FREEBSD"
104         PLATFORM_LIBS="-lpthread"
105         PORT_FILE=port/port_posix.cc
106         ;;
107     GNU/kFreeBSD)
108         PLATFORM=OS_KFREEBSD
109         COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_KFREEBSD"
110         PLATFORM_LIBS="-lpthread"
111         PORT_FILE=port/port_posix.cc
112         ;;
113     NetBSD)
114         PLATFORM=OS_NETBSD
115         COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_NETBSD"
116         PLATFORM_LIBS="-lpthread -lgcc_s"
117         PORT_FILE=port/port_posix.cc
118         ;;
119     OpenBSD)
120         PLATFORM=OS_OPENBSD
121         COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_OPENBSD"
122         PLATFORM_LDFLAGS="-pthread"
123         PORT_FILE=port/port_posix.cc
124         ;;
125     DragonFly)
126         PLATFORM=OS_DRAGONFLYBSD
127         COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_DRAGONFLYBSD"
128         PLATFORM_LIBS="-lpthread"
129         PORT_FILE=port/port_posix.cc
130         ;;
131     OS_ANDROID_CROSSCOMPILE)
132         PLATFORM=OS_ANDROID
133         COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_ANDROID -DLEVELDB_PLATFORM_POSIX"
134         PLATFORM_LDFLAGS=""  # All pthread features are in the Android C library
135         PORT_FILE=port/port_posix.cc
136         CROSS_COMPILE=true
137         ;;
138     HP-UX)
139         PLATFORM=OS_HPUX
140         COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_HPUX"
141         PLATFORM_LDFLAGS="-pthread"
142         PORT_FILE=port/port_posix.cc
143         # man ld: +h internal_name
144         PLATFORM_SHARED_LDFLAGS="-shared -Wl,+h -Wl,"
145         ;;
146     IOS)
147         PLATFORM=IOS
148         COMMON_FLAGS="$MEMCMP_FLAG -DOS_MACOSX"
149         [ -z "$INSTALL_PATH" ] && INSTALL_PATH=`pwd`
150         PORT_FILE=port/port_posix.cc
151         PLATFORM_SHARED_EXT=
152         PLATFORM_SHARED_LDFLAGS=
153         PLATFORM_SHARED_CFLAGS=
154         PLATFORM_SHARED_VERSIONED=
155         ;;
156     OS_WINDOWS_CROSSCOMPILE | NATIVE_WINDOWS)
157         PLATFORM=OS_WINDOWS
158         COMMON_FLAGS="-fno-builtin-memcmp -D_REENTRANT -DOS_WINDOWS -DLEVELDB_PLATFORM_WINDOWS -DWINVER=0x0500 -D__USE_MINGW_ANSI_STDIO=1"
159         PLATFORM_SOURCES="util/env_win.cc"
160         PLATFORM_LIBS="-lshlwapi"
161         PORT_FILE=port/port_win.cc
162         CROSS_COMPILE=true
163         ;;
164     *)
165         echo "Unknown platform!" >&2
166         exit 1
167 esac
168
169 # We want to make a list of all cc files within util, db, table, and helpers
170 # except for the test and benchmark files. By default, find will output a list
171 # of all files matching either rule, so we need to append -print to make the
172 # prune take effect.
173 DIRS="$PREFIX/db $PREFIX/util $PREFIX/table"
174
175 set -f # temporarily disable globbing so that our patterns aren't expanded
176 PRUNE_TEST="-name *test*.cc -prune"
177 PRUNE_BENCH="-name *_bench.cc -prune"
178 PRUNE_TOOL="-name leveldb_main.cc -prune"
179 PORTABLE_FILES=`find $DIRS $PRUNE_TEST -o $PRUNE_BENCH -o $PRUNE_TOOL -o -name '*.cc' -print | sort | sed "s,^$PREFIX/,," | tr "\n" " "`
180
181 set +f # re-enable globbing
182
183 # The sources consist of the portable files, plus the platform-specific port
184 # file.
185 echo "SOURCES=$PORTABLE_FILES $PORT_FILE" >> $OUTPUT
186 echo "MEMENV_SOURCES=helpers/memenv/memenv.cc" >> $OUTPUT
187
188 if [ "$CROSS_COMPILE" = "true" ]; then
189     # Cross-compiling; do not try any compilation tests.
190     true
191 else
192     CXXOUTPUT="${TMPDIR}/leveldb_build_detect_platform-cxx.$$"
193     COMMON_FLAGS="$COMMON_FLAGS -DLEVELDB_PLATFORM_POSIX"
194
195     # Test whether tcmalloc is available
196     $CXX $CXXFLAGS -x c++ - -o $CXXOUTPUT -ltcmalloc 2>/dev/null  <<EOF
197       int main() {}
198 EOF
199     if [ "$?" = 0 ]; then
200         PLATFORM_LIBS="$PLATFORM_LIBS -ltcmalloc"
201     fi
202
203     rm -f $CXXOUTPUT 2>/dev/null
204 fi
205
206 PLATFORM_CCFLAGS="$PLATFORM_CCFLAGS $COMMON_FLAGS"
207 PLATFORM_CXXFLAGS="$PLATFORM_CXXFLAGS $COMMON_FLAGS"
208
209 echo "CC=$CC" >> $OUTPUT
210 echo "CXX=$CXX" >> $OUTPUT
211 echo "PLATFORM=$PLATFORM" >> $OUTPUT
212 echo "PLATFORM_LDFLAGS=$PLATFORM_LDFLAGS" >> $OUTPUT
213 echo "PLATFORM_LIBS=$PLATFORM_LIBS" >> $OUTPUT
214 echo "PLATFORM_CCFLAGS=$PLATFORM_CCFLAGS" >> $OUTPUT
215 echo "PLATFORM_CXXFLAGS=$PLATFORM_CXXFLAGS" >> $OUTPUT
216 echo "PLATFORM_SHARED_CFLAGS=$PLATFORM_SHARED_CFLAGS" >> $OUTPUT
217 echo "PLATFORM_SHARED_EXT=$PLATFORM_SHARED_EXT" >> $OUTPUT
218 echo "PLATFORM_SHARED_LDFLAGS=$PLATFORM_SHARED_LDFLAGS" >> $OUTPUT
219 echo "PLATFORM_SHARED_VERSIONED=$PLATFORM_SHARED_VERSIONED" >> $OUTPUT