cmCPackGenerator.cxx 56 KB

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