cmCPackIFWPackage.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCPackIFWPackage.h"
  11. #include "cmCPackIFWGenerator.h"
  12. #include <CPack/cmCPackLog.h>
  13. #include <cmGeneratedFileStream.h>
  14. #include <cmTimestamp.h>
  15. #include <cmXMLWriter.h>
  16. //----------------------------------------------------------------- Logger ---
  17. #ifdef cmCPackLogger
  18. # undef cmCPackLogger
  19. #endif
  20. #define cmCPackLogger(logType, msg) \
  21. do { \
  22. std::ostringstream cmCPackLog_msg; \
  23. cmCPackLog_msg << msg; \
  24. if(Generator) { \
  25. Generator->Logger->Log(logType, __FILE__, __LINE__, \
  26. cmCPackLog_msg.str().c_str()); \
  27. } \
  28. } while ( 0 )
  29. //---------------------------------------------------------- CompareStruct ---
  30. cmCPackIFWPackage::CompareStruct::CompareStruct() :
  31. Type(CompareNone)
  32. {
  33. }
  34. //------------------------------------------------------- DependenceStruct ---
  35. cmCPackIFWPackage::DependenceStruct::DependenceStruct()
  36. {
  37. }
  38. //----------------------------------------------------------------------------
  39. cmCPackIFWPackage::DependenceStruct::DependenceStruct(
  40. const std::string &dependence)
  41. {
  42. // Search compare section
  43. size_t pos = std::string::npos;
  44. if((pos = dependence.find("<=")) != std::string::npos)
  45. {
  46. Compare.Type = CompareLessOrEqual;
  47. Compare.Value = dependence.substr(pos + 2);
  48. }
  49. else if((pos = dependence.find(">=")) != std::string::npos)
  50. {
  51. Compare.Type = CompareGreaterOrEqual;
  52. Compare.Value = dependence.substr(pos + 2);
  53. }
  54. else if((pos = dependence.find("<")) != std::string::npos)
  55. {
  56. Compare.Type = CompareLess;
  57. Compare.Value = dependence.substr(pos + 1);
  58. }
  59. else if((pos = dependence.find("=")) != std::string::npos)
  60. {
  61. Compare.Type = CompareEqual;
  62. Compare.Value = dependence.substr(pos + 1);
  63. }
  64. else if((pos = dependence.find(">")) != std::string::npos)
  65. {
  66. Compare.Type = CompareGreater;
  67. Compare.Value = dependence.substr(pos + 1);
  68. }
  69. Name = pos == std::string::npos ? dependence : dependence.substr(0, pos);
  70. }
  71. //----------------------------------------------------------------------------
  72. std::string cmCPackIFWPackage::DependenceStruct::NameWithCompare() const
  73. {
  74. if (Compare.Type == CompareNone) return Name;
  75. std::string result = Name;
  76. if (Compare.Type == CompareLessOrEqual)
  77. {
  78. result += "<=";
  79. }
  80. else if (Compare.Type == CompareGreaterOrEqual)
  81. {
  82. result += ">=";
  83. }
  84. else if (Compare.Type == CompareLess)
  85. {
  86. result += "<";
  87. }
  88. else if (Compare.Type == CompareEqual)
  89. {
  90. result += "=";
  91. }
  92. else if (Compare.Type == CompareGreater)
  93. {
  94. result += ">";
  95. }
  96. result += Compare.Value;
  97. return result;
  98. }
  99. //------------------------------------------------------ cmCPackIFWPackage ---
  100. cmCPackIFWPackage::cmCPackIFWPackage() :
  101. Generator(0),
  102. Installer(0)
  103. {
  104. }
  105. //----------------------------------------------------------------------------
  106. const char *cmCPackIFWPackage::GetOption(const std::string &op) const
  107. {
  108. const char *option = Generator ? Generator->GetOption(op) : 0;
  109. return option && *option ? option : 0;
  110. }
  111. //----------------------------------------------------------------------------
  112. bool cmCPackIFWPackage::IsOn(const std::string &op) const
  113. {
  114. return Generator ? Generator->IsOn(op) : false;
  115. }
  116. //----------------------------------------------------------------------------
  117. bool cmCPackIFWPackage::IsVersionLess(const char *version)
  118. {
  119. return Generator ? Generator->IsVersionLess(version) : false;
  120. }
  121. //----------------------------------------------------------------------------
  122. bool cmCPackIFWPackage::IsVersionGreater(const char *version)
  123. {
  124. return Generator ? Generator->IsVersionGreater(version) : false;
  125. }
  126. //----------------------------------------------------------------------------
  127. bool cmCPackIFWPackage::IsVersionEqual(const char *version)
  128. {
  129. return Generator ? Generator->IsVersionEqual(version) : false;
  130. }
  131. //----------------------------------------------------------------------------
  132. std::string cmCPackIFWPackage::GetComponentName(cmCPackComponent *component)
  133. {
  134. if (!component) return "";
  135. const char* option = GetOption(
  136. "CPACK_IFW_COMPONENT_"
  137. + cmsys::SystemTools::UpperCase(component->Name)
  138. + "_NAME");
  139. return option ? option : component->Name;
  140. }
  141. //----------------------------------------------------------------------------
  142. void cmCPackIFWPackage::DefaultConfiguration()
  143. {
  144. DisplayName = "";
  145. Description = "";
  146. Version = "";
  147. ReleaseDate = "";
  148. Script = "";
  149. Licenses.clear();
  150. SortingPriority = "";
  151. Default = "";
  152. Virtual = "";
  153. ForcedInstallation = "";
  154. }
  155. //----------------------------------------------------------------------------
  156. // Defaul configuration (all in one package)
  157. int cmCPackIFWPackage::ConfigureFromOptions()
  158. {
  159. // Restore defaul configuration
  160. DefaultConfiguration();
  161. // Name
  162. Name = Generator->GetRootPackageName();
  163. // Display name
  164. if (const char *option = this->GetOption("CPACK_PACKAGE_NAME"))
  165. {
  166. DisplayName = option;
  167. }
  168. else
  169. {
  170. DisplayName = "Your package";
  171. }
  172. // Description
  173. if (const char* option =
  174. this->GetOption("CPACK_PACKAGE_DESCRIPTION_SUMMARY"))
  175. {
  176. Description = option;
  177. }
  178. else
  179. {
  180. Description = "Your package description";
  181. }
  182. // Version
  183. if(const char* option = GetOption("CPACK_PACKAGE_VERSION"))
  184. {
  185. Version = option;
  186. }
  187. else
  188. {
  189. Version = "1.0.0";
  190. }
  191. ForcedInstallation = "true";
  192. return 1;
  193. }
  194. //----------------------------------------------------------------------------
  195. int cmCPackIFWPackage::ConfigureFromComponent(cmCPackComponent *component)
  196. {
  197. if(!component) return 0;
  198. // Restore defaul configuration
  199. DefaultConfiguration();
  200. std::string prefix = "CPACK_IFW_COMPONENT_"
  201. + cmsys::SystemTools::UpperCase(component->Name)
  202. + "_";
  203. // Display name
  204. DisplayName = component->DisplayName;
  205. // Description
  206. Description = component->Description;
  207. // Version
  208. if(const char* optVERSION = GetOption(prefix + "VERSION"))
  209. {
  210. Version = optVERSION;
  211. }
  212. else if(const char* optPACKAGE_VERSION =
  213. GetOption("CPACK_PACKAGE_VERSION"))
  214. {
  215. Version = optPACKAGE_VERSION;
  216. }
  217. else
  218. {
  219. Version = "1.0.0";
  220. }
  221. // Script
  222. if (const char* option = GetOption(prefix + "SCRIPT"))
  223. {
  224. Script = option;
  225. }
  226. // CMake dependencies
  227. if (!component->Dependencies.empty())
  228. {
  229. std::vector<cmCPackComponent*>::iterator dit;
  230. for(dit = component->Dependencies.begin();
  231. dit != component->Dependencies.end();
  232. ++dit)
  233. {
  234. Dependencies.insert(Generator->ComponentPackages[*dit]);
  235. }
  236. }
  237. // QtIFW dependencies
  238. if(const char* option = this->GetOption(prefix + "DEPENDS"))
  239. {
  240. std::vector<std::string> deps;
  241. cmSystemTools::ExpandListArgument(option,
  242. deps);
  243. for(std::vector<std::string>::iterator
  244. dit = deps.begin(); dit != deps.end(); ++dit)
  245. {
  246. DependenceStruct dep(*dit);
  247. if (!Generator->Packages.count(dep.Name))
  248. {
  249. bool hasDep = Generator->DependentPackages.count(dep.Name) > 0;
  250. DependenceStruct &depRef =
  251. Generator->DependentPackages[dep.Name];
  252. if(!hasDep)
  253. {
  254. depRef = dep;
  255. }
  256. AlienDependencies.insert(&depRef);
  257. }
  258. }
  259. }
  260. // Licenses
  261. if (const char* option = this->GetOption(prefix + "LICENSES"))
  262. {
  263. Licenses.clear();
  264. cmSystemTools::ExpandListArgument( option, Licenses );
  265. if ( Licenses.size() % 2 != 0 )
  266. {
  267. cmCPackLogger(cmCPackLog::LOG_WARNING, prefix << "LICENSES"
  268. << " should contain pairs of <display_name> and <file_path>."
  269. << std::endl);
  270. Licenses.clear();
  271. }
  272. }
  273. // Priority
  274. if(const char* option = this->GetOption(prefix + "PRIORITY"))
  275. {
  276. SortingPriority = option;
  277. }
  278. // Default
  279. Default = component->IsDisabledByDefault ? "false" : "true";
  280. // Virtual
  281. Virtual = component->IsHidden ? "true" : "";
  282. // ForcedInstallation
  283. ForcedInstallation = component->IsRequired ? "true" : "false";
  284. return 1;
  285. }
  286. //----------------------------------------------------------------------------
  287. int
  288. cmCPackIFWPackage::ConfigureFromGroup(cmCPackComponentGroup *group)
  289. {
  290. if(!group) return 0;
  291. // Restore defaul configuration
  292. DefaultConfiguration();
  293. std::string prefix = "CPACK_IFW_COMPONENT_GROUP_"
  294. + cmsys::SystemTools::UpperCase(group->Name)
  295. + "_";
  296. DisplayName = group->DisplayName;
  297. Description = group->Description;
  298. // Version
  299. if(const char* optVERSION = GetOption(prefix + "VERSION"))
  300. {
  301. Version = optVERSION;
  302. }
  303. else if(const char* optPACKAGE_VERSION =
  304. GetOption("CPACK_PACKAGE_VERSION"))
  305. {
  306. Version = optPACKAGE_VERSION;
  307. }
  308. else
  309. {
  310. Version = "1.0.0";
  311. }
  312. // Script
  313. if (const char* option = GetOption(prefix + "SCRIPT"))
  314. {
  315. Script = option;
  316. }
  317. // Licenses
  318. if (const char* option = this->GetOption(prefix + "LICENSES"))
  319. {
  320. Licenses.clear();
  321. cmSystemTools::ExpandListArgument( option, Licenses );
  322. if ( Licenses.size() % 2 != 0 )
  323. {
  324. cmCPackLogger(cmCPackLog::LOG_WARNING, prefix << "LICENSES"
  325. << " should contain pairs of <display_name> and <file_path>."
  326. << std::endl);
  327. Licenses.clear();
  328. }
  329. }
  330. // Priority
  331. if(const char* option = this->GetOption(prefix + "PRIORITY"))
  332. {
  333. SortingPriority = option;
  334. }
  335. return 1;
  336. }
  337. //----------------------------------------------------------------------------
  338. int cmCPackIFWPackage::ConfigureFromGroup(const std::string &groupName)
  339. {
  340. // Group configuration
  341. cmCPackComponentGroup group;
  342. std::string prefix = "CPACK_COMPONENT_GROUP_"
  343. + cmsys::SystemTools::UpperCase(groupName)
  344. + "_";
  345. if (const char *option = GetOption(prefix + "DISPLAY_NAME"))
  346. {
  347. group.DisplayName = option;
  348. }
  349. else
  350. {
  351. group.DisplayName = group.Name;
  352. }
  353. if (const char* option = GetOption(prefix + "DESCRIPTION"))
  354. {
  355. group.Description = option;
  356. }
  357. group.IsBold = IsOn(prefix + "BOLD_TITLE");
  358. group.IsExpandedByDefault = IsOn(prefix + "EXPANDED");
  359. // Package configuration
  360. group.Name = groupName;
  361. if(Generator)
  362. {
  363. Name = Generator->GetGroupPackageName(&group);
  364. }
  365. else
  366. {
  367. Name = group.Name;
  368. }
  369. return ConfigureFromGroup(&group);
  370. }
  371. //----------------------------------------------------------------------------
  372. void cmCPackIFWPackage::GeneratePackageFile()
  373. {
  374. // Lazy directory initialization
  375. if (Directory.empty())
  376. {
  377. if(Installer)
  378. {
  379. Directory = Installer->Directory + "/packages/" + Name;
  380. }
  381. else if (Generator)
  382. {
  383. Directory = Generator->toplevel + "/packages/" + Name;
  384. }
  385. }
  386. // Output stream
  387. cmGeneratedFileStream fout((Directory + "/meta/package.xml").data());
  388. cmXMLWriter xout(fout);
  389. xout.StartDocument();
  390. WriteGeneratedByToStrim(xout);
  391. xout.StartElement("Package");
  392. xout.Element("DisplayName", DisplayName);
  393. xout.Element("Description", Description);
  394. xout.Element("Name", Name);
  395. xout.Element("Version", Version);
  396. if (!ReleaseDate.empty())
  397. {
  398. xout.Element("ReleaseDate", ReleaseDate);
  399. }
  400. else
  401. {
  402. xout.Element("ReleaseDate", cmTimestamp().CurrentTime("%Y-%m-%d", true));
  403. }
  404. // Script (copy to meta dir)
  405. if(!Script.empty())
  406. {
  407. std::string name = cmSystemTools::GetFilenameName(Script);
  408. std::string path = Directory + "/meta/" + name;
  409. cmsys::SystemTools::CopyFileIfDifferent(Script.data(), path.data());
  410. xout.Element("Script", name);
  411. }
  412. // Dependencies
  413. std::set<DependenceStruct> compDepSet;
  414. for(std::set<DependenceStruct*>::iterator ait = AlienDependencies.begin();
  415. ait != AlienDependencies.end(); ++ait)
  416. {
  417. compDepSet.insert(*(*ait));
  418. }
  419. for(std::set<cmCPackIFWPackage*>::iterator it = Dependencies.begin();
  420. it != Dependencies.end(); ++it)
  421. {
  422. compDepSet.insert(DependenceStruct((*it)->Name));
  423. }
  424. // Write dependencies
  425. if (!compDepSet.empty())
  426. {
  427. std::stringstream dependencies;
  428. std::set<DependenceStruct>::iterator it = compDepSet.begin();
  429. dependencies << it->NameWithCompare();
  430. ++it;
  431. while(it != compDepSet.end())
  432. {
  433. dependencies << "," << it->NameWithCompare();
  434. ++it;
  435. }
  436. xout.Element("Dependencies", dependencies.str());
  437. }
  438. // Licenses (copy to meta dir)
  439. std::vector<std::string> licenses = Licenses;
  440. for(size_t i = 1; i < licenses.size(); i += 2)
  441. {
  442. std::string name = cmSystemTools::GetFilenameName(licenses[i]);
  443. std::string path = Directory + "/meta/" + name;
  444. cmsys::SystemTools::CopyFileIfDifferent(licenses[i].data(), path.data());
  445. licenses[i] = name;
  446. }
  447. if(!licenses.empty())
  448. {
  449. xout.StartElement("Licenses");
  450. for(size_t i = 0; i < licenses.size(); i += 2)
  451. {
  452. xout.StartElement("License");
  453. xout.Attribute("name", licenses[i]);
  454. xout.Attribute("file", licenses[i + 1]);
  455. xout.EndElement();
  456. }
  457. xout.EndElement();
  458. }
  459. if (!ForcedInstallation.empty())
  460. {
  461. xout.Element("ForcedInstallation", ForcedInstallation);
  462. }
  463. if (!Virtual.empty())
  464. {
  465. xout.Element("Virtual", Virtual);
  466. }
  467. else if (!Default.empty())
  468. {
  469. xout.Element("Default", Default);
  470. }
  471. // Priority
  472. if(!SortingPriority.empty())
  473. {
  474. xout.Element("SortingPriority", SortingPriority);
  475. }
  476. xout.EndElement();
  477. xout.EndDocument();
  478. }
  479. void cmCPackIFWPackage::WriteGeneratedByToStrim(cmXMLWriter &xout)
  480. {
  481. if(Generator) Generator->WriteGeneratedByToStrim(xout);
  482. }