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