#!/bin/bash # $Id$ # compiles/installs .smage2 source install scripts # needs pkgbuild_dir (mage) # TODO: record dynlib, gz | xz database # set default user mage.rc : ${MAGERC="/etc/mage.rc"} ## setup ## SMAGENAME="$1" SMAGEVERSION="$( < ${MLIBDIR}/version)" # export default C locale export LC_ALL=C source /etc/mage.rc.global source ${MAGERC} source ${MLIBDIR}/mage4.functions.sh # set PKGDIR and BUILDDIR and BINDIR to MROOT if [[ -n ${MROOT} ]] then export PKGDIR=${MROOT}/${PKGDIR} export BUILDDIR=${MROOT}/${BUILDDIR} export BINDIR=${MROOT}/${BINDIR} fi # sources the smage file and uses state from distribution file if exist # may helpful for repository support later on smagesource() { local file="$1" local mystate local mycodename source ${file} [[ -n ${STATE} ]] && mystate="${STATE}" # do not overide if local state was broken or disabled! case ${STATE} in broken) return ;; disabled) return ;; esac if [ -f ${SMAGESCRIPTSDIR}/distribution ] then source ${SMAGESCRIPTSDIR}/distribution [[ -n ${STATE} ]] && mystate="${STATE}" fi # now switch state and export it STATE="${mystate}" } showversion() { echo -en "Magellan Source Install v${SMAGEVERSION} " echo -e "-- Niels Rogalla (niro@magellan-linux.de)" } die() { xtitleclean echo -e ${COLRED}"Exited ${BASH_SOURCE} at line no ${BASH_LINENO}."${COLDEFAULT} echo "SMAGE failed: $@" exit 1 } die_pipestatus() { # the status change if we do any parameter declarations!! # dont do this anymore, keep this in mind! # # local pos="$1" # local comment="$2" # # [ ${PIPESTATUS[${pos}]} -ne 0 ] && die "${comment}" # [ ${PIPESTATUS[$1]} -ne 0 ] && die "$2" } xtitle() { if [[ ${TERM} = xterm ]] then echo -ne "\033]0;[sMage: $@]\007" fi return 0 } xtitleclean() { if [[ ${TERM} = xterm ]] then echo -ne "\033]0;\007" fi return 0 } syncsmage2() { xtitle "Updating smage2-script tree ..." local i for i in ${SMAGE2RSYNC} do rsync ${RSYNC_FETCH_OPTIONS} ${i} ${SMAGESCRIPTSDIR} if [[ $? = 0 ]] then break else continue fi done # clean up backup files (foo~) find ${SMAGESCRIPTSDIR} -name *~ -exec rm '{}' ';' xtitleclean } # $1 filename get_db_md5_sum() { local DB_FILE local MD5_FILE local i DB_ENTRY="$(basename $1)" MD5_FILE="${MD5DIR}/$(basename ${SMAGENAME} ${SMAGESUFFIX})" i="$(cat ${MD5_FILE}| grep ${DB_ENTRY} | cut -d' ' -f1)" echo "${i}" } download_sources() { [ -z "${SRC_URI}" ] && echo -e "\nNothing declared to download.\n" && return 0 local EOA=${#SRC_URI[*]} local my_SRC_URI local my_SRC_URI_DEST local my_SRC_URI_MIRROR local my_SOURCEDIR local DB_MD5_SUM_FILE="${MD5DIR}/$(basename ${SMAGENAME} .${SMAGESUFFIX}).md5" local FETCHING local i mirror local wget_opts # filter wget command if busybox was found wget_opts="$(busybox_filter_wget_options ${WGET_FETCH_OPTIONS})" # install SRCDIR/PNAME if not exist [ ! -d ${SOURCEDIR}/${PNAME} ] && install -d ${SOURCEDIR}/${PNAME} # check if FETCHING is needed ( cd ${SOURCEDIR}/${PNAME}; md5sum -c ${DB_MD5_SUM_FILE} &> /dev/null ) if [[ $? = 0 ]] then # md5's ok, no fetching needed FETCHING=false else FETCHING=true fi for ((i=0; i < EOA; i++)) do # url to file my_SRC_URI="$(echo ${SRC_URI[${i}]} | cut -d' ' -f1)" # subdir in sources dir; the my_SRCI_URI file goes to there my_SRC_URI_DEST="$(echo ${SRC_URI[${i}]} | cut -d' ' -f2)" # if my_src_uri_dest is not equal my_src_uri; than an other dir is used if [[ ${my_SRC_URI_DEST} != ${my_SRC_URI} ]] then my_SOURCEDIR="${SOURCEDIR}/${PNAME}/${my_SRC_URI_DEST}" else my_SOURCEDIR="${SOURCEDIR}/${PNAME}" fi # create the SOURCEDIR install -d ${my_SOURCEDIR} # if an mirrored file than replace first the mirror uri if [[ -n $(echo ${my_SRC_URI} | grep 'mirror://') ]] then for mirror in ${MIRRORS} do my_SRC_URI_MIRROR="$(echo ${my_SRC_URI} | sed "s|mirror:/|${mirror}/sources|g")" if [[ ${FETCHING} = true ]] then echo -e "${COLBLUE}==>${COLGREEN} fetching ${my_SRC_URI_MIRROR}${COLDEFAULT}" wget \ ${wget_opts} \ --output-document="${my_SOURCEDIR}/$(basename ${my_SRC_URI_MIRROR})" \ "${my_SRC_URI_MIRROR}" if [[ $? = 0 ]] then break else continue fi fi done elif [[ -n $(echo ${my_SRC_URI} | grep 'sourceforge://') ]] then for mirror in ${SOURCEFORGE_MIRRORS} do my_SRC_URI_MIRROR="$(echo ${my_SRC_URI} | sed "s|sourceforge:/|${mirror}|g")" if [[ ${FETCHING} = true ]] then echo -e "${COLBLUE}==>${COLGREEN} fetching ${my_SRC_URI_MIRROR}${COLDEFAULT}" wget \ ${wget_opts} \ --output-document="${my_SOURCEDIR}/$(basename ${my_SRC_URI_MIRROR})" \ "${my_SRC_URI_MIRROR}" if [[ $? = 0 ]] then break else continue fi fi done elif [[ -n $(echo ${my_SRC_URI} | grep 'gnu://') ]] then for mirror in ${GNU_MIRRORS} do my_SRC_URI_MIRROR="$(echo ${my_SRC_URI} | sed "s|gnu:/|${mirror}|g")" if [[ ${FETCHING} = true ]] then echo -e "${COLBLUE}==>${COLGREEN} fetching ${my_SRC_URI_MIRROR}${COLDEFAULT}" wget \ ${wget_opts} \ --output-document="${my_SOURCEDIR}/$(basename ${my_SRC_URI_MIRROR})" \ "${my_SRC_URI_MIRROR}" if [[ $? = 0 ]] then break else continue fi fi done elif [[ -n $(echo ${my_SRC_URI} | grep 'kde://') ]] then for mirror in ${KDE_MIRRORS} do my_SRC_URI_MIRROR="$(echo ${my_SRC_URI} | sed "s|kde:/|${mirror}|g")" if [[ ${FETCHING} = true ]] then echo -e "${COLBLUE}==>${COLGREEN} fetching ${my_SRC_URI_MIRROR}${COLDEFAULT}" wget \ ${wget_opts} \ --output-document="${my_SOURCEDIR}/$(basename ${my_SRC_URI_MIRROR})" \ "${my_SRC_URI_MIRROR}" if [[ $? = 0 ]] then break else continue fi fi done elif [[ -n $(echo ${my_SRC_URI} | grep 'gnome://') ]] then for mirror in ${GNOME_MIRRORS} do my_SRC_URI_MIRROR="$(echo ${my_SRC_URI} | sed "s|gnome:/|${mirror}|g")" if [[ ${FETCHING} = true ]] then echo -e "${COLBLUE}==>${COLGREEN} fetching ${my_SRC_URI_MIRROR}${COLDEFAULT}" wget \ ${wget_opts} \ --output-document="${my_SOURCEDIR}/$(basename ${my_SRC_URI_MIRROR})" \ "${my_SRC_URI_MIRROR}" if [[ $? = 0 ]] then break else continue fi fi done else if [[ ${FETCHING} = true ]] then echo -e "${COLBLUE}==>${COLGREEN} fetching ${my_SRC_URI}${COLDEFAULT}" wget \ ${wget_opts} \ --output-document="${my_SOURCEDIR}/$(basename ${my_SRC_URI})" \ "${my_SRC_URI}" fi fi # unset them to be shure unset my_SRC_URI unset my_SRC_URI_DEST unset my_SRC_URI_MIRROR unset my_SOURCEDIR done # recheck md5 sums echo echo -e "${COLBLUE}===${COLGREEN} Checking MD5 sums:${COLDEFAULT}" ( cd ${SOURCEDIR}/${PNAME}; md5sum -c ${DB_MD5_SUM_FILE} ) || die "md5 failed" echo # not needed anymore unset SRC_URI } # dummy function, used if that does not exist in smage file src_prepare() { echo "no src_prepare defined" sleep 2 return 0 } # dummy function, used if that does not exist in smage file src_compile() { echo "no src_compile defined" sleep 2 return 0 } # dummy function, used if that does not exist in smage file src_install() { echo "no src_install defined" sleep 2 return 0 } mlibdir() { local libdir=lib [[ ${ARCH} = x86_64 ]] && libdir=lib64 echo "${libdir}" } mconfigure() { if [ -x ./configure ] then ./configure \ --prefix=/usr \ --host=${CHOST} \ --build=${CHOST} \ --mandir=/usr/share/man \ --infodir=/usr/share/info \ --datadir=/usr/share \ --sysconfdir=/etc \ --localstatedir=/var/lib \ --libdir=/usr/$(mlibdir) \ "$@" || die "mconfigure failed" else echo "configure is not an executable ..." exit 1 fi } minstall() { if [ -f ./[mM]akefile -o -f ./GNUmakefile ] then make prefix=${BINDIR}/usr \ datadir=${BINDIR}/usr/share \ infodir=${BINDIR}/usr/share/info \ localstatedir=${BINDIR}/var/lib \ mandir=${BINDIR}/usr/share/man \ sysconfdir=${BINDIR}/etc \ libdir=${BINDIR}/usr/$(mlibdir) \ "$@" install || die "minstall failed" else die "no Makefile found" fi } mmake() { make ${MAKEOPTS} ${EXTRA_EMAKE} "$@" } munpack() { local SRCFILE local IFTAR local DEST SRCFILE=$1 if [[ -z $2 ]] then DEST=${BUILDDIR} else DEST=$2 fi [[ ! -d ${DEST} ]] && install -d ${DEST} case "${SRCFILE##*.}" in bz2) IFTAR="$(basename $SRCFILE .bz2)" IFTAR="${IFTAR##*.}" if [[ ${IFTAR} = tar ]] then tar --no-same-owner -xvjf ${SOURCEDIR}/${PNAME}/${SRCFILE} -C ${DEST} || die ".tar.bz2 unpack failed." else pushd ${DEST} > /dev/null bzcat ${SOURCEDIR}/${PNAME}/${SRCFILE} > ${DEST}/$(basename ${SRCFILE} .bz2) || die ".bz2 unpack failed." popd > /dev/null fi ;; gz) IFTAR="$(basename $SRCFILE .gz)" IFTAR="${IFTAR##*.}" if [[ ${IFTAR} = tar ]] then tar --no-same-owner -xvzf ${SOURCEDIR}/${PNAME}/${SRCFILE} -C ${DEST} || die ".tar.gz unpack failed." else pushd ${DEST} > /dev/null zcat ${SOURCEDIR}/${PNAME}/${SRCFILE} > ${DEST}/$(basename ${SRCFILE} .gz) || die ".gz unpack failed." popd > /dev/null fi ;; xz) IFTAR="$(basename $SRCFILE .xz)" IFTAR="${IFTAR##*.}" if [[ ${IFTAR} = tar ]] then tar --no-same-owner -xvJf ${SOURCEDIR}/${PNAME}/${SRCFILE} -C ${DEST} || die ".tar.xz unpack failed." else pushd ${DEST} > /dev/null xzcat ${SOURCEDIR}/${PNAME}/${SRCFILE} > ${DEST}/$(basename ${SRCFILE} .xz) || die ".xz unpack failed." popd > /dev/null fi ;; tbz2|mpks|mpk) tar --no-same-owner -xvjf ${SOURCEDIR}/${PNAME}/${SRCFILE} -C ${DEST} || die ".tbz2 unpack failed." ;; tgz) tar --no-same-owner -xvzf ${SOURCEDIR}/${PNAME}/${SRCFILE} -C ${DEST} || die ".tgz unpack failed." ;; txz|mpkzs|mpkz) tar --no-same-owner -xvJf ${SOURCEDIR}/${PNAME}/${SRCFILE} -C ${DEST} || die ".txz unpack failed." ;; rar) unrar x ${SOURCEDIR}/${PNAME}/${SRCFILE} ${DEST} || die ".rar unpack failed." ;; zip|xpi) unzip ${SOURCEDIR}/${PNAME}/${SRCFILE} -d ${DEST} || die ".zip unpack failed." ;; rpm) pushd ${DEST} > /dev/null rpm2targz ${SOURCEDIR}/${PNAME}/${SRCFILE} || die "rpm2targz: .rpm unpack failed." tar --no-same-owner -xvzf ${SRCFILE/.rpm/.tar.gz} || die "tar: .rpm unpack failed." if [[ -f ${DEST}/${SRCFILE/.rpm/.tar.gz} ]] then rm ${DEST}/${SRCFILE/.rpm/.tar.gz} fi ;; *) die "munpack failed" ;; esac } mpatch() { local PATCHOPTS local PATCHFILE local i PATCHOPTS=$1 PATCHFILE=$2 if [[ -z $2 ]] then PATCHFILE=$1 ## patch level auto-detection, get patch level for ((i=0; i < 10; i++)) do patch --dry-run -Np${i} -i ${SOURCEDIR}/${PNAME}/${PATCHFILE} > /dev/null if [[ $? = 0 ]] then PATCHOPTS="-Np${i}" break fi done fi echo -e "${COLBLUE}*** ${COLGREEN}Applying patch '${PATCHFILE}'${COLDEFAULT}" patch "${PATCHOPTS}" -i ${SOURCEDIR}/${PNAME}/${PATCHFILE} } mlibtoolize() { local opts="$@" [[ -z ${opts} ]] && opts="--verbose --install --force" libtoolize ${opts} || die "running: mlibtoolize ${opts}" } mautoreconf() { local opts="$@" [[ -z ${opts} ]] && opts="--verbose --install --force" autoreconf ${opts} || die "running: mautoreconf ${opts}" } minstalldocs() { local docfiles docfiles="$@" if [ ! -d ${BINDIR}/usr/share/doc/${PNAME}-${PVER} ] then install -d ${BINDIR}/usr/share/doc/${PNAME}-${PVER} || die "creating doc dirs." fi local i for i in ${docfiles} do if [ -f ${i} ] then cat ${i} | gzip -9c > ${i}.gz || die "gzipping docs." install -m 0644 ${SRCDIR}/${i}.gz \ ${BINDIR}/usr/share/doc/${PNAME}-${PVER} || die "coping docs." fi done } mstriplibs() { local stripdir="$@" [ -z "${stripdir}" ] && stripdir=${BINDIR} find ${stripdir} | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip ${STRIP_DYN_LIB} 2> /dev/null } mstripbins() { local stripdir="$@" [ -z "${stripdir}" ] && stripdir=${BINDIR} find ${stripdir} | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip ${STRIP_DYN_BIN} 2> /dev/null } mstripstatic() { local stripdir="$@" [ -z "${stripdir}" ] && stripdir=${BINDIR} find ${stripdir} | xargs file | grep "ar archive" | cut -f 1 -d : | xargs strip ${STRIP_STATIC_LIB} 2> /dev/null } mcompressdocs() { local bindir="$@" if [ -d ${bindir}/usr/share/man ] then echo -e "${COLBLUE}===${COLGREEN} compressing man-pages ...${COLDEFAULT}" ${MLIBDIR}/compressdoc -g -9 ${bindir}/usr/share/man fi if [ -d ${bindir}/usr/share/info ] then echo -e "${COLBLUE}===${COLGREEN} compressing info-pages ...${COLDEFAULT}" ${MLIBDIR}/compressdoc -g -9 ${bindir}/usr/share/info fi } sminclude() { local i if [[ -n "$@" ]] then for i in $@ do echo "--- Including ${SMAGESCRIPTSDIR}/include/${i}.sminc" source ${SMAGESCRIPTSDIR}/include/${i}.sminc done echo fi } setup_distcc_environment() { if [ -x /usr/bin/distcc ] then echo -e "${COLBLUE}---${COLGREEN} Using DistCC for compilation ...${COLDEFAULT}" export PATH=/usr/$(mlibdir)/distcc/bin:${PATH} || die "distcc: could not export new $PATH" export DISTCC_DIR="${DISTCC_DIR}" || die "distcc_dir export failed" # creating distcc tempdir install -o distcc -g daemon -d ${DISTCC_DIR} chmod 1777 ${DISTCC_DIR} fi } setup_ccache_environment() { if [ -x /usr/bin/ccache ] then echo -e "${COLBLUE}---${COLGREEN} Using CCache for compilation ...${COLDEFAULT}" export PATH=/usr/$(mlibdir)/ccache/bin:${PATH} || die "ccache: could not export new $PATH" fi } # fixes given dependencies to match a MAGE_TARGET # fix_mage_deps -target s/depend # <-- note -target ! fix_mage_deps() { local target="$1" local depend="$2" local NDEPEND local sym dep cat pver pname # deps and provides are special # they must be fixed to match the target # run this only if target and depend is not empty if [ -n "${target}" ] && [ -n "${depend}" ] then # fix DEPEND while read sym dep do # ignore empty lines [[ -z ${dep} ]] && continue cat="$(dirname ${dep})" # change if not virtual if [[ ${cat} = virtual ]] then pname="$(basename ${dep})" else # fix pver to target-pver # to get pname-target-pver # doing it backwards ! pver="${dep##*-}" # full pver pname="$(basename ${dep/-${pver}/})${target}-${pver}" fi # do not add empty lines if [ -z "${NDEPEND}" ] then NDEPEND="${sym} ${cat}/${pname}" else NDEPEND="${NDEPEND} ${sym} ${cat}/${pname}" fi unset cat pname pver done << EOF ${depend} EOF # set NDEPEND to DEPEND depend="${NDEPEND}" fi echo "${depend}" } # build_mage_script(): helper functions for regen_mage_tree() # generates an mage file with given information in smage file # needs at least: # PNAME name of pkg # PVER version # PBUILD revision # PCATEGORIE categorie of the pkg # STATE state of pkg stable|unstable|old # DESCRIPTION va short description (opt) # HOMEPAGE homepage (opt) # DEPEND runtime dependencies (opt) # SDEPEND add. needed deps to build the pkg (opt) # PROVIDE provides a virtual (opt) # # special tags: # PKGTYPE type of pkg # INHERITS which functions get included # SPECIAL_FUNCTIONS special functions which should also be added # warning: they get killed before the build starts ! # SPLIT_PACKAGES names of all subpackages which are splitted from parent # SPLIT_PACKAGE_BASE base package name for splitpackages # (only in the resulting magefile} # # MAGE_TREE_DEST target destination of the generated tree # REGEN_MAGE_TREE set to 'true' to enable this # # gets called with build_mage_script target build_mage_script() { local magefile local dest local target local split_pkg_base local sym local depname # if MAGE_TREE_DEST not set use BUILDDIR : ${MAGE_TREE_DEST=${BUILDDIR}/mage-tree} # determinate which suffix this mage file should get, if any [[ $1 = --target ]] && shift && target="-$1" # mark package as splitpackage [[ $1 = --split-pkg-base ]] && shift && split_pkg_base="$1" # name of magefile magefile="${PNAME}${target}-${PVER}-${PBUILD}.mage" # destination to magefile dest="${MAGE_TREE_DEST}/${PCATEGORIE}/${PNAME}${target}/${magefile}" # show what we are doing echo -e "${COLBLUE}===${COLGREEN} generating mage file:${COLDEFAULT}" echo "${dest}" install -d "$(dirname ${dest})" # now build the mage file > ${dest} # pgkname and state echo "PKGNAME=\"${PNAME}${target}-${PVER}-\${ARCH}-${PBUILD}\"" >> ${dest} echo "STATE=\"${STATE}\"" >> ${dest} # description and homepage echo "DESCRIPTION=\"${DESCRIPTION}\"" >> ${dest} echo "HOMEPAGE=\"${HOMEPAGE}\"" >> ${dest} # special tags and vars echo "PKGTYPE=\"${PKGTYPE}\"" >> ${dest} # echo MAGE_TARGETS ## note -target is needed ! echo "MAGE_TARGETS=\"${target}\"" >> ${dest} # split package base echo "SPLIT_PACKAGE_BASE=\"${split_pkg_base}\"" >> ${dest} # add special vars if [ -n "${SPECIAL_VARS}" ] then local i for i in ${SPECIAL_VARS} do # being tricky here :) echo "${i}=\"$(eval echo \$${i})\"" >> ${dest} done fi # add at least all includes if [ -n "${INHERITS}" ] then echo -n "minclude" >> ${dest} local i for i in ${INHERITS} do echo -n " ${i}" >> ${dest} done # a CRLF is needed here! echo >> ${dest} fi # deps and provides echo "DEPEND=\"$(fix_mage_deps "${target}" "${DEPEND}")\"" >> ${dest} echo "SDEPEND=\"$(fix_mage_deps "${target}" "${SDEPEND}")\"" >> ${dest} echo "PROVIDE=\"${PROVIDE}\"" >> ${dest} # add special functions if [ -n "${SPECIAL_FUNCTIONS}" ] then local i for i in ${SPECIAL_FUNCTIONS} do # add to mage (quotes needed !) typeset -f "${i}" >> ${dest} # unset to be safe (quotes needed !) #unset "${i}" <-- later to get every target built done fi # pre|post-install|removes typeset -f preinstall >> ${dest} typeset -f postinstall >> ${dest} typeset -f preremove >> ${dest} typeset -f postremove >> ${dest} } regen_mage_tree() { local subpackage # build them only if requested if [[ ${REGEN_MAGE_TREE} = true ]] then # run it without targets if [[ -n ${MAGE_TARGETS} ]] then # build for each target a mage file # run it with several targets echo for subpackage in ${MAGE_TARGETS} do build_mage_script --target "${subpackage}" done echo # run it for splitpackages elif [[ -n ${SPLIT_PACKAGES} ]] then local split_pkg_base="${PNAME}" # save smage environment split_save_variables # build for each subpackage a mage file # run it with several targets echo for subpackage in ${SPLIT_PACKAGES} do # get the right variables for the split export PNAME="${subpackage}" split_info_${subpackage} # get the preinstall etc split_export_inherits ${subpackage} build_mage_script --split-pkg-base "${split_pkg_base}" # delete split preinstall etc split_delete_inherits ${subpackage} # restore smage environment split_restore_variables done echo # unset all saved smage variables split_unset_variables else echo build_mage_script echo fi fi # now unset all uneeded vars to be safe # unset PKGNAME <-- don't do that; smage needs this var # unset to be safe (quotes needed !) # for i in ${SPECIAL_FUNCTIONS} # do # unset "${i}" # done unset SPECIAL_FUNCTIONS # for i in ${SPECIAL_VARS} # do # unset "${i}" # done unset SPECIAL_VARS unset STATE unset DESCRIPTION unset HOMEPAGE unset PKGTYPE unset INHERITS unset DEPEND unset SDEPEND unset PROVIDE unset preinstall unset postinstall unset preremove unset postremove } split_save_variables() { export SAVED_PNAME="${PNAME}" export SAVED_PVER="${PVER}" export SAVED_PBUILD="${PBUILD}" export SAVED_PCATEGORIE="${PCATEGORIE}" export SAVED_DESCRIPTION="${DESCRIPTION}" export SAVED_HOMEPAGE="${HOMEPAGE}" export SAVED_SPECIAL_VARS="${SPECIAL_VARS}" export SAVED_STATE="${STATE}" export SAVED_PKGTYPE="${PKGTYPE}" export SAVED_INHERITS="${INHERITS}" export SAVED_DEPEND="${DEPEND}" export SAVED_SDEPEND="${SDEPEND}" export SAVED_PROVIDE="${PROVIDE}" export SAVED_NOPKGBUILD="${NOPKGBUILD}" # bindir too export SAVED_BINDIR="${BINDIR}" # export the SPLIT_PACKAGE_BASE export SPLIT_PACKAGE_BASE="${SAVED_PNAME}" # functions if [[ ! -z $(typeset -f preinstall) ]] then # rename the old one local saved_preinstall saved_preinstall=SAVED_$(typeset -f preinstall) eval "${saved_preinstall}" export -f SAVED_preinstall fi if [[ ! -z $(typeset -f postinstall) ]] then # rename the old one local saved_postinstall saved_postinstall=SAVED_$(typeset -f postinstall) eval "${saved_postinstall}" export -f SAVED_postinstall fi if [[ ! -z $(typeset -f preremove) ]] then # rename the old one local saved_preremove saved_preremove=SAVED_$(typeset -f preremove) eval "${saved_preremove}" export -f SAVED_preremove fi if [[ ! -z $(typeset -f postremove) ]] then # rename the old one local saved_postremove saved_postremove=SAVED_$(typeset -f postremove) eval "${saved_postremove}" export -f SAVED_postremove fi } split_restore_variables() { export PNAME="${SAVED_PNAME}" export PVER="${SAVED_PVER}" export PBUILD="${SAVED_PBUILD}" export PCATEGORIE="${SAVED_PCATEGORIE}" export DESCRIPTION="${SAVED_DESCRIPTION}" export HOMEPAGE="${SAVED_HOMEPAGE}" export SPECIAL_VARS="${SAVED_SPECIAL_VARS}" export STATE="${SAVED_STATE}" export PKGTYPE="${SAVED_PKGTYPE}" export INHERITS="${SAVED_INHERITS}" export DEPEND="${SAVED_DEPEND}" export SDEPEND="${SAVED_SDEPEND}" export PROVIDE="${SAVED_PROVIDE}" export NOPKGBUILD="${SAVED_NOPKGBUILD}" # bindir too export BINDIR="${SAVED_BINDIR}" # functions if [[ ! -z $(typeset -f SAVED_preinstall) ]] then # rename the old one local saved_preinstall saved_preinstall=$(typeset -f SAVED_preinstall) eval "${saved_preinstall/SAVED_/}" export -f preinstall fi if [[ ! -z $(typeset -f SAVED_postinstall) ]] then # rename the old one local saved_postinstall saved_postinstall=$(typeset -f SAVED_postinstall) eval "${saved_postinstall/SAVED_/}" export -f postinstall fi if [[ ! -z $(typeset -f SAVED_preremove) ]] then # rename the old one local saved_preremove saved_preremove=$(typeset -f SAVED_preremove) eval "${saved_preremove/SAVED_/}" export -f preremove fi if [[ ! -z $(typeset -f SAVED_postremove) ]] then # rename the old one local saved_postremove saved_postremove=$(typeset -f SAVED_postremove) eval "${saved_postremove/SAVED_/}" export -f postremove fi } split_unset_variables() { # unset saved vars; not needed anymore unset SAVED_PNAME unset SAVED_PVER unset SAVED_PBUILD unset SAVED_PCATEGORIE unset SAVED_DESCRIPTION unset SAVED_HOMEPAGE unset SAVED_SPECIAL_VARS unset SAVED_STATE unset SAVED_PKGTYPE unset SAVED_INHERITS unset SAVED_DEPEND unset SAVED_SDEPEND unset SAVED_PROVIDE unset SAVED_BINDIR unset SAVED_NOPKGBUILD unset SPLIT_PACKAGE_BASE unset -f SAVED_preinstall unset -f SAVED_postinstall unset -f SAVED_preremove unset -f SAVED_postremove } split_export_inherits() { local subpackage="$1" local func local newfunc for func in preinstall postinstall preremove postremove do if [[ ! -z $(typeset -f ${func}_${subpackage}) ]] then newfunc=$(typeset -f ${func}_${subpackage}) newfunc="${newfunc/_${subpackage} (/ (}" eval "${newfunc}" fi done } split_delete_inherits() { local subpackage="$1" local func for func in preinstall postinstall preremove postremove do if [[ ! -z $(typeset -f ${func}_${subpackage}) ]] then unset -f ${func} fi done } export_inherits() { local include="$1" shift while [ "$1" ] do local functions="$1" # sanity checks [ -z "${include}" ] && die "export_inherits(): \$include not given." [ -z "${functions}" ] && die "export_inherits(): \$functions not given." eval "${functions}() { ${include}_${functions} ; }" # debug [[ ${MAGEDEBUG} = on ]] && typeset -f "${functions}" shift done } generate_package_md5sum() { local dest local pcat local pname local pver local pbuild local parch local target local pkgname # very basic getops for i in $* do case $1 in --pcat|-c) shift; pcat="$1" ;; --pname|-n) shift; pname="$1" ;; --pver|-v) shift; pver="$1" ;; --pbuild|-b) shift; pbuild="$1" ;; --parch|a) shift; parch="$1" ;; --target|t) shift; target="$1" ;; esac shift done # sanity checks; abort if not given [ -z "${pcat}" ] && die "generate_package_md5sum() \$pcat not given." [ -z "${pname}" ] && die "generate_package_md5sum() \$pname not given." [ -z "${pver}" ] && die "generate_package_md5sum() \$pver not given." [ -z "${pbuild}" ] && die "generate_package_md5sum() \$pbuild not given." [ -z "${parch}" ] && die "generate_package_md5sum() \$parch not given." # check needed global vars [ -z "${PKGDIR}" ] && die "generate_package_md5sum() \$PKGDIR not set." [ -z "${PKGSUFFIX}" ] && die "generate_package_md5sum() \$PKGSUFFIX not set." # fix target as it may be empty ! [ -n "${target}" ] && target="-${target}" # build pkgname pkgname="${pname}${target}-${pver}-${parch}-${pbuild}" # build pkg-md5-sum only if requested if [[ ${REGEN_MAGE_TREE} = true ]] then echo -ne "${COLBLUE}===${COLGREEN} generating md5's for ${pkgname}.${PKGSUFFIX} ... ${COLDEFAULT}" # abort if not exist if [ ! -f ${PKGDIR}/${pkgname}.${PKGSUFFIX} ] then echo -e "${COLRED}! exists${COLDEFAULT}" return 0 fi # if MAGE_TREE_DEST not set use BUILDDIR : ${MAGE_TREE_DEST=${BUILDDIR}/mage-tree} # setup md5 dir dest="${MAGE_TREE_DEST}/${pcat}/${pname}${target}/md5" install -d ${dest} # gen md5sum ( cd ${PKGDIR}; md5sum "${pkgname}.${PKGSUFFIX}" ) \ > ${dest}/${pkgname}.md5 echo -e "${COLGREEN}done${COLDEFAULT}" fi } source_pkg_build() { if [[ ${PKGTYPE} = virtual ]] then echo "Virtual package detected; src-pkg-tarball not necessary ..." return 0 fi if [[ ! -d ${SOURCEDIR}/${PNAME} ]] then echo "No SRC_URI defined; src-pkg-tarball not necessary ..." return 0 fi [ -z "${SRCPKGDIR}" ] && die "\$SRCPKGDIR not found. Please setup your ${MAGERC} correctly." echo -e "${COLGREEN}Creating source package tarball ... ${COLDEFAULT}" # include the smage2 file cp ${SMAGENAME} ${SOURCEDIR}/${PNAME} ( cd ${SOURCEDIR}; tar cvjf ${BUILDDIR}/${PNAME}-${PVER}-${PBUILD}.tar.bz2 ${PNAME}; ) [[ ! -d ${SRCPKGDIR} ]] && install -d ${SRCPKGDIR} mv ${BUILDDIR}/${PNAME}-${PVER}-${PBUILD}.tar.bz2 ${SRCPKGDIR}/${PNAME}-${PVER}-${PBUILD}.${SRCPKGSUFFIX} echo -e "${COLGREEN}Source package ${COLBLUE}${PNAME}-${PVER}-${PBUILD}.${SRCPKGSUFFIX} ${COLGREEN}successfully builded.${COLDEFAULT}" } step_by_step() { if [[ ${STEP_BY_STEP} = true ]] then echo -e "${COLRED}Step-by-step enabled! Paused after $1.${COLDEFAULT}" echo "Press [enter] to continue" read fi } # print out our version showversion echo if [ -z "$1" ] then echo "No .smage2 file given. Exiting." echo exit 1 fi # updating smage2-scripts if [[ $1 = update ]] then if [ ! -d ${SOURCEDIR} ] then install -d ${SOURCEDIR} fi syncsmage2 exit 0 fi # creates md5sums for smages to given dir if [[ $1 = calcmd5 ]] then if [ $# -ge 2 ] then SMAGENAME="$2" MD5DIR="$3" [[ -z ${MD5DIR} ]] && MD5DIR="$(dirname ${SMAGENAME})/md5" smagesource ${SMAGENAME} || die "download source failed" # overridable sourcedir; must be declared after source of the smage2 CALC_SOURCEDIR="${CALC_SOURCEDIR:="${SOURCEDIR}/${PNAME}"}" [ -z "${SRC_URI}" ] && die "Nothing declared to calculate." # end of array EOA=${#SRC_URI[*]} [ ! -d ${MD5DIR} ] && install -d ${MD5DIR} # clear md5sum file MY_MD5_FILE="${MD5DIR}/$(basename ${SMAGENAME} .${SMAGESUFFIX}).md5" echo -n > ${MY_MD5_FILE} for ((i=0; i < EOA; i++)) do # url to file my_SRC_URI="$(echo ${SRC_URI[${i}]} | cut -d' ' -f1)" # subdir in sources dir; the my_SRCI_URI file goes to there my_SRC_URI_DEST="$(echo ${SRC_URI[${i}]} | cut -d' ' -f2)" # if my_src_uri_dest is not equal my_src_uri; than an other dir is used if [[ ${my_SRC_URI_DEST} != ${my_SRC_URI} ]] then MY_SRC_FILE="${my_SRC_URI_DEST}/$(basename ${SRC_URI[${i}]})" else MY_SRC_FILE="$(basename ${SRC_URI[${i}]})" fi if [ -e "${CALC_SOURCEDIR}/${MY_SRC_FILE}" ] then echo "calculating $(basename ${MY_SRC_FILE}) ..." ( cd ${CALC_SOURCEDIR}; md5sum "${MY_SRC_FILE}" ) >> ${MY_MD5_FILE} else echo "WARNING: File '$(basename ${MY_SRC_FILE}) not found in ${CALC_SOURCEDIR}." fi # unset them to be shure unset my_SRC_URI unset my_SRC_URI_DEST unset my_SRC_URI_MIRROR unset my_SOURCEDIR done echo echo "Calculating of md5 sums for '$(basename ${SMAGENAME} .${SMAGESUFFIX})' done." echo else echo "Usage: Calculating MD5 Sums:" echo " $(basename $0) calcmd5 /path/to/SMAGENAME [/path/to/MD5DIR]" echo echo echo "Export the CALC_SOURCEDIR variable to override current SOURCEDIRs." echo exit 1 fi exit 0 fi # download sources if [ "$1" == "download" -a -n "$2" ] then if [ ! -d ${SMAGESCRIPTSDIR} ] then install -d ${SMAGESCRIPTSDIR} fi # get smage SMAGENAME="$2" MD5DIR="$(dirname ${SMAGENAME})/md5" smagesource ${SMAGENAME} || die "download source failed" download_sources exit 0 fi # regen-mage-tree if [ "$1" == "only-regen-tree" -a -n "$2" ] then # set correct SMAGENAME SMAGENAME="$2" MD5DIR="$(dirname ${SMAGENAME})/md5" smagesource ${SMAGENAME} || die "regen: smage2 not found" regen_mage_tree # build several targets if [[ -n ${MAGE_TARGETS} ]] then for target in ${MAGE_TARGETS} do # build md5sum for existing packages generate_package_md5sum \ --pcat "${PCATEGORIE}" \ --pname "${PNAME}" \ --pver "${PVER}" \ --pbuild "${PBUILD}" \ --parch "${ARCH}" \ --target "${target}" done # build several subpackages elif [[ -n ${SPLIT_PACKAGES} ]] then split_save_variables for subpackage in ${SPLIT_PACKAGES} do # get the right variables for the split export PNAME="${subpackage}" split_info_${subpackage} # build md5sum for existing packages generate_package_md5sum \ --pcat "${PCATEGORIE}" \ --pname "${PNAME}" \ --pver "${PVER}" \ --pbuild "${PBUILD}" \ --parch "${ARCH}" # restore smage environment split_restore_variables done # unset all saved smage variables split_unset_variables else # build md5sum for existing packages generate_package_md5sum \ --pcat "${PCATEGORIE}" \ --pname "${PNAME}" \ --pver "${PVER}" \ --pbuild "${PBUILD}" \ --parch "${ARCH}" fi exit 0 fi if [ "$1" == "--create-src-tarball" -a -n "$2" ] then # set correct SMAGENAME SMAGENAME="$2" MD5DIR="$(dirname ${SMAGENAME})/md5" echo -e "${COLGREEN}create-src-tarball called for ${COLBLUE}${SMAGENAME}${COLGREEN} ...${COLDEFAULT}" smagesource ${SMAGENAME} || die "regen: smage2 not found" if [[ -d ${SOURCEDIR}/${PNAME} ]] then echo -e "${COLGREEN}Deleting old sourcefiles ${COLBLUE}${SOURCEDIR}/${PNAME}${COLGREEN} ...${COLDEFAULT}" rm -rf ${SOURCEDIR}/${PKGNAME} fi download_sources source_pkg_build ${SMAGENAME} exit 0 fi if [ "$1" == "--src-tarball" -a -n "$2" ] || [ "$1" == "-st" -a -n "$2" ] then SRCPKGTARBALL="${2}" USE_SRC_PKG_TARBALL=true # abort if given file is not a source pkg [[ ${SRCPKGTARBALL##*.} != ${SRCPKGSUFFIX} ]] && die "${SRCPKGTARBALL} is not a valid src-pkg file." # set correct SMAGENAME; use the one that the src_pkg provide # /path/to/SOURCEDIR/PNAME/SMAGENAME SMAGENAME="${SOURCEDIR}/$(basename ${SRCPKGTARBALL%-*-*})/$(basename ${SRCPKGTARBALL} .${SRCPKGSUFFIX}).${SMAGESUFFIX}" echo -e "${COLGREEN}Using src-tarball ${COLBLUE}${SRCPKGTARBALL}${COLGREEN} ...${COLDEFAULT}" [[ ! -d ${SOURCEDIR} ]] && install -d ${SOURCEDIR} # unpack srctarball [[ ! -f ${SRCPKGTARBALL} ]] && die "Error: ${SRCPKGTARBALL} does not exist. Aborting." tar xvjf ${SRCPKGTARBALL} -C ${SOURCEDIR} || die "Error unpackung src-tarball ${SRCPKGTARBALL}" [[ ! -f ${SMAGENAME} ]] && die "Included smage2 file in src-tarball not found: ${SMAGENAME}" fi [ ! -e ${MLIBDIR}/pkgbuild_dir.sh ] && die "Error: ${MLIBDIR}/pkgbuild_dir.sh not found. Aborting." [ -z "$(basename ${SMAGENAME} | grep .${SMAGESUFFIX})" ] && die "File '$(basename ${SMAGENAME})' is not a sMage v${SMAGEVERSION} file. Aborting." [ -z "${SOURCEDIR}" ] && die "\$SOURCEDIR not found. Please setup your ${MAGERC} correctly." [ -z "${SMAGESCRIPTSDIR}" ] && die "\$SMAGESCRIPTSDIR not found. Please setup your ${MAGERC} correctly." [ -z "${SMAGE2RSYNC}" ] && die "\$SMAGE2RSYNC not found. Please setup your ${MAGERC} correctly." [ -z "${BINDIR}" ] && die "no BINDIR variable found in ${MAGERC}" [ -z "${CHOST}" ] && die "no CHOST variable found in ${MAGERC}" [ -z "${CFLAGS}" ] && die "no CFLAGS variable found in ${MAGERC}" [ -z "${CXXFLAGS}" ] && die "no CXXFLAGS variable found in ${MAGERC}" smagesource ${SMAGENAME} || die "source failed" PKGNAME="${PNAME}-${PVER}-${ARCH}-${PBUILD}" MD5DIR="$(dirname ${SMAGENAME})/md5" SMAGE_LOG_CMD="tee -a /var/log/smage/${PKGNAME}.log" xtitle "Compiling ${PKGNAME}" echo -e "${COLGREEN}Compiling ${PKGNAME}${COLDEFAULT}" # auto regen mage tree if requested regen_mage_tree if [[ ${CREATE_SRC_PKG_TARBALL} = true ]] then if [[ -d ${SOURCEDIR}/${PNAME} ]] then echo -e "${COLBLUE}===${COLGREEN} deleting old sourcefiles ${COLBLUE}${SOURCEDIR}/${PNAME}${COLGREEN} ...${COLDEFAULT}" rm -rf ${SOURCEDIR}/${PNAME} fi fi # download sources [[ ${USE_SRC_PKG_TARBALL} != true ]] && download_sources # fixes some issues with these functions export -f src_prepare || die "src_prepare export failed" export -f src_compile || die "src_compile export failed" export -f src_install || die "src_install export failed" # fixes some compile issues export CHOST="${CHOST}" || die "CHOST export failed" export CFLAGS="${CFLAGS}" || die "CFLAGS export failed" export CXXFLAGS="${CFLAGS}" || die "CXXFLAGS export failed" export BINDIR="${BINDIR}" || die "BINDIR export failed" export MAKEOPTS="${MAKEOPTS}" || die "MAKEOPTS export failed" # setup distcc # setup for distcc goes *before* ccache, so ccache comes before distcc in path [[ ${SMAGE_USE_DISTCC} = true ]] && setup_distcc_environment # setup ccache [[ ${SMAGE_USE_CCACHE} = true ]] && setup_ccache_environment # small sleep to show our settings sleep 1 # clean up builddir if a previously one exist if [ -d ${BUILDDIR} ] then rm -rf ${BUILDDIR}/* || die "couldn't cleanup \$BUILDDIR." fi install -d ${BUILDDIR} || die "couldn't create \$BUILDDIR." # clean up srcdir if a previously unpacked one exist if [ -d ${SRCDIR} ] then rm -rf ${SRCDIR} fi # clean up bindir if a previous build exist or create a new one if [ -d ${BINDIR} ] then rm -rf ${BINDIR} fi install -d ${BINDIR} || die "couldn't create \$BINDIR." # cleans up package temp dir if a previous build exists if [ -d ${BUILDDIR}/${PKGNAME} ] then rm -rf ${BUILDDIR}/${PKGNAME} fi # cleans up timestamp if one exists if [ -f /var/tmp/timestamp ] then mage rmstamp fi # setup build logging [[ ! -d /var/log/smage ]] && install -d /var/log/smage echo -e "### Build started on $(date) ###\n" > /var/log/smage/${PKGNAME}.log src_prepare | ${SMAGE_LOG_CMD} die_pipestatus 0 "src_prepare failed" step_by_step $_ src_compile | ${SMAGE_LOG_CMD} die_pipestatus 0 "src_compile failed" step_by_step $_ # build several subpackages if [[ -n ${SPLIT_PACKAGES} ]] then # save bindir & pname split_save_variables export SAVED_BINDIR="${BINDIR}" for subpackage in ${SPLIT_PACKAGES} do if typeset -f src_install_${subpackage} > /dev/null then # export subpackage bindir export BINDIR="${SAVED_BINDIR}_${subpackage}" # export PNAME, several internal function and include # rely on this variable export PNAME="${subpackage}" echo echo -en "${COLBLUE}*** ${COLDEFAULT}" echo -en " Running ${COLGREEN}split src_install()${COLDEFAULT}" echo -en " for subpkg: ${COLBLUE}${PNAME}${COLDEFAULT}" echo -e " - basepkg: ${COLBLUE}${SPLIT_PACKAGE_BASE}${COLDEFAULT} ..." src_install_${subpackage} | ${SMAGE_LOG_CMD} die_pipestatus 0 "src_install_${subpackage} failed" step_by_step $_ fi done # restore bindir & pname split_restore_variables # unset all saved smage variables split_unset_variables else src_install | ${SMAGE_LOG_CMD} die_pipestatus 0 "src_install failed" step_by_step $_ fi # compressing doc, info & man files if [[ -n ${SPLIT_PACKAGES} ]] then for subpackage in ${SPLIT_PACKAGES} do mcompressdocs ${BINDIR}_${subpackage} done else mcompressdocs ${BINDIR} fi # stripping all bins and libs case ${NOSTRIP} in true|TRUE|yes|y) echo -e "NOSTRIP=true detected; Package will not be stripped ..." ;; *) if [[ -n ${SPLIT_PACKAGES} ]] then for subpackage in ${SPLIT_PACKAGES} do echo -e "${COLBLUE}===${COLGREEN} stripping binaries for '${subpackage}' ...${COLDEFAULT}" mstripbins ${BINDIR}_${subpackage} echo -e "${COLBLUE}===${COLGREEN} stripping dynamic libraries for '${subpackage}' ...${COLDEFAULT}" mstriplibs ${BINDIR}_${subpackage} echo -e "${COLBLUE}===${COLGREEN} stripping static libraries for '${subpackage}' ...${COLDEFAULT}" mstripstatic ${BINDIR}_${subpackage} done else echo -e "${COLBLUE}===${COLGREEN} stripping binaries ...${COLDEFAULT}" mstripbins ${BINDIR} echo -e "${COLBLUE}===${COLGREEN} stripping dynamic libraries ...${COLDEFAULT}" mstriplibs ${BINDIR} echo -e "${COLBLUE}===${COLGREEN} stripping static libraries ...${COLDEFAULT}" mstripstatic ${BINDIR} fi ;; esac # the new buildpkg command case ${NOPKGBUILD} in true|TRUE|yes|y) echo -e "NOPGKBUILD=true detected; Package will not be build ..." ;; *) # build several targets if [[ -n ${MAGE_TARGETS} ]] then for target in ${MAGE_TARGETS} do # check if a special target_pkgbuild exists if typeset -f ${target}_pkgbuild > /dev/null then # run it ${target}_pkgbuild fi # now create the target package ${MLIBDIR}/pkgbuild_dir.sh \ "${PNAME}-${target}-${PVER}-${ARCH}-${PBUILD}" \ ${BINDIR} || die "target: ${target} package-build failed" # build pkg-md5-sum if requested generate_package_md5sum \ --pcat "${PCATEGORIE}" \ --pname "${PNAME}" \ --pver "${PVER}" \ --pbuild "${PBUILD}" \ --parch "${ARCH}" \ --target "${target}" echo -e "${COLGREEN}\nPackage ${PNAME}-${target}-${PVER}-${ARCH}-${PBUILD} successfully builded.\n${COLDEFAULT}" done # build several subpackages elif [[ -n ${SPLIT_PACKAGES} ]] then split_save_variables for subpackage in ${SPLIT_PACKAGES} do # get the right variables for the split export PNAME="${subpackage}" split_info_${PNAME} # jump to next one if NOPKGBUILD is set in split_info case ${NOPKGBUILD} in true|TRUE|yes|y) continue ;; esac # check if an special subpackage_pkgbuild exists if typeset -f ${PNAME}_pkgbuild > /dev/null then # run it ${PNAME}_pkgbuild fi # now create the target package ${MLIBDIR}/pkgbuild_dir.sh \ "${PNAME}-${PVER}-${ARCH}-${PBUILD}" \ "${BINDIR}_${PNAME}" || die "split_package: ${PNAME} package-build failed" # build pkg-md5-sum if requested generate_package_md5sum \ --pcat "${PCATEGORIE}" \ --pname "${PNAME}" \ --pver "${PVER}" \ --pbuild "${PBUILD}" \ --parch "${ARCH}" echo -e "${COLGREEN}\nPackage ${PNAME}-${PVER}-${ARCH}-${PBUILD} successfully builded.\n${COLDEFAULT}" # restore smage environment split_restore_variables done # unset all saved smage variables split_unset_variables else ${MLIBDIR}/pkgbuild_dir.sh ${PKGNAME} ${BINDIR} || die "package-build failed" # build pkg-md5-sum if requested generate_package_md5sum \ --pcat "${PCATEGORIE}" \ --pname "${PNAME}" \ --pver "${PVER}" \ --pbuild "${PBUILD}" \ --parch "${ARCH}" echo -e "${COLGREEN}\nPackage ${PKGNAME} successfully builded.\n${COLDEFAULT}" fi # build src-pkg-tarball if requested [[ ${CREATE_SRC_PKG_TARBALL} = true ]] && source_pkg_build ${SMAGENAME} ;; esac if [[ ${SMAGE_BUILD_LOGGING} != false ]] then bzip2 -9f /var/log/smage/${PKGNAME}.log else [[ -f /var/log/smage/${PKGNAME}.log ]] && rm /var/log/smage/${PKGNAME}.log fi # for sure unset NOPKGBUILD unset NOSTRIP xtitleclean