aboutsummaryrefslogtreecommitdiff
path: root/build_gcc
blob: 1d7394f72d4b6066d3461ff5b0c1efa8a21f3a75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#!/bin/sh
# APPLE LOCAL file B&I

set -x

# -arch arguments are different than configure arguments. We need to
# translate them.

TRANSLATE_ARCH="sed -e s/ppc/powerpc/ -e s/i386/i686/"

# Build GCC the "Apple way".
# Parameters:

# The first parameter is a space-separated list of the architectures
# the compilers will run on.  For instance, "ppc i386".  If the
# current machine isn't in the list, it will (effectively) be added.
HOSTS=`echo $1 | $TRANSLATE_ARCH `

# The second parameter is a space-separated list of the architectures the
# compilers will generate code for.  If the current machine isn't in
# the list, a compiler for it will get built anyway, but won't be
# installed.
TARGETS=`echo $2 | $TRANSLATE_ARCH`

# The GNU makefile target ('bootstrap' by default).
BOOTSTRAP=${BOOTSTRAP-bootstrap}

# The B&I build srcript (~rc/bin/buildit) accepts an '-othercflags'
# command-line flag, and captures the argument to that flag in
# $RC_NONARCH_CFLAGS (and mysteriously prepends '-pipe' thereto).
# We will allow this to override the default $CFLAGS and $CXXFLAGS.

CFLAGS=${RC_NONARCH_CFLAGS/-pipe/}
CFLAGS=${CFLAGS:-"-g -O2"}

# This isn't a parameter; it is the architecture of the current machine.
BUILD=`arch | $TRANSLATE_ARCH`

# The third parameter is the path to the compiler sources.  There should
# be a shell script named 'configure' in this directory.  This script
# makes a copy...
ORIG_SRC_DIR="$3"

# The fourth parameter is the location where the compiler will be installed,
# normally "/usr".  You can move it once it's built, so this mostly controls
# the layout of $DEST_DIR.
DEST_ROOT="$4"

# The fifth parameter is the place where the compiler will be copied once
# it's built.
DEST_DIR="$5"

# The sixth parameter is a directory in which to place information (like
# unstripped executables and generated source files) helpful in debugging
# the resulting compiler.
SYM_DIR="$6"

# The current working directory is where the build will happen.
# It may already contain a partial result of an interrupted build,
# in which case this script will continue where it left off.
DIR=`pwd`

# This isn't a parameter; it's the version of the compiler that we're
# about to build.  It's included in the names of various files and
# directories in the installed image.
VERS=`sed -n -e '/version_string/s/.*\"\([^ \"]*\)[ \"].*/\1/p' \
  < $ORIG_SRC_DIR/gcc/version.c || exit 1`

# This isn't a parameter either, it's the major version of the compiler
# to be built.  It's VERS but only up to the second '.' (if there is one).
MAJ_VERS=`echo $VERS | sed 's/\([0-9]*\.[0-9]*\)\..*/\1/'`

########################################
# Run the build.

