| 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 |
|
| 12 | set -o nounset
|
| 13 | set -o pipefail
|
| 14 | set -o errexit
|
| 15 |
|
| 16 | REPO_ROOT=$(cd "$(dirname $0)/.."; pwd)
|
| 17 |
|
| 18 | source build/dev-shell.sh # python2 in $PATH
|
| 19 | #source devtools/types.sh # typecheck-files
|
| 20 | source $REPO_ROOT/test/tsv-lib.sh # time-tsv
|
| 21 |
|
| 22 | die() {
|
| 23 | echo "$@" >& 2
|
| 24 | exit 1
|
| 25 | }
|
| 26 |
|
| 27 | example-main-wrapper() {
|
| 28 | ### Used by mycpp/examples
|
| 29 | local name_namespace=${1:-fib_iter}
|
| 30 |
|
| 31 | cat <<EOF
|
| 32 | int 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 | }
|
| 45 | EOF
|
| 46 | }
|
| 47 |
|
| 48 | main-wrapper() {
|
| 49 | ### Used by oils-for-unix and yaks
|
| 50 | local main_namespace=$1
|
| 51 |
|
| 52 | cat <<EOF
|
| 53 | int 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 | }
|
| 67 | EOF
|
| 68 | }
|
| 69 |
|
| 70 | # Differences between bin/* and mycpp/examples/*
|
| 71 | #
|
| 72 | # - raw files
|
| 73 | # - bin/* does NOT use a Ninja rule; it uses _build/tmp
|
| 74 | # - mycpp/examples/* uses _gen/mycpp/example/foo{,_raw}.mycpp.cc
|
| 75 | # - MYPYPATH
|
| 76 | # - bin/* uses "$REPO_ROOT:$REPO_ROOT/pyext"
|
| 77 | # - mycpp/examples/* uses $NINJA_REPO_ROOT ...
|
| 78 | # - wrapper
|
| 79 | # - main-wrapper vs. example-main-wrapper
|
| 80 |
|
| 81 | mycpp-gen() {
|
| 82 | ### Run mycpp, and then wrap the _raw.cc file, and maybe the _raw.h file
|
| 83 |
|
| 84 | local py_module=$1 # e.g. bin.oils_for_unix
|
| 85 | local shwrap_path=$2 # e.g. _bin/shwrap/mycpp_main
|
| 86 | local out_prefix=$3 # e.g. _gen/bin/oils_for_unix.mycpp
|
| 87 | local preamble=$4 # e.g. cpp/preamble.h
|
| 88 | shift 4 # rest are inputs
|
| 89 |
|
| 90 | # Put raw files in _build/tmp so they're not in the tarball
|
| 91 | local tmp=_build/tmp/$(basename $shwrap_path)
|
| 92 | mkdir -p $tmp
|
| 93 |
|
| 94 | # e.g. bin_hello, bin_oils_for_unix
|
| 95 | local py_id=${py_module//'.'/'_'}
|
| 96 |
|
| 97 | local raw_cc=$tmp/${py_id}_raw.cc
|
| 98 | local cc_out=${out_prefix}.cc
|
| 99 |
|
| 100 | local raw_header=$tmp/${py_id}_raw.h
|
| 101 | local header_out=${out_prefix}.h
|
| 102 |
|
| 103 | local mypypath="$REPO_ROOT:$REPO_ROOT/pyext"
|
| 104 |
|
| 105 | $shwrap_path $mypypath $raw_cc \
|
| 106 | --header-out $raw_header \
|
| 107 | "$@"
|
| 108 |
|
| 109 | #if test -f "$raw_header"; then
|
| 110 | if false; then
|
| 111 | # bin_oils_for_unix -> BIN_OILS_FOR_UNIX_MYCPP_H
|
| 112 | local guard=${py_id^^}_MYCPP_H
|
| 113 |
|
| 114 | { echo "// $header_out: translated from Python by mycpp"
|
| 115 | echo
|
| 116 | echo "#ifndef $guard"
|
| 117 | echo "#define $guard"
|
| 118 |
|
| 119 | cat $raw_header
|
| 120 |
|
| 121 | echo "#endif // $guard"
|
| 122 |
|
| 123 | } > $header_out
|
| 124 | fi
|
| 125 |
|
| 126 | { echo "// $cc_out: translated from Python by mycpp"
|
| 127 |
|
| 128 | #echo '#include "'$header_out'"'
|
| 129 |
|
| 130 | if test -n "$preamble"; then
|
| 131 | echo '#include "'$preamble'"'
|
| 132 | fi
|
| 133 |
|
| 134 | cat $raw_cc
|
| 135 |
|
| 136 | # bin.oils_for_unix -> oils_for_unix
|
| 137 | # mycpp.examples.pea_hello -> pea_hello
|
| 138 | local main_namespace=${py_module##*.}
|
| 139 | main-wrapper $main_namespace
|
| 140 | } > $cc_out
|
| 141 | }
|
| 142 |
|
| 143 | print-wrap-cc() {
|
| 144 | local out=$1
|
| 145 | local main_func=$2
|
| 146 | local main_namespace=$3
|
| 147 | local in=$4
|
| 148 | local preamble_path=$5
|
| 149 |
|
| 150 | echo "// $out - generated from Python source code"
|
| 151 | echo
|
| 152 |
|
| 153 | if test -f "$preamble_path"; then
|
| 154 | echo "#include \"$preamble_path\""
|
| 155 | fi
|
| 156 |
|
| 157 | cat $in
|
| 158 |
|
| 159 | # example-main-wrapper, main-wrapper, etc.
|
| 160 | $main_func $main_namespace
|
| 161 | }
|
| 162 |
|
| 163 | wrap-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 |
|
| 173 | task() {
|
| 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 |
|
| 198 | example-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 |
|
| 211 | benchmark-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 |
|
| 227 | MYPY_FLAGS='--strict --no-strict-optional'
|
| 228 | typecheck-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 |
|
| 235 | typecheck() {
|
| 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 |
|
| 264 | logs-equal() {
|
| 265 | local out=$1
|
| 266 | shift
|
| 267 |
|
| 268 | mycpp/compare_pairs.py "$@" | tee $out
|
| 269 | }
|
| 270 |
|
| 271 | #
|
| 272 | # shwrap rules
|
| 273 | #
|
| 274 |
|
| 275 | shwrap-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 |
|
| 282 | shwrap-mycpp() {
|
| 283 | ### Part of shell template for mycpp executable
|
| 284 |
|
| 285 | cat <<'EOF'
|
| 286 | MYPYPATH=$1 # e.g. $REPO_ROOT/mycpp
|
| 287 | out=$2
|
| 288 | shift 2
|
| 289 |
|
| 290 | # Modifies $PATH; do not combine
|
| 291 | . build/dev-shell.sh
|
| 292 |
|
| 293 | tmp=$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
|
| 297 | set -- 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)
|
| 301 | if 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 | "$@"
|
| 306 | fi
|
| 307 |
|
| 308 | MYPYPATH="$MYPYPATH" "$@"
|
| 309 | status=$?
|
| 310 |
|
| 311 | mv $tmp $out
|
| 312 | exit $status
|
| 313 | EOF
|
| 314 | }
|
| 315 |
|
| 316 | shwrap-pea() {
|
| 317 | ### Part of shell template for pea executable
|
| 318 |
|
| 319 | cat <<'EOF'
|
| 320 | MYPYPATH=$1 # e.g. $REPO_ROOT/mycpp
|
| 321 | out=$2
|
| 322 | shift 2
|
| 323 |
|
| 324 | tmp=$out.tmp # avoid creating partial files
|
| 325 |
|
| 326 | # copied from build/dev-shell.sh
|
| 327 |
|
| 328 | USER_WEDGE_DIR=~/wedge/oils-for-unix.org
|
| 329 |
|
| 330 | MYPY_VERSION=0.780
|
| 331 | MYPY_WEDGE=$USER_WEDGE_DIR/pkg/mypy/$MYPY_VERSION
|
| 332 |
|
| 333 | PY3_LIBS_VERSION=2023-03-04
|
| 334 | site_packages=lib/python3.10/site-packages
|
| 335 | PY3_LIBS_WEDGE=$USER_WEDGE_DIR/pkg/py3-libs/$PY3_LIBS_VERSION/$site_packages
|
| 336 |
|
| 337 | PYTHONPATH="$REPO_ROOT:$MYPY_WEDGE:$PY3_LIBS_WEDGE" MYPYPATH="$MYPYPATH" \
|
| 338 | python3 pea/pea_main.py mycpp "$@" > $tmp
|
| 339 | status=$?
|
| 340 |
|
| 341 | mv $tmp $out
|
| 342 | exit $status
|
| 343 | EOF
|
| 344 | }
|
| 345 |
|
| 346 | print-shwrap() {
|
| 347 | local template=$1
|
| 348 | local unused=$2
|
| 349 | shift 2
|
| 350 |
|
| 351 | cat << 'EOF'
|
| 352 | #!/bin/sh
|
| 353 | REPO_ROOT=$(cd "$(dirname $0)/../.."; pwd)
|
| 354 | . $REPO_ROOT/build/py2.sh
|
| 355 | EOF
|
| 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 |
|
| 381 | write-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
|
| 395 | if test $(basename $0) = 'ninja-rules-py.sh'; then
|
| 396 | "$@"
|
| 397 | fi
|