2001-01-11 22:55:47 +03:00
|
|
|
/*=========================================================================
|
|
|
|
|
|
|
|
Program: Insight Segmentation & Registration Toolkit
|
|
|
|
Module: $RCSfile$
|
|
|
|
Language: C++
|
|
|
|
Date: $Date$
|
|
|
|
Version: $Revision$
|
|
|
|
|
|
|
|
|
|
|
|
Copyright (c) 2000 National Library of Medicine
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
See COPYRIGHT.txt for copyright details.
|
|
|
|
|
|
|
|
=========================================================================*/
|
2000-08-29 23:26:29 +04:00
|
|
|
#include "cmMakefile.h"
|
2001-01-18 19:20:24 +03:00
|
|
|
#include "cmCommand.h"
|
2001-01-05 19:41:20 +03:00
|
|
|
#include "cmStandardIncludes.h"
|
2000-08-29 23:26:29 +04:00
|
|
|
#include "cmClassFile.h"
|
2000-08-30 21:35:41 +04:00
|
|
|
#include "cmDirectory.h"
|
2000-09-12 13:30:35 +04:00
|
|
|
#include "cmSystemTools.h"
|
2001-01-05 19:41:20 +03:00
|
|
|
#include "cmMakefileGenerator.h"
|
2000-08-29 23:26:29 +04:00
|
|
|
|
2001-01-18 19:20:24 +03:00
|
|
|
#include "cmAbstractFilesCommand.h"
|
|
|
|
#include "cmAddTargetCommand.h"
|
|
|
|
#include "cmAuxSourceDirectoryCommand.h"
|
|
|
|
#include "cmExecutablesCommand.h"
|
|
|
|
#include "cmFindIncludeCommand.h"
|
|
|
|
#include "cmFindLibraryCommand.h"
|
|
|
|
#include "cmFindProgramCommand.h"
|
|
|
|
#include "cmIncludeDirectoryCommand.h"
|
|
|
|
#include "cmLibraryCommand.h"
|
|
|
|
#include "cmLinkDirectoriesCommand.h"
|
|
|
|
#include "cmLinkLibrariesCommand.h"
|
|
|
|
#include "cmProjectCommand.h"
|
|
|
|
#include "cmSourceFilesCommand.h"
|
|
|
|
#include "cmSourceFilesRequireCommand.h"
|
|
|
|
#include "cmSubdirCommand.h"
|
|
|
|
#include "cmUnixDefinesCommand.h"
|
|
|
|
#include "cmUnixLibrariesCommand.h"
|
|
|
|
#include "cmWin32DefinesCommand.h"
|
|
|
|
#include "cmWin32LibrariesCommand.h"
|
|
|
|
#include "cmTestsCommand.h"
|
2000-08-29 23:26:29 +04:00
|
|
|
|
|
|
|
// default is not to be building executables
|
|
|
|
cmMakefile::cmMakefile()
|
|
|
|
{
|
2001-01-05 19:41:20 +03:00
|
|
|
m_DefineFlags = " ";
|
2000-08-29 23:26:29 +04:00
|
|
|
m_Executables = false;
|
2001-01-05 19:41:20 +03:00
|
|
|
m_MakefileGenerator = 0;
|
2001-01-18 19:20:24 +03:00
|
|
|
this->AddDefaultCommands();
|
2001-01-05 19:41:20 +03:00
|
|
|
}
|
|
|
|
|
2001-01-18 19:20:24 +03:00
|
|
|
void cmMakefile::AddDefaultCommands()
|
2001-01-05 19:41:20 +03:00
|
|
|
{
|
2001-01-18 19:20:24 +03:00
|
|
|
this->AddCommand(new cmAbstractFilesCommand);
|
|
|
|
this->AddCommand(new cmAddTargetCommand);
|
|
|
|
this->AddCommand(new cmAuxSourceDirectoryCommand);
|
|
|
|
this->AddCommand(new cmExecutablesCommand);
|
|
|
|
this->AddCommand(new cmFindIncludeCommand);
|
|
|
|
this->AddCommand(new cmFindLibraryCommand);
|
|
|
|
this->AddCommand(new cmFindProgramCommand);
|
|
|
|
this->AddCommand(new cmIncludeDirectoryCommand);
|
|
|
|
this->AddCommand(new cmLibraryCommand);
|
|
|
|
this->AddCommand(new cmLinkDirectoriesCommand);
|
|
|
|
this->AddCommand(new cmLinkLibrariesCommand);
|
|
|
|
this->AddCommand(new cmProjectCommand);
|
|
|
|
this->AddCommand(new cmSourceFilesCommand);
|
|
|
|
this->AddCommand(new cmSourceFilesRequireCommand);
|
|
|
|
this->AddCommand(new cmSubdirCommand);
|
|
|
|
this->AddCommand(new cmUnixLibrariesCommand);
|
|
|
|
this->AddCommand(new cmUnixDefinesCommand);
|
|
|
|
this->AddCommand(new cmWin32LibrariesCommand);
|
|
|
|
this->AddCommand(new cmWin32DefinesCommand);
|
|
|
|
this->AddCommand(new cmTestsCommand);
|
2001-01-05 19:41:20 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
this->AddDefinition("WIN32", "1");
|
|
|
|
#else
|
|
|
|
this->AddDefinition("UNIX", "1");
|
|
|
|
#endif
|
2001-01-18 19:20:24 +03:00
|
|
|
// Cygwin is more like unix so enable the unix commands
|
2001-01-05 19:41:20 +03:00
|
|
|
#if defined(__CYGWIN__)
|
|
|
|
this->AddDefinition("UNIX", "1");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cmMakefile::~cmMakefile()
|
|
|
|
{
|
2001-01-18 19:20:24 +03:00
|
|
|
for(int i=0; i < m_UsedCommands.size(); i++)
|
2001-01-05 19:41:20 +03:00
|
|
|
{
|
2001-01-18 19:20:24 +03:00
|
|
|
delete m_UsedCommands[i];
|
2001-01-05 19:41:20 +03:00
|
|
|
}
|
2001-01-18 19:20:24 +03:00
|
|
|
for(RegisteredCommandsMap::iterator j = m_Commands.begin();
|
|
|
|
j != m_Commands.end(); ++j)
|
2001-01-05 19:41:20 +03:00
|
|
|
{
|
|
|
|
delete (*j).second;
|
|
|
|
}
|
|
|
|
delete m_MakefileGenerator;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmMakefile::PrintStringVector(const char* s, std::vector<std::string>& v)
|
|
|
|
{
|
|
|
|
std::cout << s << ": ( \n";
|
|
|
|
for(std::vector<std::string>::iterator i = v.begin();
|
|
|
|
i != v.end(); ++i)
|
|
|
|
{
|
|
|
|
std::cout << (*i).c_str() << " ";
|
|
|
|
}
|
|
|
|
std::cout << " )\n";
|
2000-08-29 23:26:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// call print on all the classes in the makefile
|
|
|
|
void cmMakefile::Print()
|
|
|
|
{
|
2001-01-05 19:41:20 +03:00
|
|
|
std::cout << "classes:\n";
|
|
|
|
for(unsigned int i = 0; i < m_Classes.size(); i++)
|
2000-08-29 23:26:29 +04:00
|
|
|
m_Classes[i].Print();
|
2001-01-05 19:41:20 +03:00
|
|
|
std::cout << " m_OutputDirectory; " <<
|
|
|
|
m_OutputDirectory.c_str() << std::endl;
|
|
|
|
std::cout << " m_OutputHomeDirectory; " <<
|
|
|
|
m_OutputHomeDirectory.c_str() << std::endl;
|
|
|
|
std::cout << " m_cmHomeDirectory; " <<
|
|
|
|
m_cmHomeDirectory.c_str() << std::endl;
|
|
|
|
std::cout << " m_cmCurrentDirectory; " <<
|
|
|
|
m_cmCurrentDirectory.c_str() << std::endl;
|
|
|
|
std::cout << " m_LibraryName; " << m_LibraryName.c_str() << std::endl;
|
|
|
|
std::cout << " m_ProjectName; " << m_ProjectName.c_str() << std::endl;
|
|
|
|
this->PrintStringVector("m_SubDirectories ", m_SubDirectories);
|
|
|
|
this->PrintStringVector("m_MakeVerbatim ", m_MakeVerbatim);
|
|
|
|
this->PrintStringVector("m_IncludeDirectories;", m_IncludeDirectories);
|
|
|
|
this->PrintStringVector("m_LinkDirectories", m_LinkDirectories);
|
|
|
|
this->PrintStringVector("m_LinkLibraries", m_LinkLibraries);
|
|
|
|
this->PrintStringVector("m_LinkLibrariesWin32", m_LinkLibrariesWin32);
|
|
|
|
this->PrintStringVector("m_LinkLibrariesUnix", m_LinkLibrariesUnix);
|
2000-08-29 23:26:29 +04:00
|
|
|
}
|
|
|
|
|
2000-08-30 21:35:41 +04:00
|
|
|
// Parse the given CMakeLists.txt file into a list of classes.
|
2001-01-05 19:41:20 +03:00
|
|
|
bool cmMakefile::ReadMakefile(const char* filename, bool inheriting)
|
2000-08-29 23:26:29 +04:00
|
|
|
{
|
2001-01-05 19:41:20 +03:00
|
|
|
// If not being called from ParseDirectory which
|
|
|
|
// sets the inheriting flag, then parse up the
|
|
|
|
// tree and collect inherited parameters
|
|
|
|
if(!inheriting)
|
|
|
|
{
|
|
|
|
cmSystemTools::ConvertToUnixSlashes(m_cmCurrentDirectory);
|
|
|
|
m_SourceHomeDirectory = m_cmHomeDirectory;
|
|
|
|
cmSystemTools::ConvertToUnixSlashes(m_SourceHomeDirectory);
|
|
|
|
this->ParseDirectory(m_cmCurrentDirectory.c_str());
|
|
|
|
}
|
|
|
|
// Now read the input file
|
2000-08-29 23:26:29 +04:00
|
|
|
std::ifstream fin(filename);
|
|
|
|
if(!fin)
|
|
|
|
{
|
2001-01-05 19:41:20 +03:00
|
|
|
cmSystemTools::Error("error can not open file ", filename);
|
2000-08-29 23:26:29 +04:00
|
|
|
return false;
|
|
|
|
}
|
2001-01-05 19:41:20 +03:00
|
|
|
std::string name;
|
|
|
|
std::vector<std::string> arguments;
|
|
|
|
while ( fin )
|
2000-08-29 23:26:29 +04:00
|
|
|
{
|
2001-01-05 19:41:20 +03:00
|
|
|
if(cmSystemTools::ParseFunction(fin, name, arguments) )
|
2000-08-29 23:26:29 +04:00
|
|
|
{
|
2001-01-18 19:20:24 +03:00
|
|
|
// Special command that needs to be removed when
|
|
|
|
// ADD_COMMAND is implemented
|
2001-01-05 19:41:20 +03:00
|
|
|
if(name == "VERBATIM")
|
2000-08-30 21:35:41 +04:00
|
|
|
{
|
2001-01-05 19:41:20 +03:00
|
|
|
if(!inheriting)
|
2000-08-30 21:35:41 +04:00
|
|
|
{
|
2001-01-05 19:41:20 +03:00
|
|
|
m_MakeVerbatim = arguments;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-01-18 19:20:24 +03:00
|
|
|
RegisteredCommandsMap::iterator pos = m_Commands.find(name);
|
|
|
|
if(pos != m_Commands.end())
|
2001-01-05 19:41:20 +03:00
|
|
|
{
|
2001-01-18 19:20:24 +03:00
|
|
|
cmCommand* rm = (*pos).second;
|
|
|
|
cmCommand* usedCommand = rm->Clone();
|
|
|
|
usedCommand->SetMakefile(this);
|
|
|
|
usedCommand->LoadCache();
|
|
|
|
m_UsedCommands.push_back(usedCommand);
|
|
|
|
if(usedCommand->GetEnabled())
|
2001-01-05 19:41:20 +03:00
|
|
|
{
|
|
|
|
// if not running in inherit mode or
|
2001-01-18 19:20:24 +03:00
|
|
|
// if the command is inherited then Invoke it.
|
|
|
|
if(!inheriting || usedCommand->IsInherited())
|
2001-01-05 19:41:20 +03:00
|
|
|
{
|
2001-01-18 19:20:24 +03:00
|
|
|
if(!usedCommand->Invoke(arguments))
|
2001-01-05 19:41:20 +03:00
|
|
|
{
|
2001-01-18 19:20:24 +03:00
|
|
|
cmSystemTools::Error(usedCommand->GetError());
|
2001-01-05 19:41:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cmSystemTools::Error("unknown CMake function", name.c_str());
|
2000-08-30 21:35:41 +04:00
|
|
|
}
|
|
|
|
}
|
2000-08-29 23:26:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-18 19:20:24 +03:00
|
|
|
void cmMakefile::AddCommand(cmCommand* wg)
|
2001-01-05 19:41:20 +03:00
|
|
|
{
|
|
|
|
std::string name = wg->GetName();
|
2001-01-18 19:20:24 +03:00
|
|
|
m_Commands.insert( RegisteredCommandsMap::value_type(name, wg));
|
2001-01-05 19:41:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the make file
|
|
|
|
void cmMakefile::SetMakefileGenerator(cmMakefileGenerator* mf)
|
|
|
|
{
|
|
|
|
delete m_MakefileGenerator;
|
|
|
|
m_MakefileGenerator = mf;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the output file
|
|
|
|
void cmMakefile::GenerateMakefile()
|
|
|
|
{
|
|
|
|
// do all the variable expansions here
|
|
|
|
this->ExpandVaribles();
|
|
|
|
// set the makefile on the generator
|
|
|
|
m_MakefileGenerator->SetMakefile(this);
|
2001-01-18 19:20:24 +03:00
|
|
|
// give all the commands a chance to do something
|
2001-01-05 19:41:20 +03:00
|
|
|
// after the file has been parsed before generation
|
2001-01-18 19:20:24 +03:00
|
|
|
for(std::vector<cmCommand*>::iterator i = m_UsedCommands.begin();
|
|
|
|
i != m_UsedCommands.end(); ++i)
|
2001-01-05 19:41:20 +03:00
|
|
|
{
|
|
|
|
(*i)->FinalPass();
|
2000-08-29 23:26:29 +04:00
|
|
|
}
|
2001-01-05 19:41:20 +03:00
|
|
|
// now do the generation
|
|
|
|
m_MakefileGenerator->GenerateMakefile();
|
2000-08-29 23:26:29 +04:00
|
|
|
}
|
|
|
|
|
2001-01-05 19:41:20 +03:00
|
|
|
void cmMakefile::AddClass(cmClassFile& cmfile)
|
|
|
|
{
|
|
|
|
m_Classes.push_back(cmfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-01-18 19:20:24 +03:00
|
|
|
void cmMakefile::AddCustomCommand(const char* source,
|
2001-01-05 19:41:20 +03:00
|
|
|
const char* result,
|
|
|
|
const char* command,
|
|
|
|
std::vector<std::string>& depends)
|
|
|
|
{
|
2001-01-18 19:20:24 +03:00
|
|
|
cmMakefile::customCommand customCommand;
|
|
|
|
customCommand.m_Source = source;
|
|
|
|
customCommand.m_Result = result;
|
|
|
|
customCommand.m_Command = command;
|
|
|
|
customCommand.m_Depends = depends;
|
|
|
|
m_CustomCommands.push_back(customCommand);
|
2001-01-05 19:41:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmMakefile::AddDefineFlag(const char* flag)
|
|
|
|
{
|
|
|
|
m_DefineFlags += " ";
|
|
|
|
m_DefineFlags += flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmMakefile::AddExecutable(cmClassFile& cf)
|
|
|
|
{
|
|
|
|
m_Classes.push_back(cf);
|
|
|
|
m_Executables = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmMakefile::AddLinkLibrary(const char* lib)
|
|
|
|
{
|
|
|
|
m_LinkLibraries.push_back(lib);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmMakefile::AddLinkDirectory(const char* dir)
|
|
|
|
{
|
|
|
|
m_LinkDirectories.push_back(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmMakefile::AddSubDirectory(const char* sub)
|
|
|
|
{
|
|
|
|
m_SubDirectories.push_back(sub);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmMakefile::AddIncludeDirectory(const char* inc)
|
|
|
|
{
|
|
|
|
m_IncludeDirectories.push_back(inc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmMakefile::AddDefinition(const char* name, const char* value)
|
|
|
|
{
|
|
|
|
m_Definitions.insert(DefinitionMap::value_type(name, value));
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmMakefile::SetProjectName(const char* p)
|
|
|
|
{
|
|
|
|
m_ProjectName = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cmMakefile::SetLibraryName(const char* l)
|
|
|
|
{
|
|
|
|
m_LibraryName = l;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void cmMakefile::AddExtraDirectory(const char* dir)
|
|
|
|
{
|
|
|
|
m_AuxSourceDirectories.push_back(dir);
|
|
|
|
}
|
2000-08-29 23:26:29 +04:00
|
|
|
|
2001-01-05 19:41:20 +03:00
|
|
|
|
|
|
|
// Go until directory == m_cmHomeDirectory
|
|
|
|
// 1. fix slashes
|
|
|
|
// 2. peal off /dir until home found, go no higher
|
|
|
|
void cmMakefile::ParseDirectory(const char* dir)
|
2000-08-30 21:35:41 +04:00
|
|
|
{
|
2001-01-05 19:41:20 +03:00
|
|
|
std::string listsFile = dir;
|
|
|
|
listsFile += "/CMakeLists.txt";
|
|
|
|
if(cmSystemTools::FileExists(listsFile.c_str()))
|
2000-08-30 21:35:41 +04:00
|
|
|
{
|
2001-01-05 19:41:20 +03:00
|
|
|
this->ReadMakefile(listsFile.c_str(), true);
|
|
|
|
}
|
|
|
|
if(m_SourceHomeDirectory == dir)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string dotdotDir = dir;
|
|
|
|
std::string::size_type pos = dotdotDir.rfind('/');
|
|
|
|
if(pos != std::string::npos)
|
|
|
|
{
|
|
|
|
dotdotDir = dotdotDir.substr(0, pos);
|
|
|
|
this->ParseDirectory(dotdotDir.c_str());
|
2000-08-30 21:35:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-05 19:41:20 +03:00
|
|
|
// expance CMAKE_BINARY_DIR and CMAKE_SOURCE_DIR in the
|
|
|
|
// include and library directories.
|
|
|
|
|
|
|
|
void cmMakefile::ExpandVaribles()
|
|
|
|
{
|
|
|
|
// Now replace varibles
|
|
|
|
std::vector<std::string>::iterator j, begin, end;
|
|
|
|
begin = m_IncludeDirectories.begin();
|
|
|
|
end = m_IncludeDirectories.end();
|
|
|
|
for(j = begin; j != end; ++j)
|
|
|
|
{
|
|
|
|
cmSystemTools::ReplaceString(*j, "${CMAKE_BINARY_DIR}",
|
|
|
|
this->GetOutputHomeDirectory() );
|
|
|
|
cmSystemTools::ReplaceString(*j, "${CMAKE_SOURCE_DIR}",
|
|
|
|
this->GetHomeDirectory() );
|
|
|
|
}
|
|
|
|
begin = m_LinkDirectories.begin();
|
|
|
|
end = m_LinkDirectories.end();
|
|
|
|
for(j = begin; j != end; ++j)
|
|
|
|
{
|
|
|
|
cmSystemTools::ReplaceString(*j, "${CMAKE_BINARY_DIR}",
|
|
|
|
this->GetOutputHomeDirectory() );
|
|
|
|
cmSystemTools::ReplaceString(*j, "${CMAKE_SOURCE_DIR}",
|
|
|
|
this->GetHomeDirectory() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* cmMakefile::GetDefinition(const char* name)
|
|
|
|
{
|
|
|
|
DefinitionMap::iterator pos = m_Definitions.find(name);
|
|
|
|
if(pos != m_Definitions.end())
|
|
|
|
{
|
|
|
|
return (*pos).second.c_str();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2001-01-12 20:43:00 +03:00
|
|
|
|
|
|
|
int cmMakefile::DumpDocumentationToFile(const char *fileName)
|
|
|
|
{
|
|
|
|
// Open the supplied filename
|
|
|
|
std::ofstream f;
|
|
|
|
f.open(fileName, std::ios::out);
|
|
|
|
|
|
|
|
if ( f.fail() )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-01-18 19:20:24 +03:00
|
|
|
// Loop over all registered commands and print out documentation
|
2001-01-12 20:43:00 +03:00
|
|
|
const char *name;
|
|
|
|
const char *terse;
|
|
|
|
const char *full;
|
|
|
|
|
2001-01-18 19:20:24 +03:00
|
|
|
for(RegisteredCommandsMap::iterator j = m_Commands.begin();
|
|
|
|
j != m_Commands.end(); ++j)
|
2001-01-12 20:43:00 +03:00
|
|
|
{
|
|
|
|
name = (*j).second->GetName();
|
2001-01-12 22:35:15 +03:00
|
|
|
terse = (*j).second->GetTerseDocumentation();
|
|
|
|
full = (*j).second->GetFullDocumentation();
|
2001-01-12 20:43:00 +03:00
|
|
|
f << name << " - " << terse << std::endl
|
2001-01-12 20:49:49 +03:00
|
|
|
<< "Usage: " << full << std::endl << std::endl;
|
2001-01-12 20:43:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|