OILS / build / py.sh View on Github | oilshell.org

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