cmake/Source/CPack/cmCPackRPMGenerator.cxx

438 lines
16 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmCPackRPMGenerator.h"
2016-07-09 11:21:54 +02:00
2016-10-30 18:24:19 +01:00
#include <algorithm>
2020-02-01 23:06:01 +01:00
#include <cctype>
2016-10-30 18:24:19 +01:00
#include <map>
#include <ostream>
#include <utility>
#include <vector>
2017-04-14 19:02:05 +02:00
#include "cmCPackComponentGroup.h"
#include "cmCPackGenerator.h"
#include "cmCPackLog.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
2019-11-11 23:01:05 +01:00
cmCPackRPMGenerator::cmCPackRPMGenerator() = default;
2019-11-11 23:01:05 +01:00
cmCPackRPMGenerator::~cmCPackRPMGenerator() = default;
int cmCPackRPMGenerator::InitializeInternal()
{
this->SetOptionIfNotSet("CPACK_PACKAGING_INSTALL_PREFIX", "/usr");
2020-02-01 23:06:01 +01:00
if (cmIsOff(this->GetOption("CPACK_SET_DESTDIR"))) {
2010-11-13 01:00:53 +02:00
this->SetOption("CPACK_SET_DESTDIR", "I_ON");
2016-07-09 11:21:54 +02:00
}
2011-06-19 15:41:06 +03:00
/* Replace space in CPACK_PACKAGE_NAME in order to avoid
* rpmbuild scream on unwanted space in filename issue
* Moreover RPM file do not usually embed space in filename
*/
if (this->GetOption("CPACK_PACKAGE_NAME")) {
2016-07-09 11:21:54 +02:00
std::string packageName = this->GetOption("CPACK_PACKAGE_NAME");
std::replace(packageName.begin(), packageName.end(), ' ', '-');
this->SetOption("CPACK_PACKAGE_NAME", packageName.c_str());
2011-06-19 15:41:06 +03:00
}
/* same for CPACK_PACKAGE_FILE_NAME */
if (this->GetOption("CPACK_PACKAGE_FILE_NAME")) {
2016-07-09 11:21:54 +02:00
std::string packageName = this->GetOption("CPACK_PACKAGE_FILE_NAME");
std::replace(packageName.begin(), packageName.end(), ' ', '-');
this->SetOption("CPACK_PACKAGE_FILE_NAME", packageName.c_str());
2011-06-19 15:41:06 +03:00
}
return this->Superclass::InitializeInternal();
}
2016-07-09 11:21:54 +02:00
void cmCPackRPMGenerator::AddGeneratedPackageNames()
{
// add the generated packages to package file names list
std::string fileNames(this->GetOption("GEN_CPACK_OUTPUT_FILES"));
const char sep = ';';
std::string::size_type pos1 = 0;
std::string::size_type pos2 = fileNames.find(sep, pos1 + 1);
while (pos2 != std::string::npos) {
packageFileNames.push_back(fileNames.substr(pos1, pos2 - pos1));
pos1 = pos2 + 1;
pos2 = fileNames.find(sep, pos1 + 1);
}
packageFileNames.push_back(fileNames.substr(pos1, pos2 - pos1));
}
int cmCPackRPMGenerator::PackageOnePack(std::string const& initialToplevel,
std::string const& packageName)
{
2011-01-16 11:35:12 +01:00
int retval = 1;
2011-06-19 15:41:06 +03:00
// Begin the archive for this pack
std::string localToplevel(initialToplevel);
2016-07-09 11:21:54 +02:00
std::string packageFileName(cmSystemTools::GetParentDirectory(toplevel));
2011-06-19 15:41:06 +03:00
std::string outputFileName(
2016-07-09 11:21:54 +02:00
GetComponentPackageFileName(this->GetOption("CPACK_PACKAGE_FILE_NAME"),
packageName, true) +
this->GetOutputExtension());
2011-01-16 11:35:12 +01:00
2016-07-09 11:21:54 +02:00
localToplevel += "/" + packageName;
2011-06-19 15:41:06 +03:00
/* replace the TEMP DIRECTORY with the component one */
2016-07-09 11:21:54 +02:00
this->SetOption("CPACK_TEMPORARY_DIRECTORY", localToplevel.c_str());
packageFileName += "/" + outputFileName;
2011-06-19 15:41:06 +03:00
/* replace proposed CPACK_OUTPUT_FILE_NAME */
2016-07-09 11:21:54 +02:00
this->SetOption("CPACK_OUTPUT_FILE_NAME", outputFileName.c_str());
2011-06-19 15:41:06 +03:00
/* replace the TEMPORARY package file name */
this->SetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
packageFileName.c_str());
// Tell CPackRPM.cmake the name of the component NAME.
2016-07-09 11:21:54 +02:00
this->SetOption("CPACK_RPM_PACKAGE_COMPONENT", packageName.c_str());
2013-03-16 19:13:01 +02:00
// Tell CPackRPM.cmake the path where the component is.
2020-02-01 23:06:01 +01:00
std::string component_path = cmStrCat('/', packageName);
2013-03-16 19:13:01 +02:00
this->SetOption("CPACK_RPM_PACKAGE_COMPONENT_PART_PATH",
component_path.c_str());
2018-10-28 12:09:07 +01:00
if (!this->ReadListFile("Internal/CPack/CPackRPM.cmake")) {
2018-08-09 18:06:22 +02:00
cmCPackLogger(cmCPackLog::LOG_ERROR,
"Error while execution CPackRPM.cmake" << std::endl);
2011-06-19 15:41:06 +03:00
retval = 0;
2016-07-09 11:21:54 +02:00
}
2011-06-19 15:41:06 +03:00
return retval;
}
int cmCPackRPMGenerator::PackageComponents(bool ignoreGroup)
{
int retval = 1;
/* Reset package file name list it will be populated during the
* component packaging run*/
packageFileNames.clear();
std::string initialTopLevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
2017-04-14 19:02:05 +02:00
const char* mainComponent = this->GetOption("CPACK_RPM_MAIN_COMPONENT");
if (this->IsOn("CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE") &&
!this->IsOn("CPACK_RPM_DEBUGINFO_PACKAGE")) {
// check if we need to set CPACK_RPM_DEBUGINFO_PACKAGE because non of
// the components is setting per component debuginfo package variable
bool shouldSet = true;
if (ignoreGroup) {
std::map<std::string, cmCPackComponent>::iterator compIt;
for (compIt = this->Components.begin(); compIt != this->Components.end();
++compIt) {
std::string component(compIt->first);
std::transform(component.begin(), component.end(), component.begin(),
::toupper);
if (this->IsOn("CPACK_RPM_" + compIt->first + "_DEBUGINFO_PACKAGE") ||
this->IsOn("CPACK_RPM_" + component + "_DEBUGINFO_PACKAGE")) {
shouldSet = false;
break;
}
}
} else {
std::map<std::string, cmCPackComponentGroup>::iterator compGIt;
for (compGIt = this->ComponentGroups.begin();
compGIt != this->ComponentGroups.end(); ++compGIt) {
std::string component(compGIt->first);
std::transform(component.begin(), component.end(), component.begin(),
::toupper);
if (this->IsOn("CPACK_RPM_" + compGIt->first + "_DEBUGINFO_PACKAGE") ||
this->IsOn("CPACK_RPM_" + component + "_DEBUGINFO_PACKAGE")) {
shouldSet = false;
break;
}
}
if (shouldSet) {
std::map<std::string, cmCPackComponent>::iterator compIt;
for (compIt = this->Components.begin();
compIt != this->Components.end(); ++compIt) {
// Does the component belong to a group?
2018-01-26 17:06:56 +01:00
if (compIt->second.Group == nullptr) {
2017-04-14 19:02:05 +02:00
std::string component(compIt->first);
std::transform(component.begin(), component.end(),
component.begin(), ::toupper);
if (this->IsOn("CPACK_RPM_" + compIt->first +
"_DEBUGINFO_PACKAGE") ||
this->IsOn("CPACK_RPM_" + component + "_DEBUGINFO_PACKAGE")) {
shouldSet = false;
break;
}
}
}
}
}
if (shouldSet) {
2018-08-09 18:06:22 +02:00
cmCPackLogger(cmCPackLog::LOG_VERBOSE,
"Setting "
2017-04-14 19:02:05 +02:00
<< "CPACK_RPM_DEBUGINFO_PACKAGE because "
<< "CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE is set but "
<< " none of the "
<< "CPACK_RPM_<component>_DEBUGINFO_PACKAGE variables "
<< "are set." << std::endl);
this->SetOption("CPACK_RPM_DEBUGINFO_PACKAGE", "ON");
}
}
if (mainComponent) {
if (this->IsOn("CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE")) {
this->SetOption("GENERATE_SPEC_PARTS", "ON");
}
std::string mainComponentUpper(mainComponent);
std::transform(mainComponentUpper.begin(), mainComponentUpper.end(),
mainComponentUpper.begin(), ::toupper);
// The default behavior is to have one package by component group
// unless CPACK_COMPONENTS_IGNORE_GROUP is specified.
if (!ignoreGroup) {
2020-02-01 23:06:01 +01:00
auto mainCompGIt = this->ComponentGroups.end();
2017-04-14 19:02:05 +02:00
std::map<std::string, cmCPackComponentGroup>::iterator compGIt;
for (compGIt = this->ComponentGroups.begin();
compGIt != this->ComponentGroups.end(); ++compGIt) {
std::string component(compGIt->first);
std::transform(component.begin(), component.end(), component.begin(),
::toupper);
if (mainComponentUpper == component) {
// main component will be handled last
mainCompGIt = compGIt;
continue;
}
2018-08-09 18:06:22 +02:00
cmCPackLogger(cmCPackLog::LOG_VERBOSE,
"Packaging component group: " << compGIt->first
<< std::endl);
2017-04-14 19:02:05 +02:00
retval &= PackageOnePack(initialTopLevel, compGIt->first);
}
// Handle Orphan components (components not belonging to any groups)
2020-02-01 23:06:01 +01:00
auto mainCompIt = this->Components.end();
2017-04-14 19:02:05 +02:00
std::map<std::string, cmCPackComponent>::iterator compIt;
for (compIt = this->Components.begin(); compIt != this->Components.end();
++compIt) {
// Does the component belong to a group?
2018-01-26 17:06:56 +01:00
if (compIt->second.Group == nullptr) {
2017-04-14 19:02:05 +02:00
std::string component(compIt->first);
std::transform(component.begin(), component.end(), component.begin(),
::toupper);
if (mainComponentUpper == component) {
// main component will be handled last
mainCompIt = compIt;
continue;
}
cmCPackLogger(
2018-08-09 18:06:22 +02:00
cmCPackLog::LOG_VERBOSE,
"Component <"
2017-04-14 19:02:05 +02:00
<< compIt->second.Name
<< "> does not belong to any group, package it separately."
<< std::endl);
retval &= PackageOnePack(initialTopLevel, compIt->first);
}
}
if (retval) {
this->SetOption("GENERATE_SPEC_PARTS", "OFF");
if (mainCompGIt != this->ComponentGroups.end()) {
retval &= PackageOnePack(initialTopLevel, mainCompGIt->first);
} else if (mainCompIt != this->Components.end()) {
retval &= PackageOnePack(initialTopLevel, mainCompIt->first);
} else {
2018-08-09 18:06:22 +02:00
cmCPackLogger(cmCPackLog::LOG_ERROR,
"CPACK_RPM_MAIN_COMPONENT set"
2017-04-14 19:02:05 +02:00
<< " to non existing component.\n");
retval = 0;
}
}
2016-07-09 11:21:54 +02:00
}
2017-04-14 19:02:05 +02:00
// CPACK_COMPONENTS_IGNORE_GROUPS is set
// We build 1 package per component
else {
2020-02-01 23:06:01 +01:00
auto mainCompIt = this->Components.end();
2017-04-14 19:02:05 +02:00
std::map<std::string, cmCPackComponent>::iterator compIt;
for (compIt = this->Components.begin(); compIt != this->Components.end();
++compIt) {
std::string component(compIt->first);
std::transform(component.begin(), component.end(), component.begin(),
::toupper);
if (mainComponentUpper == component) {
// main component will be handled last
mainCompIt = compIt;
continue;
}
2016-07-09 11:21:54 +02:00
retval &= PackageOnePack(initialTopLevel, compIt->first);
2011-01-16 11:35:12 +01:00
}
2017-04-14 19:02:05 +02:00
if (retval) {
this->SetOption("GENERATE_SPEC_PARTS", "OFF");
if (mainCompIt != this->Components.end()) {
retval &= PackageOnePack(initialTopLevel, mainCompIt->first);
} else {
2018-08-09 18:06:22 +02:00
cmCPackLogger(cmCPackLog::LOG_ERROR,
"CPACK_RPM_MAIN_COMPONENT set"
2017-04-14 19:02:05 +02:00
<< " to non existing component.\n");
retval = 0;
}
}
2011-01-16 11:35:12 +01:00
}
2017-04-14 19:02:05 +02:00
} else if (!this->IsOn("CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE") ||
this->Components.size() == 1) {
// The default behavior is to have one package by component group
// unless CPACK_COMPONENTS_IGNORE_GROUP is specified.
if (!ignoreGroup) {
std::map<std::string, cmCPackComponentGroup>::iterator compGIt;
for (compGIt = this->ComponentGroups.begin();
compGIt != this->ComponentGroups.end(); ++compGIt) {
2018-08-09 18:06:22 +02:00
cmCPackLogger(cmCPackLog::LOG_VERBOSE,
"Packaging component group: " << compGIt->first
<< std::endl);
2017-04-14 19:02:05 +02:00
retval &= PackageOnePack(initialTopLevel, compGIt->first);
}
// Handle Orphan components (components not belonging to any groups)
std::map<std::string, cmCPackComponent>::iterator compIt;
for (compIt = this->Components.begin(); compIt != this->Components.end();
++compIt) {
// Does the component belong to a group?
2018-01-26 17:06:56 +01:00
if (compIt->second.Group == nullptr) {
2017-04-14 19:02:05 +02:00
cmCPackLogger(
2018-08-09 18:06:22 +02:00
cmCPackLog::LOG_VERBOSE,
"Component <"
2017-04-14 19:02:05 +02:00
<< compIt->second.Name
<< "> does not belong to any group, package it separately."
<< std::endl);
retval &= PackageOnePack(initialTopLevel, compIt->first);
}
}
2011-06-19 15:41:06 +03:00
}
2017-04-14 19:02:05 +02:00
// CPACK_COMPONENTS_IGNORE_GROUPS is set
// We build 1 package per component
else {
std::map<std::string, cmCPackComponent>::iterator compIt;
for (compIt = this->Components.begin(); compIt != this->Components.end();
++compIt) {
retval &= PackageOnePack(initialTopLevel, compIt->first);
}
}
} else {
cmCPackLogger(
2018-08-09 18:06:22 +02:00
cmCPackLog::LOG_ERROR,
"CPACK_RPM_MAIN_COMPONENT not set but"
2017-04-14 19:02:05 +02:00
<< " it is mandatory with CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE"
<< " being set.\n");
retval = 0;
2016-07-09 11:21:54 +02:00
}
if (retval) {
AddGeneratedPackageNames();
}
2011-06-19 15:41:06 +03:00
return retval;
}
2016-07-09 11:21:54 +02:00
int cmCPackRPMGenerator::PackageComponentsAllInOne(
const std::string& compInstDirName)
2011-06-19 15:41:06 +03:00
{
int retval = 1;
/* Reset package file name list it will be populated during the
* component packaging run*/
packageFileNames.clear();
std::string initialTopLevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
2017-04-14 19:02:05 +02:00
if (this->IsOn("CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE")) {
this->SetOption("CPACK_RPM_DEBUGINFO_PACKAGE", "ON");
}
2011-06-19 15:41:06 +03:00
cmCPackLogger(cmCPackLog::LOG_VERBOSE,
"Packaging all groups in one package..."
"(CPACK_COMPONENTS_ALL_[GROUPS_]IN_ONE_PACKAGE is set)"
2016-07-09 11:21:54 +02:00
<< std::endl);
2011-06-19 15:41:06 +03:00
// The ALL GROUPS in ONE package case
std::string localToplevel(initialTopLevel);
2016-07-09 11:21:54 +02:00
std::string packageFileName(cmSystemTools::GetParentDirectory(toplevel));
2011-06-19 15:41:06 +03:00
std::string outputFileName(
2016-07-09 11:21:54 +02:00
std::string(this->GetOption("CPACK_PACKAGE_FILE_NAME")) +
this->GetOutputExtension());
2011-06-19 15:41:06 +03:00
// all GROUP in one vs all COMPONENT in one
2016-07-09 11:21:54 +02:00
localToplevel += "/" + compInstDirName;
2011-06-19 15:41:06 +03:00
/* replace the TEMP DIRECTORY with the component one */
2016-07-09 11:21:54 +02:00
this->SetOption("CPACK_TEMPORARY_DIRECTORY", localToplevel.c_str());
packageFileName += "/" + outputFileName;
2011-06-19 15:41:06 +03:00
/* replace proposed CPACK_OUTPUT_FILE_NAME */
2016-07-09 11:21:54 +02:00
this->SetOption("CPACK_OUTPUT_FILE_NAME", outputFileName.c_str());
2011-06-19 15:41:06 +03:00
/* replace the TEMPORARY package file name */
this->SetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
2016-07-09 11:21:54 +02:00
packageFileName.c_str());
if (!compInstDirName.empty()) {
// Tell CPackRPM.cmake the path where the component is.
2020-02-01 23:06:01 +01:00
std::string component_path = cmStrCat('/', compInstDirName);
2016-07-09 11:21:54 +02:00
this->SetOption("CPACK_RPM_PACKAGE_COMPONENT_PART_PATH",
component_path.c_str());
}
2018-10-28 12:09:07 +01:00
if (this->ReadListFile("Internal/CPack/CPackRPM.cmake")) {
2016-07-09 11:21:54 +02:00
AddGeneratedPackageNames();
} else {
2018-08-09 18:06:22 +02:00
cmCPackLogger(cmCPackLog::LOG_ERROR,
"Error while execution CPackRPM.cmake" << std::endl);
2011-06-19 15:41:06 +03:00
retval = 0;
2016-07-09 11:21:54 +02:00
}
2011-06-19 15:41:06 +03:00
return retval;
}
int cmCPackRPMGenerator::PackageFiles()
{
2016-07-09 11:21:54 +02:00
cmCPackLogger(cmCPackLog::LOG_DEBUG, "Toplevel: " << toplevel << std::endl);
2011-06-19 15:41:06 +03:00
/* Are we in the component packaging case */
2012-04-19 19:04:21 +03:00
if (WantsComponentInstallation()) {
2011-06-19 15:41:06 +03:00
// CASE 1 : COMPONENT ALL-IN-ONE package
// If ALL COMPONENTS in ONE package has been requested
// then the package file is unique and should be open here.
2016-07-09 11:21:54 +02:00
if (componentPackageMethod == ONE_PACKAGE) {
return PackageComponentsAllInOne("ALL_COMPONENTS_IN_ONE");
}
2011-06-19 15:41:06 +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-10-30 18:24:19 +01:00
return PackageComponents(componentPackageMethod ==
ONE_PACKAGE_PER_COMPONENT);
2011-06-19 15:41:06 +03:00
}
// CASE 3 : NON COMPONENT package.
2016-10-30 18:24:19 +01:00
return PackageComponentsAllInOne("");
}
2011-01-16 11:35:12 +01:00
bool cmCPackRPMGenerator::SupportsComponentInstallation() const
2016-07-09 11:21:54 +02:00
{
2016-10-30 18:24:19 +01:00
return IsOn("CPACK_RPM_COMPONENT_INSTALL");
2016-07-09 11:21:54 +02:00
}
2011-06-19 15:41:06 +03:00
std::string cmCPackRPMGenerator::GetComponentInstallDirNameSuffix(
2016-07-09 11:21:54 +02:00
const std::string& componentName)
{
2011-06-19 15:41:06 +03:00
if (componentPackageMethod == ONE_PACKAGE_PER_COMPONENT) {
return componentName;
}
if (componentPackageMethod == ONE_PACKAGE) {
return std::string("ALL_COMPONENTS_IN_ONE");
}
// We have to find the name of the COMPONENT GROUP
// the current COMPONENT belongs to.
2016-07-09 11:21:54 +02:00
std::string groupVar =
"CPACK_COMPONENT_" + cmSystemTools::UpperCase(componentName) + "_GROUP";
2018-01-26 17:06:56 +01:00
if (nullptr != GetOption(groupVar)) {
2016-07-09 11:21:54 +02:00
return std::string(GetOption(groupVar));
2011-06-19 15:41:06 +03:00
}
2016-10-30 18:24:19 +01:00
return componentName;
2016-07-09 11:21:54 +02:00
}