cmCPackGenerator.cxx 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCPackGenerator.h"
  4. #include "cmsys/FStream.hxx"
  5. #include "cmsys/Glob.hxx"
  6. #include "cmsys/RegularExpression.hxx"
  7. #include <algorithm>
  8. #include <memory> // IWYU pragma: keep
  9. #include <utility>
  10. #include "cmCPackComponentGroup.h"
  11. #include "cmCPackLog.h"
  12. #include "cmCryptoHash.h"
  13. #include "cmGeneratedFileStream.h"
  14. #include "cmGlobalGenerator.h"
  15. #include "cmMakefile.h"
  16. #include "cmStateSnapshot.h"
  17. #include "cmWorkingDirectory.h"
  18. #include "cmXMLSafe.h"
  19. #include "cmake.h"
  20. #if defined(__HAIKU__)
  21. #include <FindDirectory.h>
  22. #include <StorageDefs.h>
  23. #endif
  24. cmCPackGenerator::cmCPackGenerator()
  25. {
  26. this->GeneratorVerbose = cmSystemTools::OUTPUT_NONE;
  27. this->MakefileMap = nullptr;
  28. this->Logger = nullptr;
  29. this->componentPackageMethod = ONE_PACKAGE_PER_GROUP;
  30. }
  31. cmCPackGenerator::~cmCPackGenerator()
  32. {
  33. this->MakefileMap = nullptr;
  34. }
  35. void cmCPackGeneratorProgress(const char* msg, float prog, void* ptr)
  36. {
  37. cmCPackGenerator* self = static_cast<cmCPackGenerator*>(ptr);
  38. self->DisplayVerboseOutput(msg, prog);
  39. }
  40. void cmCPackGenerator::DisplayVerboseOutput(const char* msg, float progress)
  41. {
  42. (void)progress;
  43. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "" << msg << std::endl);
  44. }
  45. int cmCPackGenerator::PrepareNames()
  46. {
  47. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Create temp directory." << std::endl);
  48. // checks CPACK_SET_DESTDIR support
  49. if (IsOn("CPACK_SET_DESTDIR")) {
  50. if (SETDESTDIR_UNSUPPORTED == SupportsSetDestdir()) {
  51. cmCPackLogger(
  52. cmCPackLog::LOG_ERROR, "CPACK_SET_DESTDIR is set to ON but the '"
  53. << Name << "' generator does NOT support it." << std::endl);
  54. return 0;
  55. }
  56. if (SETDESTDIR_SHOULD_NOT_BE_USED == SupportsSetDestdir()) {
  57. cmCPackLogger(cmCPackLog::LOG_WARNING,
  58. "CPACK_SET_DESTDIR is set to ON but it is "
  59. << "usually a bad idea to do that with '" << Name
  60. << "' generator. Use at your own risk." << std::endl);
  61. }
  62. }
  63. std::string tempDirectory = this->GetOption("CPACK_PACKAGE_DIRECTORY");
  64. tempDirectory += "/_CPack_Packages/";
  65. const char* toplevelTag = this->GetOption("CPACK_TOPLEVEL_TAG");
  66. if (toplevelTag) {
  67. tempDirectory += toplevelTag;
  68. tempDirectory += "/";
  69. }
  70. tempDirectory += this->GetOption("CPACK_GENERATOR");
  71. std::string topDirectory = tempDirectory;
  72. const char* pfname = this->GetOption("CPACK_PACKAGE_FILE_NAME");
  73. if (!pfname) {
  74. cmCPackLogger(cmCPackLog::LOG_ERROR,
  75. "CPACK_PACKAGE_FILE_NAME not specified" << std::endl);
  76. return 0;
  77. }
  78. std::string outName = pfname;
  79. tempDirectory += "/" + outName;
  80. if (!this->GetOutputExtension()) {
  81. cmCPackLogger(cmCPackLog::LOG_ERROR, "No output extension specified"
  82. << std::endl);
  83. return 0;
  84. }
  85. outName += this->GetOutputExtension();
  86. const char* pdir = this->GetOption("CPACK_PACKAGE_DIRECTORY");
  87. if (!pdir) {
  88. cmCPackLogger(cmCPackLog::LOG_ERROR,
  89. "CPACK_PACKAGE_DIRECTORY not specified" << std::endl);
  90. return 0;
  91. }
  92. std::string destFile = pdir;
  93. this->SetOptionIfNotSet("CPACK_OUTPUT_FILE_PREFIX", destFile.c_str());
  94. destFile += "/" + outName;
  95. std::string outFile = topDirectory + "/" + outName;
  96. this->SetOptionIfNotSet("CPACK_TOPLEVEL_DIRECTORY", topDirectory.c_str());
  97. this->SetOptionIfNotSet("CPACK_TEMPORARY_DIRECTORY", tempDirectory.c_str());
  98. this->SetOptionIfNotSet("CPACK_OUTPUT_FILE_NAME", outName.c_str());
  99. this->SetOptionIfNotSet("CPACK_OUTPUT_FILE_PATH", destFile.c_str());
  100. this->SetOptionIfNotSet("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
  101. outFile.c_str());
  102. this->SetOptionIfNotSet("CPACK_INSTALL_DIRECTORY", this->GetInstallPath());
  103. this->SetOptionIfNotSet(
  104. "CPACK_NATIVE_INSTALL_DIRECTORY",
  105. cmsys::SystemTools::ConvertToOutputPath(this->GetInstallPath()).c_str());
  106. this->SetOptionIfNotSet("CPACK_TEMPORARY_INSTALL_DIRECTORY",
  107. tempDirectory.c_str());
  108. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  109. "Look for: CPACK_PACKAGE_DESCRIPTION_FILE" << std::endl);
  110. const char* descFileName = this->GetOption("CPACK_PACKAGE_DESCRIPTION_FILE");
  111. if (descFileName) {
  112. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Look for: " << descFileName
  113. << std::endl);
  114. if (!cmSystemTools::FileExists(descFileName)) {
  115. cmCPackLogger(cmCPackLog::LOG_ERROR,
  116. "Cannot find description file name: ["
  117. << descFileName << "]" << std::endl);
  118. return 0;
  119. }
  120. cmsys::ifstream ifs(descFileName);
  121. if (!ifs) {
  122. cmCPackLogger(cmCPackLog::LOG_ERROR,
  123. "Cannot open description file name: " << descFileName
  124. << std::endl);
  125. return 0;
  126. }
  127. std::ostringstream ostr;
  128. std::string line;
  129. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  130. "Read description file: " << descFileName << std::endl);
  131. while (ifs && cmSystemTools::GetLineFromStream(ifs, line)) {
  132. ostr << cmXMLSafe(line) << std::endl;
  133. }
  134. this->SetOptionIfNotSet("CPACK_PACKAGE_DESCRIPTION", ostr.str().c_str());
  135. }
  136. if (!this->GetOption("CPACK_PACKAGE_DESCRIPTION")) {
  137. cmCPackLogger(
  138. cmCPackLog::LOG_ERROR,
  139. "Project description not specified. Please specify "
  140. "CPACK_PACKAGE_DESCRIPTION or CPACK_PACKAGE_DESCRIPTION_FILE."
  141. << std::endl);
  142. return 0;
  143. }
  144. const char* algoSignature = this->GetOption("CPACK_PACKAGE_CHECKSUM");
  145. if (algoSignature) {
  146. if (!cmCryptoHash::New(algoSignature)) {
  147. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot recognize algorithm: "
  148. << algoSignature << std::endl);
  149. return 0;
  150. }
  151. }
  152. this->SetOptionIfNotSet("CPACK_REMOVE_TOPLEVEL_DIRECTORY", "1");
  153. return 1;
  154. }
  155. int cmCPackGenerator::InstallProject()
  156. {
  157. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "Install projects" << std::endl);
  158. this->CleanTemporaryDirectory();
  159. std::string bareTempInstallDirectory =
  160. this->GetOption("CPACK_TEMPORARY_INSTALL_DIRECTORY");
  161. std::string tempInstallDirectoryStr = bareTempInstallDirectory;
  162. bool setDestDir = cmSystemTools::IsOn(this->GetOption("CPACK_SET_DESTDIR")) |
  163. cmSystemTools::IsInternallyOn(this->GetOption("CPACK_SET_DESTDIR"));
  164. if (!setDestDir) {
  165. tempInstallDirectoryStr += this->GetPackagingInstallPrefix();
  166. }
  167. const char* tempInstallDirectory = tempInstallDirectoryStr.c_str();
  168. int res = 1;
  169. if (!cmsys::SystemTools::MakeDirectory(bareTempInstallDirectory.c_str())) {
  170. cmCPackLogger(cmCPackLog::LOG_ERROR,
  171. "Problem creating temporary directory: "
  172. << (tempInstallDirectory ? tempInstallDirectory : "(NULL)")
  173. << std::endl);
  174. return 0;
  175. }
  176. if (setDestDir) {
  177. std::string destDir = "DESTDIR=";
  178. destDir += tempInstallDirectory;
  179. cmSystemTools::PutEnv(destDir);
  180. } else {
  181. // Make sure there is no destdir
  182. cmSystemTools::PutEnv("DESTDIR=");
  183. }
  184. // If the CPackConfig file sets CPACK_INSTALL_COMMANDS then run them
  185. // as listed
  186. if (!this->InstallProjectViaInstallCommands(setDestDir,
  187. tempInstallDirectory)) {
  188. return 0;
  189. }
  190. // If the CPackConfig file sets CPACK_INSTALL_SCRIPT then run them
  191. // as listed
  192. if (!this->InstallProjectViaInstallScript(setDestDir,
  193. tempInstallDirectory)) {
  194. return 0;
  195. }
  196. // If the CPackConfig file sets CPACK_INSTALLED_DIRECTORIES
  197. // then glob it and copy it to CPACK_TEMPORARY_DIRECTORY
  198. // This is used in Source packaging
  199. if (!this->InstallProjectViaInstalledDirectories(setDestDir,
  200. tempInstallDirectory)) {
  201. return 0;
  202. }
  203. // If the project is a CMAKE project then run pre-install
  204. // and then read the cmake_install script to run it
  205. if (!this->InstallProjectViaInstallCMakeProjects(setDestDir,
  206. bareTempInstallDirectory)) {
  207. return 0;
  208. }
  209. if (setDestDir) {
  210. cmSystemTools::PutEnv("DESTDIR=");
  211. }
  212. return res;
  213. }
  214. int cmCPackGenerator::InstallProjectViaInstallCommands(
  215. bool setDestDir, const std::string& tempInstallDirectory)
  216. {
  217. (void)setDestDir;
  218. const char* installCommands = this->GetOption("CPACK_INSTALL_COMMANDS");
  219. if (installCommands && *installCommands) {
  220. std::string tempInstallDirectoryEnv = "CMAKE_INSTALL_PREFIX=";
  221. tempInstallDirectoryEnv += tempInstallDirectory;
  222. cmSystemTools::PutEnv(tempInstallDirectoryEnv);
  223. std::vector<std::string> installCommandsVector;
  224. cmSystemTools::ExpandListArgument(installCommands, installCommandsVector);
  225. for (std::string const& ic : installCommandsVector) {
  226. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Execute: " << ic << std::endl);
  227. std::string output;
  228. int retVal = 1;
  229. bool resB =
  230. cmSystemTools::RunSingleCommand(ic.c_str(), &output, &output, &retVal,
  231. nullptr, this->GeneratorVerbose, 0);
  232. if (!resB || retVal) {
  233. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  234. tmpFile += "/InstallOutput.log";
  235. cmGeneratedFileStream ofs(tmpFile.c_str());
  236. ofs << "# Run command: " << ic << std::endl
  237. << "# Output:" << std::endl
  238. << output << std::endl;
  239. cmCPackLogger(
  240. cmCPackLog::LOG_ERROR, "Problem running install command: "
  241. << ic << std::endl
  242. << "Please check " << tmpFile << " for errors" << std::endl);
  243. return 0;
  244. }
  245. }
  246. }
  247. return 1;
  248. }
  249. int cmCPackGenerator::InstallProjectViaInstalledDirectories(
  250. bool setDestDir, const std::string& tempInstallDirectory)
  251. {
  252. (void)setDestDir;
  253. (void)tempInstallDirectory;
  254. std::vector<cmsys::RegularExpression> ignoreFilesRegex;
  255. const char* cpackIgnoreFiles = this->GetOption("CPACK_IGNORE_FILES");
  256. if (cpackIgnoreFiles) {
  257. std::vector<std::string> ignoreFilesRegexString;
  258. cmSystemTools::ExpandListArgument(cpackIgnoreFiles,
  259. ignoreFilesRegexString);
  260. for (std::string const& ifr : ignoreFilesRegexString) {
  261. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  262. "Create ignore files regex for: " << ifr << std::endl);
  263. ignoreFilesRegex.push_back(ifr.c_str());
  264. }
  265. }
  266. const char* installDirectories =
  267. this->GetOption("CPACK_INSTALLED_DIRECTORIES");
  268. if (installDirectories && *installDirectories) {
  269. std::vector<std::string> installDirectoriesVector;
  270. cmSystemTools::ExpandListArgument(installDirectories,
  271. installDirectoriesVector);
  272. if (installDirectoriesVector.size() % 2 != 0) {
  273. cmCPackLogger(
  274. cmCPackLog::LOG_ERROR,
  275. "CPACK_INSTALLED_DIRECTORIES should contain pairs of <directory> and "
  276. "<subdirectory>. The <subdirectory> can be '.' to be installed in "
  277. "the toplevel directory of installation."
  278. << std::endl);
  279. return 0;
  280. }
  281. std::vector<std::string>::iterator it;
  282. const std::string& tempDir = tempInstallDirectory;
  283. for (it = installDirectoriesVector.begin();
  284. it != installDirectoriesVector.end(); ++it) {
  285. std::vector<std::pair<std::string, std::string>> symlinkedFiles;
  286. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Find files" << std::endl);
  287. cmsys::Glob gl;
  288. std::string top = *it;
  289. it++;
  290. std::string subdir = *it;
  291. std::string findExpr = top;
  292. findExpr += "/*";
  293. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  294. "- Install directory: " << top << std::endl);
  295. gl.RecurseOn();
  296. gl.SetRecurseListDirs(true);
  297. if (!gl.FindFiles(findExpr)) {
  298. cmCPackLogger(cmCPackLog::LOG_ERROR,
  299. "Cannot find any files in the installed directory"
  300. << std::endl);
  301. return 0;
  302. }
  303. files = gl.GetFiles();
  304. std::vector<std::string>::iterator gfit;
  305. std::vector<cmsys::RegularExpression>::iterator regIt;
  306. for (std::string const& gf : files) {
  307. bool skip = false;
  308. std::string inFile = gf;
  309. if (cmSystemTools::FileIsDirectory(gf)) {
  310. inFile += '/';
  311. }
  312. for (cmsys::RegularExpression& reg : ignoreFilesRegex) {
  313. if (reg.find(inFile.c_str())) {
  314. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  315. "Ignore file: " << inFile << std::endl);
  316. skip = true;
  317. }
  318. }
  319. if (skip) {
  320. continue;
  321. }
  322. std::string filePath = tempDir;
  323. filePath += "/" + subdir + "/" +
  324. cmSystemTools::RelativePath(top.c_str(), gf.c_str());
  325. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Copy file: "
  326. << inFile << " -> " << filePath << std::endl);
  327. /* If the file is a symlink we will have to re-create it */
  328. if (cmSystemTools::FileIsSymlink(inFile)) {
  329. std::string targetFile;
  330. std::string inFileRelative =
  331. cmSystemTools::RelativePath(top.c_str(), inFile.c_str());
  332. cmSystemTools::ReadSymlink(inFile, targetFile);
  333. symlinkedFiles.push_back(
  334. std::pair<std::string, std::string>(targetFile, inFileRelative));
  335. }
  336. /* If it is not a symlink then do a plain copy */
  337. else if (!(cmSystemTools::CopyFileIfDifferent(inFile.c_str(),
  338. filePath.c_str()) &&
  339. cmSystemTools::CopyFileTime(inFile.c_str(),
  340. filePath.c_str()))) {
  341. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem copying file: "
  342. << inFile << " -> " << filePath << std::endl);
  343. return 0;
  344. }
  345. }
  346. /* rebuild symlinks in the installed tree */
  347. if (!symlinkedFiles.empty()) {
  348. std::string curDir = cmSystemTools::GetCurrentWorkingDirectory();
  349. std::string goToDir = tempDir;
  350. goToDir += "/" + subdir;
  351. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Change dir to: " << goToDir
  352. << std::endl);
  353. cmWorkingDirectory workdir(goToDir);
  354. for (auto const& symlinked : symlinkedFiles) {
  355. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Will create a symlink: "
  356. << symlinked.second << "--> " << symlinked.first
  357. << std::endl);
  358. // make sure directory exists for symlink
  359. std::string destDir =
  360. cmSystemTools::GetFilenamePath(symlinked.second);
  361. if (!destDir.empty() && !cmSystemTools::MakeDirectory(destDir)) {
  362. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot create dir: "
  363. << destDir << "\nTrying to create symlink: "
  364. << symlinked.second << "--> " << symlinked.first
  365. << std::endl);
  366. }
  367. if (!cmSystemTools::CreateSymlink(symlinked.first,
  368. symlinked.second)) {
  369. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot create symlink: "
  370. << symlinked.second << "--> " << symlinked.first
  371. << std::endl);
  372. return 0;
  373. }
  374. }
  375. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Going back to: " << curDir
  376. << std::endl);
  377. }
  378. }
  379. }
  380. return 1;
  381. }
  382. int cmCPackGenerator::InstallProjectViaInstallScript(
  383. bool setDestDir, const std::string& tempInstallDirectory)
  384. {
  385. const char* cmakeScripts = this->GetOption("CPACK_INSTALL_SCRIPT");
  386. if (cmakeScripts && *cmakeScripts) {
  387. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "- Install scripts: " << cmakeScripts
  388. << std::endl);
  389. std::vector<std::string> cmakeScriptsVector;
  390. cmSystemTools::ExpandListArgument(cmakeScripts, cmakeScriptsVector);
  391. for (std::string const& installScript : cmakeScriptsVector) {
  392. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  393. "- Install script: " << installScript << std::endl);
  394. if (setDestDir) {
  395. // For DESTDIR based packaging, use the *project* CMAKE_INSTALL_PREFIX
  396. // underneath the tempInstallDirectory. The value of the project's
  397. // CMAKE_INSTALL_PREFIX is sent in here as the value of the
  398. // CPACK_INSTALL_PREFIX variable.
  399. std::string dir;
  400. if (this->GetOption("CPACK_INSTALL_PREFIX")) {
  401. dir += this->GetOption("CPACK_INSTALL_PREFIX");
  402. }
  403. this->SetOption("CMAKE_INSTALL_PREFIX", dir.c_str());
  404. cmCPackLogger(
  405. cmCPackLog::LOG_DEBUG,
  406. "- Using DESTDIR + CPACK_INSTALL_PREFIX... (this->SetOption)"
  407. << std::endl);
  408. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  409. "- Setting CMAKE_INSTALL_PREFIX to '" << dir << "'"
  410. << std::endl);
  411. } else {
  412. this->SetOption("CMAKE_INSTALL_PREFIX", tempInstallDirectory.c_str());
  413. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  414. "- Using non-DESTDIR install... (this->SetOption)"
  415. << std::endl);
  416. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  417. "- Setting CMAKE_INSTALL_PREFIX to '"
  418. << tempInstallDirectory << "'" << std::endl);
  419. }
  420. this->SetOptionIfNotSet("CMAKE_CURRENT_BINARY_DIR",
  421. tempInstallDirectory.c_str());
  422. this->SetOptionIfNotSet("CMAKE_CURRENT_SOURCE_DIR",
  423. tempInstallDirectory.c_str());
  424. int res = this->MakefileMap->ReadListFile(installScript.c_str());
  425. if (cmSystemTools::GetErrorOccuredFlag() || !res) {
  426. return 0;
  427. }
  428. }
  429. }
  430. return 1;
  431. }
  432. int cmCPackGenerator::InstallProjectViaInstallCMakeProjects(
  433. bool setDestDir, const std::string& baseTempInstallDirectory)
  434. {
  435. const char* cmakeProjects = this->GetOption("CPACK_INSTALL_CMAKE_PROJECTS");
  436. const char* cmakeGenerator = this->GetOption("CPACK_CMAKE_GENERATOR");
  437. std::string absoluteDestFiles;
  438. if (cmakeProjects && *cmakeProjects) {
  439. if (!cmakeGenerator) {
  440. cmCPackLogger(cmCPackLog::LOG_ERROR,
  441. "CPACK_INSTALL_CMAKE_PROJECTS is specified, but "
  442. "CPACK_CMAKE_GENERATOR is not. CPACK_CMAKE_GENERATOR "
  443. "is required to install the project."
  444. << std::endl);
  445. return 0;
  446. }
  447. std::vector<std::string> cmakeProjectsVector;
  448. cmSystemTools::ExpandListArgument(cmakeProjects, cmakeProjectsVector);
  449. std::vector<std::string>::iterator it;
  450. for (it = cmakeProjectsVector.begin(); it != cmakeProjectsVector.end();
  451. ++it) {
  452. if (it + 1 == cmakeProjectsVector.end() ||
  453. it + 2 == cmakeProjectsVector.end() ||
  454. it + 3 == cmakeProjectsVector.end()) {
  455. cmCPackLogger(
  456. cmCPackLog::LOG_ERROR,
  457. "Not enough items on list: CPACK_INSTALL_CMAKE_PROJECTS. "
  458. "CPACK_INSTALL_CMAKE_PROJECTS should hold quadruplet of install "
  459. "directory, install project name, install component, and install "
  460. "subdirectory."
  461. << std::endl);
  462. return 0;
  463. }
  464. std::string installDirectory = *it;
  465. ++it;
  466. std::string installProjectName = *it;
  467. ++it;
  468. std::string installComponent = *it;
  469. ++it;
  470. std::string installSubDirectory = *it;
  471. std::string installFile = installDirectory + "/cmake_install.cmake";
  472. std::vector<std::string> componentsVector;
  473. bool componentInstall = false;
  474. /*
  475. * We do a component install iff
  476. * - the CPack generator support component
  477. * - the user did not request Monolithic install
  478. * (this works at CPack time too)
  479. */
  480. if (this->SupportsComponentInstallation() &
  481. !(this->IsOn("CPACK_MONOLITHIC_INSTALL"))) {
  482. // Determine the installation types for this project (if provided).
  483. std::string installTypesVar = "CPACK_" +
  484. cmSystemTools::UpperCase(installComponent) + "_INSTALL_TYPES";
  485. const char* installTypes = this->GetOption(installTypesVar);
  486. if (installTypes && *installTypes) {
  487. std::vector<std::string> installTypesVector;
  488. cmSystemTools::ExpandListArgument(installTypes, installTypesVector);
  489. for (std::string const& installType : installTypesVector) {
  490. this->GetInstallationType(installProjectName, installType);
  491. }
  492. }
  493. // Determine the set of components that will be used in this project
  494. std::string componentsVar =
  495. "CPACK_COMPONENTS_" + cmSystemTools::UpperCase(installComponent);
  496. const char* components = this->GetOption(componentsVar);
  497. if (components && *components) {
  498. cmSystemTools::ExpandListArgument(components, componentsVector);
  499. for (std::string const& comp : componentsVector) {
  500. GetComponent(installProjectName, comp);
  501. }
  502. componentInstall = true;
  503. }
  504. }
  505. if (componentsVector.empty()) {
  506. componentsVector.push_back(installComponent);
  507. }
  508. const char* buildConfigCstr = this->GetOption("CPACK_BUILD_CONFIG");
  509. std::string buildConfig = buildConfigCstr ? buildConfigCstr : "";
  510. cmGlobalGenerator* globalGenerator =
  511. this->MakefileMap->GetCMakeInstance()->CreateGlobalGenerator(
  512. cmakeGenerator);
  513. if (!globalGenerator) {
  514. cmCPackLogger(cmCPackLog::LOG_ERROR,
  515. "Specified package generator not found. "
  516. "CPACK_CMAKE_GENERATOR value is invalid."
  517. << std::endl);
  518. return 0;
  519. }
  520. // set the global flag for unix style paths on cmSystemTools as
  521. // soon as the generator is set. This allows gmake to be used
  522. // on windows.
  523. cmSystemTools::SetForceUnixPaths(globalGenerator->GetForceUnixPaths());
  524. // Does this generator require pre-install?
  525. if (const char* preinstall =
  526. globalGenerator->GetPreinstallTargetName()) {
  527. std::string buildCommand = globalGenerator->GenerateCMakeBuildCommand(
  528. preinstall, buildConfig, "", false);
  529. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  530. "- Install command: " << buildCommand << std::endl);
  531. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "- Run preinstall target for: "
  532. << installProjectName << std::endl);
  533. std::string output;
  534. int retVal = 1;
  535. bool resB = cmSystemTools::RunSingleCommand(
  536. buildCommand.c_str(), &output, &output, &retVal,
  537. installDirectory.c_str(), this->GeneratorVerbose, 0);
  538. if (!resB || retVal) {
  539. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  540. tmpFile += "/PreinstallOutput.log";
  541. cmGeneratedFileStream ofs(tmpFile.c_str());
  542. ofs << "# Run command: " << buildCommand << std::endl
  543. << "# Directory: " << installDirectory << std::endl
  544. << "# Output:" << std::endl
  545. << output << std::endl;
  546. cmCPackLogger(
  547. cmCPackLog::LOG_ERROR, "Problem running install command: "
  548. << buildCommand << std::endl
  549. << "Please check " << tmpFile << " for errors" << std::endl);
  550. return 0;
  551. }
  552. }
  553. delete globalGenerator;
  554. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  555. "- Install project: " << installProjectName << std::endl);
  556. // Run the installation for each component
  557. for (std::string const& component : componentsVector) {
  558. std::string tempInstallDirectory = baseTempInstallDirectory;
  559. installComponent = component;
  560. if (componentInstall) {
  561. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "- Install component: "
  562. << installComponent << std::endl);
  563. }
  564. cmake cm(cmake::RoleScript);
  565. cm.SetHomeDirectory("");
  566. cm.SetHomeOutputDirectory("");
  567. cm.GetCurrentSnapshot().SetDefaultDefinitions();
  568. cm.AddCMakePaths();
  569. cm.SetProgressCallback(cmCPackGeneratorProgress, this);
  570. cmGlobalGenerator gg(&cm);
  571. cmMakefile mf(&gg, cm.GetCurrentSnapshot());
  572. if (!installSubDirectory.empty() && installSubDirectory != "/" &&
  573. installSubDirectory != ".") {
  574. tempInstallDirectory += installSubDirectory;
  575. }
  576. if (componentInstall) {
  577. tempInstallDirectory += "/";
  578. // Some CPack generators would rather chose
  579. // the local installation directory suffix.
  580. // Some (e.g. RPM) use
  581. // one install directory for each component **GROUP**
  582. // instead of the default
  583. // one install directory for each component.
  584. tempInstallDirectory +=
  585. GetComponentInstallDirNameSuffix(installComponent);
  586. if (this->IsOn("CPACK_COMPONENT_INCLUDE_TOPLEVEL_DIRECTORY")) {
  587. tempInstallDirectory += "/";
  588. tempInstallDirectory += this->GetOption("CPACK_PACKAGE_FILE_NAME");
  589. }
  590. }
  591. if (!setDestDir) {
  592. tempInstallDirectory += this->GetPackagingInstallPrefix();
  593. }
  594. if (setDestDir) {
  595. // For DESTDIR based packaging, use the *project*
  596. // CMAKE_INSTALL_PREFIX underneath the tempInstallDirectory. The
  597. // value of the project's CMAKE_INSTALL_PREFIX is sent in here as
  598. // the value of the CPACK_INSTALL_PREFIX variable.
  599. //
  600. // If DESTDIR has been 'internally set ON' this means that
  601. // the underlying CPack specific generator did ask for that
  602. // In this case we may override CPACK_INSTALL_PREFIX with
  603. // CPACK_PACKAGING_INSTALL_PREFIX
  604. // I know this is tricky and awkward but it's the price for
  605. // CPACK_SET_DESTDIR backward compatibility.
  606. if (cmSystemTools::IsInternallyOn(
  607. this->GetOption("CPACK_SET_DESTDIR"))) {
  608. this->SetOption("CPACK_INSTALL_PREFIX",
  609. this->GetOption("CPACK_PACKAGING_INSTALL_PREFIX"));
  610. }
  611. std::string dir;
  612. if (this->GetOption("CPACK_INSTALL_PREFIX")) {
  613. dir += this->GetOption("CPACK_INSTALL_PREFIX");
  614. }
  615. mf.AddDefinition("CMAKE_INSTALL_PREFIX", dir.c_str());
  616. cmCPackLogger(
  617. cmCPackLog::LOG_DEBUG,
  618. "- Using DESTDIR + CPACK_INSTALL_PREFIX... (mf.AddDefinition)"
  619. << std::endl);
  620. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  621. "- Setting CMAKE_INSTALL_PREFIX to '" << dir << "'"
  622. << std::endl);
  623. // Make sure that DESTDIR + CPACK_INSTALL_PREFIX directory
  624. // exists:
  625. //
  626. if (cmSystemTools::StringStartsWith(dir.c_str(), "/")) {
  627. dir = tempInstallDirectory + dir;
  628. } else {
  629. dir = tempInstallDirectory + "/" + dir;
  630. }
  631. /*
  632. * We must re-set DESTDIR for each component
  633. * We must not add the CPACK_INSTALL_PREFIX part because
  634. * it will be added using the override of CMAKE_INSTALL_PREFIX
  635. * The main reason for this awkward trick is that
  636. * are using DESTDIR for 2 different reasons:
  637. * - Because it was asked by the CPack Generator or the user
  638. * using CPACK_SET_DESTDIR
  639. * - Because it was already used for component install
  640. * in order to put things in subdirs...
  641. */
  642. cmSystemTools::PutEnv(std::string("DESTDIR=") +
  643. tempInstallDirectory);
  644. cmCPackLogger(cmCPackLog::LOG_DEBUG, "- Creating directory: '"
  645. << dir << "'" << std::endl);
  646. if (!cmsys::SystemTools::MakeDirectory(dir.c_str())) {
  647. cmCPackLogger(
  648. cmCPackLog::LOG_ERROR,
  649. "Problem creating temporary directory: " << dir << std::endl);
  650. return 0;
  651. }
  652. } else {
  653. mf.AddDefinition("CMAKE_INSTALL_PREFIX",
  654. tempInstallDirectory.c_str());
  655. if (!cmsys::SystemTools::MakeDirectory(
  656. tempInstallDirectory.c_str())) {
  657. cmCPackLogger(cmCPackLog::LOG_ERROR,
  658. "Problem creating temporary directory: "
  659. << tempInstallDirectory << std::endl);
  660. return 0;
  661. }
  662. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  663. "- Using non-DESTDIR install... (mf.AddDefinition)"
  664. << std::endl);
  665. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  666. "- Setting CMAKE_INSTALL_PREFIX to '"
  667. << tempInstallDirectory << "'" << std::endl);
  668. }
  669. if (!buildConfig.empty()) {
  670. mf.AddDefinition("BUILD_TYPE", buildConfig.c_str());
  671. }
  672. std::string installComponentLowerCase =
  673. cmSystemTools::LowerCase(installComponent);
  674. if (installComponentLowerCase != "all") {
  675. mf.AddDefinition("CMAKE_INSTALL_COMPONENT",
  676. installComponent.c_str());
  677. }
  678. // strip on TRUE, ON, 1, one or several file names, but not on
  679. // FALSE, OFF, 0 and an empty string
  680. if (!cmSystemTools::IsOff(this->GetOption("CPACK_STRIP_FILES"))) {
  681. mf.AddDefinition("CMAKE_INSTALL_DO_STRIP", "1");
  682. }
  683. // Remember the list of files before installation
  684. // of the current component (if we are in component install)
  685. const char* InstallPrefix = tempInstallDirectory.c_str();
  686. std::vector<std::string> filesBefore;
  687. std::string findExpr(InstallPrefix);
  688. if (componentInstall) {
  689. cmsys::Glob glB;
  690. findExpr += "/*";
  691. glB.RecurseOn();
  692. glB.SetRecurseListDirs(true);
  693. glB.FindFiles(findExpr);
  694. filesBefore = glB.GetFiles();
  695. std::sort(filesBefore.begin(), filesBefore.end());
  696. }
  697. // If CPack was asked to warn on ABSOLUTE INSTALL DESTINATION
  698. // then forward request to cmake_install.cmake script
  699. if (this->IsOn("CPACK_WARN_ON_ABSOLUTE_INSTALL_DESTINATION")) {
  700. mf.AddDefinition("CMAKE_WARN_ON_ABSOLUTE_INSTALL_DESTINATION", "1");
  701. }
  702. // If current CPack generator does support
  703. // ABSOLUTE INSTALL DESTINATION or CPack has been asked for
  704. // then ask cmake_install.cmake script to error out
  705. // as soon as it occurs (before installing file)
  706. if (!SupportsAbsoluteDestination() ||
  707. this->IsOn("CPACK_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION")) {
  708. mf.AddDefinition("CMAKE_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION", "1");
  709. }
  710. // do installation
  711. int res = mf.ReadListFile(installFile.c_str());
  712. // forward definition of CMAKE_ABSOLUTE_DESTINATION_FILES
  713. // to CPack (may be used by generators like CPack RPM or DEB)
  714. // in order to transparently handle ABSOLUTE PATH
  715. if (mf.GetDefinition("CMAKE_ABSOLUTE_DESTINATION_FILES")) {
  716. mf.AddDefinition(
  717. "CPACK_ABSOLUTE_DESTINATION_FILES",
  718. mf.GetDefinition("CMAKE_ABSOLUTE_DESTINATION_FILES"));
  719. }
  720. // Now rebuild the list of files after installation
  721. // of the current component (if we are in component install)
  722. if (componentInstall) {
  723. cmsys::Glob glA;
  724. glA.RecurseOn();
  725. glA.SetRecurseListDirs(true);
  726. glA.SetRecurseThroughSymlinks(false);
  727. glA.FindFiles(findExpr);
  728. std::vector<std::string> filesAfter = glA.GetFiles();
  729. std::sort(filesAfter.begin(), filesAfter.end());
  730. std::vector<std::string>::iterator diff;
  731. std::vector<std::string> result(filesAfter.size());
  732. diff = std::set_difference(filesAfter.begin(), filesAfter.end(),
  733. filesBefore.begin(), filesBefore.end(),
  734. result.begin());
  735. std::vector<std::string>::iterator fit;
  736. std::string localFileName;
  737. // Populate the File field of each component
  738. for (fit = result.begin(); fit != diff; ++fit) {
  739. localFileName =
  740. cmSystemTools::RelativePath(InstallPrefix, fit->c_str());
  741. localFileName =
  742. localFileName.substr(localFileName.find_first_not_of('/'));
  743. Components[installComponent].Files.push_back(localFileName);
  744. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Adding file <"
  745. << localFileName << "> to component <"
  746. << installComponent << ">" << std::endl);
  747. }
  748. }
  749. if (nullptr != mf.GetDefinition("CPACK_ABSOLUTE_DESTINATION_FILES")) {
  750. if (!absoluteDestFiles.empty()) {
  751. absoluteDestFiles += ";";
  752. }
  753. absoluteDestFiles +=
  754. mf.GetDefinition("CPACK_ABSOLUTE_DESTINATION_FILES");
  755. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  756. "Got some ABSOLUTE DESTINATION FILES: "
  757. << absoluteDestFiles << std::endl);
  758. // define component specific var
  759. if (componentInstall) {
  760. std::string absoluteDestFileComponent =
  761. std::string("CPACK_ABSOLUTE_DESTINATION_FILES") + "_" +
  762. GetComponentInstallDirNameSuffix(installComponent);
  763. if (nullptr != this->GetOption(absoluteDestFileComponent)) {
  764. std::string absoluteDestFilesListComponent =
  765. this->GetOption(absoluteDestFileComponent);
  766. absoluteDestFilesListComponent += ";";
  767. absoluteDestFilesListComponent +=
  768. mf.GetDefinition("CPACK_ABSOLUTE_DESTINATION_FILES");
  769. this->SetOption(absoluteDestFileComponent,
  770. absoluteDestFilesListComponent.c_str());
  771. } else {
  772. this->SetOption(
  773. absoluteDestFileComponent,
  774. mf.GetDefinition("CPACK_ABSOLUTE_DESTINATION_FILES"));
  775. }
  776. }
  777. }
  778. if (cmSystemTools::GetErrorOccuredFlag() || !res) {
  779. return 0;
  780. }
  781. }
  782. }
  783. }
  784. this->SetOption("CPACK_ABSOLUTE_DESTINATION_FILES",
  785. absoluteDestFiles.c_str());
  786. return 1;
  787. }
  788. bool cmCPackGenerator::ReadListFile(const char* moduleName)
  789. {
  790. bool retval;
  791. std::string fullPath = this->MakefileMap->GetModulesFile(moduleName);
  792. retval = this->MakefileMap->ReadListFile(fullPath.c_str());
  793. // include FATAL_ERROR and ERROR in the return status
  794. retval = retval && (!cmSystemTools::GetErrorOccuredFlag());
  795. return retval;
  796. }
  797. void cmCPackGenerator::SetOptionIfNotSet(const std::string& op,
  798. const char* value)
  799. {
  800. const char* def = this->MakefileMap->GetDefinition(op);
  801. if (def && *def) {
  802. return;
  803. }
  804. this->SetOption(op, value);
  805. }
  806. void cmCPackGenerator::SetOption(const std::string& op, const char* value)
  807. {
  808. if (!value) {
  809. this->MakefileMap->RemoveDefinition(op);
  810. return;
  811. }
  812. cmCPackLogger(cmCPackLog::LOG_DEBUG, this->GetNameOfClass()
  813. << "::SetOption(" << op << ", " << value << ")"
  814. << std::endl);
  815. this->MakefileMap->AddDefinition(op, value);
  816. }
  817. int cmCPackGenerator::DoPackage()
  818. {
  819. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "Create package using " << this->Name
  820. << std::endl);
  821. // Prepare CPack internal name and check
  822. // values for many CPACK_xxx vars
  823. if (!this->PrepareNames()) {
  824. return 0;
  825. }
  826. // Digest Component grouping specification
  827. if (!this->PrepareGroupingKind()) {
  828. return 0;
  829. }
  830. if (cmSystemTools::IsOn(
  831. this->GetOption("CPACK_REMOVE_TOPLEVEL_DIRECTORY"))) {
  832. const char* toplevelDirectory =
  833. this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  834. if (cmSystemTools::FileExists(toplevelDirectory)) {
  835. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Remove toplevel directory: "
  836. << toplevelDirectory << std::endl);
  837. if (!cmSystemTools::RepeatedRemoveDirectory(toplevelDirectory)) {
  838. cmCPackLogger(cmCPackLog::LOG_ERROR,
  839. "Problem removing toplevel directory: "
  840. << toplevelDirectory << std::endl);
  841. return 0;
  842. }
  843. }
  844. }
  845. cmCPackLogger(cmCPackLog::LOG_DEBUG, "About to install project "
  846. << std::endl);
  847. if (!this->InstallProject()) {
  848. return 0;
  849. }
  850. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Done install project " << std::endl);
  851. const char* tempPackageFileName =
  852. this->GetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME");
  853. const char* tempDirectory = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  854. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Find files" << std::endl);
  855. cmsys::Glob gl;
  856. std::string findExpr = tempDirectory;
  857. findExpr += "/*";
  858. gl.RecurseOn();
  859. gl.SetRecurseListDirs(true);
  860. gl.SetRecurseThroughSymlinks(false);
  861. if (!gl.FindFiles(findExpr)) {
  862. cmCPackLogger(cmCPackLog::LOG_ERROR,
  863. "Cannot find any files in the packaging tree" << std::endl);
  864. return 0;
  865. }
  866. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "Create package" << std::endl);
  867. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Package files to: "
  868. << (tempPackageFileName ? tempPackageFileName : "(NULL)")
  869. << std::endl);
  870. if (cmSystemTools::FileExists(tempPackageFileName)) {
  871. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Remove old package file"
  872. << std::endl);
  873. cmSystemTools::RemoveFile(tempPackageFileName);
  874. }
  875. if (cmSystemTools::IsOn(
  876. this->GetOption("CPACK_INCLUDE_TOPLEVEL_DIRECTORY"))) {
  877. tempDirectory = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  878. }
  879. // The files to be installed
  880. files = gl.GetFiles();
  881. packageFileNames.clear();
  882. /* Put at least one file name into the list of
  883. * wanted packageFileNames. The specific generator
  884. * may update this during PackageFiles.
  885. * (either putting several names or updating the provided one)
  886. */
  887. packageFileNames.push_back(tempPackageFileName ? tempPackageFileName : "");
  888. toplevel = tempDirectory;
  889. if (!this->PackageFiles() || cmSystemTools::GetErrorOccuredFlag()) {
  890. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem compressing the directory"
  891. << std::endl);
  892. return 0;
  893. }
  894. /* Prepare checksum algorithm*/
  895. const char* algo = this->GetOption("CPACK_PACKAGE_CHECKSUM");
  896. std::unique_ptr<cmCryptoHash> crypto = cmCryptoHash::New(algo ? algo : "");
  897. /*
  898. * Copy the generated packages to final destination
  899. * - there may be several of them
  900. * - the initially provided name may have changed
  901. * (because the specific generator did 'normalize' it)
  902. */
  903. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Copying final package(s) ["
  904. << packageFileNames.size() << "]:" << std::endl);
  905. /* now copy package one by one */
  906. for (std::string const& pkgFileName : packageFileNames) {
  907. std::string tmpPF(this->GetOption("CPACK_OUTPUT_FILE_PREFIX"));
  908. std::string filename(cmSystemTools::GetFilenameName(pkgFileName));
  909. tempPackageFileName = pkgFileName.c_str();
  910. tmpPF += "/" + filename;
  911. const char* packageFileName = tmpPF.c_str();
  912. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Copy final package(s): "
  913. << (tempPackageFileName ? tempPackageFileName : "(NULL)")
  914. << " to " << (packageFileName ? packageFileName : "(NULL)")
  915. << std::endl);
  916. if (!cmSystemTools::CopyFileIfDifferent(tempPackageFileName,
  917. packageFileName)) {
  918. cmCPackLogger(
  919. cmCPackLog::LOG_ERROR, "Problem copying the package: "
  920. << (tempPackageFileName ? tempPackageFileName : "(NULL)") << " to "
  921. << (packageFileName ? packageFileName : "(NULL)") << std::endl);
  922. return 0;
  923. }
  924. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "- package: "
  925. << packageFileName << " generated." << std::endl);
  926. /* Generate checksum file */
  927. if (crypto) {
  928. std::string hashFile(this->GetOption("CPACK_OUTPUT_FILE_PREFIX"));
  929. hashFile += "/" + filename;
  930. hashFile += "." + cmSystemTools::LowerCase(algo);
  931. cmsys::ofstream outF(hashFile.c_str());
  932. if (!outF) {
  933. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot create checksum file: "
  934. << hashFile << std::endl);
  935. return 0;
  936. }
  937. outF << crypto->HashFile(packageFileName) << " " << filename << "\n";
  938. cmCPackLogger(cmCPackLog::LOG_OUTPUT, "- checksum file: "
  939. << hashFile << " generated." << std::endl);
  940. }
  941. }
  942. return 1;
  943. }
  944. int cmCPackGenerator::Initialize(const std::string& name, cmMakefile* mf)
  945. {
  946. this->MakefileMap = mf;
  947. this->Name = name;
  948. // set the running generator name
  949. this->SetOption("CPACK_GENERATOR", this->Name.c_str());
  950. // Load the project specific config file
  951. const char* config = this->GetOption("CPACK_PROJECT_CONFIG_FILE");
  952. if (config) {
  953. mf->ReadListFile(config);
  954. }
  955. int result = this->InitializeInternal();
  956. if (cmSystemTools::GetErrorOccuredFlag()) {
  957. return 0;
  958. }
  959. // If a generator subclass did not already set this option in its
  960. // InitializeInternal implementation, and the project did not already set
  961. // it, the default value should be:
  962. this->SetOptionIfNotSet("CPACK_PACKAGING_INSTALL_PREFIX", "/");
  963. return result;
  964. }
  965. int cmCPackGenerator::InitializeInternal()
  966. {
  967. return 1;
  968. }
  969. bool cmCPackGenerator::IsSet(const std::string& name) const
  970. {
  971. return this->MakefileMap->IsSet(name);
  972. }
  973. bool cmCPackGenerator::IsOn(const std::string& name) const
  974. {
  975. return cmSystemTools::IsOn(GetOption(name));
  976. }
  977. bool cmCPackGenerator::IsSetToOff(const std::string& op) const
  978. {
  979. const char* ret = this->MakefileMap->GetDefinition(op);
  980. if (ret && *ret) {
  981. return cmSystemTools::IsOff(ret);
  982. }
  983. return false;
  984. }
  985. bool cmCPackGenerator::IsSetToEmpty(const std::string& op) const
  986. {
  987. const char* ret = this->MakefileMap->GetDefinition(op);
  988. if (ret) {
  989. return !*ret;
  990. }
  991. return false;
  992. }
  993. const char* cmCPackGenerator::GetOption(const std::string& op) const
  994. {
  995. const char* ret = this->MakefileMap->GetDefinition(op);
  996. if (!ret) {
  997. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  998. "Warning, GetOption return NULL for: " << op << std::endl);
  999. }
  1000. return ret;
  1001. }
  1002. std::vector<std::string> cmCPackGenerator::GetOptions() const
  1003. {
  1004. return this->MakefileMap->GetDefinitions();
  1005. }
  1006. int cmCPackGenerator::PackageFiles()
  1007. {
  1008. return 0;
  1009. }
  1010. const char* cmCPackGenerator::GetInstallPath()
  1011. {
  1012. if (!this->InstallPath.empty()) {
  1013. return this->InstallPath.c_str();
  1014. }
  1015. #if defined(_WIN32) && !defined(__CYGWIN__)
  1016. std::string prgfiles;
  1017. std::string sysDrive;
  1018. if (cmsys::SystemTools::GetEnv("ProgramFiles", prgfiles)) {
  1019. this->InstallPath = prgfiles;
  1020. } else if (cmsys::SystemTools::GetEnv("SystemDrive", sysDrive)) {
  1021. this->InstallPath = sysDrive;
  1022. this->InstallPath += "/Program Files";
  1023. } else {
  1024. this->InstallPath = "c:/Program Files";
  1025. }
  1026. this->InstallPath += "/";
  1027. this->InstallPath += this->GetOption("CPACK_PACKAGE_NAME");
  1028. this->InstallPath += "-";
  1029. this->InstallPath += this->GetOption("CPACK_PACKAGE_VERSION");
  1030. #elif defined(__HAIKU__)
  1031. char dir[B_PATH_NAME_LENGTH];
  1032. if (find_directory(B_SYSTEM_DIRECTORY, -1, false, dir, sizeof(dir)) ==
  1033. B_OK) {
  1034. this->InstallPath = dir;
  1035. } else {
  1036. this->InstallPath = "/boot/system";
  1037. }
  1038. #else
  1039. this->InstallPath = "/usr/local/";
  1040. #endif
  1041. return this->InstallPath.c_str();
  1042. }
  1043. const char* cmCPackGenerator::GetPackagingInstallPrefix()
  1044. {
  1045. cmCPackLogger(cmCPackLog::LOG_DEBUG, "GetPackagingInstallPrefix: '"
  1046. << this->GetOption("CPACK_PACKAGING_INSTALL_PREFIX") << "'"
  1047. << std::endl);
  1048. return this->GetOption("CPACK_PACKAGING_INSTALL_PREFIX");
  1049. }
  1050. std::string cmCPackGenerator::FindTemplate(const char* name)
  1051. {
  1052. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Look for template: "
  1053. << (name ? name : "(NULL)") << std::endl);
  1054. std::string ffile = this->MakefileMap->GetModulesFile(name);
  1055. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Found template: " << ffile
  1056. << std::endl);
  1057. return ffile;
  1058. }
  1059. bool cmCPackGenerator::ConfigureString(const std::string& inString,
  1060. std::string& outString)
  1061. {
  1062. this->MakefileMap->ConfigureString(inString, outString, true, false);
  1063. return true;
  1064. }
  1065. bool cmCPackGenerator::ConfigureFile(const char* inName, const char* outName,
  1066. bool copyOnly /* = false */)
  1067. {
  1068. return this->MakefileMap->ConfigureFile(inName, outName, copyOnly, true,
  1069. false) == 1;
  1070. }
  1071. int cmCPackGenerator::CleanTemporaryDirectory()
  1072. {
  1073. std::string tempInstallDirectoryWithPostfix =
  1074. this->GetOption("CPACK_TEMPORARY_INSTALL_DIRECTORY");
  1075. const char* tempInstallDirectory = tempInstallDirectoryWithPostfix.c_str();
  1076. if (cmsys::SystemTools::FileExists(tempInstallDirectory)) {
  1077. cmCPackLogger(cmCPackLog::LOG_OUTPUT,
  1078. "- Clean temporary : " << tempInstallDirectory << std::endl);
  1079. if (!cmSystemTools::RepeatedRemoveDirectory(tempInstallDirectory)) {
  1080. cmCPackLogger(cmCPackLog::LOG_ERROR,
  1081. "Problem removing temporary directory: "
  1082. << tempInstallDirectory << std::endl);
  1083. return 0;
  1084. }
  1085. }
  1086. return 1;
  1087. }
  1088. cmInstalledFile const* cmCPackGenerator::GetInstalledFile(
  1089. std::string const& name) const
  1090. {
  1091. cmake const* cm = this->MakefileMap->GetCMakeInstance();
  1092. return cm->GetInstalledFile(name);
  1093. }
  1094. int cmCPackGenerator::PrepareGroupingKind()
  1095. {
  1096. // find a component package method specified by the user
  1097. ComponentPackageMethod method = UNKNOWN_COMPONENT_PACKAGE_METHOD;
  1098. if (this->GetOption("CPACK_COMPONENTS_ALL_IN_ONE_PACKAGE")) {
  1099. method = ONE_PACKAGE;
  1100. }
  1101. if (this->GetOption("CPACK_COMPONENTS_IGNORE_GROUPS")) {
  1102. method = ONE_PACKAGE_PER_COMPONENT;
  1103. }
  1104. if (this->GetOption("CPACK_COMPONENTS_ONE_PACKAGE_PER_GROUP")) {
  1105. method = ONE_PACKAGE_PER_GROUP;
  1106. }
  1107. std::string groupingType;
  1108. // Second way to specify grouping
  1109. if (nullptr != this->GetOption("CPACK_COMPONENTS_GROUPING")) {
  1110. groupingType = this->GetOption("CPACK_COMPONENTS_GROUPING");
  1111. }
  1112. if (!groupingType.empty()) {
  1113. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "["
  1114. << this->Name << "]"
  1115. << " requested component grouping = " << groupingType
  1116. << std::endl);
  1117. if (groupingType == "ALL_COMPONENTS_IN_ONE") {
  1118. method = ONE_PACKAGE;
  1119. } else if (groupingType == "IGNORE") {
  1120. method = ONE_PACKAGE_PER_COMPONENT;
  1121. } else if (groupingType == "ONE_PER_GROUP") {
  1122. method = ONE_PACKAGE_PER_GROUP;
  1123. } else {
  1124. cmCPackLogger(
  1125. cmCPackLog::LOG_WARNING, "["
  1126. << this->Name << "]"
  1127. << " requested component grouping type <" << groupingType
  1128. << "> UNKNOWN not in (ALL_COMPONENTS_IN_ONE,IGNORE,ONE_PER_GROUP)"
  1129. << std::endl);
  1130. }
  1131. }
  1132. // Some components were defined but NO group
  1133. // fallback to default if not group based
  1134. if (method == ONE_PACKAGE_PER_GROUP && this->ComponentGroups.empty() &&
  1135. !this->Components.empty()) {
  1136. if (componentPackageMethod == ONE_PACKAGE) {
  1137. method = ONE_PACKAGE;
  1138. } else {
  1139. method = ONE_PACKAGE_PER_COMPONENT;
  1140. }
  1141. cmCPackLogger(
  1142. cmCPackLog::LOG_WARNING, "["
  1143. << this->Name << "]"
  1144. << " One package per component group requested, "
  1145. << "but NO component groups exist: Ignoring component group."
  1146. << std::endl);
  1147. }
  1148. // if user specified packaging method, override the default packaging method
  1149. if (method != UNKNOWN_COMPONENT_PACKAGE_METHOD) {
  1150. componentPackageMethod = method;
  1151. }
  1152. const char* method_names[] = { "ALL_COMPONENTS_IN_ONE", "IGNORE_GROUPS",
  1153. "ONE_PER_GROUP" };
  1154. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "["
  1155. << this->Name << "]"
  1156. << " requested component grouping = "
  1157. << method_names[componentPackageMethod] << std::endl);
  1158. return 1;
  1159. }
  1160. std::string cmCPackGenerator::GetComponentInstallDirNameSuffix(
  1161. const std::string& componentName)
  1162. {
  1163. return componentName;
  1164. }
  1165. std::string cmCPackGenerator::GetComponentPackageFileName(
  1166. const std::string& initialPackageFileName,
  1167. const std::string& groupOrComponentName, bool isGroupName)
  1168. {
  1169. /*
  1170. * the default behavior is to use the
  1171. * component [group] name as a suffix
  1172. */
  1173. std::string suffix = "-" + groupOrComponentName;
  1174. /* check if we should use DISPLAY name */
  1175. std::string dispNameVar = "CPACK_" + Name + "_USE_DISPLAY_NAME_IN_FILENAME";
  1176. if (IsOn(dispNameVar)) {
  1177. /* the component Group case */
  1178. if (isGroupName) {
  1179. std::string groupDispVar = "CPACK_COMPONENT_GROUP_" +
  1180. cmSystemTools::UpperCase(groupOrComponentName) + "_DISPLAY_NAME";
  1181. const char* groupDispName = GetOption(groupDispVar);
  1182. if (groupDispName) {
  1183. suffix = "-" + std::string(groupDispName);
  1184. }
  1185. }
  1186. /* the [single] component case */
  1187. else {
  1188. std::string dispVar = "CPACK_COMPONENT_" +
  1189. cmSystemTools::UpperCase(groupOrComponentName) + "_DISPLAY_NAME";
  1190. const char* dispName = GetOption(dispVar);
  1191. if (dispName) {
  1192. suffix = "-" + std::string(dispName);
  1193. }
  1194. }
  1195. }
  1196. return initialPackageFileName + suffix;
  1197. }
  1198. enum cmCPackGenerator::CPackSetDestdirSupport
  1199. cmCPackGenerator::SupportsSetDestdir() const
  1200. {
  1201. return cmCPackGenerator::SETDESTDIR_SUPPORTED;
  1202. }
  1203. bool cmCPackGenerator::SupportsAbsoluteDestination() const
  1204. {
  1205. return true;
  1206. }
  1207. bool cmCPackGenerator::SupportsComponentInstallation() const
  1208. {
  1209. return false;
  1210. }
  1211. bool cmCPackGenerator::WantsComponentInstallation() const
  1212. {
  1213. return (!IsOn("CPACK_MONOLITHIC_INSTALL") && SupportsComponentInstallation()
  1214. // check that we have at least one group or component
  1215. && (!this->ComponentGroups.empty() || !this->Components.empty()));
  1216. }
  1217. cmCPackInstallationType* cmCPackGenerator::GetInstallationType(
  1218. const std::string& projectName, const std::string& name)
  1219. {
  1220. (void)projectName;
  1221. bool hasInstallationType = this->InstallationTypes.count(name) != 0;
  1222. cmCPackInstallationType* installType = &this->InstallationTypes[name];
  1223. if (!hasInstallationType) {
  1224. // Define the installation type
  1225. std::string macroPrefix =
  1226. "CPACK_INSTALL_TYPE_" + cmsys::SystemTools::UpperCase(name);
  1227. installType->Name = name;
  1228. const char* displayName = this->GetOption(macroPrefix + "_DISPLAY_NAME");
  1229. if (displayName && *displayName) {
  1230. installType->DisplayName = displayName;
  1231. } else {
  1232. installType->DisplayName = installType->Name;
  1233. }
  1234. installType->Index = static_cast<unsigned>(this->InstallationTypes.size());
  1235. }
  1236. return installType;
  1237. }
  1238. cmCPackComponent* cmCPackGenerator::GetComponent(
  1239. const std::string& projectName, const std::string& name)
  1240. {
  1241. bool hasComponent = this->Components.count(name) != 0;
  1242. cmCPackComponent* component = &this->Components[name];
  1243. if (!hasComponent) {
  1244. // Define the component
  1245. std::string macroPrefix =
  1246. "CPACK_COMPONENT_" + cmsys::SystemTools::UpperCase(name);
  1247. component->Name = name;
  1248. const char* displayName = this->GetOption(macroPrefix + "_DISPLAY_NAME");
  1249. if (displayName && *displayName) {
  1250. component->DisplayName = displayName;
  1251. } else {
  1252. component->DisplayName = component->Name;
  1253. }
  1254. component->IsHidden = this->IsOn(macroPrefix + "_HIDDEN");
  1255. component->IsRequired = this->IsOn(macroPrefix + "_REQUIRED");
  1256. component->IsDisabledByDefault = this->IsOn(macroPrefix + "_DISABLED");
  1257. component->IsDownloaded = this->IsOn(macroPrefix + "_DOWNLOADED") ||
  1258. cmSystemTools::IsOn(this->GetOption("CPACK_DOWNLOAD_ALL"));
  1259. const char* archiveFile = this->GetOption(macroPrefix + "_ARCHIVE_FILE");
  1260. if (archiveFile && *archiveFile) {
  1261. component->ArchiveFile = archiveFile;
  1262. }
  1263. const char* plist = this->GetOption(macroPrefix + "_PLIST");
  1264. if (plist && *plist) {
  1265. component->Plist = plist;
  1266. }
  1267. const char* groupName = this->GetOption(macroPrefix + "_GROUP");
  1268. if (groupName && *groupName) {
  1269. component->Group = GetComponentGroup(projectName, groupName);
  1270. component->Group->Components.push_back(component);
  1271. } else {
  1272. component->Group = nullptr;
  1273. }
  1274. const char* description = this->GetOption(macroPrefix + "_DESCRIPTION");
  1275. if (description && *description) {
  1276. component->Description = description;
  1277. }
  1278. // Determine the installation types.
  1279. const char* installTypes = this->GetOption(macroPrefix + "_INSTALL_TYPES");
  1280. if (installTypes && *installTypes) {
  1281. std::vector<std::string> installTypesVector;
  1282. cmSystemTools::ExpandListArgument(installTypes, installTypesVector);
  1283. std::vector<std::string>::iterator installTypesIt;
  1284. for (std::string const& installType : installTypesVector) {
  1285. component->InstallationTypes.push_back(
  1286. this->GetInstallationType(projectName, installType));
  1287. }
  1288. }
  1289. // Determine the component dependencies.
  1290. const char* depends = this->GetOption(macroPrefix + "_DEPENDS");
  1291. if (depends && *depends) {
  1292. std::vector<std::string> dependsVector;
  1293. cmSystemTools::ExpandListArgument(depends, dependsVector);
  1294. std::vector<std::string>::iterator dependIt;
  1295. for (std::string const& depend : dependsVector) {
  1296. cmCPackComponent* child = GetComponent(projectName, depend);
  1297. component->Dependencies.push_back(child);
  1298. child->ReverseDependencies.push_back(component);
  1299. }
  1300. }
  1301. }
  1302. return component;
  1303. }
  1304. cmCPackComponentGroup* cmCPackGenerator::GetComponentGroup(
  1305. const std::string& projectName, const std::string& name)
  1306. {
  1307. (void)projectName;
  1308. std::string macroPrefix =
  1309. "CPACK_COMPONENT_GROUP_" + cmsys::SystemTools::UpperCase(name);
  1310. bool hasGroup = this->ComponentGroups.count(name) != 0;
  1311. cmCPackComponentGroup* group = &this->ComponentGroups[name];
  1312. if (!hasGroup) {
  1313. // Define the group
  1314. group->Name = name;
  1315. const char* displayName = this->GetOption(macroPrefix + "_DISPLAY_NAME");
  1316. if (displayName && *displayName) {
  1317. group->DisplayName = displayName;
  1318. } else {
  1319. group->DisplayName = group->Name;
  1320. }
  1321. const char* description = this->GetOption(macroPrefix + "_DESCRIPTION");
  1322. if (description && *description) {
  1323. group->Description = description;
  1324. }
  1325. group->IsBold = this->IsOn(macroPrefix + "_BOLD_TITLE");
  1326. group->IsExpandedByDefault = this->IsOn(macroPrefix + "_EXPANDED");
  1327. const char* parentGroupName =
  1328. this->GetOption(macroPrefix + "_PARENT_GROUP");
  1329. if (parentGroupName && *parentGroupName) {
  1330. group->ParentGroup = GetComponentGroup(projectName, parentGroupName);
  1331. group->ParentGroup->Subgroups.push_back(group);
  1332. } else {
  1333. group->ParentGroup = nullptr;
  1334. }
  1335. }
  1336. return group;
  1337. }