makehandler.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. """Generate a make command from a `keyboard:keymap` pair.
  2. """
  3. import os
  4. from milc import cli
  5. from qmk.path import is_keyboard
  6. def parse_rule(rule):
  7. """Parses a QMK make rule into its compononent parts.
  8. Expects rule to be in the form `<keyboard>:<keyboard>[:<COMMAND>]`
  9. """
  10. rule = rule.split(':')
  11. if len(rule) == 1:
  12. return rule + ['all', '']
  13. if len(rule) == 2:
  14. return rule + ['']
  15. if len(rule) == 3:
  16. return rule
  17. raise IndexError('rule %s has too many parts!' % rule)
  18. @cli.argument('arguments', arg_only=True, nargs='+', help='Make arguments.')
  19. @cli.subcommand('QMK Hello World.', hidden=True)
  20. def makehandler(cli):
  21. """Turn a `make keyboard:keymap` command into a full make command.
  22. """
  23. environment = os.environ.copy() # FIXME: We should sanitize this
  24. print('***', 'environment')
  25. for key, value in environment.items():
  26. cli.log.info('%s=%s', key, value)
  27. environment['COLOR'] = '1' if cli.config.general.color else '0'
  28. environment['SILENT_MODE'] = '0'
  29. environment['VERBOSE'] = '1' if cli.config.general.verbose else '0'
  30. compile_targets = []
  31. # Check submodules and warn if dirty
  32. pass
  33. # Parse the command line (PARSE_RULE)
  34. for argument in cli.args.arguments:
  35. if "=" in argument:
  36. key, value = argument.split('=', 1)
  37. environment[key] = value
  38. else:
  39. keyboard, keymap, command = parse_rule(argument)
  40. keyboard_env = environment.copy()
  41. # Check the keyboard
  42. if not (keyboard in ['all', 'test'] or is_keyboard(keyboard)):
  43. cli.log.error('Invalid keyboard: %s', keyboard)
  44. continue
  45. # Check the keymap
  46. if not (keymap in ['all'] or True): # FIXME: Replace True with is_keymap(keyboard, keymap)
  47. cli.log.error('Invalid keymap: %s', keymap)
  48. continue
  49. # Add this compile target
  50. keyboard_env['CURRENT_KB'] = keyboard_env['DEFAULT_FOLDER'] = keyboard_env['KEYBOARD_RULE'] = keyboard
  51. compile_targets.append([keyboard, keymap, command, keyboard_env])
  52. # Determine if we should turn on SILENT_MODE.
  53. if 'SILENT' in environment:
  54. for target in compile_targets:
  55. target[3]['SILENT_MODE'] = environment['SILENT']
  56. elif len(compile_targets) > 1:
  57. for target in compile_targets:
  58. target[3]['SILENT_MODE'] = environment['SILENT']
  59. # Iterate through our compile targets and generate make commands.
  60. compiles_ok = True
  61. for keyboard, keymap, command, environment in compile_targets:
  62. print('***', repr(keyboard), repr(keymap), repr(command))
  63. for key, value in environment.items():
  64. cli.log.info('%s=%s', key, value)
  65. if not compiles_ok:
  66. cli.log.error('{fg_red}Make finished with errors')
  67. exit(1)
  68. # $(foreach TEST,$(sort $(TESTS)),$(RUN_TEST))
  69. tests_ok = False # FIXME: Set to true when all tests are true
  70. if not tests_ok:
  71. # printf "$(MSG_ERRORS)" & exit 1
  72. pass