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