Bugfix Release STK5 2010-10-04
[karo-tx-redboot.git] / build.sh
1 #!/bin/bash
2 options="cnqrR"
3 release=false
4 tools_dir="$PWD/tools/bin"
5 ecosconfig="$tools_dir/tools/configtool/standalone/common/ecosconfig"
6 src_dir="$PWD/packages"
7 quiet=false
8 clean=false
9 rebuild=false
10 doit=true
11 make_opts=
12
13 error() {
14     if [ -n "${target}" ];then
15         echo "${target} build aborted"
16     fi
17 }
18
19 build_host_tools() {
20     echo "Building host tools in $tools_dir"
21     # Debian packages tcl-dev and tk-dev are required for this build
22     local wd="$PWD"
23     local tcldirs="/usr/lib/tcl /usr/local/lib/tcl"
24     local config_opts=""
25     for d in $tcldirs;do
26         if [ -d "$d" ];then
27             config_opts="$config_opts --with-tcl=${d%lib/tcl}"
28             break
29         fi
30     done
31     if [ -z "$config_opts" ];then
32         for d in /usr/lib/tcl*;do
33             [ -d "$d" ] || continue
34             config_opts="$config_opts --with-tcl-version=${d##*tcl}"
35         done
36     fi
37     if [ -z "$config_opts" ];then
38         echo "No Tcl installation found"
39         exit 1
40     fi
41
42     export TCL_INC_DIR="$(. /usr/lib/tclConfig.sh; echo $TCL_INCLUDE_SPEC | sed 's/^-I//')"
43     mkdir -p "$tools_dir"
44     cd "$tools_dir"
45     sh ../src/configure $config_opts
46     make
47     cd "$wd"
48 }
49
50 while getopts "$options" opt;do
51     case $opt in
52         c)
53             clean=true
54             ;;
55         n)
56             doit=false
57             make_opts="${make_opts} -n"
58             ;;
59         q)
60             quiet=true
61             ;;
62         r)
63             rebuild=true
64             ;;
65         R)
66             release=true
67             ;;
68         '?')
69             exit 1
70             ;;
71         *)
72             echo "Unsupported option '$opt'"
73             exit 1
74     esac
75 done
76 shift $(($OPTIND - 1))
77
78 if [ $# -gt 0 ];then
79     targets="$@"
80 else
81     targets=$(cd config;ls *.ecc)
82 fi
83
84 set -e
85 trap error 0
86 conf_dir="$PWD/config"
87 [ -d build ] || mkdir -p build
88 cd build
89 wd=$PWD
90 if [ ! -x "${ecosconfig}" ];then
91     build_host_tools
92 fi
93 for target in ${targets};do
94     target="${target%.ecc}"
95     if [ ! -d "${target}" ];then
96         echo "Creating build dir ${target}"
97         mkdir -p "${target}"
98     fi
99
100     cd "${target}"
101     echo "Building ${target}"
102
103     build_dir="$PWD/${target}_build"
104     inst_dir="$PWD/${target}_install"
105
106     $quiet || echo "Checking configuration ${target}"
107     cp -p "${conf_dir}/${target}.ecc" "${conf_dir}/${target}.ecc.bak"
108     echo ecosconfig --srcdir="$src_dir" --config="${conf_dir}/${target}.ecc" check
109     if $doit;then
110         stty -isig # prevent CTRL-C from trashing the config file
111         set +e
112         "${ecosconfig}" --srcdir="$src_dir" --config="${conf_dir}/${target}.ecc" check
113         if [ $? != 0 ];then
114             mv "${conf_dir}/${target}.ecc.bak" "${conf_dir}/${target}.ecc"
115             exit 1
116         fi
117         set -e
118         stty isig
119     fi
120
121     if $rebuild;then
122         echo "Removing build dir ${build_dir} and ${inst_dir}"
123         $doit && rm -rf "${build_dir}" "${inst_dir}"
124     fi
125     if [ ! -d "${build_dir}" ];then
126         $quiet || echo "Creating build tree for ${target}"
127         echo mkdir "${build_dir}"
128         $doit && mkdir "${build_dir}"
129     fi
130     $doit && cd "${build_dir}"
131     echo ecosconfig --srcdir="$src_dir" --prefix="${inst_dir}" \
132         --config="${conf_dir}/${target}.ecc" tree
133     if $doit;then
134         stty -isig
135         "${ecosconfig}" --srcdir="$src_dir" --prefix="${inst_dir}" \
136             --config="${conf_dir}/${target}.ecc" tree
137         stty isig
138         rm -f "${target}" ../../current && ln -svf "${target}" ../../current
139         rm -f ../install && ln -svf "${target}_install" ../install
140         rm -f ../build && ln -svf "${target}_build" ../build
141     fi
142
143     if $clean;then
144         $quiet || echo "Cleaning up build tree for ${target}"
145         make ${make_opts} clean
146     fi
147
148     $quiet || echo "Compiling ${target}"
149     [ -d "${build_dir}" ]
150     make -C "${build_dir}" ${make_opts}
151
152     cd $wd
153     if $doit && [ -s "${inst_dir}/bin/redboot.elf" ];then
154         bootstrap_addr="$(${cmd_prefix}nm "${inst_dir}/bin/redboot.elf" \
155             | sed '/Now_in_SDRAM/!d;s/ .*$//')"
156         if [ -n "$bootstrap_addr" ] && ! echo "$bootstrap_addr" | grep -i '^[0-9a-f]\{4\}0[0-7]';then
157             echo "ERROR: Bootstrap does not fit into first NAND page!"
158             echo $bootstrap_addr
159             exit 1
160         fi
161     fi
162     echo "${target} build finished"
163
164 done
165 trap - 0