cookutils view cross @ rev 412

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