diff --git a/SConstruct.py b/SConstruct.py index f18ea9577..36ca05ff4 100644 --- a/SConstruct.py +++ b/SConstruct.py @@ -39,6 +39,7 @@ 'default', 'Component', 'EspIdf', + 'OpenCan', 'Phony', ], ) diff --git a/can/SConscript.py b/can/SConscript.py index c6f0bde5d..9c0dda6fc 100644 --- a/can/SConscript.py +++ b/can/SConscript.py @@ -3,20 +3,8 @@ Import('env') -dbc = env.File('network.dbc') network = env.File('network.yml') - -dbc_emmitter = env.Command( - 'create-dbc.py', - network, - [ - 'opencan-cli compose $SOURCE --dump-python > $TARGET', - # what a crime - f'sed -i -e s%opencan\\.dbc%{dbc.path}%g $TARGET', - ], -) - -dbc = env.Command(dbc, dbc_emmitter, 'python $SOURCE') +dbc = env.OpenCanDbc(network) env.Alias('dbc', dbc) diff --git a/site_scons/site_tools/OpenCan.py b/site_scons/site_tools/OpenCan.py new file mode 100644 index 000000000..c37e11dd0 --- /dev/null +++ b/site_scons/site_tools/OpenCan.py @@ -0,0 +1,57 @@ +import os + + +OUTPUTS = [ + f'opencan_{file}' + for file in [ + 'callbacks.h', + 'rx.c', + 'rx.h', + 'templates.h', + 'tx.c', + 'tx.h', + ] +] + + +def OpenCan(env, network, node, *, GEN_DIR=None): + if GEN_DIR is None: + GEN_DIR = env.Dir('opencan-codegen') + + out = env.Command( + [f'{GEN_DIR.path}/{output}' for output in OUTPUTS], + network, + f'opencan-cli codegen {network.path} {GEN_DIR.path} {node}', + ) + + return out + + +def OpenCanDbc(env, network): + dbc = env.File(os.path.splitext(network.path)[0] + '.dbc') + + dbc_emmitter = env.Command( + 'dbc-emmitter.py', + network, + [ + 'opencan-cli compose $SOURCE --dump-python > $TARGET', + # what a crime + f'sed -i -e s%opencan\\.dbc%{dbc.path}%g $TARGET', + ], + ) + + dbc = env.Command(dbc, dbc_emmitter, 'python $SOURCE') + + return dbc + + +def generate(env): + if env.Detect('OpenCan') and env.Detect('OpenCanDbc'): + return + + env.AddMethod(OpenCan, 'OpenCan') + env.AddMethod(OpenCanDbc, 'OpenCanDbc') + + +def exists(env): + return env.Detect('OpenCan') and env.Detect('OpenCanDbc')