version_h.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. """Used by the make system to generate version.h for use in code.
  2. """
  3. from time import strftime
  4. from pathlib import Path
  5. from milc import cli
  6. from qmk.constants import QMK_USERSPACE, HAS_QMK_USERSPACE
  7. from qmk.path import normpath
  8. from qmk.commands import dump_lines
  9. from qmk.git import git_get_qmk_hash, git_get_version, git_is_dirty
  10. from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
  11. from qmk.util import triplet_to_bcd
  12. TIME_FMT = '%Y-%m-%d-%H:%M:%S'
  13. @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
  14. @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages")
  15. @cli.argument('--skip-git', arg_only=True, action='store_true', help='Skip Git operations')
  16. @cli.argument('--skip-all', arg_only=True, action='store_true', help='Use placeholder values for all defines (implies --skip-git)')
  17. @cli.subcommand('Used by the make system to generate version.h for use in code', hidden=True)
  18. def generate_version_h(cli):
  19. """Generates the version.h file.
  20. """
  21. if cli.args.skip_all:
  22. cli.args.skip_git = True
  23. if cli.args.skip_all:
  24. current_time = "1970-01-01-00:00:00"
  25. else:
  26. current_time = strftime(TIME_FMT)
  27. if cli.args.skip_git:
  28. git_dirty = False
  29. git_version = "NA"
  30. git_qmk_hash = "NA"
  31. git_bcd_version = "0x00000000"
  32. chibios_version = "NA"
  33. chibios_contrib_version = "NA"
  34. userspace_version = "NA"
  35. else:
  36. git_dirty = git_is_dirty()
  37. git_version = git_get_version() or current_time
  38. git_qmk_hash = git_get_qmk_hash() or "Unknown"
  39. git_bcd_version = triplet_to_bcd(git_version)
  40. chibios_version = git_get_version(Path('lib') / "chibios", "os") or current_time
  41. chibios_contrib_version = git_get_version(Path('lib') / "chibios-contrib") or current_time
  42. if HAS_QMK_USERSPACE:
  43. userspace_version = git_get_version(Path(QMK_USERSPACE).resolve()) or current_time
  44. else:
  45. userspace_version = "None"
  46. # Build the version.h file.
  47. version_h_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#pragma once']
  48. version_h_lines.append(
  49. f"""
  50. #define QMK_VERSION "{git_version}"
  51. #define QMK_BUILDDATE "{current_time}"
  52. #define QMK_VERSION_BCD {git_bcd_version}
  53. #define QMK_GIT_HASH "{git_qmk_hash}{'*' if git_dirty else ''}"
  54. #define CHIBIOS_VERSION "{chibios_version}"
  55. #define CHIBIOS_CONTRIB_VERSION "{chibios_contrib_version}"
  56. #define QMK_USERSPACE_VERSION "{userspace_version}"
  57. """
  58. )
  59. # Show the results
  60. dump_lines(cli.args.output, version_h_lines, cli.args.quiet)