QCMake.cxx 8.2 KB

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