cookutils view cross @ rev 443

Some update to cook to use sysroot
author Christophe Lincoln <pankso@slitaz.org>
date Tue May 29 09:10:12 2012 +0000 (2012-05-29)
parents 1be327c53ee7
children 91b32bc2e34d
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 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 alike 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 an 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 build.
112 linux_headers() {
113 init_compile
114 echo "Extracting: $LINUX_TARBALL"
115 tar xjf $SRC/$LINUX_TARBALL
116 cd linux-$LINUX_VERSION
117 make mrproper
118 make ARCH=$ARCH headers_check
119 make ARCH=$ARCH headers_install INSTALL_HDR_PATH=$sysroot/usr
120 }
122 # 3. GCC static (first pass)
123 gcc_static() {
124 init_compile
125 echo "Extracting: $GCC_TARBALL"
126 tar xjf $SRC/$GCC_TARBALL
127 echo "Configure: $GCC_STATIC_ARGS"
128 # Arch fixes and work around
129 case "$ARCH" in
130 x86_64)
131 # GCC wants Glib headers in cross environment (not tested
132 # with sysroot) Should we install glibc-headers before ?
133 echo "Try with eglibc or install glibc-headers first"
134 #rm -f $tools/$TARGET/include
135 #ln -s /usr/include $tools/$TARGET/include
136 ;;
137 esac
138 rm -rf gcc-static
139 mkdir gcc-static && cd gcc-static
140 ../gcc-$GCC_VERSION/configure \
141 --prefix=$tools \
142 --libexec=$tools/lib \
143 --target=$TARGET \
144 --disable-shared \
145 --disable-threads \
146 --disable-libgomp \
147 --disable-libmudflap \
148 --disable-libssp \
149 --without-headers \
150 --with-newlib \
151 --with-sysroot=$sysroot \
152 $GCC_STATIC_ARGS &&
153 make all-gcc all-target-libgcc || exit 1
154 make install-gcc install-target-libgcc
155 echo "cross: gcc-static compiled on: $(date)"
156 }
158 # 4. GNU Glibc
159 glibc() {
160 init_compile
161 echo "Extracting: $GLIBC_TARBALL"
162 tar xjf $SRC/$GLIBC_TARBALL
163 echo "Configure: $GLIBC_ARGS"
164 # Some arch may need glibc-ports and custom CFLAGS
165 case "$ARCH" in
166 arm)
167 export CFLAGS="-march=armv6 -O2"
168 [ -f "$SRC/glibc-ports-$GLIBC_VERSION.tar.bz2" ] || wget \
169 http://ftp.gnu.org/gnu/libc/glibc-ports-$GLIBC_VERSION.tar.bz2 \
170 -O $SRC/glibc-ports-$GLIBC_VERSION.tar.bz2 || exit 1
171 echo "Extracting: glibc-ports-$GLIBC_VERSION.tar.bz2"
172 rm -rf glibc-$GLIBC_VERSION/ports
173 tar xjf $SRC/glibc-ports-$GLIBC_VERSION.tar.bz2
174 mv glibc-ports-$GLIBC_VERSION glibc-$GLIBC_VERSION/ports ;;
175 x86_64)
176 ccflags="-m64" ;;
177 esac
178 # Disable linking to libgcc_eh
179 cd glibc-$GLIBC_VERSION
180 cp Makeconfig Makeconfig.orig
181 sed -e 's/-lgcc_eh//g' Makeconfig.orig > Makeconfig
182 cd ..
183 echo "CFLAGS: $CFLAGS"
184 rm -rf glibc-build
185 mkdir -p glibc-build && cd glibc-build
186 BUILD_CC="gcc" \
187 CC="${TARGET}-gcc $ccflags" \
188 AR="${TARGET}-ar" \
189 RANLIB="${TARGET}-ranlib" \
190 libc_cv_forced_unwind=yes \
191 libc_cv_c_cleanup=yes \
192 ../glibc-$GLIBC_VERSION/configure \
193 --prefix=/usr \
194 --libexec=/usr/lib/glibc \
195 --host=$TARGET \
196 --with-headers=$sysroot/usr/include \
197 --with-binutils=$tools/bin \
198 --enable-kernel=2.6.32 \
199 $GLIBC_ARGS &&
200 make || exit 1
201 make install_root=$sysroot install
202 # Work around to let GCC find Glibc headers.
203 #cd $sysroot
204 #ln -s usr/include sys-include
205 echo "cross: glibc compiled on: $(date)"
206 }
208 # 4. Eglibc: always use --prefix=/usr
209 eglibc() {
210 init_compile
211 rm -rf eglibc-build eglibc-$EGLIBC_VERSION
212 echo "Extracting: $EGLIBC_TARBALL"
213 tar xjf $SRC/$EGLIBC_TARBALL
214 # Some arch may need glibc-ports and custom CFLAGS
215 case "$ARCH" in
216 arm)
217 export CFLAGS="-march=armv6 -O2"
218 if [ ! -d "$source/eglibc-ports-$EGLIBC_VERSION" ]; then
219 echo "Cloning $EGLIBC_WGET/ports"
220 svn co $EGLIBC_WGET/ports eglibc-ports-$EGLIBC_VERSION >/dev/null
221 fi
222 cp -a eglibc-ports-$EGLIBC_VERSION eglibc-$EGLIBC_VERSION/ports ;;
223 x86_64)
224 ccflags="-m64" ;;
225 esac
226 # Disable linking to libgcc_eh
227 cd eglibc-$EGLIBC_VERSION
228 cp Makeconfig Makeconfig.orig
229 sed -e 's/-lgcc_eh//g' Makeconfig.orig > Makeconfig
230 cd ..
231 mkdir -p eglibc-build && cd eglibc-build
232 # config.cache
233 cat > config.cache << EOT
234 libc_cv_forced_unwind=yes
235 libc_cv_c_cleanup=yes
236 libc_cv_gnu89_inline=yes
237 EOT
238 BUILD_CC="gcc" \
239 CC="${TARGET}-gcc $ccflags" \
240 AR="${TARGET}-ar" \
241 RANLIB="${TARGET}-ranlib" \
242 ../eglibc-$EGLIBC_VERSION/configure \
243 --prefix=/usr \
244 --libexec=/usr/lib/eglibc \
245 --host=$TARGET \
246 --with-headers=$sysroot/usr/include \
247 --with-binutils=$tools/bin \
248 --enable-kernel=2.6.32 \
249 --with-__thread \
250 --without-gd \
251 --without-cvs \
252 --cache-file=config.cache \
253 $EGLIBC_ARGS &&
254 make || exit 1
255 make install_root=$sysroot install
256 }
258 # 5. GCC final
259 gcc_final() {
260 init_compile
261 if [ ! -d "gcc-$GCC_VERSION" ]; then
262 echo "Extracting: $GCC_TARBALL"
263 tar xjf $SRC/$GCC_TARBALL
264 fi
265 echo "Configure: $GCC_FINAL_ARGS"
266 rm -rf gcc-build
267 mkdir -p gcc-build && cd gcc-build
268 AR=ar \
269 ../gcc-$GCC_VERSION/configure \
270 --prefix=$tools \
271 --libexec=$tools/lib \
272 --target=$TARGET \
273 --enable-shared \
274 --enable-c99 \
275 --enable-long-long \
276 --enable-__cxa_atexit \
277 --with-system-zlib \
278 --enable-plugin \
279 --disable-multilib \
280 --disable-libssp \
281 --disable-checking \
282 --disable-werror \
283 --with-pkgversion="SliTaz" \
284 --with-bugurl="https://bugs.slitaz.org/" \
285 --with-sysroot=$sysroot \
286 $GCC_FINAL_ARGS &&
287 make AS_FOR_TARGET="${TARGET}-as" \
288 LD_FOR_TARGET="${TARGET}-ld" || exit 1
289 make install
290 echo "cross: GCC final compiled on: $(date)"
291 }
293 #
294 # Commands
295 #
297 case "$1" in
298 howto|man)
299 doc=/usr/share/doc/cookutils/cross.txt
300 [ -f "$doc" ] && less -E $doc ;;
301 info)
302 init_compile
303 CC=${TARGET}-gcc
304 echo -e "\nCross Toolchain information" && separator
305 [ "$config" ] && echo "Config file : $config"
306 cat << EOT
307 Target arch : $ARCH
308 C Compiler : $CC
309 Build directory : $WORK
310 Tools prefix : $tools/bin
311 Arch sysroot : $sysroot
312 EOT
313 separator && echo ""
314 echo "GCC version" && separator
315 if [ -x "$tools/bin/$CC" ]; then
316 $CC -v
317 else
318 echo "No C compiler. To build a toolchain run: cross compile"
319 echo "Missing: $tools/bin/$CC"
320 fi
321 separator && echo "" ;;
322 testsuite)
323 init_compile
324 echo "[COMPILING] $TARGET-gcc -v -Wall -o test.out test.c" \
325 | tee $logdir/testsuite.log
326 echo 'int main() { return 0; }' > test.c
327 $TARGET-gcc -v -Wall -o test.out test.c 2>&1 | tee -a $logdir/testsuite.log
328 if [ -x /usr/bin/file ]; then
329 echo -e "\n[CHECKING] file test.out" | tee -a $logdir/testsuite.log
330 file test.out | tee -a $logdir/testsuite.log
331 fi
332 echo -e "\n[CHECKING] readelf -h test.out" | tee -a $logdir/testsuite.log
333 readelf -h test.out | tee -a $logdir/testsuite.log ;;
334 check)
335 echo "Checking: build system packages"
336 for pkg in slitaz-toolchain mpfr mpfr-dev gmp gmp-dev mpc-library \
337 gawk autoconf; do
338 if [ ! -d "/var/lib/tazpkg/installed/$pkg" ]; then
339 echo "Missing packages: $pkg"
340 if [ -x /usr/sbin/spk-add ]; then
341 spk-add $pkg
342 else
343 tazpkg -gi $pkg
344 fi
345 fi
346 done
347 echo "Using: --with-sysroot=$sysroot" ;;
348 download)
349 download_src ;;
350 show-log)
351 pkg=$2
352 log=$logdir/$pkg.log
353 if [ ! -f "$log" ]; then
354 echo "No log file found for: $pkg" && exit 1
355 fi
356 less -E $log ;;
357 binutils)
358 rm -f $logdir/binutils.log
359 binutils 2>&1 | tee $logdir/binutils.log ;;
360 gcc-static)
361 gcc_static 2>&1 | tee $logdir/gcc-static.log ;;
362 linux-headers)
363 linux_headers 2>&1 | tee $logdir/linux-headers.log ;;
364 glibc)
365 glibc 2>&1 | tee $logdir/glibc.log ;;
366 eglibc)
367 eglibc 2>&1 | tee $logdir/eglibc.log ;;
368 gcc-final)
369 gcc_final 2>&1 | tee $logdir/gcc-final.log ;;
370 compile)
371 # Compile the full toolchain.
372 time=$(date +%s)
373 init_compile
374 echo "Compile start: $(date)" | tee $logdir/compile.log
375 download_src
376 binutils 2>&1 | tee $logdir/binutils.log
377 gcc_static 2>&1 | tee $logdir/gcc-static.log
378 linux_headers 2>&1 | tee $logdir/linux-headers.log
379 case "$ARCH" in
380 arm) eglibc 2>&1 | tee $logdir/eglibc.log ;;
381 x86_64) glibc 2>&1 | tee $logdir/glibc.log ;;
382 esac
383 gcc_final 2>&1 | tee $logdir/gcc-final.log
384 echo ""
385 echo "Compile end : $(date)" | tee -a $logdir/compile.log
386 time=$(($(date +%s) - $time))
387 sec=$time
388 div=$(( ($time + 30) / 60))
389 [ "$div" != 0 ] && min="~ ${div}m"
390 echo "Build time : ${sec}s $min" | tee -a $logdir/compile.log
391 echo "" ;;
392 clean)
393 echo -n "Removing all source files..."
394 rm -rf $WORK/source && status
395 [ "$log" ] && rm -f $WORK/log/*.log ;;
396 clean-tools)
397 # Remove crap :-)
398 init_compile
399 echo "Cleaning : $tools ($(du -sh $tools | awk '{print $1}'))"
400 for file in share/info share/man share/local
401 do
402 echo -n "Removing : $file"
403 rm -rf $tools/$file && status
404 done
405 echo -n "Stripping : shared libs and binaries"
406 find $tools/bin -type f -exec strip -s '{}' 2>/dev/null \;
407 find $tools/lib -name cc1* -exec strip -s '{}' 2>/dev/null \;
408 find $tools/lib -name lto* -exec strip -s '{}' 2>/dev/null \;
409 #find $sysroot -name "*.so*" -exec ${TARGET}-strip -s '{}' 2>/dev/null \;
410 sleep 1 && status
411 echo -n "Tools size : " && du -sh $tools | awk '{print $1}' ;;
412 gen-prebuilt)
413 # Create a prebuilt cross toolchain tarball.
414 init_compile
415 date=$(date "+%Y%m%d")
416 package="slitaz-$ARCH-toolchain-$date"
417 tarball="$package.tar.bz2"
418 cd /cross
419 mkdir -p $package/$ARCH || exit 1
420 echo ""
421 echo -n "Copying $ARCH to: $package"
422 cp -a $ARCH/tools $package/$ARCH
423 cp -a $ARCH/sysroot $package/$ARCH
424 status
425 prebuilt_readme
426 echo -n "Creating prebuilt $ARCH toolchain tarball..."
427 tar cjf $tarball $package
428 status
429 rm -rf $package
430 size=$(du -sh $tarball | awk '{print $1}')
431 echo "Tarball path: $(pwd)/$tarball"
432 echo "Tarball size: $size"
433 echo "" ;;
434 *)
435 usage ;;
436 esac