CMake/Source/cmLoadCommandCommand.cxx

266 lines
7.2 KiB
C++
Raw Normal View History

/*============================================================================
CMake - Cross Platform Makefile Generator
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
2002-08-21 19:58:48 +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
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"
2002-08-26 18:52:04 +04:00
#include "cmCPluginAPI.cxx"
#include "cmCPluginAPI.h"
2002-08-26 18:52:04 +04:00
#include "cmDynamicLoader.h"
#include <cmsys/DynamicLoader.hxx>
#include <stdlib.h>
#ifdef __QNX__
#include <malloc.h> /* for malloc/free on QNX */
#endif
#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:
cmLoadedCommand()
{
memset(&this->info, 0, sizeof(this->info));
2002-08-26 18:52:04 +04:00
this->info.CAPI = &cmStaticCAPI;
}
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;
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
{
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
{
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; }
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) {
signal(SIGSEGV, TrapsForSignalsCFunction);
#ifdef SIGBUS
signal(SIGBUS, TrapsForSignalsCFunction);
#endif
signal(SIGILL, TrapsForSignalsCFunction);
} else {
signal(SIGSEGV, 0);
#ifdef SIGBUS
signal(SIGBUS, 0);
#endif
signal(SIGILL, 0);
}
}
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);
}
const char* cmLoadedCommand::LastName = 0;
bool cmLoadedCommand::InitialPass(std::vector<std::string> const& args,
cmExecutionStatus&)
2002-08-21 19:58:48 +04:00
{
if (!info.InitialPass) {
2002-08-21 19:58:48 +04:00
return true;
}
2002-10-08 23:55:04 +04:00
// clear the error string
if (this->info.Error) {
2002-10-08 23:55:04 +04:00
free(this->info.Error);
}
2002-08-21 19:58:48 +04:00
// create argc and argv and then invoke the command
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;
for (i = 0; i < argc; ++i) {
2002-08-21 19:58:48 +04:00
argv[i] = strdup(args[i].c_str());
}
cmLoadedCommand::InstallSignalHandlers(info.Name);
int result =
info.InitialPass((void*)&info, (void*)this->Makefile, argc, argv);
cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
cmFreeArguments(argc, argv);
if (result) {
2002-08-21 19:58:48 +04:00
return true;
}
2002-10-08 23:55:04 +04:00
/* Initial Pass must have failed so set the error string */
if (this->info.Error) {
2002-10-08 23:55:04 +04:00
this->SetError(this->info.Error);
}
2002-08-21 19:58:48 +04:00
return false;
}
void cmLoadedCommand::FinalPass()
{
if (this->info.FinalPass) {
cmLoadedCommand::InstallSignalHandlers(info.Name);
this->info.FinalPass((void*)&this->info, (void*)this->Makefile);
cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
}
2002-09-17 18:38:00 +04:00
}
cmLoadedCommand::~cmLoadedCommand()
{
if (this->info.Destructor) {
cmLoadedCommand::InstallSignalHandlers(info.Name);
this->info.Destructor((void*)&this->info);
cmLoadedCommand::InstallSignalHandlers(info.Name, 1);
}
if (this->info.Error) {
2002-10-08 23:55:04 +04:00
free(this->info.Error);
}
2002-08-21 19:58:48 +04:00
}
// cmLoadCommandCommand
bool cmLoadCommandCommand::InitialPass(std::vector<std::string> const& args,
cmExecutionStatus&)
2002-08-21 19:58:48 +04: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;
}
if (args.size() < 1) {
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];
this->Makefile->RemoveDefinition(reportVar);
2002-08-21 19:58:48 +04:00
// the file must exist
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++) {
2002-08-21 19:58:48 +04:00
// expand variables
std::string exp = args[j];
cmSystemTools::ExpandRegistryValues(exp);
2002-08-21 19:58:48 +04:00
// Glob the entry in case of wildcards.
cmSystemTools::GlobDirs(exp, path);
}
2002-08-21 19:58:48 +04:00
// Try to find the program.
std::string fullPath = cmSystemTools::FindFile(moduleName.c_str(), path);
if (fullPath == "") {
std::ostringstream e;
e << "Attempt to load command failed from file \"" << moduleName << "\"";
this->SetError(e.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
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 ";
err += fullPath + " failed.";
const char* error = cmsys::DynamicLoader::LastError();
if (error) {
err += " Additional error info is:\n";
err += error;
}
this->SetError(err);
2002-10-15 19:45:04 +04:00
return false;
}
// Report what file was loaded for this command.
this->Makefile->AddDefinition(reportVar, fullPath.c_str());
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)cmsys::DynamicLoader::GetSymbolAddress(
lib, initFuncName.c_str());
if (!initFunction) {
initFuncName = "_";
initFuncName += args[0];
initFuncName += "Init";
initFunction = (CM_INIT_FUNCTION)(
cmsys::DynamicLoader::GetSymbolAddress(lib, initFuncName.c_str()));
}
// 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) {
2002-10-15 19:45:04 +04:00
// create a function blocker and set it up
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;
}
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
}