diff options
author | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-04 19:08:17 +0000 |
---|---|---|
committer | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-04 19:08:17 +0000 |
commit | 0837d0c2ed18e58cffa2d2cb57d6864201a11354 (patch) | |
tree | 24fa0e975ad0fc257f60f2a3c50ca5c97381833e /tools/protoc_wrapper | |
parent | a4dd60ac689f3cf63c426da79c8a788a68f13c44 (diff) | |
download | chromium_src-0837d0c2ed18e58cffa2d2cb57d6864201a11354.zip chromium_src-0837d0c2ed18e58cffa2d2cb57d6864201a11354.tar.gz chromium_src-0837d0c2ed18e58cffa2d2cb57d6864201a11354.tar.bz2 |
Linux: update protoc wrapper to allow building with system protobuf.
Now when using system protobuf the wrapper copies all relevant
.proto files to a temporary directory and removes parts not supported
by system protobuf.
No rewriting is done when using bundled protobuf (default).
BUG=157155
Review URL: https://codereview.chromium.org/11418310
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@171015 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'tools/protoc_wrapper')
-rwxr-xr-x | tools/protoc_wrapper/protoc_wrapper.py | 59 |
1 files changed, 54 insertions, 5 deletions
diff --git a/tools/protoc_wrapper/protoc_wrapper.py b/tools/protoc_wrapper/protoc_wrapper.py index be7f7a5..69a7aec 100755 --- a/tools/protoc_wrapper/protoc_wrapper.py +++ b/tools/protoc_wrapper/protoc_wrapper.py @@ -3,11 +3,19 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. -"""A simple wrapper for protoc to add includes in generated cpp headers.""" +""" +A simple wrapper for protoc. + +- Adds includes in generated headers. +- Handles building with system protobuf as an option. +""" import optparse +import os.path +import shutil import subprocess import sys +import tempfile PROTOC_INCLUDE_POINT = '// @@protoc_insertion_point(includes)\n' @@ -34,6 +42,30 @@ def ModifyHeader(header_file, extra_header): return 0 +def RewriteProtoFilesForSystemProtobuf(path): + wrapper_dir = tempfile.mkdtemp() + try: + for filename in os.listdir(path): + if not filename.endswith('.proto'): + continue + with open(os.path.join(path, filename), 'r') as src_file: + with open(os.path.join(wrapper_dir, filename), 'w') as dst_file: + for line in src_file: + # Remove lines that break build with system protobuf. + # We cannot optimize for lite runtime, because system lite runtime + # does not have a Chromium-specific hack to retain unknown fields. + # Similarly, it does not understand corresponding option to control + # the usage of that hack. + if 'LITE_RUNTIME' in line or 'retain_unknown_fields' in line: + continue + dst_file.write(line) + + return wrapper_dir + except: + shutil.rmtree(wrapper_dir) + raise + + def main(argv): parser = optparse.OptionParser() parser.add_option('--include', dest='extra_header', @@ -42,14 +74,31 @@ def main(argv): parser.add_option('--protobuf', dest='generated_header', help='The c++ protobuf header to add the extra header to. ' 'This must be specified along with --include.') + parser.add_option('--proto-in-dir', + help='The directory containing .proto files.') + parser.add_option('--proto-in-file', help='Input file to compile.') + parser.add_option('--use-system-protobuf', type=int, default=0, + help='Option to use system-installed protobuf ' + 'instead of bundled one.') (options, args) = parser.parse_args(sys.argv) if len(args) < 2: return 1 - # Run what is hopefully protoc. - ret = subprocess.call(args[1:]) - if ret != 0: - return ret + proto_path = options.proto_in_dir + if options.use_system_protobuf == 1: + proto_path = RewriteProtoFilesForSystemProtobuf(proto_path) + try: + # Run what is hopefully protoc. + protoc_args = args[1:] + protoc_args += ['--proto_path=%s' % proto_path, + os.path.join(proto_path, options.proto_in_file)] + ret = subprocess.call(protoc_args) + if ret != 0: + return ret + finally: + if options.use_system_protobuf == 1: + # Remove temporary directory holding re-written files. + shutil.rmtree(proto_path) # protoc succeeded, check to see if the generated cpp header needs editing. if not options.extra_header or not options.generated_header: |