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. */
|
2013-11-02 20:24:53 +04:00
|
|
|
#include "cmCPackProductBuildGenerator.h"
|
|
|
|
|
2016-06-07 15:39:45 +03:00
|
|
|
#include "cmCPackComponentGroup.h"
|
|
|
|
#include "cmCPackLog.h"
|
|
|
|
#include "cmGeneratedFileStream.h"
|
2013-11-02 20:24:53 +04:00
|
|
|
#include "cmGlobalGenerator.h"
|
|
|
|
#include "cmLocalGenerator.h"
|
|
|
|
#include "cmMakefile.h"
|
2016-06-07 15:39:45 +03:00
|
|
|
#include "cmSystemTools.h"
|
|
|
|
#include "cmake.h"
|
2013-11-02 20:24:53 +04:00
|
|
|
|
|
|
|
#include <cmsys/Glob.hxx>
|
2016-06-07 15:39:45 +03:00
|
|
|
#include <cmsys/SystemTools.hxx>
|
2013-11-02 20:24:53 +04:00
|
|
|
|
|
|
|
cmCPackProductBuildGenerator::cmCPackProductBuildGenerator()
|
|
|
|
{
|
|
|
|
this->componentPackageMethod = ONE_PACKAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmCPackProductBuildGenerator::~cmCPackProductBuildGenerator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int cmCPackProductBuildGenerator::PackageFiles()
|
|
|
|
{
|
|
|
|
// TODO: Use toplevel
|
|
|
|
// It is used! Is this an obsolete comment?
|
|
|
|
|
2016-06-07 15:39:45 +03:00
|
|
|
std::string packageDirFileName =
|
|
|
|
this->GetOption("CPACK_TEMPORARY_DIRECTORY");
|
2013-11-02 20:24:53 +04:00
|
|
|
|
|
|
|
// Create the directory where component packages will be built.
|
|
|
|
std::string basePackageDir = packageDirFileName;
|
|
|
|
basePackageDir += "/Contents/Packages";
|
2016-06-07 15:39:45 +03:00
|
|
|
if (!cmsys::SystemTools::MakeDirectory(basePackageDir.c_str())) {
|
2013-11-02 20:24:53 +04:00
|
|
|
cmCPackLogger(cmCPackLog::LOG_ERROR,
|
|
|
|
"Problem creating component packages directory: "
|
2016-06-07 00:53:32 +03:00
|
|
|
<< basePackageDir << std::endl);
|
2013-11-02 20:24:53 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-07 15:39:45 +03:00
|
|
|
if (!this->Components.empty()) {
|
2013-11-02 20:24:53 +04:00
|
|
|
std::map<std::string, cmCPackComponent>::iterator compIt;
|
|
|
|
for (compIt = this->Components.begin(); compIt != this->Components.end();
|
2016-06-07 15:39:45 +03:00
|
|
|
++compIt) {
|
2013-11-02 20:24:53 +04:00
|
|
|
std::string packageDir = toplevel;
|
|
|
|
packageDir += '/';
|
|
|
|
packageDir += compIt->first;
|
|
|
|
if (!this->GenerateComponentPackage(basePackageDir,
|
|
|
|
GetPackageName(compIt->second),
|
2016-06-07 15:39:45 +03:00
|
|
|
packageDir, &compIt->second)) {
|
2013-11-02 20:24:53 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2016-06-07 15:39:45 +03:00
|
|
|
} else {
|
|
|
|
if (!this->GenerateComponentPackage(basePackageDir,
|
|
|
|
this->GetOption("CPACK_PACKAGE_NAME"),
|
|
|
|
toplevel, NULL)) {
|
2013-11-02 20:24:53 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy or create all of the resource files we need.
|
|
|
|
std::string resDir = packageDirFileName + "/Contents";
|
2016-09-04 17:55:05 +03:00
|
|
|
if (!this->CopyCreateResourceFile("License", resDir) ||
|
|
|
|
!this->CopyCreateResourceFile("ReadMe", resDir) ||
|
|
|
|
!this->CopyCreateResourceFile("Welcome", resDir)) {
|
2013-11-02 20:24:53 +04:00
|
|
|
cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem copying the resource files"
|
2016-06-07 15:39:45 +03:00
|
|
|
<< std::endl);
|
2013-11-02 20:24:53 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// combine package(s) into a distribution
|
|
|
|
WriteDistributionFile(packageDirFileName.c_str());
|
|
|
|
std::ostringstream pkgCmd;
|
|
|
|
|
|
|
|
std::string version = this->GetOption("CPACK_PACKAGE_VERSION");
|
|
|
|
std::string productbuild = this->GetOption("CPACK_COMMAND_PRODUCTBUILD");
|
|
|
|
|
2016-06-07 15:39:45 +03:00
|
|
|
pkgCmd << productbuild << " --distribution \"" << packageDirFileName
|
|
|
|
<< "/Contents/distribution.dist\""
|
|
|
|
<< " --package-path \"" << packageDirFileName << "/Contents/Packages"
|
|
|
|
<< "\""
|
|
|
|
<< " --resources \"" << resDir << "\""
|
|
|
|
<< " --version \"" << version << "\""
|
|
|
|
<< " \"" << packageFileNames[0] << "\"";
|
2013-11-02 20:24:53 +04:00
|
|
|
|
|
|
|
// Run ProductBuild
|
|
|
|
return RunProductBuild(pkgCmd.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
int cmCPackProductBuildGenerator::InitializeInternal()
|
|
|
|
{
|
|
|
|
this->SetOptionIfNotSet("CPACK_PACKAGING_INSTALL_PREFIX", "/Applications");
|
|
|
|
|
|
|
|
std::vector<std::string> no_paths;
|
|
|
|
std::string program =
|
|
|
|
cmSystemTools::FindProgram("pkgbuild", no_paths, false);
|
|
|
|
if (program.empty()) {
|
|
|
|
cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find pkgbuild executable"
|
|
|
|
<< std::endl);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
this->SetOptionIfNotSet("CPACK_COMMAND_PKGBUILD", program.c_str());
|
|
|
|
|
|
|
|
program = cmSystemTools::FindProgram("productbuild", no_paths, false);
|
|
|
|
if (program.empty()) {
|
|
|
|
cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find productbuild executable"
|
|
|
|
<< std::endl);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
this->SetOptionIfNotSet("CPACK_COMMAND_PRODUCTBUILD", program.c_str());
|
|
|
|
|
|
|
|
return this->Superclass::InitializeInternal();
|
|
|
|
}
|
|
|
|
|
2016-06-07 15:39:45 +03:00
|
|
|
bool cmCPackProductBuildGenerator::RunProductBuild(const std::string& command)
|
2013-11-02 20:24:53 +04:00
|
|
|
{
|
|
|
|
std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
|
|
|
|
tmpFile += "/ProductBuildOutput.log";
|
|
|
|
|
|
|
|
cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Execute: " << command << std::endl);
|
|
|
|
std::string output, error_output;
|
|
|
|
int retVal = 1;
|
2016-06-07 15:39:45 +03:00
|
|
|
bool res =
|
|
|
|
cmSystemTools::RunSingleCommand(command.c_str(), &output, &error_output,
|
|
|
|
&retVal, 0, this->GeneratorVerbose, 0);
|
|
|
|
cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Done running command" << std::endl);
|
|
|
|
if (!res || retVal) {
|
2013-11-02 20:24:53 +04:00
|
|
|
cmGeneratedFileStream ofs(tmpFile.c_str());
|
|
|
|
ofs << "# Run command: " << command << std::endl
|
2016-06-07 00:53:32 +03:00
|
|
|
<< "# Output:" << std::endl
|
|
|
|
<< output << std::endl;
|
2013-11-02 20:24:53 +04:00
|
|
|
cmCPackLogger(cmCPackLog::LOG_ERROR,
|
2016-06-07 00:53:32 +03:00
|
|
|
"Problem running command: " << command << std::endl
|
|
|
|
<< "Please check " << tmpFile
|
|
|
|
<< " for errors" << std::endl);
|
2013-11-02 20:24:53 +04:00
|
|
|
return false;
|
2016-06-07 15:39:45 +03:00
|
|
|
}
|
2013-11-02 20:24:53 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cmCPackProductBuildGenerator::GenerateComponentPackage(
|
2016-06-07 15:39:45 +03:00
|
|
|
const std::string& packageFileDir, const std::string& packageFileName,
|
|
|
|
const std::string& packageDir, const cmCPackComponent* component)
|
2013-11-02 20:24:53 +04:00
|
|
|
{
|
|
|
|
std::string packageFile = packageFileDir;
|
|
|
|
packageFile += '/';
|
|
|
|
packageFile += packageFileName;
|
|
|
|
|
2016-06-07 15:39:45 +03:00
|
|
|
cmCPackLogger(cmCPackLog::LOG_OUTPUT, "- Building component package: "
|
|
|
|
<< packageFile << std::endl);
|
2013-11-02 20:24:53 +04:00
|
|
|
|
|
|
|
const char* comp_name = component ? component->Name.c_str() : NULL;
|
|
|
|
|
|
|
|
const char* preflight = this->GetComponentScript("PREFLIGHT", comp_name);
|
|
|
|
const char* postflight = this->GetComponentScript("POSTFLIGHT", comp_name);
|
|
|
|
|
|
|
|
std::string resDir = packageFileDir;
|
2016-06-07 15:39:45 +03:00
|
|
|
if (component) {
|
2013-11-02 20:24:53 +04:00
|
|
|
resDir += "/";
|
|
|
|
resDir += component->Name;
|
|
|
|
}
|
|
|
|
std::string scriptDir = resDir + "/scripts";
|
|
|
|
|
2016-06-07 15:39:45 +03:00
|
|
|
if (!cmsys::SystemTools::MakeDirectory(scriptDir.c_str())) {
|
2013-11-02 20:24:53 +04:00
|
|
|
cmCPackLogger(cmCPackLog::LOG_ERROR,
|
2016-06-07 00:53:32 +03:00
|
|
|
"Problem creating installer directory: " << scriptDir
|
|
|
|
<< std::endl);
|
2013-11-02 20:24:53 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if preflight, postflight, or postupgrade are set
|
|
|
|
// then copy them into the script directory and make
|
|
|
|
// them executable
|
2016-06-07 15:39:45 +03:00
|
|
|
if (preflight) {
|
2016-09-04 17:55:05 +03:00
|
|
|
this->CopyInstallScript(scriptDir, preflight, "preinstall");
|
2013-11-02 20:24:53 +04:00
|
|
|
}
|
2016-06-07 15:39:45 +03:00
|
|
|
if (postflight) {
|
2016-09-04 17:55:05 +03:00
|
|
|
this->CopyInstallScript(scriptDir, postflight, "postinstall");
|
2013-11-02 20:24:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// The command that will be used to run ProductBuild
|
|
|
|
std::ostringstream pkgCmd;
|
|
|
|
|
|
|
|
std::string pkgId = "com.";
|
|
|
|
pkgId += this->GetOption("CPACK_PACKAGE_VENDOR");
|
|
|
|
pkgId += '.';
|
|
|
|
pkgId += this->GetOption("CPACK_PACKAGE_NAME");
|
2016-06-07 15:39:45 +03:00
|
|
|
if (component) {
|
2013-11-02 20:24:53 +04:00
|
|
|
pkgId += '.';
|
|
|
|
pkgId += component->Name;
|
2016-06-07 15:39:45 +03:00
|
|
|
}
|
2013-11-02 20:24:53 +04:00
|
|
|
|
|
|
|
std::string version = this->GetOption("CPACK_PACKAGE_VERSION");
|
|
|
|
std::string pkgbuild = this->GetOption("CPACK_COMMAND_PKGBUILD");
|
|
|
|
|
2016-06-07 15:39:45 +03:00
|
|
|
pkgCmd << pkgbuild << " --root \"" << packageDir << "\""
|
2013-11-02 20:24:53 +04:00
|
|
|
<< " --identifier \"" << pkgId << "\""
|
|
|
|
<< " --scripts \"" << scriptDir << "\""
|
|
|
|
<< " --version \"" << version << "\""
|
|
|
|
<< " --install-location \"/\""
|
|
|
|
<< " \"" << packageFile << "\"";
|
|
|
|
|
|
|
|
// Run ProductBuild
|
|
|
|
return RunProductBuild(pkgCmd.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* cmCPackProductBuildGenerator::GetComponentScript(
|
2016-06-07 15:39:45 +03:00
|
|
|
const char* script, const char* component_name)
|
2013-11-02 20:24:53 +04:00
|
|
|
{
|
|
|
|
std::string scriptname = std::string("CPACK_") + script + "_";
|
2016-06-07 15:39:45 +03:00
|
|
|
if (component_name) {
|
2013-11-02 20:24:53 +04:00
|
|
|
scriptname += cmSystemTools::UpperCase(component_name);
|
|
|
|
scriptname += "_";
|
|
|
|
}
|
|
|
|
scriptname += "SCRIPT";
|
|
|
|
|
|
|
|
return this->GetOption(scriptname);
|
|
|
|
}
|