| 1 | #!/usr/bin/env bash
 | 
| 2 | #
 | 
| 3 | # Build the dev version of Oil on CPython.
 | 
| 4 | # This is in contrast to oils-for-unix and the oil.ovm build.
 | 
| 5 | #
 | 
| 6 | # Usage:
 | 
| 7 | #   build/py.sh <function name>
 | 
| 8 | 
 | 
| 9 | : ${LIB_OSH=stdlib/osh}
 | 
| 10 | source $LIB_OSH/bash-strict.sh
 | 
| 11 | source $LIB_OSH/task-five.sh
 | 
| 12 | 
 | 
| 13 | REPO_ROOT=$(cd "$(dirname $0)/.."; pwd)
 | 
| 14 | source build/common.sh       # log, $CLANGXX
 | 
| 15 | 
 | 
| 16 | if test -z "${IN_NIX_SHELL:-}"; then
 | 
| 17 |   source build/dev-shell.sh  # to run 're2c'
 | 
| 18 | fi
 | 
| 19 | 
 | 
| 20 | export PYTHONPATH='.:vendor/'
 | 
| 21 | 
 | 
| 22 | ubuntu-deps() {
 | 
| 23 |   ### Alias for backward compatility
 | 
| 24 |   build/deps.sh install-ubuntu-packages
 | 
| 25 | }
 | 
| 26 | 
 | 
| 27 | # This is what Python uses on OS X.
 | 
| 28 | #
 | 
| 29 | # https://www.thrysoee.dk/editline/
 | 
| 30 | install-libedit() {
 | 
| 31 |   sudo apt install libedit-dev
 | 
| 32 | }
 | 
| 33 | 
 | 
| 34 | libedit-flags() {
 | 
| 35 |   pkg-config --libs --cflags libedit
 | 
| 36 | }
 | 
| 37 | 
 | 
| 38 | install-py3() {
 | 
| 39 |   pip3 install mypy
 | 
| 40 | }
 | 
| 41 | 
 | 
| 42 | destroy-pip() {
 | 
| 43 |   rm -r -f -v ~/.cache/pip ~/.local/lib/python2.7
 | 
| 44 | }
 | 
| 45 | 
 | 
| 46 | # Needed for the release process, but not the dev process.
 | 
| 47 | # TODO: remove in favor of wedges in deps/
 | 
| 48 | release-ubuntu-deps() {
 | 
| 49 |   # For the release to run test/report.R, you need r-base-core too.
 | 
| 50 |   # cloc is used for line counts
 | 
| 51 |   # valgrind/cachegrind for benchmarks
 | 
| 52 |   sudo apt-get install r-base-core cloc valgrind
 | 
| 53 | }
 | 
| 54 | 
 | 
| 55 | # 3/2021: For installing dplyr on Ubuntu Xenial 16.04 LTS, which has an old R version
 | 
| 56 | # Following these instructions
 | 
| 57 | # https://cloud.r-project.org/bin/linux/ubuntu/README.html
 | 
| 58 | 
 | 
| 59 | # 5/2021: Upgraded to Ubuntu Bionic, which has R 3.4.4.  So it looks like I no
 | 
| 60 | # longer need this.
 | 
| 61 | #
 | 
| 62 | # 2/2023: I need this again because R 3.4.4 is too old for dplyr.
 | 
| 63 | #
 | 
| 64 | # https://cloud.r-project.org/bin/linux/ubuntu/
 | 
| 65 | 
 | 
| 66 | _install-new-r() {
 | 
| 67 |   # update indices
 | 
| 68 |   apt update -qq
 | 
| 69 | 
 | 
| 70 |   # install two helper packages we need
 | 
| 71 |   apt install --no-install-recommends software-properties-common dirmngr
 | 
| 72 | 
 | 
| 73 |   # import the signing key (by Michael Rutter) for these repo
 | 
| 74 |   apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
 | 
| 75 | 
 | 
| 76 |   # add the R 4.0 repo from CRAN -- adjust 'focal' to 'groovy' or 'bionic' as needed
 | 
| 77 | 
 | 
| 78 |   local ubuntu_version
 | 
| 79 |   ubuntu_version=$(lsb_release -cs)
 | 
| 80 |   add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $ubuntu_version-cran40/"
 | 
| 81 | 
 | 
| 82 |   # Hm I had to run this manually and I got R 4.0
 | 
| 83 |   # 2021-04: Hm this had to be run twice
 | 
| 84 |   apt install --no-install-recommends r-base
 | 
| 85 | }
 | 
