blob: 06772f0a01f4ec4401ef3c7929974c4c800d2253 [file] [log] [blame]
Shinichiro Hamaji08808d32015-06-26 08:02:45 +09001// 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 Hamaji003d06e2015-09-09 18:22:04 +090019#include <unistd.h>
20
21#include "log.h"
22#include "strutil.h"
23
24Flags g_flags;
25
26static 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
50void Flags::Parse(int argc, char** argv) {
51 subkati_args.push_back(argv[0]);
52 num_jobs = 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;
Shinichiro Hamaji644d6b92015-11-17 14:47:56 +090070 } else if (!strcmp(arg, "--warn")) {
71 enable_kati_warnings = true;
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +090072 } else if (!strcmp(arg, "--ninja")) {
73 generate_ninja = true;
Shinichiro Hamaji7223e7b2015-09-28 15:17:27 +090074 } else if (!strcmp(arg, "--gen_all_targets")) {
75 gen_all_targets = true;
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +090076 } 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 (ParseCommandLineOptionWithArg(
86 "-j", argv, &i, &num_jobs_str)) {
87 num_jobs = strtol(num_jobs_str, NULL, 10);
88 if (num_jobs <= 0) {
89 ERROR("Invalid -j flag: %s", num_jobs_str);
90 }
91 } else if (ParseCommandLineOptionWithArg(
92 "--remote_num_jobs", argv, &i, &num_jobs_str)) {
93 remote_num_jobs = strtol(num_jobs_str, NULL, 10);
94 if (remote_num_jobs <= 0) {
95 ERROR("Invalid -j flag: %s", num_jobs_str);
96 }
97 } else if (ParseCommandLineOptionWithArg(
98 "--ninja_suffix", argv, &i, &ninja_suffix)) {
99 } else if (ParseCommandLineOptionWithArg(
100 "--ninja_dir", argv, &i, &ninja_dir)) {
101 } else if (!strcmp(arg, "--use_find_emulator")) {
102 use_find_emulator = true;
103 } else if (!strcmp(arg, "--gen_regen_rule")) {
104 // TODO: Make this default once we have removed unnecessary
105 // command line change from Android build.
106 gen_regen_rule = true;
107 } else if (ParseCommandLineOptionWithArg(
108 "--goma_dir", argv, &i, &goma_dir)) {
109 } else if (ParseCommandLineOptionWithArg(
110 "--ignore_optional_include",
111 argv, &i, &ignore_optional_include_pattern)) {
112 } else if (ParseCommandLineOptionWithArg(
113 "--ignore_dirty",
114 argv, &i, &ignore_dirty_pattern)) {
Colin Crossf23ae8c2015-11-12 17:05:32 -0800115 } else if (ParseCommandLineOptionWithArg(
116 "--no_ignore_dirty",
117 argv, &i, &no_ignore_dirty_pattern)) {
Shinichiro Hamaji003d06e2015-09-09 18:22:04 +0900118 } else if (arg[0] == '-') {
119 ERROR("Unknown flag: %s", arg);
120 } else {
121 if (strchr(arg, '=')) {
122 cl_vars.push_back(arg);
123 } else {
124 should_propagate = false;
125 targets.push_back(Intern(arg));
126 }
127 }
128
129 if (should_propagate) {
130 for (; pi <= i; pi++) {
131 subkati_args.push_back(argv[pi]);
132 }
133 }
134 }
135}