blob: 1a5471a83c60caac2aa0b931d37d27dad817224a [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001#!/usr/bin/env python
2#
3
4import sys, cpp, kernel, glob, os, re, getopt
5from defaults import *
6from utils import *
7
8noUpdate = 1
9
Frank Maker7b6795d2011-05-25 11:07:04 -070010def cleanupFile( path, original_path):
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080011 """reads an original header and perform the cleanup operation on it
12 this functions returns the destination path and the clean header
13 as a single string"""
14 # check the header path
David 'Digit' Turnerfc269312010-10-11 22:11:06 +020015 src_path = path
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080016
17 if not os.path.exists(src_path):
18 if noUpdate:
19 panic( "file does not exist: '%s'\n" % path )
20 sys.stderr.write( "warning: file does not exit: %s\n" % path )
21 return None, None
22
23 if not os.path.isfile(src_path):
24 if noUpdate:
25 panic( "path is not a file: '%s'\n" % path )
26 sys.stderr.write( "warning: not a file: %s\n" % path )
27 return None, None
28
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080029 if os.path.commonprefix( [ src_path, original_path ] ) != original_path:
30 if noUpdate:
31 panic( "file is not in 'original' directory: %s\n" % path );
32 sys.stderr.write( "warning: file not in 'original' ignored: %s\n" % path )
33 return None, None
34
35 src_path = src_path[len(original_path):]
36 if len(src_path) > 0 and src_path[0] == '/':
37 src_path = src_path[1:]
38
39 if len(src_path) == 0:
Glenn Kastenc61f9902011-12-19 11:27:50 -080040 panic( "oops, internal error, can't extract correct relative path\n" )
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041
42 # convert into destination path, extracting architecture if needed
43 # and the corresponding list of known static functions
44 #
45 arch = None
46 re_asm_arch = re.compile( r"asm-([\w\d_\+\.\-]+)(/.*)" )
47 m = re_asm_arch.match(src_path)
48 statics = kernel_known_generic_statics
49 if m and m.group(1) != 'generic':
50 dst_path = "arch-%s/asm/%s" % m.groups()
51 arch = m.group(1)
52 statics = statics.union( kernel_known_statics.get( arch, set() ) )
53 else:
54 dst_path = "common/" + src_path
55
David 'Digit' Turnerfc269312010-10-11 22:11:06 +020056 dst_path = os.path.normpath( kernel_cleaned_path + "/" + dst_path )
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080057
58 # now, let's parse the file
59 #
David 'Digit' Turnerfc269312010-10-11 22:11:06 +020060 blocks = cpp.BlockParser().parseFile(path)
61 if not blocks:
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080062 sys.stderr.write( "error: can't parse '%s'" % path )
63 sys.exit(1)
64
Andrew Hsieh126601d2012-03-23 23:07:36 +080065 macros = kernel_known_macros.copy()
66 if arch and arch in kernel_default_arch_macros:
67 macros.update(kernel_default_arch_macros[arch])
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080068
Raghu Gandhama864c2c2013-01-16 16:42:47 -080069 if arch and arch in kernel_arch_token_replacements:
70 blocks.replaceTokens( kernel_arch_token_replacements[arch] )
71
Andrew Hsieh126601d2012-03-23 23:07:36 +080072 blocks.optimizeMacros( macros )
David 'Digit' Turnerfc269312010-10-11 22:11:06 +020073 blocks.optimizeIf01()
74 blocks.removeVarsAndFuncs( statics )
75 blocks.replaceTokens( kernel_token_replacements )
76 blocks.removeComments()
77 blocks.removeMacroDefines( kernel_ignored_macros )
78 blocks.removeWhiteSpace()
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080079
80 out = StringOutput()
David 'Digit' Turnerfc269312010-10-11 22:11:06 +020081 out.write( kernel_disclaimer )
82 blocks.writeWithWarning(out, kernel_warning, 4)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080083 return dst_path, out.get()
84
85
86if __name__ == "__main__":
87
88 def usage():
89 print """\
90 usage: %s [options] <header_path>
91
92 options:
93 -v enable verbose mode
94
95 -u enabled update mode
96 this will try to update the corresponding 'clean header'
97 if the content has changed. with this, you can pass more
98 than one file on the command-line
99
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200100 -k<path> specify path of original kernel headers
101 -d<path> specify path of cleaned kernel headers
102
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800103 <header_path> must be in a subdirectory of 'original'
104 """ % os.path.basename(sys.argv[0])
105 sys.exit(1)
106
107 try:
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200108 optlist, args = getopt.getopt( sys.argv[1:], 'uvk:d:' )
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800109 except:
110 # unrecognized option
111 sys.stderr.write( "error: unrecognized option\n" )
112 usage()
113
114 for opt, arg in optlist:
115 if opt == '-u':
116 noUpdate = 0
117 elif opt == '-v':
118 verbose = 1
119 D_setlevel(1)
Dima Zavin4c4a9632009-08-05 17:55:30 -0700120 elif opt == '-k':
121 kernel_original_path = arg
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200122 elif opt == '-d':
123 kernel_cleaned_path = arg
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800124
125 if len(args) == 0:
126 usage()
127
128 if noUpdate:
129 for path in args:
Frank Maker7b6795d2011-05-25 11:07:04 -0700130 dst_path, newdata = cleanupFile(path,kernel_original_path)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800131 print newdata
132
133 sys.exit(0)
134
135 # now let's update our files.
136
137 b = BatchFileUpdater()
138
139 for path in args:
Frank Maker7b6795d2011-05-25 11:07:04 -0700140 dst_path, newdata = cleanupFile(path,kernel_original_path)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800141 if not dst_path:
142 continue
143
144 b.readFile( dst_path )
145 r = b.editFile( dst_path, newdata )
146 if r == 0:
147 r = "unchanged"
148 elif r == 1:
149 r = "edited"
150 else:
151 r = "added"
152
153 print "cleaning: %-*s -> %-*s (%s)" % ( 35, path, 35, dst_path, r )
154
155
David 'Digit' Turnerfc269312010-10-11 22:11:06 +0200156 b.updateGitFiles()
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800157
158 sys.exit(0)