1 // Copyright 2018 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef TOOLS_GN_COMPILE_COMMANDS_WRITER_H_ 6 #define TOOLS_GN_COMPILE_COMMANDS_WRITER_H_ 7 8 #include <optional> 9 #include <vector> 10 11 #include "gn/err.h" 12 #include "gn/label_pattern.h" 13 #include "gn/target.h" 14 15 class Builder; 16 class BuildSettings; 17 18 class CompileCommandsWriter { 19 public: 20 // Writes a compilation database to the given file name consisting of the 21 // recursive dependencies of all targets that match or are dependencies of 22 // targets that match any given pattern. 23 // 24 // The legacy target filters takes a deprecated list of comma-separated target 25 // names ("target_name1,target_name2...") which are matched against targets in 26 // any directory. This is passed as an optional to encapsulate the legacy 27 // behavior that providing the switch with no patterns matches everything, but 28 // not passing the flag (nullopt for the function parameter) matches nothing. 29 // 30 // The union of the legacy matches and the target patterns are used. 31 // 32 // TODO(https://bugs.chromium.org/p/gn/issues/detail?id=302): 33 // Remove this legacy target filters behavior. 34 static bool RunAndWriteFiles( 35 const BuildSettings* build_setting, 36 const std::vector<const Target*>& all_targets, 37 const std::vector<LabelPattern>& patterns, 38 const std::optional<std::string>& legacy_target_filters, 39 const base::FilePath& output_path, 40 Err* err); 41 42 // Collects all the targets whose commands should get written as part of 43 // RunAndWriteFiles() (separated out for unit testing). 44 static std::vector<const Target*> CollectTargets( 45 const BuildSettings* build_setting, 46 const std::vector<const Target*>& all_targets, 47 const std::vector<LabelPattern>& patterns, 48 const std::optional<std::string>& legacy_target_filters, 49 Err* err); 50 51 static std::string RenderJSON(const BuildSettings* build_settings, 52 std::vector<const Target*>& all_targets); 53 54 // Does a depth-first search of the graph starting at the input target and 55 // collects all recursive dependencies of those targets. 56 static std::vector<const Target*> CollectDepsOfMatches( 57 const std::vector<const Target*>& input_targets); 58 59 // Performs the legacy target_name filtering. 60 static std::vector<const Target*> FilterLegacyTargets( 61 const std::vector<const Target*>& all_targets, 62 const std::string& target_filter_string); 63 }; 64 65 #endif // TOOLS_GN_COMPILE_COMMANDS_WRITER_H_ 66