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 "--- 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                         d) 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 # read build configuration, if existing
232 #if [ -f debian/$paket.conf ]
233 #then
234 #       . debian/$paket.conf
235 #fi
236
237 if [ -n "$ARCH" -a -n "$cross" ]
238 then
239         echo "invalid options: supply -a for multiarch or -C for explicit cross compile environment"
240         exit 8
241 fi
242
243 if [ ! -e Makefile ]
244 then
245         ln -s /usr/share/mbuild/rules Makefile
246 fi
247
248 # delete changelog and control
249 rm debian/changelog debian/control debian/README.debian debian/copyright 2>/dev/null || true
250
251 # mconfigure builds 2 environment files: setenv.sh + rules.pre
252 echo "paket=$paket" > debian/rules.pre
253 echo "INSTALL_DIR=$install_dir" >> debian/rules.pre
254 if [ -e debian/setenv.sh ]; then rm debian/setenv.sh; fi
255
256 if [ -n "$cross" ]
257 then
258         if [ ${cross:0:1} != "/" ]
259         then
260                 cross="/opt/cross/$cross"
261                 #export $cross
262         fi
263         if [ -f "$cross/setenv.sh" ]
264         then
265                 . "$cross/setenv.sh"
266                 cp $cross/setenv.sh debian/setenv.sh
267         fi
268         if [ -z "$ARCH" ]
269         then
270                 # determine architecture
271                 GNU_ARCH=${cross%-*} # assumed format: DEB_HOST_GNU_TYPE-g++version, e.g. arm-linux-gnueabihf-4.9
272                 GNU_ARCH=${GNU_ARCH##*/}
273                 arch_opt="-t $GNU_ARCH"
274         fi
275 fi
276
277 if [ -n "$ARCH" ]
278 then
279         arch_opt="-a $ARCH"
280 fi
281 dpkg-architecture $arch_opt >> debian/setenv.sh
282
283 echo "paket=$paket" >> debian/setenv.sh
284
285 set_build
286 if [ -z "$optversion" ]
287 then
288         check_version
289 else
290         version=$optversion
291 fi
292 echo "version=$version" >> debian/rules.pre
293 echo "version=$version" >> debian/setenv.sh
294 echo "build=$build" >> debian/rules.pre
295 echo "build=$build" >> debian/setenv.sh
296
297 # get repository name
298 #       tmp=$(pwd)
299 #       pwd=${tmp##*/}
300 #       pwd=${pwd,,*}
301 #       echo "pwd=$pwd" >> debian/rules.pre
302
303 echo "building $paket with version/build=$version-$build"
304
305 pushd debian >/dev/null
306 # ./debian -------------------------------------
307
308 ln -sf /usr/share/mbuild/rules .
309
310 if [ -e tmp ]
311 then
312         rm -rf tmp
313 fi
314
315 # check for pre/post installation scripts
316 if [ -f $paket.preinst ]
317 then
318         echo "add_inst_tgt += debian/tmp/DEBIAN/preinst" >> rules.pre
319 fi
320 if [ -f $paket.postinst ]
321 then
322             echo "add_inst_tgt += debian/tmp/DEBIAN/postinst" >> rules.pre
323 fi
324 if [ -f $paket.prerm ]
325 then
326             echo "add_inst_tgt += debian/tmp/DEBIAN/prerm" >> rules.pre
327 fi
328 if [ -f $paket.postrm ]
329 then
330             echo "add_inst_tgt += debian/tmp/DEBIAN/postrm" >> rules.pre
331 fi
332
333 # check for README.debian
334 if [ -f $paket.README.debian ]
335 then
336             cp $paket.README.debian README.debian
337 fi
338
339 mkdir -p tmp/DEBIAN
340
341 # add export to setenv.sh
342 sed -i "s/^/export /" setenv.sh
343
344 popd >/dev/null
345 # ./. ---------------------------------------------
346
347 # build prepare
348 if [ -x debian/$paket.prepare ]
349 then
350         . debian/setenv.sh
351         debian/$paket.prepare
352 fi
353
354 if [ "$compile_type" == "NONE" ]
355 then
356         echo "BUILD=nobuild" >> debian/rules.pre
357 else
358         echo "BUILD=build" >> debian/rules.pre
359
360         if [ $extern_build -eq 1 ]
361         then
362                 BUILD_DIR=~/build
363                 echo "BUILD_DIR=~/build" >> debian/rules.pre
364                 debian/rules sync
365         fi
366
367         # .build
368         if [ "$compile_type" != "CMAKE" -a "$compile_type" != "ANY" ]
369         then
370                 cp debian/rules.pre $BUILD_DIR/$compile_dir/make.pre
371                 pushd $BUILD_DIR/$compile_dir >/dev/null
372                         ln -sf /usr/share/mbuild/makefile .
373                         if [ -n "$sources_suffix" ]
374                         then
375                                 echo "SOURCES = \\" >> make.pre
376                                 for suf in $sources_suffix
377                                 do
378                                         find . -name "*.$suf" -exec echo "  "{}" \\" >> make.pre \;
379                                 done
380                                 echo >> make.pre
381                         fi
382                 popd >/dev/null
383         fi
384
385         if [ "$compile_type" == "PDF" ]
386         then
387                 pushd $BUILD_DIR/$compile_dir >/dev/null
388                         echo "SOURCES = \\" >> make.pre
389                         add_sources tex xml
390                         echo >> make.pre
391                 popd >/dev/null
392         fi
393         if [ -x debian/$paket.prebuild ]
394         then
395                 pushd $BUILD_DIR >/dev/null
396                         . debian/setenv.sh
397                         debian/$paket.prebuild
398                 popd >/dev/null
399         fi
400         if ! [ -x debian/$paket.build -o -f debian/$paket.cmake ]
401         then
402                 create_build
403         fi
404
405         # .cmake
406         if [ -e debian/$paket.cmake ]
407         then
408                 cmake_check
409         fi
410 fi
411
412