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