xref: /aosp_15_r20/external/pigweed/pw_chrono/generate_build_time_header.py (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2021 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""Generate a header file for UTC time"""
15
16import argparse
17from datetime import datetime
18import sys
19
20HEADER = """// Copyright 2021 The Pigweed Authors
21//
22// Licensed under the Apache License, Version 2.0 (the "License"); you may not
23// use this file except in compliance with the License. You may obtain a copy of
24// the License at
25//
26//     https://www.apache.org/licenses/LICENSE-2.0
27//
28// Unless required by applicable law or agreed to in writing, software
29// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
30// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
31// License for the specific language governing permissions and limitations under
32// the License.
33
34#include <stdint.h>
35
36"""
37
38
39def parse_args() -> None:
40    """Setup argparse."""
41    parser = argparse.ArgumentParser()
42    parser.add_argument("out", help="path for output header file")
43    parser.add_argument(
44        '--bazel',
45        action='store_true',
46        help='Set if this script is being invoked by Bazel',
47    )
48    return parser.parse_args()
49
50
51def main() -> int:
52    """Main function"""
53    args = parse_args()
54
55    if args.bazel:
56        # See https://bazel.build/docs/user-manual#workspace-status for
57        # documentation of volatite-status.txt.
58        with open('bazel-out/volatile-status.txt', 'r') as status:
59            for line in status:
60                pieces = line.split()
61                key = pieces[0]
62                if key == "BUILD_TIMESTAMP":
63                    time_stamp = int(pieces[1])
64                    break
65    else:
66        # We're not being invoked from Bazel.
67        time_stamp = int(datetime.now().timestamp())
68
69    with open(args.out, "w") as header:
70        header.write(HEADER)
71
72        # Add a comment in the generated header to show readable build time
73        string_date = datetime.fromtimestamp(time_stamp).strftime(
74            "%m/%d/%Y %H:%M:%S"
75        )
76        header.write(f'// {string_date}\n')
77
78        # Write to the header.
79        header.write(
80            ''.join(
81                [
82                    'constexpr uint64_t kBuildTimeMicrosecondsUTC = ',
83                    f'{int(time_stamp * 1e6)};\n',
84                ]
85            )
86        )
87    return 0
88
89
90if __name__ == "__main__":
91    sys.exit(main())
92