cmake/Source/CPack/cpack.cxx

452 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. */
2017-07-20 19:35:53 +02:00
#include "cmsys/CommandLineArguments.hxx"
#include "cmsys/Encoding.hxx"
2017-04-14 19:02:05 +02:00
#include <iostream>
#include <map>
2018-01-26 17:06:56 +01:00
#include <memory> // IWYU pragma: keep
2017-04-14 19:02:05 +02:00
#include <sstream>
#include <stddef.h>
#include <string>
#include <utility>
#include <vector>
#if defined(_WIN32) && defined(CMAKE_BUILD_WITH_CMAKE)
2018-08-09 18:06:22 +02:00
# include "cmsys/ConsoleBuf.hxx"
2017-04-14 19:02:05 +02:00
#endif
#include "cmCPackGenerator.h"
2016-07-09 11:21:54 +02:00
#include "cmCPackGeneratorFactory.h"
2016-10-30 18:24:19 +01:00
#include "cmCPackLog.h"
2016-07-09 11:21:54 +02:00
#include "cmDocumentation.h"
2016-10-30 18:24:19 +01:00
#include "cmDocumentationEntry.h"
#include "cmGlobalGenerator.h"
#include "cmMakefile.h"
2017-04-14 19:02:05 +02:00
#include "cmStateSnapshot.h"
2016-10-30 18:24:19 +01:00
#include "cmSystemTools.h"
2016-07-09 11:21:54 +02:00
#include "cmake.h"
2016-07-09 11:21:54 +02:00
static const char* cmDocumentationName[][2] = {
2018-01-26 17:06:56 +01:00
{ nullptr, " cpack - Packaging driver provided by CMake." },
{ nullptr, nullptr }
};
2016-07-09 11:21:54 +02:00
static const char* cmDocumentationUsage[][2] = {
2018-08-09 18:06:22 +02:00
// clang-format off
{ nullptr, " cpack [options]" },
2018-01-26 17:06:56 +01:00
{ nullptr, nullptr }
2018-08-09 18:06:22 +02:00
// clang-format on
};
2016-07-09 11:21:54 +02:00
static const char* cmDocumentationOptions[][2] = {
2018-08-09 18:06:22 +02:00
{ "-G <generators>", "Override/define CPACK_GENERATOR" },
2016-07-09 11:21:54 +02:00
{ "-C <Configuration>", "Specify the project configuration" },
{ "-D <var>=<value>", "Set a CPack variable." },
2018-08-09 18:06:22 +02:00
{ "--config <configFile>", "Specify the config file." },
{ "--verbose,-V", "Enable verbose output" },
2018-04-23 21:13:27 +02:00
{ "--trace", "Put underlying cmake scripts in trace mode." },
{ "--trace-expand", "Put underlying cmake scripts in expanded trace mode." },
2018-08-09 18:06:22 +02:00
{ "--debug", "Enable debug output (for CPack developers)" },
{ "-P <packageName>", "Override/define CPACK_PACKAGE_NAME" },
{ "-R <packageVersion>", "Override/define CPACK_PACKAGE_VERSION" },
{ "-B <packageDirectory>", "Override/define CPACK_PACKAGE_DIRECTORY" },
{ "--vendor <vendorName>", "Override/define CPACK_PACKAGE_VENDOR" },
2018-01-26 17:06:56 +01:00
{ nullptr, nullptr }
};
2016-10-30 18:24:19 +01:00
int cpackUnknownArgument(const char* /*unused*/, void* /*unused*/)
{
return 1;
}
struct cpackDefinitions
{
2015-04-27 22:25:09 +02:00
typedef std::map<std::string, std::string> MapType;
MapType Map;
2016-07-09 11:21:54 +02:00
cmCPackLog* Log;
};
int cpackDefinitionArgument(const char* argument, const char* cValue,
2016-07-09 11:21:54 +02:00
void* call_data)
{
(void)argument;
cpackDefinitions* def = static_cast<cpackDefinitions*>(call_data);
std::string value = cValue;
2016-07-09 11:21:54 +02:00
size_t pos = value.find_first_of('=');
if (pos == std::string::npos) {
cmCPack_Log(def->Log, cmCPackLog::LOG_ERROR,
2016-07-09 11:21:54 +02:00
"Please specify CPack definitions as: KEY=VALUE" << std::endl);
return 0;
2016-07-09 11:21:54 +02:00
}
std::string key = value.substr(0, pos);
value = value.c_str() + pos + 1;
def->Map[key] = value;
2018-08-09 18:06:22 +02:00
cmCPack_Log(def->Log, cmCPackLog::LOG_DEBUG,
"Set CPack variable: " << key << " to \"" << value << "\""
<< std::endl);
return 1;
}
// this is CPack.
2016-07-09 11:21:54 +02:00
int main(int argc, char const* const* argv)
{
2017-04-14 19:02:05 +02:00
#if defined(_WIN32) && defined(CMAKE_BUILD_WITH_CMAKE)
// Replace streambuf so we can output Unicode to console
cmsys::ConsoleBuf::Manager consoleOut(std::cout);
consoleOut.SetUTF8Pipes();
cmsys::ConsoleBuf::Manager consoleErr(std::cerr, true);
consoleErr.SetUTF8Pipes();
#endif
2014-08-03 19:52:23 +02:00
cmsys::Encoding::CommandLineArguments args =
cmsys::Encoding::CommandLineArguments::Main(argc, argv);
argc = args.argc();
argv = args.argv();
2018-04-23 21:13:27 +02:00
cmSystemTools::EnableMSVCDebugHook();
cmSystemTools::InitializeLibUV();
2014-08-03 19:52:23 +02:00
cmSystemTools::FindCMakeResources(argv[0]);
cmCPackLog log;
2012-04-19 19:04:21 +03:00
log.SetErrorPrefix("CPack Error: ");
log.SetWarningPrefix("CPack Warning: ");
log.SetOutputPrefix("CPack: ");
log.SetVerbosePrefix("CPack Verbose: ");
2016-07-09 11:21:54 +02:00
if (cmSystemTools::GetCurrentWorkingDirectory().empty()) {
cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
2016-07-09 11:21:54 +02:00
"Current working directory cannot be established."
<< std::endl);
2013-03-16 19:13:01 +02:00
return 1;
2016-07-09 11:21:54 +02:00
}
std::string generator;
bool help = false;
bool helpVersion = false;
bool verbose = false;
2018-04-23 21:13:27 +02:00
bool trace = false;
bool traceExpand = false;
bool debug = false;
std::string helpFull;
std::string helpMAN;
std::string helpHTML;
std::string cpackProjectName;
2012-08-04 10:26:08 +03:00
std::string cpackProjectDirectory;
std::string cpackBuildConfig;
std::string cpackProjectVersion;
std::string cpackProjectPatch;
std::string cpackProjectVendor;
std::string cpackConfigFile;
cpackDefinitions definitions;
definitions.Log = &log;
2018-01-26 17:06:56 +01:00
cpackConfigFile.clear();
cmsys::CommandLineArguments arg;
arg.Initialize(argc, argv);
typedef cmsys::CommandLineArguments argT;
// Help arguments
arg.AddArgument("--help", argT::NO_ARGUMENT, &help, "CPack help");
arg.AddArgument("--help-full", argT::SPACE_ARGUMENT, &helpFull,
2016-07-09 11:21:54 +02:00
"CPack help");
arg.AddArgument("--help-html", argT::SPACE_ARGUMENT, &helpHTML,
2016-07-09 11:21:54 +02:00
"CPack help");
arg.AddArgument("--help-man", argT::SPACE_ARGUMENT, &helpMAN, "CPack help");
arg.AddArgument("--version", argT::NO_ARGUMENT, &helpVersion, "CPack help");
arg.AddArgument("-V", argT::NO_ARGUMENT, &verbose, "CPack verbose");
arg.AddArgument("--verbose", argT::NO_ARGUMENT, &verbose, "-V");
arg.AddArgument("--debug", argT::NO_ARGUMENT, &debug, "-V");
arg.AddArgument("--config", argT::SPACE_ARGUMENT, &cpackConfigFile,
2016-07-09 11:21:54 +02:00
"CPack configuration file");
2018-04-23 21:13:27 +02:00
arg.AddArgument("--trace", argT::NO_ARGUMENT, &trace,
"Put underlying cmake scripts in trace mode.");
arg.AddArgument("--trace-expand", argT::NO_ARGUMENT, &traceExpand,
"Put underlying cmake scripts in expanded trace mode.");
arg.AddArgument("-C", argT::SPACE_ARGUMENT, &cpackBuildConfig,
2016-07-09 11:21:54 +02:00
"CPack build configuration");
arg.AddArgument("-G", argT::SPACE_ARGUMENT, &generator, "CPack generator");
arg.AddArgument("-P", argT::SPACE_ARGUMENT, &cpackProjectName,
"CPack project name");
arg.AddArgument("-R", argT::SPACE_ARGUMENT, &cpackProjectVersion,
"CPack project version");
arg.AddArgument("-B", argT::SPACE_ARGUMENT, &cpackProjectDirectory,
"CPack project directory");
arg.AddArgument("--patch", argT::SPACE_ARGUMENT, &cpackProjectPatch,
"CPack project patch");
arg.AddArgument("--vendor", argT::SPACE_ARGUMENT, &cpackProjectVendor,
"CPack project vendor");
arg.AddCallback("-D", argT::SPACE_ARGUMENT, cpackDefinitionArgument,
&definitions, "CPack Definitions");
arg.SetUnknownArgumentCallback(cpackUnknownArgument);
// Parse command line
int parsed = arg.Parse();
// Setup logging
2016-07-09 11:21:54 +02:00
if (verbose) {
log.SetVerbose(verbose);
2010-11-13 01:00:53 +02:00
cmCPack_Log(&log, cmCPackLog::LOG_OUTPUT, "Enable Verbose" << std::endl);
2016-07-09 11:21:54 +02:00
}
if (debug) {
log.SetDebug(debug);
cmCPack_Log(&log, cmCPackLog::LOG_OUTPUT, "Enable Debug" << std::endl);
2016-07-09 11:21:54 +02:00
}
cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE,
2016-07-09 11:21:54 +02:00
"Read CPack config file: " << cpackConfigFile << std::endl);
2017-07-20 19:35:53 +02:00
cmake cminst(cmake::RoleScript);
2015-08-17 11:37:30 +02:00
cminst.SetHomeDirectory("");
cminst.SetHomeOutputDirectory("");
2016-03-13 13:35:51 +01:00
cminst.GetCurrentSnapshot().SetDefaultDefinitions();
2015-08-17 11:37:30 +02:00
cmGlobalGenerator cmgg(&cminst);
2018-01-26 17:06:56 +01:00
cmMakefile globalMF(&cmgg, cminst.GetCurrentSnapshot());
2015-08-17 11:37:30 +02:00
#if defined(__CYGWIN__)
2018-01-26 17:06:56 +01:00
globalMF.AddDefinition("CMAKE_LEGACY_CYGWIN_WIN32", "0");
2015-08-17 11:37:30 +02:00
#endif
2018-04-23 21:13:27 +02:00
if (trace) {
cminst.SetTrace(true);
}
if (traceExpand) {
cminst.SetTrace(true);
cminst.SetTraceExpand(true);
}
bool cpackConfigFileSpecified = true;
2016-07-09 11:21:54 +02:00
if (cpackConfigFile.empty()) {
cpackConfigFile = cmSystemTools::GetCurrentWorkingDirectory();
cpackConfigFile += "/CPackConfig.cmake";
cpackConfigFileSpecified = false;
2016-07-09 11:21:54 +02:00
}
cmCPackGeneratorFactory generators;
generators.SetLogger(&log);
2018-01-26 17:06:56 +01:00
cmCPackGenerator* cpackGenerator = nullptr;
2012-04-19 19:04:21 +03:00
cmDocumentation doc;
doc.addCPackStandardDocSections();
2012-08-04 10:26:08 +03:00
/* Were we invoked to display doc or to do some work ?
* Unlike cmake launching cpack with zero argument
* should launch cpack using "cpackConfigFile" if it exists
* in the current directory.
*/
2016-10-30 18:24:19 +01:00
help = doc.CheckOptions(argc, argv, "-G") && argc != 1;
2012-04-19 19:04:21 +03:00
// This part is used for cpack documentation lookup as well.
cminst.AddCMakePaths();
2016-07-09 11:21:54 +02:00
if (parsed && !help) {
// find out which system cpack is running on, so it can setup the search
// paths, so FIND_XXX() commands can be used in scripts
2012-02-18 12:40:36 +02:00
std::string systemFile =
2018-01-26 17:06:56 +01:00
globalMF.GetModulesFile("CMakeDetermineSystem.cmake");
if (!globalMF.ReadListFile(systemFile.c_str())) {
cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
2016-07-09 11:21:54 +02:00
"Error reading CMakeDetermineSystem.cmake" << std::endl);
return 1;
2016-07-09 11:21:54 +02:00
}
2012-02-18 12:40:36 +02:00
systemFile =
2018-01-26 17:06:56 +01:00
globalMF.GetModulesFile("CMakeSystemSpecificInformation.cmake");
if (!globalMF.ReadListFile(systemFile.c_str())) {
cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
2016-07-09 11:21:54 +02:00
"Error reading CMakeSystemSpecificInformation.cmake"
<< std::endl);
return 1;
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
if (!cpackBuildConfig.empty()) {
2018-01-26 17:06:56 +01:00
globalMF.AddDefinition("CPACK_BUILD_CONFIG", cpackBuildConfig.c_str());
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
2018-04-23 21:13:27 +02:00
if (cmSystemTools::FileExists(cpackConfigFile)) {
2016-07-09 11:21:54 +02:00
cpackConfigFile = cmSystemTools::CollapseFullPath(cpackConfigFile);
cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE,
2016-07-09 11:21:54 +02:00
"Read CPack configuration file: " << cpackConfigFile
<< std::endl);
2018-01-26 17:06:56 +01:00
if (!globalMF.ReadListFile(cpackConfigFile.c_str())) {
cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
2016-07-09 11:21:54 +02:00
"Problem reading CPack config file: \""
<< cpackConfigFile << "\"" << std::endl);
return 1;
}
2016-07-09 11:21:54 +02:00
} else if (cpackConfigFileSpecified) {
cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
2016-07-09 11:21:54 +02:00
"Cannot find CPack config file: \"" << cpackConfigFile
<< "\"" << std::endl);
return 1;
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
if (!generator.empty()) {
2018-01-26 17:06:56 +01:00
globalMF.AddDefinition("CPACK_GENERATOR", generator.c_str());
2016-07-09 11:21:54 +02:00
}
if (!cpackProjectName.empty()) {
2018-01-26 17:06:56 +01:00
globalMF.AddDefinition("CPACK_PACKAGE_NAME", cpackProjectName.c_str());
2016-07-09 11:21:54 +02:00
}
if (!cpackProjectVersion.empty()) {
2018-01-26 17:06:56 +01:00
globalMF.AddDefinition("CPACK_PACKAGE_VERSION",
cpackProjectVersion.c_str());
2016-07-09 11:21:54 +02:00
}
if (!cpackProjectVendor.empty()) {
2018-01-26 17:06:56 +01:00
globalMF.AddDefinition("CPACK_PACKAGE_VENDOR",
cpackProjectVendor.c_str());
2016-07-09 11:21:54 +02:00
}
2012-08-04 10:26:08 +03:00
// if this is not empty it has been set on the command line
// go for it. Command line override values set in config file.
2016-07-09 11:21:54 +02:00
if (!cpackProjectDirectory.empty()) {
2018-01-26 17:06:56 +01:00
globalMF.AddDefinition("CPACK_PACKAGE_DIRECTORY",
cpackProjectDirectory.c_str());
2016-07-09 11:21:54 +02:00
}
2012-08-04 10:26:08 +03:00
// The value has not been set on the command line
2016-07-09 11:21:54 +02:00
else {
2012-08-04 10:26:08 +03:00
// get a default value (current working directory)
cpackProjectDirectory = cmsys::SystemTools::GetCurrentWorkingDirectory();
// use default value iff no value has been provided by the config file
2018-01-26 17:06:56 +01:00
if (!globalMF.IsSet("CPACK_PACKAGE_DIRECTORY")) {
globalMF.AddDefinition("CPACK_PACKAGE_DIRECTORY",
cpackProjectDirectory.c_str());
}
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
for (auto const& cd : definitions.Map) {
globalMF.AddDefinition(cd.first, cd.second.c_str());
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
const char* cpackModulesPath = globalMF.GetDefinition("CPACK_MODULE_PATH");
2016-07-09 11:21:54 +02:00
if (cpackModulesPath) {
2018-01-26 17:06:56 +01:00
globalMF.AddDefinition("CMAKE_MODULE_PATH", cpackModulesPath);
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
const char* genList = globalMF.GetDefinition("CPACK_GENERATOR");
2016-07-09 11:21:54 +02:00
if (!genList) {
2018-08-09 18:06:22 +02:00
cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
"CPack generator not specified" << std::endl);
2016-07-09 11:21:54 +02:00
} else {
std::vector<std::string> generatorsVector;
2016-07-09 11:21:54 +02:00
cmSystemTools::ExpandListArgument(genList, generatorsVector);
2018-01-26 17:06:56 +01:00
for (std::string const& gen : generatorsVector) {
cmMakefile::ScopePushPop raii(&globalMF);
cmMakefile* mf = &globalMF;
cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE,
2016-07-09 11:21:54 +02:00
"Specified generator: " << gen << std::endl);
if (parsed && !mf->GetDefinition("CPACK_PACKAGE_NAME")) {
cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
2016-07-09 11:21:54 +02:00
"CPack project name not specified" << std::endl);
parsed = 0;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
if (parsed &&
!(mf->GetDefinition("CPACK_PACKAGE_VERSION") ||
(mf->GetDefinition("CPACK_PACKAGE_VERSION_MAJOR") &&
mf->GetDefinition("CPACK_PACKAGE_VERSION_MINOR") &&
2016-07-09 11:21:54 +02:00
mf->GetDefinition("CPACK_PACKAGE_VERSION_PATCH")))) {
cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
2016-07-09 11:21:54 +02:00
"CPack project version not specified"
<< std::endl
<< "Specify CPACK_PACKAGE_VERSION, or "
"CPACK_PACKAGE_VERSION_MAJOR, "
"CPACK_PACKAGE_VERSION_MINOR, and "
"CPACK_PACKAGE_VERSION_PATCH."
<< std::endl);
parsed = 0;
2016-07-09 11:21:54 +02:00
}
if (parsed) {
cpackGenerator = generators.NewGenerator(gen);
2018-04-23 21:13:27 +02:00
if (cpackGenerator) {
cpackGenerator->SetTrace(trace);
cpackGenerator->SetTraceExpand(traceExpand);
} else {
cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
2016-07-09 11:21:54 +02:00
"Cannot initialize CPack generator: " << gen
<< std::endl);
parsed = 0;
2016-07-09 11:21:54 +02:00
}
2018-04-23 21:13:27 +02:00
2016-07-09 11:21:54 +02:00
if (parsed && !cpackGenerator->Initialize(gen, mf)) {
cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
2016-07-09 11:21:54 +02:00
"Cannot initialize the generator " << gen
<< std::endl);
parsed = 0;
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
if (!mf->GetDefinition("CPACK_INSTALL_COMMANDS") &&
2018-01-26 17:06:56 +01:00
!mf->GetDefinition("CPACK_INSTALL_SCRIPT") &&
2016-07-09 11:21:54 +02:00
!mf->GetDefinition("CPACK_INSTALLED_DIRECTORIES") &&
!mf->GetDefinition("CPACK_INSTALL_CMAKE_PROJECTS")) {
cmCPack_Log(
&log, cmCPackLog::LOG_ERROR,
"Please specify build tree of the project that uses CMake "
"using CPACK_INSTALL_CMAKE_PROJECTS, specify "
2018-01-26 17:06:56 +01:00
"CPACK_INSTALL_COMMANDS, CPACK_INSTALL_SCRIPT, or "
"CPACK_INSTALLED_DIRECTORIES."
2016-07-09 11:21:54 +02:00
<< std::endl);
parsed = 0;
2016-07-09 11:21:54 +02:00
}
if (parsed) {
const char* projName = mf->GetDefinition("CPACK_PACKAGE_NAME");
2018-08-09 18:06:22 +02:00
cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE,
"Use generator: " << cpackGenerator->GetNameOfClass()
<< std::endl);
2016-07-09 11:21:54 +02:00
cmCPack_Log(&log, cmCPackLog::LOG_VERBOSE,
"For project: " << projName << std::endl);
2012-02-18 12:40:36 +02:00
const char* projVersion =
mf->GetDefinition("CPACK_PACKAGE_VERSION");
2016-07-09 11:21:54 +02:00
if (!projVersion) {
const char* projVersionMajor =
mf->GetDefinition("CPACK_PACKAGE_VERSION_MAJOR");
const char* projVersionMinor =
mf->GetDefinition("CPACK_PACKAGE_VERSION_MINOR");
const char* projVersionPatch =
mf->GetDefinition("CPACK_PACKAGE_VERSION_PATCH");
2015-04-27 22:25:09 +02:00
std::ostringstream ostr;
ostr << projVersionMajor << "." << projVersionMinor << "."
2016-07-09 11:21:54 +02:00
<< projVersionPatch;
mf->AddDefinition("CPACK_PACKAGE_VERSION", ostr.str().c_str());
}
int res = cpackGenerator->DoPackage();
2016-07-09 11:21:54 +02:00
if (!res) {
cmCPack_Log(&log, cmCPackLog::LOG_ERROR,
2016-07-09 11:21:54 +02:00
"Error when generating package: " << projName
<< std::endl);
return 1;
}
}
}
}
}
2016-07-09 11:21:54 +02:00
}
2012-04-19 19:04:21 +03:00
/* In this case we are building the documentation object
* instance in order to create appropriate structure
* in order to satisfy the appropriate --help-xxx request
*/
2016-07-09 11:21:54 +02:00
if (help) {
// Construct and print requested documentation.
2012-04-19 19:04:21 +03:00
doc.SetName("cpack");
2016-07-09 11:21:54 +02:00
doc.SetSection("Name", cmDocumentationName);
doc.SetSection("Usage", cmDocumentationUsage);
doc.PrependSection("Options", cmDocumentationOptions);
std::vector<cmDocumentationEntry> v;
2018-01-26 17:06:56 +01:00
for (auto const& g : generators.GetGeneratorsList()) {
cmDocumentationEntry e;
2018-01-26 17:06:56 +01:00
e.Name = g.first;
e.Brief = g.second;
2018-04-23 21:13:27 +02:00
v.push_back(std::move(e));
2016-07-09 11:21:54 +02:00
}
doc.SetSection("Generators", v);
2016-07-09 11:21:54 +02:00
return doc.PrintRequestedDocumentation(std::cout) ? 0 : 1;
}
2016-07-09 11:21:54 +02:00
if (cmSystemTools::GetErrorOccuredFlag()) {
return 1;
2016-07-09 11:21:54 +02:00
}
return 0;
}