deptest: Do not clobber the base build and staging dirs
[openwrt/svn-archive/archive.git] / scripts / deptest.sh
1 #!/bin/bash
2 #
3 # Automated OpenWrt package dependency checker
4 #
5 # Copyright (C) 2009-2010 OpenWrt.org
6 #
7 # This is free software, licensed under the GNU General Public License v2.
8 # See /LICENSE for more information.
9 #
10
11 SCRIPTDIR="$(dirname "$0")"
12 [ "${SCRIPTDIR:0:1}" = "/" ] || SCRIPTDIR="$PWD/$SCRIPTDIR"
13 BASEDIR="$SCRIPTDIR/.."
14
15 DIR="$BASEDIR/tmp/deptest"
16 STAMP_DIR_SUCCESS="$DIR/stamp-success"
17 STAMP_DIR_FAILED="$DIR/stamp-failed"
18 STAMP_DIR_BLACKLIST="$DIR/stamp-blacklist"
19 BUILD_DIR="$DIR/build_dir/target"
20 BUILD_DIR_HOST="$DIR/build_dir/host"
21 KERNEL_BUILD_DIR="$DIR/build_dir/linux"
22 STAGING_DIR="$DIR/staging_dir/target"
23 STAGING_DIR_HOST="$DIR/staging_dir/host"
24 STAGING_DIR_HOST_TMPL="$DIR/staging_dir_host_tmpl"
25 BIN_DIR="$DIR/staging_dir/bin_dir"
26 LOG_DIR="$DIR/logs"
27
28 die()
29 {
30 echo "$@"
31 exit 1
32 }
33
34 usage()
35 {
36 echo "deptest.sh [OPTIONS] [PACKAGES]"
37 echo
38 echo "OPTIONS:"
39 echo " --lean Run a lean test. Do not clean the build directory for each"
40 echo " package test."
41 echo " --force Force a test, even if a success/blacklist stamp is available"
42 echo " -j X Number of make jobs"
43 echo
44 echo "PACKAGES are packages to test. If not specified, all installed packages"
45 echo "will be tested."
46 }
47
48 deptest_make()
49 {
50 local target="$1"
51 shift
52 local logfile="$1"
53 shift
54 make -j$nrjobs "$target" \
55 BUILD_DIR="$BUILD_DIR" \
56 BUILD_DIR_HOST="$BUILD_DIR_HOST" \
57 KERNEL_BUILD_DIR="$KERNEL_BUILD_DIR" \
58 BIN_DIR="$BIN_DIR" \
59 STAGING_DIR="$STAGING_DIR" \
60 STAGING_DIR_HOST="$STAGING_DIR_HOST" \
61 FORCE_HOST_INSTALL=1 \
62 V=99 "$@" >"$LOG_DIR/$logfile" 2>&1
63 }
64
65 clean_kernel_build_dir()
66 {
67 # delete everything, except the kernel build dir "linux-X.X.X"
68 (
69 cd "$KERNEL_BUILD_DIR" || die "Failed to enter kernel build dir"
70 for entry in *; do
71 [ -z "$(echo "$entry" | egrep -e '^linux-*.*.*$')" ] || continue
72 rm -rf "$entry" || die "Failed to clean kernel build dir"
73 done
74 )
75 }
76
77 test_package() # $1=pkgname
78 {
79 local pkg="$1"
80 [ -n "$pkg" -a -z "$(echo "$pkg" | grep -e '/')" -a "$pkg" != "." -a "$pkg" != ".." ] || \
81 die "Package name \"$pkg\" contains illegal characters"
82 local SELECTED=
83 for conf in `grep CONFIG_PACKAGE tmp/.packagedeps | grep -E "[ /]$pkg\$" | sed -e 's,package-$(\(CONFIG_PACKAGE_.*\)).*,\1,'`; do
84 grep "$conf=" .config > /dev/null && SELECTED=1 && break
85 done
86 local STAMP_SUCCESS="$STAMP_DIR_SUCCESS/$pkg"
87 local STAMP_FAILED="$STAMP_DIR_FAILED/$pkg"
88 local STAMP_BLACKLIST="$STAMP_DIR_BLACKLIST/$pkg"
89 rm -f "$STAMP_FAILED"
90 [ -f "$STAMP_SUCCESS" -a $force -eq 0 ] && return
91 rm -f "$STAMP_SUCCESS"
92 [ -n "$SELECTED" ] || {
93 echo "Package $pkg is not selected"
94 return
95 }
96 [ -f "$STAMP_BLACKLIST" -a $force -eq 0 ] && {
97 echo "Package $pkg is blacklisted"
98 return
99 }
100 echo "Testing package $pkg..."
101 rm -rf "$STAGING_DIR" "$STAGING_DIR_HOST"
102 mkdir -p "$STAGING_DIR"
103 cp -al "$STAGING_DIR_HOST_TMPL" "$STAGING_DIR_HOST"
104 [ $lean_test -eq 0 ] && {
105 rm -rf "$BUILD_DIR" "$BUILD_DIR_HOST"
106 clean_kernel_build_dir
107 }
108 mkdir -p "$BUILD_DIR" "$BUILD_DIR_HOST"
109 deptest_make "package/$pkg/compile" "$(basename $pkg).log"
110 if [ $? -eq 0 ]; then
111 touch "$STAMP_SUCCESS"
112 else
113 touch "$STAMP_FAILED"
114 echo "Building package $pkg failed!"
115 fi
116 }
117
118 # parse commandline options
119 packages=
120 lean_test=0
121 force=0
122 nrjobs=1
123 while [ $# -ne 0 ]; do
124 case "$1" in
125 --help|-h)
126 usage
127 exit 0
128 ;;
129 --lean)
130 lean_test=1
131 ;;
132 --force)
133 force=1
134 ;;
135 -j*)
136 if [ -n "${1:2}" ]; then
137 nrjobs="${1:2}"
138 else
139 shift
140 nrjobs="$1"
141 fi
142 ;;
143 *)
144 packages="$packages $1"
145 ;;
146 esac
147 shift
148 done
149
150 [ -f "$BASEDIR/include/toplevel.mk" ] || \
151 die "Error: Could not find buildsystem base directory"
152 [ -f "$BASEDIR/.config" ] || \
153 die "The buildsystem is not configured. Please run make menuconfig."
154 cd "$BASEDIR" || die "Failed to enter base directory"
155
156 mkdir -p "$STAMP_DIR_SUCCESS" "$STAMP_DIR_FAILED" "$STAMP_DIR_BLACKLIST" \
157 "$BIN_DIR" "$LOG_DIR"
158
159 bootstrap_deptest_make()
160 {
161 local target="$1"
162 shift
163 local logfile="bootstrap-deptest-$(echo "$target" | tr / -).log"
164 echo "deptest-make $target"
165 deptest_make "$target" "$logfile" "$@" || \
166 die "make $target failed, please check $logfile"
167 }
168
169 bootstrap_native_make()
170 {
171 local target="$1"
172 shift
173 local logfile="bootstrap-native-$(echo "$target" | tr / -).log"
174 echo "make $target"
175 make -j$nrjobs "$target" \
176 V=99 "$@" >"$LOG_DIR/$logfile" 2>&1 || \
177 die "make $target failed, please check $logfile"
178 }
179
180 [ -d "$STAGING_DIR_HOST_TMPL" ] || {
181 echo "Bootstrapping build environment..."
182 rm -rf "$STAGING_DIR" "$STAGING_DIR_HOST" "$BUILD_DIR" "$BUILD_DIR_HOST" "$KERNEL_BUILD_DIR"
183 mkdir -p "$STAGING_DIR" "$STAGING_DIR_HOST" \
184 "$BUILD_DIR" "$BUILD_DIR_HOST" "$KERNEL_BUILD_DIR"
185 bootstrap_native_make tools/install
186 bootstrap_native_make toolchain/install
187 bootstrap_deptest_make tools/install
188 bootstrap_deptest_make target/linux/install
189 cp -al "$STAGING_DIR_HOST" "$STAGING_DIR_HOST_TMPL"
190 rm -rf "$STAGING_DIR" "$STAGING_DIR_HOST" "$BUILD_DIR" "$BUILD_DIR_HOST"
191 echo "Build environment OK."
192 }
193
194 if [ -z "$packages" ]; then
195 # iterate over all packages
196 for pkg in `cat tmp/.packagedeps | grep CONFIG_PACKAGE | grep -v curdir | sed -e 's,.*[/=]\s*,,' | sort -u`; do
197 test_package "$pkg"
198 done
199 else
200 # only check the specified packages
201 for pkg in $packages; do
202 test_package "$pkg"
203 done
204 fi