Fixes compile error
[chromium-blink-merge.git] / build / protoc_java.py
blob3ccbaa77215d197e37efb05481c3ac67bb96d3ba
1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium 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.
6 """Generate java source files from protobuf files.
8 Usage:
9 protoc_java.py {protoc} {proto_path} {java_out} {proto_runtime} \
10 {stamp_file} {proto_files}
12 This is a helper file for the genproto_java action in protoc_java.gypi.
14 It performs the following steps:
15 1. Deletes all old sources (ensures deleted classes are not part of new jars).
16 2. Creates source directory.
17 3. Generates Java files using protoc.
18 4. Creates a new stamp file.
20 proto_runtime must be one of 'nano' and 'lite'.
21 """
23 import os
24 import shutil
25 import subprocess
26 import sys
28 def main(argv):
29 if len(argv) < 6:
30 usage()
31 return 1
33 protoc_path, proto_path, java_out, proto_runtime, stamp_file = argv[1:6]
34 proto_files = argv[6:]
36 # Delete all old sources.
37 if os.path.exists(java_out):
38 shutil.rmtree(java_out)
40 # Create source directory.
41 os.makedirs(java_out)
43 # Figure out which runtime to use.
44 if proto_runtime == 'nano':
45 out_arg = '--javanano_out=optional_field_style=reftypes,' + \
46 'store_unknown_fields=true:' + java_out
47 elif proto_runtime == 'lite':
48 out_arg = '--java_out=' + java_out
49 else:
50 usage()
51 return 1
53 # Generate Java files using protoc.
54 ret = subprocess.call(
55 [protoc_path, '--proto_path', proto_path, out_arg] + proto_files)
57 if ret == 0:
58 # Create a new stamp file.
59 with file(stamp_file, 'a'):
60 os.utime(stamp_file, None)
62 return ret
64 def usage():
65 print(__doc__);
67 if __name__ == '__main__':
68 sys.exit(main(sys.argv))