CMake/Source/cmCPluginAPI.cxx

688 lines
18 KiB
C++
Raw Normal View History

2002-08-21 15:54:06 +00:00
/*=========================================================================
Program: CMake - Cross-Platform Makefile Generator
2002-08-21 15:54:06 +00:00
Module: $RCSfile$
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
2002-08-21 15:54:06 +00:00
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.
=========================================================================*/
/*
this file contains the implementation of the C API to CMake. Generally
these routines just manipulate arguments and then call the associated
methods on the CMake classes. */
#include "cmMakefile.h"
#include "cmCPluginAPI.h"
2004-02-22 18:14:59 +00:00
#include "cmSourceFile.h"
2002-08-29 13:45:34 +00:00
extern "C"
{
2002-08-21 15:54:06 +00:00
void *cmGetClientData(void *info)
{
return ((cmLoadedCommandInfo *)info)->ClientData;
}
void cmSetClientData(void *info, void *cd)
{
((cmLoadedCommandInfo *)info)->ClientData = cd;
}
2002-10-08 19:55:04 +00:00
void cmSetError(void *info, const char *err)
{
if (((cmLoadedCommandInfo *)info)->Error)
{
free(((cmLoadedCommandInfo *)info)->Error);
}
((cmLoadedCommandInfo *)info)->Error = strdup(err);
}
unsigned int cmGetCacheMajorVersion(void *arg)
2002-08-21 15:54:06 +00:00
{
cmMakefile *mf = static_cast<cmMakefile *>(arg);
return mf->GetCacheMajorVersion();
2002-08-21 15:54:06 +00:00
}
unsigned int cmGetCacheMinorVersion(void *arg)
2002-08-21 15:54:06 +00:00
{
cmMakefile *mf = static_cast<cmMakefile *>(arg);
return mf->GetCacheMinorVersion();
2002-08-21 15:54:06 +00:00
}
unsigned int cmGetMajorVersion(void *)
{
return cmMakefile::GetMajorVersion();
}
unsigned int cmGetMinorVersion(void *)
{
return cmMakefile::GetMinorVersion();
}
void cmAddDefinition(void *arg, const char* name, const char* value)
{
cmMakefile *mf = static_cast<cmMakefile *>(arg);
mf->AddDefinition(name,value);
}
/* Add a definition to this makefile and the global cmake cache. */
void cmAddCacheDefinition(void *arg, const char* name, const char* value,
const char* doc,
int type)
{
cmMakefile *mf = static_cast<cmMakefile *>(arg);
switch (type)
{
case CM_CACHE_BOOL:
mf->AddCacheDefinition(name,value,doc,
2002-08-21 17:16:07 +00:00
cmCacheManager::BOOL);
2002-08-21 15:54:06 +00:00
break;
case CM_CACHE_PATH:
mf->AddCacheDefinition(name,value,doc,
2002-08-21 17:16:07 +00:00
cmCacheManager::PATH);
2002-08-21 15:54:06 +00:00
break;
case CM_CACHE_FILEPATH:
mf->AddCacheDefinition(name,value,doc,
2002-08-21 17:16:07 +00:00
cmCacheManager::FILEPATH);
2002-08-21 15:54:06 +00:00
break;
case CM_CACHE_STRING:
mf->AddCacheDefinition(name,value,doc,
2002-08-21 17:16:07 +00:00
cmCacheManager::STRING);
2002-08-21 15:54:06 +00:00
break;
case CM_CACHE_INTERNAL:
mf->AddCacheDefinition(name,value,doc,
2002-08-21 17:16:07 +00:00
cmCacheManager::INTERNAL);
2002-08-21 15:54:06 +00:00
break;
case CM_CACHE_STATIC:
mf->AddCacheDefinition(name,value,doc,
2002-08-21 17:16:07 +00:00
cmCacheManager::STATIC);
2002-08-21 15:54:06 +00:00
break;
}
}
const char* cmGetProjectName(void *arg)
{
cmMakefile *mf = static_cast<cmMakefile *>(arg);
return mf->GetProjectName();
}
const char* cmGetHomeDirectory(void *arg)
{
cmMakefile *mf = static_cast<cmMakefile *>(arg);
return mf->GetHomeDirectory();
}
const char* cmGetHomeOutputDirectory(void *arg)
{
cmMakefile *mf = static_cast<cmMakefile *>(arg);
return mf->GetHomeOutputDirectory();
}
const char* cmGetStartDirectory(void *arg)
{
cmMakefile *mf = static_cast<cmMakefile *>(arg);
return mf->GetStartDirectory();
}
const char* cmGetStartOutputDirectory(void *arg)
{
cmMakefile *mf = static_cast<cmMakefile *>(arg);
return mf->GetStartOutputDirectory();
}
const char* cmGetCurrentDirectory(void *arg)
{
cmMakefile *mf = static_cast<cmMakefile *>(arg);
return mf->GetCurrentDirectory();
}
const char* cmGetCurrentOutputDirectory(void *arg)
{
cmMakefile *mf = static_cast<cmMakefile *>(arg);
return mf->GetCurrentOutputDirectory();
}
const char* cmGetDefinition(void *arg,const char*def)
{
cmMakefile *mf = static_cast<cmMakefile *>(arg);
return mf->GetDefinition(def);
}
int cmIsOn(void *arg, const char* name)
{
cmMakefile *mf = static_cast<cmMakefile *>(arg);
return static_cast<int>(mf->IsOn(name));
}
/** Check if a command exists. */
int cmCommandExists(void *arg, const char* name)
{
cmMakefile *mf = static_cast<cmMakefile *>(arg);
return static_cast<int>(mf->CommandExists(name));
}
void cmAddDefineFlag(void *arg, const char* definition)
{
cmMakefile *mf = static_cast<cmMakefile *>(arg);
mf->AddDefineFlag(definition);
}
void cmAddLinkDirectoryForTarget(void *arg, const char *tgt, const char* d)
{
cmMakefile *mf = static_cast<cmMakefile *>(arg);
mf->AddLinkDirectoryForTarget(tgt,d);
}
void cmAddExecutable(void *arg, const char *exename,
int numSrcs, const char **srcs, int win32)
{
cmMakefile *mf = static_cast<cmMakefile *>(arg);
std::vector<std::string> srcs2;
int i;
for (i = 0; i < numSrcs; ++i)
{
srcs2.push_back(srcs[i]);
}
cmTarget* tg = mf->AddExecutable(exename, srcs2);
if ( win32 )
{
tg->SetProperty("WIN32_EXECUTABLE", "ON");
}
2002-08-21 15:54:06 +00:00
}
void cmAddUtilityCommand(void *arg, const char* utilityName,
const char* command,
const char* arguments,
int all,
int numDepends,
const char **depends,
int numOutputs,
const char **outputs)
{
// Get the makefile instance. Perform an extra variable expansion
// now because the API caller expects it.
cmMakefile* mf = static_cast<cmMakefile*>(arg);
// Construct the command line for the command.
cmCustomCommandLine commandLine;
std::string expand = command;
commandLine.push_back(mf->ExpandVariablesInString(expand));
if(arguments && arguments[0])
{
// TODO: Parse arguments!
expand = arguments;
commandLine.push_back(mf->ExpandVariablesInString(expand));
}
cmCustomCommandLines commandLines;
commandLines.push_back(commandLine);
// Accumulate the list of dependencies.
2002-08-21 15:54:06 +00:00
std::vector<std::string> depends2;
for(int i = 0; i < numDepends; ++i)
2002-08-21 15:54:06 +00:00
{
expand = depends[i];
depends2.push_back(mf->ExpandVariablesInString(expand));
2002-08-21 15:54:06 +00:00
}
// Only one output is allowed.
const char* output = 0;
std::string outputStr;
if(numOutputs > 0)
2002-08-21 15:54:06 +00:00
{
expand = outputs[0];
outputStr = mf->ExpandVariablesInString(expand);
output = outputStr.c_str();
2002-08-21 15:54:06 +00:00
}
// Pass the call to the makefile instance.
mf->AddUtilityCommand(utilityName, (all ? true : false),
output, depends2, commandLines);
2002-08-21 15:54:06 +00:00
}
void cmAddCustomCommand(void *arg, const char* source,
const char* command,
int numArgs, const char **args,
int numDepends, const char **depends,
int numOutputs, const char **outputs,
const char *target)
{
// Get the makefile instance. Perform an extra variable expansion
// now because the API caller expects it.
cmMakefile* mf = static_cast<cmMakefile*>(arg);
// Construct the command line for the command.
cmCustomCommandLine commandLine;
std::string expand = command;
commandLine.push_back(mf->ExpandVariablesInString(expand));
for(int i=0; i < numArgs; ++i)
2002-08-21 15:54:06 +00:00
{
expand = args[i];
commandLine.push_back(mf->ExpandVariablesInString(expand));
2002-08-21 15:54:06 +00:00
}
cmCustomCommandLines commandLines;
commandLines.push_back(commandLine);
// Accumulate the list of dependencies.
2002-08-21 15:54:06 +00:00
std::vector<std::string> depends2;
for(int i = 0; i < numDepends; ++i)
2002-08-21 15:54:06 +00:00
{
expand = depends[i];
depends2.push_back(mf->ExpandVariablesInString(expand));
2002-08-21 15:54:06 +00:00
}
// Accumulate the list of outputs.
2002-08-21 15:54:06 +00:00
std::vector<std::string> outputs2;
for(int i = 0; i < numOutputs; ++i)
2002-08-21 15:54:06 +00:00
{
expand = outputs[i];
outputs2.push_back(mf->ExpandVariablesInString(expand));
2002-08-21 15:54:06 +00:00
}
// Pass the call to the makefile instance.
const char* no_comment = 0;
mf->AddCustomCommandOldStyle(target, outputs2, depends2, source,
commandLines, no_comment);
2002-08-21 15:54:06 +00:00
}
2003-06-03 14:30:23 +00:00
void cmAddCustomCommandToOutput(void *arg, const char* output,
const char* command,
int numArgs, const char **args,
const char* main_dependency,
int numDepends, const char **depends)
{
// Get the makefile instance. Perform an extra variable expansion
// now because the API caller expects it.
cmMakefile* mf = static_cast<cmMakefile*>(arg);
// Construct the command line for the command.
cmCustomCommandLine commandLine;
std::string expand = command;
commandLine.push_back(mf->ExpandVariablesInString(expand));
for(int i=0; i < numArgs; ++i)
2003-06-03 14:30:23 +00:00
{
expand = args[i];
commandLine.push_back(mf->ExpandVariablesInString(expand));
2003-06-03 14:30:23 +00:00
}
cmCustomCommandLines commandLines;
commandLines.push_back(commandLine);
// Accumulate the list of dependencies.
2003-06-03 14:30:23 +00:00
std::vector<std::string> depends2;
for(int i = 0; i < numDepends; ++i)
2003-06-03 14:30:23 +00:00
{
expand = depends[i];
depends2.push_back(mf->ExpandVariablesInString(expand));
2003-06-03 14:30:23 +00:00
}
// Pass the call to the makefile instance.
const char* no_comment = 0;
mf->AddCustomCommandToOutput(output, depends2, main_dependency,
commandLines, no_comment);
2003-06-03 14:30:23 +00:00
}
void cmAddCustomCommandToTarget(void *arg, const char* target,
const char* command,
int numArgs, const char **args,
int commandType)
{
// Get the makefile instance.
cmMakefile* mf = static_cast<cmMakefile*>(arg);
// Construct the command line for the command. Perform an extra
// variable expansion now because the API caller expects it.
cmCustomCommandLine commandLine;
std::string expand = command;
commandLine.push_back(mf->ExpandVariablesInString(expand));
for(int i=0; i < numArgs; ++i)
2003-06-03 14:30:23 +00:00
{
expand = args[i];
commandLine.push_back(mf->ExpandVariablesInString(expand));
2003-06-03 14:30:23 +00:00
}
cmCustomCommandLines commandLines;
commandLines.push_back(commandLine);
// Select the command type.
cmTarget::CustomCommandType cctype = cmTarget::POST_BUILD;
2003-06-03 14:30:23 +00:00
switch (commandType)
{
case CM_PRE_BUILD:
cctype = cmTarget::PRE_BUILD;
2003-06-03 14:30:23 +00:00
break;
case CM_PRE_LINK:
cctype = cmTarget::PRE_LINK;
2003-06-03 14:30:23 +00:00
break;
case CM_POST_BUILD:
cctype = cmTarget::POST_BUILD;
2003-06-03 14:30:23 +00:00
break;
}
// Pass the call to the makefile instance.
std::vector<std::string> no_depends;
const char* no_comment = 0;
mf->AddCustomCommandToTarget(target, no_depends, commandLines,
cctype, no_comment);
2003-06-03 14:30:23 +00:00
}
2002-08-21 15:54:06 +00:00
void cmAddLinkLibraryForTarget(void *arg, const char *tgt, const char*value,
int libtype)
{
cmMakefile *mf = static_cast<cmMakefile *>(arg);
switch (libtype)
{
case CM_LIBRARY_GENERAL:
2002-08-21 17:16:07 +00:00
mf->AddLinkLibraryForTarget(tgt,value, cmTarget::GENERAL);
2002-08-21 15:54:06 +00:00
break;
case CM_LIBRARY_DEBUG:
2002-08-21 17:16:07 +00:00
mf->AddLinkLibraryForTarget(tgt,value, cmTarget::DEBUG);
2002-08-21 15:54:06 +00:00
break;
case CM_LIBRARY_OPTIMIZED:
2002-08-21 17:16:07 +00:00
mf->AddLinkLibraryForTarget(tgt,value, cmTarget::OPTIMIZED);
2002-08-21 15:54:06 +00:00
break;
}
}
void cmAddLibrary(void *arg, const char *libname, int shared,
int numSrcs, const char **srcs)
{
cmMakefile *mf = static_cast<cmMakefile *>(arg);
std::vector<std::string> srcs2;
int i;
for (i = 0; i < numSrcs; ++i)
{
srcs2.push_back(srcs[i]);
}
mf->AddLibrary(libname, (shared ? true : false), srcs2);
}
char *cmExpandVariablesInString(void *arg, const char *source,
int escapeQuotes, int atOnly)
{
cmMakefile *mf = static_cast<cmMakefile *>(arg);
2002-08-21 17:16:07 +00:00
std::string barf = source;
2002-08-21 15:54:06 +00:00
std::string result =
2002-08-21 17:16:07 +00:00
mf->ExpandVariablesInString(barf,
2002-08-21 15:54:06 +00:00
(escapeQuotes ? true : false),
(atOnly ? true : false));
char *res = static_cast<char *>(malloc(result.size() + 1));
if (result.size())
{
strcpy(res,result.c_str());
}
res[result.size()] = '\0';
return res;
}
int cmExecuteCommand(void *arg, const char *name,
int numArgs, const char **args)
2002-08-21 15:54:06 +00:00
{
cmMakefile *mf = static_cast<cmMakefile *>(arg);
cmListFileFunction lff;
lff.m_Name = name;
for(int i = 0; i < numArgs; ++i)
2002-08-21 15:54:06 +00:00
{
// Assume all arguments are quoted.
lff.m_Arguments.push_back(cmListFileArgument(args[i], true,
"[CMake-Plugin]", 0));
2002-08-21 15:54:06 +00:00
}
return mf->ExecuteCommand(lff);
2002-08-21 15:54:06 +00:00
}
void cmExpandSourceListArguments(void *arg,
int numArgs,
const char **args,
int *resArgc,
char ***resArgv,
unsigned int startArgumentIndex)
{
cmMakefile *mf = static_cast<cmMakefile *>(arg);
std::vector<std::string> result;
std::vector<std::string> args2;
int i;
for (i = 0; i < numArgs; ++i)
{
args2.push_back(args[i]);
}
mf->ExpandSourceListArguments(args2, result, startArgumentIndex);
2002-08-23 19:13:49 +00:00
int resargc = static_cast<int>(result.size());
char **resargv = 0;
2002-08-21 15:54:06 +00:00
if (resargc)
{
resargv = (char **)malloc(resargc*sizeof(char *));
}
for (i = 0; i < resargc; ++i)
{
resargv[i] = strdup(result[i].c_str());
}
*resArgc = resargc;
*resArgv = resargv;
}
void cmFreeArguments(int argc, char **argv)
{
int i;
for (i = 0; i < argc; ++i)
{
free(argv[i]);
}
if (argv)
{
free(argv);
}
}
int cmGetTotalArgumentSize(int argc, char **argv)
{
int i;
int result = 0;
for (i = 0; i < argc; ++i)
{
if (argv[i])
{
2002-08-23 19:13:49 +00:00
result = result + static_cast<int>(strlen(argv[i]));
2002-08-21 15:54:06 +00:00
}
}
return result;
}
void *cmGetSource(void *arg, const char *name)
{
cmMakefile *mf = static_cast<cmMakefile *>(arg);
return (void *)mf->GetSource(name);
}
void * cmAddSource(void *arg, void *arg2)
{
cmMakefile *mf = static_cast<cmMakefile *>(arg);
cmSourceFile *sf = static_cast<cmSourceFile *>(arg2);
return (void *)mf->AddSource(*sf);
}
void * cmCreateSourceFile()
{
return (void *)(new cmSourceFile);
}
2002-12-11 19:16:55 +00:00
void cmDestroySourceFile(void *arg)
{
cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
delete sf;
}
2002-08-21 15:54:06 +00:00
const char *cmSourceFileGetSourceName(void *arg)
{
cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
return sf->GetSourceName().c_str();
}
2002-11-14 16:03:08 +00:00
const char *cmSourceFileGetFullPath(void *arg)
{
cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
return sf->GetFullPath().c_str();
}
2002-08-21 15:54:06 +00:00
const char *cmSourceFileGetProperty(void *arg,const char *prop)
{
cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
return sf->GetProperty(prop);
}
int cmSourceFileGetPropertyAsBool(void *arg,const char *prop)
{
cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
return (sf->GetPropertyAsBool(prop) ? 1: 0);
}
void cmSourceFileSetProperty(void *arg,const char *prop, const char *val)
{
cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
sf->SetProperty(prop,val);
}
void cmSourceFileAddDepend(void *arg, const char *depend)
{
cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
sf->GetDepends().push_back(depend);
}
void cmSourceFileSetName(void *arg, const char* name, const char* dir,
int numSourceExtensions,
const char **sourceExtensions,
int numHeaderExtensions,
const char **headerExtensions)
{
cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
std::vector<std::string> srcs;
std::vector<std::string> hdrs;
int i;
for (i = 0; i < numSourceExtensions; ++i)
{
srcs.push_back(sourceExtensions[i]);
}
for (i = 0; i < numHeaderExtensions; ++i)
{
hdrs.push_back(headerExtensions[i]);
}
sf->SetName(name,dir, srcs, hdrs);
}
void cmSourceFileSetName2(void *arg, const char* name, const char* dir,
const char *ext, int headerFileOnly)
{
cmSourceFile *sf = static_cast<cmSourceFile *>(arg);
sf->SetName(name,dir,ext,(headerFileOnly ? true : false));
}
char *cmGetFilenameWithoutExtension(const char *name)
{
std::string sres = cmSystemTools::GetFilenameWithoutExtension(name);
char *result = (char *)malloc(sres.size()+1);
strcpy(result,sres.c_str());
return result;
}
2002-11-14 16:16:58 +00:00
char *cmGetFilenamePath(const char *name)
{
std::string sres = cmSystemTools::GetFilenamePath(name);
char *result = (char *)malloc(sres.size()+1);
strcpy(result,sres.c_str());
return result;
}
2002-08-21 15:54:06 +00:00
char *cmCapitalized(const char *name)
{
std::string sres = cmSystemTools::Capitalized(name);
char *result = (char *)malloc(sres.size()+1);
strcpy(result,sres.c_str());
return result;
}
void cmCopyFileIfDifferent(const char *name1, const char *name2)
{
cmSystemTools::CopyFileIfDifferent(name1,name2);
}
void cmRemoveFile(const char *name)
{
cmSystemTools::RemoveFile(name);
}
2003-07-22 17:14:55 +00:00
void cmDisplayStatus(void *arg, const char* message)
{
cmMakefile *mf = static_cast<cmMakefile *>(arg);
mf->DisplayStatus(message, -1);
2003-07-22 17:14:55 +00:00
}
2002-12-11 19:16:55 +00:00
void cmFree(void *data)
{
free(data);
}
2002-08-29 13:45:34 +00:00
} // close the extern "C" scope
2002-08-26 14:52:04 +00:00
cmCAPI cmStaticCAPI =
{
cmGetClientData,
2002-10-08 19:55:04 +00:00
cmGetTotalArgumentSize,
cmFreeArguments,
cmSetClientData,
cmSetError,
cmAddCacheDefinition,
cmAddCustomCommand,
cmAddDefineFlag,
cmAddDefinition,
cmAddExecutable,
cmAddLibrary,
cmAddLinkDirectoryForTarget,
cmAddLinkLibraryForTarget,
cmAddUtilityCommand,
cmCommandExists,
cmExecuteCommand,
cmExpandSourceListArguments,
cmExpandVariablesInString,
cmGetCacheMajorVersion,
cmGetCacheMinorVersion,
cmGetCurrentDirectory,
cmGetCurrentOutputDirectory,
cmGetDefinition,
cmGetHomeDirectory,
cmGetHomeOutputDirectory,
cmGetMajorVersion,
cmGetMinorVersion,
cmGetProjectName,
cmGetStartDirectory,
cmGetStartOutputDirectory,
cmIsOn,
cmAddSource,
cmCreateSourceFile,
2002-12-11 19:16:55 +00:00
cmDestroySourceFile,
2002-10-08 19:55:04 +00:00
cmGetSource,
cmSourceFileAddDepend,
cmSourceFileGetProperty,
cmSourceFileGetPropertyAsBool,
cmSourceFileGetSourceName,
2002-11-14 16:03:08 +00:00
cmSourceFileGetFullPath,
2002-10-08 19:55:04 +00:00
cmSourceFileSetName,
cmSourceFileSetName2,
cmSourceFileSetProperty,
2002-08-26 14:52:04 +00:00
2002-10-08 19:55:04 +00:00
cmCapitalized,
cmCopyFileIfDifferent,
cmGetFilenameWithoutExtension,
2002-11-14 16:16:58 +00:00
cmGetFilenamePath,
2002-10-08 19:55:04 +00:00
cmRemoveFile,
2002-12-11 19:16:55 +00:00
cmFree,
2003-06-03 14:30:23 +00:00
cmAddCustomCommandToOutput,
cmAddCustomCommandToTarget,
2003-07-23 18:31:30 +00:00
cmDisplayStatus,
2002-08-26 14:52:04 +00:00
};