| 86 | 
 | 
| 87 | install-new-r() {
 | 
| 88 |   sudo $0 _install-new-r "$@"
 | 
| 89 | }
 | 
| 90 | 
 | 
| 91 | const-mypy-gen() {
 | 
| 92 |   local out=_devbuild/gen/id_kind_asdl.py
 | 
| 93 |   frontend/consts_gen.py mypy > $out
 | 
| 94 |   log "  (frontend/consts_gen) -> $out"
 | 
| 95 | 
 | 
| 96 |   out=_devbuild/gen/id_kind.py
 | 
| 97 |   frontend/consts_gen.py py-consts > $out
 | 
| 98 |   log "  (frontend/consts_gen) -> $out"
 | 
| 99 | }
 | 
| 100 | 
 | 
| 101 | option-mypy-gen() {
 | 
| 102 |   local out=_devbuild/gen/option_asdl.py
 | 
| 103 |   frontend/option_gen.py mypy > $out
 | 
| 104 |   log "  (frontend/option_gen) -> $out"
 | 
| 105 | }
 | 
| 106 | 
 | 
| 107 | flag-gen-mypy() {
 | 
| 108 |   local out=_devbuild/gen/arg_types.py
 | 
| 109 |   frontend/flag_gen.py mypy > $out
 | 
| 110 |   log "  (frontend/flag_gen) -> $out"
 | 
| 111 | }
 | 
| 112 | 
 | 
| 113 | # Helper
 | 
| 114 | gen-asdl-py() {
 | 
| 115 |   local asdl_path=$1  # e.g. osh/osh.asdl
 | 
| 116 | 
 | 
| 117 |   local name
 | 
| 118 |   name=$(basename $asdl_path .asdl)
 | 
| 119 | 
 | 
| 120 |   local tmp=_tmp/${name}_asdl.py
 | 
| 121 |   local out=_devbuild/gen/${name}_asdl.py
 | 
| 122 | 
 | 
| 123 |   # abbrev module is optional
 | 
| 124 |   asdl/asdl_main.py mypy "$@" > $tmp
 | 
| 125 | 
 | 
| 126 |   # BUG: MUST BE DONE ATOMICALLY; otherwise the Python interpreter can
 | 
| 127 |   # import an empty file!
 | 
| 128 |   mv $tmp $out
 | 
| 129 | 
 | 
| 130 |   log "$asdl_path -> (asdl_main) -> $out"
 | 
| 131 | }
 | 
| 132 | 
 | 
| 133 | py-codegen() {
 | 
| 134 |   # note: filename must come first
 | 
| 135 |   # hnode.asdl has REQUIRED fields so it's --py-init-N
 | 
| 136 |   gen-asdl-py 'asdl/hnode.asdl' --no-pretty-print-methods --py-init-N 
 | 
| 137 | 
 | 
| 138 |   gen-asdl-py 'frontend/types.asdl'
 | 
| 139 |   # depends on syntax.asdl
 | 
| 140 |   gen-asdl-py 'core/runtime.asdl'
 | 
| 141 |   gen-asdl-py 'core/value.asdl'
 | 
| 142 |   gen-asdl-py 'data_lang/nil8.asdl'
 | 
| 143 |   gen-asdl-py 'data_lang/pretty.asdl'
 | 
| 144 | 
 | 
| 145 |   gen-asdl-py 'tools/find/find.asdl'
 | 
| 146 | 
 | 
| 147 |   const-mypy-gen  # depends on bool_arg_type_e, generates Id_t
 | 
| 148 | 
 | 
| 149 |   # does __import__ of syntax_abbrev.py, which depends on Id.  We could use the
 | 
| 150 |   # AST module later?
 | 
| 151 |   # depends on syntax_asdl
 | 
| 152 |   gen-asdl-py 'frontend/syntax.asdl' 'frontend.syntax_abbrev'
 | 
| 153 | 
 | 
| 154 |   option-mypy-gen
 | 
| 155 |   flag-gen-mypy
 | 
| 156 | 
 | 
| 157 |   # Experiment
 | 
| 158 |   gen-asdl-py 'yaks/yaks.asdl'
 | 
| 159 | 
 | 
| 160 |   # For tests
 | 
| 161 |   gen-asdl-py 'mycpp/examples/expr.asdl'
 | 
| 162 | }
 | 
