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