| 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 | print-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 |
|
| 90 | wrap-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 |
|
| 100 | task() {
|
| 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 |
|
| 125 | example-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 |
|
| 138 | benchmark-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 |
|
| 154 | MYPY_FLAGS='--strict --no-strict-optional'
|
| 155 | typecheck-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 |
|
| 162 | typecheck() {
|
| 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 |
|
| 191 | logs-equal() {
|
| 192 | local out=$1
|
| 193 | shift
|
| 194 |
|
| 195 | mycpp/compare_pairs.py "$@" | tee $out
|
| 196 | }
|
| 197 |
|
| 198 | #
|
| 199 | # shwrap rules
|
| 200 | #
|
| 201 |
|
| 202 | shwrap-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 |
|
| 209 | shwrap-mycpp() {
|
| 210 | ### Part of shell template for mycpp executable
|
| 211 |
|
| 212 | cat <<'EOF'
|
| 213 | MYPYPATH=$1 # e.g. $REPO_ROOT/mycpp
|
| 214 | out=$2
|
| 215 | shift 2
|
| 216 |
|
| 217 | # Modifies $PATH; do not combine
|
| 218 | . build/dev-shell.sh
|
| 219 |
|
| 220 | tmp=$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
|
| 224 | set -- 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)
|
| 228 | if 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 | "$@"
|
| 233 | fi
|
| 234 |
|
| 235 | MYPYPATH="$MYPYPATH" "$@"
|
| 236 | status=$?
|
| 237 |
|
| 238 | mv $tmp $out
|
| 239 | exit $status
|
| 240 | EOF
|
| 241 | }
|
| 242 |
|
| 243 | shwrap-pea() {
|
| 244 | ### Part of shell template for pea executable
|
| 245 |
|
| 246 | cat <<'EOF'
|
| 247 | MYPYPATH=$1 # e.g. $REPO_ROOT/mycpp
|
| 248 | out=$2
|
| 249 | shift 2
|
| 250 |
|
| 251 | tmp=$out.tmp # avoid creating partial files
|
| 252 |
|
| 253 | # copied from build/dev-shell.sh
|
| 254 |
|
| 255 | USER_WEDGE_DIR=~/wedge/oils-for-unix.org
|
| 256 |
|
| 257 | MYPY_VERSION=0.780
|
| 258 | MYPY_WEDGE=$USER_WEDGE_DIR/pkg/mypy/$MYPY_VERSION
|
| 259 |
|
| 260 | PY3_LIBS_VERSION=2023-03-04
|
| 261 | site_packages=lib/python3.10/site-packages
|
| 262 | PY3_LIBS_WEDGE=$USER_WEDGE_DIR/pkg/py3-libs/$PY3_LIBS_VERSION/$site_packages
|
| 263 |
|
| 264 | PYTHONPATH="$REPO_ROOT:$MYPY_WEDGE:$PY3_LIBS_WEDGE" MYPYPATH="$MYPYPATH" \
|
| 265 | python3 pea/pea_main.py mycpp "$@" > $tmp
|
| 266 | status=$?
|
| 267 |
|
| 268 | mv $tmp $out
|
| 269 | exit $status
|
| 270 | EOF
|
| 271 | }
|
| 272 |
|
| 273 | print-shwrap() {
|
| 274 | local template=$1
|
| 275 | local unused=$2
|
| 276 | shift 2
|
| 277 |
|
| 278 | cat << 'EOF'
|
| 279 | #!/bin/sh
|
| 280 | REPO_ROOT=$(cd "$(dirname $0)/../.."; pwd)
|
| 281 | . $REPO_ROOT/build/py2.sh
|
| 282 | EOF
|
| 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 |
|
| 308 | write-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
|
| 322 | if test $(basename $0) = 'ninja-rules-py.sh'; then
|
| 323 | "$@"
|
| 324 | fi
|