OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 #include <algorithm> |
| 6 #include <iterator> |
| 7 #include <set> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" |
| 12 #include "tools/gn/analyzer.h" |
| 13 #include "tools/gn/commands.h" |
| 14 #include "tools/gn/filesystem_utils.h" |
| 15 #include "tools/gn/location.h" |
| 16 #include "tools/gn/setup.h" |
| 17 |
| 18 namespace commands { |
| 19 |
| 20 const char kAnalyze[] = "analyze"; |
| 21 const char kAnalyze_HelpShort[] = |
| 22 "analyze: Analyze which targets are affected by a list of files."; |
| 23 const char kAnalyze_Help[] = |
| 24 "gn analyze <out_dir> <input_path> <output_path>\n" |
| 25 "\n" |
| 26 " Analyze which targets are affected by a list of files.\n" |
| 27 "\n" |
| 28 " This command takes three arguments:\n" |
| 29 "\n" |
| 30 " out_dir is the path to the build directory.\n" |
| 31 "\n" |
| 32 " input_path is a path to a file containing a JSON object with three\n" |
| 33 " fields:\n" |
| 34 "\n" |
| 35 " - \"files\": A list of the filenames to check.\n" |
| 36 "\n" |
| 37 " - \"compile_targets\": A list of the labels targets that we wish to\n" |
| 38 " rebuild, but aren't necessarily needed for testing. The\n" |
| 39 " important difference between this field and \"test_targets\"\n" |
| 40 " is that if an item in the compile_targets list is a group, then\n" |
| 41 " any dependencies of that group will be returned if they are out\n" |
| 42 " of date, but the group itself does not need to be. If the\n" |
| 43 " dependencies themselves are groups, the same filtering is\n" |
| 44 " repeated. This filtering can be used to avoid rebuilding\n" |
| 45 " dependencies of a group that are unaffected by the input files.\n" |
| 46 " The list may contain the string \"all\" to refer to a\n" |
| 47 " pseudo-group that contains every root target in the build graph.\n" |
| 48 "\n" |
| 49 " This filtering behavior is also known as \"pruning\" the list\n" |
| 50 " of compile targets.\n" |
| 51 "\n" |
| 52 " - \"test_targets\": A list of the labels for targets that\n" |
| 53 " are needed to run the tests we wish to run. Unlike\n" |
| 54 " \"compile_targets\", this list may not contain the string \"all\",\n" |
| 55 " because having a test be dependent on everything in the build\n" |
| 56 " would be silly.\n" |
| 57 "\n" |
| 58 " output_path is a path indicating where the results of the command\n" |
| 59 " are to be written. The results will be a file containing a JSON\n" |
| 60 " object with one or more of following fields:\n" |
| 61 "\n" |
| 62 " - \"compile_targets\": A list of the labels derived from the input\n" |
| 63 " compile_targets list that are affected by the input files.\n" |
| 64 " Due to the way the filtering works for compile targets as\n" |
| 65 " described above, this list may contain targets that do not appear\n" |
| 66 " in the input list.\n" |
| 67 "\n" |
| 68 " - \"test_targets\": A list of the labels from the input\n" |
| 69 " test_targets list that are affected by the input files. This list\n" |
| 70 " will be a proper subset of the input list.\n" |
| 71 "\n" |
| 72 " - \"invalid_targets\": A list of any names from the input that\n" |
| 73 " do not exist in the build graph. If this list is non-empty,\n" |
| 74 " the \"error\" field will also be set to \"Invalid targets\".\n" |
| 75 "\n" |
| 76 " - \"status\": A string containing one of three values:\n" |
| 77 "\n" |
| 78 " - \"Found dependency\"\n" |
| 79 " - \"No dependency\"\n" |
| 80 " - \"Found dependency (all)\"\n" |
| 81 "\n" |
| 82 " In the first case, the lists returned in compile_targets and\n" |
| 83 " test_targets should be passed to ninja to build. In the second\n" |
| 84 " case, nothing was affected and no build is necessary. In the third\n" |
| 85 " case, GN could not determine the correct answer and returned the\n" |
| 86 " input as the output in order to be safe.\n" |
| 87 "\n" |
| 88 " - \"error\": This will only be present if an error occurred, and\n" |
| 89 " will contain a string describing the error. This includes cases\n" |
| 90 " where the input file is not in the right format, or contains\n" |
| 91 " invalid targets.\n" |
| 92 |
| 93 " The command returns 1 if it is unable to read the input file or write\n" |
| 94 " the output file, or if there is something wrong with the build such\n" |
| 95 " that gen would also fail, and 0 otherwise. In particular, it returns\n" |
| 96 " 0 even if the \"error\" key is non-empty and a non-fatal error\n" |
| 97 " occurred. In other words, it tries really hard to always write\n" |
| 98 " something to the output JSON and convey errors that way rather than\n" |
| 99 " via return codes.\n"; |
| 100 |
| 101 int RunAnalyze(const std::vector<std::string>& args) { |
| 102 if (args.size() != 3) { |
| 103 Err(Location(), "You're holding it wrong.", |
| 104 "Usage: \"gn analyze <out_dir> <input_path> <output_path>") |
| 105 .PrintToStdout(); |
| 106 return 1; |
| 107 } |
| 108 |
| 109 std::string input; |
| 110 bool ret = base::ReadFileToString(UTF8ToFilePath(args[1]), &input); |
| 111 if (!ret) { |
| 112 Err(Location(), "Input file " + args[1] + " not found.").PrintToStdout(); |
| 113 return 1; |
| 114 } |
| 115 |
| 116 Setup* setup = new Setup; |
| 117 setup->build_settings().set_check_for_bad_items(false); |
| 118 if (!setup->DoSetup(args[0], false) || !setup->Run()) |
| 119 return 1; |
| 120 |
| 121 Analyzer analyzer(setup->builder()); |
| 122 |
| 123 Err err; |
| 124 std::string output = Analyzer(setup->builder()).Analyze(input, &err); |
| 125 if (err.has_error()) { |
| 126 err.PrintToStdout(); |
| 127 return 1; |
| 128 } |
| 129 |
| 130 WriteFile(UTF8ToFilePath(args[2]), output, &err); |
| 131 if (err.has_error()) { |
| 132 err.PrintToStdout(); |
| 133 return 1; |
| 134 } |
| 135 |
| 136 return 0; |
| 137 } |
| 138 |
| 139 } // namespace commands |
OLD | NEW |