--- trunk/mage/usr/lib/mage/smage2.sh 2005/01/06 02:57:12 33 +++ trunk/mage/usr/lib/mage/smage2.sh 2005/08/20 15:32:59 194 @@ -4,14 +4,13 @@ # needs pkgbuild_dir (mage) # SMAGE2 -# version: 0.3.6-r9 +# $Header: /home/cvsd/magellan-cvs/magellan-src/mage/usr/lib/mage/smage2.sh,v 1.25 2005-08-20 15:32:59 niro Exp $ #01.10.2004 # added ccache support # added distcc support ## setup ## -SMAGEVERSION=0.3.6-r8 PKGSUFFIX="mpk" SMAGENAME="$1" SMAGESUFFIX="smage2" @@ -19,6 +18,10 @@ #SMAGESCRIPTSDIR="/bootstrap/smage2-install-scripts" #SMAGE2RSYNC="rsync://192.168.0.2/smage2-scripts" MLIBDIR=/usr/lib/mage +SMAGEVERSION="$( < ${MLIBDIR}/version)" + +# export default C locale +export LC_ALL=C source /etc/mage.rc @@ -84,6 +87,128 @@ 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 + + + #install SRCDIR/PNAME if not exist + [ ! -d ${SOURCEDIR}/${PNAME} ] && install -d ${SOURCEDIR}/${PNAME} + + # check if FETCHING is needed + ( cd ${SOURCEDIR}/${PNAME}; md5sum --check ${DB_MD5_SUM_FILE} &> /dev/null ) + if [[ $? == 0 ]] + then + # md5's ok, not 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 + + # 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")" + + #echo "DEBUG: ${MY_SRC_URI}" + if [[ ${FETCHING} == true ]] + then + echo "==> fetching ${my_SRC_URI_MIRROR}" + wget \ + --passive-ftp \ + --tries 3 \ + --continue \ + --progress bar \ + --directory-prefix="${my_SOURCEDIR}" \ + "${my_SRC_URI_MIRROR}" + if [ "$?" == "0" ] + then + break + else + continue + fi + fi + done + else + #echo "DEBUG: ${SRC_URI[${i}]}" + if [[ ${FETCHING} == true ]] + then + echo "==> fetching ${my_SRC_URI}" + wget \ + --passive-ftp \ + --tries 3 \ + --continue \ + --progress bar \ + --directory-prefix="${my_SOURCEDIR}" \ + "${my_SRC_URI}" +# only needed to run through a list of mirrors +# if [ "$?" == "0" ] +# then +# break +# else +# continue +# fi + 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 ">== Checking MD5 sums:" + ( cd ${SOURCEDIR}/${PNAME}; md5sum --check ${DB_MD5_SUM_FILE} ) || die "md5 failed" + echo + + # not needed anymore + unset SRC_URI +} + # dummy function, used if that not exist in smage file src_prepare() { echo "no src_prepare defined" @@ -105,11 +230,6 @@ return 0 } - -build_mage_script() { - return 0 -} - mconfigure() { if [ -x ./configure ] then @@ -218,6 +338,34 @@ done } +mstriplibs() { + local stripdir="$@" + + [ -z "${stripdir}" ] && stripdir=${BINDIR} + find ${stripdir} | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 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-unneeded 2> /dev/null +} + +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 @@ -258,27 +406,327 @@ fi } + +# fixes given dependencies to match a MAGE_TARGET +# fix_mage_deps -target s/depend # <-- '-' is essential ! (build_mage_script needs this) +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 + cat="$(dirname ${dep})" + # abort on virtual + [[ ${cat} = virtual ]] && continue + + # fix pver to target-pver + # to get pname-target-pver + + # doing it backwards ! + pver="${dep##*-}" + pname=$(basename ${dep/-${pver}/}) + + # do not add empty lines + if [ -z "${NDEPEND}" ] + then + NDEPEND="${sym} ${cat}/${pname}${target}-${pver}" + else + NDEPEND="${NDEPEND} + ${sym} ${cat}/${pname}${target}-${pver}" + 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 wich should also be added +# warning: they get killed before the build starts ! +# +# 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 # <-- '-' is essential ! +build_mage_script() +{ + local magefile + local dest + local target + 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 + target="$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 "Generating Mage file:" + echo " ${dest}" + + install -d "$(dirname ${dest})" + # now build the mage file + > ${dest} + + # header + echo '# $Header: /home/cvsd/magellan-cvs/magellan-src/mage/usr/lib/mage/smage2.sh,v 1.25 2005-08-20 15:32:59 niro Exp $' >> ${dest} + echo >> ${dest} + + # pgkname and state + echo "PKGNAME=\"${PNAME}${target}-${PVER}-\${ARCH}-${PBUILD}\"" >> ${dest} + echo "STATE=\"${STATE}\"" >> ${dest} + echo >> ${dest} + + # description and homepage + echo "DESCRIPTION=\"${DESCRIPTION}\"" >> ${dest} + echo "HOMEPAGE=\"${HOMEPAGE}\"" >> ${dest} + echo >> ${dest} + + # special tags + echo "PKGTYPE=\"${PKGTYPE}\"" >> ${dest} + if [ -n "${INHERITS}" ] + then + echo -n "minclude" >> ${dest} + local i + for i in ${INHERITS} + do + echo -n " ${i}" >> ${dest} + done + echo >> ${dest} + fi + echo >> ${dest} + + # deps and provides + echo "DEPEND=\"$(fix_mage_deps "${target}" "${DEPEND}")\"" >> ${dest} + echo "SDEPEND=\"$(fix_mage_deps "${target}" "${SDEPEND}")\"" >> ${dest} + echo "PROVIDE=\"${PROVIDE}\"" >> ${dest} + echo >> ${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 + + # 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 + + # pre|post-install|removes + typeset -f preinstall >> ${dest} + echo >> ${dest} + typeset -f postinstall >> ${dest} + echo >> ${dest} + typeset -f preremove >> ${dest} + echo >> ${dest} + typeset -f postremove >> ${dest} + echo >> ${dest} +} + +regen_mage_tree() +{ + local i + + # build them only if requested + if [[ ${REGEN_MAGE_TREE} = true ]] + then + # build for each target an mage file + for i in ${MAGE_TARGETS} + do + echo + build_mage_script "-${i}" + echo + done + 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 +} + +# print out our version +showversion +echo + if [ -z "$1" ] then - showversion - echo echo "No .smage2 file given. Exiting." echo exit 1 fi -#updating smage2-scripts +# updating smage2-scripts if [ "$1" == "update" ] then - showversion - if [ ! -d ${SMAGESCRIPTSDIR} ] + if [ ! -d ${SOURCEDIR} ] then - install -d ${SMAGESCRIPTSDIR} + install -d ${SOURCEDIR} fi syncsmage2 exit 0 fi +# creates md5sums for smages to given dir +if [ "$1" == "calcmd5" ] +then + if [ $# -ge 3 ] + then + SMAGENAME="$2" + MD5DIR="$3" + source ${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" + source ${SMAGENAME} || die "download source failed" + + download_sources + exit 0 +fi if [ ! -e ${MLIBDIR}/pkgbuild_dir.sh ] then @@ -329,14 +777,23 @@ source ${SMAGENAME} || die "source failed" PKGNAME="${PNAME}-${PVER}-${CHOST%%-*}-${PBUILD}" +MD5DIR="$(dirname ${SMAGENAME})/md5" xtitle "Compiling ${PKGNAME}" -#fixes some issues with these functions +echo "Compiling ${PKGNAME}" + +# auto regen mage tree if requested +regen_mage_tree + +# download sources +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 +# 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" @@ -344,14 +801,14 @@ export MAKEOPTS="${MAKEOPTS}" || die "MAKEOPTS export failed" -#setup distcc -#distcc mus be setup *before* ccache, as ccache need to be before distcc in path +# setup distcc +# distcc mus be setup *before* ccache, as ccache need to be before distcc in path if [ "${SMAGE_USE_DISTCC}" == "true" ] then setup_distcc_environment fi -#setup ccache +# setup ccache if [ "${SMAGE_USE_CCACHE}" == "true" ] then setup_ccache_environment @@ -372,33 +829,33 @@ #read #debug end -#cleans up build if a previously one exists +# cleans up build if a previously one exists if [ -d ${BUILDDIR} ] then rm -rf ${BUILDDIR}/* || die "couldn't cleanup \$BUILDDIR." fi install -d ${BUILDDIR} || die "couldn't create \$BUILDDIR." -#cleans up srcdir if a previously unpacked one exists +# cleans up srcdir if a previously unpacked one exists if [ -d ${SRCDIR} ] then rm -rf ${SRCDIR} fi -#cleans up bindir if a previous build exists or creates a new one +# cleans up bindir if a previous build exists or creates 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 +# 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 +# cleans up timestamp if one exists if [ -f /var/tmp/timestamp ] then mage rmstamp @@ -409,7 +866,7 @@ src_install || die "src_install failed" -#compressing doc, info & man files +# compressing doc, info & man files echo -e "Compressing man-pages ..." if [ -d ${BUILDDIR}/builded/usr/share/man ] then @@ -422,19 +879,52 @@ ${MLIBDIR}/compressdoc -g -9 ${BUILDDIR}/builded/usr/share/info fi -#the new buildpkg command +# stripping all bins and libs +case ${NOSTRIP} in + true|TRUE|yes|y) + echo -e "NOSTRIP=true detected; Package will not be stripped ..." + ;; + *) + echo -e "Stripping binaries ..." + mstripbins ${BINDIR} + echo -e "Stripping libraries ..." + mstriplibs ${BINDIR} + ;; +esac + +# the new buildpkg command case ${NOPKGBUILD} in true|TRUE|yes|y) echo -e "NOPGKBUILD=true detected; Package will not be build ..." ;; - *) - ${MLIBDIR}/pkgbuild_dir.sh ${PKGNAME} ${BINDIR} || die "package-build failed" - echo -e "\nPackage ${PKGNAME} successfully builded.\n" + *) + # build serveral targets + if [ -n "${MAGE_TARGETS}" ] + then + for target in ${MAGE_TARGETS} + do + # check if an 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}-${CHOST%%-*}-${PBUILD}" \ + ${BINDIR} || die "target: ${target} package-build failed" + echo -e "\nPackage ${PNAME}-${target}-${PVER}-${CHOST%%-*}-${PBUILD} successfully builded.\n" + done + else + ${MLIBDIR}/pkgbuild_dir.sh ${PKGNAME} ${BINDIR} || die "package-build failed" + echo -e "\nPackage ${PKGNAME} successfully builded.\n" + fi ;; esac -#for sure +# for sure unset NOPKGBUILD +unset NOSTRIP xtitleclean #echo -e "\nPackage ${PKGNAME} successfully builded.\n"