Squashed 'src/leveldb/' changes from a31c8aa40..196962ff0
[bitcoinplatinum.git] / build_detect_platform
blob4a94715900969161b9f29d41e27f0659c648df10
1 #!/bin/sh
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.
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.
21 # The PLATFORM_CCFLAGS and PLATFORM_CXXFLAGS might include the following:
23 # -DLEVELDB_ATOMIC_PRESENT if <atomic> is present
24 # -DLEVELDB_PLATFORM_POSIX for Posix-based platforms
25 # -DSNAPPY if the Snappy library is present
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
35 # Delete existing output, if it exists
36 rm -f $OUTPUT
37 touch $OUTPUT
39 if test -z "$CC"; then
40 CC=cc
43 if test -z "$CXX"; then
44 CXX=g++
47 if test -z "$TMPDIR"; then
48 TMPDIR=/tmp
51 # Detect OS
52 if test -z "$TARGET_OS"; then
53 TARGET_OS=`uname -s`
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 PLATFORM_SSEFLAGS=
68 MEMCMP_FLAG=
69 if [ "$CXX" = "g++" ]; then
70 # Use libc's memcmp instead of GCC's memcmp. This results in ~40%
71 # performance improvement on readrandom under gcc 4.4.3 on Linux/x86.
72 MEMCMP_FLAG="-fno-builtin-memcmp"
75 case "$TARGET_OS" in
76 CYGWIN_*)
77 PLATFORM=OS_LINUX
78 COMMON_FLAGS="$MEMCMP_FLAG -lpthread -DOS_LINUX -DCYGWIN"
79 PLATFORM_LDFLAGS="-lpthread"
80 PORT_FILE=port/port_posix.cc
81 PORT_SSE_FILE=port/port_posix_sse.cc
83 Darwin)
84 PLATFORM=OS_MACOSX
85 COMMON_FLAGS="$MEMCMP_FLAG -DOS_MACOSX"
86 PLATFORM_SHARED_EXT=dylib
87 [ -z "$INSTALL_PATH" ] && INSTALL_PATH=`pwd`
88 PLATFORM_SHARED_LDFLAGS="-dynamiclib -install_name $INSTALL_PATH/"
89 PORT_FILE=port/port_posix.cc
90 PORT_SSE_FILE=port/port_posix_sse.cc
92 Linux)
93 PLATFORM=OS_LINUX
94 COMMON_FLAGS="$MEMCMP_FLAG -pthread -DOS_LINUX"
95 PLATFORM_LDFLAGS="-pthread"
96 PORT_FILE=port/port_posix.cc
97 PORT_SSE_FILE=port/port_posix_sse.cc
99 SunOS)
100 PLATFORM=OS_SOLARIS
101 COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_SOLARIS"
102 PLATFORM_LIBS="-lpthread -lrt"
103 PORT_FILE=port/port_posix.cc
104 PORT_SSE_FILE=port/port_posix_sse.cc
106 FreeBSD)
107 PLATFORM=OS_FREEBSD
108 COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_FREEBSD"
109 PLATFORM_LIBS="-lpthread"
110 PORT_FILE=port/port_posix.cc
111 PORT_SSE_FILE=port/port_posix_sse.cc
113 GNU/kFreeBSD)
114 PLATFORM=OS_KFREEBSD
115 COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_KFREEBSD"
116 PLATFORM_LIBS="-lpthread"
117 PORT_FILE=port/port_posix.cc
119 NetBSD)
120 PLATFORM=OS_NETBSD
121 COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_NETBSD"
122 PLATFORM_LIBS="-lpthread -lgcc_s"
123 PORT_FILE=port/port_posix.cc
124 PORT_SSE_FILE=port/port_posix_sse.cc
126 OpenBSD)
127 PLATFORM=OS_OPENBSD
128 COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_OPENBSD"
129 PLATFORM_LDFLAGS="-pthread"
130 PORT_FILE=port/port_posix.cc
131 PORT_SSE_FILE=port/port_posix_sse.cc
133 DragonFly)
134 PLATFORM=OS_DRAGONFLYBSD
135 COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_DRAGONFLYBSD"
136 PLATFORM_LIBS="-lpthread"
137 PORT_FILE=port/port_posix.cc
138 PORT_SSE_FILE=port/port_posix_sse.cc
140 OS_ANDROID_CROSSCOMPILE)
141 PLATFORM=OS_ANDROID
142 COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_ANDROID -DLEVELDB_PLATFORM_POSIX"
143 PLATFORM_LDFLAGS="" # All pthread features are in the Android C library
144 PORT_FILE=port/port_posix.cc
145 PORT_SSE_FILE=port/port_posix_sse.cc
146 CROSS_COMPILE=true
148 HP-UX)
149 PLATFORM=OS_HPUX
150 COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_HPUX"
151 PLATFORM_LDFLAGS="-pthread"
152 PORT_FILE=port/port_posix.cc
153 PORT_SSE_FILE=port/port_posix_sse.cc
154 # man ld: +h internal_name
155 PLATFORM_SHARED_LDFLAGS="-shared -Wl,+h -Wl,"
157 IOS)
158 PLATFORM=IOS
159 COMMON_FLAGS="$MEMCMP_FLAG -DOS_MACOSX"
160 [ -z "$INSTALL_PATH" ] && INSTALL_PATH=`pwd`
161 PORT_FILE=port/port_posix.cc
162 PORT_SSE_FILE=port/port_posix_sse.cc
163 PLATFORM_SHARED_EXT=
164 PLATFORM_SHARED_LDFLAGS=
165 PLATFORM_SHARED_CFLAGS=
166 PLATFORM_SHARED_VERSIONED=
168 OS_WINDOWS_CROSSCOMPILE | NATIVE_WINDOWS)
169 PLATFORM=OS_WINDOWS
170 COMMON_FLAGS="-fno-builtin-memcmp -D_REENTRANT -DOS_WINDOWS -DLEVELDB_PLATFORM_WINDOWS -DWINVER=0x0500 -D__USE_MINGW_ANSI_STDIO=1"
171 PLATFORM_SOURCES="util/env_win.cc"
172 PLATFORM_LIBS="-lshlwapi"
173 PORT_FILE=port/port_win.cc
174 CROSS_COMPILE=true
177 echo "Unknown platform!" >&2
178 exit 1
179 esac
181 # We want to make a list of all cc files within util, db, table, and helpers
182 # except for the test and benchmark files. By default, find will output a list
183 # of all files matching either rule, so we need to append -print to make the
184 # prune take effect.
185 DIRS="$PREFIX/db $PREFIX/util $PREFIX/table"
187 set -f # temporarily disable globbing so that our patterns aren't expanded
188 PRUNE_TEST="-name *test*.cc -prune"
189 PRUNE_BENCH="-name *_bench.cc -prune"
190 PRUNE_TOOL="-name leveldbutil.cc -prune"
191 PORTABLE_FILES=`find $DIRS $PRUNE_TEST -o $PRUNE_BENCH -o $PRUNE_TOOL -o -name '*.cc' -print | sort | sed "s,^$PREFIX/,," | tr "\n" " "`
193 set +f # re-enable globbing
195 # The sources consist of the portable files, plus the platform-specific port
196 # file.
197 echo "SOURCES=$PORTABLE_FILES $PORT_FILE $PORT_SSE_FILE" >> $OUTPUT
198 echo "MEMENV_SOURCES=helpers/memenv/memenv.cc" >> $OUTPUT
200 if [ "$CROSS_COMPILE" = "true" ]; then
201 # Cross-compiling; do not try any compilation tests.
202 true
203 else
204 CXXOUTPUT="${TMPDIR}/leveldb_build_detect_platform-cxx.$$"
206 # If -std=c++0x works, use <atomic> as fallback for when memory barriers
207 # are not available.
208 $CXX $CXXFLAGS -std=c++0x -x c++ - -o $CXXOUTPUT 2>/dev/null <<EOF
209 #include <atomic>
210 int main() {}
212 if [ "$?" = 0 ]; then
213 COMMON_FLAGS="$COMMON_FLAGS -DLEVELDB_PLATFORM_POSIX -DLEVELDB_ATOMIC_PRESENT"
214 PLATFORM_CXXFLAGS="-std=c++0x"
215 else
216 COMMON_FLAGS="$COMMON_FLAGS -DLEVELDB_PLATFORM_POSIX"
219 # Test whether tcmalloc is available
220 $CXX $CXXFLAGS -x c++ - -o $CXXOUTPUT -ltcmalloc 2>/dev/null <<EOF
221 int main() {}
223 if [ "$?" = 0 ]; then
224 PLATFORM_LIBS="$PLATFORM_LIBS -ltcmalloc"
227 rm -f $CXXOUTPUT 2>/dev/null
229 # Test if gcc SSE 4.2 is supported
230 $CXX $CXXFLAGS -x c++ - -o $CXXOUTPUT -msse4.2 2>/dev/null <<EOF
231 int main() {}
233 if [ "$?" = 0 ]; then
234 PLATFORM_SSEFLAGS="-msse4.2"
237 rm -f $CXXOUTPUT 2>/dev/null
240 # Use the SSE 4.2 CRC32C intrinsics iff runtime checks indicate compiler supports them.
241 if [ -n "$PLATFORM_SSEFLAGS" ]; then
242 PLATFORM_SSEFLAGS="$PLATFORM_SSEFLAGS -DLEVELDB_PLATFORM_POSIX_SSE"
245 PLATFORM_CCFLAGS="$PLATFORM_CCFLAGS $COMMON_FLAGS"
246 PLATFORM_CXXFLAGS="$PLATFORM_CXXFLAGS $COMMON_FLAGS"
248 echo "CC=$CC" >> $OUTPUT
249 echo "CXX=$CXX" >> $OUTPUT
250 echo "PLATFORM=$PLATFORM" >> $OUTPUT
251 echo "PLATFORM_LDFLAGS=$PLATFORM_LDFLAGS" >> $OUTPUT
252 echo "PLATFORM_LIBS=$PLATFORM_LIBS" >> $OUTPUT
253 echo "PLATFORM_CCFLAGS=$PLATFORM_CCFLAGS" >> $OUTPUT
254 echo "PLATFORM_CXXFLAGS=$PLATFORM_CXXFLAGS" >> $OUTPUT
255 echo "PLATFORM_SSEFLAGS=$PLATFORM_SSEFLAGS" >> $OUTPUT
256 echo "PLATFORM_SHARED_CFLAGS=$PLATFORM_SHARED_CFLAGS" >> $OUTPUT
257 echo "PLATFORM_SHARED_EXT=$PLATFORM_SHARED_EXT" >> $OUTPUT
258 echo "PLATFORM_SHARED_LDFLAGS=$PLATFORM_SHARED_LDFLAGS" >> $OUTPUT
259 echo "PLATFORM_SHARED_VERSIONED=$PLATFORM_SHARED_VERSIONED" >> $OUTPUT