CMake/Source/cmLoadCommandCommand.cxx

257 lines
6.8 KiB
C++
Raw Normal View History

Simplify CMake per-source license notices Per-source copyright/license notice headers that spell out copyright holder names and years are hard to maintain and often out-of-date or plain wrong. Precise contributor information is already maintained automatically by the version control tool. Ultimately it is the receiver of a file who is responsible for determining its licensing status, and per-source notices are merely a convenience. Therefore it is simpler and more accurate for each source to have a generic notice of the license name and references to more detailed information on copyright holders and full license terms. Our `Copyright.txt` file now contains a list of Contributors whose names appeared source-level copyright notices. It also references version control history for more precise information. Therefore we no longer need to spell out the list of Contributors in each source file notice. Replace CMake per-source copyright/license notice headers with a short description of the license and links to `Copyright.txt` and online information available from "https://cmake.org/licensing". The online URL also handles cases of modules being copied out of our source into other projects, so we can drop our notices about replacing links with full license text. Run the `Utilities/Scripts/filter-notices.bash` script to perform the majority of the replacements mechanically. Manually fix up shebang lines and trailing newlines in a few files. Manually update the notices in a few files that the script does not handle.
2016-09-27 22:01:08 +03:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
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 {
2016-06-27 23:44:16 +03:00
signal(SIGSEGV, CM_NULLPTR);
#ifdef SIGBUS
2016-06-27 23:44:16 +03:00
signal(SIGBUS, CM_NULLPTR);
#endif
2016-06-27 23:44:16 +03:00
signal(SIGILL, CM_NULLPTR);
}
}
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);
}
2016-06-27 23:44:16 +03:00
const char* cmLoadedCommand::LastName = CM_NULLPTR;
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());
2016-06-27 23:44:16 +03:00
char** argv = CM_NULLPTR;
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.empty()) {
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
}