cookutils view cross @ rev 410

More x86_64 support and show custom configure args in logs
author Christophe Lincoln <pankso@slitaz.org>
date Mon May 14 11:39:33 2012 +0200 (2012-05-14)
parents d85e10cb82c7
children 6b8f13182be3
line source
1 #!/bin/sh
2 #
3 # Cross - Help build a cross toolchain on SliTaz.
4 #
5 # Copyright 2012 (C) SliTaz GNU/Linux - BSD License
6 # Author: Christophe Lincoln <pankso@slitaz.org>
7 #
8 . /lib/libtaz.sh
10 [ -f "/etc/slitaz/cross.conf" ] && . /etc/slitaz/cross.conf
11 [ -f "cross.conf" ] && . ./cross.conf
13 # Handle --config=/path/to/cross.conf
14 [ "$config" ] && . $config
16 # Help and usage.
17 usage() {
18 cat << EOT
20 Usage: $(basename $0) command --option
22 Commands:
23 howto Man alike and howto
24 info Display cross-tools info
25 testsuite Execute a small testsuite
26 check-env Check build host tools
27 download Download necessary sources
28 clean Clean-up build environment
29 clean-tools Clean: $PREFIX
30 show-log Show a compile log
31 binutils Compile Binutils
32 gcc-static Compile GCC static
33 linux-headers Install Kernel headers
34 glibc Compile GNU Glibc
35 gcc-final Compile final GCC
36 busybox Cross compile Busybox
37 compile Compile everything at once
39 EOT
40 }
42 # Make sure we have all directories.
43 init_compile() {
44 export LC_ALL=POSIX LANG=POSIX
45 [ "$SYSROOT" ] || export PATH=$PATH:$PREFIX/bin
46 export CROSS_COMPILE=${TARGET}-
47 source=$WORK/source
48 logdir=$WORK/log
49 #install=$WORK/install
50 mkdir -p $source $logdir $install
51 cd $source
52 }
54 # Get source if not yet in $SRC.
55 download_src() {
56 mkdir -p $SRC && cd $SRC
57 [ -f "binutils-$BINUTILS_VERSION.tar.bz2" ] || wget $BINUTILS_WGET
58 [ -f "linux-$LINUX_VERSION.tar.bz2" ] || wget $LINUX_WGET
59 [ -f "glibc-$GLIBC_VERSION.tar.bz2" ] || wget $GLIBC_WGET
60 [ -f "gcc-$GCC_VERSION.tar.bz2" ] || wget $GCC_WGET
61 [ -f "busybox-$BUSYBOX_VERSION.tar.bz2" ] || wget $BUSYBOX_WGET
62 }
64 # Use sysroot or not ?
65 check_sysroot() {
66 if [ "$SYSROOT" ]; then
67 PREFIX=/usr
68 HDR_PATH=$SYSROOT/usr
69 sysroot="--with-sysroot=$SYSROOT"
70 echo "configure: $sysroot"
71 else
72 HDR_PATH=$PREFIX
73 fi
74 }
76 # 1. Binutils
77 binutils() {
78 init_compile
79 echo "Extracting: binutils-$BINUTILS_VERSION.tar.bz2"
80 tar xjf $SRC/binutils-$BINUTILS_VERSION.tar.bz2
81 : ${BINUTILS_ARGS=--enable-shared}
82 echo "configure: $BINUTILS_ARGS"
83 check_sysroot
84 rm -rf binutils-$BINUTILS_VERSION
85 cd binutils-$BINUTILS_VERSION
86 ./configure \
87 --prefix=$PREFIX \
88 --target=$TARGET \
89 --enable-targets=$BUILD_SYSTEM \
90 $BINUTILS_ARGS $sysroot
91 make || exit 1
92 make install
93 echo "cross: binutils compiled on: $(date)"
94 }
96 # 2. GCC static (first pass)
97 gcc_static() {
98 init_compile
99 echo "Extracting: gcc-$GCC_VERSION.tar.bz2"
100 tar xjf $SRC/gcc-$GCC_VERSION.tar.bz2
101 echo "configure: $GCC_STATIC_ARGS"
102 check_sysroot
103 # Arch fixes and work around
104 case "$ARCH" in
105 x86_64)
106 # GCC wants Glib headers in cross environment (not tested
107 # with sysroot) Should we install glibc-headers before ?
108 [ "$SYSROOT" ] || \
109 ln -s /usr/include $PREFIX/$TARGET/include ;;
110 esac
111 rm -rf gcc-static
112 mkdir gcc-static && cd gcc-static
113 ../gcc-$GCC_VERSION/configure \
114 --prefix=$PREFIX \
115 --libexec=$PREFIX/lib \
116 --target=$TARGET \
117 --disable-shared \
118 --disable-threads \
119 --without-headers \
120 --with-newlib \
121 $GCC_STATIC_ARGS $sysroot
122 make all-gcc all-target-libgcc || exit 1
123 make install-gcc install-target-libgcc
124 cd $PREFIX/lib/gcc/$TARGET/$GCC_VERSION
125 echo "Creating symlink for static libgcc: libgcc_eh.a"
126 rm -f libgcc_eh.a && ln -s libgcc.a libgcc_eh.a
127 }
129 # 3. Kernel headers use static GCC
130 linux_headers() {
131 init_compile
132 echo "Extracting: linux-$LINUX_VERSION.tar.bz2"
133 tar xjf $SRC/linux-$LINUX_VERSION.tar.bz2
134 check_sysroot
135 cd linux-$LINUX_VERSION
136 make mrproper
137 make ARCH=$ARCH headers_check
138 make ARCH=$ARCH headers_install \
139 INSTALL_HDR_PATH=$HDR_PATH
140 }
142 # 4. GNU Glibc
143 glibc() {
144 init_compile
145 echo "Extracting: glibc-$GLIBC_VERSION.tar.bz2"
146 tar xjf $SRC/glibc-$GLIBC_VERSION.tar.bz2
147 echo "configure: $GLIBC_ARGS"
148 [ "$continue" ] || rm -rf glibc-build
149 # Some arch may need glibc-ports and custom CFLAGS
150 case "$ARCH" in
151 arm)
152 #export CFLAGS="-march=armv6 -mtune=generic -g -O2"
153 [ -f "$SRC/glibc-ports-$GLIBC_VERSION.tar.bz2" ] || wget \
154 http://ftp.gnu.org/gnu/libc/glibc-ports-$GLIBC_VERSION.tar.bz2 \
155 -O $SRC/glibc-ports-$GLIBC_VERSION.tar.bz2 || exit 1
156 echo "Extracting: glibc-ports-$GLIBC_VERSION.tar.bz2"
157 rm -rf glibc-$GLIBC_VERSION/ports
158 tar xjf $SRC/glibc-ports-$GLIBC_VERSION.tar.bz2
159 mv glibc-ports-$GLIBC_VERSION glibc-$GLIBC_VERSION/ports ;;
160 esac
161 #echo "CFLAGS: $CFLAGS"
162 mkdir -p glibc-build && cd glibc-build
163 BUILD_CC="gcc" \
164 CC="$PREFIX/bin/$TARGET-gcc" \
165 libc_cv_forced_unwind=yes \
166 libc_cv_c_cleanup=yes \
167 ../glibc-$GLIBC_VERSION/configure \
168 --prefix=$PREFIX \
169 --host=$TARGET \
170 --with-headers=$PREFIX/include \
171 --with-binutils=$PREFIX/bin \
172 $GLIBC_ARGS
173 make || exit 1
174 make install
175 # Work around to let GCC find Glibc headers.
176 if [ "$SYSROOT" ]; then
177 cd $SYSROOT
178 ln -s usr/include sys-include
179 else
180 cd $PREFIX/$TARGET
181 rm -rf lib include
182 ln -s ../lib lib
183 ln -s ../include include
184 fi
185 #unset CFLAGS
186 }
188 # 5. GCC final
189 gcc_final() {
190 init_compile
191 if [ ! -d "gcc-$GCC_VERSION" ]; then
192 echo "Extracting: gcc-$GCC_VERSION.tar.bz2"
193 tar xjf $SRC/gcc-$GCC_VERSION.tar.bz2
194 fi
195 echo "configure: $GCC_FINAL_ARGS"
196 check_sysroot
197 rm -rf gcc-build
198 mkdir gcc-build && cd gcc-build
199 ../gcc-$GCC_VERSION/configure \
200 --prefix=$PREFIX \
201 --libexec=$PREFIX/lib \
202 --target=$TARGET \
203 --enable-shared \
204 --enable-c99 \
205 --enable-long-long \
206 --enable-__cxa_atexit \
207 --with-pkgversion="SliTaz" \
208 $GCC_FINAL_ARGS $sysroot
209 make || exit 1
210 make install
211 }
213 # Build Busybox to we can create prebuilt tiny rootfs image and boot
214 # from NFS ?
215 cross_busybox() {
216 init_compile
217 echo "Extracting: busybox-$BUSYBOX_VERSION.tar.bz2"
218 tar xjf $SRC/busybox-$BUSYBOX_VERSION.tar.bz2
219 cd busybox-$BUSYBOX_VERSION
220 # CROSS_COMPILE is exported via init_compile.
221 make defconfig
222 make || exit 1
223 make install
224 chmod 4755 _install/bin/busybox
225 readelf -h _install/bin/busybox
226 echo "Busybox install path: $(pwd)/_install"
227 }
229 #
230 # Commands
231 #
233 case "$1" in
234 howto|man)
235 doc=/usr/share/doc/cookutils/cross.txt
236 [ -f "$doc" ] && less -E $doc ;;
237 info)
238 init_compile
239 CC=${TARGET}-gcc
240 echo -e "\nCross Toolchain information" && separator
241 [ "$config" ] && echo "Config file : $config"
242 cat << EOT
243 Target arch : $ARCH
244 C Compiler : $CC
245 Build directory : $WORK
246 EOT
247 if [ "$SYSROOT" ]; then
248 PREFIX=/usr
249 echo "Arch sysroot : $SYSROOT"
250 else
251 echo "Additional path : $PREFIX/bin"
252 fi
253 separator && echo ""
254 echo "GCC version" && separator
255 if [ -x "$PREFIX/bin/$CC" ]; then
256 $CC -v
257 else
258 echo "No C compiler. To build a toolchain run: cross compile"
259 fi
260 separator && echo "" ;;
261 testsuite)
262 init_compile
263 echo "[COMPILING] $TARGET-gcc -v -Wall -o test.out test.c" \
264 | tee $logdir/testsuite.log
265 echo 'int main() { return 0; }' > test.c
266 $TARGET-gcc -v -Wall -o test.out test.c 2>&1 | tee -a $logdir/testsuite.log
267 if [ -x /usr/bin/file ]; then
268 echo -e "\n[CHECKING] file test.out" | tee -a $logdir/testsuite.log
269 file test.out | tee -a $logdir/testsuite.log
270 fi
271 echo -e "\n[CHECKING] readelf -h test.out" | tee -a $logdir/testsuite.log
272 readelf -h test.out | tee -a $logdir/testsuite.log ;;
273 check-env)
274 for pkg in mpfr mpfr-dev gmp gmp-dev mpc-library gawk autoconf
275 do
276 if [ ! -d "/var/lib/tazpkg/installed/$pkg" ]; then
277 echo "Missing packages: $pkg"
278 [ "$install" ] && tazpkg -gi $pkg
279 fi
280 done ;;
281 download)
282 download_src ;;
283 clean)
284 echo -n "Removing all source files..."
285 rm -rf $WORK/source/* && status
286 [ "$log" ] && rm -f $WORK/log/*.log
287 echo "To clean chroot: rm -rf $PREFIX" ;;
288 show-log)
289 pkg=$2
290 log=$logdir/$pkg.log
291 if [ ! -f "$log" ]; then
292 echo "No log file found for: $pkg" && exit 1
293 fi
294 less -E $log ;;
295 binutils)
296 binutils 2>&1 | tee $logdir/binutils.log ;;
297 gcc-static)
298 gcc_static 2>&1 | tee $logdir/gcc-static.log ;;
299 linux-headers)
300 linux_headers 2>&1 | tee $logdir/linux-headers.log ;;
301 glibc)
302 glibc 2>&1 | tee $logdir/glibc.log ;;
303 gcc-final)
304 gcc_final 2>&1 | tee $logdir/gcc-final.log ;;
305 busybox)
306 cross_busybox 2>&1 | tee $logdir/busybox.log ;;
307 compile)
308 # Compile the full toolchain.
309 time=$(date +%s)
310 init_compile
311 echo "Compile start: $(date)" | tee $logdir/compile.log
312 download_src
313 binutils 2>&1 | tee $logdir/binutils.log
314 gcc_static 2>&1 | tee $logdir/gcc-static.log
315 linux_headers 2>&1 | tee $logdir/linux-headers.log
316 glibc 2>&1 | tee $logdir/glibc.log
317 gcc_final 2>&1 | tee $logdir/gcc-final.log
318 echo ""
319 echo "Compile end : $(date)" | tee -a $logdir/compile.log
320 time=$(($(date +%s) - $time))
321 sec=$time
322 div=$(( ($time + 30) / 60))
323 [ "$div" != 0 ] && min="~ ${div}m"
324 echo "Build time : ${sec}s $min" | tee -a $logdir/compile.log
325 echo "" ;;
326 clean-tools)
327 # Remove crap :-)
328 init_compile
329 echo "Cleaning : $PREFIX"
330 for dir in info man locale
331 do
332 echo -n "Removing : $dir"
333 rm -rf $PREFIX/share && status
334 done
335 echo -n "Stripping : binaries"
336 for bin in $PREFIX/bin/${TARGET}-*
337 do
338 [ "$bin" == "$PREFIX/bin/${TARGET}-strip" ] && continue
339 if [ -x "$bin" ]; then
340 ${TARGET}-strip -s $bin 2>/dev/null
341 fi
342 done && status
343 echo -n "Tools size: " && du -sh $PREFIX | awk '{print $1}' ;;
344 gen-rootfs)
345 #
346 # TESTING
347 #
348 # Create a bootable rootfs ? dd for an HD image ?
349 init_compile
350 rootfs=/tmp/cross/rootfs
351 tarball="rootfs.tar.bz2"
352 rm -rf $rootfs && mkdir -p $rootfs
353 cd /tmp/cross
354 echo -n "Installing SliTaz base files..."
355 tar xzf $SRC/slitaz-base-files-5.2.tar.gz
356 cp -a slitaz-base-files-*/rootfs/* $rootfs
357 status
358 echo -n "Installing Busybox..."
359 cp -a $source/busybox-$BUSYBOX_VERSION/_install/* $rootfs
360 status
361 echo -n "Creating tarball: $tarball"
362 tar cjf $tarball rootfs
363 status
364 echo -n "Moving rootfs to: $WORK"
365 mv $tarball $WORK
366 status
367 du -sh $WORK/$tarball
368 rm -rf /tmp/cross ;;
369 *)
370 usage ;;
371 esac