Also support uncompressed control.tar files
[tools/build.git] / build-recipe-snapcraft
1 #################################################################
2 #
3 # Debian live-build specific functions.
4 #
5 # Author: Adrian Schroeter <adrian@suse.de>
6 #
7 # This file is part of build.
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License version 2 or 3 as
11 # published by the Free Software Foundation.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program (see the file COPYING); if not, write to the
20 # Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 #
23 #################################################################
24
25 recipe_setup_snapcraft() {
26     TOPDIR=/usr/src/packages
27     test "$DO_INIT_TOPDIR" = false || rm -rf "$BUILD_ROOT$TOPDIR"
28     for i in OTHER SOURCES SNAPCRAFT_ROOT ; do
29         mkdir -p "$BUILD_ROOT$TOPDIR/$i"
30     done
31     chown -R "$ABUILD_UID:$ABUILD_GID" "$BUILD_ROOT$TOPDIR"
32     if test "$MYSRCDIR" = $BUILD_ROOT/.build-srcdir ; then
33         mv "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
34     else
35         cp -p "$MYSRCDIR"/* $BUILD_ROOT$TOPDIR/SOURCES/
36     fi
37 }
38
39 recipe_prepare_snapcraft() {
40     :
41 }
42
43 createrepo_debian() {
44     local DIR=${1}
45     local ARCH=${2}
46     local DIST=${3}
47
48     if [ -z "${DIR}" -o ! -d ${DIR} -o ${DIR} = ${DIR##${BUILD_ROOT}} ] ; then
49         return
50     fi
51
52     pushd ${DIR} >/dev/null
53
54     # cleanup existing repository files
55     rm -f Packages Packages.gz Release
56     rm -fr dists
57
58     mkdir -p dists/${DIST}
59     # Suite is symlinked to Codename
60     ln -s ${DIST} dists/stable
61
62     # create Packages and Sources files
63     mkdir -p dists/${DIST}/main/binary-${ARCH}
64     mkdir -p dists/${DIST}/main/source
65     cat > ${BUILD_ROOT}/.createrepo_debian.tmp.sh <<-EOF
66         cd /.build.binaries || exit 1
67         dpkg-scanpackages -m . > dists/${DIST}/main/binary-${ARCH}/Packages
68         gzip -c9 < dists/${DIST}/main/binary-${ARCH}/Packages \
69             > dists/${DIST}/main/binary-${ARCH}/Packages.gz
70         dpkg-scansources . > dists/${DIST}/main/source/Sources
71         gzip -c9 dists/${DIST}/main/source/Sources \
72             > dists/${DIST}/main/source/Sources.gz
73         EOF
74     chroot $BUILD_ROOT su -c "sh /.createrepo_debian.tmp.sh" - root
75     local RESULT=$?
76     rm -f $BUILD_ROOT/.createrepo_debian.tmp.sh
77     [ "${RESULT}" != 0 ] && return
78
79     # create Release file
80     pushd dists/${DIST} >/dev/null
81     cat > Release <<-EOF
82         Origin: Debian
83         Label: Debian
84         Suite: stable
85         Version: 7.1
86         Codename: ${DIST}
87         Date: Sat, 15 Jun 2013 10:55:26 UTC
88         Description: Debian repository created by build-recipe-livebuild
89         Components: main
90         EOF
91     echo "SHA256:" >> Release
92     for file in main/binary-${ARCH}/Packages* ; do
93         local SUM=( $(sha256sum ${file}) )
94         local SIZE=$(stat -c '%s' ${file})
95         echo " ${SUM} ${SIZE} ${file}" >> Release
96     done
97     for file in main/source/Sources* ; do
98         local SUM=( $(sha256sum ${file}) )
99         local SIZE=$(stat -c '%s' ${file})
100         echo " ${SUM} ${SIZE} ${file}" >> Release
101     done
102     popd >/dev/null
103
104     # TODO: this is missing the signature with the private key
105
106     popd >/dev/null
107 }
108
109 # This script expects that the $BUILD_ROOT is a Debian installation with
110 # snapcraft already installed!
111 #
112 # Variables:
113 # $BUILD_ROOT the Debian chroot
114 # $TOPDIR/SOURCES includes the snapcraft sources
115 # $TOPDIR/$SNAPCRAFT_ROOT where snapcraft will be called
116 # $RECIPEFILE the name of the snapcraft.yaml config file
117
118 recipe_build_snapcraft() {
119     local ARCH=$(chroot $BUILD_ROOT su -c "dpkg-architecture -qDEB_BUILD_ARCH")
120 #    local DIST=$(chroot $BUILD_ROOT su -c "lsb_release --codename" | awk '{ print $2 }')
121     local SNAPCRAFT_ROOT="SNAPCRAFT_ROOT"
122
123 #    [ -z "${ARCH}" -o -z "${DIST}" ] && cleanup_and_exit 1
124
125     test -d $BUILD_ROOT/.build.binaries || cleanup_and_exit 1
126     if test "$DO_INIT" = true -o ! -d "$BUILD_ROOT/.build.binaries/dists" ; then
127         echo "creating repository metadata..."
128         createrepo_debian $BUILD_ROOT/.build.binaries ${ARCH} ${DIST}
129     fi
130
131     chroot $BUILD_ROOT su -c "cd $TOPDIR/SOURCES && snapcraft build" - root \
132         || cleanup_and_exit 1
133     chroot $BUILD_ROOT su -c "cd $TOPDIR/SOURCES && snapcraft snap" - root \
134         || cleanup_and_exit 1
135
136     # extract build result basenames
137     local build_results=""
138     for i in $BUILD_ROOT/$TOPDIR/SOURCES/* ; do
139         test -f "$i" || continue
140         case "${i##*/}" in
141             *.snap)
142                 build_results="${build_results}\n${i%%.snap}"
143                 ;;
144             *)
145                 ;;
146         esac
147     done
148
149     # Fail the build if no build results are found
150     if [ -z "${build_results}" ] ; then
151         echo "No live-build result found"
152         cleanup_and_exit 1
153     fi
154
155     # move created products (and their metadata files) to destination
156     local buildnum="${RELEASE:+-Build${RELEASE}}"
157     for prefix in $(echo -e ${build_results} | sort | uniq) ; do
158         for f in ${prefix}.* ; do
159             mv ${f} \
160                 $BUILD_ROOT/$TOPDIR/OTHER/${prefix##*/}${buildnum}${f#${prefix}}
161             BUILD_SUCCEEDED=true
162         done
163     done
164 }
165
166 recipe_resultdirs_snapcraft() {
167     # our results are already in OTHER
168     :
169 }
170
171 # Local Variables:
172 # mode: Shell-script
173 # End: