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