2009-09-28 15:43:28 +00:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2007-06-08 15:57:16 +00:00
|
|
|
|
2009-09-28 15:43:28 +00:00
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
2007-06-08 15:57:16 +00:00
|
|
|
|
2009-09-28 15:43:28 +00: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.
|
|
|
|
============================================================================*/
|
2007-06-08 15:57:16 +00:00
|
|
|
#include "cmExportCommand.h"
|
|
|
|
#include "cmGlobalGenerator.h"
|
|
|
|
#include "cmLocalGenerator.h"
|
|
|
|
#include "cmGeneratedFileStream.h"
|
|
|
|
#include "cmake.h"
|
|
|
|
|
2009-09-01 18:04:53 +00:00
|
|
|
#include <cmsys/RegularExpression.hxx>
|
2013-12-05 05:17:24 +00:00
|
|
|
#include <cmsys/Encoding.hxx>
|
2009-09-01 18:04:53 +00:00
|
|
|
|
2008-01-28 13:38:36 +00:00
|
|
|
#include "cmExportBuildFileGenerator.h"
|
2007-06-08 15:57:16 +00:00
|
|
|
|
2009-09-28 13:45:34 +00:00
|
|
|
#if defined(__HAIKU__)
|
2013-10-05 14:31:36 +00:00
|
|
|
#include <FindDirectory.h>
|
|
|
|
#include <StorageDefs.h>
|
2009-09-28 13:45:34 +00:00
|
|
|
#endif
|
|
|
|
|
2007-07-02 19:43:21 +00:00
|
|
|
cmExportCommand::cmExportCommand()
|
|
|
|
:cmCommand()
|
|
|
|
,ArgumentGroup()
|
2007-07-02 20:46:18 +00:00
|
|
|
,Targets(&Helper, "TARGETS")
|
2008-01-28 18:21:42 +00:00
|
|
|
,Append(&Helper, "APPEND", &ArgumentGroup)
|
2013-12-23 16:07:26 +00:00
|
|
|
,ExportSetName(&Helper, "EXPORT", &ArgumentGroup)
|
2008-01-28 13:38:36 +00:00
|
|
|
,Namespace(&Helper, "NAMESPACE", &ArgumentGroup)
|
2007-07-02 20:46:18 +00:00
|
|
|
,Filename(&Helper, "FILE", &ArgumentGroup)
|
2013-06-04 14:47:57 +00:00
|
|
|
,ExportOld(&Helper, "EXPORT_LINK_INTERFACE_LIBRARIES", &ArgumentGroup)
|
2007-07-02 19:43:21 +00:00
|
|
|
{
|
2013-12-23 16:07:26 +00:00
|
|
|
this->ExportSet = 0;
|
2007-07-02 19:43:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-08 15:57:16 +00:00
|
|
|
// cmExportCommand
|
|
|
|
bool cmExportCommand
|
2008-01-23 15:28:26 +00:00
|
|
|
::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
|
2007-06-08 15:57:16 +00:00
|
|
|
{
|
|
|
|
if(args.size() < 2 )
|
|
|
|
{
|
|
|
|
this->SetError("called with too few arguments");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-09-01 18:04:53 +00:00
|
|
|
if(args[0] == "PACKAGE")
|
|
|
|
{
|
|
|
|
return this->HandlePackage(args);
|
|
|
|
}
|
2013-12-23 16:07:26 +00:00
|
|
|
else if (args[0] == "EXPORT")
|
|
|
|
{
|
|
|
|
this->ExportSetName.Follows(0);
|
|
|
|
this->ArgumentGroup.Follows(&this->ExportSetName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->Targets.Follows(0);
|
|
|
|
this->ArgumentGroup.Follows(&this->Targets);
|
|
|
|
}
|
2009-09-01 18:04:53 +00:00
|
|
|
|
2007-07-02 19:43:21 +00:00
|
|
|
std::vector<std::string> unknownArgs;
|
|
|
|
this->Helper.Parse(&args, &unknownArgs);
|
|
|
|
|
|
|
|
if (!unknownArgs.empty())
|
|
|
|
{
|
|
|
|
this->SetError("Unknown arguments.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-23 16:07:26 +00:00
|
|
|
std::string fname;
|
2008-01-28 13:38:36 +00:00
|
|
|
if(!this->Filename.WasFound())
|
2007-06-08 15:57:16 +00:00
|
|
|
{
|
2013-12-23 16:07:26 +00:00
|
|
|
if (args[0] != "EXPORT")
|
|
|
|
{
|
|
|
|
this->SetError("FILE <filename> option missing.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fname = this->ExportSetName.GetString() + ".cmake";
|
2007-06-08 15:57:16 +00:00
|
|
|
}
|
2013-12-23 16:07:26 +00:00
|
|
|
else
|
2007-06-08 15:57:16 +00:00
|
|
|
{
|
2013-12-23 16:07:26 +00:00
|
|
|
// Make sure the file has a .cmake extension.
|
|
|
|
if(cmSystemTools::GetFilenameLastExtension(this->Filename.GetCString())
|
|
|
|
!= ".cmake")
|
|
|
|
{
|
|
|
|
cmOStringStream e;
|
|
|
|
e << "FILE option given filename \"" << this->Filename.GetString()
|
|
|
|
<< "\" which does not have an extension of \".cmake\".\n";
|
2014-03-10 23:04:11 +00:00
|
|
|
this->SetError(e.str());
|
2013-12-23 16:07:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fname = this->Filename.GetString();
|
2007-06-08 15:57:16 +00:00
|
|
|
}
|
|
|
|
|
2008-01-28 13:38:36 +00:00
|
|
|
// Get the file to write.
|
|
|
|
if(cmSystemTools::FileIsFullPath(fname.c_str()))
|
2007-06-08 15:57:16 +00:00
|
|
|
{
|
2008-01-28 13:38:36 +00:00
|
|
|
if(!this->Makefile->CanIWriteThisFile(fname.c_str()))
|
|
|
|
{
|
|
|
|
cmOStringStream e;
|
|
|
|
e << "FILE option given filename \"" << fname
|
|
|
|
<< "\" which is in the source tree.\n";
|
2014-03-10 23:04:11 +00:00
|
|
|
this->SetError(e.str());
|
2008-01-28 13:38:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-06-08 15:57:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-01-28 13:38:36 +00:00
|
|
|
// Interpret relative paths with respect to the current build dir.
|
2013-12-23 16:07:26 +00:00
|
|
|
std::string dir = this->Makefile->GetCurrentOutputDirectory();
|
|
|
|
fname = dir + "/" + fname;
|
2007-06-08 15:57:16 +00:00
|
|
|
}
|
|
|
|
|
2013-12-23 16:07:26 +00:00
|
|
|
std::vector<std::string> targets;
|
|
|
|
|
|
|
|
cmGlobalGenerator *gg = this->Makefile->GetLocalGenerator()
|
|
|
|
->GetGlobalGenerator();
|
|
|
|
|
|
|
|
if(args[0] == "EXPORT")
|
2007-06-08 20:19:13 +00:00
|
|
|
{
|
2013-12-23 16:07:26 +00:00
|
|
|
if (this->Append.IsEnabled())
|
|
|
|
{
|
|
|
|
cmOStringStream e;
|
|
|
|
e << "EXPORT signature does not recognise the APPEND option.";
|
2014-03-10 23:04:11 +00:00
|
|
|
this->SetError(e.str());
|
2013-12-23 16:07:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->ExportOld.IsEnabled())
|
2013-07-12 07:14:31 +00:00
|
|
|
{
|
|
|
|
cmOStringStream e;
|
2013-12-23 16:07:26 +00:00
|
|
|
e << "EXPORT signature does not recognise the "
|
|
|
|
"EXPORT_LINK_INTERFACE_LIBRARIES option.";
|
2014-03-10 23:04:11 +00:00
|
|
|
this->SetError(e.str());
|
2013-07-12 07:14:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-23 16:07:26 +00:00
|
|
|
cmExportSetMap &setMap = gg->GetExportSets();
|
|
|
|
std::string setName = this->ExportSetName.GetString();
|
|
|
|
if (setMap.find(setName) == setMap.end())
|
|
|
|
{
|
|
|
|
cmOStringStream e;
|
|
|
|
e << "Export set \"" << setName << "\" not found.";
|
2014-03-10 23:04:11 +00:00
|
|
|
this->SetError(e.str());
|
2013-12-23 16:07:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
this->ExportSet = setMap[setName];
|
|
|
|
}
|
|
|
|
else if (this->Targets.WasFound())
|
|
|
|
{
|
|
|
|
for(std::vector<std::string>::const_iterator
|
|
|
|
currentTarget = this->Targets.GetVector().begin();
|
|
|
|
currentTarget != this->Targets.GetVector().end();
|
|
|
|
++currentTarget)
|
2008-01-28 13:38:36 +00:00
|
|
|
{
|
2014-01-15 22:56:38 +00:00
|
|
|
if (this->Makefile->IsAlias(*currentTarget))
|
2012-03-12 14:47:40 +00:00
|
|
|
{
|
|
|
|
cmOStringStream e;
|
2013-12-23 16:07:26 +00:00
|
|
|
e << "given ALIAS target \"" << *currentTarget
|
2012-03-12 14:47:40 +00:00
|
|
|
<< "\" which may not be exported.";
|
2014-03-10 23:04:11 +00:00
|
|
|
this->SetError(e.str());
|
2012-03-12 14:47:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
2013-12-23 16:07:26 +00:00
|
|
|
|
2014-03-10 23:04:11 +00:00
|
|
|
if(cmTarget* target = gg->FindTarget(*currentTarget))
|
2013-12-23 16:07:26 +00:00
|
|
|
{
|
|
|
|
if(target->GetType() == cmTarget::OBJECT_LIBRARY)
|
|
|
|
{
|
|
|
|
cmOStringStream e;
|
|
|
|
e << "given OBJECT library \"" << *currentTarget
|
|
|
|
<< "\" which may not be exported.";
|
2014-03-10 23:04:11 +00:00
|
|
|
this->SetError(e.str());
|
2013-12-23 16:07:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cmOStringStream e;
|
|
|
|
e << "given target \"" << *currentTarget
|
|
|
|
<< "\" which is not built by this project.";
|
2014-03-10 23:04:11 +00:00
|
|
|
this->SetError(e.str());
|
2013-12-23 16:07:26 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
targets.push_back(*currentTarget);
|
2008-01-28 13:38:36 +00:00
|
|
|
}
|
2013-12-23 16:07:26 +00:00
|
|
|
if (this->Append.IsEnabled())
|
2013-12-02 16:18:29 +00:00
|
|
|
{
|
2013-12-23 16:07:26 +00:00
|
|
|
if (cmExportBuildFileGenerator *ebfg = gg->GetExportedTargetsFile(fname))
|
|
|
|
{
|
|
|
|
ebfg->AppendTargets(targets);
|
|
|
|
return true;
|
|
|
|
}
|
2013-12-02 16:18:29 +00:00
|
|
|
}
|
2012-10-06 15:27:40 +00:00
|
|
|
}
|
2013-12-23 16:07:26 +00:00
|
|
|
else
|
2012-10-06 15:27:40 +00:00
|
|
|
{
|
2013-12-23 16:07:26 +00:00
|
|
|
this->SetError("EXPORT or TARGETS specifier missing.");
|
|
|
|
return false;
|
2007-06-08 20:19:13 +00:00
|
|
|
}
|
|
|
|
|
2008-01-28 13:38:36 +00:00
|
|
|
// Setup export file generation.
|
2012-10-06 15:27:40 +00:00
|
|
|
cmExportBuildFileGenerator *ebfg = new cmExportBuildFileGenerator;
|
|
|
|
ebfg->SetExportFile(fname.c_str());
|
|
|
|
ebfg->SetNamespace(this->Namespace.GetCString());
|
|
|
|
ebfg->SetAppendMode(this->Append.IsEnabled());
|
2013-12-23 16:07:26 +00:00
|
|
|
if (this->ExportSet)
|
|
|
|
{
|
|
|
|
ebfg->SetExportSet(this->ExportSet);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ebfg->SetTargets(targets);
|
|
|
|
}
|
2012-10-06 15:27:40 +00:00
|
|
|
ebfg->SetMakefile(this->Makefile);
|
|
|
|
ebfg->SetExportOld(this->ExportOld.IsEnabled());
|
2013-06-18 14:33:33 +00:00
|
|
|
|
2008-01-28 13:38:36 +00:00
|
|
|
// Compute the set of configurations exported.
|
2010-09-08 18:54:49 +00:00
|
|
|
std::vector<std::string> configurationTypes;
|
|
|
|
this->Makefile->GetConfigurations(configurationTypes);
|
2014-03-27 08:46:58 +00:00
|
|
|
if(configurationTypes.empty())
|
2007-06-08 15:57:16 +00:00
|
|
|
{
|
2014-03-27 08:46:58 +00:00
|
|
|
configurationTypes.push_back("");
|
2007-06-08 15:57:16 +00:00
|
|
|
}
|
2014-03-27 08:46:58 +00:00
|
|
|
for(std::vector<std::string>::const_iterator
|
|
|
|
ci = configurationTypes.begin();
|
|
|
|
ci != configurationTypes.end(); ++ci)
|
2008-01-28 13:38:36 +00:00
|
|
|
{
|
2014-03-27 08:46:58 +00:00
|
|
|
ebfg->AddConfiguration(*ci);
|
2008-01-28 13:38:36 +00:00
|
|
|
}
|
2013-12-26 13:34:27 +00:00
|
|
|
if (this->ExportSet)
|
|
|
|
{
|
|
|
|
gg->AddBuildExportExportSet(ebfg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gg->AddBuildExportSet(ebfg);
|
|
|
|
}
|
2008-01-30 22:25:52 +00:00
|
|
|
|
2007-06-08 15:57:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
2009-09-01 18:04:53 +00:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
bool cmExportCommand::HandlePackage(std::vector<std::string> const& args)
|
|
|
|
{
|
|
|
|
// Parse PACKAGE mode arguments.
|
|
|
|
enum Doing { DoingNone, DoingPackage };
|
|
|
|
Doing doing = DoingPackage;
|
|
|
|
std::string package;
|
|
|
|
for(unsigned int i=1; i < args.size(); ++i)
|
|
|
|
{
|
|
|
|
if(doing == DoingPackage)
|
|
|
|
{
|
|
|
|
package = args[i];
|
|
|
|
doing = DoingNone;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cmOStringStream e;
|
2013-05-01 09:36:14 +00:00
|
|
|
e << "PACKAGE given unknown argument: " << args[i];
|
2014-03-10 23:04:11 +00:00
|
|
|
this->SetError(e.str());
|
2009-09-01 18:04:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the package name.
|
|
|
|
if(package.empty())
|
|
|
|
{
|
|
|
|
this->SetError("PACKAGE must be given a package name.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const char* packageExpr = "^[A-Za-z0-9_.-]+$";
|
|
|
|
cmsys::RegularExpression packageRegex(packageExpr);
|
|
|
|
if(!packageRegex.find(package.c_str()))
|
|
|
|
{
|
|
|
|
cmOStringStream e;
|
|
|
|
e << "PACKAGE given invalid package name \"" << package << "\". "
|
|
|
|
<< "Package names must match \"" << packageExpr << "\".";
|
2014-03-10 23:04:11 +00:00
|
|
|
this->SetError(e.str());
|
2009-09-01 18:04:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We store the current build directory in the registry as a value
|
|
|
|
// named by a hash of its own content. This is deterministic and is
|
|
|
|
// unique with high probability.
|
|
|
|
const char* outDir = this->Makefile->GetCurrentOutputDirectory();
|
|
|
|
std::string hash = cmSystemTools::ComputeStringMD5(outDir);
|
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
|
|
this->StorePackageRegistryWin(package, outDir, hash.c_str());
|
|
|
|
#else
|
|
|
|
this->StorePackageRegistryDir(package, outDir, hash.c_str());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
|
|
# include <windows.h>
|
|
|
|
# undef GetCurrentDirectory
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
void cmExportCommand::ReportRegistryError(std::string const& msg,
|
|
|
|
std::string const& key,
|
|
|
|
long err)
|
|
|
|
{
|
|
|
|
cmOStringStream e;
|
|
|
|
e << msg << "\n"
|
|
|
|
<< " HKEY_CURRENT_USER\\" << key << "\n";
|
2013-12-05 05:17:24 +00:00
|
|
|
wchar_t winmsg[1024];
|
|
|
|
if(FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
|
2009-09-01 18:04:53 +00:00
|
|
|
FORMAT_MESSAGE_IGNORE_INSERTS, 0, err,
|
|
|
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
|
|
winmsg, 1024, 0) > 0)
|
|
|
|
{
|
|
|
|
e << "Windows reported:\n"
|
2013-12-05 05:17:24 +00:00
|
|
|
<< " " << cmsys::Encoding::ToNarrow(winmsg);
|
2009-09-01 18:04:53 +00:00
|
|
|
}
|
|
|
|
this->Makefile->IssueMessage(cmake::WARNING, e.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
void cmExportCommand::StorePackageRegistryWin(std::string const& package,
|
|
|
|
const char* content,
|
|
|
|
const char* hash)
|
|
|
|
{
|
|
|
|
std::string key = "Software\\Kitware\\CMake\\Packages\\";
|
|
|
|
key += package;
|
|
|
|
HKEY hKey;
|
2013-12-05 05:17:24 +00:00
|
|
|
LONG err = RegCreateKeyExW(HKEY_CURRENT_USER,
|
|
|
|
cmsys::Encoding::ToWide(key).c_str(),
|
|
|
|
0, 0, REG_OPTION_NON_VOLATILE,
|
2009-09-01 18:04:53 +00:00
|
|
|
KEY_SET_VALUE, 0, &hKey, 0);
|
|
|
|
if(err != ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
this->ReportRegistryError(
|
|
|
|
"Cannot create/open registry key", key, err);
|
|
|
|
return;
|
|
|
|
}
|
2013-12-05 05:17:24 +00:00
|
|
|
|
|
|
|
std::wstring wcontent = cmsys::Encoding::ToWide(content);
|
|
|
|
err = RegSetValueExW(hKey, cmsys::Encoding::ToWide(hash).c_str(),
|
|
|
|
0, REG_SZ, (BYTE const*)wcontent.c_str(),
|
|
|
|
static_cast<DWORD>(wcontent.size()+1)*sizeof(wchar_t));
|
2009-09-01 18:04:53 +00:00
|
|
|
RegCloseKey(hKey);
|
|
|
|
if(err != ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
cmOStringStream msg;
|
|
|
|
msg << "Cannot set registry value \"" << hash << "\" under key";
|
|
|
|
this->ReportRegistryError(msg.str(), key, err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
void cmExportCommand::StorePackageRegistryDir(std::string const& package,
|
|
|
|
const char* content,
|
|
|
|
const char* hash)
|
|
|
|
{
|
2009-09-28 13:45:34 +00:00
|
|
|
#if defined(__HAIKU__)
|
2013-10-05 14:31:36 +00:00
|
|
|
char dir[B_PATH_NAME_LENGTH];
|
|
|
|
if (find_directory(B_USER_SETTINGS_DIRECTORY, -1, false, dir, sizeof(dir)) !=
|
|
|
|
B_OK)
|
2009-09-28 13:45:34 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-10-05 14:31:36 +00:00
|
|
|
std::string fname = dir;
|
|
|
|
fname += "/cmake/packages/";
|
|
|
|
fname += package;
|
2009-09-28 13:45:34 +00:00
|
|
|
#else
|
2009-09-01 18:04:53 +00:00
|
|
|
const char* home = cmSystemTools::GetEnv("HOME");
|
|
|
|
if(!home)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::string fname = home;
|
|
|
|
cmSystemTools::ConvertToUnixSlashes(fname);
|
|
|
|
fname += "/.cmake/packages/";
|
|
|
|
fname += package;
|
2009-09-28 13:45:34 +00:00
|
|
|
#endif
|
2009-09-01 18:04:53 +00:00
|
|
|
cmSystemTools::MakeDirectory(fname.c_str());
|
|
|
|
fname += "/";
|
|
|
|
fname += hash;
|
|
|
|
if(!cmSystemTools::FileExists(fname.c_str()))
|
|
|
|
{
|
|
|
|
cmGeneratedFileStream entry(fname.c_str(), true);
|
|
|
|
if(entry)
|
|
|
|
{
|
|
|
|
entry << content << "\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cmOStringStream e;
|
|
|
|
e << "Cannot create package registry file:\n"
|
|
|
|
<< " " << fname << "\n"
|
|
|
|
<< cmSystemTools::GetLastSystemError() << "\n";
|
|
|
|
this->Makefile->IssueMessage(cmake::WARNING, e.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|