1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmCPackGenerator.cxx,v $
6 Date: $Date: 2008-03-07 16:06:44 $
7 Version: $Revision: 1.5 $
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"
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;
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
,
57 cmCPackLogger(cmCPackLog::LOG_VERBOSE
, "" << msg
<< std::endl
);
60 //----------------------------------------------------------------------
61 int cmCPackGenerator::PrepareNames()
63 cmCPackLogger(cmCPackLog::LOG_DEBUG
,
64 "Create temp directory." << std::endl
);
66 std::string tempDirectory
= this->GetOption("CPACK_PACKAGE_DIRECTORY");
67 tempDirectory
+= "/_CPack_Packages/";
68 const char* toplevelTag
= this->GetOption("CPACK_TOPLEVEL_TAG");
71 tempDirectory
+= toplevelTag
;
74 tempDirectory
+= this->GetOption("CPACK_GENERATOR");
75 std::string topDirectory
= tempDirectory
;
76 this->GetOption("CPACK_PACKAGE_FILE_NAME");
77 const char* pfname
= this->GetOption("CPACK_PACKAGE_FILE_NAME");
80 cmCPackLogger(cmCPackLog::LOG_ERROR
,
81 "CPACK_PACKAGE_FILE_NAME not specified" << std::endl
);
84 std::string outName
= pfname
;
85 tempDirectory
+= "/" + outName
;
86 if(!this->GetOutputExtension())
88 cmCPackLogger(cmCPackLog::LOG_ERROR
,
89 "No output extension specified" << std::endl
);
92 outName
+= this->GetOutputExtension();
93 const char* pdir
= this->GetOption("CPACK_PACKAGE_DIRECTORY");
96 cmCPackLogger(cmCPackLog::LOG_ERROR
,
97 "CPACK_PACKAGE_DIRECTORY not specified" << std::endl
);
101 std::string destFile
= pdir
;
102 destFile
+= "/" + outName
;
103 std::string outFile
= topDirectory
+ "/" + outName
;
104 bool setDestDir
= cmSystemTools::IsOn(this->GetOption("CPACK_SET_DESTDIR"));
105 std::string installPrefix
= tempDirectory
;
108 installPrefix
+= this->GetPackagingInstallPrefix();
111 this->SetOptionIfNotSet("CPACK_TOPLEVEL_DIRECTORY", topDirectory
.c_str());
112 this->SetOptionIfNotSet("CPACK_TEMPORARY_DIRECTORY", tempDirectory
.c_str());
113 this->SetOptionIfNotSet("CPACK_OUTPUT_FILE_NAME", outName
.c_str());
114 this->SetOptionIfNotSet("CPACK_OUTPUT_FILE_PATH", destFile
.c_str());
115 this->SetOptionIfNotSet("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
117 this->SetOptionIfNotSet("CPACK_INSTALL_DIRECTORY", this->GetInstallPath());
118 this->SetOptionIfNotSet("CPACK_NATIVE_INSTALL_DIRECTORY",
119 cmsys::SystemTools::ConvertToOutputPath(this->GetInstallPath()).c_str());
120 this->SetOptionIfNotSet("CPACK_TEMPORARY_INSTALL_DIRECTORY",
121 installPrefix
.c_str());
123 cmCPackLogger(cmCPackLog::LOG_DEBUG
,
124 "Look for: CPACK_PACKAGE_DESCRIPTION_FILE" << std::endl
);
125 const char* descFileName
126 = this->GetOption("CPACK_PACKAGE_DESCRIPTION_FILE");
129 cmCPackLogger(cmCPackLog::LOG_DEBUG
,
130 "Look for: " << descFileName
<< std::endl
);
131 if ( !cmSystemTools::FileExists(descFileName
) )
133 cmCPackLogger(cmCPackLog::LOG_ERROR
,
134 "Cannot find description file name: ["
135 << descFileName
<< "]" << std::endl
);
138 std::ifstream
ifs(descFileName
);
141 cmCPackLogger(cmCPackLog::LOG_ERROR
,
142 "Cannot open description file name: " << descFileName
<< std::endl
);
145 cmOStringStream ostr
;
148 cmCPackLogger(cmCPackLog::LOG_VERBOSE
,
149 "Read description file: " << descFileName
<< std::endl
);
150 while ( ifs
&& cmSystemTools::GetLineFromStream(ifs
, line
) )
152 ostr
<< cmSystemTools::MakeXMLSafe(line
.c_str()) << std::endl
;
154 this->SetOptionIfNotSet("CPACK_PACKAGE_DESCRIPTION", ostr
.str().c_str());
156 if ( !this->GetOption("CPACK_PACKAGE_DESCRIPTION") )
158 cmCPackLogger(cmCPackLog::LOG_ERROR
,
159 "Project description not specified. Please specify "
160 "CPACK_PACKAGE_DESCRIPTION or CPACK_PACKAGE_DESCRIPTION_FILE."
165 this->SetOptionIfNotSet("CPACK_REMOVE_TOPLEVEL_DIRECTORY", "1");
170 //----------------------------------------------------------------------
171 int cmCPackGenerator::InstallProject()
173 cmCPackLogger(cmCPackLog::LOG_OUTPUT
, "Install projects" << std::endl
);
174 this->CleanTemporaryDirectory();
175 std::string tempInstallDirectoryWithPostfix
176 = this->GetOption("CPACK_TEMPORARY_INSTALL_DIRECTORY");
177 const char* tempInstallDirectory
= tempInstallDirectoryWithPostfix
.c_str();
179 if ( !cmsys::SystemTools::MakeDirectory(tempInstallDirectory
))
181 cmCPackLogger(cmCPackLog::LOG_ERROR
,
182 "Problem creating temporary directory: "
183 << (tempInstallDirectory
? tempInstallDirectory
: "(NULL}")
188 bool setDestDir
= cmSystemTools::IsOn(this->GetOption("CPACK_SET_DESTDIR"));
191 std::string destDir
= "DESTDIR=";
192 destDir
+= tempInstallDirectory
;
193 cmSystemTools::PutEnv(destDir
.c_str());
197 // Make sure there is no destdir
198 cmSystemTools::PutEnv("DESTDIR=");
201 // If the CPackConfig file sets CPACK_INSTALL_COMMANDS then run them
203 if ( !this->InstallProjectViaInstallCommands(
204 setDestDir
, tempInstallDirectory
) )
209 // If the CPackConfig file sets CPACK_INSTALL_SCRIPT then run them
211 if ( !this->InstallProjectViaInstallScript(
212 setDestDir
, tempInstallDirectory
) )
217 // If the CPackConfig file sets CPACK_INSTALLED_DIRECTORIES
218 // then glob it and copy it to CPACK_TEMPORARY_DIRECTORY
219 // This is used in Source packageing
220 if ( !this->InstallProjectViaInstalledDirectories(
221 setDestDir
, tempInstallDirectory
) )
227 // If the project is a CMAKE project then run pre-install
228 // and then read the cmake_install script to run it
229 if ( !this->InstallProjectViaInstallCMakeProjects(
230 setDestDir
, tempInstallDirectory
) )
237 cmSystemTools::PutEnv("DESTDIR=");
243 //----------------------------------------------------------------------
244 int cmCPackGenerator::InstallProjectViaInstallCommands(
245 bool setDestDir
, const char* tempInstallDirectory
)
248 (void)tempInstallDirectory
;
249 const char* installCommands
= this->GetOption("CPACK_INSTALL_COMMANDS");
250 if ( installCommands
&& *installCommands
)
252 std::string tempInstallDirectoryEnv
= "CMAKE_INSTALL_PREFIX=";
253 tempInstallDirectoryEnv
+= tempInstallDirectory
;
254 cmSystemTools::PutEnv(tempInstallDirectoryEnv
.c_str());
255 std::vector
<std::string
> installCommandsVector
;
256 cmSystemTools::ExpandListArgument(installCommands
,installCommandsVector
);
257 std::vector
<std::string
>::iterator it
;
258 for ( it
= installCommandsVector
.begin();
259 it
!= installCommandsVector
.end();
262 cmCPackLogger(cmCPackLog::LOG_VERBOSE
, "Execute: " << it
->c_str()
266 bool resB
= cmSystemTools::RunSingleCommand(it
->c_str(), &output
,
267 &retVal
, 0, this->GeneratorVerbose
, 0);
268 if ( !resB
|| retVal
)
270 std::string tmpFile
= this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
271 tmpFile
+= "/InstallOutput.log";
272 cmGeneratedFileStream
ofs(tmpFile
.c_str());
273 ofs
<< "# Run command: " << it
->c_str() << std::endl
274 << "# Output:" << std::endl
275 << output
.c_str() << std::endl
;
276 cmCPackLogger(cmCPackLog::LOG_ERROR
,
277 "Problem running install command: " << it
->c_str() << std::endl
278 << "Please check " << tmpFile
.c_str() << " for errors"
287 //----------------------------------------------------------------------
288 int cmCPackGenerator::InstallProjectViaInstalledDirectories(
289 bool setDestDir
, const char* tempInstallDirectory
)
292 (void)tempInstallDirectory
;
293 std::vector
<cmsys::RegularExpression
> ignoreFilesRegex
;
294 const char* cpackIgnoreFiles
= this->GetOption("CPACK_IGNORE_FILES");
295 if ( cpackIgnoreFiles
)
297 std::vector
<std::string
> ignoreFilesRegexString
;
298 cmSystemTools::ExpandListArgument(cpackIgnoreFiles
,
299 ignoreFilesRegexString
);
300 std::vector
<std::string
>::iterator it
;
301 for ( it
= ignoreFilesRegexString
.begin();
302 it
!= ignoreFilesRegexString
.end();
305 cmCPackLogger(cmCPackLog::LOG_VERBOSE
,
306 "Create ignore files regex for: " << it
->c_str() << std::endl
);
307 ignoreFilesRegex
.push_back(it
->c_str());
310 const char* installDirectories
311 = this->GetOption("CPACK_INSTALLED_DIRECTORIES");
312 if ( installDirectories
&& *installDirectories
)
314 std::vector
<std::string
> installDirectoriesVector
;
315 cmSystemTools::ExpandListArgument(installDirectories
,
316 installDirectoriesVector
);
317 if ( installDirectoriesVector
.size() % 2 != 0 )
319 cmCPackLogger(cmCPackLog::LOG_ERROR
,
320 "CPACK_INSTALLED_DIRECTORIES should contain pairs of <directory> and "
321 "<subdirectory>. The <subdirectory> can be '.' to be installed in "
322 "the toplevel directory of installation." << std::endl
);
325 std::vector
<std::string
>::iterator it
;
326 const char* tempDir
= tempInstallDirectory
;
327 for ( it
= installDirectoriesVector
.begin();
328 it
!= installDirectoriesVector
.end();
331 cmCPackLogger(cmCPackLog::LOG_DEBUG
, "Find files" << std::endl
);
333 std::string toplevel
= it
->c_str();
335 std::string subdir
= it
->c_str();
336 std::string findExpr
= toplevel
;
338 cmCPackLogger(cmCPackLog::LOG_OUTPUT
,
339 "- Install directory: " << toplevel
<< std::endl
);
341 if ( !gl
.FindFiles(findExpr
) )
343 cmCPackLogger(cmCPackLog::LOG_ERROR
,
344 "Cannot find any files in the installed directory" << std::endl
);
347 std::vector
<std::string
>& files
= gl
.GetFiles();
348 std::vector
<std::string
>::iterator gfit
;
349 std::vector
<cmsys::RegularExpression
>::iterator regIt
;
350 for ( gfit
= files
.begin(); gfit
!= files
.end(); ++ gfit
)
353 std::string
&inFile
= *gfit
;
354 for ( regIt
= ignoreFilesRegex
.begin();
355 regIt
!= ignoreFilesRegex
.end();
358 if ( regIt
->find(inFile
.c_str()) )
360 cmCPackLogger(cmCPackLog::LOG_VERBOSE
, "Ignore file: "
361 << inFile
.c_str() << std::endl
);
369 std::string filePath
= tempDir
;
370 filePath
+= "/" + subdir
+ "/"
371 + cmSystemTools::RelativePath(toplevel
.c_str(), gfit
->c_str());
372 cmCPackLogger(cmCPackLog::LOG_DEBUG
, "Copy file: "
373 << inFile
.c_str() << " -> " << filePath
.c_str() << std::endl
);
374 if ( !cmSystemTools::CopyFileIfDifferent(inFile
.c_str(),
377 cmCPackLogger(cmCPackLog::LOG_ERROR
, "Problem copying file: "
378 << inFile
.c_str() << " -> " << filePath
.c_str() << std::endl
);
387 //----------------------------------------------------------------------
388 int cmCPackGenerator::InstallProjectViaInstallScript(
389 bool setDestDir
, const char* tempInstallDirectory
)
391 const char* cmakeScripts
392 = this->GetOption("CPACK_INSTALL_SCRIPT");
393 std::string currentWorkingDirectory
=
394 cmSystemTools::GetCurrentWorkingDirectory();
395 if ( cmakeScripts
&& *cmakeScripts
)
397 cmCPackLogger(cmCPackLog::LOG_OUTPUT
,
398 "- Install scripts: " << cmakeScripts
<< std::endl
);
399 std::vector
<std::string
> cmakeScriptsVector
;
400 cmSystemTools::ExpandListArgument(cmakeScripts
,
402 std::vector
<std::string
>::iterator it
;
403 for ( it
= cmakeScriptsVector
.begin();
404 it
!= cmakeScriptsVector
.end();
407 std::string installScript
= it
->c_str();
409 cmCPackLogger(cmCPackLog::LOG_OUTPUT
,
410 "- Install script: " << installScript
<< std::endl
);
414 // For DESTDIR based packaging, use the *project* CMAKE_INSTALL_PREFIX
415 // underneath the tempInstallDirectory. The value of the project's
416 // CMAKE_INSTALL_PREFIX is sent in here as the value of the
417 // CPACK_INSTALL_PREFIX variable.
419 if (this->GetOption("CPACK_INSTALL_PREFIX"))
421 dir
+= this->GetOption("CPACK_INSTALL_PREFIX");
423 this->SetOption("CMAKE_INSTALL_PREFIX", dir
.c_str());
424 cmCPackLogger(cmCPackLog::LOG_DEBUG
,
425 "- Using DESTDIR + CPACK_INSTALL_PREFIX... (this->SetOption)"
427 cmCPackLogger(cmCPackLog::LOG_DEBUG
,
428 "- Setting CMAKE_INSTALL_PREFIX to '" << dir
<< "'" << std::endl
);
432 this->SetOption("CMAKE_INSTALL_PREFIX", tempInstallDirectory
);
434 cmCPackLogger(cmCPackLog::LOG_DEBUG
,
435 "- Using non-DESTDIR install... (this->SetOption)" << std::endl
);
436 cmCPackLogger(cmCPackLog::LOG_DEBUG
,
437 "- Setting CMAKE_INSTALL_PREFIX to '" << tempInstallDirectory
438 << "'" << std::endl
);
441 this->SetOptionIfNotSet("CMAKE_CURRENT_BINARY_DIR",
442 tempInstallDirectory
);
443 this->SetOptionIfNotSet("CMAKE_CURRENT_SOURCE_DIR",
444 tempInstallDirectory
);
445 int res
= this->MakefileMap
->ReadListFile(0, installScript
.c_str());
446 if ( cmSystemTools::GetErrorOccuredFlag() || !res
)
455 //----------------------------------------------------------------------
456 int cmCPackGenerator::InstallProjectViaInstallCMakeProjects(
457 bool setDestDir
, const char* tempInstallDirectory
)
459 const char* cmakeProjects
460 = this->GetOption("CPACK_INSTALL_CMAKE_PROJECTS");
461 const char* cmakeGenerator
462 = this->GetOption("CPACK_CMAKE_GENERATOR");
463 std::string currentWorkingDirectory
=
464 cmSystemTools::GetCurrentWorkingDirectory();
465 if ( cmakeProjects
&& *cmakeProjects
)
467 if ( !cmakeGenerator
)
469 cmCPackLogger(cmCPackLog::LOG_ERROR
,
470 "CPACK_INSTALL_CMAKE_PROJECTS is specified, but "
471 "CPACK_CMAKE_GENERATOR is not. CPACK_CMAKE_GENERATOR "
472 "is required to install the project."
476 std::vector
<std::string
> cmakeProjectsVector
;
477 cmSystemTools::ExpandListArgument(cmakeProjects
,
478 cmakeProjectsVector
);
479 std::vector
<std::string
>::iterator it
;
480 for ( it
= cmakeProjectsVector
.begin();
481 it
!= cmakeProjectsVector
.end();
484 if ( it
+1 == cmakeProjectsVector
.end() ||
485 it
+2 == cmakeProjectsVector
.end() ||
486 it
+3 == cmakeProjectsVector
.end() )
488 cmCPackLogger(cmCPackLog::LOG_ERROR
,
489 "Not enough items on list: CPACK_INSTALL_CMAKE_PROJECTS. "
490 "CPACK_INSTALL_CMAKE_PROJECTS should hold quadruplet of install "
491 "directory, install project name, install component, and install "
496 std::string installDirectory
= it
->c_str();
498 std::string installProjectName
= it
->c_str();
500 std::string installComponent
= it
->c_str();
502 std::string installSubDirectory
= it
->c_str();
503 std::string installFile
= installDirectory
+ "/cmake_install.cmake";
505 const char* buildConfig
= this->GetOption("CPACK_BUILD_CONFIG");
506 cmGlobalGenerator
* globalGenerator
507 = this->MakefileMap
->GetCMakeInstance()->CreateGlobalGenerator(
509 // set the global flag for unix style paths on cmSystemTools as
510 // soon as the generator is set. This allows gmake to be used
512 cmSystemTools::SetForceUnixPaths(globalGenerator
->GetForceUnixPaths());
514 // Does this generator require pre-install?
515 if ( globalGenerator
->GetPreinstallTargetName() )
517 globalGenerator
->FindMakeProgram(this->MakefileMap
);
518 const char* cmakeMakeProgram
519 = this->MakefileMap
->GetDefinition("CMAKE_MAKE_PROGRAM");
520 std::string buildCommand
521 = globalGenerator
->GenerateBuildCommand(cmakeMakeProgram
,
522 installProjectName
.c_str(), 0,
523 globalGenerator
->GetPreinstallTargetName(),
524 buildConfig
, false, false);
525 cmCPackLogger(cmCPackLog::LOG_DEBUG
,
526 "- Install command: " << buildCommand
<< std::endl
);
527 cmCPackLogger(cmCPackLog::LOG_OUTPUT
,
528 "- Run preinstall target for: " << installProjectName
<< std::endl
);
532 cmSystemTools::RunSingleCommand(buildCommand
.c_str(),
535 installDirectory
.c_str(),
536 this->GeneratorVerbose
, 0);
537 if ( !resB
|| retVal
)
539 std::string tmpFile
= this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
540 tmpFile
+= "/PreinstallOutput.log";
541 cmGeneratedFileStream
ofs(tmpFile
.c_str());
542 ofs
<< "# Run command: " << buildCommand
.c_str() << std::endl
543 << "# Directory: " << installDirectory
.c_str() << std::endl
544 << "# Output:" << std::endl
545 << output
.c_str() << std::endl
;
546 cmCPackLogger(cmCPackLog::LOG_ERROR
,
547 "Problem running install command: " << buildCommand
.c_str()
549 << "Please check " << tmpFile
.c_str() << " for errors"
554 delete globalGenerator
;
556 cmCPackLogger(cmCPackLog::LOG_OUTPUT
,
557 "- Install project: " << installProjectName
<< std::endl
);
560 cm
.SetProgressCallback(cmCPackGeneratorProgress
, this);
561 cmGlobalGenerator gg
;
562 gg
.SetCMakeInstance(&cm
);
563 std::auto_ptr
<cmLocalGenerator
> lg(gg
.CreateLocalGenerator());
564 lg
->SetGlobalGenerator(&gg
);
565 cmMakefile
*mf
= lg
->GetMakefile();
566 std::string realInstallDirectory
= tempInstallDirectory
;
567 if ( !installSubDirectory
.empty() && installSubDirectory
!= "/" )
569 realInstallDirectory
+= installSubDirectory
;
574 // For DESTDIR based packaging, use the *project* CMAKE_INSTALL_PREFIX
575 // underneath the tempInstallDirectory. The value of the project's
576 // CMAKE_INSTALL_PREFIX is sent in here as the value of the
577 // CPACK_INSTALL_PREFIX variable.
579 if (this->GetOption("CPACK_INSTALL_PREFIX"))
581 dir
+= this->GetOption("CPACK_INSTALL_PREFIX");
583 mf
->AddDefinition("CMAKE_INSTALL_PREFIX", dir
.c_str());
585 cmCPackLogger(cmCPackLog::LOG_DEBUG
,
586 "- Using DESTDIR + CPACK_INSTALL_PREFIX... (mf->AddDefinition)"
588 cmCPackLogger(cmCPackLog::LOG_DEBUG
,
589 "- Setting CMAKE_INSTALL_PREFIX to '" << dir
<< "'" << std::endl
);
593 mf
->AddDefinition("CMAKE_INSTALL_PREFIX", tempInstallDirectory
);
595 cmCPackLogger(cmCPackLog::LOG_DEBUG
,
596 "- Using non-DESTDIR install... (mf->AddDefinition)" << std::endl
);
597 cmCPackLogger(cmCPackLog::LOG_DEBUG
,
598 "- Setting CMAKE_INSTALL_PREFIX to '" << tempInstallDirectory
599 << "'" << std::endl
);
602 if ( buildConfig
&& *buildConfig
)
604 mf
->AddDefinition("BUILD_TYPE", buildConfig
);
606 std::string installComponentLowerCase
607 = cmSystemTools::LowerCase(installComponent
);
608 if ( installComponentLowerCase
!= "all" )
610 mf
->AddDefinition("CMAKE_INSTALL_COMPONENT",
611 installComponent
.c_str());
614 // strip on TRUE, ON, 1, one or several file names, but not on
615 // FALSE, OFF, 0 and an empty string
616 if (!cmSystemTools::IsOff(this->GetOption("CPACK_STRIP_FILES")))
618 mf
->AddDefinition("CMAKE_INSTALL_DO_STRIP", "1");
620 int res
= mf
->ReadListFile(0, installFile
.c_str());
621 if ( cmSystemTools::GetErrorOccuredFlag() || !res
)
630 //----------------------------------------------------------------------
631 bool cmCPackGenerator::ReadListFile(const char* moduleName
)
633 std::string fullPath
= this->MakefileMap
->GetModulesFile(moduleName
);
634 return this->MakefileMap
->ReadListFile(0, fullPath
.c_str());
637 //----------------------------------------------------------------------
638 void cmCPackGenerator::SetOptionIfNotSet(const char* op
,
641 const char* def
= this->MakefileMap
->GetDefinition(op
);
646 this->SetOption(op
, value
);
649 //----------------------------------------------------------------------
650 void cmCPackGenerator::SetOption(const char* op
, const char* value
)
658 this->MakefileMap
->RemoveDefinition(op
);
661 cmCPackLogger(cmCPackLog::LOG_DEBUG
, this->GetNameOfClass()
662 << "::SetOption(" << op
<< ", " << value
<< ")" << std::endl
);
663 this->MakefileMap
->AddDefinition(op
, value
);
666 //----------------------------------------------------------------------
667 int cmCPackGenerator::DoPackage()
669 cmCPackLogger(cmCPackLog::LOG_OUTPUT
,
670 "Create package using " << this->Name
.c_str() << std::endl
);
672 if ( !this->PrepareNames() )
677 if ( cmSystemTools::IsOn(
678 this->GetOption("CPACK_REMOVE_TOPLEVEL_DIRECTORY")) )
680 const char* toplevelDirectory
681 = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
682 if ( cmSystemTools::FileExists(toplevelDirectory
) )
684 cmCPackLogger(cmCPackLog::LOG_VERBOSE
, "Remove toplevel directory: "
685 << toplevelDirectory
<< std::endl
);
686 if ( !cmSystemTools::RemoveADirectory(toplevelDirectory
) )
688 cmCPackLogger(cmCPackLog::LOG_ERROR
,
689 "Problem removing toplevel directory: "
696 cmCPackLogger(cmCPackLog::LOG_DEBUG
,
697 "About to install project " << std::endl
);
699 if ( !this->InstallProject() )
703 cmCPackLogger(cmCPackLog::LOG_DEBUG
,
704 "Done install project " << std::endl
);
707 const char* tempPackageFileName
= this->GetOption(
708 "CPACK_TEMPORARY_PACKAGE_FILE_NAME");
709 const char* packageFileName
= this->GetOption("CPACK_OUTPUT_FILE_PATH");
710 const char* tempDirectory
= this->GetOption("CPACK_TEMPORARY_DIRECTORY");
713 cmCPackLogger(cmCPackLog::LOG_DEBUG
, "Find files" << std::endl
);
715 std::string findExpr
= tempDirectory
;
718 if ( !gl
.FindFiles(findExpr
) )
720 cmCPackLogger(cmCPackLog::LOG_ERROR
,
721 "Cannot find any files in the packaging tree" << std::endl
);
725 cmCPackLogger(cmCPackLog::LOG_OUTPUT
, "Compress package" << std::endl
);
726 cmCPackLogger(cmCPackLog::LOG_VERBOSE
, "Compress files to: "
727 << (tempPackageFileName
? tempPackageFileName
: "(NULL)") << std::endl
);
728 if ( cmSystemTools::FileExists(tempPackageFileName
) )
730 cmCPackLogger(cmCPackLog::LOG_VERBOSE
, "Remove old package file"
732 cmSystemTools::RemoveFile(tempPackageFileName
);
734 if ( cmSystemTools::IsOn(this->GetOption(
735 "CPACK_INCLUDE_TOPLEVEL_DIRECTORY")) )
737 tempDirectory
= this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
739 if ( !this->CompressFiles(tempPackageFileName
,
740 tempDirectory
, gl
.GetFiles()) || cmSystemTools::GetErrorOccuredFlag())
742 cmCPackLogger(cmCPackLog::LOG_ERROR
, "Problem compressing the directory"
747 cmCPackLogger(cmCPackLog::LOG_OUTPUT
, "Finalize package" << std::endl
);
748 cmCPackLogger(cmCPackLog::LOG_VERBOSE
, "Copy final package: "
749 << (tempPackageFileName
? tempPackageFileName
: "(NULL)" )
751 << (packageFileName
? packageFileName
: "(NULL)")
753 if ( !cmSystemTools::CopyFileIfDifferent(tempPackageFileName
,
756 cmCPackLogger(cmCPackLog::LOG_ERROR
, "Problem copying the package: "
757 << (tempPackageFileName
? tempPackageFileName
: "(NULL)" )
759 << (packageFileName
? packageFileName
: "(NULL)")
764 cmCPackLogger(cmCPackLog::LOG_OUTPUT
, "Package "
765 << (packageFileName
? packageFileName
: "(NULL)")
766 << " generated." << std::endl
);
770 //----------------------------------------------------------------------
771 int cmCPackGenerator::Initialize(const char* name
, cmMakefile
* mf
,
774 this->MakefileMap
= mf
;
776 if ( !this->FindRunningCMake(argv0
) )
778 cmCPackLogger(cmCPackLog::LOG_ERROR
,
779 "Cannot initialize the generator" << std::endl
);
782 // set the running generator name
783 this->SetOption("CPACK_GENERATOR", this->Name
.c_str());
784 // Load the project specific config file
786 this->GetOption("CPACK_PROJECT_CONFIG_FILE");
789 mf
->ReadListFile(config
);
791 int result
= this->InitializeInternal();
792 if (cmSystemTools::GetErrorOccuredFlag())
797 // If a generator subclass did not already set this option in its
798 // InitializeInternal implementation, and the project did not already set
799 // it, the default value should be:
800 this->SetOptionIfNotSet("CPACK_PACKAGING_INSTALL_PREFIX", "/");
805 //----------------------------------------------------------------------
806 int cmCPackGenerator::InitializeInternal()
811 //----------------------------------------------------------------------
812 bool cmCPackGenerator::IsSet(const char* name
) const
814 return this->MakefileMap
->IsSet(name
);
817 //----------------------------------------------------------------------
818 const char* cmCPackGenerator::GetOption(const char* op
)
820 return this->MakefileMap
->GetDefinition(op
);
823 //----------------------------------------------------------------------
824 int cmCPackGenerator::FindRunningCMake(const char* arg0
)
827 // Find our own executable.
828 std::vector
<cmStdString
> failures
;
829 this->CPackSelf
= arg0
;
830 cmSystemTools::ConvertToUnixSlashes(this->CPackSelf
);
831 failures
.push_back(this->CPackSelf
);
832 this->CPackSelf
= cmSystemTools::FindProgram(this->CPackSelf
.c_str());
833 if(!cmSystemTools::FileExists(this->CPackSelf
.c_str()))
835 failures
.push_back(this->CPackSelf
);
836 this->CPackSelf
= "/usr/local/bin/ctest";
838 if(!cmSystemTools::FileExists(this->CPackSelf
.c_str()))
840 failures
.push_back(this->CPackSelf
);
842 msg
<< "CPack can not find the command line program ctest.\n";
843 msg
<< " argv[0] = \"" << arg0
<< "\"\n";
844 msg
<< " Attempted paths:\n";
845 std::vector
<cmStdString
>::iterator i
;
846 for(i
=failures
.begin(); i
!= failures
.end(); ++i
)
848 msg
<< " \"" << i
->c_str() << "\"\n";
850 cmCPackLogger(cmCPackLog::LOG_ERROR
, msg
.str().c_str()
856 if(cmSystemTools::SplitProgramPath(this->CPackSelf
.c_str(),
859 this->CMakeSelf
= dir
+= "/cmake";
860 this->CMakeSelf
+= cmSystemTools::GetExecutableExtension();
861 if(cmSystemTools::FileExists(this->CMakeSelf
.c_str()))
868 failures
.push_back(this->CMakeSelf
);
869 #ifdef CMAKE_BUILD_DIR
870 std::string intdir
= ".";
872 intdir
= CMAKE_INTDIR
;
874 this->CMakeSelf
= CMAKE_BUILD_DIR
;
875 this->CMakeSelf
+= "/bin/";
876 this->CMakeSelf
+= intdir
;
877 this->CMakeSelf
+= "/cmake";
878 this->CMakeSelf
+= cmSystemTools::GetExecutableExtension();
880 if(!cmSystemTools::FileExists(this->CMakeSelf
.c_str()))
882 failures
.push_back(this->CMakeSelf
);
884 msg
<< "CPack can not find the command line program cmake.\n";
885 msg
<< " argv[0] = \"" << arg0
<< "\"\n";
886 msg
<< " Attempted paths:\n";
887 std::vector
<cmStdString
>::iterator i
;
888 for(i
=failures
.begin(); i
!= failures
.end(); ++i
)
890 msg
<< " \"" << i
->c_str() << "\"\n";
892 cmCPackLogger(cmCPackLog::LOG_ERROR
, msg
.str().c_str()
897 // do CMAKE_ROOT, look for the environment variable first
898 std::string cMakeRoot
;
900 cmCPackLogger(cmCPackLog::LOG_DEBUG
, "Looking for CMAKE_ROOT" << std::endl
);
901 if (getenv("CMAKE_ROOT"))
903 cMakeRoot
= getenv("CMAKE_ROOT");
904 modules
= cMakeRoot
+ "/Modules/CMake.cmake";
906 if(modules
.empty() || !cmSystemTools::FileExists(modules
.c_str()))
909 cMakeRoot
= cmSystemTools::GetProgramPath(this->CMakeSelf
.c_str());
910 std::string::size_type slashPos
= cMakeRoot
.rfind("/");
911 if(slashPos
!= std::string::npos
)
913 cMakeRoot
= cMakeRoot
.substr(0, slashPos
);
915 // is there no Modules direcory there?
916 modules
= cMakeRoot
+ "/Modules/CMake.cmake";
917 cmCPackLogger(cmCPackLog::LOG_DEBUG
, "Looking for CMAKE_ROOT: "
918 << modules
.c_str() << std::endl
);
921 if (!cmSystemTools::FileExists(modules
.c_str()))
923 // try exe/../share/cmake
924 cMakeRoot
+= CMAKE_DATA_DIR
;
925 modules
= cMakeRoot
+ "/Modules/CMake.cmake";
926 cmCPackLogger(cmCPackLog::LOG_DEBUG
, "Looking for CMAKE_ROOT: "
927 << modules
.c_str() << std::endl
);
929 #ifdef CMAKE_ROOT_DIR
930 if (!cmSystemTools::FileExists(modules
.c_str()))
932 // try compiled in root directory
933 cMakeRoot
= CMAKE_ROOT_DIR
;
934 modules
= cMakeRoot
+ "/Modules/CMake.cmake";
935 cmCPackLogger(cmCPackLog::LOG_DEBUG
, "Looking for CMAKE_ROOT: "
936 << modules
.c_str() << std::endl
);
940 if (!cmSystemTools::FileExists(modules
.c_str()))
942 // try compiled in install prefix
943 cMakeRoot
= CMAKE_PREFIX CMAKE_DATA_DIR
;
944 modules
= cMakeRoot
+ "/Modules/CMake.cmake";
945 cmCPackLogger(cmCPackLog::LOG_DEBUG
, "Looking for CMAKE_ROOT: "
946 << modules
.c_str() << std::endl
);
949 if (!cmSystemTools::FileExists(modules
.c_str()))
952 cMakeRoot
= cmSystemTools::GetProgramPath(this->CMakeSelf
.c_str());
953 cMakeRoot
+= CMAKE_DATA_DIR
;
954 modules
= cMakeRoot
+ "/Modules/CMake.cmake";
955 cmCPackLogger(cmCPackLog::LOG_DEBUG
, "Looking for CMAKE_ROOT: "
956 << modules
.c_str() << std::endl
);
958 if(!cmSystemTools::FileExists(modules
.c_str()))
961 cMakeRoot
= cmSystemTools::GetProgramPath(this->CMakeSelf
.c_str());
962 // is there no Modules direcory there?
963 modules
= cMakeRoot
+ "/Modules/CMake.cmake";
964 cmCPackLogger(cmCPackLog::LOG_DEBUG
, "Looking for CMAKE_ROOT: "
965 << modules
.c_str() << std::endl
);
967 if (!cmSystemTools::FileExists(modules
.c_str()))
969 // couldn't find modules
970 cmCPackLogger(cmCPackLog::LOG_ERROR
,
971 "Could not find CMAKE_ROOT !!!" << std::endl
972 << "CMake has most likely not been installed correctly." << std::endl
973 <<"Modules directory not found in" << std::endl
978 this->CMakeRoot
= cMakeRoot
;
979 cmCPackLogger(cmCPackLog::LOG_DEBUG
, "Looking for CMAKE_ROOT: "
980 << this->CMakeRoot
.c_str() << std::endl
);
981 this->SetOption("CMAKE_ROOT", this->CMakeRoot
.c_str());
985 //----------------------------------------------------------------------
986 int cmCPackGenerator::CompressFiles(const char* outFileName
,
987 const char* toplevel
, const std::vector
<std::string
>& files
)
995 //----------------------------------------------------------------------
996 const char* cmCPackGenerator::GetInstallPath()
998 if ( !this->InstallPath
.empty() )
1000 return this->InstallPath
.c_str();
1002 #if defined(_WIN32) && !defined(__CYGWIN__)
1003 const char* prgfiles
= cmsys::SystemTools::GetEnv("ProgramFiles");
1004 const char* sysDrive
= cmsys::SystemTools::GetEnv("SystemDrive");
1007 this->InstallPath
= prgfiles
;
1009 else if ( sysDrive
)
1011 this->InstallPath
= sysDrive
;
1012 this->InstallPath
+= "/Program Files";
1016 this->InstallPath
= "c:/Program Files";
1018 this->InstallPath
+= "/";
1019 this->InstallPath
+= this->GetOption("CPACK_PACKAGE_NAME");
1020 this->InstallPath
+= "-";
1021 this->InstallPath
+= this->GetOption("CPACK_PACKAGE_VERSION");
1023 this->InstallPath
= "/usr/local/";
1025 return this->InstallPath
.c_str();
1028 //----------------------------------------------------------------------
1029 const char* cmCPackGenerator::GetPackagingInstallPrefix()
1031 cmCPackLogger(cmCPackLog::LOG_DEBUG
, "GetPackagingInstallPrefix: '"
1032 << this->GetOption("CPACK_PACKAGING_INSTALL_PREFIX") << "'" << std::endl
);
1034 return this->GetOption("CPACK_PACKAGING_INSTALL_PREFIX");
1037 //----------------------------------------------------------------------
1038 std::string
cmCPackGenerator::FindTemplate(const char* name
)
1040 cmCPackLogger(cmCPackLog::LOG_DEBUG
, "Look for template: "
1041 << (name
? name
: "(NULL)") << std::endl
);
1042 std::string ffile
= this->MakefileMap
->GetModulesFile(name
);
1043 cmCPackLogger(cmCPackLog::LOG_DEBUG
, "Found template: "
1044 << ffile
.c_str() << std::endl
);
1048 //----------------------------------------------------------------------
1049 bool cmCPackGenerator::ConfigureString(const std::string
& inString
,
1050 std::string
& outString
)
1052 this->MakefileMap
->ConfigureString(inString
,
1053 outString
, true, false);
1057 //----------------------------------------------------------------------
1058 bool cmCPackGenerator::ConfigureFile(const char* inName
,
1059 const char* outName
, bool copyOnly
/* = false */)
1061 return this->MakefileMap
->ConfigureFile(inName
, outName
,
1062 copyOnly
, true, false) == 1;
1065 //----------------------------------------------------------------------
1066 int cmCPackGenerator::CleanTemporaryDirectory()
1068 std::string tempInstallDirectoryWithPostfix
1069 = this->GetOption("CPACK_TEMPORARY_INSTALL_DIRECTORY");
1070 const char* tempInstallDirectory
= tempInstallDirectoryWithPostfix
.c_str();
1071 if(cmsys::SystemTools::FileExists(tempInstallDirectory
))
1073 cmCPackLogger(cmCPackLog::LOG_OUTPUT
,
1074 "- Clean temporary : "
1075 << tempInstallDirectory
<< std::endl
);
1076 if(!cmsys::SystemTools::RemoveADirectory(tempInstallDirectory
))
1078 cmCPackLogger(cmCPackLog::LOG_ERROR
,
1079 "Problem removing temporary directory: " <<
1080 tempInstallDirectory