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"
|
2016-04-29 16:40:20 +03:00
|
|
|
|
2002-08-26 18:52:04 +04:00
|
|
|
#include "cmCPluginAPI.cxx"
|
2016-04-29 17:53:13 +03:00
|
|
|
#include "cmCPluginAPI.h"
|
2002-08-26 18:52:04 +04:00
|
|
|
#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__
|
2016-05-16 17:34:04 +03:00
|
|
|
#include <malloc.h> /* for malloc/free on QNX */
|
2006-08-01 22:31:24 +04:00
|
|
|
#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
|
|
|
|
|
|
|
// a class for loadabple commands
|
|
|
|
class cmLoadedCommand : public cmCommand
|
|
|
|
{
|
|
|
|
public:
|
2016-05-16 17:34:04 +03:00
|
|
|
cmLoadedCommand()
|
|
|
|
{
|
|
|
|
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
|
2016-06-27 22:25:27 +03:00
|
|
|
~cmLoadedCommand() CM_OVERRIDE;
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2002-08-21 19:58:48 +04:00
|
|
|
/**
|
|
|
|
* This is a virtual constructor for the command.
|
|
|
|
*/
|
2016-06-27 22:25:27 +03:00
|
|
|
cmCommand* Clone() CM_OVERRIDE
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
|
|
|
cmLoadedCommand* newC = new cmLoadedCommand;
|
|
|
|
// we must copy when we clone
|
|
|
|
memcpy(&newC->info, &this->info, sizeof(info));
|
|
|
|
return newC;
|
|
|
|
}
|
2002-08-21 19:58:48 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is called when the command is first encountered in
|
|
|
|
* the CMakeLists.txt file.
|
|
|
|
*/
|
2016-06-27 22:25:27 +03:00
|
|
|
bool InitialPass(std::vector<std::string> const& args,
|
|
|
|
cmExecutionStatus&) CM_OVERRIDE;
|
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.
|
|
|
|
*/
|
2016-06-27 22:25:27 +03:00
|
|
|
void FinalPass() CM_OVERRIDE;
|
|
|
|
bool HasFinalPass() const CM_OVERRIDE
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
|
|
|
return this->info.FinalPass ? true : false;
|
|
|
|
}
|
2002-08-21 19:58:48 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the command as specified in CMakeList.txt.
|
|
|
|
*/
|
2016-06-27 22:25:27 +03:00
|
|
|
std::string GetName() const CM_OVERRIDE { 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)
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
|
|
|
fprintf(stderr, "CMake loaded command %s crashed with signal: %d.\n",
|
|
|
|
cmLoadedCommand::LastName, sig);
|
|
|
|
}
|
2004-04-27 01:45:53 +04:00
|
|
|
static void InstallSignalHandlers(const char* name, int remove = 0)
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
|
|
|
cmLoadedCommand::LastName = name;
|
|
|
|
if (!name) {
|
|
|
|
cmLoadedCommand::LastName = "????";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!remove) {
|
|
|
|
signal(SIGSEGV, TrapsForSignalsCFunction);
|
2004-04-27 01:45:53 +04:00
|
|
|
#ifdef SIGBUS
|
2016-05-16 17:34:04 +03:00
|
|
|
signal(SIGBUS, TrapsForSignalsCFunction);
|
2004-04-27 01:45:53 +04:00
|
|
|
#endif
|
2016-05-16 17:34:04 +03:00
|
|
|
signal(SIGILL, TrapsForSignalsCFunction);
|
|
|
|
} else {
|
|
|
|
signal(SIGSEGV, 0);
|
2004-04-27 01:45:53 +04:00
|
|
|
#ifdef SIGBUS
|
2016-05-16 17:34:04 +03:00
|
|
|
signal(SIGBUS, 0);
|
2004-04-27 01:45:53 +04:00
|
|
|
#endif
|
2016-05-16 17:34:04 +03:00
|
|
|
signal(SIGILL, 0);
|
2004-04-27 01:45:53 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
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,
|
2016-05-16 17:34:04 +03:00
|
|
|
cmExecutionStatus&)
|
2002-08-21 19:58:48 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!info.InitialPass) {
|
2002-08-21 19:58:48 +04:00
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-10-08 23:55:04 +04:00
|
|
|
|
|
|
|
// clear the error string
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->info.Error) {
|
2002-10-08 23:55:04 +04:00
|
|
|
free(this->info.Error);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
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
|
2016-05-16 17:34:04 +03:00
|
|
|
int argc = static_cast<int>(args.size());
|
|
|
|
char** argv = 0;
|
|
|
|
if (argc) {
|
|
|
|
argv = (char**)malloc(argc * sizeof(char*));
|
|
|
|
}
|
2002-08-21 19:58:48 +04:00
|
|
|
int i;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (i = 0; i < argc; ++i) {
|
2002-08-21 19:58:48 +04:00
|
|
|
argv[i] = strdup(args[i].c_str());
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2004-04-27 01:45:53 +04:00
|
|
|
cmLoadedCommand::InstallSignalHandlers(info.Name);
|
2016-05-16 17:34:04 +03:00
|
|
|
int result =
|
|
|
|
info.InitialPass((void*)&info, (void*)this->Makefile, argc, argv);
|
2004-04-27 01:45:53 +04:00
|
|
|
cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
|
2016-05-16 17:34:04 +03:00
|
|
|
cmFreeArguments(argc, argv);
|
2012-08-13 21:42:58 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (result) {
|
2002-08-21 19:58:48 +04:00
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-10-08 23:55:04 +04:00
|
|
|
|
|
|
|
/* Initial Pass must have failed so set the error string */
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->info.Error) {
|
2002-10-08 23:55:04 +04:00
|
|
|
this->SetError(this->info.Error);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-08-21 19:58:48 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmLoadedCommand::FinalPass()
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->info.FinalPass) {
|
2004-04-27 01:45:53 +04:00
|
|
|
cmLoadedCommand::InstallSignalHandlers(info.Name);
|
2016-05-16 17:34:04 +03:00
|
|
|
this->info.FinalPass((void*)&this->info, (void*)this->Makefile);
|
2004-04-27 01:45:53 +04:00
|
|
|
cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-09-17 18:38:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
cmLoadedCommand::~cmLoadedCommand()
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->info.Destructor) {
|
2004-04-27 01:45:53 +04:00
|
|
|
cmLoadedCommand::InstallSignalHandlers(info.Name);
|
2016-05-16 17:34:04 +03:00
|
|
|
this->info.Destructor((void*)&this->info);
|
2004-04-27 01:45:53 +04:00
|
|
|
cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (this->info.Error) {
|
2002-10-08 23:55:04 +04:00
|
|
|
free(this->info.Error);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-08-21 19:58:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// cmLoadCommandCommand
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmLoadCommandCommand::InitialPass(std::vector<std::string> const& args,
|
|
|
|
cmExecutionStatus&)
|
2002-08-21 19:58:48 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->Disallowed(
|
|
|
|
cmPolicies::CMP0031,
|
|
|
|
"The load_command command should not be called; see CMP0031.")) {
|
2002-08-21 19:58:48 +04:00
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (args.size() < 1) {
|
|
|
|
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];
|
2014-03-11 03:04:11 +04:00
|
|
|
this->Makefile->RemoveDefinition(reportVar);
|
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;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (unsigned int j = 1; j < args.size(); j++) {
|
2002-08-21 19:58:48 +04:00
|
|
|
// 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.
|
2014-03-11 03:04:11 +04:00
|
|
|
cmSystemTools::GlobDirs(exp, path);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-08-21 19:58:48 +04:00
|
|
|
|
|
|
|
// Try to find the program.
|
2006-03-17 01:09:08 +03:00
|
|
|
std::string fullPath = cmSystemTools::FindFile(moduleName.c_str(), path);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (fullPath == "") {
|
2015-01-05 22:31:31 +03:00
|
|
|
std::ostringstream e;
|
2016-05-16 17:34:04 +03:00
|
|
|
e << "Attempt to load command failed from file \"" << moduleName << "\"";
|
2014-03-11 03:04:11 +04:00
|
|
|
this->SetError(e.str());
|
2002-08-21 19:58:48 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2002-09-23 21:11:39 +04:00
|
|
|
|
2002-08-21 19:58:48 +04:00
|
|
|
// try loading the shared library / dll
|
2016-05-16 17:34:04 +03:00
|
|
|
cmsys::DynamicLoader::LibraryHandle lib =
|
|
|
|
cmDynamicLoader::OpenLibrary(fullPath.c_str());
|
|
|
|
if (!lib) {
|
2002-10-15 19:45:04 +04:00
|
|
|
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();
|
2016-05-16 17:34:04 +03: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;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-03-11 03:04:11 +04:00
|
|
|
this->SetError(err);
|
2002-10-15 19:45:04 +04:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2005-02-22 22:52:55 +03:00
|
|
|
|
|
|
|
// Report what file was loaded for this command.
|
2014-03-11 03:04:11 +04:00
|
|
|
this->Makefile->AddDefinition(reportVar, 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";
|
2016-05-16 17:34:04 +03:00
|
|
|
CM_INIT_FUNCTION initFunction =
|
|
|
|
(CM_INIT_FUNCTION)cmsys::DynamicLoader::GetSymbolAddress(
|
|
|
|
lib, initFuncName.c_str());
|
|
|
|
if (!initFunction) {
|
2007-04-20 17:49:27 +04:00
|
|
|
initFuncName = "_";
|
|
|
|
initFuncName += args[0];
|
|
|
|
initFuncName += "Init";
|
|
|
|
initFunction = (CM_INIT_FUNCTION)(
|
|
|
|
cmsys::DynamicLoader::GetSymbolAddress(lib, initFuncName.c_str()));
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
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
|
2016-05-16 17:34:04 +03:00
|
|
|
if (initFunction) {
|
2002-10-15 19:45:04 +04:00
|
|
|
// create a function blocker and set it up
|
2016-05-16 17:34:04 +03:00
|
|
|
cmLoadedCommand* f = new cmLoadedCommand();
|
2002-10-15 19:45:04 +04:00
|
|
|
(*initFunction)(&f->info);
|
2015-04-11 13:52:14 +03:00
|
|
|
this->Makefile->GetState()->AddCommand(f);
|
2002-10-15 19:45:04 +04:00
|
|
|
return true;
|
2016-05-16 17:34:04 +03: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
|
|
|
}
|