posaune
[projects.git] / tools / make / mconfigure
index a5e9297..e5f11b3 100755 (executable)
 set -e
 
 function echo_usage {
-       echo "usage: configure <package> [options]"
+       echo "usage: mconfigure [options] <package>"
        echo "  -a <arch>"
+       echo "  -c : configure"
+       echo "  -C <cross environment>"
+       echo "  -d : compile"
+       echo "  -p : pack default: do all"
+       echo "  -v : set version"
+       echo "  -V : version set by project"
+       echo "  -o : support old Debian 7 format"
+}
+
+function cmake_check {
+       pushd ../build >/dev/null
+               # do checks
+               if [ -e debian/$paket.build ]
+               then
+                       echo "cmake is not compatible with other builds. Remove $paket.build"
+                       exit 1
+               fi
+               if [ -e debian/$paket.prebuild ]
+               then
+                       echo "cmake is not compatible with other builds. Remove $paket.prebuild"
+                       exit 2
+               fi
+               
+               if [ -n "$cross" ]
+               then
+                       # inject cross toolset
+                       echo "set (CMAKE_MODULE_PATH $cross)" > CMakeLists.txt
+                       echo "include(toolset)" >> CMakeLists.txt
+                       cat debian/$paket.cmake >> CMakeLists.txt
+               else
+                       cp debian/$paket.cmake CMakeLists.txt
+               fi
+
+               echo "make" > debian/$paket.build
+               chmod 755 debian/$paket.build
+               cmake .
+       popd >/dev/null
+}
+function check_unversioned {
+       # has to be called in the projects base directory
+       # result: "M" in stdout or nothing
+       set -e
+       
+       # check if build is running in a sandbox
+       if ! [ -f .cm.ignore ]
+       then
+               # we create one
+               echo ".cm.ignore" > .cm.ignore
+               echo "debian.*" >> .cm.ignore
+               echo "up.*" >> .cm.ignore
+       fi
+       
+       project=$(pwd)
+       project=${project##*/}
+       pushd .. >/dev/null
+               mCM $project -b -an > /dev/null
+               wc=$(wc $project.batch)
+               rm $project.batch
+               if [ "${wc:0:5}" != "0 0 0" ]
+               then
+                       echo "M"
+               fi
+       popd >/dev/null
 }
 
 function set_build {
+       set -e
        if [ -e .svn ]
        then
+               version="0.0"
                build=$(svnversion)
+               build=${build/:/-}
+               build="$build$(check_unversioned)"
        elif [ -e .git ]
        then
-               build=$(git describe --tags 2>/dev/null)
-               test -n "$build" || build="TOP"
-               changes=$(git status -s |grep "^ *M") || true
-               test -z "$changes" || build="${build}M"
+               tag=$(git describe --tags --match "${paket}_*" 2>/dev/null) || true
+               if test -z "$tag"
+               then
+                       # try generic "v_" tag
+                       tag=$(git describe --tags --match "v_*" 2>/dev/null) || true
+               fi
+               if test -z "$tag"
+               then
+                       tag="0.0-0TOP"
+               fi
+               changes=$(check_unversioned)
+               tag="$tag$changes"
+               version=${tag##*_}
+               version=${version%%-*}
+               build=${tag##*-}
+       else
+               echo "No configuration system found. Cannot determine version."
+               exit 1
        fi
 }
 
-# to build a package you need ...
-# <package>.cp (optional): copy step for package production
-# <package>.cpp.sh (optional): commands to setup the C++ compile environment
+function check_version {
+       set -e
+       
+       vline=$(head -1 debian/$paket.changelog)
+       vline=${vline%-*}
+       clversion=${vline#*(}
+       if [ $version = "0.0" ]
+       then
+               version=$clversion
+               return
+       fi
 
-if [ $# -lt 1 ]
-then
-       echo_usage
-       exit 1
-fi
+       # do no check on a modified git repos
+       if [ "${build: -1}" == "M" ]
+       then
+               return
+       fi
 
-paket=$1
-shift
-ARCH=""
-cpp_build=0
-delete_src=1
-
-set_build
-echo "building $paket with build $build"
-
-while getopts ":a" opt; do
-       case $opt in
-               a) ARCH=$OPTARG
-                       ;;
-               \?) echo "Invalid option: -$OPTARG"
-                       echo_usage
-                       exit 1
-                       ;;
-               :) echo "Option -$OPTARG requires an argument."
-                       echo_usage
-                       exit 1
-                       ;;
-       esac
-done
+       if [ "$clversion" != $version ]
+       then
+               echo "version mismatch: git: $version, changelog: $clversion"
+               exit 1
+       fi
+}
+
+##############################################################################
+# Main program
+##############################################################################
 
-# clean dirs and check out
-if [ -d build -a $delete_src -eq 1 ]
+if [ ! -d debian ]
 then
-       rm -rf build
+       echo "run mconfigure in the project base directory with a debian directory in it!"
+       exit 2
 fi
 
-mkdir -p build
-pushd build >/dev/null
-
-ln -sf /usr/share/mbuild/makefile .
+configure=0
+compile=0
+pack=0
+while [ $OPTIND -le "$#" ]
+do
+       if getopts "a:cC:dpov:V" opt
+       then
+               case $opt in
+                       a) ARCH=$OPTARG
+                               ;;
+                       c) configure=1
+                               ;;
+                       d)      compile=1
+                               ;;
+                       C) cross=$OPTARG
+                               ;;
+                       p)      pack=1
+                               ;;
+                       o)      oldpack="-Zgzip"
+                               ;;
+                       v)      optversion=$OPTARG
+                               ;;
+                       V)      optversion="0.0"
+                               ;;
+                       \?) echo "Invalid option: -$OPTARG"
+                               echo_usage
+                               exit 1
+                               ;;
+                       :) echo "Option -$OPTARG requires an argument."
+                               echo_usage
+                               exit 1
+                               ;;
+               esac
+       else
+               paket="${!OPTIND}"
+               eval OPTIND=OPTIND+1
+       fi
+done
 
