cookutils view cross @ rev 648

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