gitarre
[projects.git] / tools / make / configure
1 #!/bin/bash
2 set -e
3
4 function echo_usage {
5         echo "usage: configure [options] <package>"
6         echo "  -a <arch>"
7         echo "  -b : extern build in ../build directory"
8         echo "  -c <compile_dir>"
9         echo "  -C <cross environment>"
10         echo "  -i : install dir. default: /"
11         echo "  -I : print this info"
12         echo "  -t <compile type: NONE, CPP, CMAKE, PDF> default: NONE"
13         echo "  -v : set version"
14         echo "  -V : version set by project"
15 }
16
17 function create_build {
18         pushd $BUILD_DIR >/dev/null
19                 if [ "$compile_type" == "PDF" ]
20                 then
21                         echo "make $compile_target" > debian/$paket.build
22                         chmod 755 debian/$paket.build
23                 else
24                         echo "build for compile_type not implemented."
25                         exit 10
26                 fi
27         popd >/dev/null
28 }
29 function cmake_check {
30         pushd ../build >/dev/null
31                 # do checks
32                 if [ -e debian/$paket.build ]
33                 then
34                         echo "cmake is not compatible with other builds. Remove $paket.build"
35                         exit 1
36                 fi
37                 if [ -e debian/$paket.prebuild ]
38                 then
39                         echo "cmake is not compatible with other builds. Remove $paket.prebuild"
40                         exit 2
41                 fi
42                 
43                 if [ -n "$cross" ]
44                 then
45                         # inject cross toolset
46                         echo "set (CMAKE_MODULE_PATH $cross)" > CMakeLists.txt
47                         echo "include(toolset)" >> CMakeLists.txt
48                         cat debian/$paket.cmake >> CMakeLists.txt
49                 else
50                         cp debian/$paket.cmake CMakeLists.txt
51                 fi
52
53                 echo "make" > debian/$paket.build
54                 chmod 755 debian/$paket.build
55                 cmake .
56         popd >/dev/null
57 }
58 function check_unversioned {
59         # has to be called in the projects base directory
60         # result: "M" in stdout or nothing
61         set -e
62         
63         # check if build is running in a sandbox
64         if ! [ -f .cm.ignore ]
65         then
66                 # we create one
67                 echo ".cm.ignore" > .cm.ignore
68                 echo "debian.*" >> .cm.ignore
69                 echo "up.*" >> .cm.ignore
70         fi
71         
72         project=$(pwd)
73         project=${project##*/}
74         pushd .. >/dev/null
75                 mCM $project -b -an > /dev/null
76                 wc=$(wc $project.batch)
77                 rm $project.batch
78                 if [ "${wc:0:5}" != "0 0 0" ]
79                 then
80                         echo "M"
81                 fi
82         popd >/dev/null
83 }
84
85 function set_build {
86         set -e
87         if [ -e .svn ]
88         then
89                 version="0.0"
90                 build=$(svnversion)
91                 build=${build/:/-}
92                 build="$build$(check_unversioned)"
93         elif [ -e .git ]
94         then
95                 tag=$(git describe --tags --match "${paket}_*" 2>/dev/null) || true
96                 if test -z "$tag"
97                 then
98                         # try generic "v_" tag
99                         tag=$(git describe --tags --match "v_*" 2>/dev/null) || true
100                 fi
101                 if test -z "$tag"
102                 then
103                         tag="0.0-0TOP"
104                 fi
105                 changes=$(check_unversioned)
106                 tag="$tag$changes"
107                 version=${tag##*_}
108                 version=${version%%-*}
109                 build=${tag##*-}
110         else
111                 echo "No configuration system found. Cannot determine version."
112                 exit 3
113         fi
114 }
115
116 function check_version {
117         set -e
118         
119         vline=$(head -1 debian/$paket.changelog)
120         vline=${vline%-*}
121         clversion=${vline#*(}
122         if [ $version = "0.0" ]
123         then
124                 version=$clversion
125                 return
126         fi
127
128         # do no check on a modified git repos
129         if [ "${build: -1}" == "M" ]
130         then
131                 return
132         fi
133
134         if [ "$clversion" != $version ]
135         then
136                 echo "version mismatch: git: $version, changelog: $clversion"
137                 exit 4
138         fi
139 }
140
141 ##############################################################################
142 # Main program
143 ##############################################################################
144
145 install_dir="/"
146 compile_type=NONE
147 extern_build=0
148
149 while [ $OPTIND -le "$#" ]
150 do
151         if getopts "a:bB:C:i:It:v:V" opt
152         then
153                 case $opt in
154                         a) ARCH=$OPTARG
155                                 ;;
156                         b) extern_build=1
157                                 ;;
158                         c) compile_dir=$OPTARG
159                                 ;;
160                         C) cross=$OPTARG
161                                 ;;
162                         i)      install_dir=$OPTARG
163                                 ;;
164                         I)      echo_usage
165                                 exit 0
166                                 ;;
167                         t) compile_type=$OPTARG
168                                 ;;
169                         v)      optversion=$OPTARG
170                                 ;;
171                         V)      optversion="0.0"
172                                 ;;
173                         \?) echo "Invalid option: -$OPTARG"
174                                 echo_usage
175                                 exit 5
176                                 ;;
177                         :) echo "Option -$OPTARG requires an argument."
178                                 echo_usage
179                                 exit 6
180                                 ;;
181                 esac
182         else
183                 paket="${!OPTIND}"
184                 eval OPTIND=OPTIND+1
185         fi
186 done
187
188 if [ -z "$paket" ]
189 then
190         echo_usage
191         exit 7
192 fi
193
194 # read build configuration, if existing
195 if [ -f debian/$paket.conf ]
196 then
197         . debian/$paket.conf
198 fi
199
200 if [ -n "$ARCH" -a -n "$cross" ]
201 then
202         echo "invalid options: supply -a for multiarch or -C for explicit cross compile environment"
203         exit 8
204 fi
205
206 if [ ! -e Makefile ]
207 then
208         ln -s /usr/share/mbuild/rules Makefile
209 fi
210
211 # delete changelog and control
212 rm debian/changelog debian/control debian/README.debian debian/copyright 2>/dev/null || true
213
214 # mconfigure builds 2 environment files: setenv.sh + rules.pre
215 echo "paket=$paket" > debian/rules.pre
216 echo "INSTALL_DIR=$install_dir" >> debian/rules.pre
217 if [ -e debian/setenv.sh ]; then rm debian/setenv.sh; fi
218
219 if [ -n "$cross" ]
220 then
221         if [ ${cross:0:1} != "/" ]
222         then
223                 cross="/opt/cross/$cross"
224                 #export $cross
225         fi
226         if [ -f "$cross/setenv.sh" ]
227         then
228                 . "$cross/setenv.sh"
229                 cp $cross/setenv.sh debian/setenv.sh
230         fi
231         if [ -z "$ARCH" ]
232         then
233                 # determine architecture
234                 GNU_ARCH=${cross%-*} # assumed format: DEB_HOST_GNU_TYPE-g++version, e.g. arm-linux-gnueabihf-4.9
235                 GNU_ARCH=${GNU_ARCH##*/}
236                 arch_opt="-t $GNU_ARCH"
237         fi
238 fi
239
240 if [ -n "$ARCH" ]
241 then
242         arch_opt="-a $ARCH"
243 fi
244 dpkg-architecture $arch_opt >> debian/setenv.sh
245
246 echo "paket=$paket" >> debian/setenv.sh
247
248 set_build
249 if [ -z "$optversion" ]
250 then
251         check_version
252 else
253         version=$optversion
254 fi
255 echo "version=$version" >> debian/rules.pre
256 echo "version=$version" >> debian/setenv.sh
257 echo "build=$build" >> debian/rules.pre
258 echo "build=$build" >> debian/setenv.sh
259
260 # get repository name
261 #       tmp=$(pwd)
262 #       pwd=${tmp##*/}
263 #       pwd=${pwd,,*}
264 #       echo "pwd=$pwd" >> debian/rules.pre
265
266 echo "building $paket with version/build=$version-$build"
267
268 pushd debian >/dev/null
269 # ./debian -------------------------------------
270
271 ln -sf /usr/share/mbuild/rules .
272
273 if [ -e tmp ]
274 then
275         rm -rf tmp
276 fi
277
278 # check for pre/post installation scripts
279 if [ -f $paket.preinst ]
280 then
281         echo "add_inst_tgt += debian/tmp/DEBIAN/preinst" >> rules.pre
282 fi
283 if [ -f $paket.postinst ]
284 then
285             echo "add_inst_tgt += debian/tmp/DEBIAN/postinst" >> rules.pre
286 fi
287 if [ -f $paket.prerm ]
288 then
289             echo "add_inst_tgt += debian/tmp/DEBIAN/prerm" >> rules.pre
290 fi
291 if [ -f $paket.postrm ]
292 then
293             echo "add_inst_tgt += debian/tmp/DEBIAN/postrm" >> rules.pre
294 fi
295
296 # check for README.debian
297 if [ -f $paket.README.debian ]
298 then
299             cp $paket.README.debian README.debian
300 fi
301
302 mkdir -p tmp/DEBIAN
303
304 # add export to setenv.sh
305 sed -i "s/^/export /" setenv.sh
306
307 popd >/dev/null
308 # ./. ---------------------------------------------
309
310 # build prepare
311 if [ -x debian/$paket.prepare ]
312 then
313         . debian/setenv.sh
314         debian/$paket.prepare
315 fi
316
317 if [ "$compile_type" == "NONE" ]
318 then
319         echo "BUILD=nobuild" >> debian/rules.pre
320 else
321         echo "BUILD=build" >> debian/rules.pre
322
323         if [ $extern_build -eq 1 ]
324         then
325                 BUILD_DIR=~/build
326                 echo "BUILD_DIR=~/build" >> debian/rules.pre
327                 debian/rules sync
328         fi
329
330         # .build
331         if [ "$compile_type" != "CMAKE" -a "$compile_type" != "OLD" ]
332         then
333                 cp debian/rules.pre $BUILD_DIR/$compile_dir/make.pre
334                 pushd $BUILD_DIR/$compile_dir >/dev/null
335                         ln -sf /usr/share/mbuild/makefile .
336                         if [ -n "$sources_suffix" ]
337                         then
338                                 echo "SOURCES = \\" >> make.pre
339                                 for suf in $sources_suffix
340                                 do
341                                         find . -name "*.$suf" -exec echo "  "{}" \\" >> make.pre \;
342                                 done
343                                 echo >> make.pre
344                         fi
345                 popd >/dev/null
346         fi
347                 
348         if [ -x debian/$paket.prebuild ]
349         then
350                 pushd $BUILD_DIR >/dev/null
351                         . debian/setenv.sh
352                         debian/$paket.prebuild
353                 popd >/dev/null
354         fi
355         if ! [ -x debian/$paket.build -o -f debian/$paket.cmake ]
356         then
357                 create_build
358         fi
359
360         # .cmake
361         if [ -e debian/$paket.cmake ]
362         then
363                 cmake_check
364         fi
365 fi
366
367