Magellan Linux

Annotation of /branches/mage-next/src/smage2.functions.sh.in

Parent Directory Parent Directory | Revision Log Revision Log


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