Skip to content

Commit

Permalink
Fix include dependency tracking.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillof committed May 25, 2024
1 parent 68fecc5 commit fab3e5a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/File.cc
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,11 @@ void File::parse(const std::filesystem::path& filename) {

case Tokenizer::TokenType::INCLUDE: {
auto name = tokenizer.expect(Tokenizer::TokenType::WORD, Tokenizer::Skip::SPACE);
includes.insert(name.string());
tokenizers.emplace_back(name.string());
auto include_filename = Filename(Filename::Type::SOURCE, name.string());
auto result = ResolveResult();
include_filename.resolve(ResolveContext(*this, result));
includes.insert(include_filename);
tokenizers.emplace_back(include_filename.full_name().string());
break;
}

Expand Down Expand Up @@ -295,6 +298,7 @@ void File::parse_subninja(Tokenizer& tokenizer) {

void File::add_generator_build(std::vector<Filename>& ninja_outputs, std::vector<Filename>& ninja_inputs) const { // NOLINT(misc-no-recursion)
ninja_outputs.emplace_back(Filename::Type::BUILD, build_filename.string());
ninja_inputs.insert(ninja_inputs.end(), includes.begin(), includes.end());
ninja_inputs.emplace_back(Filename::Type::COMPLETE, source_filename.string());
for (const auto& file: subfiles) {
file->add_generator_build(ninja_outputs, ninja_inputs);
Expand Down
2 changes: 1 addition & 1 deletion src/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class File: public Scope {
std::filesystem::path build_filename;

std::unordered_set<std::string> outputs;
std::set<std::string> includes;
std::set<Filename> includes;
std::map<std::string, Rule> rules;
std::map<std::string, Pool> pools;
std::vector<Build> builds;
Expand Down
15 changes: 13 additions & 2 deletions src/Filename.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,19 @@ void Filename::resolve(const ResolveContext& context) {

switch (type) {
case Type::BUILD:
prefix = file->build_directory;
if (prefix.empty()) {
prefix = file->build_directory;
}
break;

case Type::COMPLETE:
prefix = "";
break;

case Type::SOURCE:
prefix = file->source_directory;
if (prefix.empty()) {
prefix = file->source_directory;
}
if (!std::filesystem::exists(full_name())) {
throw Exception("source file '" + full_name().string() + "' does not exist");
}
Expand All @@ -80,6 +84,13 @@ std::filesystem::path Filename::full_name() const {
}
}

bool Filename::operator<(const Filename& other) const {
if (type != other.type) {
return type < other.type;
}
return name < other.name;
}

std::ostream& operator<<(std::ostream& stream, const Filename& file_name) {
stream << file_name.full_name().generic_string();
return stream;
Expand Down
4 changes: 4 additions & 0 deletions src/Filename.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class Filename {

[[nodiscard]] std::filesystem::path full_name() const;

// TODO: consider prefix?
bool operator<(const Filename& other) const;
bool operator==(const Filename& other) const {return type == other.type && name == other.name;}

Type type{Type::UNKNOWN};
std::string name;
std::filesystem::path prefix;
Expand Down

0 comments on commit fab3e5a

Please sign in to comment.