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