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