1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmCPackGenerator.cxx,v $
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"
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()
64 std::string tempDirectory
= this->GetOption("CPACK_PACKAGE_DIRECTORY");
65 tempDirectory
+= "/_CPack_Packages/";
66 const char* toplevelTag
= this->GetOption("CPACK_TOPLEVEL_TAG");
69 tempDirectory
+= toplevelTag
;
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
;
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",
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");
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
);
118 std::ifstream
ifs(descFileName
);
121 cmCPackLogger(cmCPackLog::LOG_ERROR
,
122 "Cannot open description file name: " << descFileName
<< std::endl
);
125 cmOStringStream ostr
;
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."
145 this->SetOptionIfNotSet("CPACK_REMOVE_TOPLEVEL_DIRECTORY", "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();
159 if ( !cmsys::SystemTools::MakeDirectory(tempInstallDirectory
))
161 cmCPackLogger(cmCPackLog::LOG_ERROR
,
162 "Problem creating temporary directory: "
163 << (tempInstallDirectory
? tempInstallDirectory
: "(NULL}")
168 bool setDestDir
= cmSystemTools::IsOn(this->GetOption("CPACK_SET_DESTDIR"));
171 std::string destDir
= "DESTDIR=";
172 destDir
+= tempInstallDirectory
;
173 cmSystemTools::PutEnv(destDir
.c_str());
177 // Make sure there is no destdir
178 cmSystemTools::PutEnv("DESTDIR=");
181 // If the CPackConfig file sets CPACK_INSTALL_COMMANDS then run them
183 if ( !this->InstallProjectViaInstallCommands(
184 setDestDir
, tempInstallDirectory
) )
189 // If the CPackConfig file sets CPACK_INSTALL_SCRIPT then run them
191 if ( !this->InstallProjectViaInstallScript(
192 setDestDir
, tempInstallDirectory
) )
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
) )
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
) )
217 cmSystemTools::PutEnv("DESTDIR=");
223 //----------------------------------------------------------------------
224 int cmCPackGenerator::InstallProjectViaInstallCommands(
225 bool setDestDir
, const char* tempInstallDirectory
)
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();
242 cmCPackLogger(cmCPackLog::LOG_VERBOSE
, "Execute: " << it
->c_str()
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"
267 //----------------------------------------------------------------------
268 int cmCPackGenerator::InstallProjectViaInstalledDirectories(
269 bool setDestDir
, const char* tempInstallDirectory
)
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();
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
);
305 std::vector
<std::string
>::iterator it
;
306 const char* tempDir
= tempInstallDirectory
;
307 for ( it
= installDirectoriesVector
.begin();
308 it
!= installDirectoriesVector
.end();
311 cmCPackLogger(cmCPackLog::LOG_DEBUG
, "Find files" << std::endl
);
313 std::string toplevel
= it
->c_str();
315 std::string subdir
= it
->c_str();
316 std::string findExpr
= toplevel
;
318 cmCPackLogger(cmCPackLog::LOG_OUTPUT
,
319 "- Install directory: " << toplevel
<< std::endl
);
321 if ( !gl
.FindFiles(findExpr
) )
323 cmCPackLogger(cmCPackLog::LOG_ERROR
,
324 "Cannot find any files in the installed directory" << std::endl
);
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
)
333 std::string
&inFile
= *gfit
;
334 for ( regIt
= ignoreFilesRegex
.begin();
335 regIt
!= ignoreFilesRegex
.end();
338 if ( regIt
->find(inFile
.c_str()) )
340 cmCPackLogger(cmCPackLog::LOG_VERBOSE
, "Ignore file: "
341 << inFile
.c_str() << std::endl
);
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(),
357 cmCPackLogger(cmCPackLog::LOG_ERROR
, "Problem copying file: "
358 << inFile
.c_str() << " -> " << filePath
.c_str() << std::endl
);
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
,
382 std::vector
<std::string
>::iterator it
;
383 for ( it
= cmakeScriptsVector
.begin();
384 it
!= cmakeScriptsVector
.end();
387 std::string installScript
= it
->c_str();
389 cmCPackLogger(cmCPackLog::LOG_OUTPUT
,
390 "- Install script: " << installScript
<< std::endl
);
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)"
408 cmCPackLogger(cmCPackLog::LOG_DEBUG
,
409 "- Setting CMAKE_INSTALL_PREFIX to '" << dir
<< "'" << std::endl
);
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
)
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."
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();
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 "
477 std::string installDirectory
= it
->c_str();
479 std::string installProjectName
= it
->c_str();
481 std::string installComponent
= it
->c_str();
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(
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
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
);
513 cmSystemTools::RunSingleCommand(buildCommand
.c_str(),
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()
530 << "Please check " << tmpFile
.c_str() << " for errors"
535 delete globalGenerator
;
537 cmCPackLogger(cmCPackLog::LOG_OUTPUT
,
538 "- Install project: " << installProjectName
<< std::endl
);
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
;
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)"
568 cmCPackLogger(cmCPackLog::LOG_DEBUG
,
569 "- Setting CMAKE_INSTALL_PREFIX to '" << dir
<< "'" << std::endl
);
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
)
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
,
622 const char* def
= this->MakefileMap
->GetDefinition(op
);
627 this->SetOption(op
, value
);
630 //----------------------------------------------------------------------
631 void cmCPackGenerator::SetOption(const char* op
, const char* value
)
639 this->MakefileMap
->RemoveDefinition(op
);
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() )
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: "
676 if ( !this->InstallProject() )
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
);
689 std::string findExpr
= tempDirectory
;
692 if ( !gl
.FindFiles(findExpr
) )
694 cmCPackLogger(cmCPackLog::LOG_ERROR
,
695 "Cannot find any files in the packaging tree" << std::endl
);
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"
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"
721 cmCPackLogger(cmCPackLog::LOG_OUTPUT
, "Finalize package" << std::endl
);
722 cmCPackLogger(cmCPackLog::LOG_VERBOSE
, "Copy final package: "
723 << (tempPackageFileName
? tempPackageFileName
: "(NULL)" )
725 << (packageFileName
? packageFileName
: "(NULL)")
727 if ( !cmSystemTools::CopyFileIfDifferent(tempPackageFileName
,
730 cmCPackLogger(cmCPackLog::LOG_ERROR
, "Problem copying the package: "
731 << (tempPackageFileName
? tempPackageFileName
: "(NULL)" )
733 << (packageFileName
? packageFileName
: "(NULL)")
738 cmCPackLogger(cmCPackLog::LOG_OUTPUT
, "Package "
739 << (packageFileName
? packageFileName
: "(NULL)")
740 << " generated." << std::endl
);
744 //----------------------------------------------------------------------
745 int cmCPackGenerator::Initialize(const char* name
, cmMakefile
* mf
,
748 this->MakefileMap
= mf
;
750 if ( !this->FindRunningCMake(argv0
) )
752 cmCPackLogger(cmCPackLog::LOG_ERROR
,
753 "Cannot initialize the generator" << std::endl
);
756 // set the running generator name
757 this->SetOption("CPACK_GENERATOR", this->Name
.c_str());
758 // Load the project specific config file
760 this->GetOption("CPACK_PROJECT_CONFIG_FILE");
763 mf
->ReadListFile(config
);
765 int result
= this->InitializeInternal();
766 if (cmSystemTools::GetErrorOccuredFlag())
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", "/");
779 //----------------------------------------------------------------------
780 int cmCPackGenerator::InitializeInternal()
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
)
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
);
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()
830 if(cmSystemTools::SplitProgramPath(this->CPackSelf
.c_str(),
833 this->CMakeSelf
= dir
+= "/cmake";
834 this->CMakeSelf
+= cmSystemTools::GetExecutableExtension();
835 if(cmSystemTools::FileExists(this->CMakeSelf
.c_str()))
842 failures
.push_back(this->CMakeSelf
);
843 #ifdef CMAKE_BUILD_DIR
844 std::string intdir
= ".";
846 intdir
= CMAKE_INTDIR
;
848 this->CMakeSelf
= CMAKE_BUILD_DIR
;
849 this->CMakeSelf
+= "/bin/";
850 this->CMakeSelf
+= intdir
;
851 this->CMakeSelf
+= "/cmake";
852 this->CMakeSelf
+= cmSystemTools::GetExecutableExtension();
854 if(!cmSystemTools::FileExists(this->CMakeSelf
.c_str()))
856 failures
.push_back(this->CMakeSelf
);
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()
871 // do CMAKE_ROOT, look for the environment variable first
872 std::string cMakeRoot
;
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()))
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
);
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
);
923 if (!cmSystemTools::FileExists(modules
.c_str()))
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()))
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
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());
959 //----------------------------------------------------------------------
960 int cmCPackGenerator::CompressFiles(const char* outFileName
,
961 const char* toplevel
, const std::vector
<std::string
>& files
)
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");
981 this->InstallPath
= prgfiles
;
985 this->InstallPath
= sysDrive
;
986 this->InstallPath
+= "/Program Files";
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");
997 this->InstallPath
= "/usr/local/";
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
);
1022 //----------------------------------------------------------------------
1023 bool cmCPackGenerator::ConfigureString(const std::string
& inString
,
1024 std::string
& outString
)
1026 this->MakefileMap
->ConfigureString(inString
,
1027 outString
, true, false);
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