OILS / build / deps.sh View on Github | oilshell.org

1152 lines, 592 significant
1#!/usr/bin/env bash
2#
3# Script for contributors to build dev dependencies -- packaged as cross-distro
4# "wedges". Tested in the Soil CI.
5#
6# Usage:
7# build/deps.sh <function name>
8#
9# Examples:
10# build/deps.sh fetch
11# build/deps.sh install-wedges # for both Python and C++
12# build/deps.sh rm-oils-crap # rm -r -f /wedge ~/wedge to start over
13#
14# TODO: Do we need something faster, just python2, re2c, and cmark?
15#
16# - build/deps.sh fetch-py
17# - build/deps.sh install-wedges-py
18#
19# TODO: Can we make most of them non-root deps? This requires rebuilding
20# containers, which requires podman.
21#
22# rm -r -f ~/wedge # would be better
23
24set -o nounset
25set -o pipefail
26set -o errexit
27
28REPO_ROOT=$(cd "$(dirname $0)/.."; pwd)
29
30source build/dev-shell.sh # python3 in PATH, PY3_LIBS_VERSION
31source deps/from-apt.sh # PY3_BUILD_DEPS
32#source deps/podman.sh
33source devtools/run-task.sh # run-task
34source test/tsv-lib.sh # tsv-concat
35source web/table/html.sh # table-sort-{begin,end}
36
37# Also in build/dev-shell.sh
38USER_WEDGE_DIR=~/wedge/oils-for-unix.org
39ROOT_WEDGE_DIR=/wedge/oils-for-unix.org
40
41readonly DEPS_SOURCE_DIR=_build/deps-source
42
43readonly RE2C_VERSION=3.0
44readonly RE2C_URL="https://github.com/skvadrik/re2c/releases/download/$RE2C_VERSION/re2c-$RE2C_VERSION.tar.xz"
45
46readonly CMARK_VERSION=0.29.0
47readonly CMARK_URL="https://github.com/commonmark/cmark/archive/$CMARK_VERSION.tar.gz"
48
49readonly PY_FTP_MIRROR="${PY_FTP_MIRROR:-https://www.python.org/ftp}"
50
51readonly PY2_VERSION=2.7.18
52readonly PY2_URL="$PY_FTP_MIRROR/python/$PY2_VERSION/Python-$PY2_VERSION.tar.xz"
53
54readonly PY3_VERSION=3.10.4
55readonly PY3_URL="$PY_FTP_MIRROR/python/$PY3_VERSION/Python-$PY3_VERSION.tar.xz"
56
57readonly BASH_VER=4.4 # don't clobber BASH_VERSION
58readonly BASH_URL="https://www.oilshell.org/blob/spec-bin/bash-$BASH_VER.tar.gz"
59
60# Another version of bash to test
61readonly BASH5_VER=5.2
62readonly BASH5_URL="https://www.oilshell.org/blob/spec-bin/bash-$BASH5_VER.tar.gz"
63
64readonly DASH_VERSION=0.5.10.2
65readonly DASH_URL="https://www.oilshell.org/blob/spec-bin/dash-$DASH_VERSION.tar.gz"
66
67readonly ZSH_VERSION=5.1.1
68readonly ZSH_URL="https://www.oilshell.org/blob/spec-bin/zsh-$ZSH_VERSION.tar.xz"
69
70readonly MKSH_VERSION=R52c
71readonly MKSH_URL="https://www.oilshell.org/blob/spec-bin/mksh-$MKSH_VERSION.tgz"
72
73readonly BUSYBOX_VERSION='1.35.0'
74readonly BUSYBOX_URL="https://www.oilshell.org/blob/spec-bin/busybox-$BUSYBOX_VERSION.tar.bz2"
75
76readonly YASH_VERSION=2.49
77readonly YASH_URL="https://www.oilshell.org/blob/spec-bin/yash-$YASH_VERSION.tar.xz"
78
79readonly MYPY_GIT_URL=https://github.com/python/mypy
80readonly MYPY_VERSION=0.780
81
82readonly PY3_LIBS=~/wedge/oils-for-unix.org/pkg/py3-libs/$MYPY_VERSION
83
84# Version 2.4.0 from 2021-10-06 was the last version that supported Python 2
85# https://github.com/PyCQA/pyflakes/blob/main/NEWS.rst
86readonly PYFLAKES_VERSION=2.4.0
87#readonly PYFLAKES_URL='https://files.pythonhosted.org/packages/15/60/c577e54518086e98470e9088278247f4af1d39cb43bcbd731e2c307acd6a/pyflakes-2.4.0.tar.gz'
88# 2023-07: Mirrored to avoid network problem on broome during release
89readonly PYFLAKES_URL='https://www.oilshell.org/blob/pyflakes-2.4.0.tar.gz'
90
91readonly BLOATY_VERSION=1.1
92readonly BLOATY_URL='https://github.com/google/bloaty/releases/download/v1.1/bloaty-1.1.tar.bz2'
93
94readonly UFTRACE_VERSION=0.13
95readonly UFTRACE_URL='https://github.com/namhyung/uftrace/archive/refs/tags/v0.13.tar.gz'
96
97readonly SOUFFLE_VERSION=2.4.1
98readonly SOUFFLE_URL=https://github.com/souffle-lang/souffle/archive/refs/tags/2.4.1.tar.gz
99
100log() {
101 echo "$0: $@" >& 2
102}
103
104die() {
105 log "$@"
106 exit 1
107}
108
109rm-oils-crap() {
110 ### When you want to start over
111
112 rm -r -f -v ~/wedge
113 sudo rm -r -f -v /wedge
114}
115
116# Note: git is an implicit dependency -- that's how we got the repo in the
117# first place!
118
119# python2-dev is no longer available on Debian 12
120# python-dev also seems gone
121#
122# wget: for fetching wedges (not on Debian by default!)
123# tree: tiny package that's useful for showing what we installed
124# g++: essential
125# libreadline-dev: needed for the build/prepare.sh Python build.
126# gawk: used by spec-runner.sh for the special match() function.
127# cmake: for cmark
128# PY3_BUILD_DEPS - I think these will be used for building the Python 2 wedge
129# as well
130readonly -a WEDGE_DEPS_DEBIAN=(
131 bzip2
132 wget
133 tree
134 gawk
135 g++
136 ninja-build
137 cmake
138 libreadline-dev
139 systemtap-sdt-dev
140
141 # for Souffle, flex and bison
142 #flex bison
143
144 "${PY3_BUILD_DEPS[@]}"
145)
146
147readonly -a WEDGE_DEPS_ALPINE=(
148 bzip2
149 xz
150
151 wget tree gawk
152
153 gcc g++
154 ninja-build
155 # https://pkgs.alpinelinux.org/packages?name=ninja-is-really-ninja&branch=v3.19&repo=&arch=&maintainer=
156 ninja-is-really-ninja
157 cmake
158
159 readline-dev
160 zlib-dev
161 libffi-dev
162 openssl-dev
163
164 ncurses-dev
165
166 # for Souffle, flex and bison
167 #flex bison
168)
169
170readonly -a WEDGE_DEPS_FEDORA=(
171
172 # Weird, Fedora doesn't have these by default!
173 hostname
174 tar
175 bzip2
176
177 # https://packages.fedoraproject.org/pkgs/wget/wget/
178 wget
179 # https://packages.fedoraproject.org/pkgs/tree-pkg/tree/
180 tree
181 gawk
182
183 # https://packages.fedoraproject.org/pkgs/gcc/gcc/
184 gcc gcc-c++
185
186 ninja-build
187 cmake
188
189 readline-devel
190
191 # Like PY3_BUILD_DEPS
192 # https://packages.fedoraproject.org/pkgs/zlib/zlib-devel/
193 zlib-devel
194 # https://packages.fedoraproject.org/pkgs/libffi/libffi-devel/
195 libffi-devel
196 # https://packages.fedoraproject.org/pkgs/openssl/openssl-devel/
197 openssl-devel
198
199 # For building zsh from source?
200 # https://koji.fedoraproject.org/koji/rpminfo?rpmID=36987813
201 ncurses-devel
202 #libcap-devel
203
204 # still have a job control error compiling bash
205 # https://packages.fedoraproject.org/pkgs/glibc/glibc-devel/
206 # glibc-devel
207)
208
209install-debian-packages() {
210 ### Packages for build/py.sh all, building wedges, etc.
211
212 set -x # show what needs sudo
213
214 # pass -y for say gitpod
215 sudo apt "$@" install "${WEDGE_DEPS_DEBIAN[@]}"
216 set +x
217
218 # maybe pass -y through
219 test/spec-bin.sh install-shells-with-apt "$@"
220}
221
222install-ubuntu-packages() {
223 ### Debian and Ubuntu packages are the same; this function is suggested on the wiki
224 install-debian-packages "$@"
225}
226
227wedge-deps-debian() {
228 # Install packages without prompt
229
230 # 2024-02 - there was an Ubuntu update, and we started needing this
231 sudo apt-get -y update
232
233 install-debian-packages -y
234}
235
236wedge-deps-fedora() {
237 # https://linuxconfig.org/install-development-tools-on-redhat-8
238 # Trying to get past compile errors
239 # sudo dnf group install --assumeyes 'Development Tools'
240
241 sudo dnf install --assumeyes "${WEDGE_DEPS_FEDORA[@]}"
242}
243
244wedge-deps-alpine() {
245 # https://linuxconfig.org/install-development-tools-on-redhat-8
246 # Trying to get past compile errors
247 # sudo dnf group install --assumeyes 'Development Tools'
248
249 sudo apk add "${WEDGE_DEPS_ALPINE[@]}"
250}
251
252#
253# Unused patch, was experiment for Fedora
254#
255
256get-typed-ast-patch() {
257 curl -o deps/typed_ast.patch https://github.com/python/typed_ast/commit/123286721923ae8f3885dbfbad94d6ca940d5c96.patch
258}
259
260# Work around typed_ast bug:
261# https://github.com/python/typed_ast/issues/169
262#
263# Apply this patch
264# https://github.com/python/typed_ast/commit/123286721923ae8f3885dbfbad94d6ca940d5c96
265#
266# typed_ast is tarred up though
267patch-typed-ast() {
268 local package_dir=_cache/py3-libs
269 local patch=$PWD/deps/typed_ast.patch
270
271 pushd $package_dir
272 cat $patch
273 echo
274
275 local dir=typed_ast-1.4.3
276 local tar=typed_ast-1.4.3.tar.gz
277
278 echo OLD
279 ls -l $tar
280 echo
281
282 rm -r -f -v $dir
283 tar -x -z < $tar
284
285 pushd $dir
286 patch -p1 < $patch
287 popd
288 #find $dir
289
290 # Create a new one
291 tar --create --gzip --file $tar typed_ast-1.4.3
292
293 echo NEW
294 ls -l $tar
295 echo
296
297 popd
298}
299
300#
301# Fetch
302#
303
304download-to() {
305 local dir=$1
306 local url=$2
307 wget --no-clobber --directory-prefix "$dir" "$url"
308}
309
310maybe-extract() {
311 local wedge_dir=$1
312 local tar_name=$2
313 local out_dir=$3
314
315 if test -d "$wedge_dir/$out_dir"; then
316 log "Not extracting because $wedge_dir/$out_dir exists"
317 return
318 fi
319
320 local tar=$wedge_dir/$tar_name
321 case $tar_name in
322 *.gz|*.tgz) # mksh ends with .tgz
323 flag='--gzip'
324 ;;
325 *.bz2)
326 flag='--bzip2'
327 ;;
328 *.xz)
329 flag='--xz'
330 ;;
331 *)
332 die "tar with unknown extension: $tar_name"
333 ;;
334 esac
335
336 tar --extract $flag --file $tar --directory $wedge_dir
337}
338
339clone-mypy() {
340 ### replaces deps/from-git
341 local dest_dir=$1
342 local version=${2:-$MYPY_VERSION}
343
344 local dest=$dest_dir/mypy-$version
345 if test -d $dest; then
346 log "Not cloning because $dest exists"
347 return
348 fi
349
350 # v$VERSION is a tag, not a branch
351
352 # size optimization: --depth=1 --shallow-submodules
353 # https://git-scm.com/docs/git-clone
354
355 git clone --recursive --branch v$version \
356 --depth=1 --shallow-submodules \
357 $MYPY_GIT_URL $dest
358
359 # TODO: verify commit checksum
360}
361
362copy-source-medo() {
363 mkdir -p $DEPS_SOURCE_DIR
364
365 # Copy the whole tree, including the .treeptr files
366 cp --verbose --recursive --no-target-directory \
367 deps/source.medo/ $DEPS_SOURCE_DIR/
368}
369
370fetch() {
371 local py_only=${1:-}
372
373 # For now, simulate what 'medo expand deps/source.medo _build/deps-source'
374 # would do: fetch compressed tarballs designated by .treeptr files, and
375 # expand them.
376
377 # _build/deps-source/
378 # re2c/
379 # WEDGE
380 # re2c-3.0/ # expanded .tar.xz file
381
382 copy-source-medo
383
384 download-to $DEPS_SOURCE_DIR/re2c "$RE2C_URL"
385 download-to $DEPS_SOURCE_DIR/cmark "$CMARK_URL"
386 maybe-extract $DEPS_SOURCE_DIR/re2c "$(basename $RE2C_URL)" re2c-$RE2C_VERSION
387 maybe-extract $DEPS_SOURCE_DIR/cmark "$(basename $CMARK_URL)" cmark-$CMARK_VERSION
388
389 if test -n "$py_only"; then
390 log "Fetched dependencies for 'build/py.sh'"
391 return
392 fi
393
394 download-to $DEPS_SOURCE_DIR/pyflakes "$PYFLAKES_URL"
395 maybe-extract $DEPS_SOURCE_DIR/pyflakes "$(basename $PYFLAKES_URL)" \
396 pyflakes-$PYFLAKES_VERSION
397
398 download-to $DEPS_SOURCE_DIR/python2 "$PY2_URL"
399 download-to $DEPS_SOURCE_DIR/python3 "$PY3_URL"
400 maybe-extract $DEPS_SOURCE_DIR/python2 "$(basename $PY2_URL)" Python-$PY2_VERSION
401 maybe-extract $DEPS_SOURCE_DIR/python3 "$(basename $PY3_URL)" Python-$PY3_VERSION
402
403 download-to $DEPS_SOURCE_DIR/bash "$BASH_URL"
404 maybe-extract $DEPS_SOURCE_DIR/bash "$(basename $BASH_URL)" bash-$BASH_VER
405
406 download-to $DEPS_SOURCE_DIR/bash "$BASH5_URL"
407 maybe-extract $DEPS_SOURCE_DIR/bash "$(basename $BASH5_URL)" bash-$BASH5_VER
408
409 download-to $DEPS_SOURCE_DIR/dash "$DASH_URL"
410 maybe-extract $DEPS_SOURCE_DIR/dash "$(basename $DASH_URL)" dash-$DASH_VERSION
411
412 download-to $DEPS_SOURCE_DIR/zsh "$ZSH_URL"
413 maybe-extract $DEPS_SOURCE_DIR/zsh "$(basename $ZSH_URL)" zsh-$ZSH_VERSION
414
415 download-to $DEPS_SOURCE_DIR/mksh "$MKSH_URL"
416 maybe-extract $DEPS_SOURCE_DIR/mksh "$(basename $MKSH_URL)" mksh-$MKSH_VERSION
417
418 download-to $DEPS_SOURCE_DIR/busybox "$BUSYBOX_URL"
419 maybe-extract $DEPS_SOURCE_DIR/busybox "$(basename $BUSYBOX_URL)" busybox-$BUSYBOX_VERSION
420
421 download-to $DEPS_SOURCE_DIR/yash "$YASH_URL"
422 maybe-extract $DEPS_SOURCE_DIR/yash "$(basename $YASH_URL)" yash-$DASH_VERSION
423
424 # Patch: this tarball doesn't follow the convention $name-$version
425 if test -d $DEPS_SOURCE_DIR/mksh/mksh; then
426 pushd $DEPS_SOURCE_DIR/mksh
427 mv -v mksh mksh-$MKSH_VERSION
428 popd
429 fi
430
431 # bloaty and uftrace are for benchmarks, in containers
432 download-to $DEPS_SOURCE_DIR/bloaty "$BLOATY_URL"
433 download-to $DEPS_SOURCE_DIR/uftrace "$UFTRACE_URL"
434 maybe-extract $DEPS_SOURCE_DIR/bloaty "$(basename $BLOATY_URL)" bloaty-$BLOATY_VERSION
435 maybe-extract $DEPS_SOURCE_DIR/uftrace "$(basename $UFTRACE_URL)" uftrace-$UFTRACE_VERSION
436
437 # This is in $DEPS_SOURCE_DIR to COPY into containers, which mycpp will directly import.
438
439 # It's also copied into a wedge in install-wedges.
440 clone-mypy $DEPS_SOURCE_DIR/mypy
441
442 if false; then
443 download-to $DEPS_SOURCE_DIR/souffle "$SOUFFLE_URL"
444 maybe-extract $DEPS_SOURCE_DIR/souffle "$(basename $SOUFFLE_URL)" souffle-$SOUFFLE_VERSION
445 fi
446
447 if command -v tree > /dev/null; then
448 tree -L 2 $DEPS_SOURCE_DIR
449 fi
450}
451
452fetch-py() {
453 fetch py_only
454}
455
456mirror-pyflakes() {
457 ### Workaround for network error during release
458 scp \
459 $DEPS_SOURCE_DIR/pyflakes/"$(basename $PYFLAKES_URL)" \
460 oilshell.org:oilshell.org/blob/
461}
462
463wedge-exists() {
464 ### Does an installed wedge already exist?
465
466 local name=$1
467 local version=$2
468 local wedge_dir=${3:-/wedge/oils-for-unix.org}
469
470 local installed=$wedge_dir/pkg/$name/$version
471
472 if test -d $installed; then
473 log "$installed already exists"
474 return 0
475 else
476 return 1
477 fi
478}
479
480#
481# Install
482#
483
484# TODO: py3-libs needs to be a WEDGE, so that that you can run
485# 'wedge build deps/source.medo/py3-libs/' and then get it in
486#
487# _build/wedge/{absolute,relative} # which one?
488#
489# It needs a BUILD DEPENDENCY on:
490# - the python3 wedge, so you can do python3 -m pip install.
491# - the mypy repo, which has test-requirements.txt
492
493download-py3-libs() {
494 ### Download source/binary packages, AFTER python3 is installed
495
496 # Note that this is NOT source code; there is binary code, e.g. in
497 # lxml-*.whl
498
499 local mypy_dir=${1:-$DEPS_SOURCE_DIR/mypy/mypy-$MYPY_VERSION}
500 local py_package_dir=_cache/py3-libs
501 mkdir -p $py_package_dir
502
503 # Avoids a warning, but doesn't fix typed_ast
504 #python3 -m pip download -d $py_package_dir wheel
505
506 python3 -m pip download -d $py_package_dir -r $mypy_dir/test-requirements.txt
507 python3 -m pip download -d $py_package_dir pexpect
508}
509
510install-py3-libs-in-venv() {
511 local venv_dir=$1
512 local mypy_dir=$2 # This is a param for host build vs. container build
513 local package_dir=_cache/py3-libs
514
515 source $venv_dir/bin/activate # enter virtualenv
516
517 # 2023-07 note: we're installing yapf in a DIFFERENT venv, because it
518 # conflicts with MyPy deps!
519 # "ERROR: pip's dependency resolver does not currently take into account all
520 # the packages that are installed."
521
522 # --find-links uses a "cache dir" for packages (weird flag name!)
523
524 # Avoids a warning, but doesn't fix typed_ast
525 #time python3 -m pip install --find-links $package_dir wheel
526
527 # for mycpp/
528 time python3 -m pip install --find-links $package_dir -r $mypy_dir/test-requirements.txt
529
530 # pexpect: for spec/stateful/*.py
531 time python3 -m pip install --find-links $package_dir pexpect
532}
533
534install-py3-libs-from-cache() {
535
536 # As well as end users
537
538 local mypy_dir=${1:-$DEPS_SOURCE_DIR/mypy/mypy-$MYPY_VERSION}
539
540 local py3
541 py3=$(command -v python3)
542 case $py3 in
543 *wedge/oils-for-unix.org/*)
544 ;;
545 *)
546 die "python3 is '$py3', but expected it to be in a wedge"
547 ;;
548 esac
549
550 log "Ensuring pip is installed (interpreter $(command -v python3)"
551 python3 -m ensurepip
552
553 local venv_dir=$USER_WEDGE_DIR/pkg/py3-libs/$PY3_LIBS_VERSION
554 log "Creating venv in $venv_dir"
555
556 # Note: the bin/python3 in this venv is a symlink to python3 in $PATH, i.e.
557 # the /wedge we just built
558 python3 -m venv $venv_dir
559
560 log "Installing MyPy deps in venv"
561
562 # Run in a subshell because it mutates shell state
563 $0 install-py3-libs-in-venv $venv_dir $mypy_dir
564}
565
566install-py3-libs() {
567 ### Invoked by Dockerfile.cpp-small, etc.
568
569 download-py3-libs
570 install-py3-libs-from-cache
571}
572
573# OBSOLETE in favor of install-spec-bin-fast
574install-spec-bin() {
575 if ! wedge-exists dash $DASH_VERSION $USER_WEDGE_DIR; then
576 deps/wedge.sh unboxed _build/deps-source/dash
577 fi
578
579 if ! wedge-exists mksh $MKSH_VERSION $USER_WEDGE_DIR; then
580 deps/wedge.sh unboxed _build/deps-source/mksh
581 fi
582
583 if ! wedge-exists busybox $BUSYBOX_VERSION $USER_WEDGE_DIR; then
584 deps/wedge.sh unboxed _build/deps-source/busybox
585 fi
586
587 # Fedora compile error - count_all_jobs
588 if ! wedge-exists bash $BASH_VER $USER_WEDGE_DIR; then
589 deps/wedge.sh unboxed _build/deps-source/bash
590 fi
591
592 # Fedora compiler error
593 # zsh ./configure is NOT detecting 'boolcodes', and then it has a broken
594 # fallback in Src/Modules/termcap.c that causes a compile error! It seems
595 # like ncurses-devel should fix this, but it doesn't
596 #
597 # https://koji.fedoraproject.org/koji/rpminfo?rpmID=36987813
598 #
599 # from /home/build/oil/_build/deps-source/zsh/zsh-5.1.1/Src/Modules/termcap.c:38:
600 # /usr/include/term.h:783:56: note: previous declaration of ‘boolcodes’ with type ‘const char * const[]’
601 # 783 | extern NCURSES_EXPORT_VAR(NCURSES_CONST char * const ) boolcodes[];
602 #
603 # I think the ./configure is out of sync with the actual build?
604
605 if ! wedge-exists zsh $ZSH_VERSION ''; then
606 deps/wedge.sh unboxed _build/deps-source/zsh
607 fi
608
609 return
610
611 # Hm this has problem with out-of-tree build? I think Oils does too actually
612 if ! wedge-exists yash $YASH_VERSION $USER_WEDGE_DIR; then
613 deps/wedge.sh unboxed _build/deps-source/yash
614 fi
615}
616
617# TODO:
618# - $ROOT_WEDGE_DIR vs. $USER_WEDGE_DIR is duplicating information that's
619# already in each WEDGE file
620
621py-wedges() {
622 ### for build/py.sh all
623
624 echo cmark $CMARK_VERSION $ROOT_WEDGE_DIR
625 echo re2c $RE2C_VERSION $ROOT_WEDGE_DIR
626 echo python2 $PY2_VERSION $ROOT_WEDGE_DIR
627 echo pyflakes $PYFLAKES_VERSION $USER_WEDGE_DIR
628}
629
630cpp-wedges() {
631 ### for ninja / mycpp translation
632
633 echo python3 $PY3_VERSION $ROOT_WEDGE_DIR
634 echo mypy $MYPY_VERSION $USER_WEDGE_DIR
635
636 # Test both outside the contianer, as well as inside?
637 echo uftrace $UFTRACE_VERSION $ROOT_WEDGE_DIR
638
639 #echo souffle $SOUFFLE_VERSION $USER_WEDGE_DIR
640
641 # py3-libs has a built time dep on both python3 and MyPy, so we're doing it
642 # separately for now
643 #echo py3-libs $PY3_LIBS_VERSION $USER_WEDGE_DIR
644}
645
646spec-bin-wedges() {
647 ### for test/spec-py.sh osh-all
648
649 echo dash $DASH_VERSION $USER_WEDGE_DIR
650 echo bash $BASH_VER $USER_WEDGE_DIR
651 echo bash $BASH5_VER $USER_WEDGE_DIR
652 echo mksh $MKSH_VERSION $USER_WEDGE_DIR
653 echo zsh $ZSH_VERSION $USER_WEDGE_DIR
654 echo busybox $BUSYBOX_VERSION $USER_WEDGE_DIR
655}
656
657timestamp() {
658 date '+%H:%M:%S'
659}
660
661my-time-tsv() {
662 python3 benchmarks/time_.py \
663 --tsv \
664 --time-span --rusage \
665 "$@"
666}
667
668maybe-install-wedge() {
669 local name=$1
670 local version=$2
671 local wedge_dir=$3 # e.g. $USER_WEDGE_DIR or empty
672
673 local task_file=$WEDGE_LOG_DIR/$name-$version.task.tsv
674 local log_file=$WEDGE_LOG_DIR/$name-$version.log.txt
675
676 echo " TASK $(timestamp) $name $version > $log_file"
677
678 # python3 because it's OUTSIDE the container
679 # Separate columns that could be joined: number of files, total size
680 my-time-tsv --print-header \
681 --field xargs_slot \
682 --field wedge \
683 --field wedge_HREF \
684 --field version \
685 --output $task_file
686
687 if wedge-exists "$name" "$version" "$wedge_dir"; then
688 echo "CACHED $(timestamp) $name $version"
689 return
690 fi
691
692 local -a cmd=( deps/wedge.sh unboxed _build/deps-source/$name/ $version)
693
694 set +o errexit
695 my-time-tsv \
696 --field "$XARGS_SLOT" \
697 --field "$name" \
698 --field "$name-$version.log.txt" \
699 --field "$version" \
700 --append \
701 --output $task_file \
702 "${cmd[@]}" "$@" >$log_file 2>&1
703 local status=$?
704 set -o errexit
705
706 if test "$status" -eq 0; then
707 echo " OK $(timestamp) $name $version"
708 else
709 echo " FAIL $(timestamp) $name $version"
710 fi
711}
712
713dummy-task() {
714 ### For testing log capture
715 local name=$1
716 local version=$2
717
718 echo "Building $name $version"
719
720 # random float between 0 and 3
721 # weirdly we need a seed from bash
722 # https://stackoverflow.com/questions/4048378/random-numbers-generation-with-awk-in-bash-shell
723 local secs
724 secs=$(awk -v seed=$RANDOM 'END { srand(seed); print rand() * 3 }' < /dev/null)
725
726 echo "sleep $secs"
727 sleep $secs
728
729 echo 'stdout'
730 log 'stderr'
731
732 if test $name = 'mksh'; then
733 echo "simulate failure for $name"
734 exit 2
735 fi
736}
737
738readonly WEDGE_LOG_DIR=_build/wedge/logs
739
740dummy-task-wrapper() {
741 # Similar to test/common.sh run-task-with-status, used by
742 # test/{spec,wild}-runner.sh
743 local name=$1
744 local version=$2
745
746 local task_file=$WEDGE_LOG_DIR/$name.task.tsv
747 local log_file=$WEDGE_LOG_DIR/$name.log.txt
748
749 echo " TASK $(timestamp) $name $version > $log_file"
750
751 # python3 because it's OUTSIDE the container
752 # Separate columns that could be joined: number of files, total size
753 my-time-tsv --print-header \
754 --field xargs_slot \
755 --field wedge \
756 --field wedge_HREF \
757 --field version \
758 --output $task_file
759
760 my-time-tsv \
761 --field "$XARGS_SLOT" \
762 --field "$name" \
763 --field "$name.log.txt" \
764 --field "$version" \
765 --append \
766 --output $task_file \
767 $0 dummy-task "$@" >$log_file 2>&1 || true
768
769 echo " DONE $(timestamp) $name $version"
770}
771
772html-head() {
773 # python3 because we're outside containers
774 PYTHONPATH=. python3 doctools/html_head.py "$@"
775}
776
777index-html() {
778 local tasks_tsv=$1
779
780 local base_url='../../../web'
781 html-head --title 'Wedge Builds' \
782 "$base_url/ajax.js" \
783 "$base_url/table/table-sort.js" \
784 "$base_url/table/table-sort.css" \
785 "$base_url/base.css"\
786
787 table-sort-begin 'width60'
788
789 cat <<EOF
790 <p id="home-link">
791 <a href="/">oilshell.org</a>
792 </p>
793
794 <h1>Wedge Builds</h1>
795EOF
796
797 tsv2html3 $tasks_tsv
798
799 cat <<EOF
800 <p>
801 <a href="tasks.tsv">tasks.tsv</a>
802 </p>
803EOF
804
805 table-sort-end 'tasks' # ID for sorting
806}
807
808NPROC=$(nproc)
809#NPROC=1
810
811install-wedge-list() {
812 ### Reads task rows from stdin
813 local parallel=${1:-}
814
815 mkdir -p _build/wedge/logs
816
817 local -a flags
818 if test -n "$parallel"; then
819 log ""
820 log "=== Installing wedges with $NPROC jobs in parallel"
821 log ""
822 flags=( -P $NPROC )
823 else
824 log ""
825 log "=== Installing wedges serially"
826 log ""
827 fi
828
829 # Reads from stdin
830 # Note: --process-slot-var requires GNU xargs! busybox args doesn't have it.
831 #
832 # $name $version $wedge_dir
833 xargs "${flags[@]}" -n 3 --process-slot-var=XARGS_SLOT -- $0 maybe-install-wedge
834
835 #xargs "${flags[@]}" -n 3 --process-slot-var=XARGS_SLOT -- $0 dummy-task-wrapper
836}
837
838write-task-report() {
839 local tasks_tsv=_build/wedge/logs/tasks.tsv
840
841 python3 devtools/tsv_concat.py $WEDGE_LOG_DIR/*.task.tsv > $tasks_tsv
842 log "Wrote $tasks_tsv"
843
844 # TODO: version can be right-justified?
845 here-schema-tsv-4col >_build/wedge/logs/tasks.schema.tsv <<EOF
846column_name type precision strftime
847status integer 0 -
848elapsed_secs float 1 -
849user_secs float 1 -
850start_time float 1 %H:%M:%S
851end_time float 1 %H:%M:%S
852sys_secs float 1 -
853max_rss_KiB integer 0 -
854xargs_slot integer 0 -
855wedge string 0 -
856wedge_HREF string 0 -
857version string 0 -
858EOF
859
860 index-html $tasks_tsv > $WEDGE_LOG_DIR/index.html
861 log "Wrote $WEDGE_LOG_DIR/index.html"
862}
863
864install-spec-bin-fast() {
865 spec-bin-wedges | install-wedge-list T
866 write-task-report
867}
868
869fake-py3-libs-wedge() {
870 local name=py3-libs
871 local version=$PY3_LIBS_VERSION
872
873 local task_file=$WEDGE_LOG_DIR/$name.task.tsv
874 local log_file=$WEDGE_LOG_DIR/$name.log.txt
875
876 my-time-tsv --print-header \
877 --field xargs_slot \
878 --field wedge \
879 --field wedge_HREF \
880 --field version \
881 --output $task_file
882
883 # There is no xargs slot!
884 my-time-tsv \
885 --field "-1" \
886 --field "$name" \
887 --field "$name.log.txt" \
888 --field "$version" \
889 --append \
890 --output $task_file \
891 $0 install-py3-libs >$log_file 2>&1 || true
892
893 echo " FAKE $(timestamp) $name $version"
894}
895
896install-wedges-fast() {
897 echo " START $(timestamp)"
898
899 # Do all of them in parallel
900 { py-wedges; cpp-wedges; spec-bin-wedges; } | install-wedge-list T
901
902 fake-py3-libs-wedge
903 echo " END $(timestamp)"
904
905 write-task-report
906}
907
908# OBSOLETE in favor of install-wedges-fast
909install-wedges() {
910 local py_only=${1:-}
911
912 # TODO:
913 # - Make all of these RELATIVE wedges
914 # - Add
915 # - unboxed-rel-smoke-test -- move it inside container
916 # - rel-smoke-test -- mount it in a different location
917
918 if ! wedge-exists cmark $CMARK_VERSION; then
919 deps/wedge.sh unboxed _build/deps-source/cmark/
920 fi
921
922 if ! wedge-exists re2c $RE2C_VERSION; then
923 deps/wedge.sh unboxed _build/deps-source/re2c/
924 fi
925
926 if ! wedge-exists python2 $PY2_VERSION; then
927 deps/wedge.sh unboxed _build/deps-source/python2/
928 fi
929
930 if test -n "$py_only"; then
931 log "Installed dependencies for 'build/py.sh'"
932 return
933 fi
934
935 # Just copy this source tarball
936 if ! wedge-exists pyflakes $PYFLAKES_VERSION $USER_WEDGE_DIR; then
937 local dest_dir=$USER_WEDGE_DIR/pkg/pyflakes/$PYFLAKES_VERSION
938 mkdir -p $dest_dir
939
940 cp --verbose --recursive --no-target-directory \
941 $DEPS_SOURCE_DIR/pyflakes/pyflakes-$PYFLAKES_VERSION $dest_dir
942 fi
943
944 if ! wedge-exists python3 $PY3_VERSION; then
945 deps/wedge.sh unboxed _build/deps-source/python3/
946 fi
947
948 # Copy all the contents, except for .git folder.
949 if ! wedge-exists mypy $MYPY_VERSION $USER_WEDGE_DIR; then
950
951 # NOTE: We have to also copy the .git dir, because it has
952 # .git/modules/typeshed
953 local dest_dir=$USER_WEDGE_DIR/pkg/mypy/$MYPY_VERSION
954 mkdir -p $dest_dir
955
956 # Note: pack files in .git/modules/typeshed/objects/pack are read-only
957 # this can fail
958 cp --verbose --recursive --no-target-directory \
959 $DEPS_SOURCE_DIR/mypy/mypy-$MYPY_VERSION $dest_dir
960 fi
961
962 if ! wedge-exists py3-libs $PY3_LIBS_VERSION $USER_WEDGE_DIR; then
963 download-py3-libs
964 # This patch doesn't work?
965 # patch-typed-ast
966 install-py3-libs
967 fi
968
969 if ! wedge-exists souffle $SOUFFLE_VERSION $USER_WEDGE_DIR; then
970 deps/wedge.sh unboxed _build/deps-source/souffle/
971 fi
972
973 if command -v tree > /dev/null; then
974 tree -L 3 $USER_WEDGE_DIR
975 echo
976 tree -L 3 /wedge/oils-for-unix.org
977 fi
978}
979
980install-wedges-py() {
981 install-wedges py_only
982}
983
984#
985# Unboxed wedge builds
986#
987
988uftrace-host() {
989 ### built on demand; run $0 first
990
991 # BUG: doesn't detect python3
992 # WEDGE tells me that it depends on pkg-config
993 # 'apt-get install pkgconf' gets it
994 # TODO: Should use python3 WEDGE instead of SYSTEM python3?
995
996 deps/wedge.sh unboxed _build/deps-source/uftrace
997}
998
999bloaty-host() {
1000 deps/wedge.sh unboxed _build/deps-source/bloaty
1001}
1002
1003R-libs-host() {
1004 deps/wedge.sh unboxed _build/deps-source/R-libs
1005}
1006
1007#
1008# Wedges built inside a container, for copying into a container
1009#
1010
1011container-wedges() {
1012 #### host _build/wedge/binary -> guest container /wedge or ~/wedge
1013
1014 #export-podman
1015
1016 # TODO:
1017 #
1018 # - Add equivalents of spec-bin
1019 # - Use the same manifest as install-wedges-fast
1020 # - so then you can delete the _build/wedge dir to re-run it
1021 # - use xargs -n 1 so it's done serially
1022
1023 # - Do these lazily like we do in install-wedges-fast?
1024
1025 # We can test if the dir _build/wedge/binary/oils-for-unix.org/pkg/FOO exists
1026 # if wedge-exists "$name" "$version" "$wedge_dir"; then
1027 # echo "CACHED $(timestamp) $name $version"
1028 # return
1029 # fi
1030
1031 if false; then
1032 deps/wedge.sh boxed deps/source.medo/time-helper
1033 deps/wedge.sh boxed deps/source.medo/cmark/
1034 deps/wedge.sh boxed deps/source.medo/re2c/
1035 deps/wedge.sh boxed deps/source.medo/python3/
1036 fi
1037
1038 if false; then
1039 deps/wedge.sh boxed deps/source.medo/bloaty/
1040 fi
1041
1042 if true; then
1043 # build with debian-12, because soil-benchmarks2 is, because it has R
1044 deps/wedge.sh boxed deps/source.medo/uftrace/ '' debian-12
1045 # python2 needed everywhere
1046 #deps/wedge.sh boxed deps/source.medo/python2/ '' debian-12
1047
1048 # TODO: build with debian-12
1049 # Used in {benchmarks,benchmarks2,other-tests}
1050 #deps/wedge.sh boxed deps/source.medo/R-libs/ '' debian-12
1051 fi
1052}
1053
1054#
1055# Report
1056#
1057
1058commas() {
1059 # Wow I didn't know this :a trick
1060 #
1061 # OK this is a label and a loop, which makes sense. You can't do it with
1062 # pure regex.
1063 #
1064 # https://shallowsky.com/blog/linux/cmdline/sed-improve-comma-insertion.html
1065 # https://shallowsky.com/blog/linux/cmdline/sed-improve-comma-insertion.html
1066 sed ':a;s/\b\([0-9]\+\)\([0-9]\{3\}\)\b/\1,\2/;ta'
1067}
1068
1069wedge-sizes() {
1070 local tmp=_tmp/wedge-sizes.txt
1071
1072 # -b is --bytes, but use short flag for busybox compat
1073 du -s -b /wedge/*/*/* ~/wedge/*/*/* | awk '
1074 { print $0 # print the line
1075 total_bytes += $1 # accumulate
1076 }
1077END { print total_bytes " TOTAL" }
1078' > $tmp
1079
1080 # printf justifies du output
1081 cat $tmp | commas | xargs -n 2 printf '%15s %s\n'
1082 echo
1083
1084 #du -s --si /wedge/*/*/* ~/wedge/*/*/*
1085 #echo
1086}
1087
1088wedge-report() {
1089 # 4 levels deep shows the package
1090 if command -v tree > /dev/null; then
1091 tree -L 4 /wedge ~/wedge
1092 echo
1093 fi
1094
1095 wedge-sizes
1096
1097 local tmp=_tmp/wedge-manifest.txt
1098
1099 echo 'Biggest files'
1100 if ! find /wedge ~/wedge -type f -a -printf '%10s %P\n' > $tmp; then
1101 # busybox find doesn't have -printf
1102 echo 'find -printf failed'
1103 return
1104 fi
1105
1106 set +o errexit # ignore SIGPIPE
1107 sort -n --reverse $tmp | head -n 20 | commas
1108 set -o errexit
1109
1110 echo
1111
1112 # Show the most common file extensions
1113 #
1114 # I feel like we should be able to get rid of .a files? That's 92 MB, second
1115 # most common
1116 #
1117 # There are also duplicate .a files for Python -- should look at how distros
1118 # get rid of those
1119
1120 cat $tmp | python3 -c '
1121import os, sys, collections
1122
1123bytes = collections.Counter()
1124files = collections.Counter()
1125
1126for line in sys.stdin:
1127 size, path = line.split(None, 1)
1128 path = path.strip() # remove newline
1129 _, ext = os.path.splitext(path)
1130 size = int(size)
1131
1132 bytes[ext] += size
1133 files[ext] += 1
1134
1135#print(bytes)
1136#print(files)
1137
1138n = 20
1139
1140print("Most common file types")
1141for ext, count in files.most_common()[:n]:
1142 print("%10d %s" % (count, ext))
1143
1144print()
1145
1146print("Total bytes by file type")
1147for ext, total_bytes in bytes.most_common()[:n]:
1148 print("%10d %s" % (total_bytes, ext))
1149' | commas
1150}
1151
1152run-task "$@"