aboutsummaryrefslogtreecommitdiff
path: root/mbuild
blob: 44800917052f375f8ef9b55325f458ea55acdf53 (plain) (blame)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env zsh
#
# This script does not have a stable API.
#
# Usage: mbuild [-b git-ref] [kernel-arch]...
#
# Builds a bunch of gitstatusd-* binaries. Without arguments builds binaries
# for all platforms. git-ref defaults to src.
#
# Before using this script you need to set up build servers and list them
# in ~/.ssh/config. There should be a Host entry for every value of `assets`
# association defined below. VMs and cloud instances work as well as physical
# machines, including localhost. As long as the machine has been set up as
# described below and you can SSH to it without password, it should work.
#
#                    ===[ Build Server Setup ]===
#
#                              Linux
#
# - Install docker.
#   $ apt install docker.io     # adjust appropriately if there is no `apt`
#   $ usermod -aG docker $USER  # not needed if going to build as root
# - Install git.
#   $ apt install git           # adjust appropriately if there is no `apt`
#
#                              macOS
#
# - Install compiler tools:
#   $ xcode-select --install
# - Install homebrew: https://brew.sh/.
#   $ bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
#
#                             FreeBSD
#
# - Install git.
#   $ pkg install git
#
#                             Windows
#
# - Disable Windows Defender (optional).
#   ps> Set-MpPreference -DisableRealtimeMonitoring $true
# - Install 64-bit and 32-bit msys2: https://www.msys2.org/wiki/MSYS2-installation/.
#   - Open each of them after installation, type `pacman -Syu --noconfirm` and close the window.
#   - Then run in powershell while having no msys2 or cygwin windows open:
#     ps> C:\msys32\autorebase.bat
#     ps> C:\msys64\autorebase.bat
# - Install 64-bit and 32-bit cygwin: https://cygwin.com/install.html.
#   - Choose to install 32-bit to c:/cygwin32 instead of the default c:/cygwin.
#   - Select these packages: binutils, cmake, gcc-core, gcc-g++, git, make, perl, wget.
#
# IMPORTANT: Install msys2 and cygwin one at a time.
#
# IMPORTANT: msys2 builder can reboot the build machine.
#
# Option 1: OpenSSH for Windows
#
# - Install OpenSSH: https://docs.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse.
#   ps> Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
#   ps> Start-Service sshd
#   ps> Set-Service -Name sshd -StartupType 'Automatic'
# - Enable publickey authentication: https://stackoverflow.com/a/50502015/1095235.
#   ps> cd $env:USERPROFILE
#   ps> mkdir .ssh
#   ps> notepad.exe .ssh/authorized_keys
#     - Paste your public key, save, close.
#   ps> icacls .ssh/authorized_keys /inheritance:r
#   ps> notepad.exe C:\ProgramData\ssh\sshd_config
#     - Comment out these two lines, save, close:
#       # Match Group administrators
#       #   AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys
#   ps> Restart-Service sshd
#
# Option 2: OpenSSH from WSL
#
# - Install WSL.
# - Install Ubuntu.
# - Install sshd.
#   $ apt install openssh-server
#   $ dpkg-reconfigure openssh-server
#   $ cat >/etc/ssh/sshd_config <<\END
#     ClientAliveInterval 60
#     AcceptEnv TERM LANG LC_*
#     PermitRootLogin no
#     AllowTcpForwarding no
#     AllowAgentForwarding no
#     AllowStreamLocalForwarding no
#     AuthenticationMethods publickey
#   END
#   service ssh --full-restart
# - Add your public ssh key to ~/.ssh/authorized_keys.
# - Make `sshd` start when Windows boots.

'emulate' '-L' 'zsh' '-o' 'no_aliases' '-o' 'err_return'
setopt no_unset extended_glob pipe_fail prompt_percent typeset_silent \
  no_prompt_subst no_prompt_bang pushd_silent warn_create_global

autoload -Uz is-at-least

if ! is-at-least 5.1 || [[ $ZSH_VERSION == 5.4.* ]]; then
  print -ru2 -- "[error] unsupported zsh version: $ZSH_VERSION"
  return 1
fi

zmodload zsh/system

