OILS / build / dynamic-deps.sh View on Github | oilshell.org

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