2021-04-15 10:10:20 +00:00
|
|
|
#!/usr/bin/env python3
|
2019-01-04 09:05:23 +00:00
|
|
|
#
|
|
|
|
# Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
|
|
|
|
# for details. All rights reserved. Use of this source code is governed by a
|
|
|
|
# BSD-style license that can be found in the LICENSE file.
|
|
|
|
"""Script to generate configuration files for analysis servers of C++ and Dart.
|
|
|
|
|
|
|
|
It generates compile_commands.json for C++ clang and intellij and
|
|
|
|
analysis_options.yaml for the Dart analyzer.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import json
|
|
|
|
import os
|
2022-09-27 11:24:03 +00:00
|
|
|
import re
|
2019-01-04 09:05:23 +00:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import generate_buildfiles
|
|
|
|
import utils
|
|
|
|
|
|
|
|
HOST_OS = utils.GuessOS()
|
|
|
|
|
|
|
|
|
|
|
|
def GenerateIdeFiles(options):
|
2019-08-05 20:34:31 +00:00
|
|
|
GenerateCompileCommands(options)
|
2019-01-04 09:05:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
def GenerateCompileCommands(options):
|
2019-08-05 20:34:31 +00:00
|
|
|
"""Generate compile_commands.json for the C++ analysis servers.
|
2019-01-04 09:05:23 +00:00
|
|
|
|
|
|
|
compile_commands.json is used by the c++ clang and intellij language analysis
|
|
|
|
servers used in IDEs such as Visual Studio Code and Emacs.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
options: supported options include: verbose, force, dir
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
success (0) or failure (non zero)
|
|
|
|
"""
|
|
|
|
|
2019-08-05 20:34:31 +00:00
|
|
|
fname = os.path.join(options.dir, "compile_commands.json")
|
2019-01-04 09:05:23 +00:00
|
|
|
|
2019-08-05 20:34:31 +00:00
|
|
|
if os.path.isfile(fname) and not options.force:
|
2021-04-15 10:10:20 +00:00
|
|
|
print(fname + " already exists, use --force to override")
|
2019-08-05 20:34:31 +00:00
|
|
|
return
|
2019-01-04 09:05:23 +00:00
|
|
|
|
2019-08-05 20:34:31 +00:00
|
|
|
gn_result = generate_buildfiles.RunGn(options)
|
|
|
|
if gn_result != 0:
|
|
|
|
return gn_result
|
2019-01-04 09:05:23 +00:00
|
|
|
|
2020-10-19 14:01:57 +00:00
|
|
|
out_folder = utils.GetBuildRoot(HOST_OS,
|
|
|
|
mode="debug",
|
|
|
|
arch=options.arch,
|
|
|
|
target_os=options.os)
|
2019-01-04 09:05:23 +00:00
|
|
|
|
2019-08-05 20:34:31 +00:00
|
|
|
if not os.path.isdir(out_folder):
|
|
|
|
return 1
|
2019-01-04 09:05:23 +00:00
|
|
|
|
2019-08-05 20:34:31 +00:00
|
|
|
command_set = json.loads(
|
2022-09-26 12:30:17 +00:00
|
|
|
subprocess.check_output([
|
|
|
|
"buildtools/ninja/ninja", "-C", out_folder, "-t", "compdb", "-x",
|
|
|
|
"cxx", "cc", "h"
|
|
|
|
]))
|
2019-01-04 09:05:23 +00:00
|
|
|
|
2019-08-05 20:34:31 +00:00
|
|
|
commands = []
|
|
|
|
for obj in command_set:
|
|
|
|
command = obj["command"]
|
2019-01-04 09:05:23 +00:00
|
|
|
|
2019-08-05 20:34:31 +00:00
|
|
|
# Skip precompiled mode, a lot of code is commented out in precompiled mode
|
|
|
|
if "-DDART_PRECOMPILED_RUNTIME" in command:
|
|
|
|
continue
|
2019-01-04 09:05:23 +00:00
|
|
|
|
2019-08-05 20:34:31 +00:00
|
|
|
# Remove warnings
|
|
|
|
command = command.replace("-Werror", "")
|
2019-01-04 09:05:23 +00:00
|
|
|
|
2022-09-26 12:30:17 +00:00
|
|
|
# Remove ninja prepend on Windows.
|
|
|
|
# This is not fully correct, as now it fails to find a sysroot for
|
|
|
|
# Windows. However, clangd completely fails with the `-t` flag.
|
2022-09-27 11:24:03 +00:00
|
|
|
command = re.sub(r"([^\s]*)ninja -t msvc -e environment.x64 --", "",
|
|
|
|
command)
|
2022-09-26 12:30:17 +00:00
|
|
|
|
|
|
|
# Add sysroot from out\DebugX64\environment.x64 on Windows.
|
|
|
|
# TODO(dacoharkes): Fetch the paths from that file.
|
|
|
|
windowsSysroots = [
|
|
|
|
'C:\\src\\depot_tools\\win_toolchain\\vs_files\\1023ce2e82\\Windows Kits\\10\\Include\\10.0.20348.0\\um',
|
|
|
|
'C:\\src\\depot_tools\\win_toolchain\\vs_files\\1023ce2e82\\Windows Kits\\10\\Include\\10.0.20348.0\\shared',
|
|
|
|
'C:\\src\\depot_tools\\win_toolchain\\vs_files\\1023ce2e82\\Windows Kits\\10\\Include\\10.0.20348.0\\winrt',
|
|
|
|
'C:\\src\\depot_tools\\win_toolchain\\vs_files\\1023ce2e82\\Windows Kits\\10\\Include\\10.0.20348.0\\ucrt',
|
|
|
|
'C:\\src\\depot_tools\\win_toolchain\\vs_files\\1023ce2e82\\VC\\Tools\\MSVC\\14.29.30133\\include',
|
|
|
|
'C:\\src\\depot_tools\\win_toolchain\\vs_files\\1023ce2e82\\VC\\Tools\\MSVC\\14.29.30133\\atlmfc\\include',
|
|
|
|
]
|
|
|
|
for windowsSysroot in windowsSysroots:
|
|
|
|
command = command.replace(
|
|
|
|
"-DDART_TARGET_OS_WINDOWS",
|
|
|
|
"-DDART_TARGET_OS_WINDOWS \"-I%s\"" % windowsSysroot)
|
|
|
|
|
|
|
|
# Prevent packing errors from causing fatal_too_many_errors on Windows.
|
|
|
|
command = command.replace("-DDART_TARGET_OS_WINDOWS",
|
|
|
|
"-DDART_TARGET_OS_WINDOWS -ferror-limit=0")
|
|
|
|
|
2019-08-05 20:34:31 +00:00
|
|
|
obj["command"] = command
|
|
|
|
commands += [obj]
|
2019-01-04 09:05:23 +00:00
|
|
|
|
2019-08-05 20:34:31 +00:00
|
|
|
with open(fname, "w") as f:
|
|
|
|
json.dump(commands, f, indent=4)
|
2019-01-04 09:05:23 +00:00
|
|
|
|
2019-08-05 20:34:31 +00:00
|
|
|
return 0
|
2019-01-04 09:05:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main(argv):
|
2019-08-05 20:34:31 +00:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="Python script to generate compile_commands.json and "
|
|
|
|
"analysis_options.yaml which are used by the analysis servers for "
|
|
|
|
"c++ and Dart.")
|
2019-01-04 09:05:23 +00:00
|
|
|
|
2020-10-19 14:01:57 +00:00
|
|
|
parser.add_argument("-v",
|
|
|
|
"--verbose",
|
|
|
|
help="Verbose output.",
|
|
|
|
action="store_true")
|
|
|
|
|
|
|
|
parser.add_argument("-f",
|
|
|
|
"--force",
|
|
|
|
help="Override files.",
|
|
|
|
action="store_true")
|
|
|
|
|
|
|
|
parser.add_argument("-d",
|
|
|
|
"--dir",
|
|
|
|
help="Target directory.",
|
|
|
|
default=utils.DART_DIR)
|
|
|
|
|
|
|
|
parser.add_argument("-a",
|
|
|
|
"--arch",
|
|
|
|
help="Target architecture for runtime sources.",
|
|
|
|
default="x64")
|
|
|
|
|
|
|
|
parser.add_argument("-s",
|
|
|
|
"--os",
|
|
|
|
help="Target operating system for runtime sources.",
|
|
|
|
default=HOST_OS)
|
2019-06-04 08:13:38 +00:00
|
|
|
|
2019-08-05 20:34:31 +00:00
|
|
|
options = parser.parse_args(argv[1:])
|
2019-01-04 09:05:23 +00:00
|
|
|
|
2019-08-05 20:34:31 +00:00
|
|
|
return GenerateIdeFiles(options)
|
2019-01-04 09:05:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2019-08-05 20:34:31 +00:00
|
|
|
sys.exit(main(sys.argv))
|