Home | History | Annotate | Download | only in tools
      1 #!/usr/bin/python
      2 
      3 # This tool is used to generate the version scripts for libc and libm
      4 # for every architecture.
      5 
      6 import atexit
      7 import os.path
      8 import shutil
      9 import tempfile
     10 import sys
     11 
     12 
     13 all_arches = ["arm", "arm64", "mips", "mips64", "x86", "x86_64"]
     14 bionic_libc_root = os.path.join(os.environ["ANDROID_BUILD_TOP"], "bionic/libc")
     15 bionic_libm_root = os.path.join(os.environ["ANDROID_BUILD_TOP"], "bionic/libm")
     16 bionic_libdl_root = os.path.join(os.environ["ANDROID_BUILD_TOP"], "bionic/libdl")
     17 libc_script = os.path.join(bionic_libc_root, "libc.map.txt")
     18 libm_script = os.path.join(bionic_libm_root, "libm.map.txt")
     19 libdl_script = os.path.join(bionic_libdl_root, "libdl.map.txt")
     20 libstdcxx_script = os.path.join(bionic_libc_root, "libstdc++.map.txt")
     21 
     22 script_name = os.path.basename(sys.argv[0])
     23 
     24 # TODO (dimity): generate architecture-specific version scripts as part of build
     25 
     26 # temp directory where we store all intermediate files
     27 bionic_temp = tempfile.mkdtemp(prefix="bionic_genversionscripts")
     28 # Make sure the directory is deleted when the script exits.
     29 atexit.register(shutil.rmtree, bionic_temp)
     30 
     31 bionic_libc_root = os.path.join(os.environ["ANDROID_BUILD_TOP"], "bionic/libc")
     32 
     33 warning = "Generated by %s. Do not edit." % script_name
     34 
     35 
     36 def has_arch_tags(tags):
     37   for arch in all_arches:
     38     if arch in tags:
     39       return True
     40   return False
     41 
     42 
     43 class VersionScriptGenerator(object):
     44 
     45   def run(self):
     46     for script in [libc_script, libstdcxx_script, libm_script, libdl_script]:
     47       basename = os.path.basename(script)
     48       dirname = os.path.dirname(script)
     49       for arch in all_arches:
     50         name = basename.split(".")[0] + "." + arch + ".map"
     51         tmp_path = os.path.join(bionic_temp, name)
     52         dest_path = os.path.join(dirname, name)
     53         with open(tmp_path, "w") as fout:
     54           with open(script, "r") as fin:
     55             fout.write("# %s\n" % warning)
     56             for line in fin:
     57               index = line.find("#")
     58               if index != -1:
     59                 tags = line[index+1:].split()
     60                 if arch not in tags and has_arch_tags(tags):
     61                   continue
     62               fout.write(line)
     63         shutil.copyfile(tmp_path, dest_path)
     64 
     65 
     66 generator = VersionScriptGenerator()
     67 generator.run()
     68