build_detect_platform 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. OUTPUT=$1
  28. PREFIX=$2
  29. if test -z "$OUTPUT" || test -z "$PREFIX"; then
  30. echo "usage: $0 <output-filename> <directory_prefix>" >&2
  31. exit 1
  32. fi
  33. # Delete existing output, if it exists
  34. rm -f $OUTPUT
  35. touch $OUTPUT
  36. if test -z "$CC"; then
  37. CC=cc
  38. fi
  39. if test -z "$CXX"; then
  40. CXX=g++
  41. fi
  42. if test -z "$TMPDIR"; then
  43. TMPDIR=/tmp
  44. fi
  45. # Detect OS
  46. if test -z "$TARGET_OS"; then
  47. TARGET_OS=`uname -s`
  48. fi
  49. COMMON_FLAGS=
  50. CROSS_COMPILE=
  51. PLATFORM_CCFLAGS=
  52. PLATFORM_CXXFLAGS=
  53. PLATFORM_LDFLAGS=
  54. PLATFORM_LIBS=
  55. PLATFORM_SHARED_EXT="so"
  56. PLATFORM_SHARED_LDFLAGS="-shared -Wl,-soname -Wl,"
  57. PLATFORM_SHARED_CFLAGS="-fPIC"
  58. PLATFORM_SHARED_VERSIONED=true
  59. MEMCMP_FLAG=
  60. if [ "$CXX" = "g++" ]; then
  61. # Use libc's memcmp instead of GCC's memcmp. This results in ~40%
  62. # performance improvement on readrandom under gcc 4.4.3 on Linux/x86.
  63. MEMCMP_FLAG="-fno-builtin-memcmp"
  64. fi
  65. case "$TARGET_OS" in
  66. CYGWIN_*)
  67. PLATFORM=OS_LINUX
  68. COMMON_FLAGS="$MEMCMP_FLAG -lpthread -DOS_LINUX -DCYGWIN"
  69. PLATFORM_LDFLAGS="-lpthread"
  70. PORT_FILE=port/port_posix.cc
  71. ;;
  72. Darwin)
  73. PLATFORM=OS_MACOSX
  74. COMMON_FLAGS="$MEMCMP_FLAG -DOS_MACOSX"
  75. PLATFORM_SHARED_EXT=dylib
  76. [ -z "$INSTALL_PATH" ] && INSTALL_PATH=`pwd`
  77. PLATFORM_SHARED_LDFLAGS="-dynamiclib -install_name $INSTALL_PATH/"
  78. PORT_FILE=port/port_posix.cc
  79. ;;
  80. Linux)
  81. PLATFORM=OS_LINUX
  82. COMMON_FLAGS="$MEMCMP_FLAG -pthread -DOS_LINUX"
  83. PLATFORM_LDFLAGS="-pthread"
  84. PORT_FILE=port/port_posix.cc
  85. ;;
  86. SunOS)
  87. PLATFORM=OS_SOLARIS
  88. COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_SOLARIS"
  89. PLATFORM_LIBS="-lpthread -lrt"
  90. PORT_FILE=port/port_posix.cc
  91. ;;
  92. FreeBSD)
  93. PLATFORM=OS_FREEBSD
  94. COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_FREEBSD"
  95. PLATFORM_LIBS="-lpthread"
  96. PORT_FILE=port/port_posix.cc
  97. ;;
  98. NetBSD)
  99. PLATFORM=OS_NETBSD
  100. COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_NETBSD"
  101. PLATFORM_LIBS="-lpthread -lgcc_s"
  102. PORT_FILE=port/port_posix.cc
  103. ;;
  104. OpenBSD)
  105. PLATFORM=OS_OPENBSD
  106. COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_OPENBSD"
  107. PLATFORM_LDFLAGS="-pthread"
  108. PORT_FILE=port/port_posix.cc
  109. ;;
  110. DragonFly)
  111. PLATFORM=OS_DRAGONFLYBSD
  112. COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_DRAGONFLYBSD"
  113. PLATFORM_LIBS="-lpthread"
  114. PORT_FILE=port/port_posix.cc
  115. ;;
  116. OS_ANDROID_CROSSCOMPILE)
  117. PLATFORM=OS_ANDROID
  118. COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_ANDROID -DLEVELDB_PLATFORM_POSIX"
  119. PLATFORM_LDFLAGS="" # All pthread features are in the Android C library
  120. PORT_FILE=port/port_posix.cc
  121. CROSS_COMPILE=true
  122. ;;
  123. HP-UX)
  124. PLATFORM=OS_HPUX
  125. COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_HPUX"
  126. PLATFORM_LDFLAGS="-pthread"
  127. PORT_FILE=port/port_posix.cc
  128. # man ld: +h internal_name
  129. PLATFORM_SHARED_LDFLAGS="-shared -Wl,+h -Wl,"
  130. ;;
  131. IOS)
  132. PLATFORM=IOS
  133. COMMON_FLAGS="$MEMCMP_FLAG -DOS_MACOSX"
  134. [ -z "$INSTALL_PATH" ] && INSTALL_PATH=`pwd`
  135. PORT_FILE=port/port_posix.cc
  136. PLATFORM_SHARED_EXT=
  137. PLATFORM_SHARED_LDFLAGS=
  138. PLATFORM_SHARED_CFLAGS=
  139. PLATFORM_SHARED_VERSIONED=
  140. ;;
  141. *)
  142. echo "Unknown platform!" >&2
  143. exit 1
  144. esac
  145. # We want to make a list of all cc files within util, db, table, and helpers
  146. # except for the test and benchmark files. By default, find will output a list
  147. # of all files matching either rule, so we need to append -print to make the
  148. # prune take effect.
  149. DIRS="$PREFIX/db $PREFIX/util $PREFIX/table"
  150. set -f # temporarily disable globbing so that our patterns aren't expanded
  151. PRUNE_TEST="-name *test*.cc -prune"
  152. PRUNE_BENCH="-name *_bench.cc -prune"
  153. PRUNE_TOOL="-name leveldb_main.cc -prune"
  154. PORTABLE_FILES=`find $DIRS $PRUNE_TEST -o $PRUNE_BENCH -o $PRUNE_TOOL -o -name '*.cc' -print | sort | sed "s,^$PREFIX/,," | tr "\n" " "`
  155. set +f # re-enable globbing
  156. # The sources consist of the portable files, plus the platform-specific port
  157. # file.
  158. echo "SOURCES=$PORTABLE_FILES $PORT_FILE" >> $OUTPUT
  159. echo "MEMENV_SOURCES=helpers/memenv/memenv.cc" >> $OUTPUT
  160. if [ "$CROSS_COMPILE" = "true" ]; then
  161. # Cross-compiling; do not try any compilation tests.
  162. true
  163. else
  164. CXXOUTPUT="${TMPDIR}/leveldb_build_detect_platform-cxx.$$"
  165. # If -std=c++0x works, use <atomic> as fallback for when memory barriers
  166. # are not available.
  167. $CXX $CXXFLAGS -std=c++0x -x c++ - -o $CXXOUTPUT 2>/dev/null <<EOF
  168. #include <atomic>
  169. int main() {}
  170. EOF
  171. if [ "$?" = 0 ]; then
  172. COMMON_FLAGS="$COMMON_FLAGS -DLEVELDB_PLATFORM_POSIX -DLEVELDB_ATOMIC_PRESENT"
  173. PLATFORM_CXXFLAGS="-std=c++0x"
  174. else
  175. COMMON_FLAGS="$COMMON_FLAGS -DLEVELDB_PLATFORM_POSIX"
  176. fi
  177. # Test whether Snappy library is installed
  178. # http://code.google.com/p/snappy/
  179. $CXX $CXXFLAGS -x c++ - -o $CXXOUTPUT 2>/dev/null <<EOF
  180. #include <snappy.h>
  181. int main() {}
  182. EOF
  183. if [ "$?" = 0 ]; then
  184. COMMON_FLAGS="$COMMON_FLAGS -DSNAPPY"
  185. PLATFORM_LIBS="$PLATFORM_LIBS -lsnappy"
  186. fi
  187. # Test whether tcmalloc is available
  188. $CXX $CXXFLAGS -x c++ - -o $CXXOUTPUT -ltcmalloc 2>/dev/null <<EOF
  189. int main() {}
  190. EOF
  191. if [ "$?" = 0 ]; then
  192. PLATFORM_LIBS="$PLATFORM_LIBS -ltcmalloc"
  193. fi
  194. rm -f $CXXOUTPUT 2>/dev/null
  195. fi
  196. PLATFORM_CCFLAGS="$PLATFORM_CCFLAGS $COMMON_FLAGS"
  197. PLATFORM_CXXFLAGS="$PLATFORM_CXXFLAGS $COMMON_FLAGS"
  198. echo "CC=$CC" >> $OUTPUT
  199. echo "CXX=$CXX" >> $OUTPUT
  200. echo "PLATFORM=$PLATFORM" >> $OUTPUT
  201. echo "PLATFORM_LDFLAGS=$PLATFORM_LDFLAGS" >> $OUTPUT
  202. echo "PLATFORM_LIBS=$PLATFORM_LIBS" >> $OUTPUT
  203. echo "PLATFORM_CCFLAGS=$PLATFORM_CCFLAGS" >> $OUTPUT
  204. echo "PLATFORM_CXXFLAGS=$PLATFORM_CXXFLAGS" >> $OUTPUT
  205. echo "PLATFORM_SHARED_CFLAGS=$PLATFORM_SHARED_CFLAGS" >> $OUTPUT
  206. echo "PLATFORM_SHARED_EXT=$PLATFORM_SHARED_EXT" >> $OUTPUT
  207. echo "PLATFORM_SHARED_LDFLAGS=$PLATFORM_SHARED_LDFLAGS" >> $OUTPUT
  208. echo "PLATFORM_SHARED_VERSIONED=$PLATFORM_SHARED_VERSIONED" >> $OUTPUT