2016-09-27 22:01:08 +03:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2009-10-30 20:10:56 +03:00
|
|
|
#include "cmCPackArchiveGenerator.h"
|
|
|
|
|
2016-09-07 01:21:35 +03:00
|
|
|
#include "cmCPackComponentGroup.h"
|
|
|
|
#include "cmCPackGenerator.h"
|
2016-04-29 17:53:13 +03:00
|
|
|
#include "cmCPackLog.h"
|
|
|
|
#include "cmGeneratedFileStream.h"
|
|
|
|
#include "cmSystemTools.h"
|
2009-10-30 20:10:56 +03:00
|
|
|
|
2016-09-07 01:21:35 +03:00
|
|
|
#include <map>
|
|
|
|
#include <ostream>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2009-10-30 20:10:56 +03:00
|
|
|
|
2010-08-24 23:41:32 +04:00
|
|
|
cmCPackArchiveGenerator::cmCPackArchiveGenerator(cmArchiveWrite::Compress t,
|
2016-05-16 17:34:04 +03:00
|
|
|
std::string const& format)
|
2009-10-30 20:10:56 +03:00
|
|
|
{
|
|
|
|
this->Compress = t;
|
2015-04-07 13:36:52 +03:00
|
|
|
this->ArchiveFormat = format;
|
2009-10-30 20:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cmCPackArchiveGenerator::~cmCPackArchiveGenerator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int cmCPackArchiveGenerator::InitializeInternal()
|
|
|
|
{
|
|
|
|
this->SetOptionIfNotSet("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", "1");
|
|
|
|
return this->Superclass::InitializeInternal();
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
int cmCPackArchiveGenerator::addOneComponentToArchive(
|
|
|
|
cmArchiveWrite& archive, cmCPackComponent* component)
|
2010-08-24 23:42:21 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
cmCPackLogger(cmCPackLog::LOG_VERBOSE,
|
|
|
|
" - packaging component: " << component->Name << std::endl);
|
2010-08-24 23:42:21 +04:00
|
|
|
// Add the files of this component to the archive
|
|
|
|
std::string localToplevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
|
2016-05-16 17:34:04 +03:00
|
|
|
localToplevel += "/" + component->Name;
|
2010-08-24 23:42:21 +04:00
|
|
|
std::string dir = cmSystemTools::GetCurrentWorkingDirectory();
|
|
|
|
// Change to local toplevel
|
2014-10-15 16:54:05 +04:00
|
|
|
cmSystemTools::ChangeDirectory(localToplevel);
|
2012-01-14 01:01:53 +04:00
|
|
|
std::string filePrefix;
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->IsOn("CPACK_COMPONENT_INCLUDE_TOPLEVEL_DIRECTORY")) {
|
2012-01-14 01:01:53 +04:00
|
|
|
filePrefix = this->GetOption("CPACK_PACKAGE_FILE_NAME");
|
|
|
|
filePrefix += "/";
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-03-26 07:36:38 +03:00
|
|
|
const char* installPrefix =
|
|
|
|
this->GetOption("CPACK_PACKAGING_INSTALL_PREFIX");
|
2016-05-16 17:34:04 +03:00
|
|
|
if (installPrefix && installPrefix[0] == '/' && installPrefix[1] != 0) {
|
2015-03-26 07:36:38 +03:00
|
|
|
// add to file prefix and remove the leading '/'
|
2016-05-16 17:34:04 +03:00
|
|
|
filePrefix += installPrefix + 1;
|
2015-03-26 07:36:38 +03:00
|
|
|
filePrefix += "/";
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2010-08-24 23:42:21 +04:00
|
|
|
std::vector<std::string>::const_iterator fileIt;
|
2010-09-16 00:41:41 +04:00
|
|
|
for (fileIt = component->Files.begin(); fileIt != component->Files.end();
|
2016-05-16 17:34:04 +03:00
|
|
|
++fileIt) {
|
2012-01-14 01:01:53 +04:00
|
|
|
std::string rp = filePrefix + *fileIt;
|
2016-05-16 17:34:04 +03:00
|
|
|
cmCPackLogger(cmCPackLog::LOG_DEBUG, "Adding file: " << rp << std::endl);
|
2016-06-27 23:44:16 +03:00
|
|
|
archive.Add(rp, 0, CM_NULLPTR, false);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!archive) {
|
2010-08-24 23:42:21 +04:00
|
|
|
cmCPackLogger(cmCPackLog::LOG_ERROR, "ERROR while packaging files: "
|
2016-05-16 17:34:04 +03:00
|
|
|
<< archive.GetError() << std::endl);
|
2010-08-24 23:42:21 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2010-08-24 23:42:21 +04:00
|
|
|
// Go back to previous dir
|
2014-10-15 16:54:05 +04:00
|
|
|
cmSystemTools::ChangeDirectory(dir);
|
2010-08-24 23:42:21 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The macro will open/create a file 'filename'
|
|
|
|
* an declare and open the associated
|
|
|
|
* cmArchiveWrite 'archive' object.
|
|
|
|
*/
|
2016-05-16 17:34:04 +03:00
|
|
|
#define DECLARE_AND_OPEN_ARCHIVE(filename, archive) \
|
|
|
|
cmGeneratedFileStream gf; \
|
2016-09-05 23:09:49 +03:00
|
|
|
gf.Open((filename).c_str(), false, true); \
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!GenerateHeader(&gf)) { \
|
|
|
|
cmCPackLogger(cmCPackLog::LOG_ERROR, \
|
|
|
|
"Problem to generate Header for archive < " \
|
2016-09-05 23:09:49 +03:00
|
|
|
<< (filename) << ">." << std::endl); \
|
2016-05-16 17:34:04 +03:00
|
|
|
return 0; \
|
|
|
|
} \
|
|
|
|
cmArchiveWrite archive(gf, this->Compress, this->ArchiveFormat); \
|
2016-09-05 23:09:49 +03:00
|
|
|
if (!(archive)) { \
|
2016-05-16 17:34:04 +03:00
|
|
|
cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem to create archive < " \
|
2016-09-05 23:09:49 +03:00
|
|
|
<< (filename) << ">. ERROR =" << (archive).GetError() \
|
2016-05-16 17:34:04 +03:00
|
|
|
<< std::endl); \
|
|
|
|
return 0; \
|
2010-08-24 23:42:21 +04:00
|
|
|
}
|
|
|
|
|
2010-11-13 19:56:36 +03:00
|
|
|
int cmCPackArchiveGenerator::PackageComponents(bool ignoreGroup)
|
2010-08-24 23:42:21 +04:00
|
|
|
{
|
|
|
|
packageFileNames.clear();
|
|
|
|
// The default behavior is to have one package by component group
|
|
|
|
// unless CPACK_COMPONENTS_IGNORE_GROUP is specified.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!ignoreGroup) {
|
2010-08-24 23:42:21 +04:00
|
|
|
std::map<std::string, cmCPackComponentGroup>::iterator compGIt;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (compGIt = this->ComponentGroups.begin();
|
|
|
|
compGIt != this->ComponentGroups.end(); ++compGIt) {
|
2010-08-24 23:42:21 +04:00
|
|
|
cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Packaging component group: "
|
2016-05-16 17:34:04 +03:00
|
|
|
<< compGIt->first << std::endl);
|
2010-08-24 23:42:21 +04:00
|
|
|
// Begin the archive for this group
|
2016-05-16 17:34:04 +03:00
|
|
|
std::string packageFileName = std::string(toplevel);
|
|
|
|
packageFileName += "/" +
|
|
|
|
GetComponentPackageFileName(this->GetOption("CPACK_PACKAGE_FILE_NAME"),
|
|
|
|
compGIt->first, true) +
|
|
|
|
this->GetOutputExtension();
|
2010-08-24 23:42:21 +04:00
|
|
|
// open a block in order to automatically close archive
|
|
|
|
// at the end of the block
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
DECLARE_AND_OPEN_ARCHIVE(packageFileName, archive);
|
2010-08-24 23:42:21 +04:00
|
|
|
// now iterate over the component of this group
|
|
|
|
std::vector<cmCPackComponent*>::iterator compIt;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (compIt = (compGIt->second).Components.begin();
|
|
|
|
compIt != (compGIt->second).Components.end(); ++compIt) {
|
2010-08-24 23:42:21 +04:00
|
|
|
// Add the files of this component to the archive
|
2016-05-16 17:34:04 +03:00
|
|
|
addOneComponentToArchive(archive, *compIt);
|
|
|
|
}
|
2010-08-24 23:42:21 +04:00
|
|
|
}
|
|
|
|
// add the generated package to package file names list
|
|
|
|
packageFileNames.push_back(packageFileName);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2011-03-03 23:19:13 +03:00
|
|
|
// Handle Orphan components (components not belonging to any groups)
|
|
|
|
std::map<std::string, cmCPackComponent>::iterator compIt;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (compIt = this->Components.begin(); compIt != this->Components.end();
|
|
|
|
++compIt) {
|
2011-03-03 23:19:13 +03:00
|
|
|
// Does the component belong to a group?
|
2016-06-27 23:44:16 +03:00
|
|
|
if (compIt->second.Group == CM_NULLPTR) {
|
2016-05-16 17:34:04 +03:00
|
|
|
cmCPackLogger(
|
|
|
|
cmCPackLog::LOG_VERBOSE, "Component <"
|
|
|
|
<< compIt->second.Name
|
|
|
|
<< "> does not belong to any group, package it separately."
|
|
|
|
<< std::endl);
|
2011-03-03 23:19:13 +03:00
|
|
|
std::string localToplevel(
|
2016-05-16 17:34:04 +03:00
|
|
|
this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
|
2011-03-03 23:19:13 +03:00
|
|
|
std::string packageFileName = std::string(toplevel);
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
localToplevel += "/" + compIt->first;
|
|
|
|
packageFileName += "/" + GetComponentPackageFileName(
|
|
|
|
this->GetOption("CPACK_PACKAGE_FILE_NAME"),
|
|
|
|
compIt->first, false) +
|
|
|
|
this->GetOutputExtension();
|
2011-03-03 23:19:13 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
DECLARE_AND_OPEN_ARCHIVE(packageFileName, archive);
|
2011-03-03 23:19:13 +03:00
|
|
|
// Add the files of this component to the archive
|
2016-05-16 17:34:04 +03:00
|
|
|
addOneComponentToArchive(archive, &(compIt->second));
|
2011-03-03 23:19:13 +03:00
|
|
|
}
|
|
|
|
// add the generated package to package file names list
|
|
|
|
packageFileNames.push_back(packageFileName);
|
|
|
|
}
|
2010-08-24 23:42:21 +04:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2010-08-24 23:42:21 +04:00
|
|
|
// CPACK_COMPONENTS_IGNORE_GROUPS is set
|
|
|
|
// We build 1 package per component
|
2016-05-16 17:34:04 +03:00
|
|
|
else {
|
2010-08-24 23:42:21 +04:00
|
|
|
std::map<std::string, cmCPackComponent>::iterator compIt;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (compIt = this->Components.begin(); compIt != this->Components.end();
|
|
|
|
++compIt) {
|
2010-08-24 23:42:21 +04:00
|
|
|
std::string localToplevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
|
|
|
|
std::string packageFileName = std::string(toplevel);
|
2009-10-30 20:10:56 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
localToplevel += "/" + compIt->first;
|
|
|
|
packageFileName += "/" +
|
|
|
|
GetComponentPackageFileName(this->GetOption("CPACK_PACKAGE_FILE_NAME"),
|
|
|
|
compIt->first, false) +
|
|
|
|
this->GetOutputExtension();
|
2010-08-24 23:42:21 +04:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
DECLARE_AND_OPEN_ARCHIVE(packageFileName, archive);
|
2010-08-24 23:42:21 +04:00
|
|
|
// Add the files of this component to the archive
|
2016-05-16 17:34:04 +03:00
|
|
|
addOneComponentToArchive(archive, &(compIt->second));
|
2010-08-24 23:42:21 +04:00
|
|
|
}
|
|
|
|
// add the generated package to package file names list
|
|
|
|
packageFileNames.push_back(packageFileName);
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2010-08-24 23:42:21 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-03-23 20:28:05 +03:00
|
|
|
int cmCPackArchiveGenerator::PackageComponentsAllInOne()
|
2010-08-24 23:42:21 +04:00
|
|
|
{
|
|
|
|
// reset the package file names
|
|
|
|
packageFileNames.clear();
|
|
|
|
packageFileNames.push_back(std::string(toplevel));
|
2016-05-16 17:34:04 +03:00
|
|
|
packageFileNames[0] += "/" +
|
|
|
|
std::string(this->GetOption("CPACK_PACKAGE_FILE_NAME")) +
|
|
|
|
this->GetOutputExtension();
|
2010-09-16 00:41:41 +04:00
|
|
|
cmCPackLogger(cmCPackLog::LOG_VERBOSE,
|
|
|
|
"Packaging all groups in one package..."
|
|
|
|
"(CPACK_COMPONENTS_ALL_GROUPS_IN_ONE_PACKAGE is set)"
|
2016-05-16 17:34:04 +03:00
|
|
|
<< std::endl);
|
|
|
|
DECLARE_AND_OPEN_ARCHIVE(packageFileNames[0], archive);
|
2010-08-24 23:42:21 +04:00
|
|
|
|
2011-03-23 20:28:05 +03:00
|
|
|
// The ALL COMPONENTS in ONE package case
|
|
|
|
std::map<std::string, cmCPackComponent>::iterator compIt;
|
2016-05-16 17:34:04 +03:00
|
|
|
for (compIt = this->Components.begin(); compIt != this->Components.end();
|
|
|
|
++compIt) {
|
2011-03-23 20:28:05 +03:00
|
|
|
// Add the files of this component to the archive
|
2016-05-16 17:34:04 +03:00
|
|
|
addOneComponentToArchive(archive, &(compIt->second));
|
|
|
|
}
|
2011-03-23 20:28:05 +03:00
|
|
|
|
2010-08-24 23:42:21 +04:00
|
|
|
// archive goes out of scope so it will finalized and closed.
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-08-11 21:48:39 +04:00
|
|
|
int cmCPackArchiveGenerator::PackageFiles()
|
2009-10-30 20:10:56 +03:00
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
cmCPackLogger(cmCPackLog::LOG_DEBUG, "Toplevel: " << toplevel << std::endl);
|
2010-08-11 21:48:39 +04:00
|
|
|
|
2012-03-18 23:21:35 +04:00
|
|
|
if (WantsComponentInstallation()) {
|
2010-11-29 20:57:24 +03:00
|
|
|
// CASE 1 : COMPONENT ALL-IN-ONE package
|
2011-03-23 20:28:05 +03:00
|
|
|
// If ALL COMPONENTS in ONE package has been requested
|
2010-11-29 20:57:24 +03:00
|
|
|
// then the package file is unique and should be open here.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (componentPackageMethod == ONE_PACKAGE) {
|
2011-03-23 20:28:05 +03:00
|
|
|
return PackageComponentsAllInOne();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2010-11-29 20:57:24 +03:00
|
|
|
// CASE 2 : COMPONENT CLASSICAL package(s) (i.e. not all-in-one)
|
|
|
|
// There will be 1 package for each component group
|
|
|
|
// however one may require to ignore component group and
|
|
|
|
// in this case you'll get 1 package for each component.
|
2016-08-18 20:47:32 +03:00
|
|
|
return PackageComponents(componentPackageMethod ==
|
|
|
|
ONE_PACKAGE_PER_COMPONENT);
|
2010-11-29 20:57:24 +03:00
|
|
|
}
|
2010-08-24 23:42:21 +04:00
|
|
|
|
|
|
|
// CASE 3 : NON COMPONENT package.
|
2016-05-16 17:34:04 +03:00
|
|
|
DECLARE_AND_OPEN_ARCHIVE(packageFileNames[0], archive);
|
2009-10-30 20:10:56 +03:00
|
|
|
std::vector<std::string>::const_iterator fileIt;
|
2009-11-09 17:58:03 +03:00
|
|
|
std::string dir = cmSystemTools::GetCurrentWorkingDirectory();
|
2014-10-15 16:54:05 +04:00
|
|
|
cmSystemTools::ChangeDirectory(toplevel);
|
2016-05-16 17:34:04 +03:00
|
|
|
for (fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
|
2009-10-30 20:10:56 +03:00
|
|
|
// Get the relative path to the file
|
2016-05-16 17:34:04 +03:00
|
|
|
std::string rp =
|
|
|
|
cmSystemTools::RelativePath(toplevel.c_str(), fileIt->c_str());
|
2016-06-27 23:44:16 +03:00
|
|
|
archive.Add(rp, 0, CM_NULLPTR, false);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!archive) {
|
2010-08-24 23:41:32 +04:00
|
|
|
cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem while adding file< "
|
2016-05-16 17:34:04 +03:00
|
|
|
<< *fileIt << "> to archive <" << packageFileNames[0]
|
|
|
|
<< "> .ERROR =" << archive.GetError() << std::endl);
|
2010-08-24 23:41:32 +04:00
|
|
|
return 0;
|
2009-10-30 20:10:56 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-10-15 16:54:05 +04:00
|
|
|
cmSystemTools::ChangeDirectory(dir);
|
2010-08-24 23:41:32 +04:00
|
|
|
// The destructor of cmArchiveWrite will close and finish the write
|
2009-10-30 20:10:56 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-08-17 02:49:57 +03:00
|
|
|
int cmCPackArchiveGenerator::GenerateHeader(std::ostream* /*unused*/)
|
2009-10-30 20:10:56 +03:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2010-08-24 23:42:21 +04:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool cmCPackArchiveGenerator::SupportsComponentInstallation() const
|
|
|
|
{
|
2010-11-29 20:57:24 +03:00
|
|
|
// The Component installation support should only
|
|
|
|
// be activated if explicitly requested by the user
|
|
|
|
// (for backward compatibility reason)
|
2016-06-02 00:29:53 +03:00
|
|
|
return IsOn("CPACK_ARCHIVE_COMPONENT_INSTALL");
|
2010-08-24 23:42:21 +04:00
|
|
|
}
|