Skip to content

Commit

Permalink
[build] Separate _bin/shwrap/mycpp_main and _bin/shwrap/mycpp_main_so…
Browse files Browse the repository at this point in the history
…uffle

Now we can build mos things without a C++ 17 compiler, which Souffle
requires.

I introduced a new wrapper _bin/shwrap/mycpp_main_souffle, and it
depends on:

- _bin/shwrap/mycpp_main
- _bin/datalog/dataflow - the binary derived from a datalog program
  • Loading branch information
Andy C authored and Andy C committed Dec 6, 2024
1 parent b0107d9 commit fc8a44b
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 34 deletions.
9 changes: 5 additions & 4 deletions bin/NINJA_subgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,15 @@ def NinjaGraph(ru):
('translator', translator),
('preamble', 'cpp/preamble.h'),
]
if translator == 'mycpp-souffle':
variables.append(
('extra_mycpp_opts', '--minimize-stack-roots'))
if translator == 'mycpp':
shwrap_path = '_bin/shwrap/mycpp_main'
else:
shwrap_path = '_bin/shwrap/mycpp_main_souffle'

n.build(outputs,
'gen-oils-for-unix',
deps,
implicit=['_bin/shwrap/mycpp_main', RULES_PY],
implicit=[shwrap_path, RULES_PY],
variables=variables)

