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