cookutils view cross @ rev 654

cross: cross libtool may help :-)
author Christophe Lincoln <pankso@slitaz.org>
date Wed Mar 05 01:19:09 2014 +0100 (2014-03-05)
parents b357bbf78c7b
children 599eac7431b6
line source
1 #!/bin/sh
2 #
3 # Cross - Help build a cross toolchain on SliTaz.
4 #
5 # Copyright 2012-2014 (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 tools=$WORK/tools
17 sysroot=$WORK/sysroot
18 logdir=$WORK/log
19 mirror_url="http://mirror.slitaz.org/packages/cross/"
21 # Cross-tools tarballs
22 binutils_tarball="binutils-$BINUTILS_VERSION.tar.bz2"
23 linux_tarball="linux-$LINUX_VERSION.tar.xz"
24 glibc_tarball="glibc-$GLIBC_VERSION.tar.bz2"
25 eglibc_tarball="eglibc-$EGLIBC_VERSION.tar.bz2"
26 gcc_tarball="gcc-$GCC_VERSION.tar.bz2"
27 libtool_tarball="libtool-$LIBTOOL_VERSION.tar.gz"
29 # Cross-tools URLs
30 binutils_wget="http://ftp.gnu.org/gnu/binutils/$binutils_tarball"
31 linux_wget="http://www.kernel.org/pub/linux/kernel/v3.x/$linux_tarball"
32 glibc_wget="http://ftp.gnu.org/gnu/libc/$glibc_tarball"
33 eglibc_wget="svn://svn.eglibc.org/branches/eglibc-2_13"
34 gcc_wget="http://ftp.gnu.org/gnu/gcc/gcc-$GCC_VERSION/$gcc_tarball"
35 libtool_wget="ftp://sunsite.cnlab-switch.ch/mirror/gnu/libtool/$libtool_tarball"
37 # Help and usage.
38 usage() {
39 cat << EOT
41 Usage: $(basename $0) command --option
43 Commands:
44 howto Man[like] page and howto
45 info Display cross-tools info
46 testsuite Execute a small testsuite
47 [arch]-setup Setup build host environment
48 download Download necessary sources
49 show-log Show a package compile log
50 binutils Compile Binutils
51 linux-headers Install Kernel headers
52 gcc-static Compile GCC static
53 glibc Compile GNU Glibc library
54 eglibc Compile EGlibc libc library
55 gcc-final Compile final GCC
56 compile Compile everything at once
57 libtool Cross GNU Libtool (test in receipt LIBTOOL=)
58 clean Clean-up build environment
59 clean-tools Clean: $tools
60 gen-prebuilt Create a prebuilt toolchain archive
62 EOT
63 }
65 # Prebuilt README
66 prebuilt_readme() {
67 echo -n "Creating toolchain README..."
68 cat >> $package/README << EOT
70 SliTaz Prebuilt $ARCH cross toolchain
71 ================================================================================
72 Move this $ARCH cross compilation toolchain to /usr/cross then add tools
73 to your PATH environment and test the toolchain:
75 # mv $ARCH /cross
76 # export PATH=\$PATH:/cross/$ARCH/tools/bin
78 # echo 'int main() { return 0; }' > test.c
79 # $TARGET-gcc -v -o test.out test.c
80 # readelf -h test.out
82 ================================================================================
84 EOT
85 status
86 }
88 # Make sure we have all directories.
89 init_compile() {
90 unset CFLAGS CXXFLAGS
91 export LC_ALL=POSIX LANG=POSIX
92 export PATH=$PATH:$tools/bin
93 export CROSS_COMPILE=${TARGET}-
94 mkdir -p $source $logdir $sysroot $tools
95 echo "Tools prefix : --prefix=$tools "
96 echo "Target sysroot : --with-sysroot=$sysroot"
97 cd $source
98 }
100 # Some arch may need custom CFLAGS to build Glibc/Eglibc
101 init_cflags() {
102 case "$ARCH" in
103 arm|armv6) export CFLAGS="-O2 -march=armv6" ;;
104 armv6hf) export CFLAGS="-O2 -march=armv6j -mfpu=vfp -mfloat-abi=hard" ;;
105 armv7) export CFLAGS="-Os -march=armv7-a -mfpu=vfpv3-d16 -mfloat-abi=softfp -pipe" ;;
106 esac
107 }
109 # Get source if not yet in $SRC.
110 download_src() {
111 mkdir -p $SRC && cd $SRC
112 [ -f "$binutils_tarball" ] || wget $binutils_wget
113 [ -f "$linux_tarball" ] || wget $linux_wget
114 [ -f "$glibc_tarball" ] || wget $glibc_wget
115 [ -f "$gcc_tarball" ] || wget $gcc_wget
116 [ -f "$libtool_tarball" ] || wget $libtool_wget
117 }
119 # 1. Binutils
120 binutils() {
121 init_compile
122 rm -rf binutils-$BINUTILS_VERSION
123 echo "Extracting: $binutils_tarball"
124 tar xjf $SRC/$binutils_tarball
125 echo "Configure: $BINUTILS_ARGS"
126 cd binutils-$BINUTILS_VERSION
127 ./configure \
128 --prefix=$tools \
129 --target=$TARGET \
130 --enable-plugins \
131 --enable-threads \
132 --enable-targets=$BUILD_SYSTEM \
133 --with-sysroot=$sysroot \
134 $BINUTILS_ARGS &&
135 make || exit 1
136 make install
137 echo "cross: binutils compiled on: $(date)"
138 }
140 # 2. Kernel headers could use CROSS_COMPILE but gcc is not yet built.
141 linux_headers() {
142 init_compile
143 if [ ! -d "linux-$LINUX_VERSION" ]; then
144 echo "Extracting: $linux_tarball"
145 tar xJf $SRC/$linux_tarball
146 fi
147 case "$ARCH" in
148 armv6hf) KARCH="arm" ;;
149 *) KARCH="$ARCH" ;;
150 esac
151 rm -rf linux-headers
152 cd linux-$LINUX_VERSION
153 make CROSS_COMPILE="" mrproper
154 make ARCH=$KARCH headers_check
155 make ARCH=$KARCH headers_install \
156 INSTALL_HDR_PATH=$source/linux-headers
157 rm $source/linux-headers/include/.*install*
158 echo "Copying headers to: $sysroot/usr"
159 cp -a $source/linux-headers/* $sysroot/usr
160 }
162 # 2.1 Glibc headers needed to compile x86_64 gcc-static.
163 glibc_headers() {
164 init_compile
165 echo "Extracting: $glibc_tarball"
166 tar xjf $SRC/$glibc_tarball
167 rm -rf glibc-headers
168 mkdir glibc-headers && cd glibc-headers
169 libc_cv_forced_unwind=yes \
170 libc_cv_c_cleanup=yes \
171 ../glibc-$GLIBC_VERSION/configure \
172 --prefix=/usr \
173 --host=$TARGET \
174 --with-headers=$sysroot/usr/include \
175 --without-cvs \
176 --disable-sanity-checks \
177 --enable-kernel=2.6.32 &&
178 make -k install-headers install_root=$sysroot
179 # Fixes
180 mkdir -p $sysroot/usr/include/gnu
181 touch $sysroot/usr/include/gnu/stubs.h
182 cp bits/stdio_lim.h $sysroot/usr/include/bits
183 }
185 # 3. GCC static (first pass)
186 gcc_static() {
187 init_compile
188 echo "Extracting: $gcc_tarball"
189 tar xjf $SRC/$gcc_tarball
190 echo "Configure: $GCC_STATIC_ARGS"
191 rm -rf gcc-static
192 mkdir gcc-static && cd gcc-static
193 ../gcc-$GCC_VERSION/configure \
194 --prefix=$tools \
195 --libexec=$tools/lib \
196 --target=$TARGET \
197 --disable-shared \
198 --disable-threads \
199 --disable-libgomp \
200 --disable-libmudflap \
201 --disable-libssp \
202 --without-headers \
203 --with-newlib \
204 --with-sysroot=$sysroot \
205 $GCC_STATIC_ARGS &&
206 make all-gcc all-target-libgcc || exit 1
207 make install-gcc install-target-libgcc
208 echo "cross: gcc-static compiled on: $(date)"
209 }
211 # 4. GNU Glibc: TODO Improve ARM support
212 glibc() {
213 init_compile
214 echo "Extracting: $glibc_tarball"
215 tar xjf $SRC/$glibc_tarball
216 echo "Configure: $GLIBC_ARGS"
217 # Some arch may need glibc-ports and custom CFLAGS
218 case "$ARCH" in
219 arm*)
220 export CFLAGS="-march=armv6 -O2"
221 [ -f "$SRC/glibc-ports-$GLIBC_VERSION.tar.bz2" ] || wget \
222 http://ftp.gnu.org/gnu/libc/glibc-ports-$GLIBC_VERSION.tar.bz2 \
223 -O $SRC/glibc-ports-$GLIBC_VERSION.tar.bz2 || exit 1
224 echo "Extracting: glibc-ports-$GLIBC_VERSION.tar.bz2"
225 rm -rf glibc-$GLIBC_VERSION/ports
226 tar xjf $SRC/glibc-ports-$GLIBC_VERSION.tar.bz2
227 mv glibc-ports-$GLIBC_VERSION glibc-$GLIBC_VERSION/ports
228 libexec=/usr/lib/glibc ;;
229 x86_64)
230 #export CFLAGS="-02 -march=generic -pipe"
231 ccflags="-m64"
232 libexec=/usr/lib64/glibc ;;
233 esac
234 # Disable linking to libgcc_eh
235 cd glibc-$GLIBC_VERSION
236 cp Makeconfig Makeconfig.orig
237 sed -e 's/-lgcc_eh//g' Makeconfig.orig > Makeconfig
238 cd ..
239 echo "CFLAGS: $CFLAGS"
240 rm -rf glibc-build
241 mkdir -p glibc-build && cd glibc-build
242 BUILD_CC="gcc" \
243 CC="${TARGET}-gcc $ccflags" \
244 AR="${TARGET}-ar" \
245 RANLIB="${TARGET}-ranlib" \
246 libc_cv_forced_unwind=yes \
247 libc_cv_c_cleanup=yes \
248 ../glibc-$GLIBC_VERSION/configure \
249 --prefix=/usr \
250 --libexec=$libexec \
251 --host=$TARGET \
252 --with-headers=$sysroot/usr/include \
253 --with-binutils=$tools/bin \
254 --enable-kernel=2.6.32 \
255 $GLIBC_ARGS &&
256 make || exit 1
257 make install_root=$sysroot install
258 # Symlink lib64 to lib
259 case "$ARCH" in
260 x86_64)
261 rm -f $sysroot/lib $sysroot/usr/lib
262 cd $sysroot && ln -s lib64 lib
263 cd usr && ln -s lib64 lib ;;
264 esac
265 echo "cross: glibc compiled on: $(date)"
266 }
268 # 4. eglibc: always use --prefix=/usr
269 eglibc() {
270 init_compile
271 init_cflags
272 rm -rf eglibc-build eglibc-$EGLIBC_VERSION
273 echo "Extracting: $eglibc_tarball"
274 tar xjf $SRC/$eglibc_tarball || exit 1
275 case "$ARCH" in
276 arm*)
277 if [ ! -d "$source/eglibc-ports-$EGLIBC_VERSION" ]; then
278 echo "Cloning $eglibc_wget/ports"
279 svn co $eglibc_wget/ports eglibc-ports-$EGLIBC_VERSION >/dev/null
280 fi
281 cp -a eglibc-ports-$EGLIBC_VERSION eglibc-$EGLIBC_VERSION/ports
282 libexec=/usr/lib/eglibc ;;
283 x86_64)
284 #export CFLAGS="-march=nocona -O2 -pipe"
285 ccflags="-m64"
286 libexec=/usr/lib64/eglibc ;;
287 esac
288 # Disable linking to libgcc_eh
289 cd eglibc-$EGLIBC_VERSION
290 cp Makeconfig Makeconfig.orig
291 sed -e 's/-lgcc_eh//g' Makeconfig.orig > Makeconfig
292 cd ..
293 echo "CFLAGS: $CFLAGS"
294 mkdir -p eglibc-build && cd eglibc-build
295 # config.cache
296 cat > config.cache << EOT
297 libc_cv_forced_unwind=yes
298 libc_cv_c_cleanup=yes
299 libc_cv_gnu89_inline=yes
300 EOT
301 BUILD_CC="gcc" \
302 CC="${TARGET}-gcc $ccflags" \
303 AR="${TARGET}-ar" \
304 RANLIB="${TARGET}-ranlib" \
305 ../eglibc-$EGLIBC_VERSION/configure \
306 --prefix=/usr \
307 --libexec=$libexec \
308 --host=$TARGET \
309 --with-headers=$sysroot/usr/include \
310 --with-binutils=$tools/bin \
311 --enable-kernel=2.6.32 \
312 --with-__thread \
313 --without-gd \
314 --without-cvs \
315 --cache-file=config.cache \
316 $EGLIBC_ARGS &&
317 make || exit 1
318 make install_root=$sysroot install
319 echo "cross: eglibc compiled on: $(date)"
320 }
322 # 5. GCC final
323 gcc_final() {
324 init_compile
325 if [ ! -d "gcc-$GCC_VERSION" ]; then
326 echo "Extracting: $gcc_tarball"
327 tar xjf $SRC/$gcc_tarball
328 fi
329 echo "Configure: $GCC_FINAL_ARGS"
330 rm -rf gcc-build
331 mkdir -p gcc-build && cd gcc-build
332 AR=ar \
333 ../gcc-$GCC_VERSION/configure \
334 --prefix=$tools \
335 --libexec=$tools/lib \
336 --target=$TARGET \
337 --enable-shared \
338 --enable-c99 \
339 --enable-long-long \
340 --enable-__cxa_atexit \
341 --with-system-zlib \
342 --enable-plugin \
343 --disable-multilib \
344 --disable-libssp \
345 --disable-checking \
346 --disable-werror \
347 --with-pkgversion="SliTaz" \
348 --with-bugurl="http://bugs.slitaz.org/" \
349 --with-sysroot=$sysroot \
350 $GCC_FINAL_ARGS &&
351 make AS_FOR_TARGET="${TARGET}-as" \
352 LD_FOR_TARGET="${TARGET}-ld" || exit 1
353 make install
354 echo "cross: GCC final compiled on: $(date)"
355 }
357 # A cross libtool should avoid some shared libs path/format bugs
358 cross_libtool() {
359 init_compile
360 [ "$clean" ] && rm -rf libtool-${LIBTOOL_VERSION}
361 if [ ! -d "libtool-$LIBTOOL_VERSION" ]; then
362 echo "Extracting: $libtool_tarball"
363 tar xzf $SRC/$libtool_tarball
364 fi
365 cd libtool-${LIBTOOL_VERSION}
366 ./configure \
367 --prefix=$tools \
368 --host=${TARGET} \
369 --program-prefix=${TARGET}- &&
370 make || exit 1
371 make install
372 echo "cross: Cross libtool compiled on: $(date)"
373 }
375 #
376 # Commands
377 #
379 case "$1" in
380 howto|man)
381 doc=/usr/share/doc/cookutils/cross.txt
382 [ -f "$doc" ] && less -E $doc ;;
383 info)
384 init_compile
385 init_cflags
386 CC=${TARGET}-gcc
387 echo -e "\nCross Toolchain information" && separator
388 [ "$config" ] && echo "Config file : $config"
389 cat << EOT
390 Target arch : $ARCH
391 C Compiler : $CC
392 CFLAGS : $CFLAGS
393 Build directory : $WORK
394 Tools prefix : $tools/bin
395 Arch sysroot : $sysroot
396 EOT
397 separator && echo ""
398 echo "GCC version" && separator
399 if [ -x "$tools/bin/$CC" ]; then
400 $CC -v
401 else
402 echo "No C compiler. To build a toolchain run: cross compile"
403 echo "Missing: $tools/bin/$CC"
404 fi
405 separator && echo "" ;;
406 testsuite)
407 init_compile
408 echo "[COMPILING] $TARGET-gcc -v -Wall -o test.out test.c" \
409 | tee $logdir/testsuite.log
410 echo 'int main() { return 0; }' > test.c
411 $TARGET-gcc -v -Wall -o test.out test.c 2>&1 | tee -a $logdir/testsuite.log
412 if [ -x /usr/bin/file ]; then
413 echo -e "\n[CHECKING] file test.out" | tee -a $logdir/testsuite.log
414 file test.out | tee -a $logdir/testsuite.log
415 fi
416 echo -e "\n[CHECKING] readelf -h test.out" | tee -a $logdir/testsuite.log
417 readelf -h test.out | tee -a $logdir/testsuite.log ;;
418 *setup)
419 data="/usr/share/cross"
420 arch=${1%-setup}
421 [ "$arch" == "setup" ] && arch="arm"
422 newline
423 echo "Checking: build system packages"
424 for pkg in slitaz-toolchain mpfr mpfr-dev gmp gmp-dev mpc-library \
425 gawk autoconf; do
426 if [ ! -d "/var/lib/tazpkg/installed/$pkg" ]; then
427 echo "Missing packages: $pkg"
428 if [ -x /usr/sbin/spk-add ]; then
429 spk-add $pkg
430 else
431 tazpkg -gi $pkg
432 fi
433 fi
434 done
435 echo "Getting $arch cross.conf"
436 cp -f ${data}/cross-${arch}.conf /etc/slitaz/cross.conf
437 cook ${arch}-setup
438 newline ;;
439 download)
440 download_src ;;
441 show-log)
442 pkg=$2
443 log=$logdir/$pkg.log
444 if [ ! -f "$log" ]; then
445 echo "No log file found for: $pkg" && exit 1
446 fi
447 less -E $log ;;
448 binutils)
449 rm -f $logdir/binutils.log
450 binutils 2>&1 | tee $logdir/binutils.log ;;
451 linux-headers)
452 linux_headers 2>&1 | tee $logdir/linux-headers.log ;;
453 glibc-headers)
454 glibc_headers 2>&1 | tee $logdir/glibc-headers.log ;;
455 gcc-static)
456 gcc_static 2>&1 | tee $logdir/gcc-static.log ;;
457 glibc)
458 glibc 2>&1 | tee $logdir/glibc.log ;;
459 eglibc)
460 eglibc 2>&1 | tee $logdir/eglibc.log ;;
461 gcc-final)
462 gcc_final 2>&1 | tee $logdir/gcc-final.log ;;
463 compile)
464 # Compile the full toolchain.
465 time=$(date +%s)
466 init_compile
467 echo "Compile start: $(date)" | tee $logdir/compile.log
468 download_src
469 binutils 2>&1 | tee $logdir/binutils.log
470 case "$ARCH" in
471 x86_64) glibc_headers 2>&1 | tee $logdir/glibc-headers.log ;;
472 esac
473 linux_headers 2>&1 | tee $logdir/linux-headers.log
474 gcc_static 2>&1 | tee $logdir/gcc-static.log
475 case "$ARCH" in
476 arm*) eglibc 2>&1 | tee $logdir/eglibc.log ;;
477 x86_64) glibc 2>&1 | tee $logdir/glibc.log ;;
478 esac
479 gcc_final 2>&1 | tee $logdir/gcc-final.log
480 echo ""
481 echo "Compile end : $(date)" | tee -a $logdir/compile.log
482 time=$(($(date +%s) - $time))
483 sec=$time
484 div=$(( ($time + 30) / 60))
485 [ "$div" != 0 ] && min="~ ${div}m"
486 echo "Build time : ${sec}s $min" | tee -a $logdir/compile.log
487 echo "" ;;
488 libtool)
489 cross_libtool 2>&1 | tee $logdir/libtool.log ;;
490 clean)
491 echo -n "Removing all source files..."
492 rm -rf $WORK/source && status
493 [ "$log" ] && rm -f $WORK/log/*.log ;;
494 clean-tools)
495 # Remove crap :-)
496 init_compile
497 echo "Cleaning : $tools ($(du -sh $tools | awk '{print $1}'))"
498 for file in share/info share/man share/local
499 do
500 echo -n "Removing : $file"
501 rm -rf $tools/$file && status
502 done
503 echo -n "Stripping : shared libs and binaries"
504 find $tools/bin -type f -exec strip -s '{}' 2>/dev/null \;
505 find $tools/lib -name cc1* -exec strip -s '{}' 2>/dev/null \;
506 find $tools/lib -name lto* -exec strip -s '{}' 2>/dev/null \;
507 find $sysroot -name "*.so*" -exec ${TARGET}-strip -s '{}' 2>/dev/null \;
508 sleep 1 && status
509 echo -n "Tools size : " && du -sh $tools | awk '{print $1}' ;;
510 gen-prebuilt)
511 # Create a prebuilt cross toolchain tarball.
512 init_compile
513 date=$(date "+%Y%m%d")
514 package="slitaz-$ARCH-toolchain-$date"
515 tarball="$package.tar.bz2"
516 cd /cross
517 mkdir -p $package/$ARCH || exit 1
518 echo ""
519 echo -n "Copying $ARCH to: $package"
520 cp -a $ARCH/tools $package/$ARCH
521 cp -a $ARCH/sysroot $package/$ARCH
522 status
523 prebuilt_readme
524 echo -n "Creating prebuilt $ARCH toolchain tarball..."
525 tar cjf $tarball $package
526 status
527 rm -rf $package
528 size=$(du -sh $tarball | awk '{print $1}')
529 echo "Tarball path: $(pwd)/$tarball"
530 echo "Tarball size: $size"
531 echo "" ;;
532 *)
533 usage ;;
534 esac