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