mirror of
https://github.com/qmk/qmk_firmware.git
synced 2024-12-04 00:45:13 +00:00
Refactor inline_generator to template
This commit is contained in:
parent
7703f1edc5
commit
64c5531be6
@ -1,8 +1,25 @@
|
||||
{{ constants.GPL2_HEADER_C_LIKE }}
|
||||
{{ constants.GENERATED_HEADER_C_LIKE }}
|
||||
|
||||
{% macro route_conditions(route_stack) %}
|
||||
{% set conditions = [] %}
|
||||
{% for data in route_stack %}
|
||||
{% if 'enable_if_preprocessor' in data %}
|
||||
{{ conditions.append(data.enable_if_preprocessor) or '' }}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
|
||||
{% if conditions %}
|
||||
#if ({{ conditions | join(' && ') }})
|
||||
{% endif %}
|
||||
{{ caller() }}
|
||||
{%- if conditions %}
|
||||
#endif // ({{ conditions | join(' && ') }})
|
||||
{% endif %}
|
||||
{% endmacro %}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Full XAP {{ xap.version }} definitions
|
||||
// Broadcast
|
||||
|
||||
{% for message_id,data in xap.broadcast_messages.messages | dictsort %}
|
||||
{% if 'return_type' in data %}
|
||||
@ -12,24 +29,116 @@ void {{ xap.broadcast_messages.define_prefix | lower }}_{{ data.define | to_snak
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Decl
|
||||
|
||||
{% macro export_route_declaration(container) %}
|
||||
{% if 'routes' in container %}
|
||||
{% for route, data in container.routes | dictsort %}
|
||||
{% if 'return_execute' in data %}
|
||||
bool xap_respond_{{ data.return_execute }}(xap_token_t token, const uint8_t *data, size_t data_len);
|
||||
{% endif %}
|
||||
{{ export_route_declaration(data) }}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% endmacro %}
|
||||
|
||||
{{ export_route_declaration(xap) }}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Data
|
||||
|
||||
{% macro export_route_data(prefix, container, route_stack) %}
|
||||
{% set this_route_stack = route_stack.copy() %}
|
||||
{{ this_route_stack.append(container) or '' }}
|
||||
{% if 'routes' in container %}
|
||||
{% for route, data in container.routes | dictsort %}
|
||||
{% set this_prefix_uc = (prefix + '_' + data.define) | upper %}
|
||||
{% set this_prefix_lc = this_prefix_uc | lower %}
|
||||
{% if 'return_constant' in data %}
|
||||
{% if data.return_type == 'struct' %}
|
||||
{% call route_conditions(this_route_stack) %}
|
||||
static const {{ this_prefix_lc }}_t {{ this_prefix_lc }}_data PROGMEM = {
|
||||
{% for member in data.return_constant %}
|
||||
{{ member }},
|
||||
{% endfor %}
|
||||
};
|
||||
{% endcall %}
|
||||
{% elif data.return_type == 'string' %}
|
||||
{% call route_conditions(this_route_stack) %}
|
||||
static const char {{ this_prefix_lc }}_str[] PROGMEM = {{ data.return_constant }};
|
||||
{% endcall %}
|
||||
{% else %}
|
||||
{% call route_conditions(this_route_stack) %}
|
||||
static const {{ data.return_type | type_to_c_before }} {{ this_prefix_lc }}_data PROGMEM = {{ data.return_constant }};
|
||||
{% endcall %}
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
{{ export_route_data(this_prefix_lc, data, this_route_stack) }}
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% endmacro %}
|
||||
|
||||
{{ export_route_data('XAP_ROUTE', xap, []) }}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Routes
|
||||
|
||||
{% macro append_routing_table(prefix, container, route_stack) %}
|
||||
{% set this_route_stack = route_stack.copy() %}
|
||||
{{ this_route_stack.append(container) }}
|
||||
{% set stack_names = this_route_stack | map(attribute='name') | join(', ') %}
|
||||
Stack names: {{ stack_names }}
|
||||
{{ this_route_stack.append(container) or '' }}
|
||||
{% if 'routes' in container %}
|
||||
{% for route, data in container.routes | dictsort %}
|
||||
{% set this_prefix_uc = (prefix + '_' + data.define) | upper %}
|
||||
{% set this_prefix_lc = this_prefix_uc | lower %}
|
||||
{{ append_routing_table(this_prefix_lc, data, this_route_stack) }}
|
||||
Inner route prefix for {{ prefix }}: {{ this_prefix_lc }}
|
||||
{% endfor %}
|
||||
{% call route_conditions(this_route_stack) %}
|
||||
static const xap_route_t {{ prefix | lower}}_table[] PROGMEM = {
|
||||
{% for route, data in container.routes | dictsort %}
|
||||
{% set inner_route_stack = this_route_stack.copy() %}
|
||||
{{ inner_route_stack.append(data) or '' }}
|
||||
{% if 'permissions' in data %}
|
||||
{% set secure_status = 'ROUTE_PERMISSIONS_SECURE' %}
|
||||
{% else %}
|
||||
{% set secure_status = 'ROUTE_PERMISSIONS_INSECURE' %}
|
||||
{% endif %}
|
||||
{% call route_conditions(inner_route_stack) %}
|
||||
[{{ prefix | upper }}_{{ data.define }}] = {
|
||||
{% if 'routes' in data %}
|
||||
.flags = {
|
||||
.type = XAP_ROUTE,
|
||||
.secure = {{ secure_status }},
|
||||
},
|
||||
.child_routes = {{ prefix | lower }}_{{ data.define | lower }}_table,
|
||||
.child_routes_len = sizeof({{ prefix | lower }}_{{ data.define | lower }}_table)/sizeof(xap_route_t),
|
||||
{% elif 'return_execute' in data %}
|
||||
.flags = {
|
||||
.type = XAP_EXECUTE,
|
||||
.secure = {{ secure_status }},
|
||||
},
|
||||
.handler = xap_respond_{{ data.return_execute | lower }},
|
||||
{% elif 'return_constant' in data and data.return_type == 'string' %}
|
||||
.flags = {
|
||||
.type = XAP_CONST_MEM,
|
||||
.secure = {{ secure_status }},
|
||||
},
|
||||
.const_data = {{ prefix | lower }}_{{ data.define | lower }}_str,
|
||||
.const_data_len = sizeof({{ prefix | lower }}_{{ data.define | lower }}_str) - 1,
|
||||
{% elif 'return_constant' in data %}
|
||||
.flags = {
|
||||
.type = XAP_CONST_MEM,
|
||||
.secure = {{ secure_status }},
|
||||
},
|
||||
.const_data = &{{ prefix | lower }}_{{ data.define | lower }}_data,
|
||||
.const_data_len = sizeof({{ prefix | lower }}_{{ data.define | lower }}_data),
|
||||
{% endif %}
|
||||
},
|
||||
{% endcall %}
|
||||
{% endfor %}
|
||||
};
|
||||
{% endcall %}
|
||||
{% endif %}
|
||||
Prefix: {{ prefix }}
|
||||
{% endmacro %}
|
||||
|
||||
{{ append_routing_table("xap_route", xap, []) }}
|
||||
|
||||
#if 0
|
||||
{{ xap | tojson(indent=4) }}
|
||||
#endif
|
@ -6,7 +6,6 @@ from qmk.path import normpath
|
||||
from qmk.keyboard import keyboard_completer, keyboard_folder
|
||||
from qmk.xap.common import render_xap_output, merge_xap_defs
|
||||
from qmk.xap.gen_firmware.blob_generator import generate_blob
|
||||
from qmk.xap.gen_firmware.inline_generator import generate_inline
|
||||
|
||||
|
||||
@cli.argument('-o', '--output', type=normpath, help='File to write to')
|
||||
@ -26,10 +25,8 @@ def xap_generate_qmk_inc(cli):
|
||||
cli.subcommands['xap-generate-qmk-inc'].print_help()
|
||||
return False
|
||||
|
||||
generate_inline(cli.args.output, cli.args.keyboard, cli.args.keymap)
|
||||
|
||||
defs = merge_xap_defs(cli.args.keyboard, cli.args.keymap)
|
||||
with open(normpath(str(cli.args.output.resolve()) + '.generated.j2.c'), 'w', encoding='utf-8') as out_file:
|
||||
with open(cli.args.output, 'w', encoding='utf-8') as out_file:
|
||||
r = render_xap_output('firmware', 'xap_generated.inl.j2', defs, keyboard=cli.args.keyboard, keymap=cli.args.keymap)
|
||||
while r.find('\n\n\n') != -1:
|
||||
r = r.replace('\n\n\n', '\n\n')
|
||||
|
@ -172,17 +172,3 @@ def merge_xap_defs(kb, km):
|
||||
exit(1)
|
||||
|
||||
return defs
|
||||
|
||||
|
||||
def route_conditions(route_stack):
|
||||
"""Handles building the C preprocessor conditional based on the current route.
|
||||
"""
|
||||
conditions = []
|
||||
for route in route_stack:
|
||||
if 'enable_if_preprocessor' in route:
|
||||
conditions.append(route['enable_if_preprocessor'])
|
||||
|
||||
if len(conditions) == 0:
|
||||
return None
|
||||
|
||||
return "(" + ' && '.join([f'({c})' for c in conditions]) + ")"
|
||||
|
@ -1,298 +0,0 @@
|
||||
"""This script generates the XAP protocol generated header to be compiled into QMK.
|
||||
"""
|
||||
from qmk.casing import to_snake
|
||||
from qmk.commands import dump_lines
|
||||
from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
|
||||
from qmk.xap.common import merge_xap_defs, route_conditions
|
||||
|
||||
|
||||
def _get_c_type(xap_type):
|
||||
if xap_type == 'bool':
|
||||
return 'bool'
|
||||
elif xap_type == 'u8':
|
||||
return 'uint8_t'
|
||||
elif xap_type == 'u16':
|
||||
return 'uint16_t'
|
||||
elif xap_type == 'u32':
|
||||
return 'uint32_t'
|
||||
elif xap_type == 'u64':
|
||||
return 'uint64_t'
|
||||
elif xap_type == 'struct':
|
||||
return 'struct'
|
||||
elif xap_type == 'string':
|
||||
return 'const char *'
|
||||
return 'unknown'
|
||||
|
||||
|
||||
def _get_c_size(xap_type):
|
||||
if xap_type == 'u8':
|
||||
return 'sizeof(uint8_t)'
|
||||
elif xap_type == 'u16':
|
||||
return 'sizeof(uint16_t)'
|
||||
elif xap_type == 'u32':
|
||||
return 'sizeof(uint32_t)'
|
||||
elif xap_type == 'u64':
|
||||
return 8
|
||||
elif xap_type == 'u8[32]':
|
||||
return 32
|
||||
return 0
|
||||
|
||||
|
||||
def _get_route_type(container):
|
||||
if 'routes' in container:
|
||||
return 'XAP_ROUTE'
|
||||
elif 'return_execute' in container:
|
||||
return 'XAP_EXECUTE'
|
||||
elif 'return_value' in container:
|
||||
if container['return_type'] == 'u8':
|
||||
return 'XAP_VALUE'
|
||||
elif 'return_constant' in container:
|
||||
if container['return_type'] == 'u8':
|
||||
return 'XAP_CONST_MEM'
|
||||
elif container['return_type'] == 'u16':
|
||||
return 'XAP_CONST_MEM'
|
||||
elif container['return_type'] == 'u32':
|
||||
return 'XAP_CONST_MEM'
|
||||
elif container['return_type'] == 'u64':
|
||||
return 'XAP_CONST_MEM'
|
||||
elif container['return_type'] == 'struct':
|
||||
return 'XAP_CONST_MEM'
|
||||
elif container['return_type'] == 'string':
|
||||
return 'XAP_CONST_MEM'
|
||||
elif 'return_getter' in container:
|
||||
if container['return_type'] == 'u32':
|
||||
return 'XAP_GETTER'
|
||||
return 'UNSUPPORTED'
|
||||
|
||||
|
||||
def _append_routing_table_declaration(lines, container, container_id, route_stack):
|
||||
route_stack.append(container)
|
||||
|
||||
route_name = to_snake('_'.join([r['define'] for r in route_stack]))
|
||||
|
||||
condition = route_conditions(route_stack)
|
||||
if condition:
|
||||
lines.append(f'#if {condition}')
|
||||
|
||||
if 'routes' in container:
|
||||
pass
|
||||
elif 'return_execute' in container:
|
||||
execute = container['return_execute']
|
||||
lines.append(f'bool xap_respond_{execute}(xap_token_t token, const uint8_t *data, size_t data_len);')
|
||||
|
||||
# elif 'return_value' in container:
|
||||
# value = container['return_value']
|
||||
# return_type = container['return_type']
|
||||
# lines.append('')
|
||||
# lines.append(f'{_get_c_type(return_type)} {value} = 0;')
|
||||
|
||||
elif 'return_constant' in container:
|
||||
|
||||
if container['return_type'] == 'u8':
|
||||
constant = container['return_constant']
|
||||
lines.append('')
|
||||
lines.append(f'static const uint8_t {route_name}_data PROGMEM = {constant};')
|
||||
|
||||
elif container['return_type'] == 'u16':
|
||||
constant = container['return_constant']
|
||||
lines.append('')
|
||||
lines.append(f'static const uint16_t {route_name}_data PROGMEM = {constant};')
|
||||
|
||||
elif container['return_type'] == 'u32':
|
||||
constant = container['return_constant']
|
||||
lines.append('')
|
||||
lines.append(f'static const uint32_t {route_name}_data PROGMEM = {constant};')
|
||||
|
||||
elif container['return_type'] == 'u64':
|
||||
constant = container['return_constant']
|
||||
lines.append('')
|
||||
lines.append(f'static const uint64_t {route_name}_data PROGMEM = {constant};')
|
||||
|
||||
elif container['return_type'] == 'struct':
|
||||
lines.append('')
|
||||
lines.append(f'static const {route_name}_t {route_name}_data PROGMEM = {{')
|
||||
|
||||
for constant in container['return_constant']:
|
||||
lines.append(f' {constant},')
|
||||
|
||||
lines.append('};')
|
||||
|
||||
elif container['return_type'] == 'string':
|
||||
constant = container['return_constant']
|
||||
lines.append('')
|
||||
lines.append(f'static const char {route_name}_str[] PROGMEM = {constant};')
|
||||
|
||||
elif 'return_getter' in container:
|
||||
|
||||
if container['return_type'] == 'u32':
|
||||
lines.append('')
|
||||
lines.append(f'extern uint32_t {route_name}_getter(void);')
|
||||
|
||||
elif container['return_type'] == 'struct':
|
||||
pass
|
||||
|
||||
if condition:
|
||||
lines.append(f'#endif // {condition}')
|
||||
lines.append('')
|
||||
|
||||
route_stack.pop()
|
||||
|
||||
|
||||
def _append_routing_table_entry_flags(lines, container, container_id, route_stack):
|
||||
pem_map = {
|
||||
None: 'ROUTE_PERMISSIONS_INSECURE',
|
||||
'secure': 'ROUTE_PERMISSIONS_SECURE',
|
||||
}
|
||||
|
||||
is_secure = pem_map[container.get('permissions', None)]
|
||||
|
||||
lines.append(' .flags = {')
|
||||
lines.append(f' .type = {_get_route_type(container)},')
|
||||
lines.append(f' .secure = {is_secure},')
|
||||
lines.append(' },')
|
||||
|
||||
|
||||
def _append_routing_table_entry_route(lines, container, container_id, route_stack):
|
||||
route_name = to_snake('_'.join([r['define'] for r in route_stack]))
|
||||
lines.append(f' .child_routes = {route_name}_table,')
|
||||
lines.append(f' .child_routes_len = sizeof({route_name}_table)/sizeof(xap_route_t),')
|
||||
|
||||
|
||||
def _append_routing_table_entry_execute(lines, container, container_id, route_stack):
|
||||
value = container['return_execute']
|
||||
lines.append(f' .handler = xap_respond_{value},')
|
||||
|
||||
|
||||
def _append_routing_table_entry_value(lines, container, container_id, route_stack):
|
||||
value = container['return_value']
|
||||
lines.append(f' .const_data = &{value},')
|
||||
lines.append(f' .const_data_len = sizeof({value}),')
|
||||
|
||||
|
||||
def _append_routing_table_entry_u32getter(lines, container, container_id, route_stack):
|
||||
route_name = to_snake('_'.join([r['define'] for r in route_stack]))
|
||||
lines.append(f' .u32getter = &{route_name}_getter,')
|
||||
|
||||
|
||||
def _append_routing_table_entry_const_data(lines, container, container_id, route_stack):
|
||||
route_name = to_snake('_'.join([r['define'] for r in route_stack]))
|
||||
lines.append(f' .const_data = &{route_name}_data,')
|
||||
lines.append(f' .const_data_len = sizeof({route_name}_data),')
|
||||
|
||||
|
||||
def _append_routing_table_entry_string(lines, container, container_id, route_stack):
|
||||
route_name = to_snake('_'.join([r['define'] for r in route_stack]))
|
||||
lines.append(f' .const_data = {route_name}_str,')
|
||||
lines.append(f' .const_data_len = sizeof({route_name}_str) - 1,')
|
||||
|
||||
|
||||
def _append_routing_table_entry(lines, container, container_id, route_stack):
|
||||
route_stack.append(container)
|
||||
route_name = '_'.join([r['define'] for r in route_stack])
|
||||
condition = route_conditions(route_stack)
|
||||
|
||||
if condition:
|
||||
lines.append(f'#if {condition}')
|
||||
|
||||
lines.append(f' [{route_name}] = {{')
|
||||
|
||||
_append_routing_table_entry_flags(lines, container, container_id, route_stack)
|
||||
if 'routes' in container:
|
||||
_append_routing_table_entry_route(lines, container, container_id, route_stack)
|
||||
elif 'return_execute' in container:
|
||||
_append_routing_table_entry_execute(lines, container, container_id, route_stack)
|
||||
elif 'return_value' in container:
|
||||
_append_routing_table_entry_value(lines, container, container_id, route_stack)
|
||||
elif 'return_constant' in container:
|
||||
if container['return_type'] == 'u8':
|
||||
_append_routing_table_entry_const_data(lines, container, container_id, route_stack)
|
||||
elif container['return_type'] == 'u16':
|
||||
_append_routing_table_entry_const_data(lines, container, container_id, route_stack)
|
||||
elif container['return_type'] == 'u32':
|
||||
_append_routing_table_entry_const_data(lines, container, container_id, route_stack)
|
||||
elif container['return_type'] == 'u64':
|
||||
_append_routing_table_entry_const_data(lines, container, container_id, route_stack)
|
||||
elif container['return_type'] == 'struct':
|
||||
_append_routing_table_entry_const_data(lines, container, container_id, route_stack)
|
||||
elif container['return_type'] == 'string':
|
||||
_append_routing_table_entry_string(lines, container, container_id, route_stack)
|
||||
elif 'return_getter' in container:
|
||||
if container['return_type'] == 'u32':
|
||||
_append_routing_table_entry_u32getter(lines, container, container_id, route_stack)
|
||||
|
||||
lines.append(' },')
|
||||
|
||||
if condition:
|
||||
lines.append(f'#endif // {condition}')
|
||||
|
||||
route_stack.pop()
|
||||
|
||||
|
||||
def _append_routing_tables(lines, container, container_id=None, route_stack=None):
|
||||
"""Handles building the list of the XAP routes, combining parent and child names together, as well as the route number.
|
||||
"""
|
||||
if route_stack is None:
|
||||
route_stack = [container]
|
||||
else:
|
||||
route_stack.append(container)
|
||||
|
||||
route_name = to_snake('_'.join([r['define'] for r in route_stack]))
|
||||
condition = route_conditions(route_stack)
|
||||
|
||||
if 'routes' in container:
|
||||
for route_id in container['routes']:
|
||||
route = container['routes'][route_id]
|
||||
_append_routing_tables(lines, route, route_id, route_stack)
|
||||
|
||||
for route_id in container['routes']:
|
||||
route = container['routes'][route_id]
|
||||
_append_routing_table_declaration(lines, route, route_id, route_stack)
|
||||
|
||||
lines.append('')
|
||||
if condition:
|
||||
lines.append(f'#if {condition}')
|
||||
|
||||
lines.append(f'static const xap_route_t {route_name}_table[] PROGMEM = {{')
|
||||
|
||||
for route_id in container['routes']:
|
||||
route = container['routes'][route_id]
|
||||
_append_routing_table_entry(lines, route, route_id, route_stack)
|
||||
|
||||
lines.append('};')
|
||||
|
||||
if condition:
|
||||
lines.append(f'#endif // {condition}')
|
||||
lines.append('')
|
||||
|
||||
route_stack.pop()
|
||||
|
||||
|
||||
def _append_broadcast_messages(lines, container):
|
||||
"""TODO:
|
||||
"""
|
||||
broadcast_messages = container.get('broadcast_messages', {})
|
||||
broadcast_prefix = broadcast_messages['define_prefix']
|
||||
for key, value in broadcast_messages['messages'].items():
|
||||
define = value.get('define')
|
||||
name = to_snake(f'{broadcast_prefix}_{define}')
|
||||
|
||||
if 'return_type' in value:
|
||||
ret_type = _get_c_type(value['return_type'])
|
||||
lines.append(f'void {name}({ret_type} value) {{ xap_broadcast({key}, &value, sizeof(value)); }}')
|
||||
else:
|
||||
lines.append(f'void {name}(const void *data, size_t length){{ xap_broadcast({key}, data, length); }}')
|
||||
|
||||
|
||||
def generate_inline(output_file, keyboard, keymap):
|
||||
"""Generates the XAP protocol header file, generated during normal build.
|
||||
"""
|
||||
xap_defs = merge_xap_defs(keyboard, keymap)
|
||||
|
||||
# Preamble
|
||||
lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '']
|
||||
|
||||
# Add all the generated code
|
||||
_append_broadcast_messages(lines, xap_defs)
|
||||
_append_routing_tables(lines, xap_defs)
|
||||
|
||||
dump_lines(output_file, lines)
|
Loading…
Reference in New Issue
Block a user