File: pipeline_helper.py

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (117 lines) | stat: -rw-r--r-- 3,466 bytes parent folder | download | duplicates (10)
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
#!/usr/bin/env python
#
# ===- pipeline_helper.py - Remote Index pipeline Helper *- python -------*--===#
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
# ===------------------------------------------------------------------------===#

import argparse
import os
import subprocess
from socket import socket
import sys
import time
import threading


def kill_process_after_delay(server_process):
    time.sleep(10)
    if server_process.poll() is None:
        server_process.kill()


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--input-file-name", required=True)
    parser.add_argument("--project-root", required=True)
    parser.add_argument("--index-file", required=True)
    parser.add_argument("--server-arg", action="append", default=[])
    parser.add_argument(
        "--server-log", nargs="?", type=argparse.FileType("wb"), default=os.devnull
    )

    args = parser.parse_args()

    # Grab an available port.
    with socket() as s:
        s.bind(("localhost", 0))
        server_address = "localhost:" + str(s.getsockname()[1])

    print("Initializing clangd-index-server...", file=sys.stderr)
    index_server_process = subprocess.Popen(
        [
            "clangd-index-server",
            "--server-address=" + server_address,
            args.index_file,
            args.project_root,
        ]
        + args.server_arg,
        stderr=subprocess.PIPE,
    )

    # This will kill index_server_process if it hangs without printing init
    # message.
    shutdown_thread = threading.Thread(
        target=kill_process_after_delay, args=(index_server_process,)
    )
    shutdown_thread.daemon = True
    shutdown_thread.start()

    # Wait for the server to warm-up.
    found_init_message = False
    while index_server_process.poll() is None:
        line = index_server_process.stderr.readline()
        args.server_log.write(line)
        args.server_log.flush()
        if b"Server listening" in line:
            print("Server initialization complete.", file=sys.stderr)
            found_init_message = True
            break

    if not found_init_message:
        print("Server initialization failed. Shutting down.", file=sys.stderr)
        sys.exit(1)

    print("Running clangd-index-server-monitor...", file=sys.stderr)
    index_server_monitor_process = subprocess.Popen(
        [
            "clangd-index-server-monitor",
            server_address,
        ],
        stderr=subprocess.PIPE,
    )

    index_server_monitor_process.wait()

    in_file = open(args.input_file_name)

    print("Staring clangd...", file=sys.stderr)
    clangd_process = subprocess.Popen(
        [
            "clangd",
            "--remote-index-address=" + server_address,
            "--project-root=" + args.project_root,
            "--lit-test",
            "--sync",
        ],
        stdin=in_file,
    )
    clangd_process.wait()
    print(
        "Clangd executed successfully, shutting down child processes.", file=sys.stderr
    )
    index_server_process.kill()
    for line in index_server_process.stderr:
        args.server_log.write(line)
        args.server_log.flush()

    for line in index_server_monitor_process.stderr:
        args.server_log.write(line)
        args.server_log.flush()


if __name__ == "__main__":
    main()