local -r git_url='https://github.com/romkatv/gitstatus.git'

local -rA assets=(
  # target kernel-arch   hostname of the build machine
  cygwin_nt-10.0-i686    build-windows-x86_64
  cygwin_nt-10.0-x86_64  build-windows-x86_64
  msys_nt-10.0-i686      build-windows-x86_64
  msys_nt-10.0-x86_64    build-windows-x86_64
  darwin-x86_64          build-macos-x86_64
  freebsd-amd64          build-freebsd-amd64
  linux-aarch64          build-linux-aarch64
  linux-armv6l           build-linux-armv7l
  linux-armv7l           build-linux-armv7l
  linux-i686             build-linux-x86_64
  linux-x86_64           build-linux-x86_64
)

local -rA protocol=(
  'cygwin_nt-10.0-*' windows
  'msys_nt-10.0-*'   windows
  'darwin-*'         unix
  'freebsd-*'        unix
  'linux-*'          unix
)

local -r rootdir=${ZSH_SCRIPT:h}
local -r logs=$rootdir/logs
local -r locks=$rootdir/locks
local -r binaries=$rootdir/usrbin

function usage() {
  print -r -- 'usage: mbuild [-b REF] [KERNEL-ARCH]...'
}

local OPTARG opt git_ref=src
local -i OPTIND
while getopts ":b:h" opt; do
  case $opt in
    h) usage; return 0;;
    b) [[ -n $OPTARG ]]; git_ref=$OPTARG;;
    \?) print -ru2 -- "mbuild: invalid option: -$OPTARG"           ; return 1;;
    :)  print -ru2 -- "mbuild: missing required argument: -$OPTARG"; return 1;;
    *)  print -ru2 -- "mbuild: invalid option: -$opt"              ; return 1;;
  esac
done

shift $((OPTIND - 1))

