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 19 #include <unistd.h> 20 21 #include "log.h" 22 #include "strutil.h" 23 24 Flags g_flags; 25 26 static bool ParseCommandLineOptionWithArg(StringPiece option, 27 char* argv[], 28 int* index, 29 const char** out_arg) { 30 const char* arg = argv[*index]; 31 if (!HasPrefix(arg, option)) 32 return false; 33 if (arg[option.size()] == '\0') { 34 ++*index; 35 *out_arg = argv[*index]; 36 return true; 37 } 38 if (arg[option.size()] == '=') { 39 *out_arg = arg + option.size() + 1; 40 return true; 41 } 42 // E.g, -j999 43 if (option.size() == 2) { 44 *out_arg = arg + option.size(); 45 return true; 46 } 47 return false; 48 } 49 50 void Flags::Parse(int argc, char** argv) { 51 subkati_args.push_back(argv[0]); 52 num_jobs = num_cpus = sysconf(_SC_NPROCESSORS_ONLN); 53 const char* num_jobs_str; 54 55 for (int i = 1; i < argc; i++) { 56 const char* arg = argv[i]; 57 bool should_propagate = true; 58 int pi = i; 59 if (!strcmp(arg, "-f")) { 60 makefile = argv[++i]; 61 should_propagate = false; 62 } else if (!strcmp(arg, "-c")) { 63 is_syntax_check_only = true; 64 } else if (!strcmp(arg, "-i")) { 65 is_dry_run = true; 66 } else if (!strcmp(arg, "-s")) { 67 is_silent_mode = true; 68 } else if (!strcmp(arg, "--kati_stats")) { 69 enable_stat_logs = true; 70 } else if (!strcmp(arg, "--warn")) { 71 enable_kati_warnings = true; 72 } else if (!strcmp(arg, "--ninja")) { 73 generate_ninja = true; 74 } else if (!strcmp(arg, "--gen_all_targets")) { 75 gen_all_targets = true; 76 } else if (!strcmp(arg, "--regen")) { 77 // TODO: Make this default. 78 regen = true; 79 } else if (!strcmp(arg, "--regen_ignoring_kati_binary")) { 80 regen_ignoring_kati_binary = true; 81 } else if (!strcmp(arg, "--dump_kati_stamp")) { 82 dump_kati_stamp = true; 83 } else if (!strcmp(arg, "--detect_android_echo")) { 84 detect_android_echo = true; 85 } else if (!strcmp(arg, "--detect_depfiles")) { 86 detect_depfiles = true; 87 } else if (ParseCommandLineOptionWithArg( 88 "-j", argv, &i, &num_jobs_str)) { 89 num_jobs = strtol(num_jobs_str, NULL, 10); 90 if (num_jobs <= 0) { 91 ERROR("Invalid -j flag: %s", num_jobs_str); 92 } 93 } else if (ParseCommandLineOptionWithArg( 94 "--remote_num_jobs", argv, &i, &num_jobs_str)) { 95 remote_num_jobs = strtol(num_jobs_str, NULL, 10); 96 if (remote_num_jobs <= 0) { 97 ERROR("Invalid -j flag: %s", num_jobs_str); 98 } 99 } else if (ParseCommandLineOptionWithArg( 100 "--ninja_suffix", argv, &i, &ninja_suffix)) { 101 } else if (ParseCommandLineOptionWithArg( 102 "--ninja_dir", argv, &i, &ninja_dir)) { 103 } else if (!strcmp(arg, "--use_find_emulator")) { 104 use_find_emulator = true; 105 } else if (ParseCommandLineOptionWithArg( 106 "--goma_dir", argv, &i, &goma_dir)) { 107 } else if (ParseCommandLineOptionWithArg( 108 "--ignore_optional_include", 109 argv, &i, &ignore_optional_include_pattern)) { 110 } else if (ParseCommandLineOptionWithArg( 111 "--ignore_dirty", 112 argv, &i, &ignore_dirty_pattern)) { 113 } else if (ParseCommandLineOptionWithArg( 114 "--no_ignore_dirty", 115 argv, &i, &no_ignore_dirty_pattern)) { 116 } else if (arg[0] == '-') { 117 ERROR("Unknown flag: %s", arg); 118 } else { 119 if (strchr(arg, '=')) { 120 cl_vars.push_back(arg); 121 } else { 122 should_propagate = false; 123 targets.push_back(Intern(arg)); 124 } 125 } 126 127 if (should_propagate) { 128 for (; pi <= i; pi++) { 129 subkati_args.push_back(argv[pi]); 130 } 131 } 132 } 133 } 134