cmCPackIFWPackage.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. cmCPackIFWPackage::DependenceStruct::DependenceStruct(
  39. const std::string& dependence)
  40. {
  41. // Search compare section
  42. size_t pos = std::string::npos;
  43. if ((pos = dependence.find("<=")) != std::string::npos) {
  44. Compare.Type = CompareLessOrEqual;
  45. Compare.Value = dependence.substr(pos + 2);
  46. } else if ((pos = dependence.find(">=")) != std::string::npos) {
  47. Compare.Type = CompareGreaterOrEqual;
  48. Compare.Value = dependence.substr(pos + 2);
  49. } else if ((pos = dependence.find("<")) != std::string::npos) {
  50. Compare.Type = CompareLess;
  51. Compare.Value = dependence.substr(pos + 1);
  52. } else if ((pos = dependence.find("=")) != std::string::npos) {
  53. Compare.Type = CompareEqual;
  54. Compare.Value = dependence.substr(pos + 1);
  55. } else if ((pos = dependence.find(">")) != std::string::npos) {
  56. Compare.Type = CompareGreater;
  57. Compare.Value = dependence.substr(pos + 1);
  58. }
  59. Name = pos == std::string::npos ? dependence : dependence.substr(0, pos);
  60. }
  61. std::string cmCPackIFWPackage::DependenceStruct::NameWithCompare() const
  62. {
  63. if (Compare.Type == CompareNone)
  64. return Name;
  65. std::string result = Name;
  66. if (Compare.Type == CompareLessOrEqual) {
  67. result += "<=";
  68. } else if (Compare.Type == CompareGreaterOrEqual) {
  69. result += ">=";
  70. } else if (Compare.Type == CompareLess) {
  71. result += "<";
  72. } else if (Compare.Type == CompareEqual) {
  73. result += "=";
  74. } else if (Compare.Type == CompareGreater) {
  75. result += ">";
  76. }
  77. result += Compare.Value;
  78. return result;
  79. }
  80. //------------------------------------------------------ cmCPackIFWPackage ---
  81. cmCPackIFWPackage::cmCPackIFWPackage()
  82. : Generator(0)
  83. , Installer(0)
  84. {
  85. }
  86. const char* cmCPackIFWPackage::GetOption(const std::string& op) const
  87. {
  88. const char* option = Generator ? Generator->GetOption(op) : 0;
  89. return option && *option ? option : 0;
  90. }
  91. bool cmCPackIFWPackage::IsOn(const std::string& op) const
  92. {
  93. return Generator ? Generator->IsOn(op) : false;
  94. }
  95. bool cmCPackIFWPackage::IsVersionLess(const char* version)
  96. {
  97. return Generator ? Generator->IsVersionLess(version) : false;
  98. }
  99. bool cmCPackIFWPackage::IsVersionGreater(const char* version)
  100. {
  101. return Generator ? Generator->IsVersionGreater(version) : false;
  102. }
  103. bool cmCPackIFWPackage::IsVersionEqual(const char* version)
  104. {
  105. return Generator ? Generator->IsVersionEqual(version) : false;
  106. }
  107. std::string cmCPackIFWPackage::GetComponentName(cmCPackComponent* component)
  108. {
  109. if (!component)
  110. return "";
  111. const char* option =
  112. GetOption("CPACK_IFW_COMPONENT_" +
  113. cmsys::SystemTools::UpperCase(component->Name) + "_NAME");
  114. return option ? option : component->Name;
  115. }
  116. void cmCPackIFWPackage::DefaultConfiguration()
  117. {
  118. DisplayName = "";
  119. Description = "";
  120. Version = "";
  121. ReleaseDate = "";
  122. Script = "";
  123. Licenses.clear();
  124. SortingPriority = "";
  125. Default = "";
  126. Virtual = "";
  127. ForcedInstallation = "";
  128. }
  129. // Defaul configuration (all in one package)
  130. int cmCPackIFWPackage::ConfigureFromOptions()
  131. {
  132. // Restore defaul configuration
  133. DefaultConfiguration();
  134. // Name
  135. Name = Generator->GetRootPackageName();
  136. // Display name
  137. if (const char* option = this->GetOption("CPACK_PACKAGE_NAME")) {
  138. DisplayName = option;
  139. } else {
  140. DisplayName = "Your package";
  141. }
  142. // Description
  143. if (const char* option =
  144. this->GetOption("CPACK_PACKAGE_DESCRIPTION_SUMMARY")) {
  145. Description = option;
  146. } else {
  147. Description = "Your package description";
  148. }
  149. // Version
  150. if (const char* option = GetOption("CPACK_PACKAGE_VERSION")) {
  151. Version = option;
  152. } else {
  153. Version = "1.0.0";
  154. }
  155. ForcedInstallation = "true";
  156. return 1;
  157. }
  158. int cmCPackIFWPackage::ConfigureFromComponent(cmCPackComponent* component)
  159. {
  160. if (!component)
  161. return 0;
  162. // Restore defaul configuration
  163. DefaultConfiguration();
  164. std::string prefix = "CPACK_IFW_COMPONENT_" +
  165. cmsys::SystemTools::UpperCase(component->Name) + "_";
  166. // Display name
  167. DisplayName = component->DisplayName;
  168. // Description
  169. Description = component->Description;
  170. // Version
  171. if (const char* optVERSION = GetOption(prefix + "VERSION")) {
  172. Version = optVERSION;
  173. } else if (const char* optPACKAGE_VERSION =
  174. GetOption("CPACK_PACKAGE_VERSION")) {
  175. Version = optPACKAGE_VERSION;
  176. } else {
  177. Version = "1.0.0";
  178. }
  179. // Script
  180. if (const char* option = GetOption(prefix + "SCRIPT")) {
  181. Script = option;
  182. }
  183. // CMake dependencies
  184. if (!component->Dependencies.empty()) {
  185. std::vector<cmCPackComponent*>::iterator dit;
  186. for (dit = component->Dependencies.begin();
  187. dit != component->Dependencies.end(); ++dit) {
  188. Dependencies.insert(Generator->ComponentPackages[*dit]);
  189. }
  190. }
  191. // QtIFW dependencies
  192. if (const char* option = this->GetOption(prefix + "DEPENDS")) {
  193. std::vector<std::string> deps;
  194. cmSystemTools::ExpandListArgument(option, deps);
  195. for (std::vector<std::string>::iterator dit = deps.begin();
  196. dit != deps.end(); ++dit) {
  197. DependenceStruct dep(*dit);
  198. if (!Generator->Packages.count(dep.Name)) {
  199. bool hasDep = Generator->DependentPackages.count(dep.Name) > 0;
  200. DependenceStruct& depRef = Generator->DependentPackages[dep.Name];
  201. if (!hasDep) {
  202. depRef = dep;
  203. }
  204. AlienDependencies.insert(&depRef);
  205. }
  206. }
  207. }
  208. // Licenses
  209. if (const char* option = this->GetOption(prefix + "LICENSES")) {
  210. Licenses.clear();
  211. cmSystemTools::ExpandListArgument(option, Licenses);
  212. if (Licenses.size() % 2 != 0) {
  213. cmCPackLogger(
  214. cmCPackLog::LOG_WARNING, prefix
  215. << "LICENSES"
  216. << " should contain pairs of <display_name> and <file_path>."
  217. << std::endl);
  218. Licenses.clear();
  219. }
  220. }
  221. // Priority
  222. if (const char* option = this->GetOption(prefix + "PRIORITY")) {
  223. SortingPriority = option;
  224. }
  225. // Default
  226. Default = component->IsDisabledByDefault ? "false" : "true";
  227. // Virtual
  228. Virtual = component->IsHidden ? "true" : "";
  229. // ForcedInstallation
  230. ForcedInstallation = component->IsRequired ? "true" : "false";
  231. return 1;
  232. }
  233. int cmCPackIFWPackage::ConfigureFromGroup(cmCPackComponentGroup* group)
  234. {
  235. if (!group)
  236. return 0;
  237. // Restore defaul configuration
  238. DefaultConfiguration();
  239. std::string prefix = "CPACK_IFW_COMPONENT_GROUP_" +
  240. cmsys::SystemTools::UpperCase(group->Name) + "_";
  241. DisplayName = group->DisplayName;
  242. Description = group->Description;
  243. // Version
  244. if (const char* optVERSION = GetOption(prefix + "VERSION")) {
  245. Version = optVERSION;
  246. } else if (const char* optPACKAGE_VERSION =
  247. GetOption("CPACK_PACKAGE_VERSION")) {
  248. Version = optPACKAGE_VERSION;
  249. } else {
  250. Version = "1.0.0";
  251. }
  252. // Script
  253. if (const char* option = GetOption(prefix + "SCRIPT")) {
  254. Script = option;
  255. }
  256. // Licenses
  257. if (const char* option = this->GetOption(prefix + "LICENSES")) {
  258. Licenses.clear();
  259. cmSystemTools::ExpandListArgument(option, Licenses);
  260. if (Licenses.size() % 2 != 0) {
  261. cmCPackLogger(
  262. cmCPackLog::LOG_WARNING, prefix
  263. << "LICENSES"
  264. << " should contain pairs of <display_name> and <file_path>."
  265. << std::endl);
  266. Licenses.clear();
  267. }
  268. }
  269. // Priority
  270. if (const char* option = this->GetOption(prefix + "PRIORITY")) {
  271. SortingPriority = option;
  272. }
  273. return 1;
  274. }
  275. int cmCPackIFWPackage::ConfigureFromGroup(const std::string& groupName)
  276. {
  277. // Group configuration
  278. cmCPackComponentGroup group;
  279. std::string prefix =
  280. "CPACK_COMPONENT_GROUP_" + cmsys::SystemTools::UpperCase(groupName) + "_";
  281. if (const char* option = GetOption(prefix + "DISPLAY_NAME")) {
  282. group.DisplayName = option;
  283. } else {
  284. group.DisplayName = group.Name;
  285. }
  286. if (const char* option = GetOption(prefix + "DESCRIPTION")) {
  287. group.Description = option;
  288. }
  289. group.IsBold = IsOn(prefix + "BOLD_TITLE");
  290. group.IsExpandedByDefault = IsOn(prefix + "EXPANDED");
  291. // Package configuration
  292. group.Name = groupName;
  293. if (Generator) {
  294. Name = Generator->GetGroupPackageName(&group);
  295. } else {
  296. Name = group.Name;
  297. }
  298. return ConfigureFromGroup(&group);
  299. }
  300. void cmCPackIFWPackage::GeneratePackageFile()
  301. {
  302. // Lazy directory initialization
  303. if (Directory.empty()) {
  304. if (Installer) {
  305. Directory = Installer->Directory + "/packages/" + Name;
  306. } else if (Generator) {
  307. Directory = Generator->toplevel + "/packages/" + Name;
  308. }
  309. }
  310. // Output stream
  311. cmGeneratedFileStream fout((Directory + "/meta/package.xml").data());
  312. cmXMLWriter xout(fout);
  313. xout.StartDocument();
  314. WriteGeneratedByToStrim(xout);
  315. xout.StartElement("Package");
  316. xout.Element("DisplayName", DisplayName);
  317. xout.Element("Description", Description);
  318. xout.Element("Name", Name);
  319. xout.Element("Version", Version);
  320. if (!ReleaseDate.empty()) {
  321. xout.Element("ReleaseDate", ReleaseDate);
  322. } else {
  323. xout.Element("ReleaseDate", cmTimestamp().CurrentTime("%Y-%m-%d", true));
  324. }
  325. // Script (copy to meta dir)
  326. if (!Script.empty()) {
  327. std::string name = cmSystemTools::GetFilenameName(Script);
  328. std::string path = Directory + "/meta/" + name;
  329. cmsys::SystemTools::CopyFileIfDifferent(Script.data(), path.data());
  330. xout.Element("Script", name);
  331. }
  332. // Dependencies
  333. std::set<DependenceStruct> compDepSet;
  334. for (std::set<DependenceStruct*>::iterator ait = AlienDependencies.begin();
  335. ait != AlienDependencies.end(); ++ait) {
  336. compDepSet.insert(*(*ait));
  337. }
  338. for (std::set<cmCPackIFWPackage*>::iterator it = Dependencies.begin();
  339. it != Dependencies.end(); ++it) {
  340. compDepSet.insert(DependenceStruct((*it)->Name));
  341. }
  342. // Write dependencies
  343. if (!compDepSet.empty()) {
  344. std::stringstream dependencies;
  345. std::set<DependenceStruct>::iterator it = compDepSet.begin();
  346. dependencies << it->NameWithCompare();
  347. ++it;
  348. while (it != compDepSet.end()) {
  349. dependencies << "," << it->NameWithCompare();
  350. ++it;
  351. }
  352. xout.Element("Dependencies", dependencies.str());
  353. }
  354. // Licenses (copy to meta dir)
  355. std::vector<std::string> licenses = Licenses;
  356. for (size_t i = 1; i < licenses.size(); i += 2) {
  357. std::string name = cmSystemTools::GetFilenameName(licenses[i]);
  358. std::string path = Directory + "/meta/" + name;
  359. cmsys::SystemTools::CopyFileIfDifferent(licenses[i].data(), path.data());
  360. licenses[i] = name;
  361. }
  362. if (!licenses.empty()) {
  363. xout.StartElement("Licenses");
  364. for (size_t i = 0; i < licenses.size(); i += 2) {
  365. xout.StartElement("License");
  366. xout.Attribute("name", licenses[i]);
  367. xout.Attribute("file", licenses[i + 1]);
  368. xout.EndElement();
  369. }
  370. xout.EndElement();
  371. }
  372. if (!ForcedInstallation.empty()) {
  373. xout.Element("ForcedInstallation", ForcedInstallation);
  374. }
  375. if (!Virtual.empty()) {
  376. xout.Element("Virtual", Virtual);
  377. } else if (!Default.empty()) {
  378. xout.Element("Default", Default);
  379. }
  380. // Priority
  381. if (!SortingPriority.empty()) {
  382. xout.Element("SortingPriority", SortingPriority);
  383. }
  384. xout.EndElement();
  385. xout.EndDocument();
  386. }
  387. void cmCPackIFWPackage::WriteGeneratedByToStrim(cmXMLWriter& xout)
  388. {
  389. if (Generator)
  390. Generator->WriteGeneratedByToStrim(xout);
  391. }