QCMake.cxx 10.0 KB

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