admin
2023-03-07 8b06b1cbf112d55307ea8a6efe711db4e7506d89
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
#!/usr/bin/env python
 
# Copyright 2019 The Crashpad Authors. All rights reserved.
#
# 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 collections
import os
import subprocess
import sys
 
MigInterface = collections.namedtuple(
    'MigInterface', ['user_c', 'server_c', 'user_h', 'server_h'])
 
 
def generate_interface(defs,
                       interface,
                       includes=[],
                       sdk=None,
                       clang_path=None,
                       mig_path=None,
                       migcom_path=None,
                       arch=None):
    if mig_path is None:
        mig_path = 'mig'
 
    # yapf: disable
    command = [
        mig_path,
        '-user', interface.user_c,
        '-server', interface.server_c,
        '-header', interface.user_h,
        '-sheader', interface.server_h,
    ]
    # yapf: enable
 
    if clang_path is not None:
        os.environ['MIGCC'] = clang_path
    if migcom_path is not None:
        os.environ['MIGCOM'] = migcom_path
    if arch is not None:
        command.extend(['-arch', arch])
    if sdk is not None:
        command.extend(['-isysroot', sdk])
    for include in includes:
        command.extend(['-I' + include])
    command.append(defs)
    subprocess.check_call(command)
 
 
def parse_args(args, multiple_arch=False):
    parser = argparse.ArgumentParser()
    parser.add_argument('--clang-path', help='Path to clang')
    parser.add_argument('--mig-path', help='Path to mig')
    parser.add_argument('--migcom-path', help='Path to migcom')
    if not multiple_arch:
        parser.add_argument('--arch', help='Target architecture')
    else:
        parser.add_argument(
            '--arch',
            default=[],
            action='append',
            help='Target architecture (may appear multiple times)')
    parser.add_argument('--sdk', help='Path to SDK')
    parser.add_argument(
        '--include',
        default=[],
        action='append',
        help='Additional include directory (may appear multiple times)')
    parser.add_argument('defs')
    parser.add_argument('user_c')
    parser.add_argument('server_c')
    parser.add_argument('user_h')
    parser.add_argument('server_h')
    return parser.parse_args(args)
 
 
def main(args):
    parsed = parse_args(args)
    interface = MigInterface(parsed.user_c, parsed.server_c, parsed.user_h,
                             parsed.server_h)
    generate_interface(parsed.defs, interface, parsed.include, parsed.sdk,
                       parsed.clang_path, parsed.mig_path, parsed.migcom_path,
                       parsed.arch)
 
 
if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]))