(( $# )) || set -- ${(ko)assets}
set -- ${(u)@}

local platform
for platform; do
  if (( ! $+assets[$platform] )); then
    print -ru2 -- "mbuild: invalid platform: $platform"
    return 1
  fi
done

local build='
  rm -rf gitstatus
  git clone --recursive --shallow-submodules --depth=1 -b '$git_ref' '$git_url'
  cd gitstatus
  if command -v zsh >/dev/null 2>&1; then
    sh=zsh
  elif command -v dash >/dev/null 2>&1; then
    sh=dash
  elif command -v ash >/dev/null 2>&1; then
    sh=ash
  else
    sh=sh
  fi
  $sh -x ./build -m '

function build-unix() {
  local intro flags=(-sw)
  case $2 in
    darwin-*) intro='PATH="/usr/local/bin:$PATH"';;
    linux-*)  flags+=(-d docker);;
  esac
  ssh $1 -- /bin/sh -uex <<<"
    $intro
    cd /tmp
    $build ${2##*-} ${(j: :)${(@q)flags}}"
  scp $1:/tmp/gitstatus/usrbin/gitstatusd-$2 $binaries/
}

function build-windows() {
  local shell=$(ssh $1 'echo $0')
  if [[ $shell == '$0'* ]]; then
    local c='c:'
  else
    local c='/mnt/c'
  fi

  local tmp env bin intro flags=(-w)
  case $2 in
    cygwin_nt-10.0-i686)   bin='cygwin32/bin'  ;|
    cygwin_nt-10.0-x86_64) bin='cygwin64/bin'  ;|
    msys_nt-10.0-i686)     bin='msys32/usr/bin';|
    msys_nt-10.0-x86_64)   bin='msys64/usr/bin';|
    cygwin_nt-10.0-*)
      tmp='/cygdrive/c/tmp'
    ;|
    msys_nt-10.0-*)
      flags+=(-s)
      tmp='/c/tmp'
      env='MSYSTEM=MSYS'
      intro='pacman -Syu --noconfirm; pacman -S --needed --noconfirm git; '
      intro+='PATH="$PATH:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl"'
      while true; do
        # TODO: run autorebase only when getting an error that can be fixed by autorebasing.
        break
        local out
        out="$(ssh $1 cmd.exe "$c/${bin%%/*}/autorebase.bat" 2>&1)"
        [[ $out == *"The following DLLs couldn't be rebased"* ]] || break
        # Reboot to get rid of whatever is using those DLLs.
        ssh $1 powershell.exe <<<'Restart-Computer -Force' || true
        sleep 30
        while ! ssh $1 <<<''; do sleep 5; done
      done
      () {
        while true; do
          local -i fd
          exec {fd}< <(
            ssh $1 $c/$bin/env.exe $env c:/$bin/bash.exe -l 2>&1 <<<"
              pacman -Syu --noconfirm
              exit")
          {
            local line
            while true; do
              IFS= read -u $fd -r line || return 0
              if [[ $line == *"warning: terminate MSYS2"* ]]; then
                # At this point the machine is hosed. Rogue process with corrupted name
                # is eating all CPU. The top SSH connection won't terminate on its own.
                ssh $1 powershell.exe <<<'Restart-Computer -Force' || true
                sleep 30
                while ! ssh $1 <<<''; do sleep 5; done
                break
              fi
            done
          } always {
            exec {fd}<&-
            kill -- -$sysparams[procsubstpid] 2>/dev/null || true
          }
        done
      } "$@"
    ;|
  esac

  ssh $1 $c/$bin/env.exe $env c:/$bin/bash.exe -l <<<"
    set -uex
    $intro
    mkdir -p -- $tmp
    cd -- $tmp
    $build ${2##*-} ${(j: :)${(@q)flags}}
    exit"
  scp $1:$c/tmp/gitstatus/usrbin/gitstatusd-$2 $binaries/
  chmod +x $binaries/gitstatusd-$2
}

function build() (
  setopt xtrace
  local platform=$1
  local machine=$assets[$platform]
  print -n >>$locks/$machine
  zsystem flock $locks/$machine
  build-${protocol[(k)$platform]} $machine $platform
  local tmp=gitstatusd-$platform.tmp.$$.tar.gz
  ( cd -q -- $binaries; GZIP=-9 tar -czf $tmp gitstatusd-$platform )
  mv -f -- $binaries/$tmp $binaries/gitstatusd-$platform.tar.gz
)

function mbuild() {
  local platform pid pids=()
  for platform; do
    build $platform &>$logs/$platform &
    print -r -- "starting build for $platform on $assets[$platform] (pid $!)"
    pids+=($platform $!)
  done
  for platform pid in $pids; do
    print -rn -- "$platform => "
    if wait $pid; then
      print -r -- "ok"
    else
      print -r -- "error $?"
      print -r -- "---------------------"
      >&2 cat $logs/$platform
      return 1
    fi
  done
}

# Copied from https://github.com/romkatv/run-process-tree.
function run-process-tree() {
  zmodload zsh/parameter zsh/param/private || return
  local -P opt=(${(kv)options[@]})         || return
  local -P pat=(${patchars[@]})            || return
  local -P dis_pat=(${dis_patchars[@]})    || return
  emulate -L zsh -o err_return             || return
  setopt monitor traps_async pipe_fail no_unset
  zmodload zsh/system

  if (( $# == 0 )); then
    print -ru2 -- 'usage: run-process-tree command [arg]...'
    return 1
  fi

  local -P stdout REPLY
  exec {stdout}>&1
  {
    {
      local -Pi pipe
      local -P gid=$sysparams[pid]
      local -P sig=(ABRT EXIT HUP ILL INT PIPE QUIT TERM ZERR)
      local -P trap=(trap "trap - $sig; kill -- -$sysparams[pid]" $sig)

      exec {pipe}>&1 1>&$stdout
      $trap

      {
        $trap
        while sleep 1 && print -u $pipe .; do; done
      } 2>/dev/null &
      local -Pi watchdog=$!

      {
        trap - ZERR
        exec {pipe}>&-
        enable -p -- $pat
        disable -p -- $dis_pat
        options=($opt zle off monitor off)
        "$@"
      } &
      local -Pi ret
      wait $! || ret=$?

      trap "exit $ret" TERM
      kill $watchdog
      wait $watchdog
      return ret
    } | while read; do; done || return
  } always {
    exec {stdout}>&-
  }
}

mkdir -p -- $logs $locks $binaries
run-process-tree mbuild $@