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.
6 """Tools for exporting Native Client ABI header files.
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.
18 def ProcessStream(instr, outstr):
19 """Read internal version of header file from instr, write exported
20 version to outstr. The transformations are:
22 1) Remove nacl_abi_ prefixes (in its various incarnations)
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
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"
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
41 abi = r'\b(?:nacl_abi_|NaClAbi|NACL_ABI_)([A-Za-z0-9_]*)'
42 cabi = re.compile(abi)
44 inc = (r'^#\s*include\s+"native_client(?:/src/trusted/service_runtime)?'+
46 cinc = re.compile(inc)
48 in_toolchain_sym = re.compile(r'defined\(NACL_IN_TOOLCHAIN_HEADERS\)')
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
55 line = in_toolchain_sym.sub('(1 /* NACL_IN_TOOLCHAIN_HEADERS */)', 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),
62 print >>outstr, cabi.sub(r'\1', line),
64 if not include_guard_found:
65 print >>sys.stderr, 'No include guard found in', instr.name
69 def ProcessDir(srcdir, dstdir):
70 if not os.path.isdir(srcdir):
73 if not os.path.isdir(dstdir):
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),
82 elif os.path.isdir(srcpath):
83 ProcessDir(srcpath, dstpath)
90 print >>sys.stderr, ('Usage: ./export_header source/include/path'
94 ProcessDir(argv[1], argv[2])
98 if __name__ == '__main__':
99 sys.exit(main(sys.argv))