cookutils view cross @ rev 374

cross: add support for --config=/path/to/cross.conf
author Christophe Lincoln <pankso@slitaz.org>
date Fri May 11 14:43:34 2012 +0200 (2012-05-11)
parents 6f26ad7a0786
children 95ba9a892bb4
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 Dispaly 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 export PATH=$PATH:$PREFIX/bin
46 export CROSS_COMPILE=$TARGET-
47 source=$WORK/source
48 logdir=$WORK/log
49 mkdir -p $source $logdir $install
50 cd $source
51 }
53 # Get source if not yet in $SRC.
54 download_src() {
55 mkdir -p $SRC && cd $SRC
56 [ -f "binutils-$BINUTILS_VERSION.tar.bz2" ] || wget $BINUTILS_WGET
57 [ -f "linux-$LINUX_VERSION.tar.bz2" ] || wget $LINUX_WGET
58 [ -f "glibc-$GLIBC_VERSION.tar.bz2" ] || wget $GLIBC_WGET
59 [ -f "gcc-$GCC_VERSION.tar.bz2" ] || wget $GCC_WGET
60 [ -f "busybox-$BUSYBOX_VERSION.tar.bz2" ] || wget $BUSYBOX_WGET
61 }
63 # 1. Binutils
64 binutils() {
65 echo "Extracting: binutils-$BINUTILS_VERSION.tar.bz2"
66 tar xjf $SRC/binutils-$BINUTILS_VERSION.tar.bz2
67 # Peer arch options --disable-werror
68 case $ARCH in
69 arm) archopts="--enable-shared" ;;
70 x86_64) archopts="--disable-multilib" ;;
71 esac
72 cd binutils-$BINUTILS_VERSION
73 ./configure \
74 --prefix=$PREFIX \
75 --target=$TARGET \
76 --enable-targets=$BUILD_SYSTEM \
77 $archopts
78 make || exit 1
79 make install
80 }
82 # 2. Kernel headers
83 linux_headers() {
84 echo "Extracting: linux-$LINUX_VERSION.tar.bz2"
85 tar xjf $SRC/linux-$LINUX_VERSION.tar.bz2
86 cd linux-$LINUX_VERSION
87 make mrproper
88 make ARCH=$ARCH headers_check
89 make ARCH=$ARCH headers_install \
90 INSTALL_HDR_PATH=$PREFIX
91 }
93 # 3. GCC static (first pass)
94 gcc_static() {
95 echo "Extracting: gcc-$GCC_VERSION.tar.bz2"
96 tar xjf $SRC/gcc-$GCC_VERSION.tar.bz2
97 # Peer arch options
98 case $ARCH in
99 arm) archopts="" ;;
100 x86_64) archopts="" ;;
101 esac
102 rm -rf gcc-static
103 mkdir gcc-static && cd gcc-static
104 ../gcc-$GCC_VERSION/configure \
105 --prefix=$PREFIX \
106 --libexec=$PREFIX/lib \
107 --target=$TARGET \
108 --disable-shared \
109 --disable-threads \
110 --without-headers \
111 --with-newlib \
112 --enable-languages=c
113 make all-gcc all-target-libgcc || exit 1
114 make install-gcc install-target-libgcc
115 cd $PREFIX/lib/gcc/$TARGET/$GCC_VERSION
116 echo "Creating symlink forstatic libgcc: libgcc_eh.a"
117 rm -f libgcc_eh.a && ln -s libgcc.a libgcc_eh.a
118 }
120 # 4. GNU Glibc
121 glibc() {
122 echo "Extracting: glibc-$GLIBC_VERSION.tar.bz2"
123 tar xjf $SRC/glibc-$GLIBC_VERSION.tar.bz2
124 [ "$continue" ] || rm -rf glibc-build
125 # Peer arch options
126 case $ARCH in
127 arm)
128 archopts=""
129 [ -f "$SRC/glibc-ports-$GLIBC_VERSION.tar.bz2" ] || wget \
130 http://ftp.gnu.org/gnu/libc/glibc-ports-$GLIBC_VERSION.tar.bz2 \
131 -O $SRC/glibc-ports-$GLIBC_VERSION.tar.bz2 || exit 1
132 echo "Extracting: glibc-ports-$GLIBC_VERSION.tar.bz2"
133 rm -rf glibc-$GLIBC_VERSION/ports
134 tar xjf $SRC/glibc-ports-$GLIBC_VERSION.tar.bz2
135 mv glibc-ports-$GLIBC_VERSION glibc-$GLIBC_VERSION/ports ;;
136 x86_64)
137 archopts="" ;;
138 esac
139 mkdir -p glibc-build && cd glibc-build
140 BUILD_CC="gcc" \
141 CC="$PREFIX/bin/$TARGET-gcc" \
142 libc_cv_forced_unwind=yes \
143 libc_cv_c_cleanup=yes \
144 ../glibc-$GLIBC_VERSION/configure \
145 --prefix=$PREFIX \
146 --host=$TARGET \
147 --with-headers=$PREFIX/include \
148 --with-binutils=$PREFIX/bin \
149 --enable-add-ons
150 make || exit 1
151 make install
152 cd $PREFIX/$TARGET
153 rm -rf lib include
154 ln -s ../lib lib
155 ln -s ../include include
156 }
158 # 5. GCC final
159 gcc_final() {
160 if [ ! -d "gcc-$GCC_VERSION" ]; then
161 echo "Extracting: gcc-$GCC_VERSION.tar.bz2"
162 tar xjf $SRC/gcc-$GCC_VERSION.tar.bz2
163 fi
164 # Peer arch options
165 case $ARCH in
166 arm) archopts="" ;;
167 x86_64) archopts="" ;;
168 esac
169 rm -rf gcc-build
170 mkdir gcc-build && cd gcc-build
171 ../gcc-$GCC_VERSION/configure \
172 --prefix=$PREFIX \
173 --libexec=$PREFIX/lib \
174 --target=$TARGET \
175 --enable-shared \
176 --enable-languages=c,c++ \
177 --enable-c99 \
178 --enable-long-long \
179 --enable-__cxa_atexit \
180 --with-pkgversion="SliTaz"
181 make || exit 1
182 make install
183 }
185 # Build Busybox to we can create prebuild tiny rootfs image and boot
186 # from NFS ?
187 cross_busybox() {
188 echo "Extracting: busybox-$BUSYBOX_VERSION.tar.bz2"
189 tar xjf $SRC/busybox-$BUSYBOX_VERSION.tar.bz2
190 cd busybox-$BUSYBOX_VERSION
191 # CROSS_COMPILE is exported via init_compile, but be sure.
192 make CROSS_COMPILE=$TARGET- defconfig
193 make CROSS_COMPILE=$TARGET- || exit 1
194 make CROSS_COMPILE=$TARGET- install
195 chmod 4755 _install/bin/busybox
196 readelf -h _install/bin/busybox
197 }
199 #
200 # Commands
201 #
203 case "$1" in
204 howto|man)
205 doc=/usr/share/doc/cookutils/cross.txt
206 [ -f "$doc" ] && less -E $doc ;;
207 info)
208 init_compile
209 CC=${TARGET}-gcc
210 echo -e "\nCross Toolchain iformation" && separator
211 [ "$config" ] && echo "Config file : $config"
212 cat << EOT
213 Target arch : $ARCH
214 C Compiler : $CC
215 Additonal path : /usr/cross/$ARCH/bin
216 Build directory : $WORK
217 EOT
218 separator && echo ""
219 echo "GCC version" && separator
220 if [ -x "$PREFIX/$CC" ]; then
221 $CC -v
222 else
223 echo "No C compiler. To build a toolchain run: cross compile"
224 fi
225 separator && echo "" ;;
226 testsuite)
227 init_compile
228 echo "[COMPILING] $TARGET-gcc -v -Wall -o test.out test.c" \
229 | tee $logdir/testsuite.log
230 echo 'int main() { return 0; }' > test.c
231 $TARGET-gcc -v -Wall -o test.out test.c 2>&1 | tee -a $logdir/testsuite.log
232 if [ -x /usr/bin/file ]; then
233 echo -e "\n[CHECKING] file test.out" | tee -a $logdir/testsuite.log
234 file test.out | tee -a $logdir/testsuite.log
235 fi
236 echo -e "\n[CHECKING] readelf -h test.out" | tee -a $logdir/testsuite.log
237 readelf -h test.out | tee -a $logdir/testsuite.log ;;
238 check-env)
239 for pkg in mpfr mpfr-dev gmp gmp-dev mpc-library gawk autoconf
240 do
241 if [ ! -d "/var/lib/tazpkg/installed/$pkg" ]; then
242 echo "Missing packages: $pkg"
243 [ "$install" ] && tazpkg -gi $pkg
244 fi
245 done ;;
246 download)
247 download_src ;;
248 clean)
249 echo -n "Remove all source files..."
250 rm -rf $WORK/source/* && status
251 [ "$log" ] && rm -f $WORK/log/*.log
252 echo "To clean chroot: rm -rf $PREFIX" ;;
253 show-log)
254 pkg=$2
255 log=$logdir/$pkg.log
256 if [ ! -f "$log" ]; then
257 echo "No log file found for: $pkg" && exit 1
258 fi
259 less -E $log ;;
260 binutils)
261 init_compile
262 rm -f $logdir/binutils.log
263 binutils 2>&1 | tee $logdir/binutils.log ;;
264 linux-headers)
265 init_compile
266 linux_headers 2>&1 | tee $logdir/linux-headers.log ;;
267 gcc-static)
268 init_compile
269 gcc_static 2>&1 | tee $logdir/gcc-static.log ;;
270 glibc)
271 init_compile
272 glibc 2>&1 | tee $logdir/glibc.log ;;
273 gcc-final)
274 init_compile
275 gcc_final 2>&1 | tee $logdir/gcc-final.log ;;
276 busybox)
277 init_compile
278 cross_busybox 2>&1 | tee $logdir/busybox.log ;;
279 compile)
280 init_compile
281 echo "Compile start: $(date)" | tee $logdir/compile.log
282 download_src
283 binutils 2>&1 | tee $logdir/binutils.log
284 linux_headers 2>&1 | tee $logdir/linux-headers.log
285 gcc_static 2>&1 | tee $logdir/gcc-static.log
286 glibc 2>&1 | tee $logdir/glibc.log
287 gcc_final 2>&1 | tee $logdir/gcc-final.log
288 echo ""
289 echo "Compile end : $(date)" | tee -a $logdir/compile.log
290 echo "" ;;
291 clean-tools)
292 # Remove crap :-)
293 init_compile
294 echo "Cleaning : $PREFIX"
295 for dir in info man locale
296 do
297 echo -n "Removing : $dir"
298 rm -rf $PREFIX/share && status
299 done
300 echo -n "Stripping : binaries"
301 for bin in $PREFIX/bin/${TARGET}-*
302 do
303 [ "$bin" == "$PREFIX/bin/${TARGET}-strip" ] && continue
304 if [ -x "$bin" ]; then
305 ${TARGET}-strip -s $bin 2>/dev/null
306 fi
307 done && status
308 echo -n "Tools size: " && du -sh $PREFIX | awk '{print $1}' ;;
309 gen-rootfs)
310 #
311 # TESTING
312 #
313 # Create a bootable rootfs ? dd for an HD image ?
314 init_compile
315 rootfs=/tmp/cross/rootfs
316 tarball="rootfs.tar.bz2"
317 rm -rf $rootfs && mkdir -p $rootfs
318 cd /tmp/cross
319 echo -n "Installing SliTaz base files..."
320 tar xzf $SRC/slitaz-base-files-5.2.tar.gz
321 cp -a slitaz-base-files-*/rootfs/* $rootfs
322 status
323 echo -n "Installing Busybox..."
324 cp -a $source/busybox-$BUSYBOX_VERSION/_install/* $rootfs
325 status
326 echo -n "Creating tarball: $tarball"
327 tar cjf $tarball rootfs
328 status
329 echo -n "Moving rootfs to: $WORK"
330 mv $tarball $WORK
331 status
332 du -sh $WORK/$tarball
333 rm -rf /tmp/cross ;;
334 *)
335 usage ;;
336 esac