ENH: keep cleaning up Tcl/Tk modules
[cmake.git] / Source / CPack / cmCPackGenerator.cxx
blobff62607f1d45bf7be929cffb8736afe4504e6c0d
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmCPackGenerator.cxx,v $
5 Language: C++
6 Date: $Date: 2007-11-06 13:28:26 $
7 Version: $Revision: 1.2 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
18 #include "cmCPackGenerator.h"
20 #include "cmMakefile.h"
21 #include "cmCPackLog.h"
22 #include "cmake.h"
23 #include "cmGlobalGenerator.h"
24 #include "cmLocalGenerator.h"
25 #include "cmGeneratedFileStream.h"
27 #include <cmsys/SystemTools.hxx>
28 #include <cmsys/Glob.hxx>
29 #include <memory> // auto_ptr
31 //----------------------------------------------------------------------
32 cmCPackGenerator::cmCPackGenerator()
34 this->GeneratorVerbose = false;
35 this->MakefileMap = 0;
36 this->Logger = 0;
39 //----------------------------------------------------------------------
40 cmCPackGenerator::~cmCPackGenerator()
42 this->MakefileMap = 0;
45 //----------------------------------------------------------------------
46 void cmCPackGeneratorProgress(const char *msg, float prog, void* ptr)
48 cmCPackGenerator* self = static_cast<cmCPackGenerator*>(ptr);
49 self->DisplayVerboseOutput(msg, prog);
52 //----------------------------------------------------------------------
53 void cmCPackGenerator::DisplayVerboseOutput(const char* msg,
54 float progress)
56 (void)progress;
57 cmCPackLogger(cmCPackLog::LOG_VERBOSE, "" << msg << std::endl);
60 //----------------------------------------------------------------------
61 int cmCPackGenerator::PrepareNames()
64 std::string tempDirectory = this->GetOption("CPACK_PACKAGE_DIRECTORY");
65 tempDirectory += "/_CPack_Packages/";
66 const char* toplevelTag = this->GetOption("CPACK_TOPLEVEL_TAG");
67 if ( toplevelTag )
69 tempDirectory += toplevelTag;
70 tempDirectory += "/";
72 tempDirectory += this->GetOption("CPACK_GENERATOR");
73 std::string topDirectory = tempDirectory;
75 std::string outName = this->GetOption("CPACK_PACKAGE_FILE_NAME");
76 tempDirectory += "/" + outName;
77 outName += this->GetOutputExtension();
79 std::string destFile = this->GetOption("CPACK_PACKAGE_DIRECTORY");
80 destFile += "/" + outName;
82 std::string outFile = topDirectory + "/" + outName;
84 bool setDestDir = cmSystemTools::IsOn(this->GetOption("CPACK_SET_DESTDIR"));
85 std::string installPrefix = tempDirectory;
86 if (!setDestDir)
88 installPrefix += this->GetPackagingInstallPrefix();
91 this->SetOptionIfNotSet("CPACK_TOPLEVEL_DIRECTORY", topDirectory.c_str());
92 this->SetOptionIfNotSet("CPACK_TEMPORARY_DIRECTORY", tempDirectory.c_str());
93 this->SetOptionIfNotSet("CPACK_OUTPUT_FILE_NAME", outName.c_str());
94 this->SetOptionIfNotSet("CPACK_OUTPUT_FILE_PATH", destFile.c_str());
95 this->SetOptionIfNotSet("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
96 outFile.c_str());
97 this->SetOptionIfNotSet("CPACK_INSTALL_DIRECTORY", this->GetInstallPath());
98 this->SetOptionIfNotSet("CPACK_NATIVE_INSTALL_DIRECTORY",
99 cmsys::SystemTools::ConvertToOutputPath(this->GetInstallPath()).c_str());
100 this->SetOptionIfNotSet("CPACK_TEMPORARY_INSTALL_DIRECTORY",
101 installPrefix.c_str());
103 cmCPackLogger(cmCPackLog::LOG_DEBUG,
104 "Look for: CPACK_PACKAGE_DESCRIPTION_FILE" << std::endl);
105 const char* descFileName
106 = this->GetOption("CPACK_PACKAGE_DESCRIPTION_FILE");
107 if ( descFileName )
109 cmCPackLogger(cmCPackLog::LOG_DEBUG,
110 "Look for: " << descFileName << std::endl);
111 if ( !cmSystemTools::FileExists(descFileName) )
113 cmCPackLogger(cmCPackLog::LOG_ERROR,
114 "Cannot find description file name: ["
115 << descFileName << "]" << std::endl);
116 return 0;
118 std::ifstream ifs(descFileName);
119 if ( !ifs )
121 cmCPackLogger(cmCPackLog::LOG_ERROR,
122 "Cannot open description file name: " << descFileName << std::endl);
123 return 0;
125 cmOStringStream ostr;
126 std::string line;
128 cmCPackLogger(cmCPackLog::LOG_VERBOSE,
129 "Read description file: " << descFileName << std::endl);
130 while ( ifs && cmSystemTools::GetLineFromStream(ifs, line) )
132 ostr << cmSystemTools::MakeXMLSafe(line.c_str()) << std::endl;
134 this->SetOptionIfNotSet("CPACK_PACKAGE_DESCRIPTION", ostr.str().c_str());
136 if ( !this->GetOption("CPACK_PACKAGE_DESCRIPTION") )
138 cmCPackLogger(cmCPackLog::LOG_ERROR,
139 "Project description not specified. Please specify "
140 "CPACK_PACKAGE_DESCRIPTION or CPACK_PACKAGE_DESCRIPTION_FILE."
141 << std::endl);
142 return 0;
145 this->SetOptionIfNotSet("CPACK_REMOVE_TOPLEVEL_DIRECTORY", "1");
147 return 1;
150 //----------------------------------------------------------------------
151 int cmCPackGenerator::InstallProject()
153 cmCPackLogger(cmCPackLog::LOG_OUTPUT, "Install projects" << std::endl);
154 this->CleanTemporaryDirectory();
155 std::string tempInstallDirectoryWithPostfix
156 = this->GetOption("CPACK_TEMPORARY_INSTALL_DIRECTORY");
157 const char* tempInstallDirectory = tempInstallDirectoryWithPostfix.c_str();
158 int res = 1;
159 if ( !cmsys::SystemTools::MakeDirectory(tempInstallDirectory))
161 cmCPackLogger(cmCPackLog::LOG_ERROR,
162 "Problem creating temporary directory: "
163 << (tempInstallDirectory ? tempInstallDirectory : "(NULL}")
164 << std::endl);
165 return 0;
168 bool setDestDir = cmSystemTools::IsOn(this->GetOption("CPACK_SET_DESTDIR"));
169 if ( setDestDir )
171 std::string destDir = "DESTDIR=";
172 destDir += tempInstallDirectory;
173 cmSystemTools::PutEnv(destDir.c_str());
175 else
177 // Make sure there is no destdir
178 cmSystemTools::PutEnv("DESTDIR=");
181 // If the CPackConfig file sets CPACK_INSTALL_COMMANDS then run them
182 // as listed
183 if ( !this->InstallProjectViaInstallCommands(
184 setDestDir, tempInstallDirectory) )
186 return 0;
189 // If the CPackConfig file sets CPACK_INSTALL_SCRIPT then run them
190 // as listed
191 if ( !this->InstallProjectViaInstallScript(
192 setDestDir, tempInstallDirectory) )
194 return 0;
197 // If the CPackConfig file sets CPACK_INSTALLED_DIRECTORIES
198 // then glob it and copy it to CPACK_TEMPORARY_DIRECTORY
199 // This is used in Source packageing
200 if ( !this->InstallProjectViaInstalledDirectories(
201 setDestDir, tempInstallDirectory) )
203 return 0;
207 // If the project is a CMAKE project then run pre-install
208 // and then read the cmake_install script to run it
209 if ( !this->InstallProjectViaInstallCMakeProjects(
210 setDestDir, tempInstallDirectory) )
212 return 0;
215 if ( setDestDir )
217 cmSystemTools::PutEnv("DESTDIR=");
220 return res;
223 //----------------------------------------------------------------------
224 int cmCPackGenerator::InstallProjectViaInstallCommands(
225 bool setDestDir, const char* tempInstallDirectory)
227 (void)setDestDir;
228 (void)tempInstallDirectory;
229 const char* installCommands = this->GetOption("CPACK_INSTALL_COMMANDS");
230 if ( installCommands && *installCommands )
232 std::string tempInstallDirectoryEnv = "CMAKE_INSTALL_PREFIX=";
233 tempInstallDirectoryEnv += tempInstallDirectory;
234 cmSystemTools::PutEnv(tempInstallDirectoryEnv.c_str());
235 std::vector<std::string> installCommandsVector;
236 cmSystemTools::ExpandListArgument(installCommands,installCommandsVector);
237 std::vector<std::string>::iterator it;
238 for ( it = installCommandsVector.begin();
239 it != installCommandsVector.end();
240 ++it )
242 cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Execute: " << it->c_str()
243 << std::endl);
244 std::string output;
245 int retVal = 1;
246 bool resB = cmSystemTools::RunSingleCommand(it->c_str(), &output,
247 &retVal, 0, this->GeneratorVerbose, 0);
248 if ( !resB || retVal )
250 std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
251 tmpFile += "/InstallOutput.log";
252 cmGeneratedFileStream ofs(tmpFile.c_str());
253 ofs << "# Run command: " << it->c_str() << std::endl
254 << "# Output:" << std::endl
255 << output.c_str() << std::endl;
256 cmCPackLogger(cmCPackLog::LOG_ERROR,
257 "Problem running install command: " << it->c_str() << std::endl
258 << "Please check " << tmpFile.c_str() << " for errors"
259 << std::endl);
260 return 0;
264 return 1;
267 //----------------------------------------------------------------------
268 int cmCPackGenerator::InstallProjectViaInstalledDirectories(
269 bool setDestDir, const char* tempInstallDirectory)
271 (void)setDestDir;
272 (void)tempInstallDirectory;
273 std::vector<cmsys::RegularExpression> ignoreFilesRegex;
274 const char* cpackIgnoreFiles = this->GetOption("CPACK_IGNORE_FILES");
275 if ( cpackIgnoreFiles )
277 std::vector<std::string> ignoreFilesRegexString;
278 cmSystemTools::ExpandListArgument(cpackIgnoreFiles,
279 ignoreFilesRegexString);
280 std::vector<std::string>::iterator it;
281 for ( it = ignoreFilesRegexString.begin();
282 it != ignoreFilesRegexString.end();
283 ++it )
285 cmCPackLogger(cmCPackLog::LOG_VERBOSE,
286 "Create ignore files regex for: " << it->c_str() << std::endl);
287 ignoreFilesRegex.push_back(it->c_str());
290 const char* installDirectories
291 = this->GetOption("CPACK_INSTALLED_DIRECTORIES");
292 if ( installDirectories && *installDirectories )
294 std::vector<std::string> installDirectoriesVector;
295 cmSystemTools::ExpandListArgument(installDirectories,
296 installDirectoriesVector);
297 if ( installDirectoriesVector.size() % 2 != 0 )
299 cmCPackLogger(cmCPackLog::LOG_ERROR,
300 "CPACK_INSTALLED_DIRECTORIES should contain pairs of <directory> and "
301 "<subdirectory>. The <subdirectory> can be '.' to be installed in "
302 "the toplevel directory of installation." << std::endl);
303 return 0;
305 std::vector<std::string>::iterator it;
306 const char* tempDir = tempInstallDirectory;
307 for ( it = installDirectoriesVector.begin();
308 it != installDirectoriesVector.end();
309 ++it )
311 cmCPackLogger(cmCPackLog::LOG_DEBUG, "Find files" << std::endl);
312 cmsys::Glob gl;
313 std::string toplevel = it->c_str();
314 it ++;
315 std::string subdir = it->c_str();
316 std::string findExpr = toplevel;
317 findExpr += "/*";
318 cmCPackLogger(cmCPackLog::LOG_OUTPUT,
319 "- Install directory: " << toplevel << std::endl);
320 gl.RecurseOn();
321 if ( !gl.FindFiles(findExpr) )
323 cmCPackLogger(cmCPackLog::LOG_ERROR,
324 "Cannot find any files in the installed directory" << std::endl);
325 return 0;
327 std::vector<std::string>& files = gl.GetFiles();
328 std::vector<std::string>::iterator gfit;
329 std::vector<cmsys::RegularExpression>::iterator regIt;
330 for ( gfit = files.begin(); gfit != files.end(); ++ gfit )
332 bool skip = false;
333 std::string &inFile = *gfit;
334 for ( regIt= ignoreFilesRegex.begin();
335 regIt!= ignoreFilesRegex.end();
336 ++ regIt)
338 if ( regIt->find(inFile.c_str()) )
340 cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Ignore file: "
341 << inFile.c_str() << std::endl);
342 skip = true;
345 if ( skip )
347 continue;
349 std::string filePath = tempDir;
350 filePath += "/" + subdir + "/"
351 + cmSystemTools::RelativePath(toplevel.c_str(), gfit->c_str());
352 cmCPackLogger(cmCPackLog::LOG_DEBUG, "Copy file: "
353 << inFile.c_str() << " -> " << filePath.c_str() << std::endl);
354 if ( !cmSystemTools::CopyFileIfDifferent(inFile.c_str(),
355 filePath.c_str()) )
357 cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem copying file: "
358 << inFile.c_str() << " -> " << filePath.c_str() << std::endl);
359 return 0;
364 return 1;
367 //----------------------------------------------------------------------
368 int cmCPackGenerator::InstallProjectViaInstallScript(
369 bool setDestDir, const char* tempInstallDirectory)
371 const char* cmakeScripts
372 = this->GetOption("CPACK_INSTALL_SCRIPT");
373 std::string currentWorkingDirectory =
374 cmSystemTools::GetCurrentWorkingDirectory();
375 if ( cmakeScripts && *cmakeScripts )
377 cmCPackLogger(cmCPackLog::LOG_OUTPUT,
378 "- Install scripts: " << cmakeScripts << std::endl);
379 std::vector<std::string> cmakeScriptsVector;
380 cmSystemTools::ExpandListArgument(cmakeScripts,
381 cmakeScriptsVector);
382 std::vector<std::string>::iterator it;
383 for ( it = cmakeScriptsVector.begin();
384 it != cmakeScriptsVector.end();
385 ++it )
387 std::string installScript = it->c_str();
389 cmCPackLogger(cmCPackLog::LOG_OUTPUT,
390 "- Install script: " << installScript << std::endl);
392 if ( setDestDir )
394 // For DESTDIR based packaging, use the *project* CMAKE_INSTALL_PREFIX
395 // underneath the tempInstallDirectory. The value of the project's
396 // CMAKE_INSTALL_PREFIX is sent in here as the value of the
397 // CPACK_INSTALL_PREFIX variable.
398 std::string dir = tempInstallDirectory;
399 if (this->GetOption("CPACK_INSTALL_PREFIX"))
401 dir += this->GetOption("CPACK_INSTALL_PREFIX");
403 this->SetOption("CMAKE_INSTALL_PREFIX", dir.c_str());
405 cmCPackLogger(cmCPackLog::LOG_DEBUG,
406 "- Using DESTDIR + CPACK_INSTALL_PREFIX... (this->SetOption)"
407 << std::endl);
408 cmCPackLogger(cmCPackLog::LOG_DEBUG,
409 "- Setting CMAKE_INSTALL_PREFIX to '" << dir << "'" << std::endl);
411 else
413 this->SetOption("CMAKE_INSTALL_PREFIX", tempInstallDirectory);
415 cmCPackLogger(cmCPackLog::LOG_DEBUG,
416 "- Using non-DESTDIR install... (this->SetOption)" << std::endl);
417 cmCPackLogger(cmCPackLog::LOG_DEBUG,
418 "- Setting CMAKE_INSTALL_PREFIX to '" << tempInstallDirectory
419 << "'" << std::endl);
422 this->SetOptionIfNotSet("CMAKE_CURRENT_BINARY_DIR",
423 tempInstallDirectory);
424 this->SetOptionIfNotSet("CMAKE_CURRENT_SOURCE_DIR",
425 tempInstallDirectory);
426 int res = this->MakefileMap->ReadListFile(0, installScript.c_str());
427 if ( cmSystemTools::GetErrorOccuredFlag() || !res )
429 return 0;
433 return 1;
436 //----------------------------------------------------------------------
437 int cmCPackGenerator::InstallProjectViaInstallCMakeProjects(
438 bool setDestDir, const char* tempInstallDirectory)
440 const char* cmakeProjects
441 = this->GetOption("CPACK_INSTALL_CMAKE_PROJECTS");
442 const char* cmakeGenerator
443 = this->GetOption("CPACK_CMAKE_GENERATOR");
444 std::string currentWorkingDirectory =
445 cmSystemTools::GetCurrentWorkingDirectory();
446 if ( cmakeProjects && *cmakeProjects )
448 if ( !cmakeGenerator )
450 cmCPackLogger(cmCPackLog::LOG_ERROR,
451 "CPACK_INSTALL_CMAKE_PROJECTS is specified, but "
452 "CPACK_CMAKE_GENERATOR is not. CPACK_CMAKE_GENERATOR "
453 "is required to install the project."
454 << std::endl);
455 return 0;
457 std::vector<std::string> cmakeProjectsVector;
458 cmSystemTools::ExpandListArgument(cmakeProjects,
459 cmakeProjectsVector);
460 std::vector<std::string>::iterator it;
461 for ( it = cmakeProjectsVector.begin();
462 it != cmakeProjectsVector.end();
463 ++it )
465 if ( it+1 == cmakeProjectsVector.end() ||
466 it+2 == cmakeProjectsVector.end() ||
467 it+3 == cmakeProjectsVector.end() )
469 cmCPackLogger(cmCPackLog::LOG_ERROR,
470 "Not enough items on list: CPACK_INSTALL_CMAKE_PROJECTS. "
471 "CPACK_INSTALL_CMAKE_PROJECTS should hold quadruplet of install "
472 "directory, install project name, install component, and install "
473 "subdirectory."
474 << std::endl);
475 return 0;
477 std::string installDirectory = it->c_str();
478 ++it;
479 std::string installProjectName = it->c_str();
480 ++it;
481 std::string installComponent = it->c_str();
482 ++it;
483 std::string installSubDirectory = it->c_str();
484 std::string installFile = installDirectory + "/cmake_install.cmake";
486 const char* buildConfig = this->GetOption("CPACK_BUILD_CONFIG");
487 cmGlobalGenerator* globalGenerator
488 = this->MakefileMap->GetCMakeInstance()->CreateGlobalGenerator(
489 cmakeGenerator);
490 // set the global flag for unix style paths on cmSystemTools as
491 // soon as the generator is set. This allows gmake to be used
492 // on windows.
493 cmSystemTools::SetForceUnixPaths(globalGenerator->GetForceUnixPaths());
495 // Does this generator require pre-install?
496 if ( globalGenerator->GetPreinstallTargetName() )
498 globalGenerator->FindMakeProgram(this->MakefileMap);
499 const char* cmakeMakeProgram
500 = this->MakefileMap->GetDefinition("CMAKE_MAKE_PROGRAM");
501 std::string buildCommand
502 = globalGenerator->GenerateBuildCommand(cmakeMakeProgram,
503 installProjectName.c_str(), 0,
504 globalGenerator->GetPreinstallTargetName(),
505 buildConfig, false, false);
506 cmCPackLogger(cmCPackLog::LOG_DEBUG,
507 "- Install command: " << buildCommand << std::endl);
508 cmCPackLogger(cmCPackLog::LOG_OUTPUT,
509 "- Run preinstall target for: " << installProjectName << std::endl);
510 std::string output;
511 int retVal = 1;
512 bool resB =
513 cmSystemTools::RunSingleCommand(buildCommand.c_str(),
514 &output,
515 &retVal,
516 installDirectory.c_str(),
517 this->GeneratorVerbose, 0);
518 if ( !resB || retVal )
520 std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
521 tmpFile += "/PreinstallOutput.log";
522 cmGeneratedFileStream ofs(tmpFile.c_str());
523 ofs << "# Run command: " << buildCommand.c_str() << std::endl
524 << "# Directory: " << installDirectory.c_str() << std::endl
525 << "# Output:" << std::endl
526 << output.c_str() << std::endl;
527 cmCPackLogger(cmCPackLog::LOG_ERROR,
528 "Problem running install command: " << buildCommand.c_str()
529 << std::endl
530 << "Please check " << tmpFile.c_str() << " for errors"
531 << std::endl);
532 return 0;
535 delete globalGenerator;
537 cmCPackLogger(cmCPackLog::LOG_OUTPUT,
538 "- Install project: " << installProjectName << std::endl);
539 cmake cm;
540 cm.SetProgressCallback(cmCPackGeneratorProgress, this);
541 cmGlobalGenerator gg;
542 gg.SetCMakeInstance(&cm);
543 std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
544 lg->SetGlobalGenerator(&gg);
545 cmMakefile *mf = lg->GetMakefile();
546 std::string realInstallDirectory = tempInstallDirectory;
547 if ( !installSubDirectory.empty() && installSubDirectory != "/" )
549 realInstallDirectory += installSubDirectory;
552 if ( setDestDir )
554 // For DESTDIR based packaging, use the *project* CMAKE_INSTALL_PREFIX
555 // underneath the tempInstallDirectory. The value of the project's
556 // CMAKE_INSTALL_PREFIX is sent in here as the value of the
557 // CPACK_INSTALL_PREFIX variable.
558 std::string dir = tempInstallDirectory;
559 if (this->GetOption("CPACK_INSTALL_PREFIX"))
561 dir += this->GetOption("CPACK_INSTALL_PREFIX");
563 mf->AddDefinition("CMAKE_INSTALL_PREFIX", dir.c_str());
565 cmCPackLogger(cmCPackLog::LOG_DEBUG,
566 "- Using DESTDIR + CPACK_INSTALL_PREFIX... (mf->AddDefinition)"
567 << std::endl);
568 cmCPackLogger(cmCPackLog::LOG_DEBUG,
569 "- Setting CMAKE_INSTALL_PREFIX to '" << dir << "'" << std::endl);
571 else
573 mf->AddDefinition("CMAKE_INSTALL_PREFIX", tempInstallDirectory);
575 cmCPackLogger(cmCPackLog::LOG_DEBUG,
576 "- Using non-DESTDIR install... (mf->AddDefinition)" << std::endl);
577 cmCPackLogger(cmCPackLog::LOG_DEBUG,
578 "- Setting CMAKE_INSTALL_PREFIX to '" << tempInstallDirectory
579 << "'" << std::endl);
582 if ( buildConfig && *buildConfig )
584 mf->AddDefinition("BUILD_TYPE", buildConfig);
586 std::string installComponentLowerCase
587 = cmSystemTools::LowerCase(installComponent);
588 if ( installComponentLowerCase != "all" )
590 mf->AddDefinition("CMAKE_INSTALL_COMPONENT",
591 installComponent.c_str());
594 // strip on TRUE, ON, 1, one or several file names, but not on
595 // FALSE, OFF, 0 and an empty string
596 if (!cmSystemTools::IsOff(this->GetOption("CPACK_STRIP_FILES")))
598 mf->AddDefinition("CMAKE_INSTALL_DO_STRIP", "1");
601 int res = mf->ReadListFile(0, installFile.c_str());
602 if ( cmSystemTools::GetErrorOccuredFlag() || !res )
604 return 0;
608 return 1;
611 //----------------------------------------------------------------------
612 bool cmCPackGenerator::ReadListFile(const char* moduleName)
614 std::string fullPath = this->MakefileMap->GetModulesFile(moduleName);
615 return this->MakefileMap->ReadListFile(0, fullPath.c_str());
618 //----------------------------------------------------------------------
619 void cmCPackGenerator::SetOptionIfNotSet(const char* op,
620 const char* value)
622 const char* def = this->MakefileMap->GetDefinition(op);
623 if ( def && *def )
625 return;
627 this->SetOption(op, value);
630 //----------------------------------------------------------------------
631 void cmCPackGenerator::SetOption(const char* op, const char* value)
633 if ( !op )
635 return;
637 if ( !value )
639 this->MakefileMap->RemoveDefinition(op);
640 return;
642 cmCPackLogger(cmCPackLog::LOG_DEBUG, this->GetNameOfClass()
643 << "::SetOption(" << op << ", " << value << ")" << std::endl);
644 this->MakefileMap->AddDefinition(op, value);
647 //----------------------------------------------------------------------
648 int cmCPackGenerator::DoPackage()
650 cmCPackLogger(cmCPackLog::LOG_OUTPUT,
651 "Create package using " << this->Name.c_str() << std::endl);
653 if ( !this->PrepareNames() )
655 return 0;
657 if ( cmSystemTools::IsOn(
658 this->GetOption("CPACK_REMOVE_TOPLEVEL_DIRECTORY")) )
660 const char* toplevelDirectory
661 = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
662 if ( cmSystemTools::FileExists(toplevelDirectory) )
664 cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Remove toplevel directory: "
665 << toplevelDirectory << std::endl);
666 if ( !cmSystemTools::RemoveADirectory(toplevelDirectory) )
668 cmCPackLogger(cmCPackLog::LOG_ERROR,
669 "Problem removing toplevel directory: "
670 << toplevelDirectory
671 << std::endl);
672 return 0;
676 if ( !this->InstallProject() )
678 return 0;
681 const char* tempPackageFileName = this->GetOption(
682 "CPACK_TEMPORARY_PACKAGE_FILE_NAME");
683 const char* packageFileName = this->GetOption("CPACK_OUTPUT_FILE_PATH");
684 const char* tempDirectory = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
687 cmCPackLogger(cmCPackLog::LOG_DEBUG, "Find files" << std::endl);
688 cmsys::Glob gl;
689 std::string findExpr = tempDirectory;
690 findExpr += "/*";
691 gl.RecurseOn();
692 if ( !gl.FindFiles(findExpr) )
694 cmCPackLogger(cmCPackLog::LOG_ERROR,
695 "Cannot find any files in the packaging tree" << std::endl);
696 return 0;
699 cmCPackLogger(cmCPackLog::LOG_OUTPUT, "Compress package" << std::endl);
700 cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Compress files to: "
701 << (tempPackageFileName ? tempPackageFileName : "(NULL)") << std::endl);
702 if ( cmSystemTools::FileExists(tempPackageFileName) )
704 cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Remove old package file"
705 << std::endl);
706 cmSystemTools::RemoveFile(tempPackageFileName);
708 if ( cmSystemTools::IsOn(this->GetOption(
709 "CPACK_INCLUDE_TOPLEVEL_DIRECTORY")) )
711 tempDirectory = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
713 if ( !this->CompressFiles(tempPackageFileName,
714 tempDirectory, gl.GetFiles()) || cmSystemTools::GetErrorOccuredFlag())
716 cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem compressing the directory"
717 << std::endl);
718 return 0;
721 cmCPackLogger(cmCPackLog::LOG_OUTPUT, "Finalize package" << std::endl);
722 cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Copy final package: "
723 << (tempPackageFileName ? tempPackageFileName : "(NULL)" )
724 << " to "
725 << (packageFileName ? packageFileName : "(NULL)")
726 << std::endl);
727 if ( !cmSystemTools::CopyFileIfDifferent(tempPackageFileName,
728 packageFileName) )
730 cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem copying the package: "
731 << (tempPackageFileName ? tempPackageFileName : "(NULL)" )
732 << " to "
733 << (packageFileName ? packageFileName : "(NULL)")
734 << std::endl);
735 return 0;
738 cmCPackLogger(cmCPackLog::LOG_OUTPUT, "Package "
739 << (packageFileName ? packageFileName : "(NULL)")
740 << " generated." << std::endl);
741 return 1;
744 //----------------------------------------------------------------------
745 int cmCPackGenerator::Initialize(const char* name, cmMakefile* mf,
746 const char* argv0)
748 this->MakefileMap = mf;
749 this->Name = name;
750 if ( !this->FindRunningCMake(argv0) )
752 cmCPackLogger(cmCPackLog::LOG_ERROR,
753 "Cannot initialize the generator" << std::endl);
754 return 0;
756 // set the running generator name
757 this->SetOption("CPACK_GENERATOR", this->Name.c_str());
758 // Load the project specific config file
759 const char* config =
760 this->GetOption("CPACK_PROJECT_CONFIG_FILE");
761 if(config)
763 mf->ReadListFile(config);
765 int result = this->InitializeInternal();
766 if (cmSystemTools::GetErrorOccuredFlag())
768 return 0;
771 // If a generator subclass did not already set this option in its
772 // InitializeInternal implementation, and the project did not already set
773 // it, the default value should be:
774 this->SetOptionIfNotSet("CPACK_PACKAGING_INSTALL_PREFIX", "/");
776 return result;
779 //----------------------------------------------------------------------
780 int cmCPackGenerator::InitializeInternal()
782 return 1;
785 //----------------------------------------------------------------------
786 bool cmCPackGenerator::IsSet(const char* name) const
788 return this->MakefileMap->IsSet(name);
791 //----------------------------------------------------------------------
792 const char* cmCPackGenerator::GetOption(const char* op)
794 return this->MakefileMap->GetDefinition(op);
797 //----------------------------------------------------------------------
798 int cmCPackGenerator::FindRunningCMake(const char* arg0)
800 int found = 0;
801 // Find our own executable.
802 std::vector<cmStdString> failures;
803 this->CPackSelf = arg0;
804 cmSystemTools::ConvertToUnixSlashes(this->CPackSelf);
805 failures.push_back(this->CPackSelf);
806 this->CPackSelf = cmSystemTools::FindProgram(this->CPackSelf.c_str());
807 if(!cmSystemTools::FileExists(this->CPackSelf.c_str()))
809 failures.push_back(this->CPackSelf);
810 this->CPackSelf = "/usr/local/bin/ctest";
812 if(!cmSystemTools::FileExists(this->CPackSelf.c_str()))
814 failures.push_back(this->CPackSelf);
815 cmOStringStream msg;
816 msg << "CPack can not find the command line program ctest.\n";
817 msg << " argv[0] = \"" << arg0 << "\"\n";
818 msg << " Attempted paths:\n";
819 std::vector<cmStdString>::iterator i;
820 for(i=failures.begin(); i != failures.end(); ++i)
822 msg << " \"" << i->c_str() << "\"\n";
824 cmCPackLogger(cmCPackLog::LOG_ERROR, msg.str().c_str()
825 << std::endl);
826 return 0;
828 std::string dir;
829 std::string file;
830 if(cmSystemTools::SplitProgramPath(this->CPackSelf.c_str(),
831 dir, file, true))
833 this->CMakeSelf = dir += "/cmake";
834 this->CMakeSelf += cmSystemTools::GetExecutableExtension();
835 if(cmSystemTools::FileExists(this->CMakeSelf.c_str()))
837 found = 1;
840 if ( !found )
842 failures.push_back(this->CMakeSelf);
843 #ifdef CMAKE_BUILD_DIR
844 std::string intdir = ".";
845 #ifdef CMAKE_INTDIR
846 intdir = CMAKE_INTDIR;
847 #endif
848 this->CMakeSelf = CMAKE_BUILD_DIR;
849 this->CMakeSelf += "/bin/";
850 this->CMakeSelf += intdir;
851 this->CMakeSelf += "/cmake";
852 this->CMakeSelf += cmSystemTools::GetExecutableExtension();
853 #endif
854 if(!cmSystemTools::FileExists(this->CMakeSelf.c_str()))
856 failures.push_back(this->CMakeSelf);
857 cmOStringStream msg;
858 msg << "CPack can not find the command line program cmake.\n";
859 msg << " argv[0] = \"" << arg0 << "\"\n";
860 msg << " Attempted paths:\n";
861 std::vector<cmStdString>::iterator i;
862 for(i=failures.begin(); i != failures.end(); ++i)
864 msg << " \"" << i->c_str() << "\"\n";
866 cmCPackLogger(cmCPackLog::LOG_ERROR, msg.str().c_str()
867 << std::endl);
868 return 0;
871 // do CMAKE_ROOT, look for the environment variable first
872 std::string cMakeRoot;
873 std::string modules;
874 cmCPackLogger(cmCPackLog::LOG_DEBUG, "Looking for CMAKE_ROOT" << std::endl);
875 if (getenv("CMAKE_ROOT"))
877 cMakeRoot = getenv("CMAKE_ROOT");
878 modules = cMakeRoot + "/Modules/CMake.cmake";
880 if(modules.empty() || !cmSystemTools::FileExists(modules.c_str()))
882 // next try exe/..
883 cMakeRoot = cmSystemTools::GetProgramPath(this->CMakeSelf.c_str());
884 std::string::size_type slashPos = cMakeRoot.rfind("/");
885 if(slashPos != std::string::npos)
887 cMakeRoot = cMakeRoot.substr(0, slashPos);
889 // is there no Modules direcory there?
890 modules = cMakeRoot + "/Modules/CMake.cmake";
891 cmCPackLogger(cmCPackLog::LOG_DEBUG, "Looking for CMAKE_ROOT: "
892 << modules.c_str() << std::endl);
895 if (!cmSystemTools::FileExists(modules.c_str()))
897 // try exe/../share/cmake
898 cMakeRoot += CMAKE_DATA_DIR;
899 modules = cMakeRoot + "/Modules/CMake.cmake";
900 cmCPackLogger(cmCPackLog::LOG_DEBUG, "Looking for CMAKE_ROOT: "
901 << modules.c_str() << std::endl);
903 #ifdef CMAKE_ROOT_DIR
904 if (!cmSystemTools::FileExists(modules.c_str()))
906 // try compiled in root directory
907 cMakeRoot = CMAKE_ROOT_DIR;
908 modules = cMakeRoot + "/Modules/CMake.cmake";
909 cmCPackLogger(cmCPackLog::LOG_DEBUG, "Looking for CMAKE_ROOT: "
910 << modules.c_str() << std::endl);
912 #endif
913 #ifdef CMAKE_PREFIX
914 if (!cmSystemTools::FileExists(modules.c_str()))
916 // try compiled in install prefix
917 cMakeRoot = CMAKE_PREFIX CMAKE_DATA_DIR;
918 modules = cMakeRoot + "/Modules/CMake.cmake";
919 cmCPackLogger(cmCPackLog::LOG_DEBUG, "Looking for CMAKE_ROOT: "
920 << modules.c_str() << std::endl);
922 #endif
923 if (!cmSystemTools::FileExists(modules.c_str()))
925 // try
926 cMakeRoot = cmSystemTools::GetProgramPath(this->CMakeSelf.c_str());
927 cMakeRoot += CMAKE_DATA_DIR;
928 modules = cMakeRoot + "/Modules/CMake.cmake";
929 cmCPackLogger(cmCPackLog::LOG_DEBUG, "Looking for CMAKE_ROOT: "
930 << modules.c_str() << std::endl);
932 if(!cmSystemTools::FileExists(modules.c_str()))
934 // next try exe
935 cMakeRoot = cmSystemTools::GetProgramPath(this->CMakeSelf.c_str());
936 // is there no Modules direcory there?
937 modules = cMakeRoot + "/Modules/CMake.cmake";
938 cmCPackLogger(cmCPackLog::LOG_DEBUG, "Looking for CMAKE_ROOT: "
939 << modules.c_str() << std::endl);
941 if (!cmSystemTools::FileExists(modules.c_str()))
943 // couldn't find modules
944 cmCPackLogger(cmCPackLog::LOG_ERROR,
945 "Could not find CMAKE_ROOT !!!" << std::endl
946 << "CMake has most likely not been installed correctly." << std::endl
947 <<"Modules directory not found in" << std::endl
948 << cMakeRoot.c_str()
949 << std::endl);
950 return 0;
952 this->CMakeRoot = cMakeRoot;
953 cmCPackLogger(cmCPackLog::LOG_DEBUG, "Looking for CMAKE_ROOT: "
954 << this->CMakeRoot.c_str() << std::endl);
955 this->SetOption("CMAKE_ROOT", this->CMakeRoot.c_str());
956 return 1;
959 //----------------------------------------------------------------------
960 int cmCPackGenerator::CompressFiles(const char* outFileName,
961 const char* toplevel, const std::vector<std::string>& files)
963 (void)outFileName;
964 (void)toplevel;
965 (void)files;
966 return 0;
969 //----------------------------------------------------------------------
970 const char* cmCPackGenerator::GetInstallPath()
972 if ( !this->InstallPath.empty() )
974 return this->InstallPath.c_str();
976 #if defined(_WIN32) && !defined(__CYGWIN__)
977 const char* prgfiles = cmsys::SystemTools::GetEnv("ProgramFiles");
978 const char* sysDrive = cmsys::SystemTools::GetEnv("SystemDrive");
979 if ( prgfiles )
981 this->InstallPath = prgfiles;
983 else if ( sysDrive )
985 this->InstallPath = sysDrive;
986 this->InstallPath += "/Program Files";
988 else
990 this->InstallPath = "c:/Program Files";
992 this->InstallPath += "/";
993 this->InstallPath += this->GetOption("CPACK_PACKAGE_NAME");
994 this->InstallPath += "-";
995 this->InstallPath += this->GetOption("CPACK_PACKAGE_VERSION");
996 #else
997 this->InstallPath = "/usr/local/";
998 #endif
999 return this->InstallPath.c_str();
1002 //----------------------------------------------------------------------
1003 const char* cmCPackGenerator::GetPackagingInstallPrefix()
1005 cmCPackLogger(cmCPackLog::LOG_DEBUG, "GetPackagingInstallPrefix: '"
1006 << this->GetOption("CPACK_PACKAGING_INSTALL_PREFIX") << "'" << std::endl);
1008 return this->GetOption("CPACK_PACKAGING_INSTALL_PREFIX");
1011 //----------------------------------------------------------------------
1012 std::string cmCPackGenerator::FindTemplate(const char* name)
1014 cmCPackLogger(cmCPackLog::LOG_DEBUG, "Look for template: "
1015 << (name ? name : "(NULL)") << std::endl);
1016 std::string ffile = this->MakefileMap->GetModulesFile(name);
1017 cmCPackLogger(cmCPackLog::LOG_DEBUG, "Found template: "
1018 << ffile.c_str() << std::endl);
1019 return ffile;
1022 //----------------------------------------------------------------------
1023 bool cmCPackGenerator::ConfigureString(const std::string& inString,
1024 std::string& outString)
1026 this->MakefileMap->ConfigureString(inString,
1027 outString, true, false);
1028 return true;
1031 //----------------------------------------------------------------------
1032 bool cmCPackGenerator::ConfigureFile(const char* inName,
1033 const char* outName, bool copyOnly /* = false */)
1035 return this->MakefileMap->ConfigureFile(inName, outName,
1036 copyOnly, true, false) == 1;
1039 //----------------------------------------------------------------------
1040 int cmCPackGenerator::CleanTemporaryDirectory()
1042 std::string tempInstallDirectoryWithPostfix
1043 = this->GetOption("CPACK_TEMPORARY_INSTALL_DIRECTORY");
1044 const char* tempInstallDirectory = tempInstallDirectoryWithPostfix.c_str();
1045 if(cmsys::SystemTools::FileExists(tempInstallDirectory))
1047 cmCPackLogger(cmCPackLog::LOG_OUTPUT,
1048 "- Clean temporary : "
1049 << tempInstallDirectory << std::endl);
1050 if(!cmsys::SystemTools::RemoveADirectory(tempInstallDirectory))
1052 cmCPackLogger(cmCPackLog::LOG_ERROR,
1053 "Problem removing temporary directory: " <<
1054 tempInstallDirectory
1055 << std::endl);
1056 return 0;
1059 return 1;