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

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