Update To 11.40.268.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
22   1) Remove nacl_abi_ prefixes (in its various incarnations)
23
24   2) Change include directives from the Google coding style
25      "native_client/include/foo/bar.h" to <foo/bar.h>, and from
26      "native_client/src/trusted/service_runtime/include/baz/quux.h" to
27      <baz/quux.h>.
28
29   3) Change include guards from
30      "#ifdef NATIVE_CLIENT_SRC_TRUSTED_SERVICE_RUNTIME_FOO_H" to
31      "#ifdef EXPORT_SRC_TRUSTED_SERVICE_RUNTIME_FOO_H"
32
33   4) Replace "defined(NACL_IN_TOOLCHAIN_HEADERS)" with "1", so that
34      the header can conditionalize based on whether it is being used
35      as headers for a toolchain.  Note that doing "#ifdef
36      __native_client__" in the headers is not good because untrusted
37      code can directly #include from service_runtime/include/ just as
38      trusted code can.
39   """
40
41   abi = r'\b(?:nacl_abi_|NaClAbi|NACL_ABI_)([A-Za-z0-9_]*)'
42   cabi = re.compile(abi)
43
44   inc = (r'^#\s*include\s+"native_client(?:/src/trusted/service_runtime)?'+
45          r'/include/([^"]*)"')
46   cinc = re.compile(inc)
47
48   in_toolchain_sym = re.compile(r'defined\(NACL_IN_TOOLCHAIN_HEADERS\)')
49
50   inc_guard = r'^#\s*(ifndef|define)\s+_?NATIVE_CLIENT[A-Z_]+_H'
51   cinc_guard = re.compile(inc_guard)
52   include_guard_found = False
53
54   for line in instr:
55     line = in_toolchain_sym.sub('(1 /* NACL_IN_TOOLCHAIN_HEADERS */)', line)
56     if cinc.search(line):
57       print >>outstr, cinc.sub(r'#include <\1>', line)
58     elif cinc_guard.match(line):
59       include_guard_found = True
60       print >>outstr, re.sub('NATIVE_CLIENT', 'EXPORT', line),
61     else:
62       print >>outstr, cabi.sub(r'\1', line),
63
64   if not include_guard_found:
65     print >>sys.stderr, 'No include guard found in', instr.name
66     sys.exit(1)
67
68
69 def ProcessDir(srcdir, dstdir):
70   if not os.path.isdir(srcdir):
71     return
72   # endif
73   if not os.path.isdir(dstdir):
74     os.makedirs(dstdir)
75   # endif
76   for fn in os.listdir(srcdir):
77     srcpath = os.path.join(srcdir, fn)
78     dstpath = os.path.join(dstdir, fn)
79     if os.path.isfile(srcpath) and fn.endswith('.h'):
80       ProcessStream(open(srcpath),
81                     open(dstpath, 'w'))
82     elif os.path.isdir(srcpath):
83       ProcessDir(srcpath, dstpath)
84     # endif
85   # endfor
86 # enddef
87
88 def main(argv):
89   if len(argv) != 3:
90     print >>sys.stderr, ('Usage: ./export_header source/include/path'
91                          ' dest/include/path')
92     return 1
93   # endif
94   ProcessDir(argv[1], argv[2])
95   return 0
96 # enddef
97
98 if __name__ == '__main__':
99   sys.exit(main(sys.argv))
100 # endif