Skip to content

Commit

Permalink
Fix referring to output of subninja.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillof committed Apr 16, 2024
1 parent 43708dc commit 165a23d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 26 deletions.
49 changes: 27 additions & 22 deletions src/File.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,22 @@ File::File(const std::filesystem::path& filename, const std::filesystem::path& b
}

void File::process() {
if (is_top()) {
auto bindings = Bindings{};
bindings.add(std::shared_ptr<Variable>(new TextVariable{"command", Text{std::vector<Word>{
Word{"fast-ninja", false},
Word{" ", false},
Word{source_directory, true}
}}}));
bindings.add(std::shared_ptr<Variable>(new TextVariable{"generator", Text{"1", false}}));

rules["fast-ninja"] = Rule(this, "fast-ninja", bindings);
auto outputs = std::vector<Filename>{};
auto inputs = std::vector<Filename>{};
add_generator_build(outputs, inputs);

builds.emplace_back(this, "fast-ninja", Dependencies{FilenameList{outputs}}, Dependencies{FilenameList{inputs}}, Bindings{});
}
auto bindings = Bindings{};
bindings.add(std::shared_ptr<Variable>(new TextVariable{ "command", Text{ std::vector<Word>{ Word{ "fast-ninja", false }, Word{ " ", false }, Word{ source_directory, true } } } }));
bindings.add(std::shared_ptr<Variable>(new TextVariable{ "generator", Text{ "1", false } }));

rules["fast-ninja"] = Rule(this, "fast-ninja", bindings);
auto ninja_outputs = std::vector<Filename>{};
auto ninja_inputs = std::vector<Filename>{};
add_generator_build(ninja_outputs, ninja_inputs);

builds.emplace_back(this, "fast-ninja", Dependencies{ FilenameList{ ninja_outputs } }, Dependencies{ FilenameList{ ninja_inputs } }, Bindings{});

process_output();
process_rest();
}

void File::process_output() { // NOLINT(misc-no-recursion)
auto top_file = const_cast<File*>(top()->as_file());
if (!top_file) {
throw Exception("internal error: top scope is not a file");
Expand All @@ -85,6 +84,12 @@ void File::process() {
build.collect_output_files(top_file->outputs);
}

for (const auto& file : subfiles) {
file->process_output();
}
}

void File::process_rest() { // NOLINT(misc-no-recursion)
bindings.resolve(*this);

for (auto& pair : rules) {
Expand All @@ -99,7 +104,7 @@ void File::process() {
defaults.resolve(context);

for (const auto& file : subfiles) {
file->process();
file->process_rest();
}
}

Expand Down Expand Up @@ -127,7 +132,7 @@ const Variable* File::find_variable(const std::string& name) const {
return nullptr;
}

void File::create_output() const {
void File::create_output() const { // NOLINT(misc-no-recursion)
std::filesystem::create_directories(build_directory);
auto stream = std::ofstream(build_filename);

Expand Down Expand Up @@ -282,11 +287,11 @@ void File::parse_subninja(Tokenizer& tokenizer) {
subninjas.emplace_back(text.string());
}

void File::add_generator_build(std::vector<Filename>& outputs, std::vector<Filename>& inputs) const {
outputs.emplace_back(Filename::Type::BUILD, build_filename);
inputs.emplace_back(Filename::Type::COMPLETE, source_filename);
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);
ninja_inputs.emplace_back(Filename::Type::COMPLETE, source_filename);
for (const auto& file: subfiles) {
file->add_generator_build(outputs, inputs);
file->add_generator_build(ninja_outputs, ninja_inputs);
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/File.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ class File: public Scope {

void create_output() const;

const File* next_file() const;
const File* top_file() const {return top()->as_file();}
[[nodiscard]] const File* next_file() const;
[[nodiscard]] const File* top_file() const {return top()->as_file();}

std::filesystem::path source_directory;
std::filesystem::path build_directory;
Expand All @@ -74,7 +74,10 @@ class File: public Scope {
void parse_rule(Tokenizer& tokenizer);
void parse_subninja(Tokenizer& tokenizer);

void add_generator_build(std::vector<Filename>& outputs, std::vector<Filename>& inputs) const;
void process_output();
void process_rest();

void add_generator_build(std::vector<Filename>& ninja_outputs, std::vector<Filename>& ninja_inputs) const;

std::filesystem::path source_filename;
std::filesystem::path build_filename;
Expand Down
2 changes: 1 addition & 1 deletion src/Filename.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void Filename::resolve(const ResolveContext& context) {
const auto file = context.scope.get_file();

if (type == Type::UNKNOWN) {
if (context.scope.is_output_file(file->build_directory / name)) {
if (context.scope.is_output_file((file->build_directory / name).lexically_normal())) {
type = Type::BUILD;
}
if (std::filesystem::exists(file->source_directory / name)) {
Expand Down
50 changes: 50 additions & 0 deletions tests/sub-output.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
arguments ..
file src/input <>
source input
end-of-inline-data
file build.fninja <>
rule a
command = a $in $out
flags = --verbose

build top-output: a src/sub-output

subninja src/build.fninja
end-of-inline-data
file src/build.fninja <>
build sub-output: a input
end-of-inline-data

file build/build.ninja {} <>
# This file is automatically created by fast-ninja from ../build.fninja
# Do not edit.

build_directory = .
source_directory = ..
top_build_directory = .
top_source_directory = ..

rule a
command = a $in $out
flags = --verbose

rule fast-ninja
command = fast-ninja ..
generator = 1

build top-output : a src/sub-output

build build.ninja src/build.ninja : fast-ninja ../build.fninja ../src/build.fninja

subninja src/build.ninja
end-of-inline-data

file build/src/build.ninja {} <>
# This file is automatically created by fast-ninja from ../src/build.fninja
# Do not edit.

build_directory = src
source_directory = ../src

build src/sub-output : a ../src/input
end-of-inline-data

0 comments on commit 165a23d

Please sign in to comment.