Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1456)

Unified Diff: build/util/lastchange.py

Issue 46003002: Add a target in GN that generates a last_change.h file for the SVN revision. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review comments Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | tools/gn/BUILD.gn » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: build/util/lastchange.py
diff --git a/build/util/lastchange.py b/build/util/lastchange.py
index 3c1ce28e28ae33965546328a4c338af64a4792c4..8d758d2a24fdfa6daefada92491d739e055e5b7e 100755
--- a/build/util/lastchange.py
+++ b/build/util/lastchange.py
@@ -168,6 +168,42 @@ def FetchVersionInfo(default_lastchange, directory=None,
version_info = VersionInfo(None, None)
return version_info
+def GetHeaderGuard(path):
+ """
+ Returns the header #define guard for the given file path.
+ This treats everything after the last instance of "src/" as being a
+ relevant part of the guard. If there is no "src/", then the entire path
+ is used.
+ """
+ src_index = path.rfind('src/')
+ if src_index != -1:
+ guard = path[src_index + 4:]
+ else:
+ guard = path
+ guard = guard.upper()
+ return guard.replace('/', '_').replace('.', '_').replace('\\', '_') + '_'
+
+def GetHeaderContents(path, define, version):
+ """
+ Returns what the contents of the header file should be that indicate the given
+ revision. Note that the #define is specified as a string, even though it's
+ currently always a SVN revision number, in case we need to move to git hashes.
+ """
+ header_guard = GetHeaderGuard(path)
+
+ header_contents = """/* Generated by lastchange.py, do not edit.*/
+
+#ifndef %(header_guard)s
+#define %(header_guard)s
+
+#define %(define)s "%(version)s"
+
+#endif // %(header_guard)s
+"""
+ header_contents = header_contents % { 'header_guard': header_guard,
+ 'define': define,
+ 'version': version }
+ return header_contents
def WriteIfChanged(file_name, contents):
"""
@@ -191,16 +227,26 @@ def main(argv=None):
parser = optparse.OptionParser(usage="lastchange.py [options]")
parser.add_option("-d", "--default-lastchange", metavar="FILE",
- help="default last change input FILE")
+ help="Default last change input FILE.")
+ parser.add_option("-m", "--version-macro",
+ help="Name of C #define when using --header. Defaults to " +
+ "LAST_CHANGE.",
+ default="LAST_CHANGE")
parser.add_option("-o", "--output", metavar="FILE",
- help="write last change to FILE")
+ help="Write last change to FILE. " +
+ "Can be combined with --header to write both files.")
+ parser.add_option("", "--header", metavar="FILE",
+ help="Write last change to FILE as a C/C++ header. " +
+ "Can be combined with --output to write both files.")
parser.add_option("--revision-only", action='store_true',
- help="just print the SVN revision number")
+ help="Just print the SVN revision number. Overrides any " +
+ "file-output-related options.")
parser.add_option("-s", "--source-dir", metavar="DIR",
- help="use repository in the given directory")
+ help="Use repository in the given directory.")
opts, args = parser.parse_args(argv[1:])
out_file = opts.output
+ header = opts.header
while len(args) and out_file is None:
if out_file is None:
@@ -224,10 +270,15 @@ def main(argv=None):
print version_info.revision
else:
contents = "LASTCHANGE=%s\n" % version_info.revision
- if out_file:
- WriteIfChanged(out_file, contents)
- else:
+ if not out_file and not opts.header:
sys.stdout.write(contents)
+ else:
+ if out_file:
+ WriteIfChanged(out_file, contents)
+ if header:
+ WriteIfChanged(header,
+ GetHeaderContents(header, opts.version_macro,
+ version_info.revision))
return 0
« no previous file with comments | « no previous file | tools/gn/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698