aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.2.1-5666.3/build_libgcc
blob: d291c4df079b00109167a1303caa5f2df12d4d14 (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
#!/bin/sh
# APPLE LOCAL file ARM libgcc
# This is a pared-down version of build_gcc that builds all non-native
# TARGETS ($2) and then creates a destination directory containing only
# the libgcc files for those TARGETS.  Currently only designed to
# support ARM.

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/ -e s/armv[67]/arm/"

# 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}
if [ "$BOOTSTRAP" != bootstrap ]; then
    bootstrap=--disable-bootstrap
fi

# 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="-g -O2 ${RC_NONARCH_CFLAGS/-pipe/}"

# 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 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=`cat $ORIG_SRC_DIR/gcc/BASE-VER`
if [ -z "$VERS" ]; then
    exit 1
fi

# 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/'`

# This is the libstdc++ version to use.
LIBSTDCXX_VERSION=4.2.1
if [ ! -d "$DEST_ROOT/include/c++/$LIBSTDCXX_VERSION" ]; then
  LIBSTDCXX_VERSION=4.0.0
fi
NON_ARM_CONFIGFLAGS="--with-gxx-include-dir=\${prefix}/include/c++/$LIBSTDCXX_VERSION"

DARWIN_VERS=`uname -r | sed 's/\..*//'`
echo DARWIN_VERS = $DARWIN_VERS

# APPLE LOCAL begin ARM
ARM_LIBSTDCXX_VERSION=4.2.1
ARM_CONFIGFLAGS="--with-gxx-include-dir=/usr/include/c++/$ARM_LIBSTDCXX_VERSION"

if [ -n "$ARM_SDK" ]; then

  ARM_PLATFORM=`xcodebuild -version -sdk $ARM_SDK PlatformPath`
  ARM_SYSROOT=`xcodebuild -version -sdk $ARM_SDK Path`
  ARM_TOOLROOT=$ARM_PLATFORM/Developer

elif [ "x$RC_TARGET_CONFIG" = "xiPhone" ]; then

  # If the build target is iPhone, use the iPhone SDK as the build sysroot
  # and use the tools from the iPhone platform directory.  FIXME: This is a
  # temporary fallback for builds where ARM_SDK is not set.  It can be removed,
  # along with the following bootstrap SDK fallback, when ARM_SDK is set for
  # all builds.
  ARM_PLATFORM=/Developer/Platforms/iPhoneOS.platform
  ARM_IPHONE_SDK=iPhoneOS${IPHONEOS_DEPLOYMENT_TARGET}.Internal.sdk

  ARM_SYSROOT=$ARM_PLATFORM/Developer/SDKs/$ARM_IPHONE_SDK
  ARM_TOOLROOT=$ARM_PLATFORM/Developer

else

  # Use bootstrap SDK if it is available.
  if [ -d /Developer/SDKs/Extra ]; then
    ARM_SYSROOT=/Developer/SDKs/Extra
  else
    ARM_SYSROOT=/
  fi
  ARM_TOOLROOT=/

fi
ARM_CONFIGFLAGS="$ARM_CONFIGFLAGS --with-build-sysroot=\"$ARM_SYSROOT\""

# If building an ARM target, check that the required directories exist
# and query the libSystem arm slices to determine which multilibs we should
# build.
if echo $TARGETS | grep arm; then
  if [ ! -d $ARM_SYSROOT ]; then
    echo "Error: cannot find ARM SDK to build ARM target"
    exit 1
  fi
  if [ ! -d $ARM_TOOLROOT ]; then
    echo "Error: $ARM_TOOLROOT directory is not installed"
    exit 1
  fi
  if [ "x$ARM_MULTILIB_ARCHS" = "x" ] ; then
    ARM_MULTILIB_ARCHS=`/usr/bin/lipo -info $ARM_SYSROOT/usr/lib/libSystem.dylib | cut -d':' -f 3 | sed -e 's/x86_64//' -e 's/i386//' -e 's/ppc7400//' -e 's/ppc64//' -e 's/^ *//' -e 's/ $//'`
  fi;
  if [ "x$ARM_MULTILIB_ARCHS" == "x" ] ; then
    echo "Error: missing ARM slices in $ARM_SYSROOT"
    exit 1
  else
    export ARM_MULTILIB_ARCHS
  fi
fi
# APPLE LOCAL end ARM

# If the user has CC set in their environment unset it now
unset CC

########################################
# 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
# Also remove libstdc++ since it is built from a separate project.
rm -rf $SRC_DIR/libstdc++-v3 || exit 1
# Clean out old specs files
rm -f /usr/lib/gcc/*/4.0.0/specs

# These are the configure and build flags that are used.
CONFIGFLAGS="--disable-checking --enable-werror \
  --prefix=$DEST_ROOT \
  --mandir=\${prefix}/share/man \
  --enable-languages=c,objc,c++,obj-c++ \
  --program-transform-name=/^[cg][^.-]*$/s/$/-$MAJ_VERS/ \
  --with-slibdir=/usr/lib \
  --build=$BUILD-apple-darwin$DARWIN_VERS"

# 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 Makefile ]; then
 $SRC_DIR/configure $bootstrap $CONFIGFLAGS $NON_ARM_CONFIGFLAGS \
   --host=$BUILD-apple-darwin$DARWIN_VERS \
   --target=$BUILD-apple-darwin$DARWIN_VERS || exit 1
fi
# Unset RC_DEBUG_OPTIONS because it causes the bootstrap to fail.
# Also keep unset for cross compilers so that the cross built libraries are
# comparable to the native built libraries.
unset RC_DEBUG_OPTIONS
make $MAKEFLAGS CFLAGS="$CFLAGS" CXXFLAGS="$CFLAGS" || exit 1
make $MAKEFLAGS html 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$DARWIN_VERS-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$DARWIN_VERS-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 strip lipo ld ; do
  for t in `echo $TARGETS $HOSTS | sort -u`; do
    P=$DIR/bin/${t}-apple-darwin$DARWIN_VERS-${prog}
    # APPLE LOCAL begin toolroot
    if [ $t = "arm" ]; then
      toolroot=$ARM_TOOLROOT
    else
      toolroot=
    fi
    # APPLE LOCAL end toolroot
    echo '#!/bin/sh' > $P || exit 1
    # APPLE LOCAL insert toolroot below
    echo 'exec '${toolroot}'/usr/bin/'${prog}' "$@"' >> $P || exit 1
    chmod a+x $P || exit 1
  done
done
for t in `echo $1 $2 | sort -u`; do
  gt=`echo $t | $TRANSLATE_ARCH`
  P=$DIR/bin/${gt}-apple-darwin$DARWIN_VERS-as
  # APPLE LOCAL begin toolroot
  if [ $gt = "arm" ]; then
    toolroot=$ARM_TOOLROOT
  else
    toolroot=
  fi
  # APPLE LOCAL end toolroot
  echo '#!/bin/sh' > $P || exit 1

  # APPLE LOCAL insert toolroot below
  echo 'for a; do case $a in -arch) exec '${toolroot}'/usr/bin/as "$@";;  esac; done' >> $P || exit 1
  echo 'exec '${toolroot}'/usr/bin/as -arch '${t}' "$@"' >> $P || exit 1
  chmod a+x $P || exit 1
done
PATH=$DIR/bin:$PATH

# Determine which cross-compilers we should build.  If our build architecture is
# one of our hosts, add all of the targets to the list.
if echo $HOSTS | grep $BUILD
then
  CROSS_TARGETS=`echo $TARGETS $HOSTS | tr ' ' '\n' | sort -u`
else
  CROSS_TARGETS="$HOSTS"
fi

# Build the cross-compilers, using the compiler we just built.
for t in $CROSS_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
   if [ \! -f Makefile ]; then
    # APPLE LOCAL begin ARM ARM_CONFIGFLAGS
    T_CONFIGFLAGS="$CONFIGFLAGS --enable-werror-always \
      --program-prefix=$t-apple-darwin$DARWIN_VERS- \
      --host=$BUILD-apple-darwin$DARWIN_VERS \
      --target=$t-apple-darwin$DARWIN_VERS"
    if [ $t = 'arm' ] ; then
      # Explicitly set AS_FOR_TARGET and LD_FOR_TARGET to avoid picking up
      # older versions from the gcc installed in /usr.  Radar 7230843.
      AS_FOR_TARGET=$DIR/bin/${t}-apple-darwin$DARWIN_VERS-as \
      LD_FOR_TARGET=$DIR/bin/${t}-apple-darwin$DARWIN_VERS-ld \
      $SRC_DIR/configure $T_CONFIGFLAGS $ARM_CONFIGFLAGS || exit 1
    else
      $SRC_DIR/configure $T_CONFIGFLAGS $NON_ARM_CONFIGFLAGS || exit 1
    fi
    # APPLE LOCAL end ARM ARM_CONFIGFLAGS
   fi
   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

########################################
# 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

# libgcc
SHARED_LIBS="libgcc_s.1.dylib libgcc_s_v6.1.dylib libgcc_s_v7.1.dylib libgcc_s.10.4.dylib libgcc_s.10.5.dylib"

cd $DEST_DIR || exit 1
mkdir -p .$DEST_ROOT/lib
for l in $SHARED_LIBS ; do
  CANDIDATES=()
  for t in $TARGETS ; do
    if [ -e $DIR/dst-$t-$t$DEST_ROOT/lib/$l ] ; then
      CANDIDATES[${#CANDIDATES[*]}]=$DIR/dst-$t-$t$DEST_ROOT/lib/$l
    # If our target is not one of our hosts, we must look elsewhere for the libraries
    else
      if [ -e $DIR/dst-$BUILD-$t$DEST_ROOT/lib/$l ] ; then
        CANDIDATES[${#CANDIDATES[*]}]=$DIR/dst-$BUILD-$t$DEST_ROOT/lib/$l
      fi
    fi
  done
  if [ ${#CANDIDATES[@]} != '0' ] ; then
    if [ -L ${CANDIDATES[0]} ] ; then
      ln -s `readlink ${CANDIDATES[0]}` .$DEST_ROOT/lib/$l || exit 1
    else
      lipo -output .$DEST_ROOT/lib/$l -create "${CANDIDATES[@]}" || exit 1
    fi
    if ! file .$DEST_ROOT/lib/$l | grep stub > /dev/null ; then
      strip -x .$DEST_ROOT/lib/$l || exit 1
    fi
  fi
done

# Done!
exit 0