|
| 1 | +#!/usr/bin/python |
| 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 | + |
| 7 | +"""Script to generate configuration files for analysis servers of C++ and Dart. |
| 8 | +
|
| 9 | +It generates compile_commands.json for C++ clang and intellij and |
| 10 | +analysis_options.yaml for the Dart analyzer. |
| 11 | +""" |
| 12 | + |
| 13 | +import argparse |
| 14 | +import json |
| 15 | +import os |
| 16 | +import subprocess |
| 17 | +import sys |
| 18 | + |
| 19 | +import generate_buildfiles |
| 20 | +import utils |
| 21 | + |
| 22 | +HOST_OS = utils.GuessOS() |
| 23 | + |
| 24 | + |
| 25 | +def GenerateIdeFiles(options): |
| 26 | + GenerateCompileCommands(options) |
| 27 | + GenerateAnalysisOptions(options) |
| 28 | + |
| 29 | + |
| 30 | +def GenerateCompileCommands(options): |
| 31 | + """Generate compile_commands.json for the C++ analysis servers. |
| 32 | +
|
| 33 | + compile_commands.json is used by the c++ clang and intellij language analysis |
| 34 | + servers used in IDEs such as Visual Studio Code and Emacs. |
| 35 | +
|
| 36 | + Args: |
| 37 | + options: supported options include: verbose, force, dir |
| 38 | +
|
| 39 | + Returns: |
| 40 | + success (0) or failure (non zero) |
| 41 | + """ |
| 42 | + |
| 43 | + fname = os.path.join(options.dir, "compile_commands.json") |
| 44 | + |
| 45 | + if os.path.isfile(fname) and not options.force: |
| 46 | + print fname + " already exists, use --force to override" |
| 47 | + return |
| 48 | + |
| 49 | + gn_result = generate_buildfiles.RunGn(options) |
| 50 | + if gn_result != 0: |
| 51 | + return gn_result |
| 52 | + |
| 53 | + out_folder = utils.GetBuildRoot(HOST_OS, mode="debug", arch="x64") |
| 54 | + |
| 55 | + if not os.path.isdir(out_folder): |
| 56 | + return 1 |
| 57 | + |
| 58 | + command_set = json.loads( |
| 59 | + subprocess.check_output( |
| 60 | + ["ninja", "-C", out_folder, "-t", "compdb", "cxx", "cc", "h"])) |
| 61 | + |
| 62 | + commands = [] |
| 63 | + for obj in command_set: |
| 64 | + command = obj["command"] |
| 65 | + |
| 66 | + # Skip precompiled mode, a lot of code is commented out in precompiled mode |
| 67 | + if "-DDART_PRECOMPILED_RUNTIME" in command: |
| 68 | + continue |
| 69 | + |
| 70 | + # Remove warnings |
| 71 | + command = command.replace("-Werror", "") |
| 72 | + |
| 73 | + obj["command"] = command |
| 74 | + commands += [obj] |
| 75 | + |
| 76 | + with open(fname, "w") as f: |
| 77 | + json.dump(commands, f, indent=4) |
| 78 | + |
| 79 | + return 0 |
| 80 | + |
| 81 | + |
| 82 | +def GenerateAnalysisOptions(options): |
| 83 | + """Generate analysis_optioms.yaml for the Dart analyzer. |
| 84 | +
|
| 85 | + To prevent dartanalyzer from tripping on the non-Dart files when it is |
| 86 | + started from the root dart-sdk directory. |
| 87 | + https://github.com/Dart-Code/Dart-Code/issues/1295 |
| 88 | +
|
| 89 | + Args: |
| 90 | + options: supported options include: force, dir |
| 91 | + """ |
| 92 | + contents = """analyzer: |
| 93 | + exclude: |
| 94 | + - docs/newsletter/20171103/** |
| 95 | + - out/** |
| 96 | + - runtime/** |
| 97 | + - samples-dev/swarm/** |
| 98 | + - sdk/lib/** |
| 99 | + - tests/** |
| 100 | + - third_party/observatory_pub_packages/** |
| 101 | + - third_party/pkg/** |
| 102 | + - third_party/pkg_tested/dart_style/** |
| 103 | + - third_party/tcmalloc/** |
| 104 | + - tools/apps/update_homebrew/** |
| 105 | + - tools/dart2js/** |
| 106 | + - tools/dom/** |
| 107 | + - tools/sdks/dart-sdk/lib/** |
| 108 | + - tools/status_clean.dart |
| 109 | + - xcodebuild / **""" |
| 110 | + |
| 111 | + fname = os.path.join(options.dir, "analysis_options.yaml") |
| 112 | + |
| 113 | + if os.path.isfile(fname) and not options.force: |
| 114 | + print fname + " already exists, use --force to override" |
| 115 | + return |
| 116 | + |
| 117 | + with open(fname, "w") as f: |
| 118 | + f.write(contents) |
| 119 | + |
| 120 | + |
| 121 | +def main(argv): |
| 122 | + parser = argparse.ArgumentParser( |
| 123 | + description="Python script to generate compile_commands.json and " |
| 124 | + "analysis_options.yaml which are used by the analysis servers for " |
| 125 | + "c++ and Dart.") |
| 126 | + |
| 127 | + parser.add_argument("-v", "--verbose", |
| 128 | + help="Verbose output.", |
| 129 | + action="store_true") |
| 130 | + |
| 131 | + parser.add_argument("-f", "--force", |
| 132 | + help="Override files.", |
| 133 | + action="store_true") |
| 134 | + |
| 135 | + parser.add_argument("-d", "--dir", |
| 136 | + help="Target directory.", |
| 137 | + default=utils.DART_DIR) |
| 138 | + |
| 139 | + options = parser.parse_args(argv[1:]) |
| 140 | + |
| 141 | + return GenerateIdeFiles(options) |
| 142 | + |
| 143 | + |
| 144 | +if __name__ == "__main__": |
| 145 | + sys.exit(main(sys.argv)) |
0 commit comments