1 | #!/usr/bin/env bash
|
2 | #
|
3 | # Calculate and filter deps of Python apps.
|
4 | #
|
5 | # Usage:
|
6 | # build/dynamic-deps.sh <function name>
|
7 |
|
8 | set -o nounset
|
9 | set -o pipefail
|
10 | set -o errexit
|
11 |
|
12 | REPO_ROOT=$(cd "$(dirname $0)/.."; pwd)
|
13 |
|
14 | source mycpp/common.sh # $MYPY_REPO
|
15 |
|
16 | readonly PY_PATH='.:vendor/'
|
17 |
|
18 | # Temporary
|
19 | readonly DIR=_build/NINJA
|
20 |
|
21 | # In git
|
22 | readonly FILTER_DIR='prebuilt/dynamic-deps'
|
23 |
|
24 | make-egrep() {
|
25 | # match chars until # or space, and line must be non-empty
|
26 | gawk '
|
27 | match($0, /([^# ]*)/, m) {
|
28 | contents = m[0]
|
29 | if (contents) { # skip empty lines
|
30 | print(contents)
|
31 | }
|
32 | }
|
33 | '
|
34 | }
|
35 |
|
36 | write-filters() {
|
37 | ### Write filename filters in the egrep -f format
|
38 |
|
39 | # For ./NINJA-config.sh to use.
|
40 | # This style lets us add comments.
|
41 |
|
42 | # For asdl.asdl_main and other tools
|
43 | make-egrep >$FILTER_DIR/filter-py-tool.txt <<'EOF'
|
44 | __init__.py
|
45 | typing.py # vendor/typing.py isn't imported normally
|
46 | EOF
|
47 |
|
48 | # Don't typecheck these files.
|
49 |
|
50 | make-egrep >$FILTER_DIR/filter-typecheck.txt <<'EOF'
|
51 | __init__.py
|
52 | typing.py
|
53 |
|
54 | # OrderedDict is polymorphic
|
55 | pylib/collections_.py
|
56 |
|
57 | # lots of polymorphic stuff etc.
|
58 | mycpp/mylib.py
|
59 |
|
60 | # TODO: move or remove these
|
61 | tools/deps.py
|
62 | tools/readlink.py
|
63 | EOF
|
64 |
|
65 | # On top of the typecheck filter, exclude these from translation. They are
|
66 | # not inputs to mycpp.
|
67 |
|
68 | make-egrep >$FILTER_DIR/filter-translate.txt <<'EOF'
|
69 | # generated code shouldn't be translated
|
70 | _devbuild/
|
71 | _gen/
|
72 |
|
73 | # definitions that are used by */*_gen.py
|
74 | .*_def\.py
|
75 | .*_spec\.py
|
76 |
|
77 | asdl/py.* # pybase.py ported by hand to C++
|
78 |
|
79 | core/py.* # pyos.py, pyutil.py ported by hand to C++
|
80 | core/optview\.py # core/optview_gen.py
|
81 |
|
82 | data_lang/py.* # pyj8.py
|
83 |
|
84 | frontend/py.*\.py # py_readline.py ported by hand to C++
|
85 | frontend/consts.py # frontend/consts_gen.py
|
86 | frontend/match.py # frontend/lexer_gen.py
|
87 |
|
88 | mycpp/mops.py # Implemented in gc_mops.{h,cC}
|
89 |
|
90 | pgen2/grammar.py
|
91 | pgen2/pnode.py
|
92 |
|
93 | # should be py_path_stat.py, because it's ported by hand to C++
|
94 | pylib/path_stat.py
|
95 |
|
96 | # should be py_bool_stat.py, because it's ported by hand to C++
|
97 | osh/bool_stat.py
|
98 |
|
99 | tea/
|
100 | EOF
|
101 |
|
102 | wc -l $FILTER_DIR/filter-*
|
103 | }
|
104 |
|
105 | repo-filter() {
|
106 | ### Select files from the dynamic_deps.py output
|
107 |
|
108 | # select what's in the repo; eliminating stdlib stuff
|
109 | # eliminate _cache for mycpp running under Python-3.10
|
110 | fgrep -v "$REPO_ROOT/_cache" | fgrep "$REPO_ROOT" | awk '{ print $2 }'
|
111 | }
|
112 |
|
113 | exclude-filter() {
|
114 | ### Exclude repo-relative paths
|
115 |
|
116 | local filter_name=$1
|
117 |
|
118 | egrep -v -f $FILTER_DIR/filter-$filter_name.txt
|
119 | }
|
120 |
|
121 | mysort() {
|
122 | LC_ALL=C sort
|
123 | }
|
124 |
|
125 | #
|
126 | # Programs
|
127 | #
|
128 |
|
129 | py-tool() {
|
130 | local py_module=$1
|
131 |
|
132 | local dir=$DIR/$py_module
|
133 | mkdir -p $dir
|
134 |
|
135 | PYTHONPATH=$PY_PATH /usr/bin/env python2 \
|
136 | build/dynamic_deps.py py-manifest $py_module \
|
137 | > $dir/all-pairs.txt
|
138 |
|
139 | cat $dir/all-pairs.txt | repo-filter | exclude-filter py-tool | mysort \
|
140 | > $dir/deps.txt
|
141 |
|
142 | echo "DEPS $dir/deps.txt"
|
143 | }
|
144 |
|
145 | # Code generators
|
146 | list-gen() {
|
147 | ls */*_gen.py
|
148 | }
|
149 |
|
150 | # mycpp and pea deps are committed to git instead of in _build/NINJA/ because
|
151 | # users might not have Python 3.10
|
152 |
|
153 | write-pea() {
|
154 | # PYTHONPATH=$PY_PATH
|
155 | local module='pea.pea_main'
|
156 | local dir=prebuilt/ninja/$module
|
157 | mkdir -p $dir
|
158 |
|
159 | source build/dev-shell.sh # python3
|
160 |
|
161 | # Can't use vendor/typing.py
|
162 | PYTHONPATH=. python3 \
|
163 | build/dynamic_deps.py py-manifest $module \
|
164 | > $dir/all-pairs.txt
|
165 |
|
166 | cat $dir/all-pairs.txt | repo-filter | mysort | tee $dir/deps.txt
|
167 |
|
168 | echo
|
169 | echo $dir/*
|
170 | }
|
171 |
|
172 | write-mycpp() {
|
173 | local module='mycpp.mycpp_main'
|
174 | local dir=prebuilt/ninja/$module
|
175 | mkdir -p $dir
|
176 |
|
177 | ( source $MYCPP_VENV/bin/activate
|
178 | PYTHONPATH=$REPO_ROOT:$REPO_ROOT/mycpp:$MYPY_REPO maybe-our-python3 \
|
179 | build/dynamic_deps.py py-manifest $module > $dir/all-pairs.txt
|
180 | )
|
181 |
|
182 | cat $dir/all-pairs.txt \
|
183 | | grep -v oilshell/oil_DEPS \
|
184 | | repo-filter \
|
185 | | exclude-filter py-tool \
|
186 | | mysort \
|
187 | | tee $dir/deps.txt
|
188 |
|
189 | echo
|
190 | echo $dir/*
|
191 | }
|
192 |
|
193 | mycpp-example-parse() {
|
194 | ### Manifests for mycpp/examples/parse are committed to git
|
195 |
|
196 | local dir=$DIR/parse
|
197 | mkdir -p $dir
|
198 |
|
199 | PYTHONPATH=$PY_PATH /usr/bin/env python2 \
|
200 | build/dynamic_deps.py py-manifest mycpp.examples.parse \
|
201 | > $dir/all-pairs.txt
|
202 |
|
203 | local ty=mycpp/examples/parse.typecheck.txt
|
204 | local tr=mycpp/examples/parse.translate.txt
|
205 |
|
206 | cat $dir/all-pairs.txt | repo-filter | exclude-filter typecheck | mysort > $ty
|
207 |
|
208 | cat $ty | exclude-filter translate > $tr
|
209 |
|
210 | wc -l $ty $tr
|
211 |
|
212 | #head $ty $tr
|
213 | }
|
214 |
|
215 | pea-hack() {
|
216 | # Leave out help_.py for Soil
|
217 | grep -v '_devbuild/gen/help_meta.py' $DIR/bin.oils_for_unix/typecheck.txt \
|
218 | > pea/oils-typecheck.txt
|
219 | }
|
220 |
|
221 | # Sourced by NINJA-config.sh
|
222 | if test $(basename $0) = 'dynamic-deps.sh'; then
|
223 | "$@"
|
224 | fi
|