summaryrefslogtreecommitdiff
path: root/src/stage4/stage4.sh
blob: 6d72275661fab1977edd965cb44e2202dbcfc14a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/bash
 ##############################################################################
 #                      parabola-riscv64-bootstrap                            #
 #                                                                            #
 #    Copyright (C) 2018  Andreas Grapentin                                   #
 #                                                                            #
 #    This program is free software: you can redistribute it and/or modify    #
 #    it under the terms of the GNU General Public License as published by    #
 #    the Free Software Foundation, either version 3 of the License, or       #
 #    (at your option) any later version.                                     #
 #                                                                            #
 #    This program is distributed in the hope that it will be useful,         #
 #    but WITHOUT ANY WARRANTY; without even the implied warranty of          #
 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           #
 #    GNU General Public License for more details.                            #
 #                                                                            #
 #    You should have received a copy of the GNU General Public License       #
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.   #
 ##############################################################################

set -euo pipefail

msg "Entering Stage 4"
notify "*Bootstrap Entering Stage 4*"

# set a bunch of convenience variables
_builddir="$topbuilddir"/stage4
_srcdir="$topsrcdir"/stage4
_makepkgdir="$_builddir"/$CARCH-makepkg
_deptree="$_builddir"/DEPTREE
_groups="base base-devel"
_pkgdest="$_builddir"/packages
_logdest="$_builddir"/makepkglogs

export PKGDEST="$_pkgdest/staging"
export LOGDEST="$_logdest"

check_exe librechroot
check_exe libremakepkg

# make sure that binfmt is *enabled* for stage4 build
echo 1 > /proc/sys/fs/binfmt_misc/status

# prepare for the build
. "$_srcdir"/prepare_chroot.sh
. "$_srcdir"/prepare_deptree.sh

msg "starting $CARCH native build (phase 2)"

# keep building packages until the deptree is empty
while [ -s "$_deptree" ]; do
  # grab one without unfulfilled dependencies
  _pkgname=$(grep '\[ *\]' "$_deptree" | head -n1 | awk '{print $1}') || true
  [ -n "$_pkgname" ] || die "could not resolve dependencies. exiting."

  msg "makepkg: $_pkgname"
  msg "  remaining packages: $(cat "$_deptree" | wc -l)"

  prepare_makepkgdir
  fetch_pkgfiles $_pkgname
  import_keys

  # patch if necessary
  cp PKGBUILD{,.old}
  [ -f "$_srcdir"/patches/$_pkgname.patch ] && \
    patch -Np1 -i "$_srcdir"/patches/$_pkgname.patch
  cp PKGBUILD{,.in}
  chown -R $SUDO_USER "$_makepkgdir"/$_pkgname

  # substitute common variables
  sed -i \
      "s#@MULTILIB@#${MULTILIB:-disable}#g" \
    PKGBUILD

  # enable the target CARCH in arch array, unless it is already 'any'
  sed -i "/arch=(.*\bany\b.*)/!s/arch=([^)]*/& $CARCH/" PKGBUILD

  # scan dependencies and update deptree
  set +o pipefail
  _needs_postpone=no
  _srcinfo=$(sudo -u $SUDO_USER makepkg --config "$_builddir"/config/makepkg.conf --printsrcinfo)
  _pkgdeps=$(echo "$_srcinfo" | grep -E "	(make|check|)depends =" | awk '{print $3}')
  for _dep in $_pkgdeps; do
    _realdep=$(pacman --noconfirm -Sddw "$_dep" | grep '^Packages' | awk '{print $3}')
    _realdep="${_realdep%-*-*}"

    [ -n "$_realdep" ] || die "failed to translate dependency string '$_dep'"

    if ! grep -q "^$_realdep :" "$_deptree".FULL; then
      echo "$_realdep : [ ] # $_pkgname" >> "$_deptree".FULL
      echo "$_realdep : [ ] # $_pkgname" >> "$_deptree"
    else
      sed -i "s/^$_realdep : \[.*/&, $_pkgname/" "$_deptree".FULL
      sed -i "s/^$_realdep : \[.*/&, $_pkgname/" "$_deptree"
      sed -i "s/$_pkgname\(, $_pkgname\)*/$_pkgname/" "$_deptree".FULL
      sed -i "s/$_pkgname\(, $_pkgname\)*/$_pkgname/" "$_deptree"
    fi

    echo -n "checking for built dependency $_realdep ... "
    _depfile=$(find $_pkgdest/pool $topbuilddir/stage3/packages/ \
      -regex "^.*/$_realdep-[^-]*-[^-]*-[^-]*\.pkg\.tar\.xz\$")
    [ -n "$_depfile" ] && _have_dep=yes || _have_dep=no
    echo $_have_dep

    if [ "x$_have_dep" == "xno" ]; then
      sed -i "s/^$_pkgname : \[/& $_realdep/" "$_deptree".FULL
      sed -i "s/^$_pkgname : \[/& $_realdep/" "$_deptree"
      _needs_postpone=yes
    fi
  done
  set -o pipefail

  # missing stuff - put back to deptree and try again.
  if [ "x$_needs_postpone" == "xyes" ]; then
    popd >/dev/null
    continue
  fi

  echo -n "checking for built $_pkgname package ... "
  _pkgfile=$(find $_pkgdest/pool -regex "^.*/$_pkgname-[^-]*-[^-]*-[^-]*\.pkg\.tar\.xz\$")
  [ -n "$_pkgfile" ] && _have_pkg=yes || _have_pkg=no
  echo $_have_pkg

  if [ "x$_have_pkg" == "xno" ]; then
    # clean staging
    rm -f "$_pkgdest"/staging/*

    # build the package
    "$_builddir"/libremakepkg-$CARCH.sh -n $CHOST-stage4 || failed_build

    # release the package
    _pkgrepo=$(cat .REPO)
    for f in "$_pkgdest"/staging/*; do
      ln -s ../../../pool/$(basename "$f") "$_pkgdest"/$_pkgrepo/os/$CARCH/$(basename "$f")
      mv $f "$_pkgdest"/pool/
    done

    rm -rf /var/cache/pacman/pkg-$CARCH/*
    rm -rf "$_pkgdest"/$_pkgrepo/os/$CARCH/$_pkgrepo.{db,files}*
    repo-add -q -R "$_pkgdest"/$_pkgrepo/os/$CARCH/{$_pkgrepo.db.tar.gz,*.pkg.tar.xz}

    # install in chroot
    _pkgfile=$(find $_pkgdest/pool -regex "^.*/$_pkgname-[^-]*-[^-]*-[^-]*\.pkg\.tar\.xz\$" \
      | head -n1)
    set +o pipefail
    yes | librechroot \
        -n "$CHOST-stage4" \
        -C "$_builddir"/config/pacman.conf \
        -M "$_builddir"/config/makepkg.conf \
      run pacman -Udd /repos/pool/"$(basename "$_pkgfile")"
    yes | librechroot \
        -n "$CHOST-stage4" \
        -C "$_builddir"/config/pacman.conf \
        -M "$_builddir"/config/makepkg.conf \
      run pacman -Syyuu
    set -o pipefail
  fi

  # remove pkg from deptree
  sed -i "/^$_pkgname :/d; s/ /  /g; s/ $_pkgname / /g; s/  */ /g" "$_deptree"

  full=$(cat "$_deptree".FULL | wc -l)
  curr=$(expr $full - $(cat "$_deptree" | wc -l))
  notify -c success -u low "*$curr/$full* $_pkgname"

  popd >/dev/null
done

# unmount
umount "$_chrootdir"/native
umount "$_chrootdir"/repos

echo "all packages built."