Primiano Tucci | 34bc559 | 2021-02-19 17:53:36 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Primiano Tucci | ec59013 | 2020-11-16 14:16:44 +0100 | [diff] [blame] | 2 | # Copyright (C) 2020 The Android Open Source Project |
| 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 | """ |
| 16 | Writes the perfetto_version{.gen.h, .ts} files. |
| 17 | |
| 18 | This tool is run as part of a genrule from GN, SoonG and Bazel builds. It |
| 19 | generates a source header (or in the case of --ts_out a TypeScript file) that |
| 20 | contains: |
| 21 | - The version number (e.g. v9.0) obtained parsing the CHANGELOG file. |
| 22 | - The git HEAD's commit-ish (e.g. 6b330b772b0e973f79c70ba2e9bb2b0110c6715d) |
Primiano Tucci | ec59013 | 2020-11-16 14:16:44 +0100 | [diff] [blame] | 23 | |
Primiano Tucci | de775ba | 2021-09-07 10:20:52 +0100 | [diff] [blame] | 24 | The latter is concatenated to the version number to disambiguate builds made |
| 25 | from release tags vs builds made from the main branch vs UI builds made from the |
| 26 | ui-canary/ui-stable branch. |
Primiano Tucci | ec59013 | 2020-11-16 14:16:44 +0100 | [diff] [blame] | 27 | """ |
| 28 | |
| 29 | import argparse |
| 30 | import os |
| 31 | import re |
| 32 | import sys |
| 33 | import subprocess |
| 34 | |
| 35 | # Note: PROJECT_ROOT is not accurate in bazel builds, where this script is |
| 36 | # executed in the bazel sandbox. |
| 37 | PROJECT_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) |
| 38 | SCM_REV_NOT_AVAILABLE = 'N/A' |
| 39 | |
| 40 | |
Primiano Tucci | f0ed1d4 | 2020-11-18 16:30:18 +0100 | [diff] [blame] | 41 | def get_latest_release(changelog_path): |
Primiano Tucci | ec59013 | 2020-11-16 14:16:44 +0100 | [diff] [blame] | 42 | """Returns a string like 'v9.0'. |
| 43 | |
| 44 | It does so by searching the latest version mentioned in the CHANGELOG.""" |
Primiano Tucci | f0ed1d4 | 2020-11-18 16:30:18 +0100 | [diff] [blame] | 45 | if not changelog_path: |
| 46 | if os.path.exists('CHANGELOG'): |
| 47 | changelog_path = 'CHANGELOG' |
| 48 | else: |
| 49 | changelog_path = os.path.join(PROJECT_ROOT, 'CHANGELOG') |
Primiano Tucci | ec59013 | 2020-11-16 14:16:44 +0100 | [diff] [blame] | 50 | with open(changelog_path) as f: |
| 51 | for line in f.readlines(): |
| 52 | m = re.match('^(v\d+[.]\d+)\s.*$', line) |
| 53 | if m is not None: |
| 54 | return m.group(1) |
| 55 | raise Exception('Failed to fetch Perfetto version from %s' % changelog_path) |
| 56 | |
| 57 | |
Hector Dearman | 3c9b8cf | 2021-09-01 12:56:17 +0100 | [diff] [blame] | 58 | def get_git_sha1(commitish): |
| 59 | """Returns the SHA1 of the provided commit-ish""" |
Primiano Tucci | ec59013 | 2020-11-16 14:16:44 +0100 | [diff] [blame] | 60 | commit_sha1 = SCM_REV_NOT_AVAILABLE |
Primiano Tucci | ec59013 | 2020-11-16 14:16:44 +0100 | [diff] [blame] | 61 | git_dir = os.path.join(PROJECT_ROOT, '.git') |
| 62 | if os.path.exists(git_dir): |
| 63 | try: |
Hector Dearman | 3c9b8cf | 2021-09-01 12:56:17 +0100 | [diff] [blame] | 64 | commit_sha1 = subprocess.check_output(['git', 'rev-parse', commitish], |
Primiano Tucci | ec59013 | 2020-11-16 14:16:44 +0100 | [diff] [blame] | 65 | cwd=PROJECT_ROOT).strip().decode() |
Primiano Tucci | ec59013 | 2020-11-16 14:16:44 +0100 | [diff] [blame] | 66 | except subprocess.CalledProcessError: |
| 67 | pass |
Hector Dearman | 3c9b8cf | 2021-09-01 12:56:17 +0100 | [diff] [blame] | 68 | return commit_sha1 |
Primiano Tucci | ec59013 | 2020-11-16 14:16:44 +0100 | [diff] [blame] | 69 | |
| 70 | |
| 71 | def write_if_unchanged(path, content): |
| 72 | prev_content = None |
| 73 | if os.path.exists(path): |
| 74 | with open(path, 'r') as fprev: |
| 75 | prev_content = fprev.read() |
| 76 | if prev_content == content: |
| 77 | return 0 |
| 78 | with open(path, 'w') as fout: |
| 79 | fout.write(content) |
| 80 | |
| 81 | |
| 82 | def main(): |
| 83 | parser = argparse.ArgumentParser() |
Primiano Tucci | dc77d72 | 2021-10-15 13:17:52 +0100 | [diff] [blame] | 84 | parser.add_argument('--check_git', action='store_true') |
Primiano Tucci | ec59013 | 2020-11-16 14:16:44 +0100 | [diff] [blame] | 85 | parser.add_argument( |
| 86 | '--no_git', |
| 87 | action='store_true', |
| 88 | help='Skips running git rev-parse, emits only the version from CHANGELOG') |
| 89 | parser.add_argument('--cpp_out', help='Path of the generated .h file.') |
| 90 | parser.add_argument('--ts_out', help='Path of the generated .ts file.') |
Primiano Tucci | 7eead2e | 2021-02-16 11:46:04 +0100 | [diff] [blame] | 91 | parser.add_argument('--stdout', help='Write to stdout', action='store_true') |
Primiano Tucci | f0ed1d4 | 2020-11-18 16:30:18 +0100 | [diff] [blame] | 92 | parser.add_argument('--changelog', help='Path to CHANGELOG.') |
Primiano Tucci | ec59013 | 2020-11-16 14:16:44 +0100 | [diff] [blame] | 93 | args = parser.parse_args() |
| 94 | |
Primiano Tucci | dc77d72 | 2021-10-15 13:17:52 +0100 | [diff] [blame] | 95 | if args.check_git: |
| 96 | has_git = os.path.exists(os.path.join(PROJECT_ROOT, '.git', 'HEAD')) |
| 97 | print('1' if has_git else '0') |
| 98 | return 0 |
| 99 | |
Primiano Tucci | f0ed1d4 | 2020-11-18 16:30:18 +0100 | [diff] [blame] | 100 | release = get_latest_release(args.changelog) |
Primiano Tucci | de775ba | 2021-09-07 10:20:52 +0100 | [diff] [blame] | 101 | |
Primiano Tucci | ec59013 | 2020-11-16 14:16:44 +0100 | [diff] [blame] | 102 | if args.no_git: |
Hector Dearman | 3c9b8cf | 2021-09-01 12:56:17 +0100 | [diff] [blame] | 103 | head_sha1 = SCM_REV_NOT_AVAILABLE |
Primiano Tucci | ec59013 | 2020-11-16 14:16:44 +0100 | [diff] [blame] | 104 | else: |
Primiano Tucci | de775ba | 2021-09-07 10:20:52 +0100 | [diff] [blame] | 105 | head_sha1 = get_git_sha1('HEAD') # SCM_REV_NOT_AVAILABLE on failure. |
Primiano Tucci | ec59013 | 2020-11-16 14:16:44 +0100 | [diff] [blame] | 106 | |
Primiano Tucci | de775ba | 2021-09-07 10:20:52 +0100 | [diff] [blame] | 107 | if head_sha1 == SCM_REV_NOT_AVAILABLE: |
Primiano Tucci | ec59013 | 2020-11-16 14:16:44 +0100 | [diff] [blame] | 108 | version = release # e.g., 'v9.0'. |
Hector Dearman | 3c9b8cf | 2021-09-01 12:56:17 +0100 | [diff] [blame] | 109 | else: |
Primiano Tucci | de775ba | 2021-09-07 10:20:52 +0100 | [diff] [blame] | 110 | sha1_abbrev = head_sha1[:9] |
| 111 | version = f'{release}-{sha1_abbrev}' # e.g., 'v9.0-adeadbeef'. |
Primiano Tucci | ec59013 | 2020-11-16 14:16:44 +0100 | [diff] [blame] | 112 | |
| 113 | if args.cpp_out: |
| 114 | guard = '%s_' % args.cpp_out.upper() |
| 115 | guard = re.sub(r'[^\w]', '_', guard) |
| 116 | lines = [] |
Florian Mayer | 5ed03d1 | 2021-04-19 18:07:52 +0100 | [diff] [blame] | 117 | lines.append('// Generated by %s' % os.path.basename(__file__)) |
Primiano Tucci | ec59013 | 2020-11-16 14:16:44 +0100 | [diff] [blame] | 118 | lines.append('') |
| 119 | lines.append('#ifndef %s' % guard) |
| 120 | lines.append('#define %s' % guard) |
| 121 | lines.append('') |
| 122 | lines.append('#define PERFETTO_VERSION_STRING() "%s"' % version) |
Hector Dearman | 3c9b8cf | 2021-09-01 12:56:17 +0100 | [diff] [blame] | 123 | lines.append('#define PERFETTO_VERSION_SCM_REVISION() "%s"' % head_sha1) |
Primiano Tucci | ec59013 | 2020-11-16 14:16:44 +0100 | [diff] [blame] | 124 | lines.append('') |
| 125 | lines.append('#endif // %s' % guard) |
| 126 | lines.append('') |
| 127 | content = '\n'.join(lines) |
| 128 | write_if_unchanged(args.cpp_out, content) |
| 129 | |
| 130 | if args.ts_out: |
| 131 | lines = [] |
| 132 | lines.append('export const VERSION = "%s";' % version) |
Hector Dearman | 3c9b8cf | 2021-09-01 12:56:17 +0100 | [diff] [blame] | 133 | lines.append('export const SCM_REVISION = "%s";' % head_sha1) |
Primiano Tucci | ec59013 | 2020-11-16 14:16:44 +0100 | [diff] [blame] | 134 | content = '\n'.join(lines) |
| 135 | write_if_unchanged(args.ts_out, content) |
| 136 | |
Primiano Tucci | 7eead2e | 2021-02-16 11:46:04 +0100 | [diff] [blame] | 137 | if args.stdout: |
| 138 | print(version) |
| 139 | |
Primiano Tucci | ec59013 | 2020-11-16 14:16:44 +0100 | [diff] [blame] | 140 | |
| 141 | if __name__ == '__main__': |
| 142 | sys.exit(main()) |