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; |
| 55 | |
Shinichiro Hamaji | 4627ab2 | 2016-06-25 11:11:44 +0900 | [diff] [blame] | 56 | if (const char* makeflags = getenv("MAKEFLAGS")) { |
| 57 | for (StringPiece tok : WordScanner(makeflags)) { |
| 58 | if (!HasPrefix(tok, "-") && tok.find('=') != string::npos) |
| 59 | cl_vars.push_back(tok); |
Po Hu | d570f21 | 2016-06-25 09:08:54 +0800 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Shinichiro Hamaji | 003d06e | 2015-09-09 18:22:04 +0900 | [diff] [blame] | 63 | for (int i = 1; i < argc; i++) { |
| 64 | const char* arg = argv[i]; |
| 65 | bool should_propagate = true; |
| 66 | int pi = i; |
| 67 | if (!strcmp(arg, "-f")) { |
| 68 | makefile = argv[++i]; |
| 69 | should_propagate = false; |
| 70 | } else if (!strcmp(arg, "-c")) { |
| 71 | is_syntax_check_only = true; |
| 72 | } else if (!strcmp(arg, "-i")) { |
| 73 | is_dry_run = true; |
| 74 | } else if (!strcmp(arg, "-s")) { |
| 75 | is_silent_mode = true; |
Shinichiro Hamaji | c58db9a | 2016-05-12 15:16:12 +0900 | [diff] [blame] | 76 | } else if (!strcmp(arg, "-d")) { |
| 77 | enable_debug = true; |
Shinichiro Hamaji | 003d06e | 2015-09-09 18:22:04 +0900 | [diff] [blame] | 78 | } else if (!strcmp(arg, "--kati_stats")) { |
| 79 | enable_stat_logs = true; |
Shinichiro Hamaji | 644d6b9 | 2015-11-17 14:47:56 +0900 | [diff] [blame] | 80 | } else if (!strcmp(arg, "--warn")) { |
| 81 | enable_kati_warnings = true; |
Shinichiro Hamaji | 003d06e | 2015-09-09 18:22:04 +0900 | [diff] [blame] | 82 | } else if (!strcmp(arg, "--ninja")) { |
| 83 | generate_ninja = true; |
Shinichiro Hamaji | 7223e7b | 2015-09-28 15:17:27 +0900 | [diff] [blame] | 84 | } else if (!strcmp(arg, "--gen_all_targets")) { |
| 85 | gen_all_targets = true; |
Shinichiro Hamaji | 003d06e | 2015-09-09 18:22:04 +0900 | [diff] [blame] | 86 | } else if (!strcmp(arg, "--regen")) { |
| 87 | // TODO: Make this default. |
| 88 | regen = true; |
Dan Willemsen | f6486ce | 2016-09-16 19:21:36 -0700 | [diff] [blame] | 89 | } else if (!strcmp(arg, "--regen_debug")) { |
| 90 | regen_debug = true; |
Shinichiro Hamaji | 003d06e | 2015-09-09 18:22:04 +0900 | [diff] [blame] | 91 | } else if (!strcmp(arg, "--regen_ignoring_kati_binary")) { |
| 92 | regen_ignoring_kati_binary = true; |
| 93 | } else if (!strcmp(arg, "--dump_kati_stamp")) { |
| 94 | dump_kati_stamp = true; |
Dan Willemsen | f6486ce | 2016-09-16 19:21:36 -0700 | [diff] [blame] | 95 | regen_debug = true; |
Shinichiro Hamaji | 003d06e | 2015-09-09 18:22:04 +0900 | [diff] [blame] | 96 | } else if (!strcmp(arg, "--detect_android_echo")) { |
| 97 | detect_android_echo = true; |
Shinichiro Hamaji | f3ad9e0 | 2016-03-03 18:26:17 +0900 | [diff] [blame] | 98 | } else if (!strcmp(arg, "--detect_depfiles")) { |
| 99 | detect_depfiles = true; |
Shinichiro Hamaji | 003d06e | 2015-09-09 18:22:04 +0900 | [diff] [blame] | 100 | } else if (ParseCommandLineOptionWithArg( |
| 101 | "-j", argv, &i, &num_jobs_str)) { |
| 102 | num_jobs = strtol(num_jobs_str, NULL, 10); |
| 103 | if (num_jobs <= 0) { |
| 104 | ERROR("Invalid -j flag: %s", num_jobs_str); |
| 105 | } |
| 106 | } else if (ParseCommandLineOptionWithArg( |
| 107 | "--remote_num_jobs", argv, &i, &num_jobs_str)) { |
| 108 | remote_num_jobs = strtol(num_jobs_str, NULL, 10); |
| 109 | if (remote_num_jobs <= 0) { |
| 110 | ERROR("Invalid -j flag: %s", num_jobs_str); |
| 111 | } |
| 112 | } else if (ParseCommandLineOptionWithArg( |
| 113 | "--ninja_suffix", argv, &i, &ninja_suffix)) { |
| 114 | } else if (ParseCommandLineOptionWithArg( |
| 115 | "--ninja_dir", argv, &i, &ninja_dir)) { |
| 116 | } else if (!strcmp(arg, "--use_find_emulator")) { |
| 117 | use_find_emulator = true; |
Shinichiro Hamaji | 003d06e | 2015-09-09 18:22:04 +0900 | [diff] [blame] | 118 | } else if (ParseCommandLineOptionWithArg( |
| 119 | "--goma_dir", argv, &i, &goma_dir)) { |
| 120 | } else if (ParseCommandLineOptionWithArg( |
| 121 | "--ignore_optional_include", |
| 122 | argv, &i, &ignore_optional_include_pattern)) { |
| 123 | } else if (ParseCommandLineOptionWithArg( |
| 124 | "--ignore_dirty", |
| 125 | argv, &i, &ignore_dirty_pattern)) { |
Colin Cross | f23ae8c | 2015-11-12 17:05:32 -0800 | [diff] [blame] | 126 | } else if (ParseCommandLineOptionWithArg( |
| 127 | "--no_ignore_dirty", |
| 128 | argv, &i, &no_ignore_dirty_pattern)) { |
Shinichiro Hamaji | 003d06e | 2015-09-09 18:22:04 +0900 | [diff] [blame] | 129 | } else if (arg[0] == '-') { |
| 130 | ERROR("Unknown flag: %s", arg); |
| 131 | } else { |
| 132 | if (strchr(arg, '=')) { |
| 133 | cl_vars.push_back(arg); |
| 134 | } else { |
| 135 | should_propagate = false; |
| 136 | targets.push_back(Intern(arg)); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if (should_propagate) { |
| 141 | for (; pi <= i; pi++) { |
| 142 | subkati_args.push_back(argv[pi]); |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } |