summaryrefslogtreecommitdiff
path: root/src/stage3/stage3.sh
blob: 342862b6403e347642fbd238815a0f4671ab044f (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
#!/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/>.   #
 ##############################################################################

# shellcheck source=src/stage3/makepkg.sh
. "$TOPSRCDIR"/stage3/makepkg.sh
# shellcheck source=src/stage3/chroot.sh
. "$TOPSRCDIR"/stage3/chroot.sh

stage3_makepkg() {
  local pkgname="${1%-decross}"
  local prefix=()
  [ "x$1" != "x$pkgname" ] && prefix=(-p decross)

  package_fetch_upstream_pkgfiles "$pkgname" || return
  package_import_keys "$pkgname" || return
  package_patch "${prefix[@]}" "$pkgname" || return

  # substitute common variables
  sed "s#@MULTILIB@#${MULTILIB:-disable}#g; \
       s#@PLATFORM_CFLAGS@#${PLATFORM_CFLAGS[*]}#g; \
       s#@CHOST32@#${CHOST32}#g; \
       s#@PLATFORM32_CFLAGS@#${PLATFORM32_CFLAGS[*]}#g; \
       s#@GCC_CONFIG_FLAGS@#${GCC_CONFIG_FLAGS[*]}#g" \
    PKGBUILD.in > PKGBUILD

  # prepare the pkgbuild
  package_enable_arch "$CARCH"
  echo "checkdepends=()" >> PKGBUILD

  # check built dependencies
  local dep
  for dep in $(srcinfo_builddeps -n); do
    deptree_check_depend "$1" "$dep" || return
  done
  for dep in $(srcinfo_rundeps "$pkgname"); do
    deptree_check_depend "$1" "$dep" || return
  done

  # postpone build if necessary
  deptree_is_satisfyable "$1" || return 0

  # don't rebuild if already exists
  check_pkgfile "$PKGDEST" "$1" && return

  if [ "x$1" != "x$pkgname" ]; then
    # a bit of magic for -decross builds
    PKGDEST=. "$BUILDDIR/libremakepkg.sh" -n "$CHOST"-stage3 || return
    local pkgfiles pkgfile
    pkgfiles=("$pkgname"-*.pkg.tar.xz); pkgfile="${pkgfiles[0]}"
    mv -v "$pkgfile" "$PKGDEST/${pkgfile/$pkgname/$1}"
  else
    # regular build otherwise
    "$BUILDDIR/libremakepkg.sh" -n "$CHOST"-stage3 || return
  fi
}

stage3_package_build() {
  local pkgarch
  pkgarch=$(pkgarch "${1%-decross}") || return

  if [ "x$pkgarch" == "xany" ] || [ "x$1" == "xca-certificates-mozilla" ]; then
    package_reuse_upstream "$1" || return
  else
    stage3_makepkg "$1" || return
  fi

  # postpone on unmet dependencies
  deptree_is_satisfyable "$1" || return 0

  # update repo
  rm -rf /var/cache/pacman/pkg-"$CARCH"/*
  rm -rf "$PKGDEST"/native.{db,files}*
  repo-add -q -R "$PKGDEST"/{native.db.tar.gz,*.pkg.tar.xz}
}

stage3_package_install() {
  local esc pkgfile
  esc=$(printf '%s\n' "$1" | sed 's:[][\/.^$*]:\\&:g')
  pkgfile=$(find "$PKGPOOL" -regex "^.*/$esc-[^-]*-[^-]*-[^-]*\\.pkg\\.tar\\.xz\$" | head -n1)
  [ -n "$pkgfile" ] || { error "$1: pkgfile not found"; return "$ERROR_MISSING"; }

  yes | librechroot \
      -n "$CHOST-stage3" \
      -C "$BUILDDIR"/config/pacman.conf \
      -M "$BUILDDIR"/config/makepkg.conf \
    run pacman -U /repos/native/"$CARCH"/"$(basename "$pkgfile")" || return
  yes | librechroot \
      -n "$CHOST-stage3" \
      -C "$BUILDDIR"/config/pacman.conf \
      -M "$BUILDDIR"/config/makepkg.conf \
    run pacman -Syyuu || return
}

stage3() {
  msg -n "Entering Stage 3"

  local groups=(base-devel)
  local decross=(bash make)

  export BUILDDIR="$TOPBUILDDIR"/stage3
  export SRCDIR="$TOPSRCDIR"/stage3
  export MAKEPKGDIR="$BUILDDIR"/$CARCH-makepkg
  export DEPTREE="$BUILDDIR"/DEPTREE
  export PKGDEST="$BUILDDIR"/packages/$CARCH
  export PKGPOOL="$PKGDEST"
  export LOGDEST="$BUILDDIR"/makepkglogs
  export DEPPATH=("$PKGDEST" "${PKGDEST/stage3/stage2}")

  mkdir -p "$PKGDEST" "$PKGPOOL" "$LOGDEST"
  chown "$SUDO_USER" "$PKGDEST" "$PKGPOOL" "$LOGDEST"

  binfmt_enable

  prepare_deptree "${groups[@]}" || die -e "$ERROR_BUILDFAIL" "failed to prepare DEPTREE"
  local pkg
  for pkg in "${decross[@]}"; do
    deptree_add_entry "$pkg-decross"
  done
  echo "remaining pkges: $(wc -l < "$DEPTREE") / $(wc -l < "$DEPTREE".FULL)"
  [ -s "$DEPTREE" ] || return 0

  prepare_stage3_makepkg || die -e "$ERROR_BUILDFAIL" "failed to prepare $CARCH makepkg"
  prepare_stage3_chroot || die -e "$ERROR_BUILDFAIL" "failed to prepare $CARCH chroot"

  check_exe -r librechroot libremakepkg

  for pkg in "${decross[@]}"; do
    package_build stage3_package_build stage3_package_install "$pkg-decross" || return
  done

  # build packages from deptree
  packages_build_all stage3_package_build stage3_package_install || return

  # cleanup
  umount_stage3_chrootdir
}