diff --git a/Source/cmCustomCommandGenerator.cxx b/Source/cmCustomCommandGenerator.cxx index a65012905..07df7d5e0 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 198b43a1f..0885616ad 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 00e6907ef..b8467c27c 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); - void Parse(const char* input); +private: + cmGeneratorExpression(const cmGeneratorExpression &); + void operator=(const cmGeneratorExpression &); + + cmListFileBacktrace const& Backtrace; + cmCompiledGeneratorExpression *CompiledExpression; +}; + +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; + cmCompiledGeneratorExpression(cmListFileBacktrace const& backtrace, + const std::vector &evaluators, + const char *input, bool needsParsing); + + friend class cmGeneratorExpression; + + cmCompiledGeneratorExpression(const cmCompiledGeneratorExpression &); + void operator=(const cmCompiledGeneratorExpression &); + cmListFileBacktrace const& Backtrace; - bool Quiet; + const std::vector Evaluators; + const char* const Input; + const bool NeedsParsing; - std::set Targets; - const char* Input; - bool NeedsParsing; - - std::string Output; + mutable std::set Targets; + mutable std::string Output; }; diff --git a/Source/cmTarget.cxx b/Source/cmTarget.cxx index 789713fcc..9a3812c2e 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 e0892b282..2f650e72e 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.