Magellan Linux

Contents of /trunk/mage/usr/lib/mage/smage2.functions.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3081 - (show annotations) (download) (as text)
Tue May 22 14:09:28 2018 UTC (5 years, 11 months ago) by niro
File MIME type: application/x-sh
File size: 29151 byte(s)
-honor newly introduced method of file-5.33 to detect shared libraries as pie executables if the executable bit is set
1 #!/bin/bash
2 # smage2 releated functions
3 # $Id$
4
5 # sources the smage file and uses state from distribution file if exist
6 # may helpful for repository support later on
7 smagesource()
8 {
9 local file="$1"
10 local localstate
11 local distfilestate
12 local mytag
13
14 source ${file}
15
16 # if PCAT was not set and PCATEGORIE was found
17 # inform the user and use PCATEGORIE as PCAT
18 if [[ -z ${PCAT} ]]
19 then
20 if [[ -n ${PCATEGORIE} ]]
21 then
22 PCAT="${PCATEGORIE}"
23 unset PCATEGORIE
24 # print a warning
25 echo -e "${COLYELLOW}Warning: 'PCATEGORIE' is deprecated and gets removed in the future.${COLDEFAULT}"
26 echo -e "${COLYELLOW} Please modify this smage2 script to use the 'PCAT' variable.${COLDEFAULT}"
27 echo
28 else
29 die "Neither PCAT nor PCATEGORIE are defined!"
30 fi
31 fi
32
33 [[ -n ${STATE} ]] && localstate="${STATE}"
34 [[ -n ${DISTROTAG} ]] && mytag="${DISTROTAG}"
35
36 if [ -f ${SMAGESCRIPTSDIR}/distribution ]
37 then
38 source ${SMAGESCRIPTSDIR}/distribution
39 [[ -n ${STATE} ]] && distfilestate="${STATE}"
40 [[ -n ${DISTROTAG} ]] && mytag="${DISTROTAG}"
41 fi
42 # now switch state and export it but do not overide any local states
43 if [[ ! -z ${localstate} ]]
44 then
45 STATE="${localstate}"
46 else
47 STATE="${distfilestate}"
48 fi
49
50 if mqueryfeature "pkgdistrotag"
51 then
52 # if DISTROTAG was not defined globally
53 # or in distribution file then deactivate this feature!
54 # at this point $mytag must have the distrotag
55 if [[ -z ${mytag} ]]
56 then
57 FVERBOSE=off msetfeature "!pkgdistrotag"
58 unset DISTROTAG
59 echo -e "${COLRED}Requested 'pkgdistrotag' but no \$DISTROTAG found!${COLDEFAULT}"
60 echo -e "${COLRED}Disabled the feature for pkgbuild sanity!${COLDEFAULT}"
61 else
62 # now switch state and export it but do not overide any local states
63 export DISTROTAG="${mytag}"
64 fi
65 else
66 unset DISTROTAG
67 fi
68 }
69
70 print_distrotag()
71 {
72 if FVERBOSE=off mqueryfeature "pkgdistrotag"
73 then
74 if [[ ! -z ${DISTROTAG} ]]
75 then
76 # add a point as prefix
77 echo ".${DISTROTAG}"
78 fi
79 fi
80 }
81
82 showversion()
83 {
84 echo -en "Magellan Source Install v${SMAGEVERSION} "
85 echo -e "-- Niels Rogalla (niro@magellan-linux.de)"
86 }
87
88 die()
89 {
90 xtitleclean
91 echo -e ${COLRED}"Exited ${BASH_SOURCE} at line no ${BASH_LINENO}."${COLDEFAULT}
92 echo "SMAGE failed: $@"
93 exit 1
94 }
95
96 die_pipestatus()
97 {
98 # the status change if we do any parameter declarations!!
99 # dont do this anymore, keep this in mind!
100 #
101 # local pos="$1"
102 # local comment="$2"
103 #
104 # [ ${PIPESTATUS[${pos}]} -ne 0 ] && die "${comment}"
105 #
106 [ ${PIPESTATUS[$1]} -ne 0 ] && die "$2"
107 }
108
109 xtitle()
110 {
111 if [[ ${TERM} = xterm ]]
112 then
113 echo -ne "\033]0;[sMage: $@]\007"
114 fi
115 return 0
116 }
117
118 xtitleclean()
119 {
120 if [[ ${TERM} = xterm ]]
121 then
122 echo -ne "\033]0;\007"
123 fi
124 return 0
125 }
126
127 syncsmage2()
128 {
129 xtitle "Updating smage2-script tree ..."
130 local i
131 for i in ${SMAGE2RSYNC}
132 do
133 rsync ${RSYNC_FETCH_OPTIONS} ${i} ${SMAGESCRIPTSDIR}
134 if [[ $? = 0 ]]
135 then
136 break
137 else
138 continue
139 fi
140 done
141
142 # clean up backup files (foo~)
143 find ${SMAGESCRIPTSDIR} -name *~ -exec rm '{}' ';'
144
145 xtitleclean
146 }
147
148 # $1 filename
149 get_db_md5_sum()
150 {
151 local DB_FILE
152 local MD5_FILE
153 local i
154
155 DB_ENTRY="$(basename $1)"
156 MD5_FILE="${MD5DIR}/$(basename ${SMAGENAME} ${SMAGESUFFIX})"
157
158 i="$(cat ${MD5_FILE}| grep ${DB_ENTRY} | cut -d' ' -f1)"
159
160 echo "${i}"
161 }
162
163 download_sources()
164 {
165 [ -z "${SRC_URI}" ] && echo -e "\nNothing declared to download.\n" && return 0
166
167 local count=${#SRC_URI[*]}
168 local uri
169 local subdir
170 local outputdir
171 local db_md5_file="${MD5DIR}/$(basename ${SMAGENAME} .${SMAGESUFFIX}).md5"
172 local fetching
173 local i
174
175 # check if FETCHING is needed
176 if mchecksum --rundir "${SOURCEDIR}/${PNAME}" --file "${db_md5_file}" --method md5
177 then
178 # md5's ok, no fetching needed
179 fetching=false
180 else
181 fetching=true
182 fi
183
184 if [[ ${fetching} = true ]]
185 then
186 for ((i=0; i < count; i++))
187 do
188 # url to file
189 uri="${SRC_URI[${i}]%%' '*}"
190
191 # subdir in sources dir; the my_SRCI_URI file goes to there
192 subdir="${SRC_URI[${i}]##*' '}"
193
194 # if $subdir is not equal with $uri then an other dir is used
195 if [[ ${uri} != ${subdir} ]]
196 then
197 outputdir="${SOURCEDIR}/${PNAME}/${subdir}"
198 else
199 outputdir="${SOURCEDIR}/${PNAME}"
200 fi
201
202 echo -e "${COLBLUE}==>${COLGREEN} fetching ${uri}${COLDEFAULT}"
203 # always use verbose mode for source downloads
204 FVERBOSE=off msetfeature "verbose"
205 # do not die here, mchecksum catches download errors
206 mdownload --uri "${uri}" --dir "${outputdir}"
207
208 # unset them to be sure
209 unset uri
210 unset subdir
211 unset outputdir
212 done
213
214 # recheck md5 sums after download
215 echo
216 echo -e "${COLBLUE}===${COLGREEN} Checking MD5 sums:${COLDEFAULT}"
217 mchecksum --rundir "${SOURCEDIR}/${PNAME}" --file "${db_md5_file}" --method md5 || die "md5 failed"
218 echo
219 else
220 echo -e "${COLBLUE}===${COLGREEN} All sources already fetched, nothing to do${COLDEFAULT}"
221 fi
222
223 # not needed anymore
224 unset SRC_URI
225 }
226
227 # dummy function, used if that does not exist in smage file
228 pkg_setup()
229 {
230 echo "no pkg_setup defined; doing nothing ..."
231 return 0
232 }
233
234 # dummy function, used if that does not exist in smage file
235 src_prepare()
236 {
237 echo "no src_prepare defined; doing nothing ..."
238 return 0
239 }
240
241 # dummy function, used if that does not exist in smage file
242 src_compile()
243 {
244 echo "no src_compile defined; doing nothing ..."
245 return 0
246 }
247
248 # dummy function, used if that does not exist in smage file
249 src_check()
250 {
251 echo "no src_check defined; doing nothing ..."
252 return 0
253 }
254
255 # dummy function, used if that does not exist in smage file
256 src_install()
257 {
258 echo "no src_install defined; doing nothing ..."
259 return 0
260 }
261
262 mlibdir()
263 {
264 local libdir=lib
265 [[ ${ARCH} = x86_64 ]] && libdir=lib64
266
267 echo "${libdir}"
268 }
269
270 mconfigure()
271 {
272 local myopts
273 if [[ ! -z ${CTARGET} ]]
274 then
275 myopts+=" --target=${CTARGET}"
276 fi
277
278 if [ -x ./configure ]
279 then
280 # if requested disable-static
281 if [[ ! -z $(./configure --help | grep -- '--.*able-static') ]]
282 then
283 if mqueryfeature '!static'
284 then
285 myopts+=" --disable-static"
286 else
287 myopts+=" --enable-static"
288 fi
289 fi
290
291 # always enable shared by default but not for waf configure
292 if [[ ! -z $(./configure --help | grep -- '--.*able-shared') ]] &&
293 [[ -z $(./configure --version | grep waf) ]]
294 then
295 myopts+=" --enable-shared"
296 fi
297
298 ./configure \
299 --prefix=/usr \
300 --host=${CHOST} \
301 --build=${CHOST} \
302 --mandir=/usr/share/man \
303 --infodir=/usr/share/info \
304 --datadir=/usr/share \
305 --sysconfdir=/etc \
306 --localstatedir=/var/lib \
307 --libdir=/usr/$(mlibdir) \
308 ${myopts} \
309 "$@" || die "mconfigure failed"
310 else
311 echo "configure is not an executable ..."
312 exit 1
313 fi
314 }
315
316 minstall()
317 {
318 if [ -f ./[mM]akefile -o -f ./GNUmakefile ]
319 then
320 make prefix=${BINDIR}/usr \
321 datadir=${BINDIR}/usr/share \
322 infodir=${BINDIR}/usr/share/info \
323 localstatedir=${BINDIR}/var/lib \
324 mandir=${BINDIR}/usr/share/man \
325 sysconfdir=${BINDIR}/etc \
326 libdir=${BINDIR}/usr/$(mlibdir) \
327 "$@" install || die "minstall failed"
328 else
329 die "no Makefile found"
330 fi
331 }
332
333 mmake()
334 {
335 make ${MAKEOPTS} ${EXTRA_EMAKE} "$@"
336 }
337
338 munpack()
339 {
340 local SRCFILE
341 local IFTAR
342 local DEST
343
344 SRCFILE=$1
345
346 if [[ -z $2 ]]
347 then
348 DEST=${BUILDDIR}
349 else
350 DEST=$2
351 fi
352
353 echo -e "${COLBLUE}>>>${COLGREEN} Unpacking ${SOURCEDIR}/${PNAME}/${SRCFILE}${COLDEFAULT}"
354
355 [[ ! -d ${DEST} ]] && install -d ${DEST}
356
357 case "${SRCFILE##*.}" in
358 bz2)
359 IFTAR="$(basename $SRCFILE .bz2)"
360 IFTAR="${IFTAR##*.}"
361 if [[ ${IFTAR} = tar ]]
362 then
363 tar --no-same-owner -xvjf ${SOURCEDIR}/${PNAME}/${SRCFILE} -C ${DEST} || die ".tar.bz2 unpack failed."
364 else
365 pushd ${DEST} > /dev/null
366 bzcat ${SOURCEDIR}/${PNAME}/${SRCFILE} > ${DEST}/$(basename ${SRCFILE} .bz2) || die ".bz2 unpack failed."
367 popd > /dev/null
368 fi
369 ;;
370 gz)
371 IFTAR="$(basename $SRCFILE .gz)"
372 IFTAR="${IFTAR##*.}"
373 if [[ ${IFTAR} = tar ]]
374 then
375 tar --no-same-owner -xvzf ${SOURCEDIR}/${PNAME}/${SRCFILE} -C ${DEST} || die ".tar.gz unpack failed."
376 else
377 pushd ${DEST} > /dev/null
378 zcat ${SOURCEDIR}/${PNAME}/${SRCFILE} > ${DEST}/$(basename ${SRCFILE} .gz) || die ".gz unpack failed."
379 popd > /dev/null
380 fi
381 ;;
382 xz)
383 IFTAR="$(basename $SRCFILE .xz)"
384 IFTAR="${IFTAR##*.}"
385 if [[ ${IFTAR} = tar ]]
386 then
387 tar --no-same-owner -xvJf ${SOURCEDIR}/${PNAME}/${SRCFILE} -C ${DEST} || die ".tar.xz unpack failed."
388 else
389 pushd ${DEST} > /dev/null
390 xzcat ${SOURCEDIR}/${PNAME}/${SRCFILE} > ${DEST}/$(basename ${SRCFILE} .xz) || die ".xz unpack failed."
391 popd > /dev/null
392 fi
393 ;;
394 tbz2|mpks|mpk)
395 tar --no-same-owner -xvjf ${SOURCEDIR}/${PNAME}/${SRCFILE} -C ${DEST} || die ".tbz2 unpack failed."
396 ;;
397 tgz)
398 tar --no-same-owner -xvzf ${SOURCEDIR}/${PNAME}/${SRCFILE} -C ${DEST} || die ".tgz unpack failed."
399 ;;
400 txz|mpkzs|mpkz)
401 tar --no-same-owner -xvJf ${SOURCEDIR}/${PNAME}/${SRCFILE} -C ${DEST} || die ".txz unpack failed."
402 ;;
403 rar)
404 unrar x ${SOURCEDIR}/${PNAME}/${SRCFILE} ${DEST} || die ".rar unpack failed."
405 ;;
406 zip|xpi|jar)
407 unzip ${SOURCEDIR}/${PNAME}/${SRCFILE} -d ${DEST} || die ".zip unpack failed."
408 ;;
409 rpm)
410 pushd ${DEST} > /dev/null
411 rpm2targz ${SOURCEDIR}/${PNAME}/${SRCFILE} || die "rpm2targz: .rpm unpack failed."
412 tar --no-same-owner -xvzf ${SRCFILE/.rpm/.tar.gz} || die "tar: .rpm unpack failed."
413 if [[ -f ${DEST}/${SRCFILE/.rpm/.tar.gz} ]]
414 then
415 rm ${DEST}/${SRCFILE/.rpm/.tar.gz}
416 fi
417 ;;
418 *)
419 die "munpack failed"
420 ;;
421 esac
422 }
423
424 mpatch()
425 {
426 local PATCHOPTS
427 local PATCHFILE
428 local i
429
430 PATCHOPTS=$1
431 PATCHFILE=$2
432
433 if [[ -z $2 ]]
434 then
435 PATCHFILE=$1
436
437 ## patch level auto-detection, get patch level
438 for ((i=0; i < 10; i++))
439 do
440 patch --dry-run -Np${i} -i ${SOURCEDIR}/${PNAME}/${PATCHFILE} > /dev/null
441 if [[ $? = 0 ]]
442 then
443 PATCHOPTS="-Np${i}"
444 break
445 fi
446 done
447 fi
448
449 echo -e "${COLBLUE}*** ${COLGREEN}Applying patch '${PATCHFILE}'${COLDEFAULT}"
450 patch "${PATCHOPTS}" -i ${SOURCEDIR}/${PNAME}/${PATCHFILE}
451 }
452
453 mlibtoolize()
454 {
455 local opts="$@"
456 [[ -z ${opts} ]] && opts="--verbose --install --force"
457
458 libtoolize ${opts} || die "running: mlibtoolize ${opts}"
459 }
460
461 mautoreconf()
462 {
463 local opts="$@"
464 [[ -z ${opts} ]] && opts="--verbose --install --force"
465
466 autoreconf ${opts} || die "running: mautoreconf ${opts}"
467 }
468
469 minstalldocs()
470 {
471 local docfiles
472 local doc
473 docfiles="$@"
474
475 if [ ! -d ${BINDIR}/usr/share/doc/${PNAME}-${PVER} ]
476 then
477 install -d ${BINDIR}/usr/share/doc/${PNAME}-${PVER} || die "creating doc dirs."
478 fi
479
480 for doc in ${docfiles}
481 do
482 if [ -f ${doc} ]
483 then
484 if mqueryfeature "compressdoc"
485 then
486 cat ${doc} | gzip -9c > ${BINDIR}/usr/share/doc/${PNAME}-${PVER}/$(basename ${doc}).gz || die "gzipping +installing ${doc}."
487 chmod 0644 ${BINDIR}/usr/share/doc/${PNAME}-${PVER}/$(basename ${doc}).gz || die "fixing permissions of ${doc}."
488 else
489 install -m 0644 ${doc} ${BINDIR}/usr/share/doc/${PNAME}-${PVER} || die "installing ${doc}."
490 fi
491 fi
492 done
493 }
494
495 mstriplibs()
496 {
497 local stripdir="$@"
498
499 [[ -z ${stripdir} ]] && stripdir="${BINDIR}"
500 [[ -z ${STRIP_DYN_LIB} ]] && STRIP_DYN_LIB="--strip-debug"
501 find ${stripdir} ! -type d | xargs --no-run-if-empty file | grep "[shared object|pie executable]" | grep ELF | cut -f 1 -d : | xargs --no-run-if-empty strip ${STRIP_DYN_LIB} 2> /dev/null
502 }
503
504 mstripbins()
505 {
506 local stripdir="$@"
507
508 [[ -z ${stripdir} ]] && stripdir="${BINDIR}"
509 [[ -z ${STRIP_DYN_BIN} ]] && STRIP_DYN_BIN="--strip-debug"
510 find ${stripdir} ! -type d | xargs --no-run-if-empty file | grep -v "pie executable" | grep "executable" | grep ELF | cut -f 1 -d : | xargs --no-run-if-empty strip ${STRIP_DYN_BIN} 2> /dev/null
511 }
512
513 mstripstatic()
514 {
515 local stripdir="$@"
516
517 [[ -z ${stripdir} ]] && stripdir="${BINDIR}"
518 [[ -z ${STRIP_STATIC_LIB} ]] && STRIP_STATIC_LIB="--strip-debug"
519 find ${stripdir} ! -type d | xargs --no-run-if-empty file | grep "ar archive" | cut -f 1 -d : | xargs --no-run-if-empty strip ${STRIP_STATIC_LIB} 2> /dev/null
520 }
521
522 mstriplibtoolarchive()
523 {
524 local stripdir="$@"
525
526 [[ -z ${stripdir} ]] && stripdir="${BINDIR}"
527 find ${stripdir} ! -type d -name \*.la | xargs --no-run-if-empty file | grep "libtool library" | cut -f 1 -d : | xargs --no-run-if-empty rm -f -- 2> /dev/null
528 }
529
530 mpurgetargets()
531 {
532 local stripdir="$@"
533 local target
534
535 [[ -z ${stripdir} ]] && stripdir=${BINDIR}
536 # nothing to do in this case
537 [[ -z ${PURGE_TARGETS[*]} ]] && return
538
539 for target in ${PURGE_TARGETS[*]}
540 do
541 # check if target is a regex pattern without any slashes
542 if [[ ${target} = ${target//\/} ]]
543 then
544 find ${BINDIR} -type f -name "${target}" | xargs --no-run-if-empty rm -f -- 2> /dev/null
545 else
546 rm -f -- ${target} 2> /dev/null
547 fi
548 done
549 }
550
551 mcompressdocs()
552 {
553 local bindir="$@"
554
555 if [ -d ${bindir}/usr/share/man ]
556 then
557 echo -e "${COLBLUE}===${COLGREEN} compressing man-pages ...${COLDEFAULT}"
558 ${MLIBDIR}/compressdoc -g -9 ${bindir}/usr/share/man
559 fi
560
561 if [ -d ${bindir}/usr/share/info ]
562 then
563 echo -e "${COLBLUE}===${COLGREEN} compressing info-pages ...${COLDEFAULT}"
564 ${MLIBDIR}/compressdoc -g -9 ${bindir}/usr/share/info
565 fi
566 }
567
568 sminclude()
569 {
570 local i
571
572 if [[ -n "$@" ]]
573 then
574 for i in $@
575 do
576 [[ ${SILENT} = 1 ]] || echo "--- Including ${SMAGESCRIPTSDIR}/include/${i}.sminc"
577 source ${SMAGESCRIPTSDIR}/include/${i}.sminc
578 done
579 [[ ${SILENT} = 1 ]] || echo
580 fi
581 }
582
583 march()
584 {
585 local retval
586
587 if [[ ! -z ${ARCH} ]]
588 then
589 echo "${ARCH}"
590 retval=0
591 else
592 retval=1
593 fi
594
595 return "${retval}"
596 }
597
598 marchvariable()
599 {
600 local retval
601 local var="$1"
602
603 [[ -n ${var} ]] || die "marchvariable(): missing variable"
604
605 if [[ ! -z $(eval echo \$${var}_${ARCH/i*86/x86}) ]]
606 then
607 eval echo \$${var}_${ARCH/i*86/x86} | tr ';' '\n'
608 retval=0
609 else
610 retval=1
611 fi
612
613 return "${retval}"
614 }
615
616 marchsrcfile()
617 {
618 local retval
619 local var="$1"
620
621 [[ -z ${var} ]] && var="SRCFILE"
622
623 marchvariable "${var}" || die
624 }
625
626 marchsrcdir()
627 {
628 local retval
629 local var="$1"
630
631 [[ -z ${var} ]] && var="SRCDIR"
632
633 marchvariable "${var}" || die
634 }
635
636 marchdepend()
637 {
638 local retval
639 local var="$1"
640
641 [[ -z ${var} ]] && var="DEPEND"
642
643 if [[ ! -z $(eval echo \$${var}_${ARCH/i*86/x86}) ]]
644 then
645 # evaluate with escape strings for the magefile if called by smage
646 # but do not escape if the smagefile was called by depwalker directly
647 if [[ ${SMAGE_DEPEND} = 1 ]]
648 then
649 eval echo \$${var}_${ARCH/i*86/x86} | tr ';' '\n'
650 else
651 echo "\$(eval echo \\\$${var}_\${ARCH/i*86/x86} | tr ';' '\n')"
652 fi
653 retval=0
654 else
655 retval=1
656 fi
657
658 return "${retval}"
659 }
660
661 marchsdepend()
662 {
663 local retval
664 local var="$1"
665
666 [[ -z ${var} ]] && var="SDEPEND"
667
668 if [[ ! -z $(eval echo \$${var}_${ARCH/i*86/x86}) ]]
669 then
670 # evaluate with escape strings for the magefile if called by smage
671 # but do not escape if the smagefile was called by depwalker directly
672 if [[ ${SMAGE_DEPEND} = 1 ]]
673 then
674 eval echo \$${var}_${ARCH/i*86/x86} | tr ';' '\n'
675 else
676 echo "\$(eval echo \\\$${var}_\${ARCH/i*86/x86} | tr ';' '\n')"
677 fi
678 retval=0
679 else
680 retval=1
681 fi
682
683 return "${retval}"
684 }
685
686 setup_distcc_environment()
687 {
688 if [ -x /usr/bin/distcc ]
689 then
690 echo -e "${COLBLUE}---${COLGREEN} Using DistCC for compilation ...${COLDEFAULT}"
691 export PATH=/usr/$(mlibdir)/distcc/bin:${PATH} || die "distcc: could not export new $PATH"
692
693 export DISTCC_DIR="${DISTCC_DIR}" || die "distcc_dir export failed"
694
695 # creating distcc tempdir
696 install -o distcc -g daemon -d ${DISTCC_DIR}
697 chmod 1777 ${DISTCC_DIR}
698 fi
699 }
700
701 setup_icecc_environment()
702 {
703 if [ -x /usr/bin/icecc ]
704 then
705 echo -e "${COLBLUE}---${COLGREEN} Using IceCC for compilation ...${COLDEFAULT}"
706 export PATH=/usr/$(mlibdir)/icecc/bin:${PATH} || die "icecc: could not export new $PATH"
707 fi
708 }
709
710 setup_ccache_environment()
711 {
712 if [ -x /usr/bin/ccache ]
713 then
714 mqueryfeature "icecc" && export CCACHE_PREFIX=icecc
715
716 echo -e "${COLBLUE}---${COLGREEN} Using CCache for compilation ...${COLDEFAULT}"
717 export PATH=/usr/$(mlibdir)/ccache/bin:${PATH} || die "ccache: could not export new $PATH"
718 fi
719 }
720
721 # build_mage_script(): helper functions for regen_mage_tree()
722 # generates an mage file with given information in smage file
723 # needs at least:
724 # PNAME name of pkg
725 # PVER version
726 # PBUILD revision
727 # PCAT category of the pkg
728 # PCATEGORIE category of the pkg (deprecated, use PCAT!)
729 # STATE state of pkg stable|unstable|old
730 # DESCRIPTION a short description (opt)
731 # HOMEPAGE homepage (opt)
732 # LICENSE license information of the pkg (opt)
733 # DEPEND runtime dependencies (opt)
734 # SDEPEND adds needed deps to build the pkg (opt)
735 # PROVIDE provides a virtual (opt)
736 #
737 # special tags:
738 # PKGTYPE type of pkg
739 # INHERITS which functions get included
740 # SPECIAL_FUNCTIONS special functions which should also be added
741 # warning: they get killed before the build starts !
742 # SPLIT_PACKAGES names of all subpackages which are splitted from parent
743 # SPLIT_PACKAGE_BASE base package name for splitpackages
744 # (only in the resulting magefile}
745 #
746 # MAGE_TREE_DEST target destination of the generated tree
747 # REGEN_MAGE_TREE set to 'true' to enable this
748 #
749 # gets called with build_mage_script target
750 build_mage_script()
751 {
752 local magefile
753 local dest
754 local split_pkg_base
755 local sym
756 local depname
757
758 # if MAGE_TREE_DEST not set use BUILDDIR
759 : ${MAGE_TREE_DEST=${BUILDDIR}/mage-tree}
760
761 # mark package as splitpackage
762 [[ $1 = --split-pkg-base ]] && shift && split_pkg_base="$1"
763
764 # name of magefile
765 magefile="${PNAME}-${PVER}-${PBUILD}.mage"
766
767 # destination to magefile
768 dest="${MAGE_TREE_DEST}/${PCAT}/${PNAME}/${magefile}"
769
770 # show what we are doing
771 echo -e "${COLBLUE}===${COLGREEN} generating mage file:${COLDEFAULT}"
772 echo "${dest}"
773
774 install -d "$(dirname ${dest})"
775 # now build the mage file
776 > ${dest}
777
778 # pgkname and state
779 echo "PKGNAME=\"${PNAME}-${PVER}-\${ARCH}$(print_distrotag)-${PBUILD}\"" >> ${dest}
780 echo "STATE=\"${STATE}\"" >> ${dest}
781
782 # description and homepage
783 echo "DESCRIPTION=\"${DESCRIPTION}\"" >> ${dest}
784 echo "HOMEPAGE=\"${HOMEPAGE}\"" >> ${dest}
785
786 # license information
787 echo "LICENSE=\"${LICENSE}\"" >> ${dest}
788
789 # special tags and vars
790 echo "PKGTYPE=\"${PKGTYPE}\"" >> ${dest}
791
792 # split package base
793 echo "SPLIT_PACKAGE_BASE=\"${split_pkg_base}\"" >> ${dest}
794
795 # add special vars
796 if [ -n "${SPECIAL_VARS}" ]
797 then
798 local i
799 for i in ${SPECIAL_VARS}
800 do
801 # being tricky here :)
802 echo "${i}=\"$(eval echo \$${i})\"" >> ${dest}
803 done
804 fi
805
806 # add at least all includes
807 if [ -n "${INHERITS}" ]
808 then
809 echo -n "minclude" >> ${dest}
810 local i
811 for i in ${INHERITS}
812 do
813 echo -n " ${i}" >> ${dest}
814 done
815 # a CRLF is needed here!
816 echo >> ${dest}
817 fi
818
819 # deps and provides
820 echo "DEPEND=\"${DEPEND}\"" >> ${dest}
821 echo "SDEPEND=\"${SDEPEND}\"" >> ${dest}
822 echo "PROVIDE=\"${PROVIDE}\"" >> ${dest}
823
824 # add special functions
825 if [ -n "${SPECIAL_FUNCTIONS}" ]
826 then
827 local i
828 for i in ${SPECIAL_FUNCTIONS}
829 do
830 # add to mage (quotes needed !)
831 typeset -f "${i}" >> ${dest}
832 done
833 fi
834
835 # pre|post-install|removes
836 typeset -f preinstall >> ${dest}
837 typeset -f postinstall >> ${dest}
838 typeset -f preremove >> ${dest}
839 typeset -f postremove >> ${dest}
840 }
841
842 regen_mage_tree()
843 {
844 local subpackage
845
846 # build them only if requested
847 if mqueryfeature regentree
848 then
849 # run it for splitpackages
850 if [[ -n ${SPLIT_PACKAGES} ]]
851 then
852 local split_pkg_base="${PNAME}"
853 # save smage environment
854 split_save_variables
855 # build for each subpackage a mage file
856 # run it with several targets
857 echo
858 for subpackage in ${SPLIT_PACKAGES}
859 do
860 # get the right variables for the split
861 export PNAME="${subpackage}"
862 split_info_${subpackage}
863 # fix PCATEGORIE -> PCAT
864 if [[ ! -z ${PCATEGORIE} ]]
865 then
866 PCAT="${PCATEGORIE}"
867 unset PCATEGORIE
868 fi
869 # get the preinstall etc
870 split_export_inherits ${subpackage}
871 build_mage_script --split-pkg-base "${split_pkg_base}"
872 # delete split preinstall etc
873 split_delete_inherits ${subpackage}
874 # restore smage environment
875 split_restore_variables
876 done
877 echo
878 # unset all saved smage variables
879 split_unset_variables
880
881 else
882 echo
883 build_mage_script
884 echo
885 fi
886 fi
887
888 # now unset all uneeded vars to be safe
889 # unset PKGNAME <-- don't do that; smage needs this var
890 unset SPECIAL_FUNCTIONS
891 unset SPECIAL_VARS
892 unset STATE
893 unset DESCRIPTION
894 unset HOMEPAGE
895 # unset PKGTYPE <-- don't do that either; smage needs this var
896 unset INHERITS
897 unset DEPEND
898 unset SDEPEND
899 unset PROVIDE
900 unset preinstall
901 unset postinstall
902 unset preremove
903 unset postremove
904 }
905
906 split_save_variables()
907 {
908 export SAVED_PNAME="${PNAME}"
909 export SAVED_PVER="${PVER}"
910 export SAVED_PBUILD="${PBUILD}"
911 export SAVED_PCAT="${PCAT}"
912 export SAVED_DESCRIPTION="${DESCRIPTION}"
913 export SAVED_HOMEPAGE="${HOMEPAGE}"
914 export SAVED_SPECIAL_VARS="${SPECIAL_VARS}"
915 export SAVED_SPECIAL_FUNCTIONS="${SPECIAL_FUNCTIONS}"
916 export SAVED_STATE="${STATE}"
917 export SAVED_INHERITS="${INHERITS}"
918 export SAVED_DEPEND="${DEPEND}"
919 export SAVED_SDEPEND="${SDEPEND}"
920 export SAVED_PROVIDE="${PROVIDE}"
921 export SAVED_PKGTYPE="${PKGTYPE}"
922
923 # special handling needed for mage features
924 # pkgbuild
925 mqueryfeature "pkgbuild" && export SAVED_FEATURE_PKGBUILD="pkgbuild"
926 mqueryfeature "!pkgbuild" && export SAVED_FEATURE_PKGBUILD="!pkgbuild"
927 # strip
928 mqueryfeature "strip" && export SAVED_FEATURE_STRIP="strip"
929 mqueryfeature "!strip" && export SAVED_FEATURE_STRIP="!strip"
930 # libtool
931 mqueryfeature "libtool" && export SAVED_FEATURE_LIBTOOL="libtool"
932 mqueryfeature "!libtool" && export SAVED_FEATURE_LIBTOOL="!libtool"
933 # compressdoc
934 mqueryfeature "compressdoc" && export SAVED_FEATURE_COMPRESSDOC="compressdoc"
935 mqueryfeature "!compressdoc" && export SAVED_FEATURE_COMPRESSDOC="!compressdoc"
936
937 # bindir too
938 export SAVED_BINDIR="${BINDIR}"
939
940 # export the SPLIT_PACKAGE_BASE
941 export SPLIT_PACKAGE_BASE="${SAVED_PNAME}"
942
943 # functions
944 if [[ ! -z $(typeset -f preinstall) ]]
945 then
946 # rename the old one
947 local saved_preinstall
948 saved_preinstall=SAVED_$(typeset -f preinstall)
949 eval "${saved_preinstall}"
950 export -f SAVED_preinstall
951 fi
952
953 if [[ ! -z $(typeset -f postinstall) ]]
954 then
955 # rename the old one
956 local saved_postinstall
957 saved_postinstall=SAVED_$(typeset -f postinstall)
958 eval "${saved_postinstall}"
959 export -f SAVED_postinstall
960 fi
961
962 if [[ ! -z $(typeset -f preremove) ]]
963 then
964 # rename the old one
965 local saved_preremove
966 saved_preremove=SAVED_$(typeset -f preremove)
967 eval "${saved_preremove}"
968 export -f SAVED_preremove
969 fi
970
971 if [[ ! -z $(typeset -f postremove) ]]
972 then
973 # rename the old one
974 local saved_postremove
975 saved_postremove=SAVED_$(typeset -f postremove)
976 eval "${saved_postremove}"
977 export -f SAVED_postremove
978 fi
979 }
980
981 split_restore_variables()
982 {
983 export PNAME="${SAVED_PNAME}"
984 export PVER="${SAVED_PVER}"
985 export PBUILD="${SAVED_PBUILD}"
986 export PCAT="${SAVED_PCAT}"
987 export DESCRIPTION="${SAVED_DESCRIPTION}"
988 export HOMEPAGE="${SAVED_HOMEPAGE}"
989 export SPECIAL_VARS="${SAVED_SPECIAL_VARS}"
990 export SPECIAL_FUNCTIONS="${SAVED_SPECIAL_FUNCTIONS}"
991 export STATE="${SAVED_STATE}"
992 export INHERITS="${SAVED_INHERITS}"
993 export DEPEND="${SAVED_DEPEND}"
994 export SDEPEND="${SAVED_SDEPEND}"
995 export PROVIDE="${SAVED_PROVIDE}"
996 export PKGTYPE="${SAVED_PKGTYPE}"
997
998 # special handling needed for mage features
999 # pkgbuild
1000 FVERBOSE=off msetfeature "${SAVED_FEATURE_PKGBUILD}"
1001 # strip
1002 FVERBOSE=off msetfeature "${SAVED_FEATURE_STRIP}"
1003 # libtool
1004 FVERBOSE=off msetfeature "${SAVED_FEATURE_LIBTOOL}"
1005 # compressdoc
1006 FVERBOSE=off msetfeature "${SAVED_FEATURE_COMPRESSDOC}"
1007
1008 # bindir too
1009 export BINDIR="${SAVED_BINDIR}"
1010
1011 # functions
1012 if [[ ! -z $(typeset -f SAVED_preinstall) ]]
1013 then
1014 # rename the old one
1015 local saved_preinstall
1016 saved_preinstall=$(typeset -f SAVED_preinstall)
1017 eval "${saved_preinstall/SAVED_/}"
1018 export -f preinstall
1019 fi
1020
1021 if [[ ! -z $(typeset -f SAVED_postinstall) ]]
1022 then
1023 # rename the old one
1024 local saved_postinstall
1025 saved_postinstall=$(typeset -f SAVED_postinstall)
1026 eval "${saved_postinstall/SAVED_/}"
1027 export -f postinstall
1028 fi
1029
1030 if [[ ! -z $(typeset -f SAVED_preremove) ]]
1031 then
1032 # rename the old one
1033 local saved_preremove
1034 saved_preremove=$(typeset -f SAVED_preremove)
1035 eval "${saved_preremove/SAVED_/}"
1036 export -f preremove
1037 fi
1038
1039 if [[ ! -z $(typeset -f SAVED_postremove) ]]
1040 then
1041 # rename the old one
1042 local saved_postremove
1043 saved_postremove=$(typeset -f SAVED_postremove)
1044 eval "${saved_postremove/SAVED_/}"
1045 export -f postremove
1046 fi
1047 }
1048
1049 split_unset_variables()
1050 {
1051 # unset saved vars; not needed anymore
1052 unset SAVED_PNAME
1053 unset SAVED_PVER
1054 unset SAVED_PBUILD
1055 unset SAVED_PCAT
1056 unset SAVED_DESCRIPTION
1057 unset SAVED_HOMEPAGE
1058 unset SAVED_SPECIAL_VARS
1059 unset SAVED_SPECIAL_FUNCTIONS
1060 unset SAVED_STATE
1061 unset SAVED_PKGTYPE
1062 unset SAVED_INHERITS
1063 unset SAVED_DEPEND
1064 unset SAVED_SDEPEND
1065 unset SAVED_PROVIDE
1066 unset SAVED_BINDIR
1067 unset SAVED_PKGTYPE
1068 unset SAVED_FEATURE_PKGBUILD
1069 unset SAVED_FEATURE_STRIP
1070 unset SAVED_FEATURE_LIBTOOL
1071 unset SAVED_FEATURE_COMPRESSDOC
1072 unset SPLIT_PACKAGE_BASE
1073 unset -f SAVED_preinstall
1074 unset -f SAVED_postinstall
1075 unset -f SAVED_preremove
1076 unset -f SAVED_postremove
1077 }
1078
1079 split_export_inherits()
1080 {
1081 local subpackage="$1"
1082 local func
1083 local newfunc
1084
1085 for func in preinstall postinstall preremove postremove
1086 do
1087 if [[ ! -z $(typeset -f ${func}_${subpackage}) ]]
1088 then
1089 newfunc=$(typeset -f ${func}_${subpackage})
1090 newfunc="${newfunc/_${subpackage} (/ (}"
1091 eval "${newfunc}"
1092 fi
1093 done
1094 }
1095
1096 split_delete_inherits()
1097 {
1098 local subpackage="$1"
1099 local func
1100
1101 for func in preinstall postinstall preremove postremove
1102 do
1103 if [[ ! -z $(typeset -f ${func}_${subpackage}) ]]
1104 then
1105 unset -f ${func}
1106 fi
1107 done
1108 }
1109
1110 export_inherits()
1111 {
1112 local include="$1"
1113 shift
1114
1115 while [ "$1" ]
1116 do
1117 local functions="$1"
1118
1119 # sanity checks
1120 [ -z "${include}" ] && die "export_inherits(): \$include not given."
1121 [ -z "${functions}" ] && die "export_inherits(): \$functions not given."
1122
1123 eval "${functions}() { ${include}_${functions} ; }"
1124
1125 # debug
1126 [[ ${MAGEDEBUG} = on ]] && typeset -f "${functions}"
1127
1128 shift
1129 done
1130 }
1131
1132 generate_package_md5sum()
1133 {
1134 local dest
1135 local pcat
1136 local pname
1137 local pver
1138 local pbuild
1139 local parch
1140 local pkgname
1141
1142 # very basic getops
1143 for i in $*
1144 do
1145 case $1 in
1146 --pcat|-c) shift; pcat="$1" ;;
1147 --pname|-n) shift; pname="$1" ;;
1148 --pver|-v) shift; pver="$1" ;;
1149 --pbuild|-b) shift; pbuild="$1" ;;
1150 --parch|a) shift; parch="$1" ;;
1151 esac
1152 shift
1153 done
1154
1155 # sanity checks; abort if not given
1156 [ -z "${pcat}" ] && die "generate_package_md5sum() \$pcat not given."
1157 [ -z "${pname}" ] && die "generate_package_md5sum() \$pname not given."
1158 [ -z "${pver}" ] && die "generate_package_md5sum() \$pver not given."
1159 [ -z "${pbuild}" ] && die "generate_package_md5sum() \$pbuild not given."
1160 [ -z "${parch}" ] && die "generate_package_md5sum() \$parch not given."
1161
1162 # check needed global vars
1163 [ -z "${PKGDIR}" ] && die "generate_package_md5sum() \$PKGDIR not set."
1164 [ -z "${PKGSUFFIX}" ] && die "generate_package_md5sum() \$PKGSUFFIX not set."
1165
1166 # build pkgname
1167 pkgname="${pname}-${pver}-${parch}$(print_distrotag)-${pbuild}"
1168
1169 # build pkg-md5-sum only if requested
1170 if mqueryfeature regentree
1171 then
1172 echo -ne "${COLBLUE}===${COLGREEN} generating md5's for ${pkgname}.${PKGSUFFIX} ... ${COLDEFAULT}"
1173
1174 # abort if not exist
1175 if [ ! -f ${PKGDIR}/${pkgname}.${PKGSUFFIX} ]
1176 then
1177 echo -e "${COLRED}! exists${COLDEFAULT}"
1178 return 0
1179 fi
1180
1181 # if MAGE_TREE_DEST not set use BUILDDIR
1182 : ${MAGE_TREE_DEST=${BUILDDIR}/mage-tree}
1183
1184 # setup md5 dir
1185 dest="${MAGE_TREE_DEST}/${pcat}/${pname}/md5"
1186 install -d ${dest}
1187
1188 # gen md5sum
1189 ( cd ${PKGDIR}; md5sum "${pkgname}.${PKGSUFFIX}" ) \
1190 > ${dest}/${pkgname}.md5
1191 echo -e "${COLGREEN}done${COLDEFAULT}"
1192 fi
1193 }
1194
1195 source_pkg_build()
1196 {
1197 if [[ ! -d ${SOURCEDIR}/${PNAME} ]]
1198 then
1199 install -d ${SOURCEDIR}/${PNAME}
1200 fi
1201
1202 [ -z "${SRCPKGDIR}" ] && die "\$SRCPKGDIR not found. Please setup your ${MAGERC} correctly."
1203
1204 echo -e "${COLGREEN}Creating source package tarball ... ${COLDEFAULT}"
1205
1206 # include the smage2 file
1207 cp ${SMAGENAME} ${SOURCEDIR}/${PNAME}
1208
1209 ( cd ${SOURCEDIR}; tar cvjf ${BUILDDIR}/${PNAME}-${PVER}-${PBUILD}.tar.bz2 ${PNAME}; )
1210 [[ ! -d ${SRCPKGDIR} ]] && install -d ${SRCPKGDIR}
1211 mv ${BUILDDIR}/${PNAME}-${PVER}-${PBUILD}.tar.bz2 ${SRCPKGDIR}/${PNAME}-${PVER}-${PBUILD}.${SRCPKGSUFFIX}
1212
1213 echo -e "${COLGREEN}Source package ${COLBLUE}${PNAME}-${PVER}-${PBUILD}.${SRCPKGSUFFIX} ${COLGREEN}successfully builded.${COLDEFAULT}\n"
1214 }
1215
1216 step_by_step()
1217 {
1218 if mqueryfeature stepbystep
1219 then
1220 echo -e "${COLRED}Step-by-step enabled! Paused after $1.${COLDEFAULT}"
1221 echo "Press [enter] to continue"
1222 read
1223 fi
1224 }
1225
1226 resume_stamp()
1227 {
1228 local step="$1"
1229 [[ ! -d ${BUILDDIR}/.stamps ]] && install -d ${BUILDDIR}/.stamps
1230 touch ${BUILDDIR}/.stamps/smage-${PKGNAME}-${step}
1231 }
1232
1233 run_resume()
1234 {
1235 local step="$1"
1236
1237 if mqueryfeature "resume" && [[ -f ${BUILDDIR}/.stamps/smage-${PKGNAME}-${step} ]]
1238 then
1239 echo -e "${COLMAGENTA}${step} already processed; doing nothing${COLDEFAULT}"
1240 return 0
1241 else
1242 return 1
1243 fi
1244 }
1245
1246 mqalint()
1247 {
1248 return 0
1249 }