Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / native_client / src / trusted / service_runtime / export_header.py
1 #!/usr/bin/python
2 # Copyright (c) 2008 The Native Client 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 """Tools for exporting Native Client ABI header files.
7
8 This module is used to export Native Client ABI header files -- which
9 are in the native_client/src/trusted/service_runtime/include directory
10 -- for use with the SDK and newlib to compile NaCl applications.
11 """
12
13 import os
14 import re
15 import sys
16
17
18 def ProcessStream(instr, outstr):
19   """Read internal version of header file from instr, write exported
20   version to outstr.  The transformations are:
21   1) remove nacl_abi_ prefixes (in its various incarnations)
22   2) change include directives from the Google coding style
23      "native_client/include/foo/bar.h" to <foo/bar.h>, and from
24      "native_client/src/trusted/service_runtime/include/baz/quux.h" to
25      <baz/quux.h>.
26   3) change include guards from
27      "#ifdef NATIVE_CLIENT_SRC_TRUSTED_SERVICE_RUNTIME_FOO_H" to
28      "#ifdef EXPORT_SRC_TRUSTED_SERVICE_RUNTIME_FOO_H"
29   """
30
31   abi = r'\b(?:nacl_abi_|NaClAbi|NACL_ABI_)([A-Za-z0-9_]*)'
32   cabi = re.compile(abi)
33
34   inc = (r'^#\s*include\s+"native_client(?:/src/trusted/service_runtime)?'+
35          r'/include/([^"]*)"')
36   cinc = re.compile(inc)
37
38   inc_guard = r'^#\s*(ifndef|define)\s+_?NATIVE_CLIENT[A-Z_]+_H'
39   cinc_guard = re.compile(inc_guard)
40   include_guard_found = False
41
42   for line in instr:
43     if cinc.search(line):
44       print >>outstr, cinc.sub(r'#include <\1>', line)
45     elif cinc_guard.match(line):
46       include_guard_found = True
47       print >>outstr, re.sub('NATIVE_CLIENT', 'EXPORT', line),
48     else:
49       print >>outstr, cabi.sub(r'\1', line),
50
51   if not include_guard_found:
52     print >>sys.stderr, 'No include guard found in', instr.name
53     sys.exit(1)
54
55
56 def ProcessDir(srcdir, dstdir):
57   if not os.path.isdir(srcdir):
58     return
59   # endif
60   if not os.path.isdir(dstdir):
61     os.makedirs(dstdir)
62   # endif
63   for fn in os.listdir(srcdir):
64     srcpath = os.path.join(srcdir, fn)
65     dstpath = os.path.join(dstdir, fn)
66     if os.path.isfile(srcpath) and fn.endswith('.h'):
67       ProcessStream(open(srcpath),
68                     open(dstpath, 'w'))
69     elif os.path.isdir(srcpath):
70       ProcessDir(srcpath, dstpath)
71     # endif
72   # endfor
73 # enddef
74
75 def main(argv):
76   if len(argv) != 3:
77     print >>sys.stderr, ('Usage: ./export_header source/include/path'
78                          ' dest/include/path')
79     return 1
80   # endif
81   ProcessDir(argv[1], argv[2])
82   return 0
83 # enddef
84
85 if __name__ == '__main__':
86   sys.exit(main(sys.argv))
87 # endif