Add support for manually modified kernel headers.

This changes the scripts so that if some kernel files exists
in external/kernel-headers/modified, that they will be preferred
over the same files found in original. This is to support the case
where the kernel headers cannot be taken without some small modifications.

Included with this change, is a general cleanup of the python scripts.

This also modifies the generate uapi headers script to indicate if the
source of the modified headers has changed.

Change-Id: Id13523b244ced52a2ecd9f1399c43996dd8296fa
diff --git a/libc/kernel/tools/update_all.py b/libc/kernel/tools/update_all.py
index f45d4e0..7f3657c 100755
--- a/libc/kernel/tools/update_all.py
+++ b/libc/kernel/tools/update_all.py
@@ -6,72 +6,93 @@
 
 def usage():
     print """\
-  usage: %(progname)s [kernel-original-path]
+  usage: %(progname)s [kernel-original-path] [kernel-modified-path]
 
     this program is used to update all the auto-generated clean headers
     used by the Bionic C library. it assumes the following:
 
-      - a set of source kernel headers is located in '../original',
-        relative to the program's directory
+      - a set of source kernel headers is located in
+        'external/kernel-headers/original', relative to the current
+        android tree
 
-      - the clean headers will be placed in '../arch-<arch>/asm',
-        '../common/linux', '../common/asm-generic', etc..
+      - a set of manually modified kernel header files located in
+        'external/kernel-headers/modified', relative to the current
+        android tree
+
+      - the clean headers will be placed in 'bionic/libc/kernel/arch-<arch>/asm',
+        'bionic/libc/kernel/common', etc..
 """ % { "progname" : os.path.basename(sys.argv[0]) }
     sys.exit(0)
 
 try:
-    optlist, args = getopt.getopt( sys.argv[1:], '' )
+    optlist, args = getopt.getopt(sys.argv[1:], '')
 except:
     # unrecognized option
-    sys.stderr.write( "error: unrecognized option\n" )
+    sys.stderr.write("error: unrecognized option\n")
     usage()
 
-if len(optlist) > 0 or len(args) > 1:
+if len(optlist) > 0 or len(args) > 2:
     usage()
 
-progdir = find_program_dir()
-
-if len(args) == 1:
+modified_dir = get_kernel_headers_modified_dir()
+if len(args) == 1 or len(args) == 2:
     original_dir = args[0]
     if not os.path.isdir(original_dir):
-        panic( "Not a directory: %s\n" % original_dir )
+        panic("Not a directory: %s\n" % original_dir)
+
+    if len(args) == 2:
+        modified_dir = args[1]
+        if not os.path.isdir(modified_dir):
+            panic("Not a directory: %s\n" % modified_dir)
 else:
-    original_dir = kernel_original_path
+    original_dir = get_kernel_headers_original_dir()
     if not os.path.isdir(original_dir):
-        panic( "Missing directory, please specify one through command-line: %s\n" % original_dir )
+        panic("Missing directory, please specify one through command-line: %s\n" % original_dir)
 
-skip_ion = False
+if not os.path.isdir(modified_dir):
+    modified_dir = None
 
-# find all source files in 'original'
-#
-sources = []
-warning_ion = []
-for root, dirs, files in os.walk( original_dir ):
+# Find all source files in 'original'.
+sources = dict()
+original_dir = os.path.normpath(original_dir)
+original_dir_len = len(original_dir) + 1
+for root, _, files in os.walk(original_dir):
     for file in files:
-        if skip_ion and (file == "ion.h" or file == "ion_test.h"):
-            warning_ion.append("  Skipped file %s/%s" % (root, file))
-            continue
-        base, ext = os.path.splitext(file)
+        _, ext = os.path.splitext(file)
         if ext == ".h":
-            sources.append( "%s/%s" % (root,file) )
+            rel_path = os.path.normpath(os.path.join(root, file))
+            rel_path = rel_path[original_dir_len:]
+            # Check to see if there is a modified header to use instead.
+            if modified_dir and os.path.exists(os.path.join(modified_dir, rel_path)):
+                sources[rel_path] = False
+            else:
+                sources[rel_path] = True
+
 
 b = BatchFileUpdater()
 
+kernel_dir = get_kernel_dir()
 for arch in kernel_archs:
-    b.readDir( os.path.normpath( progdir + "/../arch-%s" % arch ) )
+    b.readDir(os.path.join(kernel_dir, "arch-%s" % arch))
 
-b.readDir( os.path.normpath( progdir + "/../common" ) )
-
-#print "OLD " + repr(b.old_files)
+b.readDir(os.path.join(kernel_dir, "common"))
 
 oldlen = 120
-for path in sources:
-    dst_path, newdata = clean_header.cleanupFile(path, original_dir)
+android_root_len = len(get_android_root()) + 1
+for rel_path in sorted(sources):
+    if sources[rel_path]:
+        src_dir = original_dir
+        src_str = "<original>/"
+    else:
+        src_dir = modified_dir
+        src_str = "<modified>/"
+    dst_path, newdata = clean_header.cleanupFile(kernel_dir, src_dir, rel_path)
     if not dst_path:
         continue
 
-    b.readFile( dst_path )
-    r = b.editFile( dst_path, newdata )
+    dst_path = os.path.join(kernel_dir, dst_path)
+    b.readFile(dst_path)
+    r = b.editFile(dst_path, newdata)
     if r == 0:
         state = "unchanged"
     elif r == 1:
@@ -79,9 +100,11 @@
     else:
         state = "added"
 
-    str = "cleaning: %-*s -> %-*s (%s)" % ( 35, "<original>" + path[len(original_dir):], 35, dst_path, state )
+    # dst_path is guaranteed to include android root.
+    rel_dst_path = dst_path[android_root_len:]
+    str = "cleaning: %-*s -> %-*s (%s)" % (35, src_str + rel_path, 35, rel_dst_path, state)
     if sys.stdout.isatty():
-        print "%-*s" % (oldlen,str),
+        print "%-*s" % (oldlen, str),
         if (r == 0):
             print "\r",
         else:
@@ -92,11 +115,8 @@
 
     oldlen = len(str)
 
-print "%-*s" % (oldlen,"Done!")
+print "%-*s" % (oldlen, "Done!")
 
 b.updateGitFiles()
 
-if warning_ion:
-    print "NOTE: Due to import into aosp, some files were not processed."
-    print "\n".join(warning_ion)
 sys.exit(0)