ENH#696: Adding REQUIRED option to FIND_PACKAGE command. It will terminate the cmake configure step if the package is not found.

This commit is contained in:
Brad King 2004-04-19 10:36:42 -04:00
parent 55a71ba572
commit d4214bc565
2 changed files with 29 additions and 6 deletions

View File

@ -50,6 +50,7 @@ bool cmFindPackageCommand::InitialPass(std::vector<std::string> const& args)
this->Name = args[0]; this->Name = args[0];
bool quiet = false; bool quiet = false;
bool required = false;
if(args.size() > 1) if(args.size() > 1)
{ {
cmsys::RegularExpression version("^[0-9.]+$"); cmsys::RegularExpression version("^[0-9.]+$");
@ -64,6 +65,10 @@ bool cmFindPackageCommand::InitialPass(std::vector<std::string> const& args)
{ {
quiet = true; quiet = true;
} }
else if(args[i] == "REQUIRED")
{
required = true;
}
else else
{ {
cmOStringStream e; cmOStringStream e;
@ -76,7 +81,7 @@ bool cmFindPackageCommand::InitialPass(std::vector<std::string> const& args)
// See if there is a Find<name>.cmake module. // See if there is a Find<name>.cmake module.
bool foundModule = false; bool foundModule = false;
if(!this->FindModule(foundModule, quiet)) if(!this->FindModule(foundModule, quiet, required))
{ {
return false; return false;
} }
@ -155,16 +160,24 @@ bool cmFindPackageCommand::InitialPass(std::vector<std::string> const& args)
e << this->Variable << " is set to \"" << def << "\", which is " e << this->Variable << " is set to \"" << def << "\", which is "
<< "not a directory containing " << this->Config; << "not a directory containing " << this->Config;
cmSystemTools::Error(e.str().c_str()); cmSystemTools::Error(e.str().c_str());
if(required)
{
cmSystemTools::SetFatalErrorOccured();
}
result = true; result = true;
} }
} }
else if(!quiet) else if(!quiet || required)
{ {
cmOStringStream e; cmOStringStream e;
e << this->Variable << " is not set. It must be set to the directory " e << this->Variable << " is not set. It must be set to the directory "
<< "containing " << this->Config << " in order to use " << "containing " << this->Config << " in order to use "
<< this->Name << "."; << this->Name << ".";
cmSystemTools::Error(e.str().c_str()); cmSystemTools::Error(e.str().c_str());
if(required)
{
cmSystemTools::SetFatalErrorOccured();
}
result = true; result = true;
} }
@ -217,7 +230,7 @@ bool cmFindPackageCommand::InitialPass(std::vector<std::string> const& args)
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
bool cmFindPackageCommand::FindModule(bool& found, bool quiet) bool cmFindPackageCommand::FindModule(bool& found, bool quiet, bool required)
{ {
std::string module = "Find"; std::string module = "Find";
module += this->Name; module += this->Name;
@ -234,6 +247,15 @@ bool cmFindPackageCommand::FindModule(bool& found, bool quiet)
m_Makefile->AddDefinition(quietly.c_str(), "1"); m_Makefile->AddDefinition(quietly.c_str(), "1");
} }
if(required)
{
// Tell the module that is about to be read that it should report
// a fatal error if the package is not found.
std::string req = this->Name;
req += "_FIND_REQUIRED";
m_Makefile->AddDefinition(req.c_str(), "1");
}
// Load the module we found. // Load the module we found.
found = true; found = true;
return this->ReadListFile(mfile.c_str()); return this->ReadListFile(mfile.c_str());

View File

@ -63,7 +63,7 @@ public:
virtual const char* GetFullDocumentation() virtual const char* GetFullDocumentation()
{ {
return return
" FIND_PACKAGE(<name> [major.minor] [QUIET])\n" " FIND_PACKAGE(<name> [major.minor] [QUIET] [REQUIRED])\n"
"Finds and loads settings from an external project. <name>_FOUND will " "Finds and loads settings from an external project. <name>_FOUND will "
"be set to indicate whether the package was found. Settings that " "be set to indicate whether the package was found. Settings that "
"can be used when <name>_FOUND is true are package-specific. The " "can be used when <name>_FOUND is true are package-specific. The "
@ -80,12 +80,13 @@ public:
"will generate an error describing the problem unless the QUIET " "will generate an error describing the problem unless the QUIET "
"argument is specified. If <name>_DIR has been set to a directory " "argument is specified. If <name>_DIR has been set to a directory "
"not containing a \"<name>Config.cmake\" file, an error is always " "not containing a \"<name>Config.cmake\" file, an error is always "
"generated."; "generated. If REQUIRED is specified and the package is not found, "
"a FATAL_ERROR is generated and the configure step stops executing.";
} }
cmTypeMacro(cmFindPackageCommand, cmCommand); cmTypeMacro(cmFindPackageCommand, cmCommand);
private: private:
bool FindModule(bool& found, bool quiet); bool FindModule(bool& found, bool quiet, bool required);
bool FindConfig(); bool FindConfig();
std::string SearchForConfig() const; std::string SearchForConfig() const;
bool ReadListFile(const char* f); bool ReadListFile(const char* f);