QCMake.cxx 10.0 KB

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