Upstream version 9.38.198.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 two transformations are to remove nacl_abi_
21   prefixes (in its various incarnations), and to change include
22   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
27   pat = r'\b(?:nacl_abi_|NaClAbi|NACL_ABI_)([A-Za-z0-9_]*)'
28   cpat = re.compile(pat)
29
30   inc = (r'^#\s*include\s+"native_client(?:/src/trusted/service_runtime)?'+
31          r'/include/([^"]*)"')
32   cinc = re.compile(inc)
33
34   for line in instr:
35     if cinc.search(line):
36       print >>outstr, cinc.sub(r'#include <\1>', line)
37     else:
38       print >>outstr, cpat.sub(r'\1', line),
39     # endif
40   # endfor
41 # enddef
42
43
44 def ProcessDir(srcdir, dstdir):
45   if not os.path.isdir(srcdir):
46     return
47   # endif
48   if not os.path.isdir(dstdir):
49     os.makedirs(dstdir)
50   # endif
51   for fn in os.listdir(srcdir):
52     srcpath = os.path.join(srcdir, fn)
53     dstpath = os.path.join(dstdir, fn)
54     if os.path.isfile(srcpath) and fn.endswith('.h'):
55       ProcessStream(open(srcpath),
56                     open(dstpath, 'w'))
57     elif os.path.isdir(srcpath):
58       ProcessDir(srcpath, dstpath)
59     # endif
60   # endfor
61 # enddef
62
63 def main(argv):
64   if len(argv) != 3:
65     print >>sys.stderr, ('Usage: ./export_header source/include/path'
66                          ' dest/include/path')
67     return 1
68   # endif
69   ProcessDir(argv[1], argv[2])
70   return 0
71 # enddef
72
73 if __name__ == '__main__':
74   sys.exit(main(sys.argv))
75 # endif