QCMake.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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<QCMakeProperty>();
  25. qRegisterMetaType<QCMakePropertyList>();
  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 = QDir::fromNativeSeparators(dir);
  73. emit this->sourceDirChanged(this->SourceDirectory);
  74. }
  75. }
  76. void QCMake::setBinaryDirectory(const QString& dir)
  77. {
  78. if(this->BinaryDirectory != dir)
  79. {
  80. this->BinaryDirectory = QDir::fromNativeSeparators(dir);
  81. emit this->binaryDirChanged(this->BinaryDirectory);
  82. cmCacheManager *cachem = this->CMakeInstance->GetCacheManager();
  83. this->setGenerator(QString());
  84. if(!this->CMakeInstance->GetCacheManager()->LoadCache(
  85. this->BinaryDirectory.toLocal8Bit().data()))
  86. {
  87. QDir testDir(this->BinaryDirectory);
  88. if(testDir.exists("CMakeCache.txt"))
  89. {
  90. cmSystemTools::Error("There is a CMakeCache.txt file for the current binary "
  91. "tree but cmake does not have permission to read it. "
  92. "Please check the permissions of the directory you are trying to run CMake on.");
  93. }
  94. }
  95. QCMakePropertyList props = this->properties();
  96. emit this->propertiesChanged(props);
  97. cmCacheManager::CacheIterator itm = cachem->NewIterator();
  98. if ( itm.Find("CMAKE_HOME_DIRECTORY"))
  99. {
  100. setSourceDirectory(itm.GetValue());
  101. }
  102. if ( itm.Find("CMAKE_GENERATOR"))
  103. {
  104. const char* extraGen = cachem->GetCacheValue("CMAKE_EXTRA_GENERATOR");
  105. std::string curGen = cmExternalMakefileProjectGenerator::
  106. CreateFullGeneratorName(itm.GetValue(), extraGen);
  107. this->setGenerator(curGen.c_str());
  108. }
  109. }
  110. }
  111. void QCMake::setGenerator(const QString& gen)
  112. {
  113. if(this->Generator != gen)
  114. {
  115. this->Generator = gen;
  116. emit this->generatorChanged(this->Generator);
  117. }
  118. }
  119. void QCMake::configure()
  120. {
  121. this->CMakeInstance->SetHomeDirectory(this->SourceDirectory.toAscii().data());
  122. this->CMakeInstance->SetStartDirectory(this->SourceDirectory.toAscii().data());
  123. this->CMakeInstance->SetHomeOutputDirectory(this->BinaryDirectory.toAscii().data());
  124. this->CMakeInstance->SetStartOutputDirectory(this->BinaryDirectory.toAscii().data());
  125. this->CMakeInstance->SetGlobalGenerator(
  126. this->CMakeInstance->CreateGlobalGenerator(this->Generator.toAscii().data()));
  127. this->CMakeInstance->LoadCache();
  128. this->CMakeInstance->SetSuppressDevWarnings(this->SuppressDevWarnings);
  129. this->CMakeInstance->PreLoadCMakeFiles();
  130. cmSystemTools::ResetErrorOccuredFlag();
  131. int err = this->CMakeInstance->Configure();
  132. emit this->propertiesChanged(this->properties());
  133. emit this->configureDone(err);
  134. }
  135. void QCMake::generate()
  136. {
  137. cmSystemTools::ResetErrorOccuredFlag();
  138. int err = this->CMakeInstance->Generate();
  139. emit this->generateDone(err);
  140. }
  141. void QCMake::setProperties(const QCMakePropertyList& newProps)
  142. {
  143. QCMakePropertyList props = newProps;
  144. QStringList toremove;
  145. // set the value of properties
  146. cmCacheManager *cachem = this->CMakeInstance->GetCacheManager();
  147. for(cmCacheManager::CacheIterator i = cachem->NewIterator();
  148. !i.IsAtEnd(); i.Next())
  149. {
  150. if(i.GetType() == cmCacheManager::INTERNAL ||
  151. i.GetType() == cmCacheManager::STATIC)
  152. {
  153. continue;
  154. }
  155. QCMakeProperty prop;
  156. prop.Key = i.GetName();
  157. int idx = props.indexOf(prop);
  158. if(idx == -1)
  159. {
  160. toremove.append(i.GetName());
  161. }
  162. else
  163. {
  164. prop = props[idx];
  165. if(prop.Value.type() == QVariant::Bool)
  166. {
  167. i.SetValue(prop.Value.toBool() ? "ON" : "OFF");
  168. }
  169. else
  170. {
  171. i.SetValue(prop.Value.toString().toAscii().data());
  172. }
  173. props.removeAt(idx);
  174. }
  175. }
  176. // remove some properites
  177. foreach(QString s, toremove)
  178. {
  179. cachem->RemoveCacheEntry(s.toAscii().data());
  180. }
  181. // add some new properites
  182. foreach(QCMakeProperty s, props)
  183. {
  184. if(s.Type == QCMakeProperty::BOOL)
  185. {
  186. this->CMakeInstance->AddCacheEntry(s.Key.toAscii().data(),
  187. s.Value.toBool() ? "ON" : "OFF",
  188. s.Help.toAscii().data(),
  189. cmCacheManager::BOOL);
  190. }
  191. else if(s.Type == QCMakeProperty::STRING)
  192. {
  193. this->CMakeInstance->AddCacheEntry(s.Key.toAscii().data(),
  194. s.Value.toString().toAscii().data(),
  195. s.Help.toAscii().data(),
  196. cmCacheManager::STRING);
  197. }
  198. else if(s.Type == QCMakeProperty::PATH)
  199. {
  200. this->CMakeInstance->AddCacheEntry(s.Key.toAscii().data(),
  201. s.Value.toString().toAscii().data(),
  202. s.Help.toAscii().data(),
  203. cmCacheManager::PATH);
  204. }
  205. else if(s.Type == QCMakeProperty::FILEPATH)
  206. {
  207. this->CMakeInstance->AddCacheEntry(s.Key.toAscii().data(),
  208. s.Value.toString().toAscii().data(),
  209. s.Help.toAscii().data(),
  210. cmCacheManager::FILEPATH);
  211. }
  212. }
  213. cachem->SaveCache(this->BinaryDirectory.toAscii().data());
  214. }
  215. QCMakePropertyList QCMake::properties() const
  216. {
  217. QCMakePropertyList ret;
  218. cmCacheManager *cachem = this->CMakeInstance->GetCacheManager();
  219. for(cmCacheManager::CacheIterator i = cachem->NewIterator();
  220. !i.IsAtEnd(); i.Next())
  221. {
  222. if(i.GetType() == cmCacheManager::INTERNAL ||
  223. i.GetType() == cmCacheManager::STATIC ||
  224. i.GetType() == cmCacheManager::UNINITIALIZED)
  225. {
  226. continue;
  227. }
  228. QCMakeProperty prop;
  229. prop.Key = i.GetName();
  230. prop.Help = i.GetProperty("HELPSTRING");
  231. prop.Value = i.GetValue();
  232. prop.Advanced = i.GetPropertyAsBool("ADVANCED");
  233. if(i.GetType() == cmCacheManager::BOOL)
  234. {
  235. prop.Type = QCMakeProperty::BOOL;
  236. prop.Value = cmSystemTools::IsOn(i.GetValue());
  237. }
  238. else if(i.GetType() == cmCacheManager::PATH)
  239. {
  240. prop.Type = QCMakeProperty::PATH;
  241. }
  242. else if(i.GetType() == cmCacheManager::FILEPATH)
  243. {
  244. prop.Type = QCMakeProperty::FILEPATH;
  245. }
  246. else if(i.GetType() == cmCacheManager::STRING)
  247. {
  248. prop.Type = QCMakeProperty::STRING;
  249. }
  250. ret.append(prop);
  251. }
  252. return ret;
  253. }
  254. void QCMake::interrupt()
  255. {
  256. cmSystemTools::SetFatalErrorOccured();
  257. }
  258. void QCMake::progressCallback(const char* msg, float percent, void* cd)
  259. {
  260. QCMake* self = reinterpret_cast<QCMake*>(cd);
  261. if(percent >= 0)
  262. {
  263. emit self->progressChanged(msg, percent);
  264. }
  265. else
  266. {
  267. emit self->outputMessage(msg);
  268. }
  269. QCoreApplication::processEvents();
  270. }
  271. void QCMake::errorCallback(const char* msg, const char* /*title*/,
  272. bool& /*stop*/, void* cd)
  273. {
  274. QCMake* self = reinterpret_cast<QCMake*>(cd);
  275. emit self->errorMessage(msg);
  276. QCoreApplication::processEvents();
  277. }
  278. QString QCMake::binaryDirectory() const
  279. {
  280. return this->BinaryDirectory;
  281. }
  282. QString QCMake::sourceDirectory() const
  283. {
  284. return this->SourceDirectory;
  285. }
  286. QString QCMake::generator() const
  287. {
  288. return this->Generator;
  289. }
  290. QStringList QCMake::availableGenerators() const
  291. {
  292. return this->AvailableGenerators;
  293. }
  294. void QCMake::deleteCache()
  295. {
  296. // delete cache
  297. this->CMakeInstance->GetCacheManager()->DeleteCache(this->BinaryDirectory.toAscii().data());
  298. // reload to make our cache empty
  299. this->CMakeInstance->GetCacheManager()->LoadCache(this->BinaryDirectory.toAscii().data());
  300. // emit no generator and no properties
  301. this->setGenerator(QString());
  302. QCMakePropertyList props = this->properties();
  303. emit this->propertiesChanged(props);
  304. }
  305. void QCMake::reloadCache()
  306. {
  307. // emit that the cache was cleaned out
  308. QCMakePropertyList props;
  309. emit this->propertiesChanged(props);
  310. // reload
  311. this->CMakeInstance->GetCacheManager()->LoadCache(this->BinaryDirectory.toAscii().data());
  312. // emit new cache properties
  313. props = this->properties();
  314. emit this->propertiesChanged(props);
  315. }
  316. void QCMake::setDebugOutput(bool flag)
  317. {
  318. if(flag != this->CMakeInstance->GetDebugOutput())
  319. {
  320. this->CMakeInstance->SetDebugOutputOn(flag);
  321. emit this->debugOutputChanged(flag);
  322. }
  323. }
  324. bool QCMake::getDebugOutput() const
  325. {
  326. return this->CMakeInstance->GetDebugOutput();
  327. }
  328. void QCMake::setSuppressDevWarnings(bool value)
  329. {
  330. this->SuppressDevWarnings = value;
  331. }