Skip to content

Commit

Permalink
Don’t expand filename variables in rule bindings.
Browse files Browse the repository at this point in the history
This allows $build_directory to be used in command.
  • Loading branch information
dillof committed Apr 14, 2024
1 parent efb0596 commit 43708dc
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 15 deletions.
10 changes: 4 additions & 6 deletions src/Bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,13 @@ Bindings::Bindings(Tokenizer& tokenizer) {

void Bindings::print(std::ostream& stream, const std::string& indent) const {
for (auto& pair : *this) {
if (!pair.second->is_filename()) {
stream << indent;
pair.second->print_definition(stream);
}
stream << indent;
pair.second->print_definition(stream);
}
}

void Bindings::resolve(const Scope& scope) {
auto context = ResolveContext{scope};
void Bindings::resolve(const Scope& scope, bool expand_variables) {
auto context = ResolveContext{scope, expand_variables};
for (auto& pair : variables) {
pair.second->resolve(context);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Bindings {
explicit Bindings(Tokenizer& tokenizer);

void print(std::ostream& stream, const std::string& indent) const;
void resolve(const Scope& scope);
void resolve(const Scope& scope, bool expand_variables = true);
void add(std::shared_ptr<Variable> variable) {variables[variable->name] = std::move(variable);}

[[nodiscard]] auto empty() const {return variables.empty();}
Expand Down
2 changes: 1 addition & 1 deletion src/File.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ void File::create_output() const {
}

stream << "# This file is automatically created by fast-ninja from " << source_filename.string() << std::endl;
stream << "# Do not edit." << std::endl;
stream << "# Do not edit." << std::endl << std::endl;

if (!bindings.empty()) {
bindings.print(stream, "");
Expand Down
4 changes: 4 additions & 0 deletions src/FilenameVariable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ FilenameVariable::FilenameVariable(std::string name, Tokenizer& tokenizer) : Var
value = FilenameList(tokenizer, FilenameList::INLINE);
}
}

void FilenameVariable::print_definition(std::ostream& stream) const {
stream << name << " = " << value << std::endl;
}
2 changes: 1 addition & 1 deletion src/FilenameVariable.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class FilenameVariable : public Variable {
FilenameVariable(std::string name, FilenameList value): Variable(std::move(name)), value{std::move(value)} {}

void resolve_sub(const ResolveContext& context) override {value.resolve(context);}
void print_definition(std::ostream& stream) const override {} // TODO
void print_definition(std::ostream& stream) const override;
void print_use(std::ostream& stream) const override {} // TODO
[[nodiscard]] std::string string() const override {return value.string();}
void collect_filenames(std::vector<Filename>& collector) const {return value.collect_filenames(collector);}
Expand Down
3 changes: 2 additions & 1 deletion src/ResolveContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

class ResolveContext {
public:
explicit ResolveContext(const Scope& scope): scope{scope} {}
explicit ResolveContext(const Scope& scope, bool expand_variables = false): scope{scope}, expand_variables{expand_variables} {}

[[nodiscard]] ResolveContext resolving(const std::string& name) const;
[[nodiscard]] const Variable* get_variable(const std::string& name) const;

const Scope& scope;
bool expand_variables{false};

private:
std::unordered_set<std::string> resolving_variables;
Expand Down
4 changes: 3 additions & 1 deletion src/Rule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ Rule::Rule(const File* file, std::string name, Tokenizer& tokenizer) : ScopedDir

Rule::Rule(const File* file, std::string name, Bindings bindings): ScopedDirective{file, std::move(bindings)}, name{std::move(name)} {}

void Rule::process(const File& file) { bindings.resolve(file); }
void Rule::process(const File& file) {
bindings.resolve(file, false);
}

void Rule::print(std::ostream& stream) const {
stream << std::endl << "rule " << name << std::endl;
Expand Down
10 changes: 6 additions & 4 deletions src/Word.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,12 @@ void Word::print(std::ostream& stream) const {
void Word::resolve(const ResolveContext& context) {
for (auto& element : elements) {
if (std::holds_alternative<VariableReference>(element)) {
auto& variable_reference = std::get<VariableReference>(element);
variable_reference.resolve(context);
if (variable_reference.is_text_variable()) {
element = StringElement{variable_reference.variable->string(), true};
if (context.expand_variables) {
auto& variable_reference = std::get<VariableReference>(element);
variable_reference.resolve(context);
if (variable_reference.is_text_variable()) {
element = StringElement{ variable_reference.variable->string(), true };
}
}
}
else if (std::holds_alternative<FilenameWord>(element)) {
Expand Down
5 changes: 5 additions & 0 deletions tests/explicit-filename.test
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ 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 = ../input $in $out
flags = --verbose
Expand Down
6 changes: 6 additions & 0 deletions tests/filelist.test
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ 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 = ..
sources = ../input ../input-2 output
top_build_directory = .
top_source_directory = ..
version = 2

rule a
Expand Down
5 changes: 5 additions & 0 deletions tests/implicit-output.test
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ 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
Expand Down
5 changes: 5 additions & 0 deletions tests/rule.test
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ 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
Expand Down
5 changes: 5 additions & 0 deletions tests/special-characters.test
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ 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 rule build = "a$ b" |@ || | @

Expand Down
8 changes: 8 additions & 0 deletions tests/special_directories.test
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ 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

Expand All @@ -33,5 +38,8 @@ 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/test : a src ../src . ..
end-of-inline-data
8 changes: 8 additions & 0 deletions tests/subninja.test
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ 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
Expand All @@ -44,5 +49,8 @@ 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/output : a output ../src/input ../input
end-of-inline-data

0 comments on commit 43708dc

Please sign in to comment.