build_llvm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. #!/bin/sh
  2. # LLVM LOCAL file B&I
  3. set -x
  4. # Build LLVM the "Apple way".
  5. # Parameters:
  6. # The first parameter is a space-separated list of the architectures the
  7. # compilers will run on. For instance, "ppc i386". If the current machine
  8. # isn't in the list, it will (effectively) be added.
  9. HOSTS="$1"
  10. # The second parameter is a space-separated list of the architectures the
  11. # compilers will generate code for. If the current machine isn't in the list, a
  12. # compiler for it will get built anyway, but won't be installed.
  13. # FIXME: The list of targets is currently hard-coded and TARGETS is not used.
  14. TARGETS="$2"
  15. # The third parameter is the path to the compiler sources. There should be a
  16. # shell script named 'configure' in this directory. This script makes a copy...
  17. ORIG_SRC_DIR="$3"
  18. # The fourth parameter is the location where the LLVM will be installed. You can
  19. # move it once it's built, so this mostly controls the layout of $DEST_DIR.
  20. DEST_ROOT="$4"
  21. # The fifth parameter is the place where the compiler will be copied once it's
  22. # built.
  23. DEST_DIR="$5"
  24. # The sixth parameter is a directory in which to place information (like
  25. # unstripped executables and generated source files) helpful in debugging the
  26. # resulting compiler.
  27. SYM_DIR="$6"
  28. # The seventh parameter is a yes/no that indicates whether assertions should be
  29. # enabled in the LLVM libs/tools.
  30. LLVM_ASSERTIONS="$7"
  31. # The eighth parameter is a yes/no that indicates whether this is an optimized
  32. # build.
  33. LLVM_OPTIMIZED="$8"
  34. # A yes/no parameter that controls whether to cross-build for an ARM host.
  35. ARM_HOSTED_BUILD="$9"
  36. # A yes/no parameter that controls whether to cross-build for the iOS simulator
  37. IOS_SIM_BUILD="${10}"
  38. # The version number of the submission, e.g. 1007.
  39. LLVM_SUBMIT_VERSION="${11}"
  40. # The subversion number of the submission, e.g. 03.
  41. LLVM_SUBMIT_SUBVERSION="${12}"
  42. # The current working directory is where the build will happen. It may already
  43. # contain a partial result of an interrupted build, in which case this script
  44. # will continue where it left off.
  45. DIR=`pwd`
  46. DARWIN_VERS=`uname -r | sed 's/\..*//'`
  47. echo DARWIN_VERS = $DARWIN_VERS
  48. ################################################################################
  49. # Run the build.
  50. # Create the source tree we'll actually use to build, deleting
  51. # tcl since it doesn't actually build properly in a cross environment
  52. # and we don't really need it.
  53. SRC_DIR=$DIR/src
  54. rm -rf $SRC_DIR || exit 1
  55. mkdir $SRC_DIR || exit 1
  56. ln -s $ORIG_SRC_DIR/* $SRC_DIR/ || exit 1
  57. # We can't use the top-level Makefile as-is. Remove the soft link:
  58. rm $SRC_DIR/Makefile || exit 1
  59. # Now create our own by editing the top-level Makefile, deleting every line marked "Apple-style":
  60. sed -e '/[Aa]pple-style/d' -e '/include.*GNUmakefile/d' $ORIG_SRC_DIR/Makefile > $SRC_DIR/Makefile || exit 1
  61. SUBVERSION=`echo $RC_ProjectSourceVersion | sed -e 's/.*\.\([0-9]*\).*/\1/'`
  62. if [ "x$SUBVERSION" != "x$RC_ProjectSourceVersion" ]; then
  63. LLVM_SUBMIT_SUBVERSION=`printf "%02d" $SUBVERSION`
  64. RC_ProjectSourceVersion=`echo $RC_ProjectSourceVersion | sed -e 's/\..*//'`
  65. LLVM_SUBMIT_VERSION=$RC_ProjectSourceVersion
  66. fi
  67. if [ "x$LLVM_SUBMIT_SUBVERSION" = "x00" -o "x$LLVM_SUBMIT_SUBVERSION" = "x0" ]; then
  68. LLVM_VERSION="$LLVM_SUBMIT_VERSION"
  69. else
  70. LLVM_VERSION="$LLVM_SUBMIT_VERSION-$LLVM_SUBMIT_SUBVERSION"
  71. fi
  72. SDKROOT_PATH=`xcodebuild -version -sdk $SDKROOT Path`
  73. # Figure out how many make processes to run.
  74. SYSCTL=`sysctl -n hw.activecpu`
  75. # sysctl -n hw.* does not work when invoked via B&I chroot /BuildRoot.
  76. # Builders can default to 2, since even if they are single processor,
  77. # nothing else is running on the machine.
  78. if [ -z "$SYSCTL" ]; then
  79. SYSCTL=2
  80. fi
  81. JOBS_FLAG="-j $SYSCTL"
  82. COMMON_CONFIGURE_OPTS="\
  83. --prefix=$DEST_DIR$DEST_ROOT \
  84. --enable-assertions=$LLVM_ASSERTIONS \
  85. --enable-optimized=$LLVM_OPTIMIZED \
  86. --disable-bindings \
  87. --disable-zlib \
  88. --enable-terminfo=no"
  89. COMMON_MAKEFLAGS="\
  90. UNIVERSAL=1 \
  91. UNIVERSAL_SDK_PATH=$SDKROOT_PATH \
  92. NO_RUNTIME_LIBS=1 \
  93. DISABLE_EDIS=1 \
  94. REQUIRES_RTTI=1 \
  95. DEBUG_SYMBOLS=1 \
  96. LLVM_SUBMIT_VERSION=$LLVM_SUBMIT_VERSION \
  97. LLVM_SUBMIT_SUBVERSION=$LLVM_SUBMIT_SUBVERSION \
  98. VERBOSE=1"
  99. # Build the LLVM tree universal.
  100. mkdir -p $DIR/obj-llvm || exit 1
  101. cd $DIR/obj-llvm || exit 1
  102. if [ "$ARM_HOSTED_BUILD" = yes ]; then
  103. # The cross-tools' build process expects to find an existing cross toolchain
  104. # under names like 'arm-apple-darwin$DARWIN_VERS-as'; so make them.
  105. rm -rf $DIR/bin || exit 1
  106. mkdir $DIR/bin || exit 1
  107. for prog in ar nm ranlib strip lipo ld as ; do
  108. P=$DIR/bin/arm-apple-darwin$DARWIN_VERS-${prog}
  109. T=`xcrun -sdk $SDKROOT -find ${prog}`
  110. ln -s $T $DIR/bin/$prog
  111. echo '#!/bin/sh' > $P || exit 1
  112. echo 'exec '$T' "$@"' >> $P || exit 1
  113. chmod a+x $P || exit 1
  114. done
  115. # Set up the links for clang.
  116. for prog in clang clang++ ; do
  117. P=$DIR/bin/arm-apple-darwin$DARWIN_VERS-${prog}
  118. T=`xcrun -sdk $SDKROOT -find ${prog}`
  119. ln -s $T $DIR/bin/$prog
  120. echo '#!/bin/sh' > $P || exit 1
  121. echo 'exec '$T' -arch armv7 -isysroot '${SDKROOT_PATH}' "$@"' >> $P || exit 1
  122. chmod a+x $P || exit 1
  123. done
  124. PATH=$DIR/bin:$PATH
  125. unset SDKROOT && \
  126. $SRC_DIR/configure $COMMON_CONFIGURE_OPTS \
  127. --enable-targets=arm,arm64 \
  128. --host=arm-apple-darwin10 \
  129. --target=arm-apple-darwin10 \
  130. --build=i686-apple-darwin10 \
  131. --program-prefix="" \
  132. || exit 1
  133. if [ -n "$IPHONEOS_DEPLOYMENT_TARGET" ]; then
  134. COMMON_MAKEFLAGS="$COMMON_MAKEFLAGS \
  135. DEPLOYMENT_TARGET=-mios-version-min=$IPHONEOS_DEPLOYMENT_TARGET"
  136. fi
  137. make $JOBS_FLAG $COMMON_MAKEFLAGS SDKROOT= UNIVERSAL_ARCH="$HOSTS" \
  138. CXXFLAGS="-DLLVM_VERSION_INFO='\" Apple Build #$LLVM_VERSION\"'"
  139. if [ $? != 0 ] ; then
  140. echo "error: LLVM 'make' failed!"
  141. exit 1
  142. fi
  143. else
  144. # not $ARM_HOSTED_BUILD
  145. if [ "$IOS_SIM_BUILD" = yes ]; then
  146. export CC=`xcrun -sdk iphonesimulator -find clang`
  147. export CXX=`xcrun -sdk iphonesimulator -find clang++`
  148. # Use a non-standard "darwin_sim" host triple to trigger a cross-build.
  149. configure_opts="--enable-targets=x86 --host=i686-apple-darwin_sim \
  150. --build=i686-apple-darwin10"
  151. if [ -n "$IPHONEOS_DEPLOYMENT_TARGET" ]; then
  152. COMMON_MAKEFLAGS="$COMMON_MAKEFLAGS \
  153. DEPLOYMENT_TARGET=-mios-simulator-version-min=$IPHONEOS_DEPLOYMENT_TARGET"
  154. fi
  155. else
  156. export CC=`xcrun -sdk macosx -find clang`
  157. export CXX=`xcrun -sdk macosx -find clang++`
  158. configure_opts="--enable-targets=arm,arm64,x86"
  159. if [ -n "$MACOSX_DEPLOYMENT_TARGET" ]; then
  160. COMMON_MAKEFLAGS="$COMMON_MAKEFLAGS \
  161. DEPLOYMENT_TARGET=-mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
  162. fi
  163. fi
  164. if [ $SDKROOT_PATH ]; then
  165. CPPFLAGS="$CPPFLAGS -isysroot $SDKROOT_PATH"
  166. fi
  167. for host in $HOSTS; do :; done
  168. CPPFLAGS="$CPPFLAGS -arch $host"
  169. $SRC_DIR/configure $COMMON_CONFIGURE_OPTS $configure_opts \
  170. --program-prefix="" \
  171. CPPFLAGS="$CPPFLAGS" \
  172. || exit 1
  173. make $JOBS_FLAG $COMMON_MAKEFLAGS UNIVERSAL_ARCH="$HOSTS" \
  174. CXXFLAGS="-DLLVM_VERSION_INFO='\" Apple Build #$LLVM_VERSION\"'"
  175. if [ $? != 0 ] ; then
  176. echo "error: LLVM 'make' failed!"
  177. exit 1
  178. fi
  179. fi
  180. ################################################################################
  181. # Construct the actual destination root, by copying stuff from $DIR/dst-* to
  182. # $DEST_DIR, with occasional 'lipo' commands.
  183. cd $DEST_DIR || exit 1
  184. # Clean out DEST_DIR in case -noclean was passed to buildit.
  185. rm -rf * || exit 1
  186. cd $DIR/obj-llvm || exit 1
  187. # Install the tree into the destination directory.
  188. make $JOBS_FLAG $COMMON_MAKEFLAGS UNIVERSAL_ARCH="$HOSTS" install
  189. if ! test $? == 0 ; then
  190. echo "error: LLVM 'make install' failed!"
  191. exit 1
  192. fi
  193. # Install Version.h
  194. LLVM_MINOR_VERSION=`echo $LLVM_SUBMIT_SUBVERSION | sed -e 's,0*\([1-9][0-9]*\),\1,'`
  195. if [ "x$LLVM_MINOR_VERSION" = "x" ]; then
  196. LLVM_MINOR_VERSION=0
  197. fi
  198. RC_ProjectSourceSubversion=`printf "%d" $LLVM_MINOR_VERSION`
  199. echo "#define LLVM_VERSION ${RC_ProjectSourceVersion}" > $DEST_DIR$DEST_ROOT/include/llvm/Version.h
  200. echo "#define LLVM_MINOR_VERSION ${RC_ProjectSourceSubversion}" >> $DEST_DIR$DEST_ROOT/include/llvm/Version.h
  201. # Run unifdef to preprocess the installed headers to reflect whether this
  202. # was a debug or release build.
  203. for file in `find $DEST_DIR$DEST_ROOT/include -type f -print`; do
  204. if [ "$LLVM_ASSERTIONS" = yes ]; then
  205. unifdef -UNDEBUG -D_DEBUG -o $file $file
  206. else
  207. unifdef -DNDEBUG -U_DEBUG -ULLVM_ENABLE_DUMP -o $file $file
  208. fi
  209. done
  210. # Find the right version of strip to use.
  211. STRIP=strip
  212. if [ -n "$SDKROOT" ]; then
  213. STRIP=`xcrun -sdk $SDKROOT -find strip`
  214. fi
  215. if [ "x$LLVM_DEBUG" != "x1" ]; then
  216. # Strip local symbols from llvm libraries.
  217. #
  218. # Use '-l' to strip i386 modules. N.B. that flag doesn't work with kext or
  219. # PPC objects!
  220. $STRIP -Sl $DEST_DIR$DEST_ROOT/lib/*.[oa]
  221. for f in `ls $DEST_DIR$DEST_ROOT/lib/*.so`; do
  222. $STRIP -Sxl $f
  223. done
  224. fi
  225. # Remove .dir files
  226. cd $DEST_DIR$DEST_ROOT
  227. rm -f bin/.dir etc/llvm/.dir lib/.dir
  228. # The Hello dylib is an example of how to build a pass.
  229. # The BugpointPasses module is only used to test bugpoint.
  230. # These unversioned dylibs cause verification failures, so do not install them.
  231. # (The wildcards are used to match a "lib" prefix if it is present.)
  232. rm $DEST_DIR$DEST_ROOT/lib/*LLVMHello.dylib
  233. rm $DEST_DIR$DEST_ROOT/lib/*BugpointPasses.dylib
  234. # Compress manpages
  235. MDIR=$DEST_DIR$DEST_ROOT/share/man/man1
  236. gzip -f $MDIR/*
  237. ################################################################################
  238. # Create SYM_DIR with information required for debugging.
  239. # Figure out how many make processes to run.
  240. SYSCTL=`sysctl -n hw.activecpu`
  241. # hw.activecpu only available in 10.2.6 and later
  242. if [ -z "$SYSCTL" ]; then
  243. SYSCTL=`sysctl -n hw.ncpu`
  244. fi
  245. # sysctl -n hw.* does not work when invoked via B&I chroot /BuildRoot. Builders
  246. # can default to 2, since even if they are single processor, nothing else is
  247. # running on the machine.
  248. if [ -z "$SYSCTL" ]; then
  249. SYSCTL=2
  250. fi
  251. cd $SYM_DIR || exit 1
  252. # Clean out SYM_DIR in case -noclean was passed to buildit.
  253. rm -rf * || exit 1
  254. # Generate .dSYM files
  255. DSYMUTIL=`xcrun -find dsymutil`
  256. find $DEST_DIR -perm -0111 -type f \
  257. ! \( -name '*.la' -o -name gccas -o -name gccld -o -name llvm-config -o -name '*.a' \) \
  258. -print | xargs -n 1 -P ${SYSCTL} ${DSYMUTIL}
  259. # Save .dSYM files and .a archives
  260. cd $DEST_DIR || exit 1
  261. find . \( -path \*.dSYM/\* -or -name \*.a \) -print \
  262. | cpio -pdml $SYM_DIR || exit 1
  263. # Save source files.
  264. mkdir $SYM_DIR/src || exit 1
  265. cd $DIR || exit 1
  266. find obj-* -name \*.\[chy\] -o -name \*.cpp -print \
  267. | cpio -pdml $SYM_DIR/src || exit 1
  268. ################################################################################
  269. # Remove libLTO.dylib and lto.h. Those are installed by clang.
  270. cd $DEST_DIR$DEST_ROOT
  271. rm -f lib/libLTO.dylib
  272. rm -f lib/libLTO.a lib/libLTO.la
  273. find $DEST_DIR$DEST_ROOT -name lto.h -delete
  274. ################################################################################
  275. # Remove debugging information from DEST_DIR.
  276. cd $DIR || exit 1
  277. find $DEST_DIR -name \*.a -print | xargs ranlib || exit 1
  278. find $DEST_DIR -name \*.dSYM -print | xargs rm -r || exit 1
  279. # Strip debugging information from files
  280. #
  281. # Use '-l' to strip i386 modules. N.B. that flag doesn't work with kext or
  282. # PPC objects!
  283. find $DEST_DIR -perm -0111 -type f \
  284. ! \( -name '*.la' -o -name gccas -o -name gccld -o -name llvm-config \) \
  285. -print | xargs -n 1 -P ${SYSCTL} $STRIP -arch all -Sl
  286. chgrp -h -R wheel $DEST_DIR
  287. chgrp -R wheel $DEST_DIR
  288. ################################################################################
  289. # Remove the docs directory
  290. rm -rf $DEST_DIR$DEST_ROOT/docs
  291. ################################################################################
  292. # w00t! Done!
  293. exit 0