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