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