# Create the source tree we'll actually use to build, deleting
# tcl since it doesn't actually build properly in a cross environment
# and we don't really need it.
SRC_DIR=$DIR/src
rm -rf $SRC_DIR || exit 1
mkdir $SRC_DIR || exit 1
ln -s $ORIG_SRC_DIR/* $SRC_DIR/ || exit 1
rm -rf $SRC_DIR/tcl $SRC_DIR/expect $SRC_DIR/dejagnu || exit 1

# These are the configure and build flags that are used.
CONFIGFLAGS="--disable-checking \
  --prefix=$DEST_ROOT \
  --disable-libmudflap \
  --mandir=\${prefix}/share/man \
  --enable-languages=c,objc,c++ --disable-libada \
  --program-transform-name=/^[cg][^.-]*$/s/$/-$MAJ_VERS/ \
  --with-gxx-include-dir=\${prefix}/include/gcc/darwin/$MAJ_VERS/c++ \
  --build=$BUILD-apple-darwin"

# Figure out how many make processes to run.
SYSCTL=`sysctl -n hw.activecpu`

# hw.activecpu only available in 10.2.6 and later
if [ -z "$SYSCTL" ]; then
  SYSCTL=`sysctl -n hw.ncpu`
fi

# sysctl -n hw.* does not work when invoked via B&I chroot /BuildRoot. 
# Builders can default to 2, since even if they are single processor, 
# nothing else is running on the machine.
if [ -z "$SYSCTL" ]; then
  SYSCTL=2
fi

# The $LOCAL_MAKEFLAGS variable can be used to override $MAKEFLAGS.
MAKEFLAGS=${LOCAL_MAKEFLAGS-"-j $SYSCTL"}

# Build the native GCC.  Do this even if the user didn't ask for it
# because it'll be needed for the bootstrap.
mkdir -p $DIR/obj-$BUILD-$BUILD $DIR/dst-$BUILD-$BUILD || exit 1
cd $DIR/obj-$BUILD-$BUILD || exit 1
if [ \! -f $DIR/$BUILD-configured ] ; then
  $SRC_DIR/configure $CONFIGFLAGS \
    --host=$BUILD-apple-darwin --target=$BUILD-apple-darwin || exit 1
  touch $DIR/$BUILD-configured || exit 1
fi
make $MAKEFLAGS $BOOTSTRAP CFLAGS="$CFLAGS" CXXFLAGS="$CFLAGS" || exit 1
make $MAKEFLAGS DESTDIR=$DIR/dst-$BUILD-$BUILD install-gcc install-target \
  CFLAGS="$CFLAGS" CXXFLAGS="$CFLAGS" || exit 1

# Add the compiler we just built to the path, giving it appropriate names.
D=$DIR/dst-$BUILD-$BUILD/usr/bin
ln -f $D/gcc-$MAJ_VERS $D/gcc || exit 1
ln -f $D/gcc $D/$BUILD-apple-darwin-gcc || exit 1
PATH=$DIR/dst-$BUILD-$BUILD/usr/bin:$PATH

# The cross-tools' build process expects to find certain programs
# under names like 'i386-apple-darwin-ar'; so make them.
# Annoyingly, ranlib changes behaviour depending on what you call it,
# so we have to use a shell script for indirection, grrr.
rm -rf $DIR/bin || exit 1
mkdir $DIR/bin || exit 1
for prog in ar nm ranlib ; do
  for t in `echo $TARGETS $HOSTS | sort -u`; do
    P=$DIR/bin/${t}-apple-darwin-${prog}
    echo '#!/bin/sh' > $P || exit 1
    echo 'exec /usr/bin/'${prog}' $*' >> $P || exit 1
    chmod a+x $P || exit 1
  done
done
PATH=$DIR/bin:$PATH

# Build the cross-compilers, using the compiler we just built.
for t in $TARGETS ; do
  if [ $t != $BUILD ] ; then
   mkdir -p $DIR/obj-$BUILD-$t $DIR/dst-$BUILD-$t || exit 1
    cd $DIR/obj-$BUILD-$t || exit 1
    $SRC_DIR/configure $CONFIGFLAGS  \
      --program-prefix=$t-apple-darwin- \
      --host=$BUILD-apple-darwin --target=$t-apple-darwin || exit 1
    make $MAKEFLAGS all CFLAGS="$CFLAGS" CXXFLAGS="$CFLAGS" || exit 1
    make $MAKEFLAGS DESTDIR=$DIR/dst-$BUILD-$t install-gcc install-target \
      CFLAGS="$CFLAGS" CXXFLAGS="$CFLAGS" || exit 1

    # Add the compiler we just built to the path.
    PATH=$DIR/dst-$BUILD-$t/usr/bin:$PATH
  fi
done

# Rearrange various libraries, for no really good reason.
for t in $TARGETS ; do
  DT=$DIR/dst-$BUILD-$t
  D=`echo $DT/usr/lib/gcc/$t-apple-darwin/$VERS`
  if [ $t == $BUILD ] ; then
    mv $DT/usr/lib/libstdc++.a $D || exit 1
    mv $DT/usr/lib/libsupc++.a $D || exit 1
  else
    DD=$DT/usr/${t}-apple-darwin/lib
    mv $DD/libstdc++.a $D || exit 1
    mv $DD/libsupc++.a $D || exit 1
  fi
  mv $D/static/libgcc.a $D/libgcc_static.a || exit 1
  mv $D/kext/libgcc.a $D/libcc_kext.a || exit 1
  rm -r $D/static $D/kext || exit 1
done

# Build the cross-hosted compilers.
for h in $HOSTS ; do
  if [ $h != $BUILD ] ; then
    for t in $TARGETS ; do
      mkdir -p $DIR/obj-$h-$t $DIR/dst-$h-$t || exit 1
      cd $DIR/obj-$h-$t || exit 1
      if [ $h = $t ] ; then
	pp=
      else
	pp=$t-apple-darwin-
      fi

      $SRC_DIR/configure $CONFIGFLAGS \
        --program-prefix=$pp \
        --host=$h-apple-darwin --target=$t-apple-darwin || exit 1
      make $MAKEFLAGS all-gcc CFLAGS="$CFLAGS" CXXFLAGS="$CFLAGS" || exit 1
      make $MAKEFLAGS DESTDIR=$DIR/dst-$h-$t install-gcc \
        CFLAGS="$CFLAGS" CXXFLAGS="$CFLAGS" || exit 1
    done
  fi
done

########################################
# Construct the actual destination root, by copying stuff from
# $DIR/dst-* to $DEST_DIR, with occasional 'lipo' commands.

cd $DEST_DIR || exit 1

# Clean out DEST_DIR in case -noclean was passed to buildit.
rm -rf * || exit 1

# HTML documentation
HTMLDIR=/Developer/Documentation/DeveloperTools
mkdir -p .$HTMLDIR || exit 1
cp -rp $DIR/obj-$BUILD-$BUILD/gcc/doc/HTML/* .$HTMLDIR/ || exit 1

# Manual pages
mkdir -p .$DEST_ROOT/share || exit 1
cp -rp $DIR/dst-$BUILD-$BUILD$DEST_ROOT/share/man .$DEST_ROOT/share/ \
  || exit 1

# libexec
LIBEXEC_FILES=` \
  ls $DIR/dst-$BUILD-$BUILD$DEST_ROOT/libexec/gcc/$BUILD-apple-darwin/$VERS  \
    | grep -v install-tools || exit 1`
for t in $TARGETS ; do
  DL=$DEST_ROOT/libexec/gcc/$t-apple-darwin/$VERS
  mkdir -p .$DL || exit 1
  for f in $LIBEXEC_FILES ; do
    lipo -output .$DL/$f -create $DIR/dst-*-$t$DL/$f || exit 1
  done
done

# bin
# The native drivers ('native' is different in different architectures).
BIN_FILES=`ls $DIR/dst-$BUILD-$BUILD$DEST_ROOT/bin | grep '^[^-]*-[0-9.]*$' \
  | grep -v gccbug | grep -v gcov || exit 1`
mkdir .$DEST_ROOT/bin
for f in $BIN_FILES ; do 
  lipo -output .$DEST_ROOT/bin/$f -create $DIR/dst-*$DEST_ROOT/bin/$f || exit 1
done
# gcov, which is special only because it gets built multiple times and lipo
# will complain if we try to add two architectures into the same output.
TARG0=`echo $TARGETS | cut -d ' ' -f 1`
lipo -output .$DEST_ROOT/bin/gcov-$MAJ_VERS -create \
  $DIR/dst-*-$TARG0$DEST_ROOT/bin/*gcov* || exit 1
# The fully-named drivers, which have the same target on every host.
for t in $TARGETS ; do
  lipo -output .$DEST_ROOT/bin/$t-apple-darwin-gcc-$VERS -create \
    $DIR/dst-*-$t$DEST_ROOT/bin/$t-apple-darwin-gcc-$MAJ_VERS || exit 1
  lipo -output .$DEST_ROOT/bin/$t-apple-darwin-g++-$VERS -create \
    $DIR/dst-*-$t$DEST_ROOT/bin/$t-apple-darwin-g++-$MAJ_VERS || exit 1
done

# lib
mkdir -p .$DEST_ROOT/lib/gcc || exit 1
for t in $TARGETS ; do
  cp -rp $DIR/dst-$BUILD-$t$DEST_ROOT/lib/gcc/$t-apple-darwin \
    .$DEST_ROOT/lib/gcc || exit 1
done
SHARED_LIBS=`ls $DIR/dst-$BUILD-$BUILD$DEST_ROOT/lib | grep '\.dylib$'`
for l in $SHARED_LIBS ; do
  F=$DIR/dst-$BUILD-$BUILD$DEST_ROOT/lib/$l
  if [ \! -L $F ] ; then
    if [ -f $DIR/dst-$BUILD-*$DEST_ROOT/*-apple-darwin/lib/$l ] ; then
      lipo -output .$DEST_ROOT/lib/$l -create $F \
	$DIR/dst-$BUILD-*$DEST_ROOT/*-apple-darwin/lib/$l || exit 1
    else
      cp -p $F .$DEST_ROOT/lib/$l || exit 1
    fi
    F1=`echo $l \
	| sed 's/\(\.[0-9]\{1,\}\)\(\.[0-9]\{1,\}\)\{1,\}\.dylib/\1.dylib/'`
    F0=`echo $l | sed 's/\(\.[0-9]\{1,\}\)\{1,\}\.dylib/.dylib/'`
    ln -s $l .$DEST_ROOT/lib/$F1 || exit 1
    for t in $TARGETS ; do
      ln -s ../../../$F1 .$DEST_ROOT/lib/gcc/$t-apple-darwin/$VERS/$F0 \
	|| exit 1
    done
  fi
done  

# FIXME
# This is a workaround for gcc_select, pending approval of a patch that
# makes it unnecessary.  Radar 3538294.
mkdir -p .$DEST_ROOT/libexec/gcc/darwin/ppc/$MAJ_VERS
mkdir -p .$DEST_ROOT/lib/gcc/darwin/$MAJ_VERS

# include
HEADERPATH=$DEST_ROOT/include/gcc/darwin/$MAJ_VERS
mkdir -p .$HEADERPATH || exit 1
cp -rp $DIR/dst-$BUILD-$BUILD$HEADERPATH/c++ \
  .$HEADERPATH/ || exit 1
for t in $TARGETS ; do
  DS=$HEADERPATH/c++
  [ $t = $BUILD ] || \
    cp -rp $DIR/dst-$BUILD-$t$DS/$t-apple-darwin .$DS/ || exit 1
done

# Some headers are installed from more-hdrs/.  They all share
# one common feature: they shouldn't be installed here.  Sometimes,
# they should be part of FSF GCC and installed from there; sometimes,
# they should be installed by some completely different package; sometimes,
# they only exist for codewarrior compatibility and codewarrior should provide
# its own.
cd $SRC_DIR/more-hdrs
for h in `echo [^C]*` ; do
  cp -r $h $DEST_DIR$HEADERPATH/$h || exit 1
  for t in $TARGETS ; do
    THEADERPATH=$DEST_DIR$DEST_ROOT/lib/gcc/${t}-apple-darwin/$VERS/include
    [ -f $THEADERPATH/$h ] || \
      ln -s ../../../../../include/gcc/darwin/$MAJ_VERS/$h $THEADERPATH/$h || \
      exit 1
  done
done

# Build driver-driver using fully-named drivers
for t in $TARGETS ; do
    $DEST_DIR$DEST_ROOT/bin/$t-apple-darwin-gcc-$VERS                          \
	$ORIG_SRC_DIR/gcc/config/darwin-driver.c                               \
	-DPDN="\"-apple-darwin-gcc-$VERS\""                                    \
	-DIL="\"$DEST_ROOT/bin/\"" -I  $ORIG_SRC_DIR/include                   \
	-I  $ORIG_SRC_DIR/gcc -I  $ORIG_SRC_DIR/gcc/config                     \
	-liberty -L$DIR/dst-$BUILD-$t$DEST_ROOT/lib/                           \
	-L$DIR/dst-$BUILD-$t$DEST_ROOT/$t-apple-darwin/lib/                    \
	-o $DEST_DIR/$DEST_ROOT/bin/tmp-$t-gcc-$MAJ_VERS || exit 1

    $DEST_DIR$DEST_ROOT/bin/$t-apple-darwin-gcc-$VERS                          \
	$ORIG_SRC_DIR/gcc/config/darwin-driver.c                               \
	-DPDN="\"-apple-darwin-g++-$VERS\""                                    \
	-DIL="\"$DEST_ROOT/bin/\"" -I  $ORIG_SRC_DIR/include                   \
	-I  $ORIG_SRC_DIR/gcc -I  $ORIG_SRC_DIR/gcc/config                     \
	-liberty -L$DIR/dst-$BUILD-$t$DEST_ROOT/lib/                           \
	-L$DIR/dst-$BUILD-$t$DEST_ROOT/$t-apple-darwin/lib/                    \
	-o $DEST_DIR/$DEST_ROOT/bin/tmp-$t-g++-$MAJ_VERS || exit 1
done

lipo -output $DEST_DIR/$DEST_ROOT/bin/gcc-$MAJ_VERS -create \
  $DEST_DIR/$DEST_ROOT/bin/tmp-*-gcc-$MAJ_VERS || exit 1

lipo -output $DEST_DIR/$DEST_ROOT/bin/g++-$MAJ_VERS -create \
  $DEST_DIR/$DEST_ROOT/bin/tmp-*-g++-$MAJ_VERS || exit 1

ln -f $DEST_DIR/$DEST_ROOT/bin/g++-$MAJ_VERS $DEST_DIR/$DEST_ROOT/bin/c++-$MAJ_VERS || exit 1

rm $DEST_DIR/$DEST_ROOT/bin/tmp-*-gcc-$MAJ_VERS || exit 1 
rm $DEST_DIR/$DEST_ROOT/bin/tmp-*-g++-$MAJ_VERS || exit 1


########################################
# Save the source files and objects needed for debugging

cd $SYM_DIR || exit 1

# Clean out SYM_DIR in case -noclean was passed to buildit.
rm -rf * || exit 1

# Save executables and libraries.
cd $DEST_DIR || exit 1
find . \( -perm -0111 -or -name \*.a \) -type f -print \
  | cpio -pdml $SYM_DIR || exit 1
# Save source files.
mkdir $SYM_DIR/src || exit 1
cd $DIR || exit 1
find obj-* -name \*.\[chy\] -print | cpio -pdml $SYM_DIR/src || exit 1

########################################
# Strip the executables and libraries
find $DEST_DIR -perm -0111 \! -name \*.dylib -type f -print \
  | xargs strip || exit 1
find $DEST_DIR \( -name \*.a -or -name \*.dylib \) -type f -print \
  | xargs strip -SX || exit 1

# Done!
exit 0