if main_name == 'oils_for_unix':
Expand Down
3 changes: 0 additions & 3 deletions build/dynamic-deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,6 @@ write-mycpp() {
| mysort \
| tee $deps

# EXTRA FILE
echo '_bin/datalog/dataflow' >> $deps

echo
echo $dir/*
}
Expand Down
25 changes: 22 additions & 3 deletions build/ninja-rules-py.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ source build/dev-shell.sh # python2 in $PATH
#source devtools/types.sh # typecheck-files
source $REPO_ROOT/test/tsv-lib.sh # time-tsv

die() {
echo "$@" >& 2
exit 1
}

example-main-wrapper() {
### Used by mycpp/examples

Expand Down Expand Up @@ -133,13 +138,13 @@ print-wrap-cc() {

# main() function
case $translator in
mycpp)
mycpp_main|mycpp_main_souffle)
example-main-wrapper $main_module
;;
yaks)
yaks_main)
main-wrapper $main_module
;;
pea)
pea_main)
main-wrapper $main_module
#echo '#include <stdio.h>'
#echo 'int main() { printf("stub\n"); return 1; }'
Expand Down Expand Up @@ -303,6 +308,17 @@ exit $status
EOF
}

shwrap-mycpp-souffle() {
cat <<'EOF'
MYPYPATH=$1 # e.g. $REPO_ROOT/mycpp
out=$2
shift 2
# Add an extra flag, and also depends on _bin/datalog
exec _bin/shwrap/mycpp_main $MYPYPATH $out --minimize-stack-roots "$@"
EOF
}

shwrap-pea() {
### Part of shell template for pea executable

Expand Down Expand Up @@ -353,6 +369,9 @@ EOF
mycpp)
shwrap-mycpp
;;
mycpp-souffle)
shwrap-mycpp-souffle
;;
pea)
shwrap-pea
;;
Expand Down
19 changes: 13 additions & 6 deletions build/ninja_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,21 +518,28 @@ def asdl_library(self,
)

def py_binary(self, main_py, deps_base_dir='_build/NINJA', template='py'):
"""
Wrapper for Python script with dynamically discovered deps
"""Wrapper for Python script with dynamically discovered deps
Args:
template: py, mycpp, or pea
Example:
_bin/shwrap/mycpp_main wraps mycpp/mycpp_main.py
- using dependencies from prebuilt/ninja/mycpp.mycpp_main/deps.txt
- with the 'shwrap-mycpp' template defined in build/ninja-lib.sh
"""
rel_path, _ = os.path.splitext(main_py)
py_module = rel_path.replace(
'/', '.') # asdl/asdl_main.py -> asdl.asdl_main
# asdl/asdl_main.py -> asdl.asdl_main
py_module = rel_path.replace('/', '.')

deps_path = os.path.join(deps_base_dir, py_module, 'deps.txt')
with open(deps_path) as f:
deps = [line.strip() for line in f]

deps.remove(main_py) # raises ValueError if it's not there

basename = os.path.basename(rel_path)
self.n.build('_bin/shwrap/%s' % basename,
shwrap_name = os.path.basename(rel_path)
self.n.build('_bin/shwrap/%s' % shwrap_name,
'write-shwrap', [main_py] + deps,
variables=[('template', template)])
self.n.newline()
Expand Down
52 changes: 37 additions & 15 deletions mycpp/NINJA_subgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@

def DefineTargets(ru):

# Creates _bin/shwrap/mycpp_main
ru.py_binary('mycpp/mycpp_main.py',
deps_base_dir='prebuilt/ninja',
template='mycpp')

# mycpp wrapper that depends on _bin/datalog/dataflow, a binary created
# from Souffle datalog!
ru.n.build(
'_bin/shwrap/mycpp_main_souffle',
'write-shwrap',
implicit=['_bin/shwrap/mycpp_main', '_bin/datalog/dataflow'],
variables=[('template', 'mycpp-souffle')],
)

ru.cc_library(
'//mycpp/runtime',
# Could separate into //mycpp/runtime_{marksweep,bumpleak}
Expand Down Expand Up @@ -170,18 +180,21 @@ def TranslatorSubgraph(ru, translator, ex):
# Note: build/ninja-rules-py.sh writes this 'shwrap'
# It depends on MYPY_WEDGE and PY3_LIBS_WEDGE
translator_wrapper = '_bin/shwrap/pea_main'
base_translator = 'pea'
translate_rule = 'translate-pea'
else:
elif translator == 'mycpp':
translate_rule = 'translate-mycpp'
base_translator = 'mycpp'
translator_wrapper = '_bin/shwrap/mycpp_main'
elif translator == 'mycpp-souffle':
translate_rule = 'translate-mycpp-souffle'
translator_wrapper = '_bin/shwrap/mycpp_main_souffle'
else:
raise AssertionError('translator = %r' % translator)

translator_vars = [
('mypypath', '$NINJA_REPO_ROOT/mycpp:$NINJA_REPO_ROOT/pyext'),
]
if translator == 'mycpp-souffle':
translator_vars.append(('extra_mycpp_opts', '--minimize-stack-roots'))
#if translator == 'mycpp-souffle':
# translator_vars.append(('extra_mycpp_opts', '--minimize-stack-roots'))

n.build(
raw,
Expand All @@ -198,12 +211,17 @@ def TranslatorSubgraph(ru, translator, ex):
main_cc_src = '_gen/mycpp/examples/%s.%s.cc' % (ex, translator)

# Make a translation unit
n.build(main_cc_src,
'wrap-cc',
raw,
implicit=[RULES_PY],
variables=[('name', ex), ('preamble_path', preamble_path),
('translator', base_translator)])
n.build(
main_cc_src,
'wrap-cc',
raw,
implicit=[RULES_PY],
variables=[
('name', ex),
('preamble_path', preamble_path),
# translator is used to determine wrap-cc
('translator', os.path.basename(translator_wrapper))
])

n.newline()

Expand Down Expand Up @@ -260,10 +278,14 @@ def NinjaGraph(ru):
n.newline()

# mycpp and pea have the same interface
n.rule(
'translate-mycpp',
command='_bin/shwrap/mycpp_main $mypypath $out $in $extra_mycpp_opts',
description='mycpp $mypypath $out $in')
n.rule('translate-mycpp',
command='_bin/shwrap/mycpp_main $mypypath $out $in',
description='mycpp $mypypath $out $in')
n.newline()

n.rule('translate-mycpp-souffle',
command='_bin/shwrap/mycpp_main_souffle $mypypath $out $in',
description='mycpp-souffle $mypypath $out $in')
n.newline()

n.rule('translate-pea',
Expand Down
1 change: 0 additions & 1 deletion prebuilt/ninja/mycpp.mycpp_main/deps.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ mycpp/translate.py
mycpp/util.py
mycpp/virtual_pass.py
mycpp/visitor.py
_bin/datalog/dataflow
4 changes: 2 additions & 2 deletions yaks/NINJA_subgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def NinjaGraph(ru):
deps,
implicit=['_bin/shwrap/mycpp_main', RULES_PY],
variables=[('out_prefix', prefix), ('main_name', main_name),
('translator', 'yaks'),
('translator', 'yaks_main'),
('preamble', 'yaks/preamble.h')])

ru.cc_binary(
Expand Down Expand Up @@ -82,7 +82,7 @@ def NinjaGraph(ru):
'wrap-cc', [raw_cc],
implicit=[RULES_PY],
variables=[('name', 'hello'), ('preamble_path', '""'),
('translator', 'yaks')])
('translator', 'yaks_main')])
n.newline()

ru.cc_binary(
Expand Down

0 comments on commit fc8a44b

Please sign in to comment.