Home | History | Annotate | Download | only in dev
      1 #!/usr/bin/env python
      2 # Copyright 2017 the V8 project authors. All rights reserved.
      3 # Use of this source code is governed by a BSD-style license that can be
      4 # found in the LICENSE file.
      5 """\
      6 Convenience script for generating arch-specific ctags file.
      7 This script MUST be executed at the top directory.
      8 
      9 Usage:
     10     $ tools/dev/gen-tags.py [<arch>...]
     11 
     12 The example usage is as follows:
     13     $ tools/dev/gen-tags.py x64
     14 
     15 If no <arch> is given, it generates tags file for all arches:
     16     $ tools/dev/gen-tags.py
     17 """
     18 import os
     19 import subprocess
     20 import sys
     21 
     22 # All arches that this script understands.
     23 ARCHES = ["ia32", "x64", "arm", "arm64", "mips", "mips64", "ppc", "s390"]
     24 
     25 def PrintHelpAndExit():
     26   print(__doc__)
     27   sys.exit(0)
     28 
     29 
     30 def _Call(cmd, silent=False):
     31   if not silent: print("# %s" % cmd)
     32   return subprocess.call(cmd, shell=True)
     33 
     34 
     35 def ParseArguments(argv):
     36   if not "tools/dev" in argv[0]:
     37     PrintHelpAndExit()
     38   argv = argv[1:]
     39 
     40   # If no argument is given, then generate ctags for all arches.
     41   if len(argv) == 0:
     42     return ARCHES
     43 
     44   user_arches = []
     45   for argstring in argv:
     46     if argstring in ("-h", "--help", "help"):
     47       PrintHelpAndExit()
     48     if argstring not in ARCHES:
     49       print("Invalid argument: %s" % argstring)
     50       sys.exit(1)
     51     user_arches.append(argstring)
     52 
     53   return user_arches
     54 
     55 
     56 def Exclude(fullpath, exclude_arches):
     57   for arch in exclude_arches:
     58     if ("/%s/" % arch) in fullpath: return True
     59   return False
     60 
     61 
     62 def Main(argv):
     63   user_arches = []
     64 
     65   user_arches = ParseArguments(argv)
     66 
     67   exclude_arches = list(ARCHES)
     68   for user_arch in user_arches:
     69     exclude_arches.remove(user_arch)
     70 
     71   paths = ["include", "src", "test"]
     72   exts = [".h", ".cc", ".c"]
     73 
     74   gtags_filename = "gtags.files"
     75 
     76   with open(gtags_filename, "w") as gtags:
     77     for path in paths:
     78       for root, dirs, files in os.walk(path):
     79         for file in files:
     80           if not file.endswith(tuple(exts)): continue
     81           fullpath = os.path.join(root, file)
     82           if Exclude(fullpath, exclude_arches): continue
     83           gtags.write(fullpath + os.linesep)
     84 
     85   _Call("ctags --fields=+l -L " + gtags_filename)
     86 
     87 
     88 if __name__ == "__main__":
     89   sys.exit(Main(sys.argv))
     90