-if [ -e $paket ]
+if [ -z "$paket" ]
 then
-       rm -rf $paket
+       echo_usage
+       exit 1
 fi
-rm *.stamp || true
-rm make.pre || true
 
-# load util functions for C/C++ - build
-if [ -f $paket.cpp.sh ]
+if [ -n "$ARCH" -a -n "$cross" ]
 then
-       cpp_build=1
-       . projects/tools/make/c_configure.sh
-       . $paket.cpp.sh
+       echo "invalid options: supply -a for multiarch or -C for explicit cross compile environment"
+       exit 1
 fi
 
-# copy package control
-control=$(find -L src -name $paket.control)
-build_number=${build#*_}
-if [ -z "$control" ]
+# if nothing is selected, select all
+if [ $configure -eq 0 -a $compile -eq 0 -a $pack -eq 0 ]
 then
-       echo "warning: control file not found"
-       echo "  this file is necessary for any package production."
-else   
-       sed "s/%BUILD%/$build_number/" $control |sed "s/%ARCH%/$ARCH/" >$paket.control
-       echo "$paket.control written."
-
-       # extract version
-       version=$(grep Version $paket.control |sed "s/Version: //")
+       configure=1
+       compile=1
+       pack=1
 fi
 
-# check for copy file
-copy=$(find -L src -name $paket.cp)
-if [ -n "$copy" ]
+if [ $configure -eq 1 ]
 then
-       ln -sf $copy .
-else
-       echo "warning: file $paket.cp missing or not executable:"
-       echo "  this file is necessary for any package production."
-fi
+       # delete changelog and control
+       rm debian/changelog debian/control debian/README.debian debian/copyright 2>/dev/null || true
+       
+       # mconfigure builds 2 environment files: setenv.sh + rules.pre
+       echo "" > debian/rules.pre
+       if [ -e debian/setenv.sh ]; then rm debian/setenv.sh; fi
+       
+       if [ -n "$cross" ]
+       then
+               if [ ${cross:0:1} != "/" ]
+               then
+                       cross="/opt/cross/$cross"
+                       #export $cross
+               fi
+               if [ -f "$cross/setenv.sh" ]
+               then
+                       . "$cross/setenv.sh"
+                       cp $cross/setenv.sh debian/setenv.sh
+               fi
+               if [ -z "$ARCH" ]
+               then
+                       # determine architecture
+                       GNU_ARCH=${cross%-*} # assumed format: DEB_HOST_GNU_TYPE-g++version, e.g. arm-linux-gnueabihf-4.9
+                       GNU_ARCH=${GNU_ARCH##*/}
+                       arch_opt="-t $GNU_ARCH"
+               fi
+       fi
 
-# check for installation scripts
-files=$(find src -name $paket.preinst)
-files="$files $(find -L src -name $paket.postinst)"
-files="$files $(find -L src -name $paket.prerm)"
-files="$files $(find -L src -name $paket.postrm)"
-for file in $files
-do
-       ln -sf $file .
-done
+       if [ -n "$ARCH" ]
+       then
+               arch_opt="-a $ARCH"
+       fi
+       dpkg-architecture $arch_opt >> debian/setenv.sh
+       
+       echo "paket=$paket" >> debian/setenv.sh
+       echo "oldpack=\"$oldpack\"" >> debian/setenv.sh
 
-# create generic make.pre, if not existing
-if [ ! -f make.pre ]
+       set_build
+       if [ -z "$optversion" ]
+       then
+               check_version
+       else
+               version=$optversion
+       fi
+       echo "version=$version" >> debian/setenv.sh
+       echo "build=$build" >> debian/setenv.sh
+
+       # get repository name
+#      tmp=$(pwd)
+#      pwd=${tmp##*/}
+#      pwd=${pwd,,*}
+#      echo "pwd=$pwd" >> debian/rules.pre
+
+       echo "building $paket with build $version-$build"
+
+       pushd debian >/dev/null
+       # ./debian -------------------------------------
+
+       ln -sf /usr/share/mbuild/rules .
+
+       if [ -e tmp ]
+       then
+               rm -rf tmp
+       fi
+
+       # check for pre/post installation scripts
+       if [ -f $paket.preinst ]
+       then
+               echo "add_inst_tgt += debian/tmp/DEBIAN/preinst" >> rules.pre
+       fi
+       if [ -f $paket.postinst ]
+       then
+                   echo "add_inst_tgt += debian/tmp/DEBIAN/postinst" >> rules.pre
+       fi
+       if [ -f $paket.prerm ]
+       then
+                   echo "add_inst_tgt += debian/tmp/DEBIAN/prerm" >> rules.pre
+       fi
+       if [ -f $paket.postrm ]
+       then
+                   echo "add_inst_tgt += debian/tmp/DEBIAN/postrm" >> rules.pre
+       fi
+       
+       # check for README.debian
+       if [ -f $paket.README.debian ]
+       then
+                   cp $paket.README.debian README.debian
+       fi
+       
+       mkdir -p tmp/DEBIAN
+
+       # add export to setenv.sh
+       sed -i "s/^/export /" setenv.sh
+
+       popd >/dev/null
+       # ./. ---------------------------------------------
+
+       # copy package control
+       if [ -f debian/$paket.control ]
+       then
+               # proceed
+               echo
+       else
+               echo "warning: control file not found"
+               echo "  this file is necessary for any package production."
+       fi
+       
+       # build prepare
+       if [ -x debian/$paket.prepare ]
+       then
+               . debian/setenv.sh
+               debian/$paket.prepare
+       fi
+       # .build or .cmake
+       if [ -x debian/$paket.build -o -e debian/$paket.cmake ]
+       then
+               if [ -e ../build ]
+               then
+                       rm -rf ../build
+                       mkdir ../build
+               fi
+               if grep -- "-prepare" debian/$paket.build >/dev/null
+               then                    
+                       echo "prepare in <paket>.build no longer supported. Use <paket>.prebuild"
+                       exit 2
+               fi
+               # sync build dir
+               . debian/setenv.sh
+               debian/rules sync
+       fi
+       # .build
+       if [ -x debian/$paket.build ]
+       then
+               if [ -x debian/$paket.prebuild ]
+               then
+                       pushd ../build >/dev/null
+                               . debian/setenv.sh
+                               debian/$paket.prebuild
+                       popd >/dev/null
+               fi
+       fi
+       # .cmake
+       if [ -e debian/$paket.cmake ]
+       then
+               cmake_check
+       fi
+       
+       # pack prepare
+       if [ -f debian/$paket.cp -a -f debian/$paket.control ]
+       then
+               echo "PACK=binary" >> debian/rules.pre
+               if grep "^Architecture: *all" debian/$paket.control >/dev/null
+               then
+                       echo "arch = all" >> debian/rules.pre
+                       echo 'BINARY_INDEP = copy ../$(paket)_$(version)-$(build)_all.deb' >> debian/rules.pre
+               else
+                       echo 'arch = ${DEB_HOST_ARCH}' >> debian/rules.pre
+                       echo "BINARY_ARCH = copy ../${paket}_$version-${build}_"'$(arch).deb' >> debian/rules.pre
+               fi
+               if ! [ -f $paket.changelog ]
+               then
+                       echo "changelog_source = debian/default.changelog" >> debian/rules.pre
+                       if [ -f debian/default.changelog ]
+                       then
+                               rm debian/default.changelog
+                       fi
+               fi
+       elif [ -f debian/$paket.cp ]
+       then
+               echo "PACK=zip" >> debian/rules.pre
+       else
+               echo "PACK=version" >> debian/rules.pre
+       fi
+fi
+
+if [ $compile -eq 1 ]
 then
-       cat  >>make.pre <<MAKE_PRE
-# mBuild make.pre script (auto generated)
-project = $paket
-COPY = ./$paket.cp
-MAKE_PRE
-       if [ -n "$ARCH" ]
+       if [ -x debian/$paket.build -o -e debian/$paket.cmake ]
        then
-               echo "arch = $ARCH" >>make.pre
-               echo "_arch = _${ARCH}" >>make.pre
+               # build package 
+               . debian/setenv.sh
+               debian/rules build
+       else
+               echo "skipping build step ..."
        fi
-       echo "make.pre written."
 fi
 
-# append version to make.pre
-echo "version = $version" >> make.pre
-echo "build = $build" >> make.pre
+if [ $pack -eq 1 ]
+then
+       # build package
+       . debian/setenv.sh
+       debian/rules pack
+fi