Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
generate_idefiles.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2#
3# Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
4# for details. All rights reserved. Use of this source code is governed by a
5# BSD-style license that can be found in the LICENSE file.
6"""Script to generate configuration files for analysis servers of C++ and Dart.
7
8It generates compile_commands.json for C++ clang and intellij and
9analysis_options.yaml for the Dart analyzer.
10"""
11
12import argparse
13import json
14import os
15import re
16import subprocess
17import sys
18
19import generate_buildfiles
20import utils
21
22HOST_OS = utils.GuessOS()
23
24
25def GenerateIdeFiles(options):
27
28
30 """Generate compile_commands.json for the C++ analysis servers.
31
32 compile_commands.json is used by the c++ clang and intellij language analysis
33 servers used in IDEs such as Visual Studio Code and Emacs.
34
35 Args:
36 options: supported options include: verbose, force, dir
37
38 Returns:
39 success (0) or failure (non zero)
40 """
41
42 fname = os.path.join(options.dir, "compile_commands.json")
43
44 if os.path.isfile(fname) and not options.force:
45 print(fname + " already exists, use --force to override")
46 return
47
48 gn_result = generate_buildfiles.RunGn(options)
49 if gn_result != 0:
50 return gn_result
51
52 out_folder = utils.GetBuildRoot(HOST_OS,
53 mode="debug",
54 arch=options.arch,
55 target_os=options.os,
56 sanitizer=options.sanitizer)
57
58 if not os.path.isdir(out_folder):
59 return 1
60
61 command_set = json.loads(
62 subprocess.check_output([
63 "buildtools/ninja/ninja", "-C", out_folder, "-t", "compdb", "-x",
64 "cxx", "cc", "h"
65 ]))
66
67 commands = []
68 for obj in command_set:
69 command = obj["command"]
70
71 # Skip precompiled mode, a lot of code is commented out in precompiled mode
72 if "-DDART_PRECOMPILED_RUNTIME" in command:
73 continue
74
75 # Remove warnings
76 command = command.replace("-Werror", "")
77
78 # Remove ninja prepend on Windows.
79 # This is not fully correct, as now it fails to find a sysroot for
80 # Windows. However, clangd completely fails with the `-t` flag.
81 command = re.sub(r"([^\s]*)ninja -t msvc -e environment.x64 --", "",
82 command)
83
84 # Add sysroot from out\DebugX64\environment.x64 on Windows.
85 # TODO(dacoharkes): Fetch the paths from that file.
86 windowsSysroots = [
87 'C:\\src\\depot_tools\\win_toolchain\\vs_files\\27370823e7\\Windows Kits\\10\\Include\\10.0.22621.0\\um',
88 'C:\\src\\depot_tools\\win_toolchain\\vs_files\\27370823e7\\Windows Kits\\10\\Include\\10.0.22621.0\\shared',
89 'C:\\src\\depot_tools\\win_toolchain\\vs_files\\27370823e7\\Windows Kits\\10\\Include\\10.0.22621.0\\winrt',
90 'C:\\src\\depot_tools\\win_toolchain\\vs_files\\27370823e7\\Windows Kits\\10\\Include\\10.0.22621.0\\ucrt',
91 'C:\\src\\depot_tools\\win_toolchain\\vs_files\\27370823e7\\VC\\Tools\\MSVC\\14.34.31933\\include',
92 'C:\\src\\depot_tools\\win_toolchain\\vs_files\\27370823e7\\VC\\Tools\\MSVC\\14.34.31933\\atlmfc\\include',
93 ]
94 for windowsSysroot in windowsSysroots:
95 command = command.replace(
96 "-DDART_TARGET_OS_WINDOWS",
97 "-DDART_TARGET_OS_WINDOWS \"-I%s\"" % windowsSysroot)
98
99 # Prevent packing errors from causing fatal_too_many_errors on Windows.
100 command = command.replace("-DDART_TARGET_OS_WINDOWS",
101 "-DDART_TARGET_OS_WINDOWS -ferror-limit=0")
102
103 obj["command"] = command
104 commands += [obj]
105
106 with open(fname, "w") as f:
107 json.dump(commands, f, indent=4)
108
109 return 0
110
111
112def main(argv):
113 parser = argparse.ArgumentParser(
114 description="Python script to generate compile_commands.json and "
115 "analysis_options.yaml which are used by the analysis servers for "
116 "c++ and Dart.")
117
118 parser.add_argument("-v",
119 "--verbose",
120 help="Verbose output.",
121 action="store_true")
122
123 parser.add_argument("-f",
124 "--force",
125 help="Override files.",
126 action="store_true")
127
128 parser.add_argument("-d",
129 "--dir",
130 help="Target directory.",
131 default=utils.DART_DIR)
132
133 parser.add_argument("-a",
134 "--arch",
135 help="Target architecture for runtime sources.",
136 default="x64")
137
138 parser.add_argument("-s",
139 "--os",
140 help="Target operating system for runtime sources.",
141 default=HOST_OS)
142
143 parser.add_argument('--sanitizer',
144 type=str,
145 help='Build variants (comma-separated).',
146 metavar='[none,asan,lsan,msan,tsan,ubsan]',
147 default='none')
148
149 options = parser.parse_args(argv[1:])
150
151 return GenerateIdeFiles(options)
152
153
154if __name__ == "__main__":
155 sys.exit(main(sys.argv))
void print(void *str)
Definition bridge.cpp:126
GenerateCompileCommands(options)
Definition main.py:1
GetBuildRoot(host_os, mode=None, arch=None, sanitizer=None)
Definition utils.py:143
GuessOS()
Definition utils.py:21