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