From 1f5c77c29881589cf491102be3e34b0b6ff93647 Mon Sep 17 00:00:00 2001 From: Daco Harkes Date: Thu, 13 Dec 2018 18:43:01 +0000 Subject: [PATCH] tools/generate_compile_commands.py conform to style guide Change-Id: I7d0b917968cf232fde4d0650e25d82fa6b4caefe Reviewed-on: https://dart-review.googlesource.com/c/86881 Reviewed-by: Zach Anderson --- tools/generate_compile_commands.py | 84 ++++++++++++++++++++---------- 1 file changed, 57 insertions(+), 27 deletions(-) diff --git a/tools/generate_compile_commands.py b/tools/generate_compile_commands.py index b94885d863b..c8a15060869 100755 --- a/tools/generate_compile_commands.py +++ b/tools/generate_compile_commands.py @@ -4,45 +4,75 @@ # 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. -import subprocess +import argparse +import generate_buildfiles import json +import subprocess +import sys import utils +import os -### Python script to generate compile_commands.json and make it more useful. +# Python script to generate compile_commands.json which is used by the clang +# and intellij language analysis servers used in IDEs such as Visual Studio +# Code and Emacs. HOST_OS = utils.GuessOS() -if not HOST_OS in ['linux', 'macos']: - print ("Generate compile_commands has not been ported to %s yet." % (HOST_OS)) - exit -if HOST_OS == 'linux': - ninja = 'ninja-linux64' - out_folder = ' out' -if HOST_OS == 'macos': - ninja = 'ninja-mac' - out_folder = ' xcodebuild' +def GenerateCompileCommands(options): + gn_result = generate_buildfiles.RunGn(options) + if (gn_result != 0): + return gn_result -subprocess.call("python tools/generate_buildfiles.py", shell=True) + out_folder = utils.GetBuildRoot(HOST_OS, mode="debug", arch="x64") -command_set = json.loads( - subprocess.check_output( - ninja + " -C " + out_folder + "/DebugX64 -t compdb cxx cc h", shell=True + if (not os.path.isdir(out_folder)): + return 1 + + command_set = json.loads( + subprocess.check_output( + ["ninja", "-C", out_folder, "-t", "compdb", "cxx", "cc", "h"] + ) ) -) -commands = [] -for obj in command_set: - C = obj["command"] + commands = [] + for obj in command_set: + command = obj["command"] - # Skip precompiled mode, a lot of code is commented out in precompiled mode - if ("-DDART_PRECOMPILED_RUNTIME" in C): - continue + # Skip precompiled mode, a lot of code is commented out in precompiled mode + if ("-DDART_PRECOMPILED_RUNTIME" in command): + continue - # Remove warnings - C = C.replace("-Werror", "") + # Remove warnings + command = command.replace("-Werror", "") - obj["command"] = C - commands += [obj] + obj["command"] = command + commands += [obj] -json.dump(commands, open("compile_commands.json", "w"), indent=4) + json.dump(commands, open("compile_commands.json", "w"), indent=4) + + return 0 + + +def ParseArgs(args): + args = args[1:] + parser = argparse.ArgumentParser( + description="A script to generate compile_commands.json which is used by" + + "the clang and intellij language analysis servers used in IDEs such as" + + "Visual Studio Code and Emacs") + + parser.add_argument("-v", "--verbose", + help='Verbose output.', + default=False, + action="store_true") + + return parser.parse_args(args) + + +def main(argv): + options = ParseArgs(argv) + return GenerateCompileCommands(options) + + +if __name__ == '__main__': + sys.exit(main(sys.argv))