Merge "Slight script cleanup; make gensyscalls work from any directory."
diff --git a/libc/kernel/tools/utils.py b/libc/kernel/tools/utils.py
index 8ec7353..0478e93 100644
--- a/libc/kernel/tools/utils.py
+++ b/libc/kernel/tools/utils.py
@@ -47,32 +47,6 @@
 def find_program_dir():
     return os.path.dirname(sys.argv[0])
 
-def find_file_from_upwards(from_path,target_file):
-    """find a file in the current directory or its parents. if 'from_path' is None,
-       seach from the current program's directory"""
-    path = from_path
-    if path == None:
-        path = os.path.realpath(sys.argv[0])
-        path = os.path.dirname(path)
-        D("this script seems to be located in: %s" % path)
-
-    while 1:
-        D("probing "+path)
-        if path == "":
-            file = target_file
-        else:
-            file = path + "/" + target_file
-
-        if os.path.isfile(file):
-            D("found %s in %s" % (target_file, path))
-            return file
-
-        if path == "":
-            return None
-
-        path = os.path.dirname(path)
-
-
 class StringOutput:
     def __init__(self):
         self.line = ""
@@ -143,35 +117,6 @@
             for name in dirs:
                 os.rmdir(os.path.join(root, name))
 
-def update_file( path, newdata ):
-    """update a file on disk, only if its content has changed"""
-    if os.path.exists( path ):
-        try:
-            f = open( path, "r" )
-            olddata = f.read()
-            f.close()
-        except:
-            D("update_file: cannot read existing file '%s'" % path)
-            return 0
-
-        if oldata == newdata:
-            D2("update_file: no change to file '%s'" % path )
-            return 0
-
-        update = 1
-    else:
-        try:
-            create_file_path(path)
-        except:
-            D("update_file: cannot create path to '%s'" % path)
-            return 0
-
-    f = open( path, "w" )
-    f.write( newdata )
-    f.close()
-
-    return 1
-
 
 class BatchFileUpdater:
     """a class used to edit several files at once"""
diff --git a/libc/tools/bionic_utils.py b/libc/tools/bionic_utils.py
index bbfff7d..dccf9e3 100644
--- a/libc/tools/bionic_utils.py
+++ b/libc/tools/bionic_utils.py
@@ -37,140 +37,6 @@
     verbose = level
 
 
-def find_dir_of(path):
-    '''return the directory name of 'path', or "." if there is none'''
-    # remove trailing slash
-    if len(path) > 1 and path[-1] == '/':
-        path = path[:-1]
-
-    # find parent directory name
-    d = os.path.dirname(path)
-    if d == "":
-        return "."
-    else:
-        return d
-
-#  other stuff
-#
-#
-def find_file_from_upwards(from_path,target_file):
-    """find a file in the current directory or its parents. if 'from_path' is None,
-       seach from the current program's directory"""
-    path = from_path
-    if path == None:
-        path = find_dir_of(sys.argv[0])
-        D("this script seems to be located in: %s" % path)
-
-    while 1:
-        if path == "":
-            path = "."
-
-        file = path + "/" + target_file
-        D("probing "+file)
-
-        if os.path.isfile(file):
-            D("found %s in %s" % (target_file, path))
-            return file
-
-        if path == ".":
-            break
-
-        path = os.path.dirname(path)
-
-    path = ""
-    while 1:
-        path = "../" + path
-        file = path + target_file
-        D("probing "+file)
-
-        if os.path.isfile(file):
-            D("found %s in %s" % (target_file, path))
-            return file
-
-
-    return None
-
-def find_bionic_root():
-    '''find the root of the Bionic source tree. we check for the SYSCALLS.TXT file
-       from the location of the current program's directory.'''
-
-    # note that we can't use find_file_from_upwards() since we can't use os.path.abspath
-    # that's because in some cases the p4 client is in a symlinked directory, and this
-    # function will return the real path instead, which later creates problems when
-    # p4 commands are issued
-    #
-    file = find_file_from_upwards(None, "SYSCALLS.TXT")
-    if file:
-        return os.path.dirname(file)
-    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")
-        return None
-
-    # get rid of the "-xenU" suffix that is found in Xen virtual machines
-    if len(version) > 5 and version[-5:] == "-xenU":
-        version = version[:-5]
-
-    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))
-        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
 #
 class SysCallsTxtParser:
@@ -312,52 +178,3 @@
 
     def get(self):
         return self.line
-
-
-def create_file_path(path):
-    dirs = []
-    while 1:
-        parent = os.path.dirname(path)
-        if parent == "/":
-            break
-        dirs.append(parent)
-        path = parent
-
-    dirs.reverse()
-    for dir in dirs:
-        #print "dir %s" % dir
-        if os.path.isdir(dir):
-            continue
-        os.mkdir(dir)
-
-def walk_source_files(paths,callback,args,excludes=[]):
-    """recursively walk a list of paths and files, only keeping the source files in directories"""
-    for path in paths:
-        if not os.path.isdir(path):
-            callback(path,args)
-        else:
-            for root, dirs, files in os.walk(path):
-                #print "w-- %s (ex: %s)" % (repr((root,dirs)), repr(excludes))
-                if len(excludes):
-                    for d in dirs[:]:
-                        if d in excludes:
-                            dirs.remove(d)
-                for f in files:
-                    r, ext = os.path.splitext(f)
-                    if ext in [ ".h", ".c", ".cpp", ".S" ]:
-                        callback( "%s/%s" % (root,f), args )
-
-def cleanup_dir(path):
-    """create a directory if needed, and ensure that it is totally empty
-       by removing any existing content in it"""
-    if not os.path.exists(path):
-        os.mkdir(path)
-    else:
-        for root, dirs, files in os.walk(path, topdown=False):
-            if root.endswith("kernel_headers/"):
-                # skip 'kernel_headers'
-                continue
-            for name in files:
-                os.remove(os.path.join(root, name))
-            for name in dirs:
-                os.rmdir(os.path.join(root, name))
diff --git a/libc/tools/gensyscalls.py b/libc/tools/gensyscalls.py
index ed1b3dc..4894f2d 100755
--- a/libc/tools/gensyscalls.py
+++ b/libc/tools/gensyscalls.py
@@ -10,17 +10,7 @@
 
 from bionic_utils import *
 
