Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / build / protoc_java.py
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.
5
6 """Generate java source files from protobuf files.
7
8 Usage:
9     protoc_java.py {protoc} {proto_path} {java_out} {stamp_file} {proto_files}
10
11 This is a helper file for the genproto_java action in protoc_java.gypi.
12
13 It performs the following steps:
14 1. Deletes all old sources (ensures deleted classes are not part of new jars).
15 2. Creates source directory.
16 3. Generates Java files using protoc.
17 4. Creates a new stamp file.
18 """
19
20 import os
21 import shutil
22 import subprocess
23 import sys
24
25 def main(argv):
26   if len(argv) < 5:
27     usage()
28     return 1
29
30   protoc_path, proto_path, java_out, stamp_file = argv[1:5]
31   proto_files = argv[5:]
32
33   # Delete all old sources.
34   if os.path.exists(java_out):
35     shutil.rmtree(java_out)
36
37   # Create source directory.
38   os.makedirs(java_out)
39
40   # Specify arguments to the generator.
41   generator_args = ['optional_field_style=reftypes',
42                     'store_unknown_fields=true']
43   out_arg = '--javanano_out=' + ','.join(generator_args) + ':' + java_out
44
45   # Generate Java files using protoc.
46   ret = subprocess.call(
47       [protoc_path, '--proto_path', proto_path, out_arg] + proto_files)
48
49   if ret == 0:
50     # Create a new stamp file.
51     with file(stamp_file, 'a'):
52       os.utime(stamp_file, None)
53
54   return ret
55
56 def usage():
57   print(__doc__);
58
59 if __name__ == '__main__':
60   sys.exit(main(sys.argv))