summaryrefslogtreecommitdiff
path: root/tools-src/build_tools.sh
blob: 1381f5e4693e9960123f9c0157250b8eab28c481 (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
#!/bin/bash
#
# build_tools.sh - Do a build of the MIPS toolchain & glibc
#
# Copyright 2001,2003,2004 Broadcom Corporation
#
#

usage ()
{
cat <<EOF
    Usage: $script [-d base_dir] [-k kernel_dir] [-i install_base]
    -d base_dir
              The base directory where the source will be extracted to and
              the release will be built.  If no -d option is provided,
              the default base_dir is the current pwd.

    -k kernel_dir
              The base directory where a copy of the kernel sources,
	      already configured; can be found. If no -k option is
	      provided, the default kernel_dir is
	      $base_dir/../release/src/linux/linux.

    -i install_base
              The base directory where the resulting binaries will be
	      installed and run from.  If no -i option is provided, the
              default install_base is /opt/brcm.
	      
    (Note: arguments specified must be absolute paths.)

EOF
    exit 1;
}

checkerr()
{
    if [ $1 -ne 0 ]; then
	shift; echo "Error: $*"
	exit 1
    fi
}

report()
{
    if [ $# -lt 2 ]; then
        echo "Internal script error ${1:-(no rc)}"
	exit 1
    fi
    rc=$1; shift
    if [ $rc -eq 0 ]; then
        echo "$* succeeded."
    else
	echo "$* failed, $rc" | tr 'a-z' 'A-Z'
	exit $rc
    fi
}

RUN_DIR=`pwd`
BUILD_DIR=`pwd`
LINUX=${BUILD_DIR}/../release/src/linux/linux
INSTALL_BASE=$BUILD_DIR/../tools/brcm

while getopts 'd:i:k:h' OPT
do
    case "$OPT" in
    d)
	BUILD_DIR=$OPTARG;
	;;
    i)
	INSTALL_BASE=$OPTARG;
	;;
    k)
	LINUX=$OPTARG;
	;;
    h)
	usage;
	;;
    :)
	echo "Missing required argument for option \"$OPTARG\".\n";
	usage;
	;;
    ?)
	echo "Unrecognized option - \"$OPTARG\".\n";
	usage;
	;;
    esac
done

