summaryrefslogtreecommitdiff
path: root/buildscripts/convert_bazel_headers.py
blob: 7149e36e01de9ec7274266dcfe9ed2d5d201e422 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import subprocess
import sys
import json
import platform
import os
import shlex
import concurrent.futures
import glob
import traceback
import shutil
from typing import Annotated, List

import typer


def work(target_library: str, silent: bool, cpu_count: int, cc: List[str]):
    headers = set()
    original_headers = set()

    def get_headers(line):
        nonlocal headers
        try:
            with open(target_library + ".bazel_headers") as f:
                bazel_headers = [line.strip() for line in f.readlines()]
                bazel_headers += [
                    "src/mongo/platform/basic.h",
                    "src/mongo/platform/windows_basic.h",
                ]

            with open(target_library + ".env_vars") as f:
                tmp_env_vars = json.load(f)
                env_vars = {}
                # subprocess requies only strings
                for k, v in tmp_env_vars.items():
                    env_vars[str(k)] = str(v)

            for command in cc:
                cmd_output = command["output"].replace("\\", "/").strip("'").strip('"')
                line_output = line.replace("\\", "/")

                if cmd_output == line_output:
                    os.makedirs(os.path.dirname(line), exist_ok=True)
                    if os.name == "nt":
                        header_arg = " /showIncludes"
                    else:
                        header_arg = " -H"

                    if not silent:
                        print(f"compiling {line}")

                    p = subprocess.run(
                        shlex.split((command["command"].replace("\\", "/") + header_arg)),
                        env=env_vars,
                        capture_output=True,
                        text=True,
                    )
                    if p.returncode != 0:
                        print(f"Error compiling, exitcode: {p.returncode}", file=sys.stderr)
                        print(f"STDOUT: {p.stdout}", file=sys.stderr)
                        print(f"STDERR: {p.stderr}", file=sys.stderr)
                        sys.exit(1)
                    if os.name == "nt":
                        for line in p.stdout.split("\n"):
                            line = (
                                line.replace("Note: including file:", "")
                                .strip(" ")
                                .replace("\\", "/")
                            )

                            if not line.startswith(os.getcwd().replace("\\", "/")):
                                continue

                            line = os.path.relpath(
                                line, start=os.getcwd().replace("\\", "/")
                            ).replace("\\", "/")
                            if line not in bazel_headers:
                                if line.startswith("src/") or line.startswith("bazel-out/"):
                                    original_headers.add(line)
                                    line = "//" + line
                                    line = ":".join(line.rsplit("/", 1))

                                    headers.add(line)
                    else:
                        for line in p.stderr.split("\n"):
                            if ". src/" in line or ". bazel-out/" in line:
                                while line.startswith("."):
                                    line = line[1:]
                                line = line.replace("\\", "/")

                                if line[1:] not in bazel_headers:
                                    original_headers.add(line[1:])
                                    line = "//" + line[1:]
                                    line = ":".join(line.rsplit("/", 1))

                                    headers.add(line)
        except Exception as exc:
            print(traceback.format_exc(), file=sys.stderr)
            raise exc

    sources = []
    with open(target_library + ".obj_files") as f:
        lines = f.readlines()
        for line in lines:
            line = line.strip()
            line = line.replace("build/opt", "//src")
            line = line[: line.find(".")] + ".cpp"
            src_header = os.path.splitext(line[2:])[0] + ".h"
            if os.path.exists(src_header):
                src_header = "//" + ":".join(src_header.rsplit("/", 1))
                headers.add(src_header)
            line = ":".join(line.rsplit("/", 1))
            if line.endswith("_gen.cpp"):
                line = line[:-4]
            sources.append(line)

        with concurrent.futures.ThreadPoolExecutor(max_workers=cpu_count) as executor:
            jobs = {executor.submit(get_headers, line.strip()): line.strip() for line in lines}
            for completed_job in concurrent.futures.as_completed(jobs):
                if not silent:
                    print(f"finished {jobs[completed_job]}")

    with open(".bazel_include_info.json") as f:
        bazel_include_info = json.load(f)

    header_map = bazel_include_info["header_map"]
    gen_header_map = bazel_include_info["gen_header_map"]
    bazel_exec = bazel_include_info["bazel_exec"]
    bazel_config = bazel_include_info["config"]

    global_headers = (
        "src/mongo:config.h",
        "src/mongo/config.h",
        "src/mongo/platform/basic.h",
        "src/mongo/platform/windows_basic.h",
    )

    reverse_header_map = {}
    for k, v in header_map.items():
        for hdr in v:
            if not hdr or hdr.endswith(global_headers):
                continue
            bazel_header = "//" + hdr.replace("\\", "/")
            bazel_header = ":".join(bazel_header.rsplit("/", 1))
            if bazel_header.startswith("//src/third_party/SafeInt"):
                reverse_header_map[bazel_header] = ["//src/third_party/SafeInt:headers"]
            elif bazel_header.startswith("//src/third_party/immer"):
                reverse_header_map[bazel_header] = ["//src/third_party/immer:headers"]
            elif bazel_header in reverse_header_map:
                if bazel_header.startswith("//src/third_party/"):
                    continue
                reverse_header_map[bazel_header].append(k)
            else:
                reverse_header_map[bazel_header] = [k]

    for k, v in gen_header_map.items():
        for hdr in v:
            if not hdr or hdr.endswith(global_headers):
                continue
            bazel_header = "//" + hdr.replace("\\", "/")
            bazel_header = ":".join(bazel_header.rsplit("/", 1))
            if bazel_header not in reverse_header_map:
                reverse_header_map[bazel_header] = [k]
            else:
                reverse_header_map[bazel_header].append(k)

    recommended_deps = set()
    minimal_headers = set()

    basename_sources = [os.path.splitext(src.rsplit(":", 1)[1])[0] for src in sources]
    for header in headers:
        header_basename = os.path.splitext(header.rsplit(":", 1)[1])[0]
        if header_basename in basename_sources:
            minimal_headers.add(header)
            continue

        if header in reverse_header_map:
            found = False
            for lib in reverse_header_map[header]:
                recommended_deps.add(lib)
        else:
            if not header.endswith(global_headers):
                minimal_headers.add(header)

    deps_order_by_height = []
    deps_queries = {}

    with open(target_library + ".bazel_deps") as f:
        original_deps = [line.strip() for line in f.readlines()]

    for dep in recommended_deps | set(original_deps):
        p = subprocess.run(
            [bazel_exec, "cquery"]
            + bazel_config
            + [f'kind("extract_debuginfo|idl_generator|render_template", deps("@{dep}"))'],
            capture_output=True,
            text=True,
        )
        deps_queries[dep] = [
            line.split(" ")[0] for line in p.stdout.splitlines() if line.startswith("//")
        ]
        deps_order_by_height.append((dep, len(deps_queries[dep])))

    deps_order_by_height.sort(key=lambda x: x[1])

    deps_order_by_height = [dep[0] for dep in deps_order_by_height]
    optimal_header_deps = set()
    for header in headers:
        if header in minimal_headers:
            continue

        path_header = "/".join(header.rsplit(":", 1))
        path_header = path_header[2:]
        for dep in deps_order_by_height:
            if dep in header_map and path_header in header_map[dep]:
                optimal_header_deps.add(dep)
                break
            found = False
            for other_dep in deps_order_by_height:
                if other_dep in gen_header_map:
                    continue
                if dep in deps_queries[other_dep]:
                    optimal_header_deps.add(other_dep)
                    found = True
                    break
            if found:
                continue
            if dep in gen_header_map:
                minimal_headers.add(dep)
            else:
                raise Exception(
                    f"Should not happen, did not find way to add dep {dep} for {target_library}"
                )

    optimal_header_deps = list(optimal_header_deps)

    working_deps = optimal_header_deps.copy()
    for dep in optimal_header_deps:
        if dep in working_deps:
            for test_dep in optimal_header_deps:
                if test_dep == dep:
                    continue
                if test_dep in working_deps and test_dep in deps_queries[dep]:
                    working_deps.remove(test_dep)

    link_deps = []
    header_deps = []
    for dep in sorted(list(set(list(working_deps) + list(set(original_deps))))):
        if dep in original_deps:
            link_deps.append(dep)
        else:
            header_deps.append(dep)

    target_name = os.path.splitext(os.path.basename(target_library))[0]
    if target_name.startswith("lib"):
        target_name = target_name[3:]

    bazel_target = f"{target_library}\n"
    bazel_target += "=" * 50 + "\n"
    local_bazel_path = os.path.dirname(target_library.replace("build/opt", "//src")) + ":"
    bazel_target += "mongo_cc_library(\n"
    bazel_target += f'    name = "{target_name}",\n'
    if sources:
        bazel_target += "    srcs = [\n"
        for src in sorted([src.replace(local_bazel_path, "") for src in sources]):
            bazel_target += f'        "{src}",\n'
        bazel_target += "    ],\n"
    if minimal_headers:
        bazel_target += "    hdrs = [\n"
        for header in sorted([header.replace(local_bazel_path, "") for header in minimal_headers]):
            bazel_target += f'        "{header}",\n'
        bazel_target += "    ],\n"
    if header_deps:
        bazel_target += "    header_deps = [\n"
        for dep in sorted([dep.strip().replace(local_bazel_path, "") for dep in header_deps]):
            bazel_target += f'        "{dep}",\n'
        bazel_target += "    ],\n"
    if link_deps:
        bazel_target += "    deps = [\n"
        for dep in sorted([dep.strip().replace(local_bazel_path, "") for dep in link_deps]):
            bazel_target += f'        "{dep}",\n'
        bazel_target += "    ],\n"
    bazel_target += ")\n"
    return bazel_target


