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

224 lines, 88 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
14source mycpp/common.sh # $MYPY_REPO
15
16readonly PY_PATH='.:vendor/'
17
18# Temporary
19readonly DIR=_build/NINJA
20
21# In git
22readonly FILTER_DIR='prebuilt/dynamic-deps'
23
24make-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
36write-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
45typing.py # vendor/typing.py isn't imported normally
46EOF
47
48 # Don't typecheck these files.
49
50 make-egrep >$FILTER_DIR/filter-typecheck.txt <<'EOF'
51__init__.py
52typing.py
53
54# OrderedDict is polymorphic
55pylib/collections_.py
56
57# lots of polymorphic stuff etc.
58mycpp/mylib.py
59
60# TODO: move or remove these
61tools/deps.py
62tools/readlink.py
63EOF
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
77asdl/py.* # pybase.py ported by hand to C++
78
79core/py.* # pyos.py, pyutil.py ported by hand to C++
80core/optview\.py # core/optview_gen.py
81
82data_lang/py.* # pyj8.py
83
84frontend/py.*\.py # py_readline.py ported by hand to C++
85frontend/consts.py # frontend/consts_gen.py
86frontend/match.py # frontend/lexer_gen.py
87
88mycpp/mops.py # Implemented in gc_mops.{h,cC}
89
90pgen2/grammar.py
91pgen2/pnode.py
92
93# should be py_path_stat.py, because it's ported by hand to C++
94pylib/path_stat.py
95
96# should be py_bool_stat.py, because it's ported by hand to C++
97osh/bool_stat.py
98
99tea/
100EOF
101
102 wc -l $FILTER_DIR/filter-*
103}
104
105repo-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
113exclude-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
121mysort() {
122 LC_ALL=C sort
123}
124
125#
126# Programs
127#
128
129py-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
146list-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
153write-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
172write-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
193mycpp-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
215pea-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
222if test $(basename $0) = 'dynamic-deps.sh'; then
223 "$@"
224fi