| Elliott Hughes | 6b586e7 | 2021-04-15 13:39:08 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 | 
| Christopher Ferris | 08b6074 | 2014-06-05 11:17:06 -0700 | [diff] [blame] | 2 |  | 
|  | 3 | #------------------------------------------------------------------------------ | 
|  | 4 | # Description of the header clean process | 
|  | 5 | #------------------------------------------------------------------------------ | 
|  | 6 | # Here is the list of actions performed by this script to clean the original | 
|  | 7 | # kernel headers. | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 8 | # | 
| Christopher Ferris | 08b6074 | 2014-06-05 11:17:06 -0700 | [diff] [blame] | 9 | # 1. Optimize well-known macros (e.g. __KERNEL__, __KERNEL_STRICT_NAMES) | 
|  | 10 | # | 
|  | 11 | #     This pass gets rid of everything that is guarded by a well-known macro | 
|  | 12 | #     definition. This means that a block like: | 
|  | 13 | # | 
|  | 14 | #        #ifdef __KERNEL__ | 
|  | 15 | #        .... | 
|  | 16 | #        #endif | 
|  | 17 | # | 
|  | 18 | #     Will be totally omitted from the output. The optimizer is smart enough to | 
|  | 19 | #     handle all complex C-preprocessor conditional expression appropriately. | 
|  | 20 | #     This means that, for example: | 
|  | 21 | # | 
|  | 22 | #        #if defined(__KERNEL__) || defined(FOO) | 
|  | 23 | #        ... | 
|  | 24 | #        #endif | 
|  | 25 | # | 
|  | 26 | #     Will be transformed into: | 
|  | 27 | # | 
|  | 28 | #        #ifdef FOO | 
|  | 29 | #        ... | 
|  | 30 | #        #endif | 
|  | 31 | # | 
|  | 32 | #     See tools/defaults.py for the list of well-known macros used in this pass, | 
|  | 33 | #     in case you need to update it in the future. | 
|  | 34 | # | 
|  | 35 | #     Note that this also removes any reference to a kernel-specific | 
|  | 36 | #     configuration macro like CONFIG_FOO from the clean headers. | 
|  | 37 | # | 
|  | 38 | # | 
|  | 39 | # 2. Remove variable and function declarations: | 
|  | 40 | # | 
|  | 41 | #   This pass scans non-directive text and only keeps things that look like a | 
|  | 42 | #   typedef/struct/union/enum declaration. This allows us to get rid of any | 
|  | 43 | #   variables or function declarations that should only be used within the | 
|  | 44 | #   kernel anyway (and which normally *should* be guarded by an #ifdef | 
|  | 45 | #   __KERNEL__ ...  #endif block, if the kernel writers were not so messy). | 
|  | 46 | # | 
|  | 47 | #   There are, however, a few exceptions: it is seldom useful to keep the | 
|  | 48 | #   definition of some static inline functions performing very simple | 
|  | 49 | #   operations. A good example is the optimized 32-bit byte-swap function | 
|  | 50 | #   found in: | 
|  | 51 | # | 
|  | 52 | #     arch-arm/asm/byteorder.h | 
|  | 53 | # | 
|  | 54 | #   The list of exceptions is in tools/defaults.py in case you need to update | 
|  | 55 | #   it in the future. | 
|  | 56 | # | 
|  | 57 | #   Note that we do *not* remove macro definitions, including these macro that | 
|  | 58 | #   perform a call to one of these kernel-header functions, or even define other | 
|  | 59 | #   functions. We consider it safe since userland applications have no business | 
|  | 60 | #   using them anyway. | 
|  | 61 | # | 
|  | 62 | # | 
| Tao Bao | d7db594 | 2015-01-28 10:07:51 -0800 | [diff] [blame] | 63 | # 3. Add a standard disclaimer: | 
| Christopher Ferris | 08b6074 | 2014-06-05 11:17:06 -0700 | [diff] [blame] | 64 | # | 
|  | 65 | #   The message: | 
|  | 66 | # | 
|  | 67 | #   /* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */ | 
|  | 68 | # | 
|  | 69 | #   Is prepended to each generated header. | 
|  | 70 | #------------------------------------------------------------------------------ | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 71 |  | 
|  | 72 | import sys, cpp, kernel, glob, os, re, getopt | 
|  | 73 | from defaults import * | 
|  | 74 | from utils import * | 
|  | 75 |  | 
| Christopher Ferris | d12c332 | 2015-09-15 14:13:17 -0700 | [diff] [blame] | 76 | def print_error(no_update, msg): | 
|  | 77 | if no_update: | 
|  | 78 | panic(msg) | 
|  | 79 | sys.stderr.write("warning: " + msg) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 80 |  | 
| Christopher Ferris | d12c332 | 2015-09-15 14:13:17 -0700 | [diff] [blame] | 81 |  | 
| Christopher Ferris | 15d3fef | 2017-02-24 12:26:48 -0800 | [diff] [blame] | 82 | def cleanupFile(dst_file, src_file, rel_path, no_update = True): | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 83 | """reads an original header and perform the cleanup operation on it | 
|  | 84 | this functions returns the destination path and the clean header | 
|  | 85 | as a single string""" | 
| Christopher Ferris | 15d3fef | 2017-02-24 12:26:48 -0800 | [diff] [blame] | 86 | # Check the header path | 
|  | 87 | if not os.path.exists(src_file): | 
|  | 88 | print_error(no_update, "'%s' does not exist\n" % src_file) | 
| Daniel Mentz | 6d6b4ce | 2019-04-04 11:51:29 -0700 | [diff] [blame] | 89 | return None | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 90 |  | 
| Christopher Ferris | 15d3fef | 2017-02-24 12:26:48 -0800 | [diff] [blame] | 91 | if not os.path.isfile(src_file): | 
|  | 92 | print_error(no_update, "'%s' is not a file\n" % src_file) | 
| Daniel Mentz | 6d6b4ce | 2019-04-04 11:51:29 -0700 | [diff] [blame] | 93 | return None | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 94 |  | 
| Christopher Ferris | 15d3fef | 2017-02-24 12:26:48 -0800 | [diff] [blame] | 95 | # Extract the architecture if found. | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 96 | arch = None | 
| Christopher Ferris | 15d3fef | 2017-02-24 12:26:48 -0800 | [diff] [blame] | 97 | m = re.search(r"(^|/)asm-([\w\d_\+\.\-]+)/.*", rel_path) | 
|  | 98 | if m and m.group(2) != 'generic': | 
|  | 99 | arch = m.group(2) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 100 |  | 
| Christopher Ferris | 15d3fef | 2017-02-24 12:26:48 -0800 | [diff] [blame] | 101 | # Now, let's parse the file. | 
| Tao Bao | d7db594 | 2015-01-28 10:07:51 -0800 | [diff] [blame] | 102 | parser = cpp.BlockParser() | 
| Christopher Ferris | 15d3fef | 2017-02-24 12:26:48 -0800 | [diff] [blame] | 103 | blocks = parser.parseFile(src_file) | 
| Tao Bao | d7db594 | 2015-01-28 10:07:51 -0800 | [diff] [blame] | 104 | if not parser.parsed: | 
| Christopher Ferris | 15d3fef | 2017-02-24 12:26:48 -0800 | [diff] [blame] | 105 | print_error(no_update, "Can't parse '%s'" % src_file) | 
|  | 106 | return None | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 107 |  | 
| Andrew Hsieh | 126601d | 2012-03-23 23:07:36 +0800 | [diff] [blame] | 108 | macros = kernel_known_macros.copy() | 
|  | 109 | if arch and arch in kernel_default_arch_macros: | 
|  | 110 | macros.update(kernel_default_arch_macros[arch]) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 111 |  | 
| Raghu Gandham | a864c2c | 2013-01-16 16:42:47 -0800 | [diff] [blame] | 112 | if arch and arch in kernel_arch_token_replacements: | 
| Christopher Ferris | d12c332 | 2015-09-15 14:13:17 -0700 | [diff] [blame] | 113 | blocks.replaceTokens(kernel_arch_token_replacements[arch]) | 
| Raghu Gandham | a864c2c | 2013-01-16 16:42:47 -0800 | [diff] [blame] | 114 |  | 
| Christopher Ferris | bb9fcb4 | 2020-04-06 11:38:04 -0700 | [diff] [blame] | 115 | blocks.removeStructs(kernel_structs_to_remove) | 
| Christopher Ferris | d12c332 | 2015-09-15 14:13:17 -0700 | [diff] [blame] | 116 | blocks.optimizeMacros(macros) | 
| David 'Digit' Turner | fc26931 | 2010-10-11 22:11:06 +0200 | [diff] [blame] | 117 | blocks.optimizeIf01() | 
| Elliott Hughes | 64f355f | 2017-08-30 16:10:24 -0700 | [diff] [blame] | 118 | blocks.removeVarsAndFuncs(kernel_known_generic_statics) | 
| Christopher Ferris | d12c332 | 2015-09-15 14:13:17 -0700 | [diff] [blame] | 119 | blocks.replaceTokens(kernel_token_replacements) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 120 |  | 
|  | 121 | out = StringOutput() | 
| Christopher Ferris | d12c332 | 2015-09-15 14:13:17 -0700 | [diff] [blame] | 122 | out.write(kernel_disclaimer) | 
| Elliott Hughes | 96c1db7 | 2017-05-25 13:48:01 -0700 | [diff] [blame] | 123 | blocks.write(out) | 
| Christopher Ferris | 15d3fef | 2017-02-24 12:26:48 -0800 | [diff] [blame] | 124 | return out.get() | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 125 |  | 
|  | 126 |  | 
|  | 127 | if __name__ == "__main__": | 
|  | 128 |  | 
|  | 129 | def usage(): | 
| Christopher Ferris | ac7ec11 | 2021-04-19 13:50:16 -0700 | [diff] [blame] | 130 | print("""\ | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 131 | usage:  %s [options] <header_path> | 
|  | 132 |  | 
|  | 133 | options: | 
|  | 134 | -v    enable verbose mode | 
|  | 135 |  | 
|  | 136 | -u    enabled update mode | 
|  | 137 | this will try to update the corresponding 'clean header' | 
|  | 138 | if the content has changed. with this, you can pass more | 
|  | 139 | than one file on the command-line | 
|  | 140 |  | 
| David 'Digit' Turner | fc26931 | 2010-10-11 22:11:06 +0200 | [diff] [blame] | 141 | -k<path>  specify path of original kernel headers | 
|  | 142 | -d<path>  specify path of cleaned kernel headers | 
|  | 143 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 144 | <header_path> must be in a subdirectory of 'original' | 
| Christopher Ferris | ac7ec11 | 2021-04-19 13:50:16 -0700 | [diff] [blame] | 145 | """ % os.path.basename(sys.argv[0])) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 146 | sys.exit(1) | 
|  | 147 |  | 
|  | 148 | try: | 
| Christopher Ferris | d12c332 | 2015-09-15 14:13:17 -0700 | [diff] [blame] | 149 | optlist, args = getopt.getopt(sys.argv[1:], 'uvk:d:') | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 150 | except: | 
|  | 151 | # unrecognized option | 
| Christopher Ferris | d12c332 | 2015-09-15 14:13:17 -0700 | [diff] [blame] | 152 | sys.stderr.write("error: unrecognized option\n") | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 153 | usage() | 
|  | 154 |  | 
| Christopher Ferris | d12c332 | 2015-09-15 14:13:17 -0700 | [diff] [blame] | 155 | no_update = True | 
| Daniel Mentz | d12d6f6 | 2019-04-15 15:19:31 -0700 | [diff] [blame] | 156 | dst_dir = None | 
|  | 157 | src_dir = None | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 158 | for opt, arg in optlist: | 
|  | 159 | if opt == '-u': | 
| Christopher Ferris | d12c332 | 2015-09-15 14:13:17 -0700 | [diff] [blame] | 160 | no_update = False | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 161 | elif opt == '-v': | 
| Elliott Hughes | dc1fb70 | 2014-08-20 11:16:11 -0700 | [diff] [blame] | 162 | logging.basicConfig(level=logging.DEBUG) | 
| Dima Zavin | 4c4a963 | 2009-08-05 17:55:30 -0700 | [diff] [blame] | 163 | elif opt == '-k': | 
| Christopher Ferris | d12c332 | 2015-09-15 14:13:17 -0700 | [diff] [blame] | 164 | src_dir = arg | 
| David 'Digit' Turner | fc26931 | 2010-10-11 22:11:06 +0200 | [diff] [blame] | 165 | elif opt == '-d': | 
| Christopher Ferris | d12c332 | 2015-09-15 14:13:17 -0700 | [diff] [blame] | 166 | dst_dir = arg | 
| Daniel Mentz | d12d6f6 | 2019-04-15 15:19:31 -0700 | [diff] [blame] | 167 | # get_kernel_dir() and get_kernel_headers_original_dir() require the current | 
|  | 168 | # working directory to be a direct or indirect subdirectory of | 
|  | 169 | # ANDROID_BUILD_TOP.  Otherwise, these functions print an error message and | 
|  | 170 | # exit.  Let's allow the user to run this program from an unrelated | 
|  | 171 | # directory, if they specify src_dir and dst_dir on the command line. | 
|  | 172 | if dst_dir is None: | 
|  | 173 | dst_dir = get_kernel_dir() | 
|  | 174 | if src_dir is None: | 
|  | 175 | src_dir = get_kernel_headers_original_dir() | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 176 |  | 
|  | 177 | if len(args) == 0: | 
|  | 178 | usage() | 
|  | 179 |  | 
| Christopher Ferris | d12c332 | 2015-09-15 14:13:17 -0700 | [diff] [blame] | 180 | if no_update: | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 181 | for path in args: | 
| Christopher Ferris | 15d3fef | 2017-02-24 12:26:48 -0800 | [diff] [blame] | 182 | dst_file = os.path.join(dst_dir, path) | 
|  | 183 | src_file = os.path.join(src_dir, path) | 
|  | 184 | new_data = cleanupFile(dst_file, src_file, path) | 
| Daniel Mentz | bb4cf7b | 2019-03-28 18:12:30 -0700 | [diff] [blame] | 185 | # Use sys.stdout.write instead of a simple print statement to avoid | 
|  | 186 | # sending an extra new line character to stdout.  Running this | 
|  | 187 | # program in non-update mode and redirecting stdout to a file should | 
|  | 188 | # yield the same result as using update mode, where new_data is | 
|  | 189 | # written directly to a file. | 
|  | 190 | sys.stdout.write(new_data) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 191 |  | 
|  | 192 | sys.exit(0) | 
|  | 193 |  | 
| Christopher Ferris | 15d3fef | 2017-02-24 12:26:48 -0800 | [diff] [blame] | 194 | # Now let's update our files. | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 195 |  | 
|  | 196 | b = BatchFileUpdater() | 
|  | 197 |  | 
|  | 198 | for path in args: | 
| Christopher Ferris | 15d3fef | 2017-02-24 12:26:48 -0800 | [diff] [blame] | 199 | dst_file = os.path.join(dst_dir, path) | 
|  | 200 | src_file = os.path.join(src_dir, path) | 
|  | 201 | new_data = cleanupFile(dst_file, src_file, path, no_update) | 
|  | 202 | if not new_data: | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 203 | continue | 
|  | 204 |  | 
| Daniel Mentz | 316f4a4 | 2019-03-28 18:32:29 -0700 | [diff] [blame] | 205 | b.readFile(dst_file) | 
|  | 206 | r = b.editFile(dst_file, new_data) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 207 | if r == 0: | 
|  | 208 | r = "unchanged" | 
|  | 209 | elif r == 1: | 
|  | 210 | r = "edited" | 
|  | 211 | else: | 
|  | 212 | r = "added" | 
|  | 213 |  | 
| Christopher Ferris | ac7ec11 | 2021-04-19 13:50:16 -0700 | [diff] [blame] | 214 | print("cleaning: %-*s -> %-*s (%s)" % (35, path, 35, path, r)) | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 215 |  | 
| Christopher Ferris | ac7ec11 | 2021-04-19 13:50:16 -0700 | [diff] [blame] | 216 | b.updateFiles() | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 217 |  | 
|  | 218 | sys.exit(0) |