ENH: Added NO_MODULE and COMPONENTS options to improve flexibility of the command. Re-implemented argument parsing to be simpler and more robust.

This commit is contained in:
Brad King 2006-10-26 11:39:56 -04:00
parent 5418998e46
commit d9f1d4d71a
2 changed files with 70 additions and 68 deletions

View File

@ -54,80 +54,80 @@ bool cmFindPackageCommand::InitialPass(std::vector<std::string> const& args)
return false;
}
// Record options.
this->Name = args[0];
// Build a list of required components.
std::string components;
const char* components_sep = "";
bool quiet = false;
bool required = false;
if(args.size() > 1)
bool no_module = false;
std::string components;
const char* components_sep = "";
// Parse the arguments.
bool doing_components = false;
cmsys::RegularExpression version("^[0-9.]+$");
bool haveVersion = false;
for(unsigned int i=1; i < args.size(); ++i)
{
cmsys::RegularExpression version("^[0-9.]+$");
bool haveVersion = false;
for(unsigned int i=1; i < args.size(); ++i)
if(args[i] == "QUIET")
{
if(!haveVersion && version.find(args[i].c_str()))
{
haveVersion = true;
}
else if(args[i] == "QUIET")
{
quiet = true;
}
else if(args[i] == "REQUIRED")
{
// The package is required.
required = true;
// Look for a list of required components.
while(++i < args.size())
{
// Stop looking when a known keyword argument is
// encountered.
if((args[i] == "QUIET") ||
(args[i] == "REQUIRED"))
{
--i;
break;
}
else
{
// Set a variable telling the find script this component
// is required.
std::string req_var = Name + "_FIND_REQUIRED_" + args[i];
this->Makefile->AddDefinition(req_var.c_str(), "1");
// Append to the list of required components.
components += components_sep;
components += args[i];
components_sep = ";";
}
}
}
else
{
cmOStringStream e;
e << "called with invalid argument \"" << args[i].c_str() << "\"";
this->SetError(e.str().c_str());
return false;
}
quiet = true;
doing_components = false;
}
else if(args[i] == "NO_MODULE")
{
no_module = true;
doing_components = false;
}
else if(args[i] == "REQUIRED")
{
required = true;
doing_components = true;
}
else if(args[i] == "COMPONENTS")
{
doing_components = true;
}
else if(doing_components)
{
// Set a variable telling the find script this component
// is required.
std::string req_var = Name + "_FIND_REQUIRED_" + args[i];
this->Makefile->AddDefinition(req_var.c_str(), "1");
// Store the list of components.
std::string components_var = Name + "_FIND_COMPONENTS";
this->Makefile->AddDefinition(components_var.c_str(), components.c_str());
// Append to the list of required components.
components += components_sep;
components += args[i];
components_sep = ";";
}
else if(!haveVersion && version.find(args[i].c_str()))
{
haveVersion = true;
}
else
{
cmOStringStream e;
e << "called with invalid argument \"" << args[i].c_str() << "\"";
this->SetError(e.str().c_str());
return false;
}
}
// Store the list of components.
std::string components_var = Name + "_FIND_COMPONENTS";
this->Makefile->AddDefinition(components_var.c_str(), components.c_str());
// See if there is a Find<name>.cmake module.
bool foundModule = false;
if(!this->FindModule(foundModule, quiet, required))
if(!no_module)
{
return false;
}
if(foundModule)
{
return true;
bool foundModule = false;
if(!this->FindModule(foundModule, quiet, required))
{
return false;
}
if(foundModule)
{
return true;
}
}
// No find module. Assume the project has a CMake config file. Use

View File

@ -65,8 +65,8 @@ public:
virtual const char* GetFullDocumentation()
{
return
" FIND_PACKAGE(<name> [major.minor] [QUIET]\n"
" [REQUIRED [componets...]])\n"
" FIND_PACKAGE(<name> [major.minor] [QUIET] [NO_MODULE]\n"
" [[REQUIRED|COMPONENTS] [componets...]])\n"
"Finds and loads settings from an external project. <name>_FOUND will "
"be set to indicate whether the package was found. Settings that "
"can be used when <name>_FOUND is true are package-specific. The "
@ -74,6 +74,7 @@ public:
"Directories listed in CMAKE_MODULE_PATH are searched for files called "
"\"Find<name>.cmake\". If such a file is found, it is read and "
"processed by CMake, and is responsible for finding the package. "
"This first step may be skipped by using the NO_MODULE option. "
"If no such file is found, it is expected that the package is another "
"project built by CMake that has a \"<name>Config.cmake\" file. "
"A cache entry called <name>_DIR is created and is expected to be set "
@ -84,9 +85,10 @@ public:
"argument is specified. If <name>_DIR has been set to a directory "
"not containing a \"<name>Config.cmake\" file, an error is always "
"generated. If REQUIRED is specified and the package is not found, "
"a FATAL_ERROR is generated and the configure step stops executing."
" A package-specific list of components may be listed after the "
"REQUIRED option.";
"a FATAL_ERROR is generated and the configure step stops executing. "
"A package-specific list of components may be listed after the "
"REQUIRED option, or after the COMPONENTS option if no REQUIRED "
"option is given.";
}
cmTypeMacro(cmFindPackageCommand, cmCommand);