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:
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_CSTDATOMIC_PRESENT if <cstdatomic> is present
24 # -DLEVELDB_PLATFORM_POSIX for Posix-based platforms
25 # -DSNAPPY if the Snappy library is present
30 if test -z "$OUTPUT" || test -z "$PREFIX"; then
31 echo "usage: $0 <output-filename> <directory_prefix>" >&2
35 # Delete existing output, if it exists
39 if test -z "$CC"; then
43 if test -z "$CXX"; then
47 if test -z "$TMPDIR"; then
52 if test -z "$TARGET_OS"; then
62 PLATFORM_SHARED_EXT="so"
63 PLATFORM_SHARED_LDFLAGS="-shared -Wl,-soname -Wl,"
64 PLATFORM_SHARED_CFLAGS="-fPIC"
65 PLATFORM_SHARED_VERSIONED=true
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"
77 COMMON_FLAGS="$MEMCMP_FLAG -DOS_MACOSX"
78 PLATFORM_SHARED_EXT=dylib
79 [ -z "$INSTALL_PATH" ] && INSTALL_PATH=`pwd`
80 PLATFORM_SHARED_LDFLAGS="-dynamiclib -install_name $INSTALL_PATH/"
81 PORT_FILE=port/port_posix.cc
85 COMMON_FLAGS="$MEMCMP_FLAG -pthread -DOS_LINUX"
86 PLATFORM_LDFLAGS="-pthread"
87 PORT_FILE=port/port_posix.cc
91 COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_SOLARIS"
92 PLATFORM_LIBS="-lpthread -lrt"
93 PORT_FILE=port/port_posix.cc
97 COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_FREEBSD"
98 PLATFORM_LIBS="-lpthread"
99 PORT_FILE=port/port_posix.cc
103 COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_KFREEBSD"
104 PLATFORM_LIBS="-lpthread"
105 PORT_FILE=port/port_posix.cc
109 COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_NETBSD"
110 PLATFORM_LIBS="-lpthread -lgcc_s"
111 PORT_FILE=port/port_posix.cc
115 COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_OPENBSD"
116 PLATFORM_LDFLAGS="-pthread"
117 PORT_FILE=port/port_posix.cc
120 PLATFORM=OS_DRAGONFLYBSD
121 COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_DRAGONFLYBSD"
122 PLATFORM_LIBS="-lpthread"
123 PORT_FILE=port/port_posix.cc
125 OS_ANDROID_CROSSCOMPILE)
127 COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_ANDROID -DLEVELDB_PLATFORM_POSIX"
128 PLATFORM_LDFLAGS="" # All pthread features are in the Android C library
129 PORT_FILE=port/port_posix.cc
134 COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_HPUX"
135 PLATFORM_LDFLAGS="-pthread"
136 PORT_FILE=port/port_posix.cc
137 # man ld: +h internal_name
138 PLATFORM_SHARED_LDFLAGS="-shared -Wl,+h -Wl,"
140 OS_WINDOWS_CROSSCOMPILE | NATIVE_WINDOWS)
142 COMMON_FLAGS="-fno-builtin-memcmp -D_REENTRANT -DOS_WINDOWS -DLEVELDB_PLATFORM_WINDOWS -DWINVER=0x0500 -D__USE_MINGW_ANSI_STDIO=1"
143 PLATFORM_SOURCES="util/env_win.cc"
144 PLATFORM_LIBS="-lshlwapi"
145 PORT_FILE=port/port_win.cc
149 echo "Unknown platform!" >&2
153 # We want to make a list of all cc files within util, db, table, and helpers
154 # except for the test and benchmark files. By default, find will output a list
155 # of all files matching either rule, so we need to append -print to make the
157 DIRS="$PREFIX/db $PREFIX/util $PREFIX/table"
159 set -f # temporarily disable globbing so that our patterns aren't expanded
160 PRUNE_TEST="-name *test*.cc -prune"
161 PRUNE_BENCH="-name *_bench.cc -prune"
162 PRUNE_TOOL="-name leveldb_main.cc -prune"
163 PORTABLE_FILES=`find $DIRS $PRUNE_TEST -o $PRUNE_BENCH -o $PRUNE_TOOL -o -name '*.cc' -print | sort | sed "s,^$PREFIX/,," | tr "\n" " "`
165 set +f # re-enable globbing
167 # The sources consist of the portable files, plus the platform-specific port
169 echo "SOURCES=$PORTABLE_FILES $PORT_FILE" >> $OUTPUT
170 echo "MEMENV_SOURCES=helpers/memenv/memenv.cc" >> $OUTPUT
172 if [ "$CROSS_COMPILE" = "true" ]; then
173 # Cross-compiling; do not try any compilation tests.
176 CXXOUTPUT="${TMPDIR}/leveldb_build_detect_platform-cxx.$$"
178 # If -std=c++0x works, use <cstdatomic>. Otherwise use port_posix.h.
179 $CXX $CXXFLAGS -std=c++0x -x c++ - -o $CXXOUTPUT 2>/dev/null <<EOF
180 #include <cstdatomic>
183 if [ "$?" = 0 ]; then
184 COMMON_FLAGS="$COMMON_FLAGS -DLEVELDB_PLATFORM_POSIX -DLEVELDB_CSTDATOMIC_PRESENT"
185 PLATFORM_CXXFLAGS="-std=c++0x"
187 COMMON_FLAGS="$COMMON_FLAGS -DLEVELDB_PLATFORM_POSIX"
190 # Test whether tcmalloc is available
191 $CXX $CXXFLAGS -x c++ - -o $CXXOUTPUT -ltcmalloc 2>/dev/null <<EOF
194 if [ "$?" = 0 ]; then
195 PLATFORM_LIBS="$PLATFORM_LIBS -ltcmalloc"
198 rm -f $CXXOUTPUT 2>/dev/null
201 PLATFORM_CCFLAGS="$PLATFORM_CCFLAGS $COMMON_FLAGS"
202 PLATFORM_CXXFLAGS="$PLATFORM_CXXFLAGS $COMMON_FLAGS"
204 echo "CC=$CC" >> $OUTPUT
205 echo "CXX=$CXX" >> $OUTPUT
206 echo "PLATFORM=$PLATFORM" >> $OUTPUT
207 echo "PLATFORM_LDFLAGS=$PLATFORM_LDFLAGS" >> $OUTPUT
208 echo "PLATFORM_LIBS=$PLATFORM_LIBS" >> $OUTPUT
209 echo "PLATFORM_CCFLAGS=$PLATFORM_CCFLAGS" >> $OUTPUT
210 echo "PLATFORM_CXXFLAGS=$PLATFORM_CXXFLAGS" >> $OUTPUT
211 echo "PLATFORM_SHARED_CFLAGS=$PLATFORM_SHARED_CFLAGS" >> $OUTPUT
212 echo "PLATFORM_SHARED_EXT=$PLATFORM_SHARED_EXT" >> $OUTPUT
213 echo "PLATFORM_SHARED_LDFLAGS=$PLATFORM_SHARED_LDFLAGS" >> $OUTPUT
214 echo "PLATFORM_SHARED_VERSIONED=$PLATFORM_SHARED_VERSIONED" >> $OUTPUT