QCMake.cxx 10 KB

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