libc: Update auto-gen scripts

Make the scripts use external/kernel-headers/original by default.

clean_header.py:  Document -k<path>, add -d<path>
find_headers.py:  Make kernel config files optional
update_all.py:    Allow setting the path to kernel headers on the command-line
update_all.py:    Better formatting of output on ttys
update_all.py:    Automatically perform "git add/rm" on affected files.
SYSCALLS.TXT:     Fix typo in __socketcall definition.
checksyscalls.py: Add support for superH architecture in the checks.
gensyscalls.py:   Automatically perform "git add/rm" on affected files.
cpp.py:           Fixed a bug that prevented certain type definitions to
                  be kept in the generated clean header (e.g.
                  struct ethtool_drvinfo in <linux/ethtool.h>)

All scripts will use the content of external/kernel-headers/original by default now.

The generated code removes all empty lines and trailing whitespace. This is useful
to ensure a unified output even if we change the parser again in the future.

The top-level disclaimer has been edited with update instructions to regenerate
the headers when needed.

Also, a warning is now inserted every 8th line in the final output:

/* WARNING: DO NOT EDIT, AUTO-GENERATED CODE - SEE TOP FOR INSTRUCTIONS */

Changes under kernel/arch-arm and kernel/arch-x86 should correspond to whitespace
differences and additionnal struct definitions that were missed by the previous
parser implementation.

Change-Id: Icd1c056bacd766759f3e9b7bb5d63a246f3d656a

WARNING: If you run these script, do not submit the result to gerrit for now.
         It seems there are discrepancies between the content of original headers
         and those currently commited under bionic/libc/kernel/.

         (This problem is the main motivation to insert the warning repeatedly).

         Current list of issues:

           - Missing SuperH headers (i.e. external/kernel-headers/original/asm-sh)
diff --git a/libc/tools/bionic_utils.py b/libc/tools/bionic_utils.py
index 17eef13..e7c8c2d 100644
--- a/libc/tools/bionic_utils.py
+++ b/libc/tools/bionic_utils.py
@@ -105,8 +105,29 @@
     else:
         return None
 
+def find_original_kernel_headers():
+    """try to find the directory containing the original kernel headers"""
+    bionic_root = find_bionic_root()
+    if not bionic_root:
+        D("Could not find Bionic root !!")
+        return None
+
+    path = os.path.normpath(bionic_root + "/../../external/kernel-headers/original")
+    if not os.path.isdir(path):
+        D("Could not find %s" % (path))
+        return None
+
+    return path
+
 def find_kernel_headers():
     """try to find the directory containing the kernel headers for this machine"""
+
+    # First try to find the original kernel headers.
+    ret = find_original_kernel_headers()
+    if ret:
+        D("found original kernel headers in: %s" % (ret))
+        return ret
+
     status, version = commands.getstatusoutput( "uname -r" )  # get Linux kernel version
     if status != 0:
         D("could not execute 'uname -r' command properly")
@@ -116,14 +137,39 @@
     if len(version) > 5 and version[-5:] == "-xenU":
         version = version[:-5]
 
-    path = "/usr/src/linux-headers-" + version
-    D("probing %s for kernel headers" % (path+"/include"))
+    path = "/usr/src/linux-headers-" + version + "/include"
+    D("probing %s for kernel headers" % (path))
     ret = os.path.isdir( path )
     if ret:
-        D("found kernel headers in: %s" % (path + "/include"))
+        D("found kernel headers in: %s" % (path))
         return path
     return None
 
+def find_arch_header(kernel_headers,arch,header):
+    # First, try in <root>/arch/<arm>/include/<header>
+    # corresponding to the location in the kernel source tree for
+    # certain architectures (e.g. arm).
+    path = "%s/arch/%s/include/asm/%s" % (kernel_headers, arch, header)
+    D("Probing for %s" % path)
+    if os.path.exists(path):
+        return path
+
+    # Try <root>/asm-<arch>/include/<header> corresponding to the location
+    # in the kernel source tree for other architectures (e.g. x86).
+    path = "%s/include/asm-%s/%s" % (kernel_headers, arch, header)
+    D("Probing for %s" % path)
+    if os.path.exists(path):
+        return path
+
+    # Otherwise, look under <root>/asm-<arch>/<header> corresponding
+    # the original kernel headers directory
+    path = "%s/asm-%s/%s" % (kernel_headers, arch, header)
+    D("Probing for %s" % path)
+    if os.path.exists(path):
+        return path
+
+
+    return None
 
 # parser for the SYSCALLS.TXT file
 #
