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:
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
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"
31 abi = r'\b(?:nacl_abi_|NaClAbi|NACL_ABI_)([A-Za-z0-9_]*)'
32 cabi = re.compile(abi)
34 inc = (r'^#\s*include\s+"native_client(?:/src/trusted/service_runtime)?'+
36 cinc = re.compile(inc)
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
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),
49 print >>outstr, cabi.sub(r'\1', line),
51 if not include_guard_found:
52 print >>sys.stderr, 'No include guard found in', instr.name
56 def ProcessDir(srcdir, dstdir):
57 if not os.path.isdir(srcdir):
60 if not os.path.isdir(dstdir):
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),
69 elif os.path.isdir(srcpath):
70 ProcessDir(srcpath, dstpath)
77 print >>sys.stderr, ('Usage: ./export_header source/include/path'
81 ProcessDir(argv[1], argv[2])
85 if __name__ == '__main__':
86 sys.exit(main(sys.argv))