summaryrefslogtreecommitdiff
path: root/scripts/gen-emscripten-exported-json.py
blob: f072a939290bc9d30b6712825db4c26790ebf553 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python3
#
# Copyright 2016 WebAssembly Community Group participants
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import argparse
import json
import os
import re
import sys

SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT_DIR = os.path.dirname(SCRIPT_DIR)
TEST_DIR = os.path.join(ROOT_DIR, 'test')
DEFAULT_EMSCRIPTEN_DIR = os.path.join(ROOT_DIR, 'emscripten')

sys.path.append(TEST_DIR)

from utils import Executable, Error  # noqa: E402


def FindFiles(cmake_build_dir):
    result = []
    for root, dirs, files in os.walk(cmake_build_dir):
        for file_ in files:
            path = os.path.join(root, file_)
            if file_ == 'libwabt.a' or re.search(r'emscripten-helpers', file_):
                result.append(path)
    return result


def GetNM(emscripten_dir):
    python = Executable(sys.executable)
    stdout = python.RunWithArgsForStdout(
        '-c', 'import tools.shared; print(tools.shared.LLVM_ROOT)',
        cwd=emscripten_dir)
    return Executable(os.path.join(stdout.strip(), 'llvm-nm'))


def ProcessFile(nm, file_):
    names = []
    for line in nm.RunWithArgsForStdout(file_).splitlines():
        line = line.rstrip()
        if not line.lstrip():
            continue
        if line.endswith(':'):
            # displaying the archive name, e.g. "foo.c.o:"
            continue
        # line looks like:
        #
        # -------- d yycheck
        # -------- t wabt_strndup_
        #          U wabt_parse_int64
        # -------- T wabt_offsetof_allocator_alloc
        #
        # we want to only keep the "T" names; the extern function symbols defined
        # in the object.
        type_ = line[9]
        if type_ != 'T':
            continue
        name = line[11:]
        names.append('_' + name)
    return names


def main(args):
    parser = argparse.ArgumentParser()
    parser.add_argument('-o', '--output', metavar='PATH', help='output file.')
    parser.add_argument('-v', '--verbose',
                        help='print more diagnostic messages.',
                        action='store_true')
    parser.add_argument('--emscripten-dir', metavar='PATH',
                        help='emscripten directory',
                        default=DEFAULT_EMSCRIPTEN_DIR)
    parser.add_argument('cmake_build_dir', metavar='cmake_build_dir')
    options = parser.parse_args(args)
    nm = GetNM(options.emscripten_dir)

    names = []
    for file_ in FindFiles(options.cmake_build_dir):
        names.extend(ProcessFile(nm, file_))

    out_data = json.dumps(sorted(names), separators=(',\n', ':\n'))
    if options.output:
        with open(options.output, 'w') as out_file:
            out_file.write(out_data)
    else:
        print(out_data)
    return 0


if __name__ == '__main__':
    try:
        sys.exit(main(sys.argv[1:]))
    except Error as e:
        sys.stderr.write(str(e) + '\n')
        sys.exit(1)