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

403 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
287preamble_path=$2
288out=$3
289shift 3
290
291# Modifies $PATH; do not combine
292. build/dev-shell.sh
293
294tmp=$out.tmp # avoid creating partial files
295
296# The command we want to run
297# EXTRA_MYCPP_ARGS is for --stack-root-warn 16, in Soil CI
298set -- python3 mycpp/mycpp_main.py \
299 --preamble-path "$preamble_path" \
300 --cc-out $tmp \
301 ${EXTRA_MYCPP_ARGS:-} \
302 "$@"
303
304# If 'time' is on the system, add timing info. (It's not present on some
305# Debian CI images)
306if which time >/dev/null; then
307 # 'busybox time' supports -f but not --format.
308 set -- \
309 time -f 'MYCPP { elapsed: %e, max_RSS: %M }' -- \
310 "$@"
311fi
312
313MYPYPATH="$MYPYPATH" "$@"
314status=$?
315
316mv $tmp $out
317exit $status
318EOF
319}
320
321shwrap-pea() {
322 ### Part of shell template for pea executable
323
324 cat <<'EOF'
325MYPYPATH=$1 # e.g. $REPO_ROOT/mycpp
326preamble_path=$2
327out=$3
328shift 3
329
330tmp=$out.tmp # avoid creating partial files
331
332# copied from build/dev-shell.sh
333
334USER_WEDGE_DIR=~/wedge/oils-for-unix.org
335
336MYPY_VERSION=0.780
337MYPY_WEDGE=$USER_WEDGE_DIR/pkg/mypy/$MYPY_VERSION
338
339PY3_LIBS_VERSION=2023-03-04
340site_packages=lib/python3.10/site-packages
341PY3_LIBS_WEDGE=$USER_WEDGE_DIR/pkg/py3-libs/$PY3_LIBS_VERSION/$site_packages
342
343PYTHONPATH="$REPO_ROOT:$MYPY_WEDGE:$PY3_LIBS_WEDGE" MYPYPATH="$MYPYPATH" \
344 python3 pea/pea_main.py mycpp "$@" > $tmp
345status=$?
346
347mv $tmp $out
348exit $status
349EOF
350}
351
352print-shwrap() {
353 local template=$1
354 local unused=$2
355 shift 2
356
357 cat << 'EOF'
358#!/bin/sh
359REPO_ROOT=$(cd "$(dirname $0)/../.."; pwd)
360. $REPO_ROOT/build/py2.sh
361EOF
362
363 case $template in
364 py)
365 local main=$1 # additional arg
366 shift
367 shwrap-py $main
368 ;;
369 mycpp)
370 shwrap-mycpp
371 ;;
372 pea)
373 shwrap-pea
374 ;;
375 *)
376 die "Invalid template '$template'"
377 ;;
378 esac
379
380 echo
381 echo '# DEPENDS ON:'
382 for dep in "$@"; do
383 echo "# $dep"
384 done
385}
386
387write-shwrap() {
388 ### Create a shell wrapper for a Python tool
389
390 # Key point: if the Python code changes, then the C++ code should be
391 # regenerated and re-compiled
392
393 local unused=$1
394 local stub_out=$2
395
396 print-shwrap "$@" > $stub_out
397 chmod +x $stub_out
398}
399
400# sourced by devtools/bin.sh
401if test $(basename $0) = 'ninja-rules-py.sh'; then
402 "$@"
403fi