OILS / build / ninja-rules-py.sh View on Github | oils.pub

397 lines, 193 significant
1#!/usr/bin/env bash
2#
3# Ninja rules for translating Python to C++.
4#
5# Usage:
6# build/ninja-rules-py.sh <function name>
7#
8# Env variables:
9# _bin/shwrap/mycpp_main respects EXTRA_MYCPP_ARGS
10# for --stack-roots-warn 16 in CI
11
12set -o nounset
13set -o pipefail
14set -o errexit
15
16REPO_ROOT=$(cd "$(dirname $0)/.."; pwd)
17
18source build/dev-shell.sh # python2 in $PATH
19#source devtools/types.sh # typecheck-files
20source $REPO_ROOT/test/tsv-lib.sh # time-tsv
21
22die() {
23 echo "$@" >& 2
24 exit 1
25}
26
27example-main-wrapper() {
28 ### Used by mycpp/examples
29 local main_namespace=${1:-fib_iter}
30 local declare=${2:-}
31
32 if test -n "$declare"; then
33 echo "namespace $main_namespace {"
34 echo 'void run_tests();'
35 echo 'void run_benchmarks();'
36 echo '}'
37 fi
38
39 cat <<EOF
40int main(int argc, char **argv) {
41 gHeap.Init();
42
43 char* b = getenv("BENCHMARK");
44 if (b && strlen(b)) { // match Python's logic
45 fprintf(stderr, "Benchmarking...\\n");
46 $main_namespace::run_benchmarks();
47 } else {
48 $main_namespace::run_tests();
49 }
50
51 gHeap.CleanProcessExit();
52}
53EOF
54}
55
56main-wrapper() {
57 ### Used by oils-for-unix and yaks
58 local main_namespace=$1
59 local declare=${2:-}
60
61 if test -n "$declare"; then
62 # forward declaration
63 echo '#include "mycpp/gc_list.h"'
64 echo "namespace $main_namespace {"
65 echo 'int main(List<BigStr*> argv);'
66 echo '}'
67 fi
68
69 cat <<EOF
70int main(int argc, char **argv) {
71 mylib::InitCppOnly(); // Initializes gHeap
72
73 auto* args = Alloc<List<BigStr*>>();
74 for (int i = 0; i < argc; ++i) {
75 args->append(StrFromC(argv[i]));
76 }
77
78 int status = $main_namespace::main(args);
79
80 gHeap.ProcessExit();
81
82 return status;
83}
84EOF
85}
86
87windows-main() {
88 local main_namespace=$1
89 local declare=${2:-}
90
91 if test -n "$declare"; then
92 # forward declaration
93 echo '#include "mycpp/gc_list.h"'
94 echo "namespace $main_namespace {"
95 echo 'int main(List<BigStr*> argv);'
96 echo '}'
97 fi
98
99 cat <<EOF
100int main(int argc, char **argv) {
101 gHeap.Init();
102
103 auto* args = Alloc<List<BigStr*>>();
104 for (int i = 0; i < argc; ++i) {
105 args->append(StrFromC(argv[i]));
106 }
107
108 int status = $main_namespace::main(args);
109
110 gHeap.ProcessExit();
111
112 return status;
113}
114EOF
115}
116
117write-main() {
118 ### Write a C++ main file, for mycpp
119 local template=$1
120 local out=$2
121 local main_namespace=$3
122
123 case $template in
124 unix)
125 main-wrapper $main_namespace T
126 ;;
127 win32)
128 windows-main $main_namespace T
129 ;;
130 example-unix)
131 example-main-wrapper $main_namespace T
132 ;;
133 *)
134 die "Invalid template '$template'"
135 ;;
136 esac > $out
137}
138
139print-wrap-cc() {
140 local out=$1
141 local main_func=$2
142 local main_namespace=$3
143 local in=$4
144 local preamble_path=$5
145
146 echo "// $out - generated from Python source code"
147 echo
148
149 if test -f "$preamble_path"; then
150 echo "#include \"$preamble_path\""
151 else
152 # the default preamble, for mycpp/examples
153 echo '#include "mycpp/runtime.h"'
154 fi
155 echo
156
157 cat $in
158
159 # example-main-wrapper, main-wrapper, etc.
160 $main_func $main_namespace
161}
162
163wrap-cc() {
164 local out=$1
165
166 # $translator $main_namespace $in $preamble_path
167 print-wrap-cc "$@" > $out
168}
169
170# TODO: Move mycpp/example tasks out of Ninja since timing is not a VALUE. It
171# depends on the machine, can be done more than once, etc.
172
173task() {
174 local bin=$1 # Run this
175 local task_out=$2
176 local log_out=$3
177
178 shift 3
179 # The rest of the args are passed as flags to time-tsv
180
181 case $bin in
182 (mycpp/examples/*.py)
183 # we import mycpp.mylib
184 export PYTHONPATH="$REPO_ROOT/mycpp:$REPO_ROOT/vendor:$REPO_ROOT"
185 ;;
186 esac
187
188 case $task_out in
189 (_test/tasks/benchmark/*)
190 export BENCHMARK=1
191 ;;
192 esac
193
194 time-tsv -o $task_out --rusage "$@" --field $bin --field $task_out -- \
195 $bin >$log_out 2>&1
196}
197
198example-task() {
199 ### Run a program in the examples/ dir, either in Python or C++
200
201 local name=$1 # e.g. 'fib_iter'
202 local impl=$2 # 'Python' or 'C++'
203
204 local bin=$3 # Run this
205 local task_out=$4
206 local log_out=$5
207
208 task $bin $task_out $log_out --field $name --field $impl
209}
210
211benchmark-table() {
212 local out=$1
213 shift
214
215 # TODO: Use QTT header with types?
216 { time-tsv --print-header --rusage \
217 --field example_name --field impl \
218 --field bin --field task_out
219
220 # Concatenate task files
221 cat "$@"
222 } > $out
223}
224
225# Copied from devtools/types.sh
226
227MYPY_FLAGS='--strict --no-strict-optional'
228typecheck-files() {
229 echo "MYPY $@"
230
231 # TODO: Adjust path for mcypp/examples/modules.py
232 time MYPYPATH='.:pyext' python3 -m mypy --py2 --follow-imports=silent $MYPY_FLAGS "$@"
233}
234
235typecheck() {
236 ### Typecheck without translation
237 local main_py=$1
238 local out=$2
239 local skip_imports=${3:-}
240
241 if test -n "$skip_imports"; then
242 local more_flags='--follow-imports=silent'
243 else
244 local more_flags=''
245 fi
246
247 # Similar to devtools/types.sh
248
249 local status=0
250
251 set +o errexit
252 typecheck-files $main_py > $out
253 status=$?
254 set -o errexit
255
256 if test $status != 0; then
257 echo "FAIL $main_py"
258 cat $out
259 fi
260
261 return $status
262}
263
264logs-equal() {
265 local out=$1
266 shift
267
268 mycpp/compare_pairs.py "$@" | tee $out
269}
270
271#
272# shwrap rules
273#
274
275shwrap-py() {
276 ### Part of shell template for Python executables
277
278 local main=$1
279 echo 'PYTHONPATH=$REPO_ROOT:$REPO_ROOT/vendor exec $REPO_ROOT/'$main' "$@"'
280}
281
282shwrap-mycpp() {
283 ### Part of shell template for mycpp executable
284
285 cat <<'EOF'
286MYPYPATH=$1 # e.g. $REPO_ROOT/mycpp
287out=$2
288shift 2
289
290# Modifies $PATH; do not combine
291. build/dev-shell.sh
292
293tmp=$out.tmp # avoid creating partial files
294
295# The command we want to run
296# EXTRA_MYCPP_ARGS is for --stack-root-warn 16, in Soil CI
297set -- python3 mycpp/mycpp_main.py --cc-out $tmp ${EXTRA_MYCPP_ARGS:-} "$@"
298
299# If 'time' is on the system, add timing info. (It's not present on some
300# Debian CI images)
301if which time >/dev/null; then
302 # 'busybox time' supports -f but not --format.
303 set -- \
304 time -f 'MYCPP { elapsed: %e, max_RSS: %M }' -- \
305 "$@"
306fi
307
308MYPYPATH="$MYPYPATH" "$@"
309status=$?
310
311mv $tmp $out
312exit $status
313EOF
314}
315
316shwrap-pea() {
317 ### Part of shell template for pea executable
318
319 cat <<'EOF'
320MYPYPATH=$1 # e.g. $REPO_ROOT/mycpp
321out=$2
322shift 2
323
324tmp=$out.tmp # avoid creating partial files
325
326# copied from build/dev-shell.sh
327
328USER_WEDGE_DIR=~/wedge/oils-for-unix.org
329
330MYPY_VERSION=0.780
331MYPY_WEDGE=$USER_WEDGE_DIR/pkg/mypy/$MYPY_VERSION
332
333PY3_LIBS_VERSION=2023-03-04
334site_packages=lib/python3.10/site-packages
335PY3_LIBS_WEDGE=$USER_WEDGE_DIR/pkg/py3-libs/$PY3_LIBS_VERSION/$site_packages
336
337PYTHONPATH="$REPO_ROOT:$MYPY_WEDGE:$PY3_LIBS_WEDGE" MYPYPATH="$MYPYPATH" \
338 python3 pea/pea_main.py mycpp "$@" > $tmp
339status=$?
340
341mv $tmp $out
342exit $status
343EOF
344}
345
346print-shwrap() {
347 local template=$1
348 local unused=$2
349 shift 2
350
351 cat << 'EOF'
352#!/bin/sh
353REPO_ROOT=$(cd "$(dirname $0)/../.."; pwd)
354. $REPO_ROOT/build/py2.sh
355EOF
356
357 case $template in
358 py)
359 local main=$1 # additional arg
360 shift
361 shwrap-py $main
362 ;;
363 mycpp)
364 shwrap-mycpp
365 ;;
366 pea)
367 shwrap-pea
368 ;;
369 *)
370 die "Invalid template '$template'"
371 ;;
372 esac
373
374 echo
375 echo '# DEPENDS ON:'
376 for dep in "$@"; do
377 echo "# $dep"
378 done
379}
380
381write-shwrap() {
382 ### Create a shell wrapper for a Python tool
383
384 # Key point: if the Python code changes, then the C++ code should be
385 # regenerated and re-compiled
386
387 local unused=$1
388 local stub_out=$2
389
390 print-shwrap "$@" > $stub_out
391 chmod +x $stub_out
392}
393
394# sourced by devtools/bin.sh
395if test $(basename $0) = 'ninja-rules-py.sh'; then
396 "$@"
397fi