community_modules.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. import contextlib
  2. from argcomplete.completers import FilesCompleter
  3. from pathlib import Path
  4. from milc import cli
  5. import qmk.path
  6. from qmk.info import get_modules
  7. from qmk.keyboard import keyboard_completer, keyboard_folder
  8. from qmk.commands import dump_lines
  9. from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, GPL2_HEADER_SH_LIKE, GENERATED_HEADER_SH_LIKE
  10. from qmk.community_modules import module_api_list, load_module_jsons, find_module_path
  11. @contextlib.contextmanager
  12. def _render_api_guard(lines, api):
  13. if api.guard:
  14. lines.append(f'#if {api.guard}')
  15. yield
  16. if api.guard:
  17. lines.append(f'#endif // {api.guard}')
  18. def _render_api_header(api):
  19. lines = []
  20. if api.header:
  21. lines.append('')
  22. with _render_api_guard(lines, api):
  23. lines.append(f'#include <{api.header}>')
  24. return lines
  25. def _render_keycodes(module_jsons):
  26. lines = []
  27. lines.append('')
  28. lines.append('enum {')
  29. first = True
  30. for module_json in module_jsons:
  31. module_name = Path(module_json['module']).name
  32. keycodes = module_json.get('keycodes', [])
  33. if len(keycodes) > 0:
  34. lines.append(f' // From module: {module_name}')
  35. for keycode in keycodes:
  36. key = keycode.get('key', None)
  37. if first:
  38. lines.append(f' {key} = QK_COMMUNITY_MODULE,')
  39. first = False
  40. else:
  41. lines.append(f' {key},')
  42. for alias in keycode.get('aliases', []):
  43. lines.append(f' {alias} = {key},')
  44. lines.append('')
  45. lines.append(' LAST_COMMUNITY_MODULE_KEY')
  46. lines.append('};')
  47. lines.append('STATIC_ASSERT((int)LAST_COMMUNITY_MODULE_KEY <= (int)(QK_COMMUNITY_MODULE_MAX+1), "Too many community module keycodes");')
  48. return lines
  49. def _render_api_declarations(api, module, user_kb=True):
  50. lines = []
  51. lines.append('')
  52. with _render_api_guard(lines, api):
  53. if user_kb:
  54. lines.append(f'{api.ret_type} {api.name}_{module}_user({api.args});')
  55. lines.append(f'{api.ret_type} {api.name}_{module}_kb({api.args});')
  56. lines.append(f'{api.ret_type} {api.name}_{module}({api.args});')
  57. return lines
  58. def _render_api_implementations(api, module):
  59. module_name = Path(module).name
  60. lines = []
  61. lines.append('')
  62. with _render_api_guard(lines, api):
  63. # _user
  64. lines.append(f'__attribute__((weak)) {api.ret_type} {api.name}_{module_name}_user({api.args}) {{')
  65. if api.ret_type == 'bool':
  66. lines.append(' return true;')
  67. elif api.ret_type in ['layer_state_t', 'report_mouse_t']:
  68. lines.append(f' return {api.call_params};')
  69. else:
  70. pass
  71. lines.append('}')
  72. lines.append('')
  73. # _kb
  74. lines.append(f'__attribute__((weak)) {api.ret_type} {api.name}_{module_name}_kb({api.args}) {{')
  75. if api.ret_type == 'bool':
  76. lines.append(f' if(!{api.name}_{module_name}_user({api.call_params})) {{ return false; }}')
  77. lines.append(' return true;')
  78. elif api.ret_type in ['layer_state_t', 'report_mouse_t']:
  79. lines.append(f' return {api.name}_{module_name}_user({api.call_params});')
  80. else:
  81. lines.append(f' {api.name}_{module_name}_user({api.call_params});')
  82. lines.append('}')
  83. lines.append('')
  84. # module (non-suffixed)
  85. lines.append(f'__attribute__((weak)) {api.ret_type} {api.name}_{module_name}({api.args}) {{')
  86. if api.ret_type == 'bool':
  87. lines.append(f' if(!{api.name}_{module_name}_kb({api.call_params})) {{ return false; }}')
  88. lines.append(' return true;')
  89. elif api.ret_type in ['layer_state_t', 'report_mouse_t']:
  90. lines.append(f' return {api.name}_{module_name}_kb({api.call_params});')
  91. else:
  92. lines.append(f' {api.name}_{module_name}_kb({api.call_params});')
  93. lines.append('}')
  94. return lines
  95. def _render_core_implementation(api, modules):
  96. lines = []
  97. lines.append('')
  98. with _render_api_guard(lines, api):
  99. lines.append(f'{api.ret_type} {api.name}_modules({api.args}) {{')
  100. if api.ret_type == 'bool':
  101. lines.append(' return true')
  102. for module in modules:
  103. module_name = Path(module).name
  104. if api.ret_type == 'bool':
  105. lines.append(f' && {api.name}_{module_name}({api.call_params})')
  106. elif api.ret_type in ['layer_state_t', 'report_mouse_t']:
  107. lines.append(f' {api.call_params} = {api.name}_{module_name}({api.call_params});')
  108. else:
  109. lines.append(f' {api.name}_{module_name}({api.call_params});')
  110. if api.ret_type == 'bool':
  111. lines.append(' ;')
  112. elif api.ret_type in ['layer_state_t', 'report_mouse_t']:
  113. lines.append(f' return {api.call_params};')
  114. lines.append('}')
  115. return lines
  116. def _generate_features_rules(features_dict):
  117. lines = []
  118. for feature, enabled in features_dict.items():
  119. feature = feature.upper()
  120. enabled = 'yes' if enabled else 'no'
  121. lines.append(f'{feature}_ENABLE={enabled}')
  122. return lines
  123. def _generate_modules_rules(keyboard, filename):
  124. lines = []
  125. modules = get_modules(keyboard, filename)
  126. if len(modules) > 0:
  127. lines.append('')
  128. lines.append('OPT_DEFS += -DCOMMUNITY_MODULES_ENABLE=TRUE')
  129. for module in modules:
  130. module_path = qmk.path.unix_style_path(find_module_path(module))
  131. if not module_path:
  132. raise FileNotFoundError(f"Module '{module}' not found.")
  133. lines.append('')
  134. lines.append(f'COMMUNITY_MODULES += {module_path.name}') # use module_path here instead of module as it may be a subdirectory
  135. lines.append(f'OPT_DEFS += -DCOMMUNITY_MODULE_{module_path.name.upper()}_ENABLE=TRUE')
  136. lines.append(f'COMMUNITY_MODULE_PATHS += {module_path}')
  137. lines.append(f'VPATH += {module_path}')
  138. lines.append(f'SRC += $(wildcard {module_path}/{module_path.name}.c)')
  139. lines.append(f'MODULE_NAME_{module_path.name.upper()} := {module_path.name}')
  140. lines.append(f'MODULE_PATH_{module_path.name.upper()} := {module_path}')
  141. lines.append(f'-include {module_path}/rules.mk')
  142. module_jsons = load_module_jsons(modules)
  143. for module_json in module_jsons:
  144. if 'features' in module_json:
  145. lines.append('')
  146. lines.append(f'# Module: {module_json["module_name"]}')
  147. lines.extend(_generate_features_rules(module_json['features']))
  148. return lines
  149. def _module_slugs(modules):
  150. return [Path(m).name.lower() for m in modules]
  151. def _render_eeconfig_declarations(modules):
  152. lines = []
  153. lines.append('')
  154. lines.append('// nvm eeconfig')
  155. for module_slug in _module_slugs(modules):
  156. lines.extend([
  157. f'#if (EECONFIG_MODULE_{module_slug.upper()}_DATA_SIZE) > 0',
  158. f'bool eeconfig_is_{module_slug}_datablock_valid(void);',
  159. f'uint32_t eeconfig_read_{module_slug}_datablock(void *data, uint32_t offset, uint32_t length) __attribute__((nonnull));',
  160. f'uint32_t eeconfig_update_{module_slug}_datablock(const void *data, uint32_t offset, uint32_t length) __attribute__((nonnull));',
  161. f'void eeconfig_init_{module_slug}_datablock(void);',
  162. f'# define eeconfig_read_{module_slug}_datablock_field(__object, __field) eeconfig_read_{module_slug}_datablock(&(__object.__field), offsetof(typeof(__object), __field), sizeof(__object.__field))',
  163. f'# define eeconfig_update_{module_slug}_datablock_field(__object, __field) eeconfig_update_{module_slug}_datablock(&(__object.__field), offsetof(typeof(__object), __field), sizeof(__object.__field))',
  164. '',
  165. f'bool nvm_eeconfig_is_{module_slug}_datablock_valid(void);',
  166. f'uint32_t nvm_eeconfig_read_{module_slug}_datablock(void *data, uint32_t offset, uint32_t length);',
  167. f'uint32_t nvm_eeconfig_update_{module_slug}_datablock(const void *data, uint32_t offset, uint32_t length);',
  168. f'void nvm_eeconfig_init_{module_slug}_datablock(void);',
  169. f'#endif // (EECONFIG_MODULE_{module_slug.upper()}_DATA_SIZE) > 0',
  170. '',
  171. ])
  172. lines.append('typedef struct PACKED {')
  173. for module_slug in _module_slugs(modules):
  174. lines.extend([
  175. f'#if (EECONFIG_MODULE_{module_slug.upper()}_DATA_SIZE) > 0',
  176. f' uint32_t {module_slug}_version;',
  177. f' uint8_t {module_slug}[EECONFIG_MODULE_{module_slug.upper()}_DATA_SIZE];',
  178. f'#endif // (EECONFIG_MODULE_{module_slug.upper()}_DATA_SIZE) > 0',
  179. ])
  180. lines.append('} eeprom_modules_t;')
  181. lines.append('')
  182. for module_slug in _module_slugs(modules):
  183. lines.append(f'#define EECONFIG_MODULE_{module_slug.upper()}_VERSION (uint32_t *)(EECONFIG_MODULES_DATABLOCK + (offsetof(eeprom_modules_t, {module_slug}_version)))')
  184. lines.append(f'#define EECONFIG_MODULE_{module_slug.upper()}_DATABLOCK (uint8_t *)(EECONFIG_MODULES_DATABLOCK + (offsetof(eeprom_modules_t, {module_slug})))')
  185. lines.append('')
  186. lines.append('bool eeconfig_is_modules_datablock_valid(void);')
  187. lines.append('void eeconfig_init_modules_datablock(void);')
  188. lines.append('')
  189. return lines
  190. def _render_eeconfig_implementation(modules):
  191. lines = []
  192. lines.append('')
  193. lines.append('// nvm eeconfig')
  194. lines.append('#if defined(NVM_DRIVER_EEPROM)'),
  195. lines.append('# include "nvm_eeprom_eeconfig_internal.h"')
  196. lines.append('# include "eeprom.h"')
  197. lines.append('#endif // defined(NVM_DRIVER_EEPROM)'),
  198. lines.append('')
  199. for module_slug in _module_slugs(modules):
  200. lines.extend([
  201. f'#if (EECONFIG_MODULE_{module_slug.upper()}_DATA_SIZE) > 0',
  202. f'bool eeconfig_is_{module_slug}_datablock_valid(void) {{ return nvm_eeconfig_is_{module_slug}_datablock_valid(); }}',
  203. f'uint32_t eeconfig_read_{module_slug}_datablock(void *data, uint32_t offset, uint32_t length) {{ return nvm_eeconfig_read_{module_slug}_datablock(data, offset, length); }}',
  204. f'uint32_t eeconfig_update_{module_slug}_datablock(const void *data, uint32_t offset, uint32_t length) {{ return nvm_eeconfig_update_{module_slug}_datablock(data, offset, length); }}',
  205. f'void eeconfig_init_{module_slug}_datablock(void) {{ nvm_eeconfig_init_{module_slug}_datablock(); }}',
  206. '',
  207. '# if defined(NVM_DRIVER_EEPROM)',
  208. f'bool nvm_eeconfig_is_{module_slug}_datablock_valid(void) {{',
  209. f' return eeprom_read_dword(EECONFIG_MODULE_{module_slug.upper()}_VERSION) == (EECONFIG_MODULE_{module_slug.upper()}_DATA_VERSION);',
  210. '}',
  211. f'uint32_t nvm_eeconfig_read_{module_slug}_datablock(void *data, uint32_t offset, uint32_t length) {{',
  212. f' if (eeconfig_is_{module_slug}_datablock_valid()) {{',
  213. f' void *ee_start = (void *)(uintptr_t)(EECONFIG_MODULE_{module_slug.upper()}_DATABLOCK + offset);',
  214. f' void *ee_end = (void *)(uintptr_t)(EECONFIG_MODULE_{module_slug.upper()}_DATABLOCK + MIN((EECONFIG_MODULE_{module_slug.upper()}_DATA_SIZE), offset + length));',
  215. ' eeprom_read_block(data, ee_start, ee_end - ee_start);',
  216. ' return ee_end - ee_start;',
  217. ' } else {',
  218. ' memset(data, 0, length);',
  219. ' return length;',
  220. ' }',
  221. '}',
  222. f'uint32_t nvm_eeconfig_update_{module_slug}_datablock(const void *data, uint32_t offset, uint32_t length) {{',
  223. f' eeprom_update_dword(EECONFIG_MODULE_{module_slug.upper()}_VERSION, (EECONFIG_MODULE_{module_slug.upper()}_DATA_VERSION));',
  224. f' void *ee_start = (void *)(uintptr_t)(EECONFIG_MODULE_{module_slug.upper()}_DATABLOCK + offset);',
  225. f' void *ee_end = (void *)(uintptr_t)(EECONFIG_MODULE_{module_slug.upper()}_DATABLOCK + MIN((EECONFIG_MODULE_{module_slug.upper()}_DATA_SIZE), offset + length));',
  226. ' eeprom_update_block(data, ee_start, ee_end - ee_start);',
  227. ' return ee_end - ee_start;',
  228. '}',
  229. f'void nvm_eeconfig_init_{module_slug}_datablock(void) {{',
  230. f' eeprom_update_dword(EECONFIG_MODULE_{module_slug.upper()}_VERSION, (EECONFIG_MODULE_{module_slug.upper()}_DATA_VERSION));',
  231. f' void *start = (void *)(uintptr_t)(EECONFIG_MODULE_{module_slug.upper()}_DATABLOCK);',
  232. f' void *end = (void *)(uintptr_t)(EECONFIG_MODULE_{module_slug.upper()}_DATABLOCK + (EECONFIG_MODULE_{module_slug.upper()}_DATA_SIZE));',
  233. ' long remaining = end - start;',
  234. ' uint8_t dummy[16] = {0};',
  235. f' for (int i = 0; i < EECONFIG_MODULE_{module_slug.upper()}_DATA_SIZE; i += sizeof(dummy)) {{',
  236. ' int this_loop = remaining < sizeof(dummy) ? remaining : sizeof(dummy);',
  237. ' eeprom_update_block(dummy, start, this_loop);',
  238. ' start += this_loop;',
  239. ' remaining -= this_loop;',
  240. ' }',
  241. '}',
  242. '# endif // defined(NVM_DRIVER_EEPROM)',
  243. f'#endif // (EECONFIG_MODULE_{module_slug.upper()}_DATA_SIZE) > 0',
  244. '',
  245. ])
  246. lines.append('bool eeconfig_is_modules_datablock_valid(void) {')
  247. lines.append(' return true')
  248. for module_slug in _module_slugs(modules):
  249. lines.extend([
  250. f'#if (EECONFIG_MODULE_{module_slug.upper()}_DATA_SIZE) > 0',
  251. f' && eeconfig_is_{module_slug}_datablock_valid()',
  252. f'#endif // (EECONFIG_MODULE_{module_slug.upper()}_DATA_SIZE) > 0',
  253. ])
  254. lines.append(' ;')
  255. lines.append('}')
  256. lines.append('')
  257. lines.append('void eeconfig_init_modules_datablock(void) {'),
  258. for module_slug in _module_slugs(modules):
  259. lines.extend([
  260. f'#if (EECONFIG_MODULE_{module_slug.upper()}_DATA_SIZE) > 0',
  261. f' eeconfig_init_{module_slug}_datablock();',
  262. f'#endif // (EECONFIG_MODULE_{module_slug.upper()}_DATA_SIZE) > 0',
  263. ])
  264. lines.append('}')
  265. lines.append('')
  266. return lines
  267. @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
  268. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  269. @cli.argument('-e', '--escape', arg_only=True, action='store_true', help="Escape spaces in quiet mode")
  270. @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate rules.mk for.')
  271. @cli.argument('filename', nargs='?', arg_only=True, type=qmk.path.FileType('r'), completer=FilesCompleter('.json'), help='A configurator export JSON to be compiled and flashed or a pre-compiled binary firmware file (bin/hex) to be flashed.')
  272. @cli.subcommand('Creates a community_modules_rules_mk from a keymap.json file.')
  273. def generate_community_modules_rules_mk(cli):
  274. rules_mk_lines = [GPL2_HEADER_SH_LIKE, GENERATED_HEADER_SH_LIKE]
  275. rules_mk_lines.extend(_generate_modules_rules(cli.args.keyboard, cli.args.filename))
  276. # Show the results
  277. dump_lines(cli.args.output, rules_mk_lines)
  278. if cli.args.output:
  279. if cli.args.quiet:
  280. if cli.args.escape:
  281. print(cli.args.output.as_posix().replace(' ', '\\ '))
  282. else:
  283. print(cli.args.output)
  284. else:
  285. cli.log.info('Wrote rules.mk to %s.', cli.args.output)
  286. @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
  287. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  288. @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate community_post_config.h for.')
  289. @cli.argument('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file')
  290. @cli.subcommand('Creates a community_post_config.h from a keymap.json file.')
  291. def generate_community_post_config_h(cli):
  292. """Creates a community_post_config.h from a keymap.json file
  293. """
  294. if cli.args.output and cli.args.output.name == '-':
  295. cli.args.output = None
  296. lines = [
  297. GPL2_HEADER_C_LIKE,
  298. GENERATED_HEADER_C_LIKE,
  299. '#pragma once',
  300. '',
  301. ]
  302. modules = get_modules(cli.args.keyboard, cli.args.filename)
  303. if len(modules) > 0:
  304. lines.append('// Split transactions')
  305. for module_slug in _module_slugs(modules):
  306. lines.extend([
  307. f'#ifdef SPLIT_TRANSACTION_IDS_MODULE_{module_slug.upper()}',
  308. '# define SPLIT_TRANSACTION_RPC',
  309. '#endif',
  310. ])
  311. lines.append('')
  312. lines.append('// nvm eeconfig')
  313. for module_slug in _module_slugs(modules):
  314. lines.extend([
  315. f'#ifndef EECONFIG_MODULE_{module_slug.upper()}_DATA_SIZE',
  316. f'# define EECONFIG_MODULE_{module_slug.upper()}_DATA_SIZE 0',
  317. '#endif',
  318. f'#ifndef EECONFIG_MODULE_{module_slug.upper()}_DATA_VERSION',
  319. f'# define EECONFIG_MODULE_{module_slug.upper()}_DATA_VERSION (EECONFIG_MODULE_{module_slug.upper()}_DATA_SIZE)',
  320. '#endif',
  321. '',
  322. ])
  323. module_size = " + ".join([f'(4 + (EECONFIG_MODULE_{module_slug.upper()}_DATA_SIZE))' for module_slug in _module_slugs(modules)])
  324. lines.append(f'#define EECONFIG_MODULE_DATA_SIZE ({module_size})')
  325. lines.append('')
  326. dump_lines(cli.args.output, lines, cli.args.quiet, remove_repeated_newlines=True)
  327. @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
  328. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  329. @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate community_modules.h for.')
  330. @cli.argument('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file')
  331. @cli.subcommand('Creates a community_modules.h from a keymap.json file.')
  332. def generate_community_modules_h(cli):
  333. """Creates a community_modules.h from a keymap.json file
  334. """
  335. if cli.args.output and cli.args.output.name == '-':
  336. cli.args.output = None
  337. api_list, api_version, ver_major, ver_minor, ver_patch = module_api_list()
  338. lines = [
  339. GPL2_HEADER_C_LIKE,
  340. GENERATED_HEADER_C_LIKE,
  341. '#pragma once',
  342. '#include <stdint.h>',
  343. '#include <stdbool.h>',
  344. '#include <string.h>',
  345. '#include <keycodes.h>',
  346. '',
  347. '#include "compiler_support.h"',
  348. '',
  349. '#define COMMUNITY_MODULES_API_VERSION_BUILDER(ver_major,ver_minor,ver_patch) (((((uint32_t)(ver_major))&0xFF) << 24) | ((((uint32_t)(ver_minor))&0xFF) << 16) | (((uint32_t)(ver_patch))&0xFF))',
  350. f'#define COMMUNITY_MODULES_API_VERSION COMMUNITY_MODULES_API_VERSION_BUILDER({ver_major},{ver_minor},{ver_patch})',
  351. f'#define ASSERT_COMMUNITY_MODULES_MIN_API_VERSION(ver_major,ver_minor,ver_patch) STATIC_ASSERT(COMMUNITY_MODULES_API_VERSION_BUILDER(ver_major,ver_minor,ver_patch) <= COMMUNITY_MODULES_API_VERSION, "Community module requires a newer version of QMK modules API -- needs: " #ver_major "." #ver_minor "." #ver_patch ", current: {api_version}.")',
  352. '',
  353. 'typedef struct keyrecord_t keyrecord_t; // forward declaration so we don\'t need to include quantum.h',
  354. '',
  355. ]
  356. modules = get_modules(cli.args.keyboard, cli.args.filename)
  357. module_jsons = load_module_jsons(modules)
  358. if len(modules) > 0:
  359. lines.extend(_render_keycodes(module_jsons))
  360. for api in api_list:
  361. lines.extend(_render_api_header(api))
  362. for module in modules:
  363. lines.append('')
  364. lines.append(f'// From module: {module}')
  365. for api in api_list:
  366. lines.extend(_render_api_declarations(api, Path(module).name))
  367. lines.append('')
  368. lines.extend(_render_eeconfig_declarations(modules))
  369. lines.append('// Core wrapper')
  370. for api in api_list:
  371. lines.extend(_render_api_declarations(api, 'modules', user_kb=False))
  372. dump_lines(cli.args.output, lines, cli.args.quiet, remove_repeated_newlines=True)
  373. @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
  374. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  375. @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate community_modules.c for.')
  376. @cli.argument('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file')
  377. @cli.subcommand('Creates a community_modules.c from a keymap.json file.')
  378. def generate_community_modules_c(cli):
  379. """Creates a community_modules.c from a keymap.json file
  380. """
  381. if cli.args.output and cli.args.output.name == '-':
  382. cli.args.output = None
  383. api_list, _, _, _, _ = module_api_list()
  384. lines = [
  385. GPL2_HEADER_C_LIKE,
  386. GENERATED_HEADER_C_LIKE,
  387. '',
  388. '#include "community_modules.h"',
  389. ]
  390. modules = get_modules(cli.args.keyboard, cli.args.filename)
  391. if len(modules) > 0:
  392. for module in modules:
  393. for api in api_list:
  394. lines.extend(_render_api_implementations(api, Path(module).name))
  395. for api in api_list:
  396. lines.extend(_render_core_implementation(api, modules))
  397. lines.extend(_render_eeconfig_implementation(modules))
  398. dump_lines(cli.args.output, lines, cli.args.quiet, remove_repeated_newlines=True)
  399. def _generate_include_per_module(cli, include_file_name):
  400. """Generates C code to include "<module_path>/include_file_name" for each module."""
  401. if cli.args.output and cli.args.output.name == '-':
  402. cli.args.output = None
  403. lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE]
  404. for module in get_modules(cli.args.keyboard, cli.args.filename):
  405. full_path = f'{find_module_path(module)}/{include_file_name}'
  406. lines.append('')
  407. lines.append(f'#if __has_include("{full_path}")')
  408. lines.append(f'#include "{full_path}"')
  409. lines.append(f'#endif // __has_include("{full_path}")')
  410. dump_lines(cli.args.output, lines, cli.args.quiet, remove_repeated_newlines=True)
  411. @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
  412. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  413. @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate community_modules_introspection.h for.')
  414. @cli.argument('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file')
  415. @cli.subcommand('Creates a community_modules_introspection.h from a keymap.json file.')
  416. def generate_community_modules_introspection_h(cli):
  417. """Creates a community_modules_introspection.h from a keymap.json file
  418. """
  419. _generate_include_per_module(cli, 'introspection.h')
  420. @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
  421. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  422. @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate community_modules.c for.')
  423. @cli.argument('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file')
  424. @cli.subcommand('Creates a community_modules_introspection.c from a keymap.json file.')
  425. def generate_community_modules_introspection_c(cli):
  426. """Creates a community_modules_introspection.c from a keymap.json file
  427. """
  428. _generate_include_per_module(cli, 'introspection.c')
  429. @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
  430. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  431. @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate led_matrix_community_modules.inc for.')
  432. @cli.argument('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file')
  433. @cli.subcommand('Creates an led_matrix_community_modules.inc from a keymap.json file.')
  434. def generate_led_matrix_community_modules_inc(cli):
  435. """Creates an led_matrix_community_modules.inc from a keymap.json file
  436. """
  437. _generate_include_per_module(cli, 'led_matrix_module.inc')
  438. @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
  439. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  440. @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate rgb_matrix_community_modules.inc for.')
  441. @cli.argument('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file')
  442. @cli.subcommand('Creates an rgb_matrix_community_modules.inc from a keymap.json file.')
  443. def generate_rgb_matrix_community_modules_inc(cli):
  444. """Creates an rgb_matrix_community_modules.inc from a keymap.json file
  445. """
  446. _generate_include_per_module(cli, 'rgb_matrix_module.inc')
  447. @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
  448. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  449. @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, help='Keyboard to generate split_transaction_id_community_modules.inc for.')
  450. @cli.argument('filename', nargs='?', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file')
  451. @cli.subcommand('Creates an split_transaction_id_community_modules.inc from a keymap.json file.')
  452. def generate_split_transaction_id_community_modules_inc(cli):
  453. """Creates an split_transaction_id_community_modules.inc from a keymap.json file
  454. """
  455. if cli.args.output and cli.args.output.name == '-':
  456. cli.args.output = None
  457. lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE]
  458. for module in get_modules(cli.args.keyboard, cli.args.filename):
  459. lines.extend([
  460. f'#ifdef SPLIT_TRANSACTION_IDS_MODULE_{Path(module).name.upper()}',
  461. f' SPLIT_TRANSACTION_IDS_MODULE_{Path(module).name.upper()},',
  462. '#endif',
  463. ])
  464. dump_lines(cli.args.output, lines, cli.args.quiet, remove_repeated_newlines=True)