summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPeter Collingbourne <peter@pcc.me.uk>2011-09-22 16:04:03 (GMT)
committerPeter Collingbourne <peter@pcc.me.uk>2011-10-15 19:23:50 (GMT)
commit7cf2bdffe2a95ea5e30e9c3166ef4398add8e6b9 (patch)
tree2b8cd622a0fa858beb0b91da121255a82274272b /src
parent07c1f9b14b5f7071a5def7d09414ec852a4372ef (diff)
downloadNinja-7cf2bdffe2a95ea5e30e9c3166ef4398add8e6b9.zip
Ninja-7cf2bdffe2a95ea5e30e9c3166ef4398add8e6b9.tar.gz
Ninja-7cf2bdffe2a95ea5e30e9c3166ef4398add8e6b9.tar.bz2
Implement generator rules
Introduce a rule attribute "generator" which, if present, specifies that this rule is used to re-invoke the generator program. Files built using generator rules are treated specially in two ways: firstly, they will not be rebuilt if the command line changes; and secondly, they are not cleaned by default. A command line flag "-g" is introduced for the clean tool, which causes it to remove generator files. Fixes issue #102.
Diffstat (limited to 'src')
-rw-r--r--src/clean.cc5
-rw-r--r--src/clean.h5
-rw-r--r--src/clean_test.cc22
-rw-r--r--src/graph.cc4
-rw-r--r--src/graph.h3
-rw-r--r--src/ninja.cc23
-rw-r--r--src/parsers.cc6
7 files changed, 61 insertions, 7 deletions
diff --git a/src/clean.cc b/src/clean.cc
index d22ac59..033fa96 100644
--- a/src/clean.cc
+++ b/src/clean.cc
@@ -96,7 +96,7 @@ void Cleaner::PrintFooter() {
printf("%d files.\n", cleaned_files_count_);
}
-int Cleaner::CleanAll() {
+int Cleaner::CleanAll(bool generator) {
Reset();
PrintHeader();
for (vector<Edge*>::iterator e = state_->edges_.begin();
@@ -104,6 +104,9 @@ int Cleaner::CleanAll() {
// Do not try to remove phony targets
if ((*e)->rule_ == &State::kPhonyRule)
continue;
+ // Do not remove generator's files unless generator specified.
+ if (!generator && (*e)->rule_->generator_)
+ continue;
for (vector<Node*>::iterator out_node = (*e)->outputs_.begin();
out_node != (*e)->outputs_.end(); ++out_node) {
Remove((*out_node)->file_->path_);
diff --git a/src/clean.h b/src/clean.h
index ab606f4..4d9b4e6 100644
--- a/src/clean.h
+++ b/src/clean.h
@@ -49,9 +49,10 @@ class Cleaner {
/// @return non-zero if an error occurs.
int CleanTargets(int target_count, char* targets[]);
- /// Clean all built files.
+ /// Clean all built files, except for files created by generator rules.
+ /// @param generator If set, also clean files created by generator rules.
/// @return non-zero if an error occurs.
- int CleanAll();
+ int CleanAll(bool generator = false);
/// Clean all the file built with the given rule @a rule.
/// @return non-zero if an error occurs.
diff --git a/src/clean_test.cc b/src/clean_test.cc
index f031dee..8606239 100644
--- a/src/clean_test.cc
+++ b/src/clean_test.cc
@@ -212,6 +212,28 @@ TEST_F(CleanTest, CleanRuleDryRun) {
EXPECT_EQ(0u, fs_.files_removed_.size());
}
+TEST_F(CleanTest, CleanRuleGenerator) {
+ ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
+"rule regen\n"
+" command = cat $in > $out\n"
+" generator = 1\n"
+"build out1: cat in1\n"
+"build out2: regen in2\n"));
+ fs_.Create("out1", 1, "");
+ fs_.Create("out2", 1, "");
+
+ Cleaner cleaner(&state_, config_, &fs_);
+ EXPECT_EQ(0, cleaner.CleanAll());
+ EXPECT_EQ(1, cleaner.cleaned_files_count());
+ EXPECT_EQ(1u, fs_.files_removed_.size());
+
+ fs_.Create("out1", 1, "");
+
+ EXPECT_EQ(0, cleaner.CleanAll(/*generator=*/true));
+ EXPECT_EQ(2, cleaner.cleaned_files_count());
+ EXPECT_EQ(2u, fs_.files_removed_.size());
+}
+
TEST_F(CleanTest, CleanFailure) {
ASSERT_NO_FATAL_FAILURE(AssertParse(&state_,
"build dir: cat src1\n"));
diff --git a/src/graph.cc b/src/graph.cc
index 0f16519..e1441ea 100644
--- a/src/graph.cc
+++ b/src/graph.cc
@@ -93,8 +93,10 @@ bool Edge::RecomputeDirty(State* state, DiskInterface* disk_interface,
(*i)->dirty_ = true;
} else {
// May also be dirty due to the command changing since the last build.
+ // But if this is a generator rule, the command changing does not make us
+ // dirty.
BuildLog::LogEntry* entry;
- if (state->build_log_ &&
+ if (!rule_->generator_ && state->build_log_ &&
(entry = state->build_log_->LookupByOutput((*i)->file_->path_))) {
if (command != entry->command)
(*i)->dirty_ = true;
diff --git a/src/graph.h b/src/graph.h
index bf714e3..5b6bdf2 100644
--- a/src/graph.h
+++ b/src/graph.h
@@ -74,7 +74,7 @@ struct Node {
/// An invokable build command and associated metadata (description, etc.).
struct Rule {
- Rule(const string& name) : name_(name) { }
+ Rule(const string& name) : name_(name), generator_(false) { }
bool ParseCommand(const string& command, string* err) {
return command_.Parse(command, err);
@@ -83,6 +83,7 @@ struct Rule {
EvalString command_;
EvalString description_;
EvalString depfile_;
+ bool generator_;
};
struct State;
diff --git a/src/ninja.cc b/src/ninja.cc
index 5e8d2e4..5b96ca1 100644
--- a/src/ninja.cc
+++ b/src/ninja.cc
@@ -358,6 +358,23 @@ int CmdRules(State* state, int argc, char* argv[]) {
}
int CmdClean(State* state, int argc, char* argv[], const BuildConfig& config) {
+ bool generator = false;
+
+ optind = 1;
+ int opt;
+ while ((opt = getopt(argc, argv, "g")) != -1) {
+ switch (opt) {
+ case 'g':
+ generator = true;
+ break;
+ default:
+ Usage(config);
+ return 1;
+ }
+ }
+ argv += optind;
+ argc -= optind;
+
Cleaner cleaner(state, config);
if (argc >= 1)
{
@@ -381,7 +398,7 @@ int CmdClean(State* state, int argc, char* argv[], const BuildConfig& config) {
}
}
else {
- return cleaner.CleanAll();
+ return cleaner.CleanAll(generator);
}
}
@@ -479,8 +496,10 @@ reload:
return CmdTargets(&state, argc, argv);
if (tool == "rules")
return CmdRules(&state, argc, argv);
+ // The clean tool uses getopt, and expects argv[0] to contain the name of
+ // the tool, i.e. "clean".
if (tool == "clean")
- return CmdClean(&state, argc, argv, config);
+ return CmdClean(&state, argc+1, argv-1, config);
Error("unknown tool '%s'", tool.c_str());
}
diff --git a/src/parsers.cc b/src/parsers.cc
index 9ed2938..c086109 100644
--- a/src/parsers.cc
+++ b/src/parsers.cc
@@ -383,6 +383,12 @@ bool ManifestParser::ParseRule(string* err) {
eval_target = &rule->depfile_;
} else if (key == "description") {
eval_target = &rule->description_;
+ } else if (key == "generator") {
+ rule->generator_ = true;
+ string dummy;
+ if (!tokenizer_.ReadToNewline(&dummy, err))
+ return false;
+ continue;
} else {
// Die on other keyvals for now; revisit if we want to add a
// scope here.