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