Magellan Linux

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2362 - (hide annotations) (download) (as text)
Mon Jan 6 12:47:23 2014 UTC (10 years, 10 months ago) by niro
Original Path: trunk/mage/usr/lib/mage/smage2.functions.sh
File MIME type: application/x-sh
File size: 30775 byte(s)
-marchdepend(), marchsdepend(), marchsrcdir(): make the used variable variable
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 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     src_prepare()
229     {
230     echo "no src_prepare defined; doing nothing ..."
231     return 0
232     }
233    
234     # dummy function, used if that does not exist in smage file
235     src_compile()
236     {
237     echo "no src_compile defined; doing nothing ..."
238     return 0
239     }
240    
241     # dummy function, used if that does not exist in smage file
242     src_check()
243     {
244     echo "no src_check defined; doing nothing ..."
245     return 0
246     }
247    
248     # dummy function, used if that does not exist in smage file
249     src_install()
250     {
251     echo "no src_install defined; doing nothing ..."
252     return 0
253     }
254    
255     mlibdir()
256     {
257     local libdir=lib
258     [[ ${ARCH} = x86_64 ]] && libdir=lib64
259    
260     echo "${libdir}"
261     }
262    
263     mconfigure()
264     {
265     local myopts
266     if [[ ! -z ${CTARGET} ]]
267     then
268     myopts+=" --target=${CTARGET}"
269     fi
270    
271     if [ -x ./configure ]
272     then
273     # if requested disable-static
274     if [[ ! -z $(./configure --help | grep -- '--.*able-static') ]]
275     then
276     if mqueryfeature '!static'
277     then
278     myopts+=" --disable-static"
279     else
280     myopts+=" --enable-static"
281     fi
282     fi
283    
284 niro 1680 # always enable shared by default but not for waf configure
285     if [[ ! -z $(./configure --help | grep -- '--.*able-shared') ]] &&
286     [[ -z $(./configure --version | grep waf) ]]
287 niro 1640 then
288     myopts+=" --enable-shared"
289     fi
290    
291     ./configure \
292     --prefix=/usr \
293     --host=${CHOST} \
294     --build=${CHOST} \
295     --mandir=/usr/share/man \
296     --infodir=/usr/share/info \
297     --datadir=/usr/share \
298     --sysconfdir=/etc \
299     --localstatedir=/var/lib \
300     --libdir=/usr/$(mlibdir) \
301     ${myopts} \
302     "$@" || die "mconfigure failed"
303     else
304     echo "configure is not an executable ..."
305     exit 1
306     fi
307     }
308    
309     minstall()
310     {
311     if [ -f ./[mM]akefile -o -f ./GNUmakefile ]
312     then
313     make prefix=${BINDIR}/usr \
314     datadir=${BINDIR}/usr/share \
315     infodir=${BINDIR}/usr/share/info \
316     localstatedir=${BINDIR}/var/lib \
317     mandir=${BINDIR}/usr/share/man \
318     sysconfdir=${BINDIR}/etc \
319     libdir=${BINDIR}/usr/$(mlibdir) \
320     "$@" install || die "minstall failed"
321 niro 2361 else
322     die "no Makefile found"
323     fi
324 niro 1640 }
325    
326     mmake()
327     {
328     make ${MAKEOPTS} ${EXTRA_EMAKE} "$@"
329     }
330    
331     munpack()
332     {
333     local SRCFILE
334     local IFTAR
335     local DEST
336    
337     SRCFILE=$1
338    
339     if [[ -z $2 ]]
340     then
341     DEST=${BUILDDIR}
342     else
343     DEST=$2
344     fi
345    
346 niro 1795 echo -e "${COLBLUE}>>>${COLGREEN} Unpacking ${SOURCEDIR}/${PNAME}/${SRCFILE}${COLDEFAULT}"
347    
348 niro 1640 [[ ! -d ${DEST} ]] && install -d ${DEST}
349    
350     case "${SRCFILE##*.}" in
351     bz2)
352     IFTAR="$(basename $SRCFILE .bz2)"
353     IFTAR="${IFTAR##*.}"
354     if [[ ${IFTAR} = tar ]]
355     then
356     tar --no-same-owner -xvjf ${SOURCEDIR}/${PNAME}/${SRCFILE} -C ${DEST} || die ".tar.bz2 unpack failed."
357     else
358     pushd ${DEST} > /dev/null
359     bzcat ${SOURCEDIR}/${PNAME}/${SRCFILE} > ${DEST}/$(basename ${SRCFILE} .bz2) || die ".bz2 unpack failed."
360     popd > /dev/null
361     fi
362     ;;
363     gz)
364     IFTAR="$(basename $SRCFILE .gz)"
365     IFTAR="${IFTAR##*.}"
366     if [[ ${IFTAR} = tar ]]
367     then
368     tar --no-same-owner -xvzf ${SOURCEDIR}/${PNAME}/${SRCFILE} -C ${DEST} || die ".tar.gz unpack failed."
369     else
370     pushd ${DEST} > /dev/null
371     zcat ${SOURCEDIR}/${PNAME}/${SRCFILE} > ${DEST}/$(basename ${SRCFILE} .gz) || die ".gz unpack failed."
372     popd > /dev/null
373     fi
374     ;;
375     xz)
376     IFTAR="$(basename $SRCFILE .xz)"
377     IFTAR="${IFTAR##*.}"
378     if [[ ${IFTAR} = tar ]]
379     then
380     tar --no-same-owner -xvJf ${SOURCEDIR}/${PNAME}/${SRCFILE} -C ${DEST} || die ".tar.xz unpack failed."
381     else
382     pushd ${DEST} > /dev/null
383     xzcat ${SOURCEDIR}/${PNAME}/${SRCFILE} > ${DEST}/$(basename ${SRCFILE} .xz) || die ".xz unpack failed."
384     popd > /dev/null
385     fi
386     ;;
387     tbz2|mpks|mpk)
388     tar --no-same-owner -xvjf ${SOURCEDIR}/${PNAME}/${SRCFILE} -C ${DEST} || die ".tbz2 unpack failed."
389     ;;
390     tgz)
391     tar --no-same-owner -xvzf ${SOURCEDIR}/${PNAME}/${SRCFILE} -C ${DEST} || die ".tgz unpack failed."
392     ;;
393     txz|mpkzs|mpkz)
394     tar --no-same-owner -xvJf ${SOURCEDIR}/${PNAME}/${SRCFILE} -C ${DEST} || die ".txz unpack failed."
395     ;;
396     rar)
397 niro 2041 unrar x ${SOURCEDIR}/${PNAME}/${SRCFILE} ${DEST} || die ".rar unpack failed."
398 niro 1640 ;;
399     zip|xpi|jar)
400     unzip ${SOURCEDIR}/${PNAME}/${SRCFILE} -d ${DEST} || die ".zip unpack failed."
401     ;;
402     rpm)
403     pushd ${DEST} > /dev/null
404     rpm2targz ${SOURCEDIR}/${PNAME}/${SRCFILE} || die "rpm2targz: .rpm unpack failed."
405     tar --no-same-owner -xvzf ${SRCFILE/.rpm/.tar.gz} || die "tar: .rpm unpack failed."
406     if [[ -f ${DEST}/${SRCFILE/.rpm/.tar.gz} ]]
407     then
408     rm ${DEST}/${SRCFILE/.rpm/.tar.gz}
409     fi
410     ;;
411     *)
412     die "munpack failed"
413     ;;
414     esac
415     }
416    
417     mpatch()
418     {
419     local PATCHOPTS
420     local PATCHFILE
421     local i
422    
423     PATCHOPTS=$1
424     PATCHFILE=$2
425    
426     if [[ -z $2 ]]
427     then
428     PATCHFILE=$1
429    
430     ## patch level auto-detection, get patch level
431     for ((i=0; i < 10; i++))
432     do
433     patch --dry-run -Np${i} -i ${SOURCEDIR}/${PNAME}/${PATCHFILE} > /dev/null
434     if [[ $? = 0 ]]
435     then
436     PATCHOPTS="-Np${i}"
437     break
438     fi
439     done
440     fi
441    
442     echo -e "${COLBLUE}*** ${COLGREEN}Applying patch '${PATCHFILE}'${COLDEFAULT}"
443     patch "${PATCHOPTS}" -i ${SOURCEDIR}/${PNAME}/${PATCHFILE}
444     }
445    
446     mlibtoolize()
447     {
448     local opts="$@"
449     [[ -z ${opts} ]] && opts="--verbose --install --force"
450    
451     libtoolize ${opts} || die "running: mlibtoolize ${opts}"
452     }
453    
454     mautoreconf()
455     {
456     local opts="$@"
457     [[ -z ${opts} ]] && opts="--verbose --install --force"
458    
459     autoreconf ${opts} || die "running: mautoreconf ${opts}"
460     }
461    
462     minstalldocs()
463     {
464     local docfiles
465     local doc
466     docfiles="$@"
467    
468     if [ ! -d ${BINDIR}/usr/share/doc/${PNAME}-${PVER} ]
469     then
470     install -d ${BINDIR}/usr/share/doc/${PNAME}-${PVER} || die "creating doc dirs."
471     fi
472    
473     for doc in ${docfiles}
474     do
475     if [ -f ${doc} ]
476     then
477     if mqueryfeature "compressdoc"
478     then
479     cat ${doc} | gzip -9c > ${BINDIR}/usr/share/doc/${PNAME}-${PVER}/$(basename ${doc}).gz || die "gzipping +installing ${doc}."
480     chmod 0644 ${BINDIR}/usr/share/doc/${PNAME}-${PVER}/$(basename ${doc}).gz || die "fixing permissions of ${doc}."
481     else
482     install -m 0644 ${doc} ${BINDIR}/usr/share/doc/${PNAME}-${PVER} || die "installing ${doc}."
483     fi
484     fi
485     done
486     }
487    
488     mstriplibs()
489     {
490     local stripdir="$@"
491    
492     [[ -z ${stripdir} ]] && stripdir="${BINDIR}"
493     [[ -z ${STRIP_DYN_LIB} ]] && STRIP_DYN_LIB="--strip-debug"
494     find ${stripdir} ! -type d | xargs --no-run-if-empty file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs --no-run-if-empty strip ${STRIP_DYN_LIB} 2> /dev/null
495     }
496    
497     mstripbins()
498     {
499     local stripdir="$@"
500    
501     [[ -z ${stripdir} ]] && stripdir="${BINDIR}"
502     [[ -z ${STRIP_DYN_BIN} ]] && STRIP_DYN_BIN="--strip-debug"
503     find ${stripdir} ! -type d | xargs --no-run-if-empty file | grep "executable" | grep ELF | cut -f 1 -d : | xargs --no-run-if-empty strip ${STRIP_DYN_BIN} 2> /dev/null
504     }
505    
506     mstripstatic()
507     {
508     local stripdir="$@"
509    
510     [[ -z ${stripdir} ]] && stripdir="${BINDIR}"
511     [[ -z ${STRIP_STATIC_LIB} ]] && STRIP_STATIC_LIB="--strip-debug"
512     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
513     }
514    
515     mstriplibtoolarchive()
516     {
517     local stripdir="$@"
518    
519     [[ -z ${stripdir} ]] && stripdir="${BINDIR}"
520     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
521     }
522    
523     mpurgetargets()
524     {
525     local stripdir="$@"
526     local target
527    
528     [[ -z ${stripdir} ]] && stripdir=${BINDIR}
529     # nothing to do in this case
530     [[ -z ${PURGE_TARGETS[*]} ]] && return
531    
532     for target in ${PURGE_TARGETS[*]}
533     do
534     # check if target is a regex pattern without any slashes
535     if [[ ${target} = ${target//\/} ]]
536     then
537     find ${BINDIR} -type f -name "${target}" | xargs --no-run-if-empty rm -f -- 2> /dev/null
538     else
539     rm -f -- ${target} 2> /dev/null
540     fi
541     done
542     }
543    
544     mcompressdocs()
545     {
546     local bindir="$@"
547    
548     if [ -d ${bindir}/usr/share/man ]
549     then
550     echo -e "${COLBLUE}===${COLGREEN} compressing man-pages ...${COLDEFAULT}"
551     ${MLIBDIR}/compressdoc -g -9 ${bindir}/usr/share/man
552     fi
553    
554     if [ -d ${bindir}/usr/share/info ]
555     then
556     echo -e "${COLBLUE}===${COLGREEN} compressing info-pages ...${COLDEFAULT}"
557     ${MLIBDIR}/compressdoc -g -9 ${bindir}/usr/share/info
558     fi
559     }
560    
561     sminclude()
562     {
563     local i
564    
565     if [[ -n "$@" ]]
566     then
567     for i in $@
568     do
569     echo "--- Including ${SMAGESCRIPTSDIR}/include/${i}.sminc"
570     source ${SMAGESCRIPTSDIR}/include/${i}.sminc
571     done
572     echo
573     fi
574     }
575    
576 niro 1689 march()
577     {
578     local retval
579    
580     if [[ ! -z ${ARCH} ]]
581     then
582     echo "${ARCH}"
583     retval=0
584     else
585     retval=1
586     fi
587    
588     return "${retval}"
589     }
590    
591     marchsrcfile()
592     {
593     local retval
594 niro 1796 local var="$1"
595    
596     [[ -z ${var} ]] && var="SRCFILE"
597    
598     if [[ ! -z $(eval echo \$${var}_${ARCH/i*86/x86}) ]]
599 niro 1689 then
600 niro 1796 eval echo \$${var}_${ARCH/i*86/x86} | tr ';' '\n'
601 niro 1689 retval=0
602     else
603     retval=1
604     fi
605    
606     return "${retval}"
607     }
608    
609     marchsrcdir()
610     {
611     local retval
612 niro 2362 local var="$1"
613    
614     [[ -z ${var} ]] && var="SRCDIR"
615    
616     if [[ ! -z $(eval echo \$${var}_${ARCH/i*86/x86}) ]]
617 niro 1689 then
618 niro 2362 eval echo \$${var}_${ARCH/i*86/x86} | tr ';' '\n'
619 niro 1689 retval=0
620     else
621     retval=1
622     fi
623    
624     return "${retval}"
625     }
626    
627     marchdepend()
628     {
629     local retval
630 niro 2362 local var="$1"
631 niro 1689
632 niro 2362 [[ -z ${var} ]] && var="DEPEND"
633    
634     if [[ ! -z $(eval echo \$${var}_${ARCH/i*86/x86}) ]]
635 niro 1689 then
636 niro 2362 echo "\$(eval echo \\\$${var}_\${ARCH/i*86/x86} | tr ';' '\n')"
637 niro 1689 retval=0
638     else
639     retval=1
640     fi
641    
642     return "${retval}"
643     }
644    
645     marchsdepend()
646     {
647     local retval
648 niro 2362 local var="$1"
649    
650     [[ -z ${var} ]] && var="SDEPEND"
651    
652     if [[ ! -z $(eval echo \$${var}_${ARCH/i*86/x86}) ]]
653 niro 1689 then
654 niro 2362 echo "\$(eval echo \\\$${var}_\${ARCH/i*86/x86} | tr ';' '\n')"
655 niro 1689 retval=0
656     else
657     retval=1
658     fi
659    
660     return "${retval}"
661     }
662    
663 niro 1640 setup_distcc_environment()
664     {
665     if [ -x /usr/bin/distcc ]
666     then
667     echo -e "${COLBLUE}---${COLGREEN} Using DistCC for compilation ...${COLDEFAULT}"
668     export PATH=/usr/$(mlibdir)/distcc/bin:${PATH} || die "distcc: could not export new $PATH"
669    
670     export DISTCC_DIR="${DISTCC_DIR}" || die "distcc_dir export failed"
671    
672     # creating distcc tempdir
673     install -o distcc -g daemon -d ${DISTCC_DIR}
674     chmod 1777 ${DISTCC_DIR}
675     fi
676     }
677    
678 niro 2159 setup_icecc_environment()
679     {
680     if [ -x /usr/bin/icecc ]
681     then
682     echo -e "${COLBLUE}---${COLGREEN} Using IceCC for compilation ...${COLDEFAULT}"
683     export PATH=/usr/$(mlibdir)/icecc/bin:${PATH} || die "icecc: could not export new $PATH"
684     fi
685     }
686    
687 niro 1640 setup_ccache_environment()
688     {
689     if [ -x /usr/bin/ccache ]
690     then
691 niro 2172 mqueryfeature "icecc" && export CCACHE_PREFIX=icecc
692 niro 2159
693 niro 1640 echo -e "${COLBLUE}---${COLGREEN} Using CCache for compilation ...${COLDEFAULT}"
694     export PATH=/usr/$(mlibdir)/ccache/bin:${PATH} || die "ccache: could not export new $PATH"
695     fi
696     }
697    
698     # fixes given dependencies to match a MAGE_TARGET
699     # fix_mage_deps -target s/depend # <-- note -target !
700     fix_mage_deps()
701     {
702     local target="$1"
703     local depend="$2"
704     local NDEPEND
705     local sym dep cat pver pname
706    
707 niro 2140 # first of all remove all tabs and duplicate lines
708     if [ -n "${depend}" ]
709     then
710     depend=$(echo "${depend}" | sed 's:\t::g' | sort -u)
711     fi
712    
713 niro 1640 # deps and provides are special
714     # they must be fixed to match the target
715    
716     # run this only if target and depend is not empty
717     if [ -n "${target}" ] && [ -n "${depend}" ]
718     then
719     # fix DEPEND
720     while read sym dep
721     do
722     # ignore empty lines
723     [[ -z ${dep} ]] && continue
724    
725     cat="$(dirname ${dep})"
726     # change if not virtual
727     if [[ ${cat} = virtual ]]
728     then
729     pname="$(basename ${dep})"
730     else
731     # fix pver to target-pver
732     # to get pname-target-pver
733    
734     # doing it backwards !
735     pver="${dep##*-}"
736     # full pver
737     pname="$(basename ${dep/-${pver}/})${target}-${pver}"
738     fi
739    
740     # do not add empty lines
741     if [ -z "${NDEPEND}" ]
742     then
743     NDEPEND="${sym} ${cat}/${pname}"
744     else
745     NDEPEND="${NDEPEND}
746     ${sym} ${cat}/${pname}"
747     fi
748    
749     unset cat pname pver
750     done << EOF
751     ${depend}
752     EOF
753     # set NDEPEND to DEPEND
754     depend="${NDEPEND}"
755     fi
756    
757     echo "${depend}"
758     }
759    
760     # build_mage_script(): helper functions for regen_mage_tree()
761     # generates an mage file with given information in smage file
762     # needs at least:
763     # PNAME name of pkg
764     # PVER version
765     # PBUILD revision
766 niro 1641 # PCAT category of the pkg
767     # PCATEGORIE category of the pkg (deprecated, use PCAT!)
768 niro 1640 # STATE state of pkg stable|unstable|old
769     # DESCRIPTION a short description (opt)
770     # HOMEPAGE homepage (opt)
771 niro 1648 # LICENSE license information of the pkg (opt)
772 niro 1640 # DEPEND runtime dependencies (opt)
773     # SDEPEND adds needed deps to build the pkg (opt)
774     # PROVIDE provides a virtual (opt)
775     #
776     # special tags:
777     # PKGTYPE type of pkg
778     # INHERITS which functions get included
779     # SPECIAL_FUNCTIONS special functions which should also be added
780     # warning: they get killed before the build starts !
781     # SPLIT_PACKAGES names of all subpackages which are splitted from parent
782     # SPLIT_PACKAGE_BASE base package name for splitpackages
783     # (only in the resulting magefile}
784     #
785     # MAGE_TREE_DEST target destination of the generated tree
786     # REGEN_MAGE_TREE set to 'true' to enable this
787     #
788     # gets called with build_mage_script target
789     build_mage_script()
790     {
791     local magefile
792     local dest
793     local target
794     local split_pkg_base
795     local sym
796     local depname
797    
798     # if MAGE_TREE_DEST not set use BUILDDIR
799     : ${MAGE_TREE_DEST=${BUILDDIR}/mage-tree}
800    
801     # determinate which suffix this mage file should get, if any
802     [[ $1 = --target ]] && shift && target="-$1"
803    
804     # mark package as splitpackage
805     [[ $1 = --split-pkg-base ]] && shift && split_pkg_base="$1"
806    
807     # name of magefile
808     magefile="${PNAME}${target}-${PVER}-${PBUILD}.mage"
809    
810     # destination to magefile
811 niro 1641 dest="${MAGE_TREE_DEST}/${PCAT}/${PNAME}${target}/${magefile}"
812 niro 1640
813     # show what we are doing
814     echo -e "${COLBLUE}===${COLGREEN} generating mage file:${COLDEFAULT}"
815     echo "${dest}"
816    
817     install -d "$(dirname ${dest})"
818     # now build the mage file
819     > ${dest}
820    
821     # pgkname and state
822 niro 1649 echo "PKGNAME=\"${PNAME}${target}-${PVER}-\${ARCH}$(print_distrotag)-${PBUILD}\"" >> ${dest}
823 niro 1640 echo "STATE=\"${STATE}\"" >> ${dest}
824    
825     # description and homepage
826     echo "DESCRIPTION=\"${DESCRIPTION}\"" >> ${dest}
827     echo "HOMEPAGE=\"${HOMEPAGE}\"" >> ${dest}
828    
829 niro 1648 # license information
830     echo "LICENSE=\"${LICENSE}\"" >> ${dest}
831    
832 niro 1640 # special tags and vars
833     echo "PKGTYPE=\"${PKGTYPE}\"" >> ${dest}
834    
835     # echo MAGE_TARGETS ## note -target is needed !
836     echo "MAGE_TARGETS=\"${target}\"" >> ${dest}
837    
838     # split package base
839     echo "SPLIT_PACKAGE_BASE=\"${split_pkg_base}\"" >> ${dest}
840    
841     # add special vars
842     if [ -n "${SPECIAL_VARS}" ]
843     then
844     local i
845     for i in ${SPECIAL_VARS}
846     do
847     # being tricky here :)
848     echo "${i}=\"$(eval echo \$${i})\"" >> ${dest}
849     done
850     fi
851    
852     # add at least all includes
853     if [ -n "${INHERITS}" ]
854     then
855     echo -n "minclude" >> ${dest}
856     local i
857     for i in ${INHERITS}
858     do
859     echo -n " ${i}" >> ${dest}
860     done
861     # a CRLF is needed here!
862     echo >> ${dest}
863     fi
864    
865     # deps and provides
866     echo "DEPEND=\"$(fix_mage_deps "${target}" "${DEPEND}")\"" >> ${dest}
867     echo "SDEPEND=\"$(fix_mage_deps "${target}" "${SDEPEND}")\"" >> ${dest}
868     echo "PROVIDE=\"${PROVIDE}\"" >> ${dest}
869    
870     # add special functions
871     if [ -n "${SPECIAL_FUNCTIONS}" ]
872     then
873     local i
874     for i in ${SPECIAL_FUNCTIONS}
875     do
876     # add to mage (quotes needed !)
877     typeset -f "${i}" >> ${dest}
878     # unset to be safe (quotes needed !)
879     #unset "${i}" <-- later to get every target built
880     done
881     fi
882    
883     # pre|post-install|removes
884     typeset -f preinstall >> ${dest}
885     typeset -f postinstall >> ${dest}
886     typeset -f preremove >> ${dest}
887     typeset -f postremove >> ${dest}
888     }
889    
890     regen_mage_tree()
891     {
892     local subpackage
893    
894     # build them only if requested
895     if mqueryfeature regentree
896     then
897     # run it without targets
898     if [[ -n ${MAGE_TARGETS} ]]
899     then
900     # build for each target a mage file
901     # run it with several targets
902     echo
903     for subpackage in ${MAGE_TARGETS}
904     do
905     build_mage_script --target "${subpackage}"
906     done
907     echo
908    
909     # run it for splitpackages
910     elif [[ -n ${SPLIT_PACKAGES} ]]
911     then
912     local split_pkg_base="${PNAME}"
913     # save smage environment
914     split_save_variables
915     # build for each subpackage a mage file
916     # run it with several targets
917     echo
918     for subpackage in ${SPLIT_PACKAGES}
919     do
920     # get the right variables for the split
921     export PNAME="${subpackage}"
922     split_info_${subpackage}
923 niro 1651 # fix PCATEGORIE -> PCAT
924     if [[ ! -z ${PCATEGORIE} ]]
925     then
926     PCAT="${PCATEGORIE}"
927     unset PCATEGORIE
928     fi
929 niro 1640 # get the preinstall etc
930     split_export_inherits ${subpackage}
931     build_mage_script --split-pkg-base "${split_pkg_base}"
932     # delete split preinstall etc
933     split_delete_inherits ${subpackage}
934     # restore smage environment
935     split_restore_variables
936     done
937     echo
938     # unset all saved smage variables
939     split_unset_variables
940    
941     else
942     echo
943     build_mage_script
944     echo
945     fi
946     fi
947    
948     # now unset all uneeded vars to be safe
949     # unset PKGNAME <-- don't do that; smage needs this var
950     # unset to be safe (quotes needed !)
951     # for i in ${SPECIAL_FUNCTIONS}
952     # do
953     # unset "${i}"
954     # done
955     unset SPECIAL_FUNCTIONS
956     # for i in ${SPECIAL_VARS}
957     # do
958     # unset "${i}"
959     # done
960     unset SPECIAL_VARS
961     unset STATE
962     unset DESCRIPTION
963     unset HOMEPAGE
964     # unset PKGTYPE <-- don't do that either; smage needs this var
965     unset INHERITS
966     unset DEPEND
967     unset SDEPEND
968     unset PROVIDE
969     unset preinstall
970     unset postinstall
971     unset preremove
972     unset postremove
973     }
974    
975     split_save_variables()
976     {
977     export SAVED_PNAME="${PNAME}"
978     export SAVED_PVER="${PVER}"
979     export SAVED_PBUILD="${PBUILD}"
980 niro 1641 export SAVED_PCAT="${PCAT}"
981 niro 1640 export SAVED_DESCRIPTION="${DESCRIPTION}"
982     export SAVED_HOMEPAGE="${HOMEPAGE}"
983     export SAVED_SPECIAL_VARS="${SPECIAL_VARS}"
984 niro 2158 export SAVED_SPECIAL_FUNCTIONS="${SPECIAL_FUNCTIONS}"
985 niro 1640 export SAVED_STATE="${STATE}"
986     export SAVED_INHERITS="${INHERITS}"
987     export SAVED_DEPEND="${DEPEND}"
988     export SAVED_SDEPEND="${SDEPEND}"
989     export SAVED_PROVIDE="${PROVIDE}"
990     export SAVED_PKGTYPE="${PKGTYPE}"
991    
992     # special handling needed for mage features
993     # pkgbuild
994     mqueryfeature "pkgbuild" && export SAVED_FEATURE_PKGBUILD="pkgbuild"
995     mqueryfeature "!pkgbuild" && export SAVED_FEATURE_PKGBUILD="!pkgbuild"
996     # strip
997     mqueryfeature "strip" && export SAVED_FEATURE_STRIP="strip"
998     mqueryfeature "!strip" && export SAVED_FEATURE_STRIP="!strip"
999     # libtool
1000     mqueryfeature "libtool" && export SAVED_FEATURE_LIBTOOL="libtool"
1001     mqueryfeature "!libtool" && export SAVED_FEATURE_LIBTOOL="!libtool"
1002     # compressdoc
1003     mqueryfeature "compressdoc" && export SAVED_FEATURE_COMPRESSDOC="compressdoc"
1004     mqueryfeature "!compressdoc" && export SAVED_FEATURE_COMPRESSDOC="!compressdoc"
1005    
1006     # bindir too
1007     export SAVED_BINDIR="${BINDIR}"
1008    
1009     # export the SPLIT_PACKAGE_BASE
1010     export SPLIT_PACKAGE_BASE="${SAVED_PNAME}"
1011    
1012     # functions
1013     if [[ ! -z $(typeset -f preinstall) ]]
1014     then
1015     # rename the old one
1016     local saved_preinstall
1017     saved_preinstall=SAVED_$(typeset -f preinstall)
1018     eval "${saved_preinstall}"
1019     export -f SAVED_preinstall
1020     fi
1021    
1022     if [[ ! -z $(typeset -f postinstall) ]]
1023     then
1024     # rename the old one
1025     local saved_postinstall
1026     saved_postinstall=SAVED_$(typeset -f postinstall)
1027     eval "${saved_postinstall}"
1028     export -f SAVED_postinstall
1029     fi
1030    
1031     if [[ ! -z $(typeset -f preremove) ]]
1032     then
1033     # rename the old one
1034     local saved_preremove
1035     saved_preremove=SAVED_$(typeset -f preremove)
1036     eval "${saved_preremove}"
1037     export -f SAVED_preremove
1038     fi
1039    
1040     if [[ ! -z $(typeset -f postremove) ]]
1041     then
1042     # rename the old one
1043     local saved_postremove
1044     saved_postremove=SAVED_$(typeset -f postremove)
1045     eval "${saved_postremove}"
1046     export -f SAVED_postremove
1047     fi
1048     }
1049    
1050     split_restore_variables()
1051     {
1052     export PNAME="${SAVED_PNAME}"
1053     export PVER="${SAVED_PVER}"
1054     export PBUILD="${SAVED_PBUILD}"
1055 niro 1641 export PCAT="${SAVED_PCAT}"
1056 niro 1640 export DESCRIPTION="${SAVED_DESCRIPTION}"
1057     export HOMEPAGE="${SAVED_HOMEPAGE}"
1058     export SPECIAL_VARS="${SAVED_SPECIAL_VARS}"
1059 niro 2163 export SPECIAL_FUNCTIONS="${SAVED_SPECIAL_FUNCTIONS}"
1060 niro 1640 export STATE="${SAVED_STATE}"
1061     export INHERITS="${SAVED_INHERITS}"
1062     export DEPEND="${SAVED_DEPEND}"
1063     export SDEPEND="${SAVED_SDEPEND}"
1064     export PROVIDE="${SAVED_PROVIDE}"
1065     export PKGTYPE="${SAVED_PKGTYPE}"
1066    
1067     # special handling needed for mage features
1068     # pkgbuild
1069     FVERBOSE=off msetfeature "${SAVED_FEATURE_PKGBUILD}"
1070     # strip
1071     FVERBOSE=off msetfeature "${SAVED_FEATURE_STRIP}"
1072     # libtool
1073     FVERBOSE=off msetfeature "${SAVED_FEATURE_LIBTOOL}"
1074     # compressdoc
1075     FVERBOSE=off msetfeature "${SAVED_FEATURE_COMPRESSDOC}"
1076    
1077     # bindir too
1078     export BINDIR="${SAVED_BINDIR}"
1079    
1080     # functions
1081     if [[ ! -z $(typeset -f SAVED_preinstall) ]]
1082     then
1083     # rename the old one
1084     local saved_preinstall
1085     saved_preinstall=$(typeset -f SAVED_preinstall)
1086     eval "${saved_preinstall/SAVED_/}"
1087     export -f preinstall
1088     fi
1089    
1090     if [[ ! -z $(typeset -f SAVED_postinstall) ]]
1091     then
1092     # rename the old one
1093     local saved_postinstall
1094     saved_postinstall=$(typeset -f SAVED_postinstall)
1095     eval "${saved_postinstall/SAVED_/}"
1096     export -f postinstall
1097     fi
1098    
1099     if [[ ! -z $(typeset -f SAVED_preremove) ]]
1100     then
1101     # rename the old one
1102     local saved_preremove
1103     saved_preremove=$(typeset -f SAVED_preremove)
1104     eval "${saved_preremove/SAVED_/}"
1105     export -f preremove
1106     fi
1107    
1108     if [[ ! -z $(typeset -f SAVED_postremove) ]]
1109     then
1110     # rename the old one
1111     local saved_postremove
1112     saved_postremove=$(typeset -f SAVED_postremove)
1113     eval "${saved_postremove/SAVED_/}"
1114     export -f postremove
1115     fi
1116     }
1117    
1118     split_unset_variables()
1119     {
1120     # unset saved vars; not needed anymore
1121     unset SAVED_PNAME
1122     unset SAVED_PVER
1123     unset SAVED_PBUILD
1124 niro 1641 unset SAVED_PCAT
1125 niro 1640 unset SAVED_DESCRIPTION
1126     unset SAVED_HOMEPAGE
1127     unset SAVED_SPECIAL_VARS
1128 niro 2158 unset SAVED_SPECIAL_FUNCTIONS
1129 niro 1640 unset SAVED_STATE
1130     unset SAVED_PKGTYPE
1131     unset SAVED_INHERITS
1132     unset SAVED_DEPEND
1133     unset SAVED_SDEPEND
1134     unset SAVED_PROVIDE
1135     unset SAVED_BINDIR
1136     unset SAVED_PKGTYPE
1137     unset SAVED_FEATURE_PKGBUILD
1138     unset SAVED_FEATURE_STRIP
1139     unset SAVED_FEATURE_LIBTOOL
1140     unset SAVED_FEATURE_COMPRESSDOC
1141     unset SPLIT_PACKAGE_BASE
1142     unset -f SAVED_preinstall
1143     unset -f SAVED_postinstall
1144     unset -f SAVED_preremove
1145     unset -f SAVED_postremove
1146     }
1147    
1148     split_export_inherits()
1149     {
1150     local subpackage="$1"
1151     local func
1152     local newfunc
1153    
1154     for func in preinstall postinstall preremove postremove
1155     do
1156     if [[ ! -z $(typeset -f ${func}_${subpackage}) ]]
1157     then
1158     newfunc=$(typeset -f ${func}_${subpackage})
1159     newfunc="${newfunc/_${subpackage} (/ (}"
1160     eval "${newfunc}"
1161     fi
1162     done
1163     }
1164    
1165     split_delete_inherits()
1166     {
1167     local subpackage="$1"
1168     local func
1169    
1170     for func in preinstall postinstall preremove postremove
1171     do
1172     if [[ ! -z $(typeset -f ${func}_${subpackage}) ]]
1173     then
1174     unset -f ${func}
1175     fi
1176     done
1177     }
1178    
1179     export_inherits()
1180     {
1181     local include="$1"
1182     shift
1183    
1184     while [ "$1" ]
1185     do
1186     local functions="$1"
1187    
1188     # sanity checks
1189     [ -z "${include}" ] && die "export_inherits(): \$include not given."
1190     [ -z "${functions}" ] && die "export_inherits(): \$functions not given."
1191    
1192     eval "${functions}() { ${include}_${functions} ; }"
1193    
1194     # debug
1195     [[ ${MAGEDEBUG} = on ]] && typeset -f "${functions}"
1196    
1197     shift
1198     done
1199     }
1200    
1201     generate_package_md5sum()
1202     {
1203     local dest
1204     local pcat
1205     local pname
1206     local pver
1207     local pbuild
1208     local parch
1209     local target
1210     local pkgname
1211    
1212     # very basic getops
1213     for i in $*
1214     do
1215     case $1 in
1216     --pcat|-c) shift; pcat="$1" ;;
1217     --pname|-n) shift; pname="$1" ;;
1218     --pver|-v) shift; pver="$1" ;;
1219     --pbuild|-b) shift; pbuild="$1" ;;
1220     --parch|a) shift; parch="$1" ;;
1221     --target|t) shift; target="$1" ;;
1222     esac
1223     shift
1224     done
1225    
1226     # sanity checks; abort if not given
1227     [ -z "${pcat}" ] && die "generate_package_md5sum() \$pcat not given."
1228     [ -z "${pname}" ] && die "generate_package_md5sum() \$pname not given."
1229     [ -z "${pver}" ] && die "generate_package_md5sum() \$pver not given."
1230     [ -z "${pbuild}" ] && die "generate_package_md5sum() \$pbuild not given."
1231     [ -z "${parch}" ] && die "generate_package_md5sum() \$parch not given."
1232    
1233     # check needed global vars
1234     [ -z "${PKGDIR}" ] && die "generate_package_md5sum() \$PKGDIR not set."
1235     [ -z "${PKGSUFFIX}" ] && die "generate_package_md5sum() \$PKGSUFFIX not set."
1236    
1237     # fix target as it may be empty !
1238     [ -n "${target}" ] && target="-${target}"
1239    
1240     # build pkgname
1241 niro 1649 pkgname="${pname}${target}-${pver}-${parch}$(print_distrotag)-${pbuild}"
1242 niro 1640
1243     # build pkg-md5-sum only if requested
1244     if mqueryfeature regentree
1245     then
1246     echo -ne "${COLBLUE}===${COLGREEN} generating md5's for ${pkgname}.${PKGSUFFIX} ... ${COLDEFAULT}"
1247    
1248     # abort if not exist
1249     if [ ! -f ${PKGDIR}/${pkgname}.${PKGSUFFIX} ]
1250     then
1251     echo -e "${COLRED}! exists${COLDEFAULT}"
1252     return 0
1253     fi
1254    
1255     # if MAGE_TREE_DEST not set use BUILDDIR
1256     : ${MAGE_TREE_DEST=${BUILDDIR}/mage-tree}
1257    
1258     # setup md5 dir
1259     dest="${MAGE_TREE_DEST}/${pcat}/${pname}${target}/md5"
1260     install -d ${dest}
1261    
1262     # gen md5sum
1263     ( cd ${PKGDIR}; md5sum "${pkgname}.${PKGSUFFIX}" ) \
1264     > ${dest}/${pkgname}.md5
1265     echo -e "${COLGREEN}done${COLDEFAULT}"
1266     fi
1267     }
1268    
1269     source_pkg_build()
1270     {
1271     if [[ ${PKGTYPE} = virtual ]]
1272     then
1273     echo "Virtual package detected; src-pkg-tarball not necessary ..."
1274     return 0
1275     fi
1276    
1277     if [[ ! -d ${SOURCEDIR}/${PNAME} ]]
1278     then
1279     echo "No SRC_URI defined; src-pkg-tarball not necessary ..."
1280     return 0
1281     fi
1282    
1283     [ -z "${SRCPKGDIR}" ] && die "\$SRCPKGDIR not found. Please setup your ${MAGERC} correctly."
1284    
1285     echo -e "${COLGREEN}Creating source package tarball ... ${COLDEFAULT}"
1286    
1287     # include the smage2 file
1288     cp ${SMAGENAME} ${SOURCEDIR}/${PNAME}
1289    
1290     ( cd ${SOURCEDIR}; tar cvjf ${BUILDDIR}/${PNAME}-${PVER}-${PBUILD}.tar.bz2 ${PNAME}; )
1291     [[ ! -d ${SRCPKGDIR} ]] && install -d ${SRCPKGDIR}
1292     mv ${BUILDDIR}/${PNAME}-${PVER}-${PBUILD}.tar.bz2 ${SRCPKGDIR}/${PNAME}-${PVER}-${PBUILD}.${SRCPKGSUFFIX}
1293    
1294 niro 1679 echo -e "${COLGREEN}Source package ${COLBLUE}${PNAME}-${PVER}-${PBUILD}.${SRCPKGSUFFIX} ${COLGREEN}successfully builded.${COLDEFAULT}\n"
1295 niro 1640 }
1296    
1297     step_by_step()
1298     {
1299     if mqueryfeature stepbystep
1300     then
1301     echo -e "${COLRED}Step-by-step enabled! Paused after $1.${COLDEFAULT}"
1302     echo "Press [enter] to continue"
1303     read
1304     fi
1305     }
1306    
1307     resume_stamp()
1308     {
1309     local step="$1"
1310     [[ ! -d ${BUILDDIR}/.stamps ]] && install -d ${BUILDDIR}/.stamps
1311     touch ${BUILDDIR}/.stamps/smage-${PKGNAME}-${step}
1312     }
1313    
1314     run_resume()
1315     {
1316     local step="$1"
1317    
1318     if mqueryfeature "resume" && [[ -f ${BUILDDIR}/.stamps/smage-${PKGNAME}-${step} ]]
1319     then
1320     echo -e "${COLMAGENTA}${step} already processed; doing nothing${COLDEFAULT}"
1321     return 0
1322     else
1323     return 1
1324     fi
1325     }
1326 niro 2157
1327     mqalint()
1328     {
1329     return 0
1330     }