diff options
-rw-r--r-- | command.cc | 2 | ||||
-rw-r--r-- | dep.cc | 2 | ||||
-rw-r--r-- | eval.cc | 4 | ||||
-rw-r--r-- | exec.cc | 2 | ||||
-rw-r--r-- | flags.cc | 124 | ||||
-rw-r--r-- | flags.h | 48 | ||||
-rw-r--r-- | func.cc | 4 | ||||
-rw-r--r-- | log.h | 2 | ||||
-rw-r--r-- | main.cc | 165 | ||||
-rw-r--r-- | ninja.cc | 24 |
10 files changed, 192 insertions, 185 deletions
@@ -182,7 +182,7 @@ void CommandEvaluator::Eval(DepNode* n, vector<Command*>* commands) { for (Value* v : n->cmds) { const string&& cmds_buf = v->Eval(ev_); StringPiece cmds = cmds_buf; - bool global_echo = !g_is_silent_mode; + bool global_echo = !g_flags.is_silent_mode; bool global_ignore_error = false; ParseCommandPrefixes(&cmds, &global_echo, &global_ignore_error); if (cmds == "") @@ -144,7 +144,7 @@ class DepBuilder { targets.push_back(first_rule_->outputs[0]); - if (g_gen_all_phony_targets) { + if (g_flags.gen_all_phony_targets) { for (Symbol s : phony_) targets.push_back(s); } @@ -249,8 +249,8 @@ void Evaluator::EvalInclude(const IncludeAST* ast) { } for (const string& fname : *files) { - if (!ast->should_exist && g_ignore_optional_include_pattern && - Pattern(g_ignore_optional_include_pattern).Match(fname)) { + if (!ast->should_exist && g_flags.ignore_optional_include_pattern && + Pattern(g_flags.ignore_optional_include_pattern).Match(fname)) { return; } DoInclude(fname); @@ -103,7 +103,7 @@ class Executor { printf("%s\n", command->cmd.c_str()); fflush(stdout); } - if (!g_is_dry_run) { + if (!g_flags.is_dry_run) { string out; int result = RunCommand(shell_, command->cmd.c_str(), RedirectStderr::STDOUT, @@ -16,14 +16,116 @@ #include "flags.h" -bool g_enable_stat_logs; -bool g_is_dry_run; -const char* g_ignore_optional_include_pattern; -const char* g_goma_dir; -int g_num_jobs; -int g_remote_num_jobs; -bool g_detect_android_echo; -bool g_gen_regen_rule; -const char* g_ignore_dirty_pattern; -bool g_gen_all_phony_targets; -bool g_is_silent_mode; +#include <unistd.h> + +#include "log.h" +#include "strutil.h" + +Flags g_flags; + +static bool ParseCommandLineOptionWithArg(StringPiece option, + char* argv[], + int* index, + const char** out_arg) { + const char* arg = argv[*index]; + if (!HasPrefix(arg, option)) + return false; + if (arg[option.size()] == '\0') { + ++*index; + *out_arg = argv[*index]; + return true; + } + if (arg[option.size()] == '=') { + *out_arg = arg + option.size() + 1; + return true; + } + // E.g, -j999 + if (option.size() == 2) { + *out_arg = arg + option.size(); + return true; + } + return false; +} + +void Flags::Parse(int argc, char** argv) { + subkati_args.push_back(argv[0]); + num_jobs = sysconf(_SC_NPROCESSORS_ONLN); + const char* num_jobs_str; + + for (int i = 1; i < argc; i++) { + const char* arg = argv[i]; + bool should_propagate = true; + int pi = i; + if (!strcmp(arg, "-f")) { + makefile = argv[++i]; + should_propagate = false; + } else if (!strcmp(arg, "-c")) { + is_syntax_check_only = true; + } else if (!strcmp(arg, "-i")) { + is_dry_run = true; + } else if (!strcmp(arg, "-s")) { + is_silent_mode = true; + } else if (!strcmp(arg, "--kati_stats")) { + enable_stat_logs = true; + } else if (!strcmp(arg, "--ninja")) { + generate_ninja = true; + } else if (!strcmp(arg, "--gen_all_phony_targets")) { + // TODO: Remove this. + gen_all_phony_targets = true; + } else if (!strcmp(arg, "--regen")) { + // TODO: Make this default. + regen = true; + } else if (!strcmp(arg, "--regen_ignoring_kati_binary")) { + regen_ignoring_kati_binary = true; + } else if (!strcmp(arg, "--dump_kati_stamp")) { + dump_kati_stamp = true; + } else if (!strcmp(arg, "--detect_android_echo")) { + detect_android_echo = true; + } else if (ParseCommandLineOptionWithArg( + "-j", argv, &i, &num_jobs_str)) { + num_jobs = strtol(num_jobs_str, NULL, 10); + if (num_jobs <= 0) { + ERROR("Invalid -j flag: %s", num_jobs_str); + } + } else if (ParseCommandLineOptionWithArg( + "--remote_num_jobs", argv, &i, &num_jobs_str)) { + remote_num_jobs = strtol(num_jobs_str, NULL, 10); + if (remote_num_jobs <= 0) { + ERROR("Invalid -j flag: %s", num_jobs_str); + } + } else if (ParseCommandLineOptionWithArg( + "--ninja_suffix", argv, &i, &ninja_suffix)) { + } else if (ParseCommandLineOptionWithArg( + "--ninja_dir", argv, &i, &ninja_dir)) { + } else if (!strcmp(arg, "--use_find_emulator")) { + use_find_emulator = true; + } else if (!strcmp(arg, "--gen_regen_rule")) { + // TODO: Make this default once we have removed unnecessary + // command line change from Android build. + gen_regen_rule = true; + } else if (ParseCommandLineOptionWithArg( + "--goma_dir", argv, &i, &goma_dir)) { + } else if (ParseCommandLineOptionWithArg( + "--ignore_optional_include", + argv, &i, &ignore_optional_include_pattern)) { + } else if (ParseCommandLineOptionWithArg( + "--ignore_dirty", + argv, &i, &ignore_dirty_pattern)) { + } else if (arg[0] == '-') { + ERROR("Unknown flag: %s", arg); + } else { + if (strchr(arg, '=')) { + cl_vars.push_back(arg); + } else { + should_propagate = false; + targets.push_back(Intern(arg)); + } + } + + if (should_propagate) { + for (; pi <= i; pi++) { + subkati_args.push_back(argv[pi]); + } + } + } +} @@ -15,16 +15,42 @@ #ifndef FLAGS_H_ #define FLAGS_H_ -extern bool g_is_dry_run; -extern bool g_enable_stat_logs; -extern const char* g_ignore_optional_include_pattern; -extern const char* g_goma_dir; -extern int g_num_jobs; -extern int g_remote_num_jobs; -extern bool g_detect_android_echo; -extern bool g_gen_regen_rule; -extern const char* g_ignore_dirty_pattern; -extern bool g_gen_all_phony_targets; -extern bool g_is_silent_mode; +#include <string> +#include <vector> + +#include "string_piece.h" +#include "symtab.h" + +using namespace std; + +struct Flags { + bool detect_android_echo; + bool dump_kati_stamp; + bool enable_stat_logs; + bool gen_all_phony_targets; + bool gen_regen_rule; + bool generate_ninja; + bool is_dry_run; + bool is_silent_mode; + bool is_syntax_check_only; + bool regen; + bool regen_ignoring_kati_binary; + bool use_find_emulator; + const char* goma_dir; + const char* ignore_dirty_pattern; + const char* ignore_optional_include_pattern; + const char* makefile; + const char* ninja_dir; + const char* ninja_suffix; + int num_jobs; + int remote_num_jobs; + vector<const char*> subkati_args; + vector<Symbol> targets; + vector<StringPiece> cl_vars; + + void Parse(int argc, char** argv); +}; + +extern Flags g_flags; #endif // FLAGS_H_ @@ -519,8 +519,8 @@ static vector<CommandResult*> g_command_results; bool ShouldStoreCommandResult(StringPiece cmd) { if (HasWord(cmd, "date") || HasWord(cmd, "echo")) return false; - if (g_ignore_dirty_pattern) { - Pattern pat(g_ignore_dirty_pattern); + if (g_flags.ignore_dirty_pattern) { + Pattern pat(g_flags.ignore_dirty_pattern); for (StringPiece tok : WordScanner(cmd)) { if (pat.Match(tok)) return false; @@ -40,7 +40,7 @@ extern string* g_last_error; #endif #define LOG_STAT(args...) do { \ - if (g_enable_stat_logs) \ + if (g_flags.enable_stat_logs) \ fprintf(stderr, "*kati*: %s\n", StringPrintf(args).c_str()); \ } while(0) @@ -42,141 +42,21 @@ #include "timeutil.h" #include "var.h" -static const char* g_makefile; -static bool g_is_syntax_check_only; -static bool g_generate_ninja; -static bool g_regen; -static bool g_regen_ignoring_kati_binary; -static bool g_dump_kati_stamp; -static const char* g_ninja_suffix; -static const char* g_ninja_dir; -static bool g_use_find_emulator; -static vector<const char*> g_subkati_args; - -static bool ParseCommandLineOptionWithArg(StringPiece option, - char* argv[], - int* index, - const char** out_arg) { - const char* arg = argv[*index]; - if (!HasPrefix(arg, option)) - return false; - if (arg[option.size()] == '\0') { - ++*index; - *out_arg = argv[*index]; - return true; - } - if (arg[option.size()] == '=') { - *out_arg = arg + option.size() + 1; - return true; - } - // E.g, -j999 - if (option.size() == 2) { - *out_arg = arg + option.size(); - return true; - } - return false; -} - -static void ParseCommandLine(int argc, char* argv[], - vector<Symbol>* targets, - vector<StringPiece>* cl_vars) { - g_subkati_args.push_back(argv[0]); - g_num_jobs = sysconf(_SC_NPROCESSORS_ONLN); - const char* num_jobs_str; - - for (int i = 1; i < argc; i++) { - const char* arg = argv[i]; - bool should_propagate = true; - int pi = i; - if (!strcmp(arg, "-f")) { - g_makefile = argv[++i]; - should_propagate = false; - } else if (!strcmp(arg, "-c")) { - g_is_syntax_check_only = true; - } else if (!strcmp(arg, "-i")) { - g_is_dry_run = true; - } else if (!strcmp(arg, "-s")) { - g_is_silent_mode = true; - } else if (!strcmp(arg, "--kati_stats")) { - g_enable_stat_logs = true; - } else if (!strcmp(arg, "--ninja")) { - g_generate_ninja = true; - } else if (!strcmp(arg, "--gen_all_phony_targets")) { - // TODO: Remove this. - g_gen_all_phony_targets = true; - } else if (!strcmp(arg, "--regen")) { - // TODO: Make this default. - g_regen = true; - } else if (!strcmp(arg, "--regen_ignoring_kati_binary")) { - g_regen_ignoring_kati_binary = true; - } else if (!strcmp(arg, "--dump_kati_stamp")) { - g_dump_kati_stamp = true; - } else if (!strcmp(arg, "--detect_android_echo")) { - g_detect_android_echo = true; - } else if (ParseCommandLineOptionWithArg( - "-j", argv, &i, &num_jobs_str)) { - g_num_jobs = strtol(num_jobs_str, NULL, 10); - if (g_num_jobs <= 0) { - ERROR("Invalid -j flag: %s", num_jobs_str); - } - } else if (ParseCommandLineOptionWithArg( - "--remote_num_jobs", argv, &i, &num_jobs_str)) { - g_remote_num_jobs = strtol(num_jobs_str, NULL, 10); - if (g_remote_num_jobs <= 0) { - ERROR("Invalid -j flag: %s", num_jobs_str); - } - } else if (ParseCommandLineOptionWithArg( - "--ninja_suffix", argv, &i, &g_ninja_suffix)) { - } else if (ParseCommandLineOptionWithArg( - "--ninja_dir", argv, &i, &g_ninja_dir)) { - } else if (!strcmp(arg, "--use_find_emulator")) { - g_use_find_emulator = true; - } else if (!strcmp(arg, "--gen_regen_rule")) { - // TODO: Make this default once we have removed unnecessary - // command line change from Android build. - g_gen_regen_rule = true; - } else if (ParseCommandLineOptionWithArg( - "--goma_dir", argv, &i, &g_goma_dir)) { - } else if (ParseCommandLineOptionWithArg( - "--ignore_optional_include", - argv, &i, &g_ignore_optional_include_pattern)) { - } else if (ParseCommandLineOptionWithArg( - "--ignore_dirty", - argv, &i, &g_ignore_dirty_pattern)) { - } else if (arg[0] == '-') { - ERROR("Unknown flag: %s", arg); - } else { - if (strchr(arg, '=')) { - cl_vars->push_back(arg); - } else { - should_propagate = false; - targets->push_back(Intern(arg)); - } - } - - if (should_propagate) { - for (; pi <= i; pi++) { - g_subkati_args.push_back(argv[pi]); - } - } - } -} - static void Init() { InitSymtab(); InitFuncTable(); InitDepNodePool(); InitParser(); - if (g_makefile == NULL) { + if (g_flags.makefile == NULL) { if (Exists("GNUmakefile")) { - g_makefile = "GNUmakefile"; + g_flags.makefile = "GNUmakefile"; #if !defined(__APPLE__) } else if (Exists("makefile")) { - g_makefile = "makefile"; + g_flags.makefile = "makefile"; #endif } else if (Exists("Makefile")) { - g_makefile = "Makefile"; + g_flags.makefile = "Makefile"; } } } @@ -216,12 +96,12 @@ static void ReadBootstrapMakefile(const vector<Symbol>& targets, "\t$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -o $@ $<\n" // TODO: Add more builtin rules. ); - if (g_generate_ninja) { + if (g_flags.generate_ninja) { bootstrap += StringPrintf("MAKE?=make -j%d\n", - g_num_jobs < 1 ? 1 : g_num_jobs / 2); + g_flags.num_jobs < 1 ? 1 : g_flags.num_jobs / 2); } else { bootstrap += StringPrintf("MAKE?=%s\n", - JoinStrings(g_subkati_args, " ").c_str()); + JoinStrings(g_flags.subkati_args, " ").c_str()); } bootstrap += StringPrintf("MAKECMDGOALS?=%s\n", JoinSymbols(targets, " ").c_str()); @@ -251,16 +131,16 @@ static int Run(const vector<Symbol>& targets, const string& orig_args) { double start_time = GetTime(); - if (g_generate_ninja && (g_regen || g_dump_kati_stamp)) { + if (g_flags.generate_ninja && (g_flags.regen || g_flags.dump_kati_stamp)) { ScopedTimeReporter tr("regen check time"); - if (!NeedsRegen(g_ninja_suffix, g_ninja_dir, - g_regen_ignoring_kati_binary, - g_dump_kati_stamp, + if (!NeedsRegen(g_flags.ninja_suffix, g_flags.ninja_dir, + g_flags.regen_ignoring_kati_binary, + g_flags.dump_kati_stamp, start_time, orig_args)) { printf("No need to regenerate ninja file\n"); return 0; } - if (g_dump_kati_stamp) { + if (g_flags.dump_kati_stamp) { printf("Need to regenerate ninja file\n"); return 0; } @@ -289,12 +169,12 @@ static int Run(const vector<Symbol>& targets, } vars->Assign(Intern("MAKEFILE_LIST"), - new SimpleVar(StringPrintf(" %s", g_makefile), + new SimpleVar(StringPrintf(" %s", g_flags.makefile), VarOrigin::FILE)); { ScopedTimeReporter tr("eval time"); - Makefile* mk = cache_mgr->ReadMakefile(g_makefile); + Makefile* mk = cache_mgr->ReadMakefile(g_flags.makefile); for (AST* ast : mk->asts()) { LOG("%s", ast->DebugString().c_str()); ast->Eval(ev); @@ -312,12 +192,13 @@ static int Run(const vector<Symbol>& targets, MakeDep(ev, ev->rules(), ev->rule_vars(), targets, &nodes); } - if (g_is_syntax_check_only) + if (g_flags.is_syntax_check_only) return 0; - if (g_generate_ninja) { + if (g_flags.generate_ninja) { ScopedTimeReporter tr("generate ninja time"); - GenerateNinja(g_ninja_suffix, g_ninja_dir, nodes, ev, !targets.empty(), + GenerateNinja(g_flags.ninja_suffix, g_flags.ninja_dir, + nodes, ev, !targets.empty(), orig_args, start_time); return 0; } @@ -357,15 +238,13 @@ int main(int argc, char* argv[]) { orig_args += ' '; orig_args += argv[i]; } - vector<Symbol> targets; - vector<StringPiece> cl_vars; - ParseCommandLine(argc, argv, &targets, &cl_vars); - if (g_makefile == NULL) + g_flags.Parse(argc, argv); + if (g_flags.makefile == NULL) ERROR("*** No targets specified and no makefile found."); // This depends on command line flags. - if (g_use_find_emulator) + if (g_flags.use_find_emulator) InitFindEmulator(); - int r = Run(targets, cl_vars, orig_args); + int r = Run(g_flags.targets, g_flags.cl_vars, orig_args); Quit(); return r; } @@ -173,8 +173,8 @@ class NinjaGenerator { : ce_(ev), ev_(ev), fp_(NULL), rule_id_(0), start_time_(start_time) { ev_->set_avoid_io(true); shell_ = ev->EvalVar(kShellSym); - if (g_goma_dir) - gomacc_ = StringPrintf("%s/gomacc ", g_goma_dir); + if (g_flags.goma_dir) + gomacc_ = StringPrintf("%s/gomacc ", g_flags.goma_dir); if (ninja_suffix) { ninja_suffix_ = ninja_suffix; } @@ -364,7 +364,7 @@ class NinjaGenerator { size_t cmd_start = cmd_buf->size(); StringPiece translated = TranslateCommand(in, cmd_buf); - if (g_detect_android_echo && !got_descritpion && !c->echo && + if (g_flags.detect_android_echo && !got_descritpion && !c->echo && GetDescriptionFromCommand(translated, description)) { got_descritpion = true; cmd_buf->resize(cmd_start); @@ -372,7 +372,7 @@ class NinjaGenerator { } if (translated.empty()) { *cmd_buf += "true"; - } else if (g_goma_dir) { + } else if (g_flags.goma_dir) { size_t pos = GetGomaccPosForAndroidCompileCommand(translated); if (pos != string::npos) { cmd_buf->insert(cmd_start + pos, gomacc_); @@ -410,7 +410,7 @@ class NinjaGenerator { // A hack to exclude out phony target in Android. If this exists, // "ninja -t clean" tries to remove this directory and fails. - if (g_detect_android_echo && node->output.str() == "out") + if (g_flags.detect_android_echo && node->output.str() == "out") return; // This node is a leaf node @@ -528,7 +528,7 @@ class NinjaGenerator { } void EmitRegenRules(const string& orig_args) { - if (!g_gen_regen_rule) + if (!g_flags.gen_regen_rule) return; fprintf(fp_, "rule regen_ninja\n"); @@ -582,7 +582,7 @@ class NinjaGenerator { } fprintf(fp_, "pool local_pool\n"); - fprintf(fp_, " depth = %d\n\n", g_num_jobs); + fprintf(fp_, " depth = %d\n\n", g_flags.num_jobs); fprintf(fp_, "build _kati_always_build_: phony\n\n"); @@ -627,9 +627,9 @@ class NinjaGenerator { } fprintf(fp, "exec ninja -f %s ", GetNinjaFilename().c_str()); - if (g_remote_num_jobs > 0) { - fprintf(fp, "-j%d ", g_remote_num_jobs); - } else if (g_goma_dir) { + if (g_flags.remote_num_jobs > 0) { + fprintf(fp, "-j%d ", g_flags.remote_num_jobs); + } else if (g_flags.goma_dir) { fprintf(fp, "-j500 "); } fprintf(fp, "\"$@\"\n"); @@ -744,8 +744,8 @@ void GenerateNinja(const char* ninja_suffix, } static bool ShouldIgnoreDirty(StringPiece s) { - return (g_ignore_dirty_pattern && - Pattern(g_ignore_dirty_pattern).Match(s)); + return (g_flags.ignore_dirty_pattern && + Pattern(g_flags.ignore_dirty_pattern).Match(s)); } bool NeedsRegen(const char* ninja_suffix, |