aboutsummaryrefslogtreecommitdiffstats
path: root/packaging-tools/libclang_training/run_batch_files.py
blob: 244661d41d2ce65d6faf3b50671e25726cb6dff1 (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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

############################################################################
#
# Copyright (C) 2023 The Qt Company Ltd.
# Contact: https://www.qt.io/licensing/
#
# This file is part of Qt Creator.
#
# Commercial License Usage
# Licensees holding valid commercial Qt licenses may use this file in
# accordance with the commercial license agreement provided with the
# Software or, alternatively, in accordance with the terms contained in
# a written agreement between you and The Qt Company. For licensing terms
# and conditions see https://www.qt.io/terms-conditions. For further
# information use the contact form at https://www.qt.io/contact-us.
#
# GNU General Public License Usage
# Alternatively, this file may be used under the terms of the GNU
# General Public License version 3 as published by the Free Software
# Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
# included in the packaging of this file. Please review the following
# information to ensure the GNU General Public License requirements will
# be met: https://www.gnu.org/licenses/gpl-3.0.html.
#
############################################################################

"""
Run Qt Creator in 'clang batch file' mode and produce csv data of resulting
libclang operations (e.g. parse, reparse, completion, preamble generation).

Multiple batch files and libclang binaries can be specified for the runs, thus
producing data for an overview of libclang's performance in certain use cases
over multiple libclang binaries/versions.

The process environment configures this script. The relevant variables and their
meaning are shown in this pseudo code of the main algorithm:

  for libclang in QTC_CLANG_BATCH_CONFIG_LIBCLANGS
     copy libclang to QTC_CLANG_BATCH_CONFIG_TARGET_LIBCLANG
          for batchfile in QTC_CLANG_BATCH_CONFIG_FILES
              run qtcreator with batchfile
              write log/csv data files into QTC_CLANG_BATCH_CONFIG_LOG_DIR
  merge csv data files per batch file.

The PATH must contain:
 * qtcreator executable and everything needed to run it.
 * qmake is in PATH for proper set up of an automatically created kit.
 * dbgview.exe on Windows since this is used to capture the output.

Notes:
 * For convenience, create a *.sh/*.bat file setting up and running this script.
 * Ensure that the specified libclang binaries expect the same intrinsics
   (<libclang install dir>/lib/clang/x.y.z/include) since these are not copied!
"""

import os
import sys
from pathlib import Path
from shutil import copyfile
from subprocess import STDOUT, Popen
from time import sleep, time
from typing import Dict, List, Optional

import libclangtimings2csv
import merge_csv_files


def verbose_start(args: List[str]) -> None:
    if Config.Verbose:
        print(f"info: starting {args}")


def check_existence_or_die(file_path: str) -> None:
    if not os.path.exists(file_path):
        raise SystemExit(f"file path does not exist: {file_path}")


def check_exit_code_or_die(exit_code: int, args: List[str]) -> None:
    if exit_code != 0:
        raise SystemExit(f"exit code {exit_code} for {' '.join(args)}")


class Config:
    Verbose: bool = False
    # Verbose = True

    LogDir: str = ""
    QtCreatorSettingsDir: str = ""
    TargetLibClangDll: str = ""

    LibClangDlls: List[str] = []
    BatchFiles: List[str] = []

    @staticmethod
    def initialize_from_environment() -> None:
        Config.LogDir = os.environ['QTC_CLANG_BATCH_CONFIG_LOG_DIR']
        check_existence_or_die(Config.LogDir)

        Config.QtCreatorSettingsDir = os.environ['QTC_CLANG_BATCH_CONFIG_SETTINGS_DIR']
        check_existence_or_die(Config.QtCreatorSettingsDir)

        Config.TargetLibClangDll = os.environ['QTC_CLANG_BATCH_CONFIG_TARGET_LIBCLANG']

        libclang_dlls = os.environ['QTC_CLANG_BATCH_CONFIG_LIBCLANGS']
        Config.LibClangDlls = libclang_dlls.split(os.pathsep)
        assert len(Config.LibClangDlls) >= 1
        for dll in Config.LibClangDlls:
            check_existence_or_die(dll)

        batch_files = os.environ['QTC_CLANG_BATCH_CONFIG_FILES']
        Config.BatchFiles = batch_files.split(os.pathsep)
        assert len(Config.BatchFiles) >= 1
        for batch_file in Config.BatchFiles:
            check_existence_or_die(batch_file)
            # TODO: Check for format

    @staticmethod
    def dump() -> None:
        print("log dir:")
        print(f"  {Config.LogDir}")
        print("qt creator settings dir:")
        print(f"  {Config.QtCreatorSettingsDir}")
        print("target libclang:")
        print(f"  {Config.TargetLibClangDll}")
        print("libclangs:")
        for dll in Config.LibClangDlls:
            print(f"  {dll}")
        print("batch files:")
        for batch_file in Config.BatchFiles:
            print(f"  {batch_file}")


class RunRecord:
    def __init__(self, libclang_id: str, batch_file_path: str):
        self.libclang_id = libclang_id
        parts = os.path.basename(batch_file_path).split('.')
        self.batch_file_id = '.'.join(parts[0:-1])  # Remove suffix
        self.log_file_path = self.batch_file_id + '___' + libclang_id + '.log'
        self.csv_file_path = ""


class DebugView:
    def __init__(self, log_file_path: str):
        self.log_file_path = log_file_path
        self.executable = 'dbgview.exe'
        self.proc: Optional[Popen] = None  # type: ignore

    def start_async(self) -> None:
        args = [self.executable, '/accepteula', '/l', self.log_file_path]
        verbose_start(args)
        self.proc = Popen(args, shell=False)  # pylint: disable=R1732
        sleep(2)

    def stop(self) -> None:
        if self.proc:
            if Config.Verbose:
                print(f"info: stopping {self.executable}")
            self.proc.terminate()
            self.proc.wait()


def create_environment(batch_file_path: str) -> Dict[str, str]:
    env = os.environ.copy()
    env['LIBCLANG_TIMING'] = '1'
    env['QT_LOGGING_RULES'] = 'qtc.clangcodemodel.batch=true'
    env['QTC_NO_CODE_INDEXER'] = '1'
    env['QTC_CLANG_NO_ALIVE_TIMER'] = '1'
    env['QTC_CLANG_NO_SUPPORTIVE_TRANSLATIONUNIT'] = '1'
    env['QTC_CLANG_BATCH_TIMEOUT'] = '3000000'
    env['QTC_CLANG_BATCH'] = batch_file_path

    return env


def run_sync_and_log_output_windows(args: List[str], batch_file_path: str, log_file_path: str) -> None:
    debug_view = DebugView(log_file_path)
    debug_view.start_async()

    verbose_start(args)
    with Popen(args, env=create_environment(batch_file_path)) as proc:
        proc.communicate()

        debug_view.stop()

        check_exit_code_or_die(proc.returncode, args)


def run_sync_and_log_output_unix(args: List[str], batch_file_path: str, log_file_path: str) -> None:
    with open(log_file_path, 'w', encoding="utf-8") as log_file:
        verbose_start(args)
        with Popen(args, stdout=log_file, stderr=STDOUT, env=create_environment(batch_file_path)) as proc:
            proc.communicate()
            check_exit_code_or_die(proc.returncode, args)


def run_qtcreator_with_batch_file(batch_file_path: str, log_file_path: str) -> None:
    args = [
        'qtcreator',
        '-noload', 'all',
        '-load', 'CppEditor',
        '-load', 'QmakeProjectManager',
        '-load', 'ClangCodeModel',
        '-load', 'Designer',
        '-settingspath',
        Config.QtCreatorSettingsDir,
    ]

    if sys.platform == "win32":
        run_sync_and_log_output_windows(args, batch_file_path, log_file_path)
    else:
        run_sync_and_log_output_unix(args, batch_file_path, log_file_path)


def convert_log_file_to_csv_file(log_file_path: str, column_label: str) -> str:
    output = libclangtimings2csv.convert(log_file_path, column_label)

    csv_file_path = log_file_path + '.csv'
    with open(csv_file_path, 'w', encoding="utf-8") as handle:
        handle.write(output)

    return csv_file_path


def log_file_from_id(log_file_id: str) -> str:
    return log_file_id + ".log"


def create_dir(dir_path: str) -> None:
    if not os.path.exists(dir_path):
        if Config.Verbose:
            print(f"info: creating not existent {dir_path}")
        Path(dir_path).mkdir(parents=True)


def create_backup_file(file_path: str) -> None:
    if os.path.exists(file_path):
        backup_path = file_path[:-4] + ".backup_" + str(time()) + ".log"
        if Config.Verbose:
            print(f"info: creating backup of already existing '{file_path}'")
        copyfile(file_path, backup_path)


def print_duration(seconds: float) -> None:
    hours, remainder = divmod(seconds, 3600)
    minutes, seconds = divmod(remainder, 60)
    print(f"...needed {hours}:{minutes}:{seconds}")


def process_batch_file_timed(libclang_id: str, batch_file_path: str) -> RunRecord:
    time_started = time()
    print(f"processing {batch_file_path}", end=' ')

    run_record = process_batch_file(libclang_id, batch_file_path)

    print_duration(time() - time_started)

    return run_record


def process_batch_file(libclang_id: str, batch_file_path: str) -> RunRecord:
    run_record = RunRecord(libclang_id, batch_file_path)
    log_file_path = os.path.join(Config.LogDir, run_record.log_file_path)

    create_dir(Config.LogDir)
    create_backup_file(log_file_path)

    run_qtcreator_with_batch_file(batch_file_path, log_file_path)

    csv_file_path = convert_log_file_to_csv_file(log_file_path, run_record.libclang_id)
    run_record.csv_file_path = csv_file_path

    return run_record


def get_libclang_id(libclang_dll: str) -> str:
    file_name = Path(libclang_dll).name
    parts = file_name.split('.')
    identifier = '.'.join(parts[0:-1])
    return identifier


def switch_libclang(libclang_dll: str) -> None:
    print(f"copying '{libclang_dll}' -> '{Config.TargetLibClangDll}'")
    copyfile(libclang_dll, Config.TargetLibClangDll)


def run_qtcreator_with_libclang(libclang_dll: str) -> List[RunRecord]:
    print("")
    switch_libclang(libclang_dll)

    run_records = []
    libclang_id = get_libclang_id(libclang_dll)
    for batch_file in Config.BatchFiles:
        run_record = process_batch_file_timed(libclang_id, batch_file)
        run_records.append(run_record)

    return run_records


def log_id_part_from_libclang_dll(libclang_dll: str) -> str:
    file_name = Path(libclang_dll).name
    parts = file_name.split('.')
    file_name = '.'.join(parts[1:-1])
    return file_name


def merge_generated_csv_files(run_records: List[RunRecord]) -> None:
    batch_file_id_2_run_record: Dict[str, List[RunRecord]] = {}
    for run_record in run_records:
        new_value = [run_record]
        if run_record.batch_file_id in batch_file_id_2_run_record:
            new_value = batch_file_id_2_run_record[run_record.batch_file_id]
            new_value.append(run_record)
        batch_file_id_2_run_record[run_record.batch_file_id] = new_value

    for batch_file_id, runrecord_list in batch_file_id_2_run_record.items():
        csv_file_paths = [run_record.csv_file_path for run_record in runrecord_list]
        merge_file_path = os.path.join(Config.LogDir, batch_file_id + ".csv")

        merge_csv_files.merge_files(merge_file_path, csv_file_paths)
        print(f"generated: {merge_file_path}")


def main() -> None:
    Config.initialize_from_environment()
    Config.dump()

    run_records = []
    for libclang_dll in Config.LibClangDlls:
        run_records += run_qtcreator_with_libclang(libclang_dll)

    print()
    merge_generated_csv_files(run_records)


if __name__ == "__main__":
    main()