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