| 163 | 
 | 
| 164 | py-asdl-examples() {
 | 
| 165 |   # dependency of typed_demo
 | 
| 166 |   gen-asdl-py 'asdl/examples/demo_lib.asdl'
 | 
| 167 |   gen-asdl-py 'asdl/examples/typed_demo.asdl'
 | 
| 168 | 
 | 
| 169 |   gen-asdl-py 'asdl/examples/shared_variant.asdl'
 | 
| 170 |   gen-asdl-py 'asdl/examples/typed_arith.asdl' 'asdl.examples.typed_arith_abbrev'
 | 
| 171 | }
 | 
| 172 | 
 | 
| 173 | oil-cpp() {
 | 
| 174 |   ### STUB for backward compatibility
 | 
| 175 | 
 | 
| 176 |   build/cpp.sh all
 | 
| 177 | }
 | 
| 178 | 
 | 
| 179 | py-ext() {
 | 
| 180 |   ### Build a Python extension
 | 
| 181 | 
 | 
| 182 |   local name=$1
 | 
| 183 |   local setup_script=$2
 | 
| 184 | 
 | 
| 185 |   log "  ($setup_script) -> $name.so"
 | 
| 186 | 
 | 
| 187 |   local arch
 | 
| 188 |   arch=$(uname -m)
 | 
| 189 | 
 | 
| 190 |   # global opts come first
 | 
| 191 |   $setup_script --quiet build_ext --inplace
 | 
| 192 | 
 | 
| 193 |   #file $name.so
 | 
| 194 | }
 | 
| 195 | 
 | 
| 196 | py-ext-test() {
 | 
| 197 |   ### Run a test and log it
 | 
| 198 | 
 | 
| 199 |   # TODO: Fold this into some kind of Ninja test runner?
 | 
| 200 |   # Or just rely on test/unit.sh all?
 | 
| 201 | 
 | 
| 202 |   local test_path=$1  # Or a function
 | 
| 203 |   shift
 | 
| 204 | 
 | 
| 205 |   local log_path=_test/unit/$test_path.log
 | 
| 206 |   mkdir -p $(dirname $log_path)
 | 
| 207 | 
 | 
| 208 |   set +o errexit
 | 
| 209 |   $test_path "$@" >$log_path 2>&1
 | 
| 210 |   local status=$?
 | 
| 211 |   set -o errexit
 | 
| 212 | 
 | 
| 213 |   if test $status -eq 0; then
 | 
| 214 |     log "OK $log_path"
 | 
| 215 |   else
 | 
| 216 |     echo
 | 
| 217 |     cat $log_path
 | 
| 218 |     echo
 | 
| 219 |     die "FAIL $log_path"
 | 
| 220 |   fi
 | 
| 221 | }
 | 
| 222 | 
 | 
| 223 | pylibc() {
 | 
| 224 |   rm -f libc.so
 | 
| 225 | 
 | 
| 226 |   py-ext libc pyext/setup_libc.py
 | 
| 227 | 
 | 
| 228 |   # Skip unit tests on Alpine for now
 | 
| 229 |   # musl libc doesn't have extended globs
 | 
| 230 |   if uname -a | grep -F Alpine; then
 | 
| 231 |     return
 | 
| 232 |   fi
 | 
| 233 | 
 | 
| 234 |   py-ext-test pyext/libc_test.py "$@"
 | 
| 235 | }
 | 
| 236 | 
 | 
| 237 | fanos() {
 | 
| 238 |   rm -f fanos.so
 | 
| 239 | 
 | 
| 240 |   py-ext fanos pyext/setup_fanos.py
 | 
| 241 |   py-ext-test pyext/fanos_test.py "$@"
 | 
| 242 | }
 | 
| 243 | 
 | 
| 244 | fastfunc() {
 | 
| 245 |   rm -f fastfunc.so
 | 
| 246 | 
 | 
| 247 |   py-ext fastfunc pyext/setup_fastfunc.py
 | 
| 248 |   py-ext-test pyext/fastfunc_test.py "$@"
 | 
| 249 | }
 | 
| 250 | 
 | 
| 251 | #
 | 
| 252 | # For frontend/match.py
 | 
| 253 | #
 | 
| 254 | 
 | 
| 255 | lexer-gen() { frontend/lexer_gen.py "$@"; }
 | 
| 256 | 
 | 
