1 | #!/usr/bin/env bash
|
2 | #
|
3 | # xshar - Executable shell archive
|
4 | #
|
5 | # Usage:
|
6 | # devtools/xshar.sh <function name>
|
7 | #
|
8 | # devtools/
|
9 | # xshar.sh # this file
|
10 | # test-oils.sh # can run benchmarks
|
11 | #
|
12 | # Results in:
|
13 | #
|
14 | # _release/test-oils.xshar
|
15 | #
|
16 | # Runtime deps:
|
17 | #
|
18 | # /bin/sh
|
19 | # tar, gzip -d
|
20 | # base64
|
21 |
|
22 | set -o nounset
|
23 | set -o pipefail
|
24 | set -o errexit
|
25 |
|
26 | source devtools/run-task.sh # run-task
|
27 |
|
28 | print-shell() {
|
29 | local tar=$1
|
30 | local main=$2
|
31 |
|
32 | # Same as soil/worker.sh
|
33 | local git_commit
|
34 | git_commit=$(git log -n 1 --pretty='format:%H')
|
35 |
|
36 | sed "s/__GIT_COMMIT__/$git_commit/" <<'EOF'
|
37 | #!/bin/sh
|
38 |
|
39 | export XSHAR_REPO=oilshell/oil
|
40 | export XSHAR_GIT_COMMIT=__GIT_COMMIT__
|
41 |
|
42 | name=$(basename $0) # e.g. hello-xshar.xshar
|
43 | default_dir=/tmp/$name.$$
|
44 |
|
45 | # User can override this, and then _build/oils.sh can use SKIP_REBUILD to make
|
46 | # it faster. Multiple runs without compiling.
|
47 |
|
48 | export XSHAR_DIR=${XSHAR_DIR:-$default_dir}
|
49 |
|
50 | change_dir() {
|
51 | mkdir -p "$XSHAR_DIR"
|
52 | cd "$XSHAR_DIR"
|
53 | }
|
54 |
|
55 | extract_data() {
|
56 | base64 -d <<'XSHAR_DATA' | tar -x -z
|
57 | EOF
|
58 |
|
59 | # Print code that extracts here doc
|
60 | base64 < $tar
|
61 |
|
62 | cat <<EOF
|
63 | XSHAR_DATA
|
64 | }
|
65 |
|
66 | change_dir
|
67 | extract_data
|
68 | EOF
|
69 | echo "$main" '"$@"'
|
70 |
|
71 | # We don't bother to clean up after, it's in /tmp
|
72 | }
|
73 |
|
74 | create() {
|
75 | local main=${1:-devtools/test-oils.sh}
|
76 | local manifest=$2
|
77 | #shift
|
78 |
|
79 | local name
|
80 | name=$(basename $main .sh)
|
81 |
|
82 | local tar=_tmp/$name.tar.gz
|
83 |
|
84 | # Need --files-from because we can run out of ARGV!
|
85 | tar --create --gzip --files-from $manifest --file $tar "$main"
|
86 | ls -l $tar
|
87 |
|
88 | local out=_release/$name.xshar
|
89 |
|
90 | print-shell $tar $main > $out
|
91 | chmod +x $out
|
92 | ls -l $out
|
93 | echo
|
94 | }
|
95 |
|
96 | create-hello() {
|
97 | local tmp=_tmp/hello-manifest.txt
|
98 | find yaks/ -name '*.py' > $tmp
|
99 | create devtools/hello-xshar.sh $tmp
|
100 |
|
101 | ls -l -h _release
|
102 | }
|
103 |
|
104 | test-oils-manifest() {
|
105 | echo '_release/oils-for-unix.tar'
|
106 |
|
107 | echo 'oil-version.txt'
|
108 |
|
109 | echo 'benchmarks/time_.py'
|
110 | echo 'benchmarks/time-helper.c'
|
111 |
|
112 | # TODO: implement shell-deps tool
|
113 | #
|
114 | # osh --tool shell-deps build/py.sh
|
115 | echo 'build/dev-shell.sh'
|
116 | echo 'build/py.sh'
|
117 | echo 'build/common.sh'
|
118 | echo 'devtools/run-task.sh'
|
119 |
|
120 | # osh --tool shell-deps benchmarks/osh-runtime.sh
|
121 | # copied from benchmarks/osh-runtime.sh
|
122 | cat <<'EOF'
|
123 | benchmarks/osh-runtime.sh
|
124 | benchmarks/common.sh
|
125 | benchmarks/id.sh
|
126 | soil/common.sh
|
127 | test/common.sh
|
128 | test/tsv-lib.sh
|
129 | EOF
|
130 | echo 'benchmarks/gc_stats_to_tsv.py'
|
131 | echo 'devtools/tsv_concat.py'
|
132 |
|
133 | find testdata/osh-runtime -type f
|
134 |
|
135 | find Python-2.7.13/ -type f
|
136 | }
|
137 |
|
138 | create-test-oils() {
|
139 | devtools/release-native.sh make-tar
|
140 |
|
141 | local tmp=_tmp/test-oils-manifest.txt
|
142 | test-oils-manifest > $tmp
|
143 | create devtools/test-oils.sh $tmp
|
144 | ls -l -h _release
|
145 | }
|
146 |
|
147 | soil-run-hello() {
|
148 | create-hello
|
149 | _release/hello-xshar.xshar main a b c
|
150 | }
|
151 |
|
152 | soil-run-test-oils() {
|
153 | create-test-oils
|
154 |
|
155 | # Demo of flag parsing
|
156 | XSHAR_DIR=/tmp/test-oils.xshar.REUSED _release/test-oils.xshar osh-runtime \
|
157 | --num-iters 1 --num-shells 1 --num-workloads 1
|
158 |
|
159 | # Run it twice to test that SKIP_REBUILD works
|
160 | XSHAR_DIR=/tmp/test-oils.xshar.REUSED _release/test-oils.xshar --help
|
161 | XSHAR_DIR=/tmp/test-oils.xshar.REUSED _release/test-oils.xshar --version
|
162 | }
|
163 |
|
164 | run-task "$@"
|