| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2014 the V8 project authors. All rights reserved. | 2 # Copyright 2014 the V8 project authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """ | 6 """ |
| 7 Script to set v8's version file to the version given by the latest tag. | 7 Script to set v8's version file to the version given by the latest tag. |
| 8 | |
| 9 The script can be run in two modes: | |
| 10 1) As a gclient hook with the option --hook. | |
| 11 The script will write a temporary version file into the checkout. | |
| 12 2) During compilation as an action. | |
| 13 The script will write version.cc in the output folder based on the | |
| 14 tag info. In case of a failure it will fall back to the temporary file | |
| 15 from the hook call. | |
| 16 | |
| 17 In most cases, 2) will succeed and the temporary information from 1) won't | |
| 18 be used. In case the checkout is copied somewhere and the git context is | |
| 19 lost (e.g. on the android_aosp builder), the temporary file from 1) is | |
| 20 required. | |
| 21 """ | 8 """ |
| 22 | 9 |
| 23 | 10 |
| 24 import optparse | |
| 25 import os | 11 import os |
| 26 import re | 12 import re |
| 27 import subprocess | 13 import subprocess |
| 28 import sys | 14 import sys |
| 29 | 15 |
| 30 | 16 |
| 31 CWD = os.path.abspath( | 17 CWD = os.path.abspath( |
| 32 os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) | 18 os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) |
| 33 VERSION_CC = os.path.join(CWD, "src", "version.cc") | 19 VERSION_CC = os.path.join(CWD, "src", "version.cc") |
| 34 TMP_VERSION_CC = os.path.join(CWD, ".version.cc") | |
| 35 | 20 |
| 36 | 21 def main(): |
| 37 def generate_version_file(): | |
| 38 tag = subprocess.check_output( | 22 tag = subprocess.check_output( |
| 39 "git describe --tags", | 23 "git describe --tags", |
| 40 shell=True, | 24 shell=True, |
| 41 cwd=CWD, | 25 cwd=CWD, |
| 42 ).strip() | 26 ).strip() |
| 43 assert tag | 27 assert tag |
| 44 | 28 |
| 45 # Check for commits not exactly matching a tag. Those are candidate builds | 29 # Check for commits not exactly matching a tag. Those are candidate builds |
| 46 # for the next version. The output has the form | 30 # for the next version. The output has the form |
| 47 # <tag name>-<n commits>-<hash>. | 31 # <tag name>-<n commits>-<hash>. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 59 assert len(version_levels) == 4 | 43 assert len(version_levels) == 4 |
| 60 | 44 |
| 61 major, minor, build, patch = version_levels | 45 major, minor, build, patch = version_levels |
| 62 | 46 |
| 63 # Increment build level for candidate builds. | 47 # Increment build level for candidate builds. |
| 64 if candidate == "1": | 48 if candidate == "1": |
| 65 build = str(int(build) + 1) | 49 build = str(int(build) + 1) |
| 66 patch = "0" | 50 patch = "0" |
| 67 | 51 |
| 68 # Modify version.cc with the new values. | 52 # Modify version.cc with the new values. |
| 53 with open(VERSION_CC, "r") as f: |
| 54 text = f.read() |
| 69 output = [] | 55 output = [] |
| 70 with open(VERSION_CC, "r") as f: | 56 for line in text.split("\n"): |
| 71 for line in f: | 57 for definition, substitute in ( |
| 72 for definition, substitute in ( | 58 ("MAJOR_VERSION", major), |
| 73 ("MAJOR_VERSION", major), | 59 ("MINOR_VERSION", minor), |
| 74 ("MINOR_VERSION", minor), | 60 ("BUILD_NUMBER", build), |
| 75 ("BUILD_NUMBER", build), | 61 ("PATCH_LEVEL", patch), |
| 76 ("PATCH_LEVEL", patch), | 62 ("IS_CANDIDATE_VERSION", candidate)): |
| 77 ("IS_CANDIDATE_VERSION", candidate)): | 63 if line.startswith("#define %s" % definition): |
| 78 if line.startswith("#define %s" % definition): | 64 line = re.sub("\d+$", substitute, line) |
| 79 line = re.sub("\d+$", substitute, line) | 65 output.append(line) |
| 80 output.append(line) | 66 with open(VERSION_CC, "w") as f: |
| 81 # Log what was calculated. | 67 f.write("\n".join(output)) |
| 68 |
| 69 # Log what was done. |
| 82 candidate_txt = " (candidate)" if candidate == "1" else "" | 70 candidate_txt = " (candidate)" if candidate == "1" else "" |
| 83 patch_txt = ".%s" % patch if patch != "0" else "" | 71 patch_txt = ".%s" % patch if patch != "0" else "" |
| 84 version_txt = ("%s.%s.%s%s%s" % | 72 version_txt = ("%s.%s.%s%s%s" % |
| 85 (major, minor, build, patch_txt, candidate_txt)) | 73 (major, minor, build, patch_txt, candidate_txt)) |
| 86 print "Modifying version.cc. Set V8 version to %s" % version_txt | 74 print "Modified version.cc. Set V8 version to %s" % version_txt |
| 87 return "".join(output) | |
| 88 | |
| 89 | |
| 90 def delete_tmp_version_file(): | |
| 91 # Make sure a subsequent call to this script doesn't use an outdated | |
| 92 # version file. | |
| 93 if os.path.exists(TMP_VERSION_CC): | |
| 94 os.remove(TMP_VERSION_CC) | |
| 95 | |
| 96 | |
| 97 def main(): | |
| 98 parser = optparse.OptionParser() | |
| 99 parser.add_option("--hook", | |
| 100 help="Run as a gclient hook", | |
| 101 default=False, action="store_true") | |
| 102 (options, args) = parser.parse_args() | |
| 103 | |
| 104 if options.hook: | |
| 105 version_out = TMP_VERSION_CC | |
| 106 else: | |
| 107 if len(args) != 1: | |
| 108 print "Error: Specify the output file path for version.cc" | |
| 109 return 1 | |
| 110 version_out = args[0] | |
| 111 | |
| 112 assert os.path.exists(os.path.dirname(version_out)) | |
| 113 | |
| 114 try: | |
| 115 version_file_content = generate_version_file() | |
| 116 except Exception as e: | |
| 117 # Allow exceptions when run during compilation. E.g. there might be no git | |
| 118 # context availabe. When run as a gclient hook, generation must succeed. | |
| 119 if options.hook: | |
| 120 delete_tmp_version_file() | |
| 121 raise e | |
| 122 # Assume the script already ran as a hook. | |
| 123 print "No git context available. Using V8 version from hook." | |
| 124 assert os.path.exists(TMP_VERSION_CC) | |
| 125 with open(TMP_VERSION_CC, "r") as f: | |
| 126 version_file_content = f.read() | |
| 127 | |
| 128 delete_tmp_version_file() | |
| 129 with open(version_out, "w") as f: | |
| 130 f.write(version_file_content) | |
| 131 return 0 | 75 return 0 |
| 132 | 76 |
| 133 if __name__ == "__main__": | 77 if __name__ == "__main__": |
| 134 sys.exit(main()) | 78 sys.exit(main()) |
| OLD | NEW |