def main(
    target_libraries: Annotated[List[str], typer.Argument()],
    silent: Annotated[bool, typer.Option()] = False,
    skip_scons: Annotated[bool, typer.Option()] = False,
    debug_mode: Annotated[bool, typer.Option()] = False,
):
    extra_args = []
    if os.name == "nt":
        extra_args += [
            "CPPPATH=C:\sasl\include",
            "LIBPATH=C:\sasl\lib",
        ]
        target_library = os.path.join(
            os.path.dirname(target_library), os.path.basename(target_library)[3:-2] + "lib"
        )

    path = shutil.which("icecc")
    if path is None:
        extra_args += ["ICECC="]

    if os.name == "nt":
        target_fmt = lambda target_library: os.path.join(
            os.path.dirname(target_library), os.path.basename(target_library)[3:-2] + "lib"
        )
    elif platform.system() == "Darwin":
        target_fmt = lambda target_library: target_library[:-2] + "a"
    else:
        target_fmt = lambda x: None

    map(target_fmt, target_libraries)

    cmd = [
        sys.executable,
        "buildscripts/scons.py",
        "--build-profile=opt",
        " ".join(
            [f"--bazel-includes-info={target_library}" for target_library in target_libraries]
        ),
        "--libdeps-linting=off",
        "--ninja=disabled",
        "compiledb",
    ] + extra_args

    if not skip_scons:
        p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)

        while True:
            line = p.stdout.readline()
            if not line:
                break
            print(line.strip(), file=sys.stderr)

        _, _ = p.communicate()

        if p.returncode != 0:
            print(f"SCons build failed, exit code {p.returncode}", file=sys.stderr)
            sys.exit(1)

    with open("compile_commands.json") as f:
        cc = json.load(f)
    if platform.system() == "Linux":
        cpu_count = len(os.sched_getaffinity(0)) + 4
    else:
        cpu_count = os.cpu_count() + 4

    # Process pool makes it harder to debug what is happening
    # so for debug mode, we disabled process pool so things happen in order
    # you can just print from the process.
    if debug_mode:
        bazel_targets = []
        for target_library in target_libraries:
            bazel_targets += [work(target_library, silent, cpu_count, cc)]
    else:
        with concurrent.futures.ProcessPoolExecutor(max_workers=cpu_count) as executor:
            jobs = {
                executor.submit(work, target_library, silent, cpu_count, cc): target_library
                for target_library in target_libraries
            }
            bazel_targets = [job.result() for job in concurrent.futures.as_completed(jobs)]

    print("====== Bazel Targets ======\n")
    print("\n".join(bazel_targets))


if __name__ == "__main__":
    typer.run(main)