CMake/Source/cmLoadCommandCommand.cxx

303 lines
7.8 KiB
C++
Raw Normal View History

2002-08-21 15:58:48 +00:00
/*=========================================================================
Program: CMake - Cross-Platform Makefile Generator
2002-08-21 15:58:48 +00:00
Module: $RCSfile$
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
2002-08-21 15:58:48 +00:00
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "cmLoadCommandCommand.h"
#include "cmCPluginAPI.h"
2002-08-26 14:52:04 +00:00
#include "cmCPluginAPI.cxx"
#include "cmDynamicLoader.h"
#include <signal.h>
2004-04-27 12:30:25 +00:00
extern "C" void TrapsForSignalsCFunction(int sig);
2002-08-21 15:58:48 +00:00
2004-04-27 12:30:25 +00:00
2002-08-21 15:58:48 +00:00
// a class for loadabple commands
class cmLoadedCommand : public cmCommand
{
public:
cmLoadedCommand() {
2002-08-26 14:52:04 +00:00
memset(&this->info,0,sizeof(this->info));
this->info.CAPI = &cmStaticCAPI;
}
2002-08-21 15:58:48 +00:00
2002-09-17 14:38:00 +00:00
///! clean up any memory allocated by the plugin
~cmLoadedCommand();
2002-08-21 15:58:48 +00:00
/**
* This is a virtual constructor for the command.
*/
virtual cmCommand* Clone()
{
cmLoadedCommand *newC = new cmLoadedCommand;
// we must copy when we clone
memcpy(&newC->info,&this->info,sizeof(info));
return newC;
}
/**
* This is called when the command is first encountered in
* the CMakeLists.txt file.
*/
virtual bool InitialPass(std::vector<std::string> const& args);
/**
* This is called at the end after all the information
* specified by the command is accumulated. Most commands do
* not implement this method. At this point, reading and
* writing to the cache can be done.
*/
virtual void FinalPass();
/**
* The name of the command as specified in CMakeList.txt.
*/
2002-09-23 18:57:24 +00:00
virtual const char* GetName() { return info.Name; }
2002-08-21 15:58:48 +00:00
/**
* Succinct documentation.
*/
virtual const char* GetTerseDocumentation()
{
if (this->info.GetTerseDocumentation)
{
cmLoadedCommand::InstallSignalHandlers(info.Name);
const char* ret = info.GetTerseDocumentation();
cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
return ret;
2002-08-21 15:58:48 +00:00
}
else
{
return "LoadedCommand without any additional documentation";
}
}
static const char* LastName;
static void TrapsForSignals(int sig)
{
fprintf(stderr, "CMake loaded command %s crashed with signal: %d.\n",
cmLoadedCommand::LastName, sig);
}
static void InstallSignalHandlers(const char* name, int remove = 0)
{
cmLoadedCommand::LastName = name;
if(!name)
{
cmLoadedCommand::LastName = "????";
}
if(!remove)
{
2004-04-27 12:30:25 +00:00
signal(SIGSEGV, TrapsForSignalsCFunction);
#ifdef SIGBUS
2004-04-27 12:30:25 +00:00
signal(SIGBUS, TrapsForSignalsCFunction);
#endif
2004-04-27 12:30:25 +00:00
signal(SIGILL, TrapsForSignalsCFunction);
}
else
{
signal(SIGSEGV, 0);
#ifdef SIGBUS
signal(SIGBUS, 0);
#endif
signal(SIGILL, 0);
}
}
2002-08-21 15:58:48 +00:00
/**
* More documentation.
*/
virtual const char* GetFullDocumentation()
{
if (this->info.GetFullDocumentation)
{
cmLoadedCommand::InstallSignalHandlers(info.Name);
const char* ret = info.GetFullDocumentation();
cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
return ret;
2002-08-21 15:58:48 +00:00
}
else
{
return "LoadedCommand without any additional documentation";
}
}
cmTypeMacro(cmLoadedCommand, cmCommand);
cmLoadedCommandInfo info;
};
2004-04-27 12:30:25 +00:00
extern "C" void TrapsForSignalsCFunction(int sig)
{
cmLoadedCommand::TrapsForSignals(sig);
}
const char* cmLoadedCommand::LastName = 0;
2002-08-21 15:58:48 +00:00
bool cmLoadedCommand::InitialPass(std::vector<std::string> const& args)
{
if (!info.InitialPass)
{
return true;
}
2002-10-08 19:55:04 +00:00
// clear the error string
if (this->info.Error)
{
free(this->info.Error);
}
2002-08-21 15:58:48 +00:00
// create argc and argv and then invoke the command
2002-08-27 13:43:54 +00:00
int argc = static_cast<int> (args.size());
char **argv = 0;
2002-08-21 15:58:48 +00:00
if (argc)
{
argv = (char **)malloc(argc*sizeof(char *));
}
int i;
for (i = 0; i < argc; ++i)
{
argv[i] = strdup(args[i].c_str());
}
cmLoadedCommand::InstallSignalHandlers(info.Name);
2006-03-15 16:02:08 +00:00
int result = info.InitialPass((void *)&info,
(void *)this->Makefile,argc,argv);
cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
2002-08-21 15:58:48 +00:00
cmFreeArguments(argc,argv);
2002-10-08 19:55:04 +00:00
2002-08-21 15:58:48 +00:00
if (result)
{
return true;
}
2002-10-08 19:55:04 +00:00
/* Initial Pass must have failed so set the error string */
if (this->info.Error)
{
this->SetError(this->info.Error);
}
2002-08-21 15:58:48 +00:00
return false;
}
void cmLoadedCommand::FinalPass()
{
if (this->info.FinalPass)
{
cmLoadedCommand::InstallSignalHandlers(info.Name);
2006-03-15 16:02:08 +00:00
this->info.FinalPass((void *)&this->info,(void *)this->Makefile);
cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
2002-08-21 15:58:48 +00:00
}
2002-09-17 14:38:00 +00:00
}
cmLoadedCommand::~cmLoadedCommand()
{
if (this->info.Destructor)
{
cmLoadedCommand::InstallSignalHandlers(info.Name);
2002-09-17 14:38:00 +00:00
this->info.Destructor((void *)&this->info);
cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
2002-09-17 14:38:00 +00:00
}
2002-10-08 19:55:04 +00:00
if (this->info.Error)
{
free(this->info.Error);
}
2002-08-21 15:58:48 +00:00
}
// cmLoadCommandCommand
bool cmLoadCommandCommand::InitialPass(std::vector<std::string> const& args)
2002-08-21 15:58:48 +00:00
{
if(args.size() < 1 )
2002-08-21 15:58:48 +00:00
{
return true;
}
// Construct a variable to report what file was loaded, if any.
// Start by removing the definition in case of failure.
std::string reportVar = "CMAKE_LOADED_COMMAND_";
reportVar += args[0];
2006-03-15 16:02:08 +00:00
this->Makefile->RemoveDefinition(reportVar.c_str());
2002-08-21 15:58:48 +00:00
// the file must exist
std::string fullPath = cmDynamicLoader::LibPrefix();
fullPath += "cm" + args[0] + cmDynamicLoader::LibExtension();
2002-08-21 15:58:48 +00:00
// search for the file
std::vector<std::string> path;
for (unsigned int j = 1; j < args.size(); j++)
{
// expand variables
std::string exp = args[j];
cmSystemTools::ExpandRegistryValues(exp);
// Glob the entry in case of wildcards.
cmSystemTools::GlobDirs(exp.c_str(), path);
}
// Try to find the program.
fullPath = cmSystemTools::FindFile(fullPath.c_str(), path);
if (fullPath == "")
{
fullPath = "Attempt to load command failed from file : ";
fullPath += cmDynamicLoader::LibPrefix();
fullPath += "cm" + args[0] + cmDynamicLoader::LibExtension();
this->SetError(fullPath.c_str());
2002-08-21 15:58:48 +00:00
return false;
}
2002-09-23 17:11:39 +00:00
2002-08-21 15:58:48 +00:00
// try loading the shared library / dll
cmLibHandle lib = cmDynamicLoader::OpenLibrary(fullPath.c_str());
2002-10-15 15:45:04 +00:00
if(!lib)
{
std::string err = "Attempt to load the library ";
err += fullPath + " failed.";
const char* error = cmDynamicLoader::LastError();
if ( error )
{
err += " Additional error info is:\n";
err += error;
}
2002-10-15 15:45:04 +00:00
this->SetError(err.c_str());
return false;
}
// Report what file was loaded for this command.
2006-03-15 16:02:08 +00:00
this->Makefile->AddDefinition(reportVar.c_str(), fullPath.c_str());
2002-10-15 15:45:04 +00:00
// find the init function
std::string initFuncName = args[0] + "Init";
CM_INIT_FUNCTION initFunction
= (CM_INIT_FUNCTION)
cmDynamicLoader::GetSymbolAddress(lib, initFuncName.c_str());
if ( !initFunction )
{
initFuncName = "_";
initFuncName += args[0];
initFuncName += "Init";
initFunction = (CM_INIT_FUNCTION)(
cmDynamicLoader::GetSymbolAddress(lib, initFuncName.c_str()));
}
// if the symbol is found call it to set the name on the
// function blocker
if(initFunction)
{
// create a function blocker and set it up
cmLoadedCommand *f = new cmLoadedCommand();
(*initFunction)(&f->info);
2006-03-15 16:02:08 +00:00
this->Makefile->AddCommand(f);
2002-10-15 15:45:04 +00:00
return true;
2002-08-21 15:58:48 +00:00
}
2002-10-15 15:45:04 +00:00
this->SetError("Attempt to load command failed. "
"No init function found.");
2002-09-23 17:11:39 +00:00
return false;
2002-08-21 15:58:48 +00:00
}