| #!/usr/bin/env python |
| # Copyright (c) 2023 Project CHIP Authors |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| import argparse |
| import os |
| from time import time |
| |
| SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__)) |
| FALLBACK_LKGT_FILENAME = os.path.abspath(os.path.join(SCRIPT_DIR, 'fallback_last_known_good_time')) |
| |
| # Offset between the Unix epoch (1970-01-01) and the Matter epoch (2000-01-01) |
| MATTER_UNIX_EPOCH_OFFSET = 10957 * 24 * 60 * 60 # 10957 days, see src/lib/support/TimeUtils.h |
| |
| |
| def posix_time_in_matter_epoch_s(posix_epoch_time: int) -> int: |
| """Converts a POSIX epoch time to Matter epoch time.""" |
| return posix_epoch_time - MATTER_UNIX_EPOCH_OFFSET |
| |
| |
| class Options: |
| def __init__(self, output, define_name, define_val): |
| self.output = output |
| self.define_name = define_name |
| self.define_val = define_val |
| |
| |
| def write_header(options): |
| with open(options.output, "w") as output_file: |
| output_file.write("// Generated by write_build_time_header.py\n") |
| output_file.write('#pragma once\n\n') |
| |
| output_file.write(f'#define {options.define_name} {options.define_val}\n') |
| |
| |
| def update_fallback_time_in_file(): |
| with open(FALLBACK_LKGT_FILENAME, "w") as output_file: |
| output_file.write(str(posix_time_in_matter_epoch_s(int(time())))) |
| |
| |
| def main(): |
| parser = argparse.ArgumentParser() |
| parser.add_argument('--output', help="Output header name (inside gen dir)") |
| parser.add_argument('--gen-dir', |
| help="Path to root of generated file directory tree.") |
| parser.add_argument('--use-current-time', default=False, action='store_true', |
| help="Set the LKGT to the current time. If this flag is not used, the LKGT is set to a hardcoded time.") |
| parser.add_argument('--update-fallback-time-in-file', default=False, action='store_true', |
| help='Write the current UTC time out to the fallback file') |
| cmdline_options = parser.parse_args() |
| |
| if cmdline_options.update_fallback_time_in_file: |
| update_fallback_time_in_file() |
| return |
| |
| # The actual output file is inside the gen dir. |
| output = os.path.join(cmdline_options.gen_dir, cmdline_options.output) |
| |
| define_name = 'CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_TIME_MATTER_EPOCH_S' |
| if cmdline_options.use_current_time: |
| build_time = posix_time_in_matter_epoch_s(int(time())) |
| else: |
| with open(FALLBACK_LKGT_FILENAME, "r") as input_file: |
| build_time = int(input_file.read()) |
| |
| # If SOURCE_DATE_EPOCH is set in the environment and is ahead of the fallback time, use it. |
| # See https://reproducible-builds.org/specs/source-date-epoch/ |
| source_date_epoch = os.environ.get('SOURCE_DATE_EPOCH') |
| if source_date_epoch is not None: |
| source_date_time = posix_time_in_matter_epoch_s(int(source_date_epoch)) |
| if source_date_time > build_time: |
| build_time = source_date_time |
| |
| opts = Options(output=output, |
| define_name=define_name, |
| define_val=str(build_time)) |
| write_header(opts) |
| |
| |
| main() |