Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Han-Wen Nienhuys | 0d56b6c | 2015-05-12 14:52:30 +0000 | [diff] [blame] | 15 | #ifndef BAZEL_SRC_MAIN_CPP_OPTION_PROCESSOR_H_ |
| 16 | #define BAZEL_SRC_MAIN_CPP_OPTION_PROCESSOR_H_ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 17 | |
| 18 | #include <list> |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | #include <memory> |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
Googler | ebd28a9 | 2018-02-07 08:46:31 -0800 | [diff] [blame] | 23 | #include "src/main/cpp/rc_file.h" |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 24 | #include "src/main/cpp/startup_options.h" |
Thiago Farina | 7f9357f | 2015-04-23 13:57:43 +0000 | [diff] [blame] | 25 | #include "src/main/cpp/util/exit_code.h" |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 26 | |
| 27 | namespace blaze { |
| 28 | |
Julio Merino | e3e3bfa | 2016-12-08 22:22:12 +0000 | [diff] [blame] | 29 | class WorkspaceLayout; |
| 30 | |
ccalvarin | 1cbe62a | 2017-08-14 21:09:07 +0200 | [diff] [blame] | 31 | // Broken down structure of the command line into logical components. The raw |
| 32 | // arguments should not be referenced after this structure exists. This |
| 33 | // breakdown should suffice to access the parts of the command line that the |
| 34 | // client cares about, notably the binary and startup startup options. |
Luis Fernando Pino Duque | 0311fc5 | 2016-11-21 13:20:50 +0000 | [diff] [blame] | 35 | struct CommandLine { |
| 36 | const std::string path_to_binary; |
| 37 | const std::vector<std::string> startup_args; |
| 38 | const std::string command; |
| 39 | const std::vector<std::string> command_args; |
| 40 | |
| 41 | CommandLine(const std::string& path_to_binary_arg, |
| 42 | const std::vector<std::string>& startup_args_arg, |
| 43 | const std::string& command_arg, |
| 44 | const std::vector<std::string>& command_args_arg) |
| 45 | : path_to_binary(path_to_binary_arg), |
| 46 | startup_args(startup_args_arg), |
| 47 | command(command_arg), |
| 48 | command_args(command_args_arg) {} |
| 49 | }; |
| 50 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 51 | // This class is responsible for parsing the command line of the Blaze binary, |
| 52 | // parsing blazerc files, and putting together the command that should be sent |
| 53 | // to the server. |
| 54 | class OptionProcessor { |
| 55 | public: |
Julio Merino | e3e3bfa | 2016-12-08 22:22:12 +0000 | [diff] [blame] | 56 | OptionProcessor(const WorkspaceLayout* workspace_layout, |
| 57 | std::unique_ptr<StartupOptions> default_startup_options); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 58 | |
Googler | ebd28a9 | 2018-02-07 08:46:31 -0800 | [diff] [blame] | 59 | virtual ~OptionProcessor() {} |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 60 | |
Luis Fernando Pino Duque | 0311fc5 | 2016-11-21 13:20:50 +0000 | [diff] [blame] | 61 | // Splits the arguments of a command line invocation. |
| 62 | // |
| 63 | // For instance: |
| 64 | // output/bazel --foo --bar=42 --bar blah build --myflag value :mytarget |
| 65 | // |
| 66 | // returns a CommandLine structure with the following values: |
| 67 | // result.path_to_binary = "output/bazel" |
| 68 | // result.startup_args = {"--foo", "--bar=42", "--bar=blah"} |
| 69 | // result.command = "build" |
ccalvarin | 1cbe62a | 2017-08-14 21:09:07 +0200 | [diff] [blame] | 70 | // result.command_args = {"--myflag", "value", ":mytarget"} |
Luis Fernando Pino Duque | 0311fc5 | 2016-11-21 13:20:50 +0000 | [diff] [blame] | 71 | // |
| 72 | // Note that result.startup_args is guaranteed to contain only valid |
| 73 | // startup options (w.r.t. StartupOptions::IsUnary and |
| 74 | // StartupOptions::IsNullary) and unary startup args of the form '--bar blah' |
| 75 | // are rewritten as '--bar=blah' for uniformity. |
| 76 | // In turn, the command and command args are not rewritten nor validated. |
| 77 | // |
| 78 | // If the method fails then error will contain the cause, otherwise error |
| 79 | // remains untouched. |
lpino | bc28fa6 | 2017-07-13 13:52:56 +0200 | [diff] [blame] | 80 | virtual std::unique_ptr<CommandLine> SplitCommandLine( |
Luis Fernando Pino Duque | 0311fc5 | 2016-11-21 13:20:50 +0000 | [diff] [blame] | 81 | const std::vector<std::string>& args, |
lpino | c00ba52 | 2017-07-10 19:17:40 +0200 | [diff] [blame] | 82 | std::string* error) const; |
Luis Fernando Pino Duque | 0311fc5 | 2016-11-21 13:20:50 +0000 | [diff] [blame] | 83 | |
ccalvarin | d8dfd78 | 2018-04-19 08:47:28 -0700 | [diff] [blame] | 84 | // Parse a command line and the appropriate blazerc files and stores the |
| 85 | // results. This should be invoked only once per OptionProcessor object. |
Thiago Farina | 80bb0f2 | 2016-10-17 15:57:13 +0000 | [diff] [blame] | 86 | blaze_exit_code::ExitCode ParseOptions(const std::vector<std::string>& args, |
| 87 | const std::string& workspace, |
| 88 | const std::string& cwd, |
| 89 | std::string* error); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 90 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 91 | // Get the Blaze command to be executed. |
| 92 | // Returns an empty string if no command was found on the command line. |
lpino | c00ba52 | 2017-07-10 19:17:40 +0200 | [diff] [blame] | 93 | std::string GetCommand() const; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 94 | |
| 95 | // Gets the arguments to the command. This is put together from the default |
| 96 | // options specified in the blazerc file(s), the command line, and various |
| 97 | // bits and pieces of information about the environment the blaze binary is |
| 98 | // executed in. |
lpino | c00ba52 | 2017-07-10 19:17:40 +0200 | [diff] [blame] | 99 | std::vector<std::string> GetCommandArguments() const; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 100 | |
lpino | 233b72d | 2017-07-05 11:08:40 -0400 | [diff] [blame] | 101 | // Gets the arguments explicitly provided by the user's command line. |
| 102 | std::vector<std::string> GetExplicitCommandArguments() const; |
| 103 | |
lpino | 3e36d7a | 2017-07-11 22:14:42 +0200 | [diff] [blame] | 104 | virtual StartupOptions* GetParsedStartupOptions() const; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 105 | |
ccalvarin | 1cbe62a | 2017-08-14 21:09:07 +0200 | [diff] [blame] | 106 | // Prints a message about the origin of startup options. This should be called |
ccalvarin | d8dfd78 | 2018-04-19 08:47:28 -0700 | [diff] [blame] | 107 | // if the server is not started or called, in case the options are related to |
| 108 | // the failure. Otherwise, the server will handle any required logging. |
ccalvarin | 1cbe62a | 2017-08-14 21:09:07 +0200 | [diff] [blame] | 109 | void PrintStartupOptionsProvenanceMessage() const; |
| 110 | |
Googler | ebd28a9 | 2018-02-07 08:46:31 -0800 | [diff] [blame] | 111 | // Constructs all synthetic command args that should be passed to the |
| 112 | // server to configure blazerc options and client environment. |
lpino | c00ba52 | 2017-07-10 19:17:40 +0200 | [diff] [blame] | 113 | static std::vector<std::string> GetBlazercAndEnvCommandArgs( |
| 114 | const std::string& cwd, |
Googler | ebd28a9 | 2018-02-07 08:46:31 -0800 | [diff] [blame] | 115 | const std::vector<std::unique_ptr<RcFile>>& blazercs, |
| 116 | const std::vector<std::string>& env); |
lpino | c00ba52 | 2017-07-10 19:17:40 +0200 | [diff] [blame] | 117 | |
ccalvarin | 5e5ee0d | 2018-08-23 08:56:01 -0700 | [diff] [blame] | 118 | // Finds and parses the appropriate RcFiles: |
| 119 | // - system rc (unless --nosystem_rc) |
| 120 | // - workspace, %workspace%/.bazelrc (unless --noworkspace_rc, or we aren't |
| 121 | // in a workspace directory, indicated by an empty workspace parameter) |
| 122 | // - user, $HOME/.bazelrc (unless --nohome_rc) |
| 123 | // - command-line provided, if a value is passed with --bazelrc. |
ccalvarin | d8dfd78 | 2018-04-19 08:47:28 -0700 | [diff] [blame] | 124 | virtual blaze_exit_code::ExitCode GetRcFiles( |
| 125 | const WorkspaceLayout* workspace_layout, const std::string& workspace, |
| 126 | const std::string& cwd, const CommandLine* cmd_line, |
| 127 | std::vector<std::unique_ptr<RcFile>>* result_rc_files, |
| 128 | std::string* error) const; |
| 129 | |
ccalvarin | d8dfd78 | 2018-04-19 08:47:28 -0700 | [diff] [blame] | 130 | private: |
jmmv | 6455379 | 2018-04-19 12:32:59 -0700 | [diff] [blame] | 131 | blaze_exit_code::ExitCode ParseStartupOptions( |
| 132 | const std::vector<std::unique_ptr<RcFile>>& rc_files, |
| 133 | std::string* error); |
lpino | c00ba52 | 2017-07-10 19:17:40 +0200 | [diff] [blame] | 134 | |
| 135 | // An ordered list of command args that contain information about the |
| 136 | // execution environment and the flags passed via the bazelrc files. |
| 137 | std::vector<std::string> blazerc_and_env_command_args_; |
| 138 | |
| 139 | // The command line constructed after calling ParseOptions. |
| 140 | std::unique_ptr<CommandLine> cmd_line_; |
| 141 | |
Julio Merino | e3e3bfa | 2016-12-08 22:22:12 +0000 | [diff] [blame] | 142 | const WorkspaceLayout* workspace_layout_; |
lpino | c00ba52 | 2017-07-10 19:17:40 +0200 | [diff] [blame] | 143 | |
lpino | b379f65 | 2017-04-07 12:05:09 +0000 | [diff] [blame] | 144 | // The startup options parsed from args, this field is initialized by |
| 145 | // ParseOptions. |
Julio Merino | 2877485 | 2016-09-14 16:59:46 +0000 | [diff] [blame] | 146 | std::unique_ptr<StartupOptions> parsed_startup_options_; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 147 | }; |
| 148 | |
ccalvarin | d8dfd78 | 2018-04-19 08:47:28 -0700 | [diff] [blame] | 149 | // Parses and returns the contents of the rc file. |
| 150 | blaze_exit_code::ExitCode ParseRcFile(const WorkspaceLayout* workspace_layout, |
| 151 | const std::string& workspace, |
| 152 | const std::string& rc_file_path, |
| 153 | std::unique_ptr<RcFile>* result_rc_file, |
| 154 | std::string* error); |
| 155 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 156 | } // namespace blaze |
Thiago Farina | 2e4c2aa | 2015-06-12 07:18:20 +0000 | [diff] [blame] | 157 | |
Han-Wen Nienhuys | 0d56b6c | 2015-05-12 14:52:30 +0000 | [diff] [blame] | 158 | #endif // BAZEL_SRC_MAIN_CPP_OPTION_PROCESSOR_H_ |