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