unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / build.sh
1 #!/bin/sh
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 cmd_prefix=${CROSS_COMPILE-arm-926ejs-linux-gnu-}
14
15 error() {
16     if [ -n "${target}" ];then
17         echo "${target} build aborted"
18     fi
19 }
20
21 build_host_tools() {
22     echo "Building host tools in $tools_dir"
23
24     local wd="$PWD"
25     local tcldirs="/usr/lib/tcl /usr/local/lib/tcl"
26     local config_opts=""
27     for d in $tcldirs;do
28         if [ -d "$d" ];then
29             config_opts="$config_opts --with-tcl=${d%lib/tcl}"
30             break
31         fi
32     done
33     if [ -z "$config_opts" ];then
34         for d in /usr/lib/tcl*;do
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     mkdir -p "$tools_dir"
44     cd "$tools_dir"
45     sh ../src/configure $config_opts
46     make
47     cd "$wd"
48 }
49
50 if [ `uname -s` = Linux ];then
51     PATH="/usr/local/arm/cross-gcc-4.2.0/i686-pc-linux-gnu/bin:$PATH"
52 fi
53
54 while getopts "$options" opt;do
55     case $opt in
56         c)
57             clean=true
58             ;;
59         n)
60             doit=false
61             make_opts="${make_opts} -n"
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 *.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} COMMAND_PREFIX=${cmd_prefix} clean
150     fi
151
152     $quiet || echo "Compiling ${target}"
153     [ -d "${build_dir}" ]
154     make -C "${build_dir}" COMMAND_PREFIX=${cmd_prefix} ${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 ! echo "$bootstrap_addr" | grep -i '^[0-9a-f]\{4\}0[0-7]';then
161 #       if ! ${cmd_prefix}nm "${inst_dir}/bin/redboot.elf" \
162 #           | grep 'Now_in_SDRAM' | grep -i '^[0-9a-f]\{4\}0[0-7]';then
163             echo "ERROR: Bootstrap does not fit into first NAND page!"
164             echo $bootstrap_addr
165             exit 1
166         fi
167     fi
168     echo "${target} build finished"
169
170 done
171 trap - 0