summaryrefslogtreecommitdiff
path: root/src/stage1/stage1.sh
blob: 2bf3bc5d879d133a7cd9ab0dfd0e690605f7f5cd (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
#!/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/>.   #
 ##############################################################################

check_cross_toolchain() {
  echo -n "checking for $CHOST binutils ... "
  local have_binutils=yes
  type -p "$CHOST-ar" >/dev/null || have_binutils=no
  echo $have_binutils
  [ "x$have_binutils" == "xyes" ] || return 1

  echo -n "checking for $CHOST gcc ... "
  local have_gcc=yes
  type -p "$CHOST-g++" >/dev/null || have_gcc=no
  echo $have_gcc
  [ "x$have_gcc" == "xyes" ] || return 1

  local sysroot
  sysroot=$("$CHOST-gcc" --print-sysroot) || die "failed to produce $CHOST-gcc sysroot"

  echo -n "checking for $CHOST linux api headers ... "
  local have_headers=yes
  [ -e "$sysroot"/include/linux/kernel.h ] || have_headers=no
  echo $have_headers
  [ "x$have_headers" == "xyes" ] || return 1

  echo -n "checking for $CHOST glibc ... "
  local have_glibc=yes
  [ -e "$sysroot"/usr/lib/libc.so.6 ] || have_glibc=no
  echo $have_glibc
  [ "x$have_glibc" == "xyes" ] || return 1
}

stage1_makepkg() {
  # produce pkgfiles
  for f in "$SRCDIR"/toolchain-pkgbuilds/$pkg/*.in; do
    sed "s#@CHOST@#$CHOST#g; \
         s#@CARCH@#$CARCH#g; \
         s#@LINUX_ARCH@#$LINUX_ARCH#g; \
         s#@GCC_MARCH@#${GCC_MARCH:-}#g; \
         s#@GCC_MABI@#${GCC_MABI:-}#g; \
         s#@MULTILIB@#${MULTILIB:-disable}#g; \
         s#@GCC_32_MARCH@#${GCC_32_MARCH:-}#g; \
         s#@GCC_32_MABI@#${GCC_32_MABI:-}#g; \
         s#@CARCH32@#${CARCH32:-}#g; \
         s#@CHOST32@#${CHOST32:-}#g" \
      "$f" > ./"$(basename "${f%.in}")"
  done

  import_keys || return

  runas "$SUDO_USER" makepkg -LC --config "$BUILDDIR"/makepkg.conf || return
}

stage1() {
  msg -n "Entering Stage 1"

  export BUILDDIR="$TOPBUILDDIR"/stage1
  export SRCDIR="$TOPSRCDIR"/stage1
  export MAKEPKGDIR="$BUILDDIR"
  export PKGDEST="$BUILDDIR"/packages
  export LOGDEST="$BUILDDIR"/makepkglogs

  check_cross_toolchain && return

  check_exe gpg makepkg pacman sed || return

  # create required directories
  mkdir -p "$LOGDEST" "$PKGDEST" "$SRCDEST"
  chown "$SUDO_USER" "$LOGDEST" "$PKGDEST" "$SRCDEST"

  # create a sane makepkg.conf
  cat "$SRCDIR"/makepkg.conf.in > "$BUILDDIR"/makepkg.conf
  cat >> "$BUILDDIR"/makepkg.conf << EOF
MAKEFLAGS="-j$(($(nproc) + 1))"
EOF

  # build and install the toolchain packages
  for pkg in binutils linux-libre-api-headers gcc-bootstrap glibc gcc; do
    msg "makepkg: $CHOST-$pkg"

    if ! check_pkgfile "$PKGDEST" "$CHOST-$pkg"; then
      prepare_makepkgdir "$MAKEPKGDIR/$CHOST-$pkg" || return

      local res=0
      stage1_makepkg "$pkg" 2>&1 | tee .MAKEPKGLOG
      res="${PIPESTATUS[0]}"

      popd >/dev/null || return

      if [ "$res" -ne 0 ]; then
        notify -c error "$CHOST-$pkg" -h string:document:"$(readlink -f .MAKEPKGLOG)"
        return "$res"
      fi

      notify -c success -u low "$CHOST-$pkg"
    fi

    # install the package
    # shellcheck disable=SC2010
    pkgfile=$(ls -t "$PKGDEST" | grep -P "^$CHOST-$pkg(-[^-]*){3}\\.pkg" | head -n1)
    yes | pacman -U "$PKGDEST/$pkgfile"
  done

  # final sanity check
  check_cross_toolchain || die -e "$ERROR_MISSING" "toolchain build incomplete"
}