| 257 | print-regex() { lexer-gen print-regex; }
 | 
| 258 | print-all() { lexer-gen print-all; }
 | 
| 259 | 
 | 
| 260 | # Structure:
 | 
| 261 | #
 | 
| 262 | # _gen
 | 
| 263 | #   frontend/
 | 
| 264 | #     id.asdl_c.h
 | 
| 265 | #     types.asdl_c.h
 | 
| 266 | #     match.re2c.h
 | 
| 267 | # _build/
 | 
| 268 | #   tmp/
 | 
| 269 | #     frontend/
 | 
| 270 | #       match.re2c.in
 | 
| 271 | #     bin/
 | 
| 272 | #       oils_for_unix_raw.mycpp.cc
 | 
| 273 | 
 | 
| 274 | # re2c native.
 | 
| 275 | osh-lex-gen-native() {
 | 
| 276 |   local in=$1
 | 
| 277 |   local out=$2
 | 
| 278 |   # Turn on all warnings and make them native.
 | 
| 279 |   # The COMMENT state can match an empty string at the end of a line, e.g.
 | 
| 280 |   # '#\n'.  So we have to turn that warning off.
 | 
| 281 |   re2c -W -Wno-match-empty-string -Werror -o $out $in
 | 
| 282 | }
 | 
| 283 | 
 | 
| 284 | fastmatch() {
 | 
| 285 |   local gen_dir=_gen/frontend
 | 
| 286 |   mkdir -p _gen/_tmp $gen_dir
 | 
| 287 | 
 | 
| 288 |   # C version of frontend/types.asdl
 | 
| 289 |   local out=$gen_dir/types.asdl_c.h
 | 
| 290 |   asdl/asdl_main.py c frontend/types.asdl "$@" > $out
 | 
| 291 |   log "  (asdl_main c) -> $out"
 | 
| 292 | 
 | 
| 293 |   # C version of id_kind
 | 
| 294 |   local out=$gen_dir/id_kind.asdl_c.h
 | 
| 295 |   frontend/consts_gen.py c > $out
 | 
| 296 |   log "  (frontend/consts_gen c) -> $out"
 | 
| 297 | 
 | 
| 298 |   # Fast matcher
 | 
| 299 |   local tmp=_gen/_tmp/match.re2c-input.h
 | 
| 300 |   local out=_gen/frontend/match.re2c.h
 | 
| 301 |   lexer-gen c > $tmp
 | 
| 302 |   log "  (lexer_gen) -> $tmp"
 | 
| 303 | 
 | 
| 304 |   osh-lex-gen-native $tmp $out
 | 
| 305 |   log "$tmp -> (re2c) -> $out"
 | 
| 306 | }
 | 
| 307 | 
 | 
| 308 | fastlex() {
 | 
| 309 |   fastmatch
 | 
| 310 | 
 | 
| 311 |   # Why do we need this?  It gets stale otherwise.
 | 
| 312 |   rm -f fastlex.so
 | 
| 313 | 
 | 
| 314 |   py-ext fastlex pyext/setup_fastlex.py
 | 
| 315 |   py-ext-test pyext/fastlex_test.py
 | 
| 316 | }
 | 
| 317 | 
 | 
| 318 | line-input() {
 | 
| 319 |   # Why do we need this?  It gets stale otherwise.
 | 
| 320 |   rm -f line_input.so
 | 
| 321 | 
 | 
| 322 |   py-ext line_input pyext/setup_line_input.py
 | 
| 323 |   py-ext-test pyext/line_input_test.py
 | 
| 324 | }
 | 
| 325 | 
 | 
| 326 | posix_() {
 | 
| 327 |   rm -f posix_.so
 | 
| 328 | 
 | 
| 329 |   py-ext posix_ pyext/setup_posix.py
 | 
| 330 |   py-ext-test pyext/posix_test.py
 | 
| 331 | }
 | 
| 332 | 
 | 
