def parseFile(self,path): """parse a given file for Linux headers""" if not os.path.exists(path): return # since tokenizing the file is very slow, we first try a quick grep # to see if this returns any meaningful results. only if this is true # do we do the tokenization""" try: f = open(path, "rt") except: print "!!! can't read '%s'" % path return hasIncludes = False for line in f: if HeaderScanner.re_combined.match(line): hasIncludes = True break if not hasIncludes: if verboseSearch: print "::: " + path return if verboseSearch: print "*** " + path list = cpp.BlockParser().parseFile(path) if list: #list.removePrefixed("CONFIG_",self.config) list.optimizeMacros(kernel_known_macros) list.optimizeIf01() includes = list.findIncludes() for inc in includes: self.checkInclude(inc,path)
def cleanupFile(dst_file, src_file, rel_path, no_update=True): """reads an original header and perform the cleanup operation on it this functions returns the destination path and the clean header as a single string""" # Check the header path if not os.path.exists(src_file): print_error(no_update, "'%s' does not exist\n" % src_file) return None, None if not os.path.isfile(src_file): print_error(no_update, "'%s' is not a file\n" % src_file) return None, None # Extract the architecture if found. arch = None statics = kernel_known_generic_statics m = re.search(r"(^|/)asm-([\w\d_\+\.\-]+)/.*", rel_path) if m and m.group(2) != 'generic': arch = m.group(2) statics = statics.union(kernel_known_statics.get(arch, set())) # Now, let's parse the file. parser = cpp.BlockParser() blocks = parser.parseFile(src_file) if not parser.parsed: print_error(no_update, "Can't parse '%s'" % src_file) return None macros = kernel_known_macros.copy() if arch and arch in kernel_default_arch_macros: macros.update(kernel_default_arch_macros[arch]) if arch and arch in kernel_arch_token_replacements: blocks.replaceTokens(kernel_arch_token_replacements[arch]) blocks.optimizeMacros(macros) blocks.optimizeIf01() blocks.removeVarsAndFuncs(statics) blocks.replaceTokens(kernel_token_replacements) blocks.removeMacroDefines(kernel_ignored_macros) out = StringOutput() out.write(kernel_disclaimer) blocks.write(out) return out.get()
def parseFile(self, path, arch=None, kernel_root=None): """parse a given file for Linux headers""" if not os.path.exists(path): return # since tokenizing the file is very slow, we first try a quick grep # to see if this returns any meaningful results. only if this is true # do we do the tokenization""" try: f = open(path, "rt") except: print("!!! can't read '%s'" % path) return hasIncludes = False for line in f: if (HeaderScanner.re_combined.match(line) or (kernel_root and HeaderScanner.re_rel_dir.match(line))): hasIncludes = True break if not hasIncludes: if verboseSearch: print("::: " + path) return if verboseSearch: print("*** " + path) list = cpp.BlockParser().parseFile(path) if list: macros = kernel_known_macros.copy() if kernel_root: macros.update(self.config) if arch and arch in kernel_default_arch_macros: macros.update(kernel_default_arch_macros[arch]) list.optimizeMacros(macros) list.optimizeIf01() includes = list.findIncludes() for inc in includes: self.checkInclude(inc, path, kernel_root)
def cleanupFile(path, original_path): """reads an original header and perform the cleanup operation on it this functions returns the destination path and the clean header as a single string""" # check the header path src_path = path if not os.path.exists(src_path): if noUpdate: panic( "file does not exist: '%s'\n" % path ) sys.stderr.write( "warning: file does not exit: %s\n" % path ) return None, None if not os.path.isfile(src_path): if noUpdate: panic( "path is not a file: '%s'\n" % path ) sys.stderr.write( "warning: not a file: %s\n" % path ) return None, None if os.path.commonprefix( [ src_path, original_path ] ) != original_path: if noUpdate: panic( "file is not in 'original' directory: %s\n" % path ); sys.stderr.write( "warning: file not in 'original' ignored: %s\n" % path ) return None, None src_path = src_path[len(original_path):] if len(src_path) > 0 and src_path[0] == '/': src_path = src_path[1:] if len(src_path) == 0: panic( "oops, internal error, can't extract correct relative path\n" ) # convert into destination path, extracting architecture if needed # and the corresponding list of known static functions # arch = None statics = kernel_known_generic_statics m = re.match(r"asm-([\w\d_\+\.\-]+)(/.*)", src_path) if m and m.group(1) != 'generic': dst_path = "arch-%s/asm/%s" % m.groups() arch = m.group(1) statics = statics.union( kernel_known_statics.get( arch, set() ) ) else: # process headers under the uapi directory # note the "asm" level has been explicitly added in the original # kernel header tree for architectural-dependent uapi headers m_uapi = re.match(r"(uapi)/([\w\d_\+\.\-]+)(/.*)", src_path) if m_uapi: dst_path = src_path m_uapi_arch = re.match(r"asm-([\w\d_\+\.\-]+)", m_uapi.group(2)) if m_uapi_arch and m_uapi_arch.group(1) != 'generic': arch = m_uapi_arch.group(1) statics = statics.union( kernel_known_statics.get( arch, set() ) ) # common headers (ie non-asm and non-uapi) else: dst_path = "common/" + src_path dst_path = os.path.normpath( kernel_cleaned_path + "/" + dst_path ) # now, let's parse the file # blocks = cpp.BlockParser().parseFile(path) if not blocks: sys.stderr.write( "error: can't parse '%s'" % path ) sys.exit(1) macros = kernel_known_macros.copy() if arch and arch in kernel_default_arch_macros: macros.update(kernel_default_arch_macros[arch]) if arch and arch in kernel_arch_token_replacements: blocks.replaceTokens( kernel_arch_token_replacements[arch] ) blocks.optimizeMacros( macros ) blocks.optimizeIf01() blocks.removeVarsAndFuncs( statics ) blocks.replaceTokens( kernel_token_replacements ) blocks.removeComments() blocks.removeMacroDefines( kernel_ignored_macros ) blocks.removeWhiteSpace() out = StringOutput() out.write( kernel_disclaimer ) blocks.writeWithWarning(out, kernel_warning, 4) return dst_path, out.get()
def cleanupFile(path): """reads an original header and perform the cleanup operation on it this functions returns the destination path and the clean header as a single string""" # check the header path src_path = path if not os.path.exists(src_path): if noUpdate: panic("file does not exist: '%s'\n" % path) sys.stderr.write("warning: file does not exit: %s\n" % path) return None, None if not os.path.isfile(src_path): if noUpdate: panic("path is not a file: '%s'\n" % path) sys.stderr.write("warning: not a file: %s\n" % path) return None, None original_path = kernel_original_path if os.path.commonprefix([src_path, original_path]) != original_path: if noUpdate: panic("file is not in 'original' directory: %s\n" % path) sys.stderr.write("warning: file not in 'original' ignored: %s\n" % path) return None, None src_path = src_path[len(original_path):] if len(src_path) > 0 and src_path[0] == '/': src_path = src_path[1:] if len(src_path) == 0: panic("oops, internal error, can't extract correct relative path") # convert into destination path, extracting architecture if needed # and the corresponding list of known static functions # arch = None re_asm_arch = re.compile(r"asm-([\w\d_\+\.\-]+)(/.*)") m = re_asm_arch.match(src_path) statics = kernel_known_generic_statics if m and m.group(1) != 'generic': dst_path = "arch-%s/asm/%s" % m.groups() arch = m.group(1) statics = statics.union(kernel_known_statics.get(arch, set())) else: dst_path = "common/" + src_path dst_path = os.path.normpath(original_path + "/../" + dst_path) # now, let's parse the file # list = cpp.BlockParser().parseFile(path) if not list: sys.stderr.write("error: can't parse '%s'" % path) sys.exit(1) list.optimizeMacros(kernel_known_macros) list.optimizeIf01() list.removeVarsAndFuncs(statics) list.removeComments() list.removeEmptyLines() list.insertDisclaimer(kernel.kernel_disclaimer) out = StringOutput() list.write(out) return dst_path, out.get()
def cleanupFile(dst_dir, src_dir, rel_path, no_update=True): """reads an original header and perform the cleanup operation on it this functions returns the destination path and the clean header as a single string""" # check the header path full_path = os.path.join(src_dir, rel_path) if not os.path.exists(full_path): print_error(no_update, "file does not exist: '%s'\n" % full_path) return None, None if not os.path.isfile(full_path): print_error(no_update, "path is not a file: '%s'\n" % full_path) return None, None # convert into destination path, extracting architecture if needed # and the corresponding list of known static functions # arch = None statics = kernel_known_generic_statics m = re.match(r"asm-([\w\d_\+\.\-]+)(/.*)", rel_path) if m and m.group(1) != 'generic': dst_path = "arch-%s/asm/%s" % m.groups() arch = m.group(1) statics = statics.union(kernel_known_statics.get(arch, set())) else: # process headers under the uapi directory # note the "asm" level has been explicitly added in the original # kernel header tree for architectural-dependent uapi headers m_uapi = re.match(r"(uapi)/([\w\d_\+\.\-]+)(/.*)", rel_path) if m_uapi: dst_path = rel_path m_uapi_arch = re.match(r"asm-([\w\d_\+\.\-]+)", m_uapi.group(2)) if m_uapi_arch and m_uapi_arch.group(1) != 'generic': arch = m_uapi_arch.group(1) statics = statics.union(kernel_known_statics.get(arch, set())) # common headers (ie non-asm and non-uapi) else: dst_path = os.path.join("android", rel_path) dst_path = os.path.join(dst_dir, dst_path) # now, let's parse the file # parser = cpp.BlockParser() blocks = parser.parseFile(full_path) if not parser.parsed: print_error(no_update, "can't parse '%s'%" % full_path) return None, None macros = kernel_known_macros.copy() if arch and arch in kernel_default_arch_macros: macros.update(kernel_default_arch_macros[arch]) if arch and arch in kernel_arch_token_replacements: blocks.replaceTokens(kernel_arch_token_replacements[arch]) blocks.optimizeMacros(macros) blocks.optimizeIf01() blocks.removeVarsAndFuncs(statics) blocks.replaceTokens(kernel_token_replacements) blocks.removeMacroDefines(kernel_ignored_macros) out = StringOutput() out.write(kernel_disclaimer) blocks.writeWithWarning(out, kernel_warning, 4) return dst_path, out.get()