CMake/Source/cmLoadCommandCommand.cxx

236 lines
5.7 KiB
C++
Raw Normal View History

2002-08-21 19:58:48 +04:00
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile$
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) 2002 Insight Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
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 18:52:04 +04:00
#include "cmCPluginAPI.cxx"
#include "cmDynamicLoader.h"
2002-08-21 19:58:48 +04:00
// a class for loadabple commands
class cmLoadedCommand : public cmCommand
{
public:
cmLoadedCommand() {
2002-08-26 18:52:04 +04:00
memset(&this->info,0,sizeof(this->info));
this->info.CAPI = &cmStaticCAPI;
}
2002-08-21 19:58:48 +04:00
2002-09-17 18:38:00 +04:00
///! clean up any memory allocated by the plugin
~cmLoadedCommand();
2002-08-21 19:58:48 +04: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();
/**
* This determines if the command gets propagated down
* to makefiles located in subdirectories.
*/
virtual bool IsInherited() {
return (info.m_Inherited != 0 ? true : false);
}
/**
* The name of the command as specified in CMakeList.txt.
*/
2002-09-23 22:57:24 +04:00
virtual const char* GetName() { return info.Name; }
2002-08-21 19:58:48 +04:00
/**
* Succinct documentation.
*/
virtual const char* GetTerseDocumentation()
{
if (this->info.GetTerseDocumentation)
{
return info.GetTerseDocumentation();
}
else
{
return "LoadedCommand without any additional documentation";
}
}
/**
* More documentation.
*/
virtual const char* GetFullDocumentation()
{
if (this->info.GetFullDocumentation)
{
return info.GetFullDocumentation();
}
else
{
return "LoadedCommand without any additional documentation";
}
}
cmTypeMacro(cmLoadedCommand, cmCommand);
cmLoadedCommandInfo info;
};
bool cmLoadedCommand::InitialPass(std::vector<std::string> const& args)
{
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);
}
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());
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());
}
int result = info.InitialPass((void *)&info,(void *)this->m_Makefile,argc,argv);
cmFreeArguments(argc,argv);
2002-10-08 23:55:04 +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)
{
this->info.FinalPass((void *)&this->info,(void *)this->m_Makefile);
}
2002-09-17 18:38:00 +04:00
}
cmLoadedCommand::~cmLoadedCommand()
{
if (this->info.Destructor)
{
this->info.Destructor((void *)&this->info);
}
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
bool cmLoadCommandCommand::InitialPass(std::vector<std::string> const& argsIn)
{
if(argsIn.size() < 1 )
{
return true;
}
// the file must exist
std::string fullPath = cmDynamicLoader::LibPrefix();
fullPath += "cm" + argsIn[0] + cmDynamicLoader::LibExtension();
2002-08-21 19:58:48 +04:00
std::vector<std::string> args;
cmSystemTools::ExpandListArguments(argsIn, args);
// 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 == "")
{
this->SetError("Attempt to load command failed.");
return false;
}
2002-09-23 21:11:39 +04:00
2002-08-21 19:58:48 +04:00
// try loading the shared library / dll
cmLibHandle lib = cmDynamicLoader::OpenLibrary(fullPath.c_str());
if(lib)
{
2002-10-08 23:55:04 +04:00
// find the init function
std::string initFuncName = args[0] + "Init";
2002-08-21 19:58:48 +04:00
CM_INIT_FUNCTION initFunction
= (CM_INIT_FUNCTION)
2002-10-08 23:55:04 +04:00
cmDynamicLoader::GetSymbolAddress(lib, initFuncName.c_str());
2002-09-23 21:11:39 +04:00
if ( !initFunction )
{
2002-10-08 23:55:04 +04:00
initFuncName = "_";
initFuncName += args[0];
initFuncName += "Init";
initFunction = (CM_INIT_FUNCTION)(
cmDynamicLoader::GetSymbolAddress(lib, initFuncName.c_str()));
2002-09-23 21:11:39 +04:00
}
2002-08-21 19:58:48 +04:00
// if the symbol is found call it to set the name on the
// function blocker
2002-09-23 22:57:24 +04:00
if(initFunction)
2002-08-21 19:58:48 +04:00
{
// create a function blocker and set it up
cmLoadedCommand *f = new cmLoadedCommand();
(*initFunction)(&f->info);
m_Makefile->AddCommand(f);
2002-09-23 21:11:39 +04:00
return true;
2002-08-21 19:58:48 +04:00
}
2002-09-24 18:30:57 +04:00
this->SetError("Attempt to load command failed. "
"No init function found.");
2002-08-21 19:58:48 +04:00
}
2002-09-23 21:11:39 +04:00
return false;
2002-08-21 19:58:48 +04:00
}