• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#! /usr/bin/python
2# -*- coding: utf-8 -*-
3#
4# Copyright (C) 2011-2014, International Business Machines
5# Corporation and others. All Rights Reserved.
6#
7# file name: depstest.py
8#
9# created on: 2011may24
10
11"""ICU dependency tester.
12
13This probably works only on Linux.
14
15The exit code is 0 if everything is fine, 1 for errors, 2 for only warnings.
16
17Sample invocation:
18  ~/svn.icu/trunk/src/source/test/depstest$ ./depstest.py ~/svn.icu/trunk/dbg
19"""
20
21__author__ = "Markus W. Scherer"
22
23import glob
24import os.path
25import subprocess
26import sys
27
28import dependencies
29
30_ignored_symbols = set()
31_obj_files = {}
32_symbols_to_files = {}
33_return_value = 0
34
35# Classes with vtables (and thus virtual methods).
36_virtual_classes = set()
37# Classes with weakly defined destructors.
38# nm shows a symbol class of "W" rather than "T".
39_weak_destructors = set()
40
41def _ReadObjFile(root_path, library_name, obj_name):
42  global _ignored_symbols, _obj_files, _symbols_to_files
43  global _virtual_classes, _weak_destructors
44  lib_obj_name = library_name + "/" + obj_name
45  if lib_obj_name in _obj_files:
46    print "Warning: duplicate .o file " + lib_obj_name
47    _return_value = 2
48    return
49
50  path = os.path.join(root_path, library_name, obj_name)
51  nm_result = subprocess.Popen(["nm", "--demangle", "--format=sysv",
52                                "--extern-only", "--no-sort", path],
53                               stdout=subprocess.PIPE).communicate()[0]
54  obj_imports = set()
55  obj_exports = set()
56  for line in nm_result.splitlines():
57    fields = line.split("|")
58    if len(fields) == 1: continue
59    name = fields[0].strip()
60    # Ignore symbols like '__cxa_pure_virtual',
61    # 'vtable for __cxxabiv1::__si_class_type_info' or
62    # 'DW.ref.__gxx_personality_v0'.
63    if name.startswith("__cxa") or "__cxxabi" in name or "__gxx" in name:
64      _ignored_symbols.add(name)
65      continue
66    type = fields[2].strip()
67    if type == "U":
68      obj_imports.add(name)
69    else:
70      obj_exports.add(name)
71      _symbols_to_files[name] = lib_obj_name
72      # Is this a vtable? E.g., "vtable for icu_49::ByteSink".
73      if name.startswith("vtable for icu"):
74        _virtual_classes.add(name[name.index("::") + 2:])
75      # Is this a destructor? E.g., "icu_49::ByteSink::~ByteSink()".
76      index = name.find("::~")
77      if index >= 0 and type == "W":
78        _weak_destructors.add(name[index + 3:name.index("(", index)])
79  _obj_files[lib_obj_name] = {"imports": obj_imports, "exports": obj_exports}
80
81def _ReadLibrary(root_path, library_name):
82  obj_paths = glob.glob(os.path.join(root_path, library_name, "*.o"))
83  for path in obj_paths:
84    _ReadObjFile(root_path, library_name, os.path.basename(path))
85
86def _Resolve(name, parents):
87  global _ignored_symbols, _obj_files, _symbols_to_files, _return_value
88  item = dependencies.items[name]
89  item_type = item["type"]
90  if name in parents:
91    sys.exit("Error: %s %s has a circular dependency on itself: %s" %
92             (item_type, name, parents))
93  # Check if already cached.
94  exports = item.get("exports")
95  if exports != None: return item
96  # Calculcate recursively.
97  parents.append(name)
98  imports = set()
99  exports = set()
100  system_symbols = item.get("system_symbols")
101  if system_symbols == None: system_symbols = item["system_symbols"] = set()
102  files = item.get("files")
103  if files:
104    for file_name in files:
105      obj_file = _obj_files[file_name]
106      imports |= obj_file["imports"]
107      exports |= obj_file["exports"]
108  imports -= exports | _ignored_symbols
109  deps = item.get("deps")
110  if deps:
111    for dep in deps:
112      dep_item = _Resolve(dep, parents)
113      # Detect whether this item needs to depend on dep,
114      # except when this item has no files, that is, when it is just
115      # a deliberate umbrella group or library.
116      dep_exports = dep_item["exports"]
117      dep_system_symbols = dep_item["system_symbols"]
118      if files and imports.isdisjoint(dep_exports) and imports.isdisjoint(dep_system_symbols):
119        print "Info:  %s %s  does not need to depend on  %s\n" % (item_type, name, dep)
120      # We always include the dependency's exports, even if we do not need them
121      # to satisfy local imports.
122      exports |= dep_exports
123      system_symbols |= dep_system_symbols
124  item["exports"] = exports
125  item["system_symbols"] = system_symbols
126  imports -= exports | system_symbols
127  for symbol in imports:
128    for file_name in files:
129      if symbol in _obj_files[file_name]["imports"]:
130        neededFile = _symbols_to_files.get(symbol)
131        if neededFile in dependencies.file_to_item:
132          neededItem = "but %s does not depend on %s (for %s)" % (name, dependencies.file_to_item[neededFile], neededFile)
133        else:
134          neededItem = "- is this a new system symbol?"
135        sys.stderr.write("Error: in %s %s: %s imports %s %s\n" %
136                         (item_type, name, file_name, symbol, neededItem))
137    _return_value = 1
138  del parents[-1]
139  return item
140
141def Process(root_path):
142  """Loads dependencies.txt, reads the libraries' .o files, and processes them.
143
144  Modifies dependencies.items: Recursively builds each item's system_symbols and exports.
145  """
146  global _ignored_symbols, _obj_files, _return_value
147  global _virtual_classes, _weak_destructors
148  dependencies.Load()
149  for name_and_item in dependencies.items.iteritems():
150    name = name_and_item[0]
151    item = name_and_item[1]
152    system_symbols = item.get("system_symbols")
153    if system_symbols:
154      for symbol in system_symbols:
155        _symbols_to_files[symbol] = name
156  for library_name in dependencies.libraries:
157    _ReadLibrary(root_path, library_name)
158  o_files_set = set(_obj_files.keys())
159  files_missing_from_deps = o_files_set - dependencies.files
160  files_missing_from_build = dependencies.files - o_files_set
161  if files_missing_from_deps:
162    sys.stderr.write("Error: files missing from dependencies.txt:\n%s\n" %
163                     sorted(files_missing_from_deps))
164    _return_value = 1
165  if files_missing_from_build:
166    sys.stderr.write("Error: files in dependencies.txt but not built:\n%s\n" %
167                     sorted(files_missing_from_build))
168    _return_value = 1
169  if not _return_value:
170    for library_name in dependencies.libraries:
171      _Resolve(library_name, [])
172  if not _return_value:
173    virtual_classes_with_weak_destructors = _virtual_classes & _weak_destructors
174    if virtual_classes_with_weak_destructors:
175      sys.stderr.write("Error: Some classes have virtual methods, and "
176                       "an implicit or inline destructor "
177                       "(see ICU ticket #8454 for details):\n%s\n" %
178                       sorted(virtual_classes_with_weak_destructors))
179      _return_value = 1
180
181def main():
182  global _return_value
183  if len(sys.argv) <= 1:
184    sys.exit(("Command line error: " +
185             "need one argument with the root path to the built ICU libraries/*.o files."))
186  Process(sys.argv[1])
187  if _ignored_symbols:
188    print "Info: ignored symbols:\n%s" % sorted(_ignored_symbols)
189  if not _return_value:
190    print "OK: Specified and actual dependencies match."
191  else:
192    print "Error: There were errors, please fix them and re-run. Processing may have terminated abnormally."
193  return _return_value
194
195if __name__ == "__main__":
196  sys.exit(main())
197