OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
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 | |
4 # found in the LICENSE file. | |
5 | |
6 """ | |
7 Script to set v8's version file to the version given by the latest tag. | |
8 | |
9 The script is intended to be run as a gclient hook. The script will write a | |
10 generated version file into the checkout. | |
11 | |
12 On build systems where no git is available and where the version information | |
13 is not necessary, the version generation can be bypassed with the option | |
14 --skip. | |
15 """ | |
16 | |
17 | |
18 import optparse | |
19 import os | |
20 import re | |
21 import subprocess | |
22 import sys | |
23 | |
24 | |
25 CWD = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) | |
26 VERSION_CC = os.path.join(CWD, "src", "version.cc") | |
27 VERSION_GEN_CC = os.path.join(CWD, "src", "version_gen.cc") | |
28 | |
29 VERSION_RE_RAW = r"^(?P<major>\d+)\.(?P<minor>\d+)\.(?P<build>\d+)" | |
30 VERSION_RE = re.compile(VERSION_RE_RAW + r"$") | |
31 VERSION_WITH_PATCH_RE = re.compile(VERSION_RE_RAW + r"\.(?P<patch>\d+)$") | |
32 | |
33 def generate_version_file(): | |
34 # Make sure the tags are fetched from cached git repos. | |
35 url = subprocess.check_output( | |
36 "git config --get remote.origin.url", | |
37 shell=True, | |
38 cwd=CWD).strip() | |
39 if not url.startswith("http"): | |
40 subprocess.check_output( | |
41 "git fetch origin +refs/tags/*:refs/tags/*", | |
42 shell=True, | |
43 cwd=CWD) | |
44 tag = subprocess.check_output( | |
45 "git describe --tags", | |
46 shell=True, | |
47 cwd=CWD, | |
48 ).strip() | |
49 assert tag | |
50 | |
51 # Check for commits not exactly matching a tag. Those are candidate builds | |
52 # for the next version. The output has the form | |
53 # <tag name>-<n commits>-<hash>. | |
54 if "-" in tag: | |
55 version = tag.split("-")[0] | |
56 candidate = "1" | |
57 else: | |
58 version = tag | |
59 candidate = "0" | |
60 | |
61 match = VERSION_RE.match(version) | |
62 match_patch = VERSION_WITH_PATCH_RE.match(version) | |
63 if match: | |
64 # Simple version e.g. "3.30.5". | |
65 major = match.group("major") | |
66 minor = match.group("minor") | |
67 build = match.group("build") | |
68 patch = "0" | |
69 invalid = "0" | |
70 elif match_patch: | |
71 # Version with patch level e.g. "3.30.5.2". | |
72 major = match.group("major") | |
73 minor = match.group("minor") | |
74 build = match.group("build") | |
75 patch = match.group("patch") | |
76 invalid = "0" | |
77 else: | |
78 # A tag was found that's not a version string. | |
79 major = "0" | |
80 minor = "0" | |
81 build = "0" | |
82 patch = "0" | |
83 invalid = "1" | |
84 | |
85 # Increment build level for candidate builds. | |
86 if candidate == "1" and invalid != "1": | |
87 build = str(int(build) + 1) | |
88 patch = "0" | |
89 | |
90 # Modify version_gen.cc with the new values. | |
91 output = [] | |
92 with open(VERSION_CC, "r") as f: | |
93 for line in f: | |
94 for definition, substitute in ( | |
95 ("MAJOR_VERSION", major), | |
96 ("MINOR_VERSION", minor), | |
97 ("BUILD_NUMBER", build), | |
98 ("PATCH_LEVEL", patch), | |
99 ("IS_CANDIDATE_VERSION", candidate), | |
100 ("IS_INVALID_VERSION", invalid)): | |
101 if line.startswith("#define %s" % definition): | |
102 line = re.sub("\d+$", substitute, line) | |
103 output.append(line) | |
104 | |
105 # Prepare log message. | |
106 suffix_txt = " (candidate)" if candidate == "1" else "" | |
107 suffix_txt = " (invalid)" if invalid == "1" else suffix_txt | |
108 patch_txt = ".%s" % patch if patch != "0" else "" | |
109 version_txt = ("%s.%s.%s%s%s" % | |
110 (major, minor, build, patch_txt, suffix_txt)) | |
111 log_message = "Modifying version_gen.cc. Set V8 version to %s" % version_txt | |
112 return "".join(output), log_message | |
113 | |
114 | |
115 def bypass_version_file(): | |
116 with open(VERSION_CC, "r") as f: | |
117 return f.read(), "Bypassing V8 version creation." | |
118 | |
119 | |
120 def main(): | |
121 parser = optparse.OptionParser() | |
122 parser.add_option("--skip", | |
123 help="Use raw version.cc file (disables version " | |
124 "generation and uses a dummy version).", | |
125 default=False, action="store_true") | |
126 (options, args) = parser.parse_args() | |
127 | |
128 if options.skip: | |
129 version_file_content, log_message = bypass_version_file() | |
130 else: | |
131 version_file_content, log_message = generate_version_file() | |
132 | |
133 old_content = "" | |
134 if os.path.exists(VERSION_GEN_CC): | |
135 with open(VERSION_GEN_CC, "r") as f: | |
136 old_content = f.read() | |
137 | |
138 # Only generate version file if content has changed. | |
139 if old_content != version_file_content: | |
140 with open(VERSION_GEN_CC, "w") as f: | |
141 f.write(version_file_content) | |
142 # Log what was calculated. | |
143 print log_message | |
144 | |
145 return 0 | |
146 | |
147 | |
148 if __name__ == "__main__": | |
149 sys.exit(main()) | |
OLD | NEW |