-# get the root Bionic directory, simply this script's dirname
-#
-bionic_root = find_bionic_root()
-if not bionic_root:
-    print "could not find the Bionic root directory. aborting"
-    sys.exit(1)
-
-if bionic_root[-1] != '/':
-    bionic_root += "/"
-
-print "bionic_root is %s" % bionic_root
+bionic_libc_root = os.environ["ANDROID_BUILD_TOP"] + "/bionic/libc/"
 
 # temp directory where we store all intermediate files
 bionic_temp = "/tmp/bionic_gensyscalls/"
@@ -334,11 +324,11 @@
         glibc_fp.write("#define _BIONIC_GLIBC_SYSCALLS_H_\n")
 
         glibc_fp.write("#if defined(__arm__)\n")
-        self.scan_linux_unistd_h(glibc_fp, "libc/kernel/arch-arm/asm/unistd.h")
+        self.scan_linux_unistd_h(glibc_fp, bionic_libc_root + "/kernel/arch-arm/asm/unistd.h")
         glibc_fp.write("#elif defined(__mips__)\n")
-        self.scan_linux_unistd_h(glibc_fp, "libc/kernel/arch-mips/asm/unistd.h")
+        self.scan_linux_unistd_h(glibc_fp, bionic_libc_root + "/kernel/arch-mips/asm/unistd.h")
         glibc_fp.write("#elif defined(__i386__)\n")
-        self.scan_linux_unistd_h(glibc_fp, "libc/kernel/arch-x86/asm/unistd_32.h")
+        self.scan_linux_unistd_h(glibc_fp, bionic_libc_root + "/kernel/arch-x86/asm/unistd_32.h")
         glibc_fp.write("#endif\n")
 
         glibc_fp.write("#endif /* _BIONIC_GLIBC_SYSCALLS_H_ */\n")
@@ -397,14 +387,14 @@
     def  regenerate(self):
         D( "scanning for existing architecture-specific stub files" )
 
-        bionic_root_len = len(bionic_root)
+        bionic_libc_root_len = len(bionic_libc_root)
 
         for arch in all_archs:
-            arch_path = bionic_root + "arch-" + arch
+            arch_path = bionic_libc_root + "arch-" + arch
             D( "scanning " + arch_path )
             files = glob.glob( arch_path + "/syscalls/*.S" )
             for f in files:
-                self.old_stubs.append( f[bionic_root_len:] )
+                self.old_stubs.append( f[bionic_libc_root_len:] )
 
         D( "found %d stub files" % len(self.old_stubs) )
 
@@ -424,13 +414,13 @@
         edits   = []
 
         for stub in self.new_stubs + self.other_files:
-            if not os.path.exists( bionic_root + stub ):
+            if not os.path.exists( bionic_libc_root + stub ):
                 # new file, git add it
                 D( "new file:     " + stub)
-                adds.append( bionic_root + stub )
-                shutil.copyfile( bionic_temp + stub, bionic_root + stub )
+                adds.append( bionic_libc_root + stub )
+                shutil.copyfile( bionic_temp + stub, bionic_libc_root + stub )
 
-            elif not filecmp.cmp( bionic_temp + stub, bionic_root + stub ):
+            elif not filecmp.cmp( bionic_temp + stub, bionic_libc_root + stub ):
                 D( "changed file: " + stub)
                 edits.append( stub )
 
@@ -438,7 +428,7 @@
         for stub in self.old_stubs:
             if not stub in self.new_stubs:
                 D( "deleted file: " + stub)
-                deletes.append( bionic_root + stub )
+                deletes.append( bionic_libc_root + stub )
 
 
         if adds:
@@ -447,11 +437,11 @@
             commands.getoutput("git rm " + " ".join(deletes))
         if edits:
             for file in edits:
-                shutil.copyfile( bionic_temp + file, bionic_root + file )
+                shutil.copyfile( bionic_temp + file, bionic_libc_root + file )
             commands.getoutput("git add " +
-                               " ".join((bionic_root + file) for file in edits))
+                               " ".join((bionic_libc_root + file) for file in edits))
 
-        commands.getoutput("git add %s%s" % (bionic_root,"SYSCALLS.TXT"))
+        commands.getoutput("git add %s%s" % (bionic_libc_root,"SYSCALLS.TXT"))
 
         if (not adds) and (not deletes) and (not edits):
             D("no changes detected!")
@@ -461,5 +451,5 @@
 D_setlevel(1)
 
 state = State()
-state.process_file(bionic_root+"SYSCALLS.TXT")
+state.process_file(bionic_libc_root+"SYSCALLS.TXT")
 state.regenerate()