blob: 9b3572a0a2dde31404356238f0e69e84d1e5c40d [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001#!/usr/bin/env python
2#
3# this program is used to find source code that includes linux kernel headers directly
4# (e.g. with #include <linux/...> or #include <asm/...>)
5#
David 'Digit' Turnerfc269312010-10-11 22:11:06 +02006# then it lists them on the standard output.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08007
8import sys, cpp, glob, os, re, getopt, kernel
9from utils import *
10from defaults import *
11
12program_dir = find_program_dir()
13
14wanted_archs = kernel_archs
David 'Digit' Turnerfc269312010-10-11 22:11:06 +020015wanted_config = None
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080016
17def usage():
18 print """\
David 'Digit' Turnerfc269312010-10-11 22:11:06 +020019 usage: find_headers.py [options] <kernel-root> (file|directory|@listfile)+
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080020
21 options:
David 'Digit' Turnerfc269312010-10-11 22:11:06 +020022 -c <file> specify .config file (none by default)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080023
24 -a <archs> used to specify an alternative list
25 of architectures to support
26 ('%s' by default)
27
28 -v enable verbose mode
29
30 this program is used to find all the kernel headers that are used
31 by a set of source files or directories containing them. the search
32 is recursive to find *all* required files.
33
David 'Digit' Turnerfc269312010-10-11 22:11:06 +020034""" % ( string.join(kernel_archs,",") )
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080035 sys.exit(1)
36
37
38try:
David 'Digit' Turnerfc269312010-10-11 22:11:06 +020039 optlist, args = getopt.getopt( sys.argv[1:], 'vc:d:a:k:' )
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080040except:
41 # unrecognized option
42 print "error: unrecognized option"
43 usage()
44
45for opt, arg in optlist:
46 if opt == '-a':
47 wanted_archs = string.split(arg,',')
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080048 elif opt == '-c':
49 wanted_config = arg
50 elif opt == '-v':
51 kernel.verboseSearch = 1
52 kernel.verboseFind = 1
53 verbose = 1
54 else:
55 usage()
56
David 'Digit' Turnerfc269312010-10-11 22:11:06 +020057if len(args) < 2:
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080058 usage()
59
David 'Digit' Turnerfc269312010-10-11 22:11:06 +020060kernel_root = args[0]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080061if not os.path.exists(kernel_root):
62 sys.stderr.write( "error: directory '%s' does not exist\n" % kernel_root )
63 sys.exit(1)
64
65if not os.path.isdir(kernel_root):
66 sys.stderr.write( "error: '%s' is not a directory\n" % kernel_root )
67 sys.exit(1)
68
David 'Digit' Turnerfc269312010-10-11 22:11:06 +020069if not os.path.isdir(kernel_root+"/include/linux"):
70 sys.stderr.write( "error: '%s' does not have an 'include/linux' directory\n" % kernel_root )
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080071 sys.exit(1)
72
David 'Digit' Turnerfc269312010-10-11 22:11:06 +020073if wanted_config:
74 if not os.path.exists(wanted_config):
75 sys.stderr.write( "error: file '%s' does not exist\n" % wanted_config )
76 sys.exit(1)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080077
David 'Digit' Turnerfc269312010-10-11 22:11:06 +020078 if not os.path.isfile(wanted_config):
79 sys.stderr.write( "error: '%s' is not a file\n" % wanted_config )
80 sys.exit(1)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080081
82# find all architectures in the kernel tree
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080083archs = []
David 'Digit' Turnerfc269312010-10-11 22:11:06 +020084for archdir in os.listdir(kernel_root+"/arch"):
85 if os.path.exists("%s/arch/%s/include/asm" % (kernel_root, archdir)):
86 if verbose:
87 print "Found arch '%s'" % archdir
88 archs.append(archdir)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080089
90# if we're using the 'kernel_headers' directory, there is only asm/
Raghu Gandham82fa43f2012-03-27 11:37:17 -070091# and no other asm-<arch> directories
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080092#
93in_kernel_headers = False
94if len(archs) == 0:
95 # this can happen when we're using the 'kernel_headers' directory
96 if os.path.isdir(kernel_root+"/asm"):
97 in_kernel_headers = True
Raghu Gandham82fa43f2012-03-27 11:37:17 -070098 archs = [ "arm", "mips"]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080099
100# if the user has specified some architectures with -a <archs> ensure that
101# all those he wants are available from the kernel include tree
102if wanted_archs != None:
Raghu Gandham82fa43f2012-03-27 11:37:17 -0700103 if in_kernel_headers and wanted_archs != [ "arm", "mips" ]:
104 sys.stderr.write( "error: when parsing kernel_headers, only 'arm' and 'mips' architectures are supported at the moment\n" )
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800105 sys.exit(1)
106 missing = []
107 for arch in wanted_archs:
108 if arch not in archs:
109 missing.append(arch)
110 if len(missing) > 0:
111 sys.stderr.write( "error: the following requested architectures are not in the kernel tree: " )
112 for a in missing:
113 sys.stderr.write( " %s" % a )
114 sys.stderr.write( "\n" )
115 sys.exit(1)
116
117 archs = wanted_archs
118
119# helper function used to walk the user files
120def parse_file(path, parser):
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200121 #print "parse %s" % path
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800122 parser.parseFile(path)
123
124
125# remove previous destination directory
126#destdir = "/tmp/bionic-kernel-headers/"
127#cleanup_dir(destdir)
128
129# try to read the config file
130try:
131 cparser = kernel.ConfigParser()
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200132 if wanted_config:
133 cparser.parseFile( wanted_config )
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800134except:
135 sys.stderr.write( "error: can't parse '%s'" % wanted_config )
136 sys.exit(1)
137
138kernel_config = cparser.getDefinitions()
139
140# first, obtain the list of kernel files used by our clients
141fparser = kernel.HeaderScanner()
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200142dir_excludes=[".repo","external/kernel-headers","ndk","out","prebuilt","bionic/libc/kernel","development/ndk","external/qemu/distrib"]
143walk_source_files( args[1:], parse_file, fparser, excludes=["./"+f for f in dir_excludes] )
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800144headers = fparser.getHeaders()
145files = fparser.getFiles()
146
147# now recursively scan the kernel headers for additionnal sub-included headers
148hparser = kernel.KernelHeaderFinder(headers,archs,kernel_root,kernel_config)
149headers = hparser.scanForAllArchs()
150
151if 0: # just for debugging
152 dumpHeaderUsers = False
153
154 print "the following %d headers:" % len(headers)
155 for h in sorted(headers):
156 if dumpHeaderUsers:
157 print " %s (%s)" % (h, repr(hparser.getHeaderUsers(h)))
158 else:
159 print " %s" % h
160
161 print "are used by the following %d files:" % len(files)
162 for f in sorted(files):
163 print " %s" % f
164
165 sys.exit(0)
166
167for h in sorted(headers):
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200168 print "%s" % h
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800169
170sys.exit(0)