@@ -212,7 +258,12 @@
                 E("invalid syscall number in '%s'" % line)
                 return
 
-        print str(syscall_id) + ':' + str(syscall_id2) + ':' + str(syscall_id3)
+		global verbose
+        if verbose >= 2:
+            if call_id < 0:
+                print "%s: %d,%d,%d" % (syscall_name, syscall_id, syscall_id2, syscall_id3)
+            else:
+                print "%s(%d): %d,%d,%d" % (syscall_name, call_id, syscall_id, syscall_id2, syscall_id3)
 
         t = { "id"     : syscall_id,
               "id2"    : syscall_id2,
diff --git a/libc/tools/checksyscalls.py b/libc/tools/checksyscalls.py
index 9edb390..f642e84 100755
--- a/libc/tools/checksyscalls.py
+++ b/libc/tools/checksyscalls.py
@@ -40,8 +40,8 @@
     if len(args) == 0:
         linux_root = find_kernel_headers()
         if linux_root == None:
-            print "could not locate this system kernel headers root directory, please"
-            print "specify one when calling this program, i.e. 'checksyscalls <headers-directory>'"
+            print "Could not locate original or system kernel headers root directory."
+            print "Please specify one when calling this program, i.e. 'checksyscalls <headers-directory>'"
             sys.exit(1)
         print "using the following kernel headers root: '%s'" % linux_root
     else:
@@ -112,62 +112,63 @@
 
 arm_dict = {}
 x86_dict = {}
+superh_dict = {}
 
-
-# remove trailing slash and '/include' from the linux_root, if any
+# remove trailing slash from the linux_root, if any
 if linux_root[-1] == '/':
     linux_root = linux_root[:-1]
 
-if len(linux_root) > 8 and linux_root[-8:] == '/include':
-    linux_root = linux_root[:-8]
-
-arm_unistd = linux_root + "/include/asm-arm/unistd.h"
-if not os.path.exists(arm_unistd):
-    print "WEIRD: could not locate the ARM unistd.h header file"
-    print "tried searching in '%s'" % arm_unistd
-    print "maybe using a different set of kernel headers might help"
+arm_unistd = find_arch_header(linux_root, "arm", "unistd.h")
+if not arm_unistd:
+    print "WEIRD: Could not locate the ARM unistd.h kernel header file,"
+    print "maybe using a different set of kernel headers might help."
     sys.exit(1)
 
 # on recent kernels, asm-i386 and asm-x64_64 have been merged into asm-x86
 # with two distinct unistd_32.h and unistd_64.h definition files.
 # take care of this here
 #
-x86_unistd = linux_root + "/include/asm-i386/unistd.h"
-if not os.path.exists(x86_unistd):
-    x86_unistd1 = x86_unistd
-    x86_unistd = linux_root + "/include/asm-x86/unistd_32.h"
-    if not os.path.exists(x86_unistd):
-        print "WEIRD: could not locate the i386/x86 unistd.h header file"
-        print "tried searching in '%s' and '%s'" % (x86_unistd1, x86_unistd)
-        print "maybe using a different set of kernel headers might help"
+x86_unistd = find_arch_header(linux_root, "i386", "unistd.h")
+if not x86_unistd:
+    x86_unistd = find_arch_header(linux_root, "x86", "unistd_32.h")
+    if not x86_unistd:
+        print "WEIRD: Could not locate the i386/x86 unistd.h header file,"
+        print "maybe using a different set of kernel headers might help."
         sys.exit(1)
 
-process_header( linux_root+"/include/asm-arm/unistd.h", arm_dict )
+superh_unistd = find_arch_header(linux_root, "sh", "unistd_32.h")
+if not superh_unistd:
+    print "WEIRD: Could not locate the SuperH unistd.h kernel header file,"
+    print "maybe using a different set of kernel headers might help."
+    sys.exit(1)
+
+process_header( arm_unistd, arm_dict )
 process_header( x86_unistd, x86_dict )
+process_header( superh_unistd, superh_dict )
 
 # now perform the comparison
 errors = 0
-for sc in syscalls:
-    sc_name = sc["name"]
-    sc_id   = sc["id"]
-    if sc_id >= 0:
-        if not arm_dict.has_key(sc_name):
-            print "arm syscall %s not defined !!" % sc_name
-            errors += 1
-        elif arm_dict[sc_name] != sc_id:
-            print "arm syscall %s should be %d instead of %d !!" % (sc_name, arm_dict[sc_name], sc_id)
-            errors += 1
 
-for sc in syscalls:
-    sc_name = sc["name"]
-    sc_id2  = sc["id2"]
-    if sc_id2 >= 0:
-        if not x86_dict.has_key(sc_name):
-            print "x86 syscall %s not defined !!" % sc_name
-            errors += 1
-        elif x86_dict[sc_name] != sc_id2:
-            print "x86 syscall %s should be %d instead of %d !!" % (sc_name, x86_dict[sc_name], sc_id2)
-            errors += 1
+def check_syscalls(archname, idname, arch_dict):
+    errors = 0
+    for sc in syscalls:
+        sc_name = sc["name"]
+        sc_id   = sc[idname]
+        if sc_id >= 0:
+            if not arch_dict.has_key(sc_name):
+                print "%s syscall %s not defined, should be %d !!" % (archname, sc_name, sc_id)
+                errors += 1
+            elif not arch_dict.has_key(sc_name):
+                print "%s syscall %s is not implemented!" % (archname, sc_name)
+                errors += 1
+            elif arch_dict[sc_name] != sc_id:
+                print "%s syscall %s should be %d instead of %d !!" % (archname, sc_name, arch_dict[sc_name], sc_id)
+                errors += 1
+    return errors
+
+errors += check_syscalls("arm", "id", arm_dict)
+errors += check_syscalls("x86", "id2", x86_dict)
+errors += check_syscalls("superh", "id3", superh_dict)
 
 if errors == 0:
     print "congratulations, everything's fine !!"
diff --git a/libc/tools/gensyscalls.py b/libc/tools/gensyscalls.py
index 0535e56..b58754b 100755
--- a/libc/tools/gensyscalls.py
+++ b/libc/tools/gensyscalls.py
@@ -557,7 +557,7 @@
         for sc in self.syscalls:
             if sc.has_key("asm-arm") and 'arm' in all_archs:
                 fname = "arch-arm/syscalls/%s.S" % sc["func"]
-                D( ">>> generating "+fname )
+                D2( ">>> generating "+fname )
                 fp = create_file( fname )
                 fp.write(sc["asm-arm"])
                 fp.close()
@@ -565,7 +565,7 @@
 
             if sc.has_key("asm-thumb") and 'arm' in all_archs:
                 fname = "arch-arm/syscalls/%s.S" % sc["func"]
-                D( ">>> generating "+fname )
+                D2( ">>> generating "+fname )
                 fp = create_file( fname )
                 fp.write(sc["asm-thumb"])
                 fp.close()
@@ -573,7 +573,7 @@
 
             if sc.has_key("asm-x86") and 'x86' in all_archs:
                 fname = "arch-x86/syscalls/%s.S" % sc["func"]
-                D( ">>> generating "+fname )
+                D2( ">>> generating "+fname )
                 fp = create_file( fname )
                 fp.write(sc["asm-x86"])
                 fp.close()
@@ -581,7 +581,7 @@
 
             if sc.has_key("asm-sh"):
                 fname = "arch-sh/syscalls/%s.S" % sc["func"]
-                D( ">>> generating "+fname )
+                D2( ">>> generating "+fname )
                 fp = create_file( fname )
                 fp.write(sc["asm-sh"])
                 fp.close()
@@ -626,7 +626,7 @@
 
         for stub in self.new_stubs + self.other_files:
             if not os.path.exists( bionic_root + stub ):
-                # new file, P4 add it
+                # new file, git add it
                 D( "new file:     " + stub)
                 adds.append( bionic_root + stub )
                 shutil.copyfile( bionic_temp + stub, bionic_root + stub )
@@ -643,16 +643,21 @@
 
 
         if adds:
-            commands.getoutput("p4 add " + " ".join(adds))
+            commands.getoutput("git add " + " ".join(adds))
         if deletes:
-            commands.getoutput("p4 delete " + " ".join(deletes))
+            commands.getoutput("git rm " + " ".join(deletes))
         if edits:
-            commands.getoutput("p4 edit " +
-                               " ".join((bionic_root + file) for file in edits))
             for file in edits:
                 shutil.copyfile( bionic_temp + file, bionic_root + file )
+            commands.getoutput("git add " +
+                               " ".join((bionic_root + file) for file in edits))
 
-        D("ready to go !!")
+        commands.getoutput("git add %s%s" % (bionic_root,"SYSCALLS.TXT"))
+
+        if (not adds) and (not deletes) and (not edits):
+            D("no changes detected!")
+        else:
+            D("ready to go!!")
 
 D_setlevel(1)