Shinichiro Hamaji | 08808d3 | 2015-06-26 08:02:45 +0900 | [diff] [blame] | 1 | // Copyright 2015 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 | |
| 15 | // +build ignore |
| 16 | |
| 17 | #include "flags.h" |
| 18 | |
Shinichiro Hamaji | 4627ab2 | 2016-06-25 11:11:44 +0900 | [diff] [blame] | 19 | #include <stdlib.h> |
Shinichiro Hamaji | 003d06e | 2015-09-09 18:22:04 +0900 | [diff] [blame] | 20 | #include <unistd.h> |
| 21 | |
| 22 | #include "log.h" |
| 23 | #include "strutil.h" |
| 24 | |
| 25 | Flags g_flags; |
| 26 | |
| 27 | static bool ParseCommandLineOptionWithArg(StringPiece option, |
| 28 | char* argv[], |
| 29 | int* index, |
| 30 | const char** out_arg) { |
| 31 | const char* arg = argv[*index]; |
| 32 | if (!HasPrefix(arg, option)) |
| 33 | return false; |
| 34 | if (arg[option.size()] == '\0') { |
| 35 | ++*index; |
| 36 | *out_arg = argv[*index]; |
| 37 | return true; |
| 38 | } |
| 39 | if (arg[option.size()] == '=') { |
| 40 | *out_arg = arg + option.size() + 1; |
| 41 | return true; |
| 42 | } |
| 43 | // E.g, -j999 |
| 44 | if (option.size() == 2) { |
| 45 | *out_arg = arg + option.size(); |
| 46 | return true; |
| 47 | } |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | void Flags::Parse(int argc, char** argv) { |
| 52 | subkati_args.push_back(argv[0]); |
Shinichiro Hamaji | 1a444a8 | 2016-02-16 13:49:49 +0900 | [diff] [blame] | 53 | num_jobs = num_cpus = sysconf(_SC_NPROCESSORS_ONLN); |
Shinichiro Hamaji | 003d06e | 2015-09-09 18:22:04 +0900 | [diff] [blame] | 54 | const char* num_jobs_str; |
Dan Willemsen | a42898a | 2018-06-09 12:09:27 -0700 | [diff] [blame] | 55 | const char* writable_str; |
Shinichiro Hamaji | 003d06e | 2015-09-09 18:22:04 +0900 | [diff] [blame] | 56 | |
Shinichiro Hamaji | 4627ab2 | 2016-06-25 11:11:44 +0900 | [diff] [blame] | 57 | if (const char* makeflags = getenv("MAKEFLAGS")) { |
| 58 | for (StringPiece tok : WordScanner(makeflags)) { |
| 59 | if (!HasPrefix(tok, "-") && tok.find('=') != string::npos) |
| 60 | cl_vars.push_back(tok); |
Po Hu | d570f21 | 2016-06-25 09:08:54 +0800 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
Shinichiro Hamaji | 003d06e | 2015-09-09 18:22:04 +0900 | [diff] [blame] | 64 | for (int i = 1; i < argc; i++) { |
| 65 | const char* arg = argv[i]; |
| 66 | bool should_propagate = true; |
| 67 | int pi = i; |
| 68 | if (!strcmp(arg, "-f")) { |
| 69 | makefile = argv[++i]; |
| 70 | should_propagate = false; |
| 71 | } else if (!strcmp(arg, "-c")) { |
| 72 | is_syntax_check_only = true; |
| 73 | } else if (!strcmp(arg, "-i")) { |
| 74 | is_dry_run = true; |
| 75 | } else if (!strcmp(arg, "-s")) { |
| 76 | is_silent_mode = true; |
Shinichiro Hamaji | c58db9a | 2016-05-12 15:16:12 +0900 | [diff] [blame] | 77 | } else if (!strcmp(arg, "-d")) { |
| 78 | enable_debug = true; |
Shinichiro Hamaji | 003d06e | 2015-09-09 18:22:04 +0900 | [diff] [blame] | 79 | } else if (!strcmp(arg, "--kati_stats")) { |
| 80 | enable_stat_logs = true; |
Shinichiro Hamaji | 644d6b9 | 2015-11-17 14:47:56 +0900 | [diff] [blame] | 81 | } else if (!strcmp(arg, "--warn")) { |
| 82 | enable_kati_warnings = true; |
Shinichiro Hamaji | 003d06e | 2015-09-09 18:22:04 +0900 | [diff] [blame] | 83 | } else if (!strcmp(arg, "--ninja")) { |
| 84 | generate_ninja = true; |
Dan Willemsen | 87b8da7 | 2018-10-26 09:21:23 -0700 | [diff] [blame] | 85 | } else if (!strcmp(arg, "--empty_ninja_file")) { |
| 86 | generate_empty_ninja = true; |
Shinichiro Hamaji | 7223e7b | 2015-09-28 15:17:27 +0900 | [diff] [blame] | 87 | } else if (!strcmp(arg, "--gen_all_targets")) { |
| 88 | gen_all_targets = true; |
Shinichiro Hamaji | 003d06e | 2015-09-09 18:22:04 +0900 | [diff] [blame] | 89 | } else if (!strcmp(arg, "--regen")) { |
| 90 | // TODO: Make this default. |
| 91 | regen = true; |
Dan Willemsen | f6486ce | 2016-09-16 19:21:36 -0700 | [diff] [blame] | 92 | } else if (!strcmp(arg, "--regen_debug")) { |
| 93 | regen_debug = true; |
Shinichiro Hamaji | 003d06e | 2015-09-09 18:22:04 +0900 | [diff] [blame] | 94 | } else if (!strcmp(arg, "--regen_ignoring_kati_binary")) { |
| 95 | regen_ignoring_kati_binary = true; |
| 96 | } else if (!strcmp(arg, "--dump_kati_stamp")) { |
| 97 | dump_kati_stamp = true; |
Dan Willemsen | f6486ce | 2016-09-16 19:21:36 -0700 | [diff] [blame] | 98 | regen_debug = true; |
Shinichiro Hamaji | 003d06e | 2015-09-09 18:22:04 +0900 | [diff] [blame] | 99 | } else if (!strcmp(arg, "--detect_android_echo")) { |
| 100 | detect_android_echo = true; |
Shinichiro Hamaji | f3ad9e0 | 2016-03-03 18:26:17 +0900 | [diff] [blame] | 101 | } else if (!strcmp(arg, "--detect_depfiles")) { |
| 102 | detect_depfiles = true; |
Dan Willemsen | e41c755 | 2017-02-22 14:31:16 -0800 | [diff] [blame] | 103 | } else if (!strcmp(arg, "--color_warnings")) { |
| 104 | color_warnings = true; |
Dan Willemsen | cf1fa88 | 2018-06-10 12:48:47 -0700 | [diff] [blame] | 105 | } else if (!strcmp(arg, "--no_builtin_rules")) { |
| 106 | no_builtin_rules = true; |
Dan Willemsen | 64c31bd | 2018-09-26 14:10:40 -0700 | [diff] [blame] | 107 | } else if (!strcmp(arg, "--no_ninja_prelude")) { |
| 108 | no_ninja_prelude = true; |
Dan Willemsen | f63a3fd | 2017-04-27 23:39:57 -0700 | [diff] [blame] | 109 | } else if (!strcmp(arg, "--werror_find_emulator")) { |
| 110 | werror_find_emulator = true; |
| 111 | } else if (!strcmp(arg, "--werror_overriding_commands")) { |
| 112 | werror_overriding_commands = true; |
Dan Willemsen | 6aafee6 | 2018-06-10 12:48:47 -0700 | [diff] [blame] | 113 | } else if (!strcmp(arg, "--warn_implicit_rules")) { |
| 114 | warn_implicit_rules = true; |
| 115 | } else if (!strcmp(arg, "--werror_implicit_rules")) { |
| 116 | werror_implicit_rules = true; |
| 117 | } else if (!strcmp(arg, "--warn_suffix_rules")) { |
| 118 | warn_suffix_rules = true; |
| 119 | } else if (!strcmp(arg, "--werror_suffix_rules")) { |
| 120 | werror_suffix_rules = true; |
Dan Willemsen | 40f26e1 | 2019-01-21 12:32:18 -0800 | [diff] [blame] | 121 | } else if (!strcmp(arg, "--top_level_phony")) { |
| 122 | top_level_phony = true; |
Dan Willemsen | 8b551c5 | 2018-06-08 16:39:23 -0700 | [diff] [blame] | 123 | } else if (!strcmp(arg, "--warn_real_to_phony")) { |
| 124 | warn_real_to_phony = true; |
| 125 | } else if (!strcmp(arg, "--werror_real_to_phony")) { |
| 126 | warn_real_to_phony = true; |
| 127 | werror_real_to_phony = true; |
| 128 | } else if (!strcmp(arg, "--warn_phony_looks_real")) { |
| 129 | warn_phony_looks_real = true; |
| 130 | } else if (!strcmp(arg, "--werror_phony_looks_real")) { |
| 131 | warn_phony_looks_real = true; |
| 132 | werror_phony_looks_real = true; |
Dan Willemsen | a42898a | 2018-06-09 12:09:27 -0700 | [diff] [blame] | 133 | } else if (!strcmp(arg, "--werror_writable")) { |
| 134 | werror_writable = true; |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame] | 135 | } else if (ParseCommandLineOptionWithArg("-j", argv, &i, &num_jobs_str)) { |
Shinichiro Hamaji | 003d06e | 2015-09-09 18:22:04 +0900 | [diff] [blame] | 136 | num_jobs = strtol(num_jobs_str, NULL, 10); |
| 137 | if (num_jobs <= 0) { |
| 138 | ERROR("Invalid -j flag: %s", num_jobs_str); |
| 139 | } |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame] | 140 | } else if (ParseCommandLineOptionWithArg("--remote_num_jobs", argv, &i, |
| 141 | &num_jobs_str)) { |
Shinichiro Hamaji | 003d06e | 2015-09-09 18:22:04 +0900 | [diff] [blame] | 142 | remote_num_jobs = strtol(num_jobs_str, NULL, 10); |
| 143 | if (remote_num_jobs <= 0) { |
| 144 | ERROR("Invalid -j flag: %s", num_jobs_str); |
| 145 | } |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame] | 146 | } else if (ParseCommandLineOptionWithArg("--ninja_suffix", argv, &i, |
| 147 | &ninja_suffix)) { |
| 148 | } else if (ParseCommandLineOptionWithArg("--ninja_dir", argv, &i, |
| 149 | &ninja_dir)) { |
Shinichiro Hamaji | 003d06e | 2015-09-09 18:22:04 +0900 | [diff] [blame] | 150 | } else if (!strcmp(arg, "--use_find_emulator")) { |
| 151 | use_find_emulator = true; |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame] | 152 | } else if (ParseCommandLineOptionWithArg("--goma_dir", argv, &i, |
| 153 | &goma_dir)) { |
Shinichiro Hamaji | 003d06e | 2015-09-09 18:22:04 +0900 | [diff] [blame] | 154 | } else if (ParseCommandLineOptionWithArg( |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame] | 155 | "--ignore_optional_include", argv, &i, |
| 156 | &ignore_optional_include_pattern)) { |
| 157 | } else if (ParseCommandLineOptionWithArg("--ignore_dirty", argv, &i, |
| 158 | &ignore_dirty_pattern)) { |
| 159 | } else if (ParseCommandLineOptionWithArg("--no_ignore_dirty", argv, &i, |
| 160 | &no_ignore_dirty_pattern)) { |
Dan Willemsen | a42898a | 2018-06-09 12:09:27 -0700 | [diff] [blame] | 161 | } else if (ParseCommandLineOptionWithArg("--writable", argv, &i, |
| 162 | &writable_str)) { |
| 163 | writable.push_back(writable_str); |
Shinichiro Hamaji | 003d06e | 2015-09-09 18:22:04 +0900 | [diff] [blame] | 164 | } else if (arg[0] == '-') { |
| 165 | ERROR("Unknown flag: %s", arg); |
| 166 | } else { |
| 167 | if (strchr(arg, '=')) { |
| 168 | cl_vars.push_back(arg); |
| 169 | } else { |
| 170 | should_propagate = false; |
| 171 | targets.push_back(Intern(arg)); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | if (should_propagate) { |
| 176 | for (; pi <= i; pi++) { |
| 177 | subkati_args.push_back(argv[pi]); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |