version_h.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. """Used by the make system to generate version.h for use in code.
  2. """
  3. from time import strftime
  4. from milc import cli
  5. from qmk.path import normpath
  6. from qmk.commands import dump_lines
  7. from qmk.git import git_get_version
  8. from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
  9. TIME_FMT = '%Y-%m-%d-%H:%M:%S'
  10. @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
  11. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  12. @cli.argument('--skip-git', arg_only=True, action='store_true', help='Skip Git operations')
  13. @cli.argument('--skip-all', arg_only=True, action='store_true', help='Use placeholder values for all defines (implies --skip-git)')
  14. @cli.subcommand('Used by the make system to generate version.h for use in code', hidden=True)
  15. def generate_version_h(cli):
  16. """Generates the version.h file.
  17. """
  18. if cli.args.skip_all:
  19. cli.args.skip_git = True
  20. if cli.args.skip_all:
  21. current_time = "1970-01-01-00:00:00"
  22. else:
  23. current_time = strftime(TIME_FMT)
  24. if cli.args.skip_git:
  25. git_version = "NA"
  26. chibios_version = "NA"
  27. chibios_contrib_version = "NA"
  28. else:
  29. git_version = git_get_version() or current_time
  30. chibios_version = git_get_version("chibios", "os") or current_time
  31. chibios_contrib_version = git_get_version("chibios-contrib", "os") or current_time
  32. # Build the version.h file.
  33. version_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#pragma once']
  34. version_h_lines.append(f"""
  35. #define QMK_VERSION "{git_version}"
  36. #define QMK_BUILDDATE "{current_time}"
  37. #define CHIBIOS_VERSION "{chibios_version}"
  38. #define CHIBIOS_CONTRIB_VERSION "{chibios_contrib_version}"
  39. """)
  40. # Show the results
  41. dump_lines(cli.args.output, version_h_lines, cli.args.quiet)