Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1 | // Copyright 2014 Google Inc. All rights reserved. |
| 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> |
| 19 | #include <map> |
| 20 | #include <memory> |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
Han-Wen Nienhuys | 36fbe63 | 2015-04-21 13:58:08 +0000 | [diff] [blame] | 24 | #include "src/main/cpp/blaze_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 | |
| 29 | using std::string; |
| 30 | |
| 31 | // This class is responsible for parsing the command line of the Blaze binary, |
| 32 | // parsing blazerc files, and putting together the command that should be sent |
| 33 | // to the server. |
| 34 | class OptionProcessor { |
| 35 | public: |
| 36 | OptionProcessor(); |
| 37 | |
Han-Wen Nienhuys | 255cf09 | 2015-05-22 14:38:59 +0000 | [diff] [blame^] | 38 | virtual ~OptionProcessor(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 39 | |
| 40 | // Parse a command line and the appropriate blazerc files. This should be |
| 41 | // invoked only once per OptionProcessor object. |
| 42 | blaze_exit_code::ExitCode ParseOptions(const std::vector<string>& args, |
| 43 | const string& workspace, |
| 44 | const string& cwd, |
| 45 | string* error); |
| 46 | |
| 47 | blaze_exit_code::ExitCode ParseOptions(int argc, const char* argv[], |
| 48 | const string& workspace, |
| 49 | const string& cwd, |
| 50 | string* error); |
| 51 | |
| 52 | // Get the Blaze command to be executed. |
| 53 | // Returns an empty string if no command was found on the command line. |
| 54 | const string& GetCommand() const; |
| 55 | |
| 56 | // Gets the arguments to the command. This is put together from the default |
| 57 | // options specified in the blazerc file(s), the command line, and various |
| 58 | // bits and pieces of information about the environment the blaze binary is |
| 59 | // executed in. |
| 60 | void GetCommandArguments(std::vector<string>* result) const; |
| 61 | |
| 62 | const BlazeStartupOptions& GetParsedStartupOptions() const; |
| 63 | |
| 64 | virtual string FindDepotBlazerc(const string& workspace); |
| 65 | virtual string FindAlongsideBinaryBlazerc(const string& cwd, |
| 66 | const string& arg0); |
| 67 | virtual blaze_exit_code::ExitCode FindUserBlazerc(const char* cmdLineRcFile, |
| 68 | const string& rc_basename, |
| 69 | const string& workspace, |
| 70 | string* user_blazerc_file, |
| 71 | string* error); |
| 72 | |
| 73 | private: |
| 74 | class RcOption { |
| 75 | public: |
| 76 | RcOption(int rcfile_index, const string& option); |
| 77 | |
| 78 | const int rcfile_index() const { return rcfile_index_; } |
| 79 | const string& option() const { return option_; } |
| 80 | |
| 81 | private: |
| 82 | int rcfile_index_; |
| 83 | string option_; |
| 84 | }; |
| 85 | |
| 86 | class RcFile { |
| 87 | public: |
| 88 | RcFile(const string& filename, int index); |
| 89 | blaze_exit_code::ExitCode Parse( |
Han-Wen Nienhuys | 255cf09 | 2015-05-22 14:38:59 +0000 | [diff] [blame^] | 90 | std::vector<RcFile*>* rcfiles, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 91 | std::map<string, std::vector<RcOption> >* rcoptions, |
| 92 | string* error); |
| 93 | const string& Filename() const { return filename_; } |
| 94 | const int Index() const { return index_; } |
| 95 | |
| 96 | private: |
Thiago Farina | 2fd7890 | 2015-05-18 11:37:59 +0000 | [diff] [blame] | 97 | static blaze_exit_code::ExitCode Parse(const string& filename, |
| 98 | const int index, |
Han-Wen Nienhuys | 255cf09 | 2015-05-22 14:38:59 +0000 | [diff] [blame^] | 99 | std::vector<RcFile*>* rcfiles, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 100 | std::map<string, |
| 101 | std::vector<RcOption> >* rcoptions, |
| 102 | std::list<string>* import_stack, |
| 103 | string* error); |
| 104 | |
| 105 | string filename_; |
| 106 | int index_; |
| 107 | }; |
| 108 | |
| 109 | void AddRcfileArgsAndOptions(bool batch, const string& cwd); |
| 110 | blaze_exit_code::ExitCode ParseStartupOptions(string *error); |
| 111 | |
Han-Wen Nienhuys | 255cf09 | 2015-05-22 14:38:59 +0000 | [diff] [blame^] | 112 | std::vector<RcFile*> blazercs_; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 113 | std::map<string, std::vector<RcOption> > rcoptions_; |
| 114 | std::vector<string> args_; |
| 115 | unsigned int startup_args_; |
| 116 | string command_; |
| 117 | std::vector<string> command_arguments_; |
| 118 | bool initialized_; |
| 119 | std::unique_ptr<BlazeStartupOptions> parsed_startup_options_; |
| 120 | }; |
| 121 | |
| 122 | } // namespace blaze |
Han-Wen Nienhuys | 0d56b6c | 2015-05-12 14:52:30 +0000 | [diff] [blame] | 123 | #endif // BAZEL_SRC_MAIN_CPP_OPTION_PROCESSOR_H_ |