QCMake.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 "QCMake.h"
  11. #include <QDir>
  12. #include <QCoreApplication>
  13. #include "cmake.h"
  14. #include "cmCacheManager.h"
  15. #include "cmSystemTools.h"
  16. #include "cmExternalMakefileProjectGenerator.h"
  17. QCMake::QCMake(QObject* p)
  18. : QObject(p)
  19. {
  20. this->SuppressDevWarnings = false;
  21. qRegisterMetaType<QCMakeProperty>();
  22. qRegisterMetaType<QCMakePropertyList>();
  23. QDir execDir(QCoreApplication::applicationDirPath());
  24. #if defined(Q_OS_MAC)
  25. if(execDir.exists("../bin/cmake"))
  26. {
  27. execDir.cd("../bin");
  28. }
  29. else
  30. {
  31. execDir.cd("../../../"); // path to cmake in build directory (need to fix for deployment)
  32. }
  33. #endif
  34. QString cmakeCommand = QString("cmake")+cmSystemTools::GetExecutableExtension();
  35. cmakeCommand = execDir.filePath(cmakeCommand);
  36. cmSystemTools::DisableRunCommandOutput();
  37. cmSystemTools::SetRunCommandHideConsole(true);
  38. cmSystemTools::SetErrorCallback(QCMake::errorCallback, this);
  39. cmSystemTools::FindExecutableDirectory(cmakeCommand.toAscii().data());
  40. this->CMakeInstance = new cmake;
  41. this->CMakeInstance->SetCMakeCommand(cmakeCommand.toAscii().data());
  42. #if defined(Q_OS_MAC)
  43. this->CMakeInstance->SetCMakeEditCommand("cmake-gui.app/Contents/MacOS/cmake-gui");
  44. #else
  45. this->CMakeInstance->SetCMakeEditCommand("cmake-gui");
  46. #endif
  47. this->CMakeInstance->SetProgressCallback(QCMake::progressCallback, this);
  48. std::vector<std::string> generators;
  49. this->CMakeInstance->GetRegisteredGenerators(generators);
  50. std::vector<std::string>::iterator iter;
  51. for(iter = generators.begin(); iter != generators.end(); ++iter)
  52. {
  53. // Skip the generator "KDevelop3", since there is also
  54. // "KDevelop3 - Unix Makefiles", which is the full and official name.
  55. // The short name is actually only still there since this was the name
  56. // in CMake 2.4, to keep "command line argument compatibility", but
  57. // this is not necessary in the GUI.
  58. if (*iter == "KDevelop3")
  59. {
  60. continue;
  61. }
  62. this->AvailableGenerators.append(iter->c_str());
  63. }
  64. }
  65. QCMake::~QCMake()
  66. {
  67. delete this->CMakeInstance;
  68. //cmDynamicLoader::FlushCache();
  69. }
  70. void QCMake::loadCache(const QString& dir)
  71. {
  72. this->setBinaryDirectory(dir);
  73. }
  74. void QCMake::setSourceDirectory(const QString& _dir)
  75. {
  76. QString dir =
  77. cmSystemTools::GetActualCaseForPath(_dir.toAscii().data()).c_str();
  78. if(this->SourceDirectory != dir)
  79. {
  80. this->SourceDirectory = QDir::fromNativeSeparators(dir);
  81. emit this->sourceDirChanged(this->SourceDirectory);
  82. }
  83. }
  84. void QCMake::setBinaryDirectory(const QString& _dir)
  85. {
  86. QString dir =
  87. cmSystemTools::GetActualCaseForPath(_dir.toAscii().data()).c_str();
  88. if(this->BinaryDirectory != dir)
  89. {
  90. this->BinaryDirectory = QDir::fromNativeSeparators(dir);
  91. emit this->binaryDirChanged(this->BinaryDirectory);
  92. cmCacheManager *cachem = this->CMakeInstance->GetCacheManager();
  93. this->setGenerator(QString());
  94. if(!this->CMakeInstance->GetCacheManager()->LoadCache(
  95. this->BinaryDirectory.toLocal8Bit().data()))
  96. {
  97. QDir testDir(this->BinaryDirectory);
  98. if(testDir.exists("CMakeCache.txt"))
  99. {
  100. cmSystemTools::Error("There is a CMakeCache.txt file for the current binary "
  101. "tree but cmake does not have permission to read it. "
  102. "Please check the permissions of the directory you are trying to run CMake on.");
  103. }
  104. }
  105. QCMakePropertyList props = this->properties();
  106. emit this->propertiesChanged(props);
  107. cmCacheManager::CacheIterator itm = cachem->NewIterator();
  108. if ( itm.Find("CMAKE_HOME_DIRECTORY"))
  109. {
  110. setSourceDirectory(itm.GetValue());
  111. }
  112. if ( itm.Find("CMAKE_GENERATOR"))
  113. {
  114. const char* extraGen = cachem->GetCacheValue("CMAKE_EXTRA_GENERATOR");
  115. std::string curGen = cmExternalMakefileProjectGenerator::
  116. CreateFullGeneratorName(itm.GetValue(), extraGen);
  117. this->setGenerator(curGen.c_str());
  118. }
  119. }
  120. }
  121. void QCMake::setGenerator(const QString& gen)
  122. {
  123. if(this->Generator != gen)
  124. {
  125. this->Generator = gen;
  126. emit this->generatorChanged(this->Generator);
  127. }
  128. }
  129. void QCMake::configure()
  130. {
  131. this->CMakeInstance->SetHomeDirectory(this->SourceDirectory.toAscii().data());
  132. this->CMakeInstance->SetStartDirectory(this->SourceDirectory.toAscii().data());
  133. this->CMakeInstance->SetHomeOutputDirectory(this->BinaryDirectory.toAscii().data());
  134. this->CMakeInstance->SetStartOutputDirectory(this->BinaryDirectory.toAscii().data());
  135. this->CMakeInstance->SetGlobalGenerator(
  136. this->CMakeInstance->CreateGlobalGenerator(this->Generator.toAscii().data()));
  137. this->CMakeInstance->LoadCache();
  138. this->CMakeInstance->SetSuppressDevWarnings(this->SuppressDevWarnings);
  139. this->CMakeInstance->PreLoadCMakeFiles();
  140. cmSystemTools::ResetErrorOccuredFlag();
  141. int err = this->CMakeInstance->Configure();
  142. emit this->propertiesChanged(this->properties());
  143. emit this->configureDone(err);
  144. }
  145. void QCMake::generate()
  146. {
  147. cmSystemTools::ResetErrorOccuredFlag();
  148. int err = this->CMakeInstance->Generate();
  149. emit this->generateDone(err);
  150. }
  151. void QCMake::setProperties(const QCMakePropertyList& newProps)
  152. {
  153. QCMakePropertyList props = newProps;
  154. QStringList toremove;
  155. // set the value of properties
  156. cmCacheManager *cachem = this->CMakeInstance->GetCacheManager();
  157. for(cmCacheManager::CacheIterator i = cachem->NewIterator();
  158. !i.IsAtEnd(); i.Next())
  159. {
  160. if(i.GetType() == cmCacheManager::INTERNAL ||
  161. i.GetType() == cmCacheManager::STATIC)
  162. {
  163. continue;
  164. }
  165. QCMakeProperty prop;
  166. prop.Key = i.GetName();
  167. int idx = props.indexOf(prop);
  168. if(idx == -1)
  169. {
  170. toremove.append(i.GetName());
  171. }
  172. else
  173. {
  174. prop = props[idx];
  175. if(prop.Value.type() == QVariant::Bool)
  176. {
  177. i.SetValue(prop.Value.toBool() ? "ON" : "OFF");
  178. }
  179. else
  180. {
  181. i.SetValue(prop.Value.toString().toAscii().data());
  182. }
  183. props.removeAt(idx);
  184. }
  185. }
  186. // remove some properites
  187. foreach(QString s, toremove)
  188. {
  189. cachem->RemoveCacheEntry(s.toAscii().data());
  190. }
  191. // add some new properites
  192. foreach(QCMakeProperty s, props)
  193. {
  194. if(s.Type == QCMakeProperty::BOOL)
  195. {
  196. this->CMakeInstance->AddCacheEntry(s.Key.toAscii().data(),
  197. s.Value.toBool() ? "ON" : "OFF",
  198. s.Help.toAscii().data(),
  199. cmCacheManager::BOOL);
  200. }
  201. else if(s.Type == QCMakeProperty::STRING)
  202. {
  203. this->CMakeInstance->AddCacheEntry(s.Key.toAscii().data(),
  204. s.Value.toString().toAscii().data(),
  205. s.Help.toAscii().data(),
  206. cmCacheManager::STRING);
  207. }
  208. else if(s.Type == QCMakeProperty::PATH)
  209. {
  210. this->CMakeInstance->AddCacheEntry(s.Key.toAscii().data(),
  211. s.Value.toString().toAscii().data(),
  212. s.Help.toAscii().data(),
  213. cmCacheManager::PATH);
  214. }
  215. else if(s.Type == QCMakeProperty::FILEPATH)
  216. {
  217. this->CMakeInstance->AddCacheEntry(s.Key.toAscii().data(),
  218. s.Value.toString().toAscii().data(),
  219. s.Help.toAscii().data(),
  220. cmCacheManager::FILEPATH);
  221. }
  222. }
  223. cachem->SaveCache(this->BinaryDirectory.toAscii().data());
  224. }
  225. QCMakePropertyList QCMake::properties() const
  226. {
  227. QCMakePropertyList ret;
  228. cmCacheManager *cachem = this->CMakeInstance->GetCacheManager();
  229. for(cmCacheManager::CacheIterator i = cachem->NewIterator();
  230. !i.IsAtEnd(); i.Next())
  231. {
  232. if(i.GetType() == cmCacheManager::INTERNAL ||
  233. i.GetType() == cmCacheManager::STATIC ||
  234. i.GetType() == cmCacheManager::UNINITIALIZED)
  235. {
  236. continue;
  237. }
  238. QCMakeProperty prop;
  239. prop.Key = i.GetName();
  240. prop.Help = i.GetProperty("HELPSTRING");
  241. prop.Value = i.GetValue();
  242. prop.Advanced = i.GetPropertyAsBool("ADVANCED");
  243. if(i.GetType() == cmCacheManager::BOOL)
  244. {
  245. prop.Type = QCMakeProperty::BOOL;
  246. prop.Value = cmSystemTools::IsOn(i.GetValue());
  247. }
  248. else if(i.GetType() == cmCacheManager::PATH)
  249. {
  250. prop.Type = QCMakeProperty::PATH;
  251. }
  252. else if(i.GetType() == cmCacheManager::FILEPATH)
  253. {
  254. prop.Type = QCMakeProperty::FILEPATH;
  255. }
  256. else if(i.GetType() == cmCacheManager::STRING)
  257. {
  258. prop.Type = QCMakeProperty::STRING;
  259. if (i.PropertyExists("STRINGS"))
  260. {
  261. prop.Strings = QString(i.GetProperty("STRINGS")).split(";");
  262. }
  263. }
  264. ret.append(prop);
  265. }
  266. return ret;
  267. }
  268. void QCMake::interrupt()
  269. {
  270. cmSystemTools::SetFatalErrorOccured();
  271. }
  272. void QCMake::progressCallback(const char* msg, float percent, void* cd)
  273. {
  274. QCMake* self = reinterpret_cast<QCMake*>(cd);
  275. if(percent >= 0)
  276. {
  277. emit self->progressChanged(msg, percent);
  278. }
  279. else
  280. {
  281. emit self->outputMessage(msg);
  282. }
  283. QCoreApplication::processEvents();
  284. }
  285. void QCMake::errorCallback(const char* msg, const char* /*title*/,
  286. bool& /*stop*/, void* cd)
  287. {
  288. QCMake* self = reinterpret_cast<QCMake*>(cd);
  289. emit self->errorMessage(msg);
  290. QCoreApplication::processEvents();
  291. }
  292. QString QCMake::binaryDirectory() const
  293. {
  294. return this->BinaryDirectory;
  295. }
  296. QString QCMake::sourceDirectory() const
  297. {
  298. return this->SourceDirectory;
  299. }
  300. QString QCMake::generator() const
  301. {
  302. return this->Generator;
  303. }
  304. QStringList QCMake::availableGenerators() const
  305. {
  306. return this->AvailableGenerators;
  307. }
  308. void QCMake::deleteCache()
  309. {
  310. // delete cache
  311. this->CMakeInstance->GetCacheManager()->DeleteCache(this->BinaryDirectory.toAscii().data());
  312. // reload to make our cache empty
  313. this->CMakeInstance->GetCacheManager()->LoadCache(this->BinaryDirectory.toAscii().data());
  314. // emit no generator and no properties
  315. this->setGenerator(QString());
  316. QCMakePropertyList props = this->properties();
  317. emit this->propertiesChanged(props);
  318. }
  319. void QCMake::reloadCache()
  320. {
  321. // emit that the cache was cleaned out
  322. QCMakePropertyList props;
  323. emit this->propertiesChanged(props);
  324. // reload
  325. this->CMakeInstance->GetCacheManager()->LoadCache(this->BinaryDirectory.toAscii().data());
  326. // emit new cache properties
  327. props = this->properties();
  328. emit this->propertiesChanged(props);
  329. }
  330. void QCMake::setDebugOutput(bool flag)
  331. {
  332. if(flag != this->CMakeInstance->GetDebugOutput())
  333. {
  334. this->CMakeInstance->SetDebugOutputOn(flag);
  335. emit this->debugOutputChanged(flag);
  336. }
  337. }
  338. bool QCMake::getDebugOutput() const
  339. {
  340. return this->CMakeInstance->GetDebugOutput();
  341. }
  342. void QCMake::setSuppressDevWarnings(bool value)
  343. {
  344. this->SuppressDevWarnings = value;
  345. }