write_build_time_header.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env python
  2. # Copyright (c) 2023 Project CHIP Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import argparse
  16. import os
  17. from datetime import datetime, timezone
  18. def utc_time_in_matter_epoch_s():
  19. """ Returns the time in matter epoch in s. """
  20. # Matter epoch is 0 hours, 0 minutes, 0 seconds on Jan 1, 2000 UTC
  21. utc_matter = datetime.now(tz=timezone.utc) - datetime(2000, 1, 1, 0, 0, 0, 0, timezone.utc)
  22. return int(utc_matter.total_seconds())
  23. class Options:
  24. def __init__(self, output, define_name, define_val):
  25. self.output = output
  26. self.define_name = define_name
  27. self.define_val = define_val
  28. def GetOptions():
  29. parser = argparse.ArgumentParser()
  30. parser.add_argument('--output', help="Output header name (inside gen dir)")
  31. parser.add_argument('--gen-dir',
  32. help="Path to root of generated file directory tree.")
  33. cmdline_options = parser.parse_args()
  34. # The actual output file is inside the gen dir.
  35. output = os.path.join(cmdline_options.gen_dir, cmdline_options.output)
  36. define_name = 'CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_TIME_MATTER_EPOCH_S'
  37. build_time = utc_time_in_matter_epoch_s()
  38. return Options(output=output,
  39. define_name=define_name,
  40. define_val=str(build_time))
  41. def WriteHeader(options):
  42. with open(options.output, "w") as output_file:
  43. output_file.write("// Generated by write_build_time_header.py\n")
  44. output_file.write('#pragma once\n\n')
  45. output_file.write(f'#define {options.define_name} {options.define_val}\n')
  46. options = GetOptions()
  47. WriteHeader(options)