From 91011bd217726f73e362b10d77a6638977d6a781 Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Wed, 12 Sep 2012 15:11:25 +0200 Subject: cmGeneratorExpression: Port users to two-stage processing Removing the Process() API and removing the parameters from the constructor will allow cmGeneratorExpressions to be cached and evaluated with multiple configs for example, such as when evaluating target properties. This requires the creation of a new compiled representation of cmGeneratorExpression. The cmListFileBacktrace remains in the constructor so that we can record where a particular generator expression appeared in the CMakeLists file. --- Source/cmCustomCommandGenerator.cxx | 7 ++-- Source/cmGeneratorExpression.cxx | 65 ++++++++++++++++++++++--------------- Source/cmGeneratorExpression.h | 54 +++++++++++++++++++----------- Source/cmTarget.cxx | 17 +++++++--- Source/cmTestGenerator.cxx | 8 ++--- 5 files changed, 95 insertions(+), 56 deletions(-) diff --git a/Source/cmCustomCommandGenerator.cxx b/Source/cmCustomCommandGenerator.cxx index a650129..07df7d5 100644 --- a/Source/cmCustomCommandGenerator.cxx +++ b/Source/cmCustomCommandGenerator.cxx @@ -21,7 +21,7 @@ cmCustomCommandGenerator::cmCustomCommandGenerator( cmCustomCommand const& cc, const char* config, cmMakefile* mf): CC(cc), Config(config), Makefile(mf), LG(mf->GetLocalGenerator()), OldStyle(cc.GetEscapeOldStyle()), MakeVars(cc.GetEscapeAllowMakeVars()), - GE(new cmGeneratorExpression(mf, config, cc.GetBacktrace())) + GE(new cmGeneratorExpression(cc.GetBacktrace())) { } @@ -47,7 +47,7 @@ std::string cmCustomCommandGenerator::GetCommand(unsigned int c) const { return target->GetLocation(this->Config); } - return this->GE->Process(argv0); + return this->GE->Parse(argv0).Evaluate(this->Makefile, this->Config); } //---------------------------------------------------------------------------- @@ -58,7 +58,8 @@ cmCustomCommandGenerator cmCustomCommandLine const& commandLine = this->CC.GetCommandLines()[c]; for(unsigned int j=1;j < commandLine.size(); ++j) { - std::string arg = this->GE->Process(commandLine[j]); + std::string arg = this->GE->Parse(commandLine[j]).Evaluate(this->Makefile, + this->Config); cmd += " "; if(this->OldStyle) { diff --git a/Source/cmGeneratorExpression.cxx b/Source/cmGeneratorExpression.cxx index 198b43a..0885616 100644 --- a/Source/cmGeneratorExpression.cxx +++ b/Source/cmGeneratorExpression.cxx @@ -22,48 +22,50 @@ //---------------------------------------------------------------------------- cmGeneratorExpression::cmGeneratorExpression( - cmMakefile* mf, const char* config, - cmListFileBacktrace const& backtrace, bool quiet): - Makefile(mf), Config(config), Backtrace(backtrace), Quiet(quiet), - NeedsParsing(true) + cmListFileBacktrace const& backtrace): + Backtrace(backtrace), CompiledExpression(0) { } //---------------------------------------------------------------------------- -const char* cmGeneratorExpression::Process(std::string const& input) +const cmCompiledGeneratorExpression & +cmGeneratorExpression::Parse(std::string const& input) { - return this->Process(input.c_str()); + return this->Parse(input.c_str()); } //---------------------------------------------------------------------------- -const char* cmGeneratorExpression::Process(const char* input) +const cmCompiledGeneratorExpression & +cmGeneratorExpression::Parse(const char* input) { - this->Parse(input); - return this->Evaluate(this->Makefile, this->Config, this->Quiet); -} - -//---------------------------------------------------------------------------- -void cmGeneratorExpression::Parse(const char* input) -{ - this->Evaluators.clear(); - - this->Input = input; cmGeneratorExpressionLexer l; - std::vector tokens = l.Tokenize(this->Input); - this->NeedsParsing = l.GetSawGeneratorExpression(); + std::vector tokens = l.Tokenize(input); + bool needsParsing = l.GetSawGeneratorExpression(); + std::vector evaluators; - if (!this->NeedsParsing) + if (needsParsing) { - return; + cmGeneratorExpressionParser p(tokens); + p.Parse(evaluators); } - cmGeneratorExpressionParser p(tokens); - p.Parse(this->Evaluators); + delete this->CompiledExpression; + this->CompiledExpression = new cmCompiledGeneratorExpression( + this->Backtrace, + evaluators, + input, + needsParsing); + return *this->CompiledExpression; +} + +cmGeneratorExpression::~cmGeneratorExpression() +{ + delete this->CompiledExpression; } //---------------------------------------------------------------------------- -const char *cmGeneratorExpression::Evaluate( - cmMakefile* mf, const char* config, bool quiet) +const char *cmCompiledGeneratorExpression::Evaluate( + cmMakefile* mf, const char* config, bool quiet) const { if (!this->NeedsParsing) { @@ -99,8 +101,19 @@ const char *cmGeneratorExpression::Evaluate( return this->Output.c_str(); } +cmCompiledGeneratorExpression::cmCompiledGeneratorExpression( + cmListFileBacktrace const& backtrace, + const std::vector &evaluators, + const char *input, bool needsParsing) + : Backtrace(backtrace), Evaluators(evaluators), Input(input), + NeedsParsing(needsParsing) +{ + +} + + //---------------------------------------------------------------------------- -cmGeneratorExpression::~cmGeneratorExpression() +cmCompiledGeneratorExpression::~cmCompiledGeneratorExpression() { std::vector::const_iterator it = this->Evaluators.begin(); diff --git a/Source/cmGeneratorExpression.h b/Source/cmGeneratorExpression.h index 00e6907..b8467c2 100644 --- a/Source/cmGeneratorExpression.h +++ b/Source/cmGeneratorExpression.h @@ -21,6 +21,8 @@ class cmListFileBacktrace; struct cmGeneratorExpressionEvaluator; +class cmCompiledGeneratorExpression; + /** \class cmGeneratorExpression * \brief Evaluate generate-time query expression syntax. * @@ -33,34 +35,48 @@ struct cmGeneratorExpressionEvaluator; class cmGeneratorExpression { public: - /** Construct with an evaluation context and configuration. */ - cmGeneratorExpression(cmMakefile* mf, const char* config, - cmListFileBacktrace const& backtrace, - bool quiet = false); - + /** Construct. */ + cmGeneratorExpression(cmListFileBacktrace const& backtrace); ~cmGeneratorExpression(); - /** Evaluate generator expressions in a string. */ - const char* Process(std::string const& input); - const char* Process(const char* input); + const cmCompiledGeneratorExpression& Parse(std::string const& input); + const cmCompiledGeneratorExpression& Parse(const char* input); + +private: + cmGeneratorExpression(const cmGeneratorExpression &); + void operator=(const cmGeneratorExpression &); + + cmListFileBacktrace const& Backtrace; + cmCompiledGeneratorExpression *CompiledExpression; +}; - void Parse(const char* input); +class cmCompiledGeneratorExpression +{ +public: const char* Evaluate(cmMakefile* mf, const char* config, - bool quiet = false); + bool quiet = false) const; /** Get set of targets found during evaluations. */ std::set const& GetTargets() const { return this->Targets; } + + ~cmCompiledGeneratorExpression(); + private: - std::vector Evaluators; - cmMakefile* Makefile; - const char* Config; - cmListFileBacktrace const& Backtrace; - bool Quiet; + cmCompiledGeneratorExpression(cmListFileBacktrace const& backtrace, + const std::vector &evaluators, + const char *input, bool needsParsing); - std::set Targets; - const char* Input; - bool NeedsParsing; + friend class cmGeneratorExpression; + + cmCompiledGeneratorExpression(const cmCompiledGeneratorExpression &); + void operator=(const cmCompiledGeneratorExpression &); + + cmListFileBacktrace const& Backtrace; + const std::vector Evaluators; + const char* const Input; + const bool NeedsParsing; - std::string Output; + mutable std::set Targets; + mutable std::string Output; }; diff --git a/Source/cmTarget.cxx b/Source/cmTarget.cxx index 789713f..9a3812c 100644 --- a/Source/cmTarget.cxx +++ b/Source/cmTarget.cxx @@ -1623,7 +1623,11 @@ cmTargetTraceDependencies { // Transform command names that reference targets built in this // project to corresponding target-level dependencies. - cmGeneratorExpression ge(this->Makefile, 0, cc.GetBacktrace(), true); + cmGeneratorExpression ge(cc.GetBacktrace()); + + // Add target-level dependencies referenced by generator expressions. + std::set targets; + for(cmCustomCommandLines::const_iterator cit = cc.GetCommandLines().begin(); cit != cc.GetCommandLines().end(); ++cit) { @@ -1645,12 +1649,17 @@ cmTargetTraceDependencies for(cmCustomCommandLine::const_iterator cli = cit->begin(); cli != cit->end(); ++cli) { - ge.Process(*cli); + const cmCompiledGeneratorExpression &cge = ge.Parse(*cli); + cge.Evaluate(this->Makefile, 0, true); + std::set geTargets = cge.GetTargets(); + for(std::set::const_iterator it = geTargets.begin(); + it != geTargets.end(); ++it) + { + targets.insert(*it); + } } } - // Add target-level dependencies referenced by generator expressions. - std::set targets = ge.GetTargets(); for(std::set::iterator ti = targets.begin(); ti != targets.end(); ++ti) { diff --git a/Source/cmTestGenerator.cxx b/Source/cmTestGenerator.cxx index e0892b2..2f650e7 100644 --- a/Source/cmTestGenerator.cxx +++ b/Source/cmTestGenerator.cxx @@ -91,8 +91,7 @@ void cmTestGenerator::GenerateScriptForConfig(std::ostream& os, this->TestGenerated = true; // Set up generator expression evaluation context. - cmMakefile* mf = this->Test->GetMakefile(); - cmGeneratorExpression ge(mf, config, this->Test->GetBacktrace()); + cmGeneratorExpression ge(this->Test->GetBacktrace()); // Start the test command. os << indent << "ADD_TEST(" << this->Test->GetName() << " "; @@ -103,6 +102,7 @@ void cmTestGenerator::GenerateScriptForConfig(std::ostream& os, // Check whether the command executable is a target whose name is to // be translated. std::string exe = command[0]; + cmMakefile* mf = this->Test->GetMakefile(); cmTarget* target = mf->FindTargetToUse(exe.c_str()); if(target && target->GetType() == cmTarget::EXECUTABLE) { @@ -112,7 +112,7 @@ void cmTestGenerator::GenerateScriptForConfig(std::ostream& os, else { // Use the command name given. - exe = ge.Process(exe.c_str()); + exe = ge.Parse(exe.c_str()).Evaluate(mf, config); cmSystemTools::ConvertToUnixSlashes(exe); } @@ -122,7 +122,7 @@ void cmTestGenerator::GenerateScriptForConfig(std::ostream& os, for(std::vector::const_iterator ci = command.begin()+1; ci != command.end(); ++ci) { - os << " " << lg->EscapeForCMake(ge.Process(*ci)); + os << " " << lg->EscapeForCMake(ge.Parse(*ci).Evaluate(mf, config)); } // Finish the test command. -- cgit v0.12