1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
/*============================================================================
CMake - Cross Platform Makefile Generator
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
Distributed under the OSI-approved BSD License (the "License");
see accompanying file Copyright.txt for details.
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.
============================================================================*/
#include "cmExportCommand.h"
#include "cmGlobalGenerator.h"
#include "cmLocalGenerator.h"
#include "cmGeneratedFileStream.h"
#include "cmake.h"
#include <cmsys/RegularExpression.hxx>
#include "cmExportBuildFileGenerator.h"
#if defined(__HAIKU__)
#include <StorageKit.h>
#endif
cmExportCommand::cmExportCommand()
:cmCommand()
,ArgumentGroup()
,Targets(&Helper, "TARGETS")
,Append(&Helper, "APPEND", &ArgumentGroup)
,Namespace(&Helper, "NAMESPACE", &ArgumentGroup)
,Filename(&Helper, "FILE", &ArgumentGroup)
{
// at first TARGETS
this->Targets.Follows(0);
// and after that the other options in any order
this->ArgumentGroup.Follows(&this->Targets);
}
// cmExportCommand
bool cmExportCommand
::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
{
if(args.size() < 2 )
{
this->SetError("called with too few arguments");
return false;
}
if(args[0] == "PACKAGE")
{
return this->HandlePackage(args);
}
std::vector<std::string> unknownArgs;
this->Helper.Parse(&args, &unknownArgs);
if (!unknownArgs.empty())
{
this->SetError("Unknown arguments.");
return false;
}
if (this->Targets.WasFound() == false)
{
this->SetError("TARGETS option missing.");
return false;
}
if(!this->Filename.WasFound())
{
this->SetError("FILE <filename> option missing.");
return false;
}
// 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";
this->SetError(e.str().c_str());
return false;
}
// Get the file to write.
std::string fname = this->Filename.GetString();
if(cmSystemTools::FileIsFullPath(fname.c_str()))
{
if(!this->Makefile->CanIWriteThisFile(fname.c_str()))
{
cmOStringStream e;
e << "FILE option given filename \"" << fname
<< "\" which is in the source tree.\n";
this->SetError(e.str().c_str());
return false;
}
}
else
{
// Interpret relative paths with respect to the current build dir.
fname = this->Makefile->GetCurrentOutputDirectory();
fname += "/";
fname += this->Filename.GetString();
}
// Collect the targets to be exported.
std::vector<cmTarget*> targets;
for(std::vector<std::string>::const_iterator
currentTarget = this->Targets.GetVector().begin();
currentTarget != this->Targets.GetVector().end();
++currentTarget)
{
if(cmTarget* target =
this->Makefile->GetLocalGenerator()->
GetGlobalGenerator()->FindTarget(0, currentTarget->c_str()))
{
if((target->GetType() == cmTarget::EXECUTABLE) ||
(target->GetType() == cmTarget::STATIC_LIBRARY) ||
(target->GetType() == cmTarget::SHARED_LIBRARY) ||
(target->GetType() == cmTarget::MODULE_LIBRARY))
{
targets.push_back(target);
}
else
{
cmOStringStream e;
e << "given target \"" << *currentTarget
<< "\" which is not an executable or library.";
this->SetError(e.str().c_str());
return false;
}
}
else
{
cmOStringStream e;
e << "given target \"" << *currentTarget
<< "\" which is not built by this project.";
this->SetError(e.str().c_str());
return false;
}
}
// Setup export file generation.
cmExportBuildFileGenerator ebfg;
ebfg.SetExportFile(fname.c_str());
ebfg.SetNamespace(this->Namespace.GetCString());
ebfg.SetAppendMode(this->Append.IsEnabled());
ebfg.SetExports(&targets);
ebfg.SetCommand(this);
// Compute the set of configurations exported.
if(const char* types =
this->Makefile->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
{
std::vector<std::string> configurationTypes;
cmSystemTools::ExpandListArgument(types, configurationTypes);
for(std::vector<std::string>::const_iterator
ci = configurationTypes.begin();
ci != configurationTypes.end(); ++ci)
{
ebfg.AddConfiguration(ci->c_str());
}
}
else if(const char* config =
this->Makefile->GetDefinition("CMAKE_BUILD_TYPE"))
{
ebfg.AddConfiguration(config);
}
else
{
ebfg.AddConfiguration("");
}
// Generate the import file.
if(!ebfg.GenerateImportFile() && this->ErrorMessage.empty())
{
this->SetError("could not write export file.");
return false;
}
// Report generated error message if any.
if(!this->ErrorMessage.empty())
{
this->SetError(this->ErrorMessage.c_str());
return false;
}
return true;
}
//----------------------------------------------------------------------------
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;
e << "PACKAGE given unknown argumsnt: " << args[i];
this->SetError(e.str().c_str());
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 << "\".";
this->SetError(e.str().c_str());
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";
char winmsg[1024];
if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, 0, err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
winmsg, 1024, 0) > 0)
{
e << "Windows reported:\n"
<< " " << winmsg;
}
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;
LONG err = RegCreateKeyEx(HKEY_CURRENT_USER,
key.c_str(), 0, 0, REG_OPTION_NON_VOLATILE,
KEY_SET_VALUE, 0, &hKey, 0);
if(err != ERROR_SUCCESS)
{
this->ReportRegistryError(
"Cannot create/open registry key", key, err);
return;
}
err = RegSetValueEx(hKey, hash, 0, REG_SZ, (BYTE const*)content,
static_cast<DWORD>(strlen(content)+1));
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)
{
#if defined(__HAIKU__)
BPath dir;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &dir) != B_OK)
{
return;
}
dir.Append("cmake/packages");
dir.Append(package.c_str());
std::string fname = dir.Path();
#else
const char* home = cmSystemTools::GetEnv("HOME");
if(!home)
{
return;
}
std::string fname = home;
cmSystemTools::ConvertToUnixSlashes(fname);
fname += "/.cmake/packages/";
fname += package;
#endif
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
|