cookutils view cross @ rev 400

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