10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
7.1 KiB

  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_CSTDATOMIC_PRESENT if <cstdatomic> 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. Darwin)
  67. PLATFORM=OS_MACOSX
  68. COMMON_FLAGS="$MEMCMP_FLAG -DOS_MACOSX"
  69. PLATFORM_SHARED_EXT=dylib
  70. [ -z "$INSTALL_PATH" ] && INSTALL_PATH=`pwd`
  71. PLATFORM_SHARED_LDFLAGS="-dynamiclib -install_name $INSTALL_PATH/"
  72. PORT_FILE=port/port_posix.cc
  73. ;;
  74. Linux)
  75. PLATFORM=OS_LINUX
  76. COMMON_FLAGS="$MEMCMP_FLAG -pthread -DOS_LINUX"
  77. PLATFORM_LDFLAGS="-pthread"
  78. PORT_FILE=port/port_posix.cc
  79. ;;
  80. SunOS)
  81. PLATFORM=OS_SOLARIS
  82. COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_SOLARIS"
  83. PLATFORM_LIBS="-lpthread -lrt"
  84. PORT_FILE=port/port_posix.cc
  85. ;;
  86. FreeBSD)
  87. PLATFORM=OS_FREEBSD
  88. COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_FREEBSD"
  89. PLATFORM_LIBS="-lpthread"
  90. PORT_FILE=port/port_posix.cc
  91. ;;
  92. NetBSD)
  93. PLATFORM=OS_NETBSD
  94. COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_NETBSD"
  95. PLATFORM_LIBS="-lpthread -lgcc_s"
  96. PORT_FILE=port/port_posix.cc
  97. ;;
  98. OpenBSD)
  99. PLATFORM=OS_OPENBSD
  100. COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_OPENBSD"
  101. PLATFORM_LDFLAGS="-pthread"
  102. PORT_FILE=port/port_posix.cc
  103. ;;
  104. DragonFly)
  105. PLATFORM=OS_DRAGONFLYBSD
  106. COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_DRAGONFLYBSD"
  107. PLATFORM_LIBS="-lpthread"
  108. PORT_FILE=port/port_posix.cc
  109. ;;
  110. OS_ANDROID_CROSSCOMPILE)
  111. PLATFORM=OS_ANDROID
  112. COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_ANDROID -DLEVELDB_PLATFORM_POSIX"
  113. PLATFORM_LDFLAGS="" # All pthread features are in the Android C library
  114. PORT_FILE=port/port_posix.cc
  115. CROSS_COMPILE=true
  116. ;;
  117. HP-UX)
  118. PLATFORM=OS_HPUX
  119. COMMON_FLAGS="$MEMCMP_FLAG -D_REENTRANT -DOS_HPUX"
  120. PLATFORM_LDFLAGS="-pthread"
  121. PORT_FILE=port/port_posix.cc
  122. # man ld: +h internal_name
  123. PLATFORM_SHARED_LDFLAGS="-shared -Wl,+h -Wl,"
  124. ;;
  125. IOS)
  126. PLATFORM=IOS
  127. COMMON_FLAGS="$MEMCMP_FLAG -DOS_MACOSX"
  128. [ -z "$INSTALL_PATH" ] && INSTALL_PATH=`pwd`
  129. PORT_FILE=port/port_posix.cc
  130. PLATFORM_SHARED_EXT=
  131. PLATFORM_SHARED_LDFLAGS=
  132. PLATFORM_SHARED_CFLAGS=
  133. PLATFORM_SHARED_VERSIONED=
  134. ;;
  135. *)
  136. echo "Unknown platform!" >&2
  137. exit 1
  138. esac
  139. # We want to make a list of all cc files within util, db, table, and helpers
  140. # except for the test and benchmark files. By default, find will output a list
  141. # of all files matching either rule, so we need to append -print to make the
  142. # prune take effect.
  143. DIRS="$PREFIX/db $PREFIX/util $PREFIX/table"
  144. set -f # temporarily disable globbing so that our patterns aren't expanded
  145. PRUNE_TEST="-name *test*.cc -prune"
  146. PRUNE_BENCH="-name *_bench.cc -prune"
  147. PRUNE_TOOL="-name leveldb_main.cc -prune"
  148. PORTABLE_FILES=`find $DIRS $PRUNE_TEST -o $PRUNE_BENCH -o $PRUNE_TOOL -o -name '*.cc' -print | sort | sed "s,^$PREFIX/,," | tr "\n" " "`
  149. set +f # re-enable globbing
  150. # The sources consist of the portable files, plus the platform-specific port
  151. # file.
  152. echo "SOURCES=$PORTABLE_FILES $PORT_FILE" >> $OUTPUT
  153. echo "MEMENV_SOURCES=helpers/memenv/memenv.cc" >> $OUTPUT
  154. if [ "$CROSS_COMPILE" = "true" ]; then
  155. # Cross-compiling; do not try any compilation tests.
  156. true
  157. else
  158. CXXOUTPUT="${TMPDIR}/leveldb_build_detect_platform-cxx.$$"
  159. # If -std=c++0x works, use <cstdatomic>. Otherwise use port_posix.h.
  160. $CXX $CXXFLAGS -std=c++0x -x c++ - -o $CXXOUTPUT 2>/dev/null <<EOF
  161. #include <cstdatomic>
  162. int main() {}
  163. EOF
  164. if [ "$?" = 0 ]; then
  165. COMMON_FLAGS="$COMMON_FLAGS -DLEVELDB_PLATFORM_POSIX -DLEVELDB_CSTDATOMIC_PRESENT"
  166. PLATFORM_CXXFLAGS="-std=c++0x"
  167. else
  168. COMMON_FLAGS="$COMMON_FLAGS -DLEVELDB_PLATFORM_POSIX"
  169. fi
  170. # Test whether Snappy library is installed
  171. # http://code.google.com/p/snappy/
  172. $CXX $CXXFLAGS -x c++ - -o $CXXOUTPUT 2>/dev/null <<EOF
  173. #include <snappy.h>
  174. int main() {}
  175. EOF
  176. if [ "$?" = 0 ]; then
  177. COMMON_FLAGS="$COMMON_FLAGS -DSNAPPY"
  178. PLATFORM_LIBS="$PLATFORM_LIBS -lsnappy"
  179. fi
  180. # Test whether tcmalloc is available
  181. $CXX $CXXFLAGS -x c++ - -o $CXXOUTPUT -ltcmalloc 2>/dev/null <<EOF
  182. int main() {}
  183. EOF
  184. if [ "$?" = 0 ]; then
  185. PLATFORM_LIBS="$PLATFORM_LIBS -ltcmalloc"
  186. fi
  187. rm -f $CXXOUTPUT 2>/dev/null
  188. fi
  189. PLATFORM_CCFLAGS="$PLATFORM_CCFLAGS $COMMON_FLAGS"
  190. PLATFORM_CXXFLAGS="$PLATFORM_CXXFLAGS $COMMON_FLAGS"
  191. echo "CC=$CC" >> $OUTPUT
  192. echo "CXX=$CXX" >> $OUTPUT
  193. echo "PLATFORM=$PLATFORM" >> $OUTPUT
  194. echo "PLATFORM_LDFLAGS=$PLATFORM_LDFLAGS" >> $OUTPUT
  195. echo "PLATFORM_LIBS=$PLATFORM_LIBS" >> $OUTPUT
  196. echo "PLATFORM_CCFLAGS=$PLATFORM_CCFLAGS" >> $OUTPUT
  197. echo "PLATFORM_CXXFLAGS=$PLATFORM_CXXFLAGS" >> $OUTPUT
  198. echo "PLATFORM_SHARED_CFLAGS=$PLATFORM_SHARED_CFLAGS" >> $OUTPUT
  199. echo "PLATFORM_SHARED_EXT=$PLATFORM_SHARED_EXT" >> $OUTPUT
  200. echo "PLATFORM_SHARED_LDFLAGS=$PLATFORM_SHARED_LDFLAGS" >> $OUTPUT
  201. echo "PLATFORM_SHARED_VERSIONED=$PLATFORM_SHARED_VERSIONED" >> $OUTPUT