Merge r169628 from ftlopt.
[WebKit-https.git] / Source / JavaScriptCore / build-symbol-table-index.py
1 #!/usr/bin/python
2
3 import glob
4 import os
5 import subprocess
6 import sys
7 from sets import Set
8 from operator import itemgetter
9
10
11 current_arch = sys.argv[1]
12
13 print("Building Index Table")
14
15 if current_arch not in ("x86_64", "arm64"):
16     sys.exit()
17
18 binary_file_directory = os.path.join(os.getenv("OBJECT_FILE_DIR_" + os.getenv("CURRENT_VARIANT")), current_arch)
19
20 if not os.path.isdir(binary_file_directory):
21     print("Failed to build index table at " + binary_file_directory)
22     sys.exit()
23
24 framework_directory = os.path.join(os.getenv("BUILT_PRODUCTS_DIR"), os.getenv("JAVASCRIPTCORE_RESOURCES_DIR"), "Runtime", current_arch)
25
26
27 symbol_table_location = os.path.join(framework_directory, "Runtime.symtbl")
28
29 symbol_table = {}
30
31 symbol_table_is_out_of_date = False
32
33 symbol_table_modification_time = 0
34
35 if os.path.isfile(symbol_table_location):
36     symbol_table_modification_time = os.path.getmtime(symbol_table_location)
37
38 file_suffix = "bc"
39 file_suffix_length = len(file_suffix)
40
41 tested_symbols_location = "./tested-symbols.symlst"
42 include_symbol_table_location = os.path.join(os.getenv("SHARED_DERIVED_FILE_DIR"), "JavaScriptCore/InlineRuntimeSymbolTable.h")
43
44 tested_symbols = Set([])
45
46 if os.path.isfile(tested_symbols_location):
47     with open(tested_symbols_location, 'r') as file:
48         print("Loading tested symbols")
49         for line in file:
50             tested_symbols.add(line[:-1])
51
52 for bitcode_file in glob.iglob(os.path.join(framework_directory, "*." + file_suffix)):
53     bitcode_basename = os.path.basename(bitcode_file)
54     binary_file = os.path.join(binary_file_directory, bitcode_basename[:-file_suffix_length] + "o")
55     if os.path.getmtime(binary_file) < symbol_table_modification_time:
56         continue
57
58     symbol_table_is_out_of_date = True
59
60     print("Appending symbols from " + bitcode_basename)
61     lines = subprocess.check_output(["nm", "-U", "-j", binary_file]).splitlines()
62
63     for symbol in lines:
64         if symbol[:2] == "__" and symbol[-3:] != ".eh" and symbol in tested_symbols:
65             symbol_table[symbol[1:]] = bitcode_basename
66
67 if not symbol_table_is_out_of_date:
68     sys.exit()
69
70 if os.path.isfile(symbol_table_location):
71     with open(symbol_table_location, 'r') as file:
72         print("Loading symbol table")
73         for line in file:
74             symbol, _, location = line[:-1].partition(" ")
75             # don't overwrite new symbols with old locations
76             if not symbol in symbol_table:
77                 symbol_table[symbol] = location
78
79 symbol_list = symbol_table.items()
80
81 print("Writing symbol table")
82
83 with open(symbol_table_location, "w") as symbol_file:
84     with open(include_symbol_table_location, "w") as include_file:
85         include_file.write("#define FOR_EACH_LIBRARY_SYMBOL(macro)")
86         for symbol, location in symbol_list:
87             symbol_file.write("{} {}\n".format(symbol, location))
88             include_file.write(" \\\nmacro(\"{}\", \"{}\")".format(symbol, location))
89         include_file.write("\n")
90 print("Done")