if [[ $BUILD_DIR != /* ]] || [[ $INSTALL_BASE != /* ]] || [[ ${LINUX:-/} != /* ]]; then
    echo "Directory arguments must be absolute paths."
    exit 1
fi

if [ "$OPTIND" -le "$#" ]; then
    usage;
fi

# Figure out install directory
INSTALL_DIR=${INSTALL_BASE}/hndtools-mipsel-linux-3.2.3

# Figure out GNU directory
GNU=${BUILD_DIR}/gnu
if [ ! -d ${GNU} ]; then
    echo "Can't find gnu (sources) in ${BUILD_DIR}"
    GNU=${RUN_DIR}/gnu
    if [ ! -d ${GNU} ]; then
	echo "Nor here (in ${RUN_DIR})"
        exit 1
    fi
    echo "Using gnu in current directory (${RUN_DIR})"
fi    

echo "BUILD_DIR=$BUILD_DIR"
echo "LINUX_DIR=$LINUX"
echo "INSTALL_DIR=$INSTALL_DIR"
echo "GNU=$GNU"

# Enter build directory
cd "${BUILD_DIR}"
checkerr $? "Cannot cd to ${BUILD_DIR}"
mkdir obj
checkerr $? "Cannot create ${BUILD_DIR}/obj"

echo -n "Building gnutools: "
date

OBJ=${BUILD_DIR}/obj
cd ${OBJ}

###############
# Do binutils
echo -n "Doing binutils: "
date

mkdir ${OBJ}/binutils && cd ${OBJ}/binutils
checkerr $? "Cannot cd to ${OBJ}/binutils"

${GNU}/binutils/configure -v \
    --prefix=${INSTALL_DIR} --target=mipsel-linux --with-bcm4710a0 \
  > ${BUILD_DIR}/,binutils-config.log 2>&1
report $? "Configure of binutils"

gmake > ${BUILD_DIR}/,binutils-build.log 2>&1
report $? "Build of binutils"

gmake install > ${BUILD_DIR}/,binutils-install.log 2>&1
report $? "Install of binutils"

# Copy additional static libraries
cp libiberty/libiberty.a opcodes/libopcodes.a bfd/libbfd.a ${INSTALL_DIR}/lib
report $? "Static library copy"

# Make sure installed tools can now be used
export PATH=${INSTALL_DIR}/bin:$PATH

###########################
# Do bootstrap gcc (xgcc)
echo -n "Doing xgcc: "
date

mkdir ${OBJ}/xgcc && cd ${OBJ}/xgcc
checkerr $? "Cannot cd to ${OBJ}/xgcc"

${GNU}/gcc/configure -v \
    --prefix=${INSTALL_DIR} --target=mipsel-linux --with-bcm4710a0 \
    --with-newlib --disable-shared --disable-threads --enable-languages=c \
  > ${BUILD_DIR}/,xgcc-config.log 2>& 1
report $? "Configure of xgcc"

gmake > ${BUILD_DIR}/,xgcc-build.log 2>& 1
report $? "Make of xgcc"

gmake install > ${BUILD_DIR}/,xgcc-install.log 2>&1
report $? "Install of xgcc"

#######################################
# Get kernel headers (to build glibc)

TARGET=mipsel-linux
INCDIR=$INSTALL_DIR/$TARGET/include
CROSS=$INSTALL_DIR/bin/$TARGET-

echo -n "Coping linux includes: "
date

cd ${LINUX}
checkerr $? "Cannot cd to ${LINUX}"
if [ ! -f .config -o ! -f include/linux/version.h ]; then
	echo "LINUX TREE IS NOT CONFIGURED"
	exit 1
fi

# Make include directory in INSTALL_DIR and copy include files
mkdir -p $INCDIR
checkerr $? "Cannot create ${INCDIR}"
cd include && tar -cf - asm asm-mips linux | tar -xf - -C $INCDIR
report $? "Linux include directory copy"

#########################
# Do glibc (using xgcc)
mkdir ${OBJ}/glibc{,-install} && cd ${OBJ}/glibc
checkerr $? "Cannot cd to ${OBJ}/glibc"

echo -n "Doing glibc: "
date

CFLAGS="-O2 -g -finline-limit=10000" ${GNU}/glibc/configure \
    --build=i686-pc-linux-gnu --host=${TARGET} --prefix= \
    --with-headers=${INCDIR} --enable-add-ons \
  > ${BUILD_DIR}/,glibc-config.log 2>&1
report $? "Configure of glibc"

gmake > ${BUILD_DIR}/,glibc-build.log 2>&1
report $? "Build of glibc"

# Separate install dir gives more control over what goes in
gmake install_root=${OBJ}/glibc-install install \
  > ${BUILD_DIR}/,glibc-install.log 2>&1
report $? "Install of glibc"

# Now copy over include and lib, not binary
cd ${OBJ}/glibc-install/include && tar -cf - . | tar -xf - -C ${INCDIR}
report $? "Copy of glibc include"
cd ${OBJ}/glibc-install/lib && tar -cf - . | tar -xf - -C ${INSTALL_DIR}/${TARGET}/lib
report $? "Copy of glibc lib"

echo "Fixing up libc.so locations"
sed -e s%/lib/%"${INSTALL_DIR}/${TARGET}/lib/"%g \
	< "${INSTALL_DIR}/${TARGET}/lib/libc.so" \
	> "${INSTALL_DIR}/${TARGET}/lib/.new.libc.so"
report $? "Fixup of libc.so"

mv -f "${INSTALL_DIR}/${TARGET}/lib/.new.libc.so" \
	"${INSTALL_DIR}/${TARGET}/lib/libc.so"
report $? "Replace of libc.so"

#######################
# Now do the full gcc

mkdir ${OBJ}/gcc && cd ${OBJ}/gcc
checkerr $? "Cannot cd to ${OBJ}/gcc"

echo -n "Doing full gcc: "
date

${GNU}/gcc/configure -v \
    --target=${TARGET} --prefix=${INSTALL_DIR} --with-bcm4710a0 \
    --with-headers=${INSTALL_DIR}/${TARGET}/include --enable-languages=c,c++ \
  > ${BUILD_DIR}/,gcc-config.log 2>&1
report $? "Configure of gcc"

gmake > ${BUILD_DIR}/,gcc-build.log 2>&1
report $? "Build of gcc"

gmake install > ${BUILD_DIR}/,gcc-install.log 2>&1
report $? "Install of gcc"

echo -n "build_tools done: "
date