2009-09-28 19:43:28 +04:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2002-08-21 19:58:48 +04:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2002-08-21 19:58:48 +04:00
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
2002-08-21 19:58:48 +04:00
|
|
|
#include "cmLoadCommandCommand.h"
|
|
|
|
#include "cmCPluginAPI.h"
|
2002-08-26 18:52:04 +04:00
|
|
|
#include "cmCPluginAPI.cxx"
|
|
|
|
#include "cmDynamicLoader.h"
|
2006-03-16 19:01:05 +03:00
|
|
|
|
|
|
|
#include <cmsys/DynamicLoader.hxx>
|
|
|
|
|
2006-08-01 22:31:24 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#ifdef __QNX__
|
|
|
|
# include <malloc.h> /* for malloc/free on QNX */
|
|
|
|
#endif
|
|
|
|
|
2004-04-27 01:45:53 +04:00
|
|
|
#include <signal.h>
|
2004-04-27 16:30:25 +04:00
|
|
|
extern "C" void TrapsForSignalsCFunction(int sig);
|
2002-08-21 19:58:48 +04:00
|
|
|
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-08-21 19:58:48 +04:00
|
|
|
// a class for loadabple commands
|
|
|
|
class cmLoadedCommand : public cmCommand
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
cmLoadedCommand() {
|
2012-08-13 21:42:58 +04:00
|
|
|
memset(&this->info,0,sizeof(this->info));
|
2002-08-26 18:52:04 +04:00
|
|
|
this->info.CAPI = &cmStaticCAPI;
|
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-09-17 18:38:00 +04:00
|
|
|
///! clean up any memory allocated by the plugin
|
|
|
|
~cmLoadedCommand();
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-08-21 19:58:48 +04:00
|
|
|
/**
|
|
|
|
* This is a virtual constructor for the command.
|
|
|
|
*/
|
2012-08-13 21:42:58 +04:00
|
|
|
virtual cmCommand* Clone()
|
2002-08-21 19:58:48 +04:00
|
|
|
{
|
|
|
|
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.
|
|
|
|
*/
|
2012-08-13 21:42:58 +04:00
|
|
|
virtual bool InitialPass(std::vector<std::string> const& args,
|
2008-01-23 18:28:26 +03:00
|
|
|
cmExecutionStatus &);
|
2002-08-21 19:58:48 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
2009-07-24 21:31:34 +04:00
|
|
|
virtual bool HasFinalPass() const
|
|
|
|
{ return this->info.FinalPass? true:false; }
|
2002-08-21 19:58:48 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the command as specified in CMakeList.txt.
|
|
|
|
*/
|
2012-02-25 10:49:24 +04:00
|
|
|
virtual const char* GetName() const { return info.Name; }
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2004-04-27 01:45:53 +04:00
|
|
|
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 = "????";
|
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2004-04-27 01:45:53 +04:00
|
|
|
if(!remove)
|
|
|
|
{
|
2004-04-27 16:30:25 +04:00
|
|
|
signal(SIGSEGV, TrapsForSignalsCFunction);
|
2004-04-27 01:45:53 +04:00
|
|
|
#ifdef SIGBUS
|
2004-04-27 16:30:25 +04:00
|
|
|
signal(SIGBUS, TrapsForSignalsCFunction);
|
2004-04-27 01:45:53 +04:00
|
|
|
#endif
|
2004-04-27 16:30:25 +04:00
|
|
|
signal(SIGILL, TrapsForSignalsCFunction);
|
2004-04-27 01:45:53 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
signal(SIGSEGV, 0);
|
|
|
|
#ifdef SIGBUS
|
|
|
|
signal(SIGBUS, 0);
|
|
|
|
#endif
|
|
|
|
signal(SIGILL, 0);
|
|
|
|
}
|
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-08-21 19:58:48 +04:00
|
|
|
cmTypeMacro(cmLoadedCommand, cmCommand);
|
|
|
|
|
|
|
|
cmLoadedCommandInfo info;
|
|
|
|
};
|
|
|
|
|
2004-04-27 16:30:25 +04:00
|
|
|
extern "C" void TrapsForSignalsCFunction(int sig)
|
|
|
|
{
|
|
|
|
cmLoadedCommand::TrapsForSignals(sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-04-27 01:45:53 +04:00
|
|
|
const char* cmLoadedCommand::LastName = 0;
|
|
|
|
|
2008-01-23 18:28:26 +03:00
|
|
|
bool cmLoadedCommand::InitialPass(std::vector<std::string> const& args,
|
|
|
|
cmExecutionStatus &)
|
2002-08-21 19:58:48 +04:00
|
|
|
{
|
|
|
|
if (!info.InitialPass)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2002-10-08 23:55:04 +04:00
|
|
|
|
|
|
|
// clear the error string
|
|
|
|
if (this->info.Error)
|
|
|
|
{
|
|
|
|
free(this->info.Error);
|
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-08-21 19:58:48 +04:00
|
|
|
// create argc and argv and then invoke the command
|
2002-08-27 17:43:54 +04:00
|
|
|
int argc = static_cast<int> (args.size());
|
2002-09-10 23:39:18 +04:00
|
|
|
char **argv = 0;
|
2002-08-21 19:58:48 +04:00
|
|
|
if (argc)
|
|
|
|
{
|
|
|
|
argv = (char **)malloc(argc*sizeof(char *));
|
|
|
|
}
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < argc; ++i)
|
|
|
|
{
|
|
|
|
argv[i] = strdup(args[i].c_str());
|
|
|
|
}
|
2004-04-27 01:45:53 +04:00
|
|
|
cmLoadedCommand::InstallSignalHandlers(info.Name);
|
2006-03-15 19:02:08 +03:00
|
|
|
int result = info.InitialPass((void *)&info,
|
2012-08-13 21:42:58 +04:00
|
|
|
(void *)this->Makefile,argc,argv);
|
2004-04-27 01:45:53 +04:00
|
|
|
cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
|
2002-08-21 19:58:48 +04:00
|
|
|
cmFreeArguments(argc,argv);
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-08-21 19:58:48 +04:00
|
|
|
if (result)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2002-10-08 23:55:04 +04:00
|
|
|
|
|
|
|
/* Initial Pass must have failed so set the error string */
|
|
|
|
if (this->info.Error)
|
|
|
|
{
|
|
|
|
this->SetError(this->info.Error);
|
|
|
|
}
|
2002-08-21 19:58:48 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmLoadedCommand::FinalPass()
|
|
|
|
{
|
|
|
|
if (this->info.FinalPass)
|
|
|
|
{
|
2004-04-27 01:45:53 +04:00
|
|
|
cmLoadedCommand::InstallSignalHandlers(info.Name);
|
2006-03-15 19:02:08 +03:00
|
|
|
this->info.FinalPass((void *)&this->info,(void *)this->Makefile);
|
2004-04-27 01:45:53 +04:00
|
|
|
cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
|
2002-08-21 19:58:48 +04:00
|
|
|
}
|
2002-09-17 18:38:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
cmLoadedCommand::~cmLoadedCommand()
|
|
|
|
{
|
|
|
|
if (this->info.Destructor)
|
|
|
|
{
|
2004-04-27 01:45:53 +04:00
|
|
|
cmLoadedCommand::InstallSignalHandlers(info.Name);
|
2002-09-17 18:38:00 +04:00
|
|
|
this->info.Destructor((void *)&this->info);
|
2004-04-27 01:45:53 +04:00
|
|
|
cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
|
2002-09-17 18:38:00 +04:00
|
|
|
}
|
2002-10-08 23:55:04 +04:00
|
|
|
if (this->info.Error)
|
|
|
|
{
|
|
|
|
free(this->info.Error);
|
|
|
|
}
|
2002-08-21 19:58:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// cmLoadCommandCommand
|
2008-01-23 18:28:26 +03:00
|
|
|
bool cmLoadCommandCommand
|
|
|
|
::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
|
2002-08-21 19:58:48 +04:00
|
|
|
{
|
2002-12-12 02:13:33 +03:00
|
|
|
if(args.size() < 1 )
|
2002-08-21 19:58:48 +04:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2005-02-22 22:52:55 +03:00
|
|
|
|
|
|
|
// 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 19:02:08 +03:00
|
|
|
this->Makefile->RemoveDefinition(reportVar.c_str());
|
2005-02-22 22:52:55 +03:00
|
|
|
|
2002-08-21 19:58:48 +04:00
|
|
|
// the file must exist
|
2006-03-17 01:09:08 +03:00
|
|
|
std::string moduleName =
|
|
|
|
this->Makefile->GetRequiredDefinition("CMAKE_SHARED_MODULE_PREFIX");
|
|
|
|
moduleName += "cm" + args[0];
|
|
|
|
moduleName +=
|
|
|
|
this->Makefile->GetRequiredDefinition("CMAKE_SHARED_MODULE_SUFFIX");
|
2002-08-21 19:58:48 +04: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);
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-08-21 19:58:48 +04:00
|
|
|
// Glob the entry in case of wildcards.
|
|
|
|
cmSystemTools::GlobDirs(exp.c_str(), path);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find the program.
|
2006-03-17 01:09:08 +03:00
|
|
|
std::string fullPath = cmSystemTools::FindFile(moduleName.c_str(), path);
|
2002-08-21 19:58:48 +04:00
|
|
|
if (fullPath == "")
|
|
|
|
{
|
2006-03-17 01:09:08 +03:00
|
|
|
cmOStringStream e;
|
|
|
|
e << "Attempt to load command failed from file \""
|
|
|
|
<< moduleName << "\"";
|
|
|
|
this->SetError(e.str().c_str());
|
2002-08-21 19:58:48 +04:00
|
|
|
return false;
|
|
|
|
}
|
2002-09-23 21:11:39 +04:00
|
|
|
|
2002-08-21 19:58:48 +04:00
|
|
|
// try loading the shared library / dll
|
2006-03-16 19:01:05 +03:00
|
|
|
cmsys::DynamicLoader::LibraryHandle lib
|
|
|
|
= cmDynamicLoader::OpenLibrary(fullPath.c_str());
|
2002-10-15 19:45:04 +04:00
|
|
|
if(!lib)
|
|
|
|
{
|
|
|
|
std::string err = "Attempt to load the library ";
|
2003-08-19 18:29:59 +04:00
|
|
|
err += fullPath + " failed.";
|
2006-03-16 19:01:05 +03:00
|
|
|
const char* error = cmsys::DynamicLoader::LastError();
|
2003-08-19 19:02:56 +04:00
|
|
|
if ( error )
|
2003-08-19 18:29:59 +04:00
|
|
|
{
|
|
|
|
err += " Additional error info is:\n";
|
2003-08-19 19:02:56 +04:00
|
|
|
err += error;
|
2003-08-19 18:29:59 +04:00
|
|
|
}
|
2002-10-15 19:45:04 +04:00
|
|
|
this->SetError(err.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
2005-02-22 22:52:55 +03:00
|
|
|
|
|
|
|
// Report what file was loaded for this command.
|
2006-03-15 19:02:08 +03:00
|
|
|
this->Makefile->AddDefinition(reportVar.c_str(), fullPath.c_str());
|
2005-02-22 22:52:55 +03:00
|
|
|
|
2002-10-15 19:45:04 +04:00
|
|
|
// find the init function
|
|
|
|
std::string initFuncName = args[0] + "Init";
|
|
|
|
CM_INIT_FUNCTION initFunction
|
|
|
|
= (CM_INIT_FUNCTION)
|
2006-03-16 19:01:05 +03:00
|
|
|
cmsys::DynamicLoader::GetSymbolAddress(lib, initFuncName.c_str());
|
2007-04-20 17:49:27 +04:00
|
|
|
if ( !initFunction )
|
|
|
|
{
|
|
|
|
initFuncName = "_";
|
|
|
|
initFuncName += args[0];
|
|
|
|
initFuncName += "Init";
|
|
|
|
initFunction = (CM_INIT_FUNCTION)(
|
|
|
|
cmsys::DynamicLoader::GetSymbolAddress(lib, initFuncName.c_str()));
|
|
|
|
}
|
2012-08-13 21:42:58 +04:00
|
|
|
// if the symbol is found call it to set the name on the
|
2002-10-15 19:45:04 +04:00
|
|
|
// function blocker
|
|
|
|
if(initFunction)
|
|
|
|
{
|
|
|
|
// create a function blocker and set it up
|
|
|
|
cmLoadedCommand *f = new cmLoadedCommand();
|
|
|
|
(*initFunction)(&f->info);
|
2006-03-15 19:02:08 +03:00
|
|
|
this->Makefile->AddCommand(f);
|
2002-10-15 19:45:04 +04:00
|
|
|
return true;
|
2002-08-21 19:58:48 +04:00
|
|
|
}
|
2002-10-15 19:45:04 +04:00
|
|
|
this->SetError("Attempt to load command failed. "
|
|
|
|
"No init function found.");
|
2002-09-23 21:11:39 +04:00
|
|
|
return false;
|
2002-08-21 19:58:48 +04:00
|
|
|
}
|
|
|
|
|