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