| 333 | py-source() {
 | 
| 334 |   ### Generate Python source code
 | 
| 335 | 
 | 
| 336 |   mkdir -p _tmp _devbuild/gen
 | 
| 337 | 
 | 
| 338 |   # need -r because Python 3 puts a __pycache__ here
 | 
| 339 |   log 'Removing _devbuild/gen/*'
 | 
| 340 |   rm -r -f _devbuild/gen/*
 | 
| 341 | 
 | 
| 342 |   # So modules are importable.
 | 
| 343 |   touch _devbuild/__init__.py  _devbuild/gen/__init__.py
 | 
| 344 | 
 | 
| 345 |   py-codegen  # depends on Id
 | 
| 346 | 
 | 
| 347 |   # Only for testing.
 | 
| 348 |   py-asdl-examples
 | 
| 349 | 
 | 
| 350 |   # Needed on Travis.
 | 
| 351 |   ysh-grammar
 | 
| 352 |   find-grammar
 | 
| 353 |   demo-grammar  # for mycpp/examples/pgen2_demo
 | 
| 354 | }
 | 
| 355 | 
 | 
| 356 | # No fastlex, because we don't want to require re2c installation.
 | 
| 357 | py-extensions() {
 | 
| 358 |   pylibc
 | 
| 359 |   line-input
 | 
| 360 |   posix_
 | 
| 361 |   fanos
 | 
| 362 |   fastfunc
 | 
| 363 | }
 | 
| 364 | 
 | 
| 365 | minimal() {
 | 
| 366 |   build/stamp.sh write-git-commit
 | 
| 367 | 
 | 
| 368 |   py-source
 | 
| 369 |   py-extensions
 | 
| 370 | 
 | 
| 371 |   cat <<EOF
 | 
| 372 | 
 | 
| 373 | *****
 | 
| 374 | '$0 minimal' succeeded
 | 
| 375 | 
 | 
| 376 |   It allows you to run and modify Oil quickly, but the lexer will be slow and
 | 
| 377 |   the help builtin won't work.
 | 
| 378 | 
 | 
| 379 | '$0 all' requires re2c and libcmark.so.  (Issue #513 is related, ask
 | 
| 380 | on #oil-dev)
 | 
| 381 | *****
 | 
| 382 | EOF
 | 
| 383 | }
 | 
| 384 | 
 | 
| 385 | ysh-grammar() {
 | 
| 386 |   mkdir -p _gen/ysh
 | 
| 387 |   touch _gen/__init__.py _gen/ysh/__init__.py
 | 
| 388 | 
 | 
| 389 |   ysh/grammar_gen.py py ysh/grammar.pgen2 _devbuild/gen
 | 
| 390 | }
 | 
| 391 | 
 | 
| 392 | find-grammar() {
 | 
| 393 |   ysh/grammar_gen.py py tools/find/find.pgen2 _devbuild/gen
 | 
| 394 | }
 | 
| 395 | 
 | 
| 396 | demo-grammar() {
 | 
| 397 |   ysh/grammar_gen.py py mycpp/examples/arith.pgen2 _devbuild/gen
 | 
| 398 | }
 | 
| 399 | 
 | 
| 400 | time-helper() {
 | 
| 401 |   local out=${1:-_devbuild/bin/time-helper}
 | 
| 402 |   local in=benchmarks/time-helper.c
 | 
| 403 | 
 | 
| 404 |   mkdir -p $(dirname $out)
 | 
| 405 | 
 | 
| 406 |   cc -std=c99 -Wall -o $out $in
 | 
| 407 |   log "  CC $in"
 | 
| 408 | }
 | 
| 409 | 
 | 
| 410 | all() {
 | 
| 411 |   rm -f *.so  # 12/2019: to clear old symlinks, maybe get rid of
 | 
| 412 | 
 | 
| 413 |   build/stamp.sh write-git-commit
 | 
| 414 | 
 | 
| 415 |   py-source
 | 
| 416 |   py-extensions  # no re2c
 | 
| 417 | 
 | 
| 418 |   # requires re2c: deps/from-tar.sh layer-re2c
 | 
| 419 |   fastlex
 | 
| 420 |   time-helper
 | 
| 421 | 
 | 
| 422 |   # help topics and chapter links are extracted from doc/ref
 | 
| 423 |   build/doc.sh all-ref
 | 
| 424 | }
 | 
| 425 | 
 | 
| 426 | gitpod-minimal() {
 | 
| 427 |   ubuntu-deps '-y'  # skip prompt
 | 
| 428 |   minimal 
 | 
| 429 |   test/spec.sh smoke
 | 
| 430 | 
 | 
| 431 |   set -x
 | 
| 432 |   bin/osh -c 'echo hi'
 | 
| 433 | }
 | 
| 434 | 
 | 
| 435 | if test $(basename $0) = 'py.sh'; then
 | 
| 436 |   task-five "$@"
 | 
| 437 | fi
 |