summaryrefslogtreecommitdiff
path: root/src/stage2/stage2.sh
blob: 9db683d56a90c1105d65db665d0ec54c002d2626 (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
#!/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/stage2/makepkg.sh
. "$TOPSRCDIR"/stage2/makepkg.sh
# shellcheck source=src/stage2/chroot.sh
. "$TOPSRCDIR"/stage2/chroot.sh

stage2_makepkg() {
  package_fetch_upstream_pkgfiles "$1" || return
  package_import_keys "$1" || return
  package_patch -r "$1" || return

  # substitute common variables
  sed "s#@CARCH@#$CARCH#g; \
       s#@CHOST@#$CHOST#g; \
       s#@PLATFORM_CFLAGS@#${PLATFORM_CFLAGS[*]}#g; \
       s#@LINUX_ARCH@#$LINUX_ARCH#g; \
       s#@GCC_CONFIG_FLAGS@#${GCC_CONFIG_FLAGS[*]}#g; \
       s#@MULTILIB@#${MULTILIB:-disable}#g; \
       s#@CARCH32@#${CARCH32:-}#g; \
       s#@CHOST32@#${CHOST32:-}#g; \
       s#@PLATFORM32_CFLAGS@#${PLATFORM32_CFLAGS[*]}#g; \
       s#@BUILDHOST@#$(gcc -dumpmachine)#g; \
       s#@SYSROOT@#$SYSROOT#g;" \
    PKGBUILD.in > PKGBUILD

  package_enable_arch "$CARCH"

  # check built dependencies
  local dep
  for dep in $(srcinfo_builddeps -nm); do
    deptree_check_depend "$1" "$dep" || return
  done
  for dep in $(srcinfo_rundeps "$1"); 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

  # build the package
  runas "$SUDO_USER" \
  "$BUILDDIR"/makepkg.sh -fLC --config "$BUILDDIR"/makepkg.conf \
    --nocheck --nodeps --nobuild --noconfirm || return

  if [ "x${REGEN_CONFIG_FRAGMENTS:-no}" == "xyes" ]; then
    url="https://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain"
    find src -iname config.sub -print -exec curl "$url;f=config.sub;hb=HEAD" -o {} \; || return
  fi

  runas "$SUDO_USER" \
  "$BUILDDIR"/makepkg.sh -efL --config "$BUILDDIR"/makepkg.conf \
    --nocheck --nodeps --noprepare --noconfirm || return
}

stage2_package_build() {
  local pkgarch
  pkgarch=$(pkgarch "$1") || return

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

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

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

stage2_package_install() {
  # install in chroot
  yes | pacman --noscriptlet --force --config "$CHROOTDIR"/etc/pacman.conf \
    -r "$CHROOTDIR" -Syydd "$1" || return
}

stage2() {
  msg -n "Entering Stage 2"

  local groups=(filesystem base-devel)

  local sysroot
  sysroot="$("$CHOST"-gcc --print-sysroot)"

  export BUILDDIR="$TOPBUILDDIR"/stage2
  export SRCDIR="$TOPSRCDIR"/stage2
  export CHROOTDIR="$BUILDDIR"/$CARCH-root
  export MAKEPKGDIR="$BUILDDIR"/$CARCH-makepkg
  export DEPTREE="$BUILDDIR"/DEPTREE
  export SYSROOT="$sysroot"
  export PKGDEST="$BUILDDIR"/packages/$CARCH
  export PKGPOOL="$PKGDEST"
  export LOGDEST="$BUILDDIR"/makepkglogs
  export DEPPATH=("$PKGDEST")

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

  binfmt_disable

  prepare_deptree "${groups[@]}" || die -e "$ERROR_BUILDFAIL" "failed to prepare DEPTREE"
  echo "remaining pkges: $(wc -l < "$DEPTREE") / $(wc -l < "$DEPTREE".FULL)"
  [ -s "$DEPTREE" ] || return 0

  prepare_stage2_makepkg || die -e "$ERROR_BUILDFAIL" "failed to prepare $CARCH makepkg"
  prepare_stage2_chroot || die -e "$ERROR_BUILDFAIL" "failed to prepare $CARCH chroot"

  # pull in various tools required to run the scripts or build the packages
  check_exe -r arch-meson asp awk bsdtar git gperf help2man pacman sed svn tar tclsh

  # build packages from deptree
  packages_build_all stage2_package_build stage2_package_install || return

  # cleanup
  umount_stage2_chrootdir
}