You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
2.1 KiB

  1. """Used by the make system to generate keyboard.h from info.json.
  2. """
  3. from milc import cli
  4. from qmk.info import info_json
  5. from qmk.keyboard import keyboard_completer, keyboard_folder
  6. from qmk.path import normpath
  7. def would_populate_layout_h(keyboard):
  8. """Detect if a given keyboard is doing data driven layouts
  9. """
  10. # Build the info.json file
  11. kb_info_json = info_json(keyboard)
  12. for layout_name in kb_info_json['layouts']:
  13. if kb_info_json['layouts'][layout_name]['c_macro']:
  14. continue
  15. if 'matrix' not in kb_info_json['layouts'][layout_name]['layout'][0]:
  16. cli.log.debug('%s/%s: No matrix data!', keyboard, layout_name)
  17. continue
  18. return True
  19. return False
  20. @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
  21. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  22. @cli.argument('-kb', '--keyboard', arg_only=True, type=keyboard_folder, completer=keyboard_completer, required=True, help='Keyboard to generate keyboard.h for.')
  23. @cli.subcommand('Used by the make system to generate keyboard.h from info.json', hidden=True)
  24. def generate_keyboard_h(cli):
  25. """Generates the keyboard.h file.
  26. """
  27. has_layout_h = would_populate_layout_h(cli.args.keyboard)
  28. # Build the layouts.h file.
  29. keyboard_h_lines = ['/* This file was generated by `qmk generate-keyboard-h`. Do not edit or copy.' ' */', '', '#pragma once', '#include "quantum.h"']
  30. if not has_layout_h:
  31. keyboard_h_lines.append('#pragma error("<keyboard>.h is only optional for data driven keyboards - kb.h == bad times")')
  32. # Show the results
  33. keyboard_h = '\n'.join(keyboard_h_lines) + '\n'
  34. if cli.args.output:
  35. cli.args.output.parent.mkdir(parents=True, exist_ok=True)
  36. if cli.args.output.exists():
  37. cli.args.output.replace(cli.args.output.parent / (cli.args.output.name + '.bak'))
  38. cli.args.output.write_text(keyboard_h)
  39. if not cli.args.quiet:
  40. cli.log.info('Wrote keyboard_h to %s.', cli.args.output)
  41. else:
  42. print(keyboard_h)