QCMake.cxx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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(iter->c_str());
  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(curGen.c_str());
  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& newProps)
  128. {
  129. QCMakeCachePropertyList props = newProps;
  130. QStringList toremove;
  131. // set the value of properties
  132. cmCacheManager *cachem = this->CMakeInstance->GetCacheManager();
  133. for(cmCacheManager::CacheIterator i = cachem->NewIterator();
  134. !i.IsAtEnd(); i.Next())
  135. {
  136. if(i.GetType() == cmCacheManager::INTERNAL ||
  137. i.GetType() == cmCacheManager::STATIC)
  138. {
  139. continue;
  140. }
  141. QCMakeCacheProperty prop;
  142. prop.Key = i.GetName();
  143. int idx = props.indexOf(prop);
  144. if(idx == -1)
  145. {
  146. toremove.append(i.GetName());
  147. }
  148. else
  149. {
  150. prop = props[idx];
  151. if(prop.Value.type() == QVariant::Bool)
  152. {
  153. i.SetValue(prop.Value.toBool() ? "ON" : "OFF");
  154. }
  155. else
  156. {
  157. i.SetValue(prop.Value.toString().toAscii().data());
  158. }
  159. props.removeAt(idx);
  160. }
  161. }
  162. // remove some properites
  163. foreach(QString s, toremove)
  164. {
  165. cachem->RemoveCacheEntry(s.toAscii().data());
  166. }
  167. // add some new properites
  168. foreach(QCMakeCacheProperty s, props)
  169. {
  170. if(s.Type == QCMakeCacheProperty::BOOL)
  171. {
  172. this->CMakeInstance->AddCacheEntry(s.Key.toAscii().data(),
  173. s.Value.toBool() ? "ON" : "OFF",
  174. s.Help.toAscii().data(),
  175. cmCacheManager::BOOL);
  176. }
  177. else if(s.Type == QCMakeCacheProperty::STRING)
  178. {
  179. this->CMakeInstance->AddCacheEntry(s.Key.toAscii().data(),
  180. s.Value.toString().toAscii().data(),
  181. s.Help.toAscii().data(),
  182. cmCacheManager::STRING);
  183. }
  184. else if(s.Type == QCMakeCacheProperty::PATH)
  185. {
  186. this->CMakeInstance->AddCacheEntry(s.Key.toAscii().data(),
  187. s.Value.toString().toAscii().data(),
  188. s.Help.toAscii().data(),
  189. cmCacheManager::PATH);
  190. }
  191. else if(s.Type == QCMakeCacheProperty::FILEPATH)
  192. {
  193. this->CMakeInstance->AddCacheEntry(s.Key.toAscii().data(),
  194. s.Value.toString().toAscii().data(),
  195. s.Help.toAscii().data(),
  196. cmCacheManager::FILEPATH);
  197. }
  198. }
  199. cachem->SaveCache(this->BinaryDirectory.toAscii().data());
  200. }
  201. QCMakeCachePropertyList QCMake::properties() const
  202. {
  203. QCMakeCachePropertyList ret;
  204. cmCacheManager *cachem = this->CMakeInstance->GetCacheManager();
  205. for(cmCacheManager::CacheIterator i = cachem->NewIterator();
  206. !i.IsAtEnd(); i.Next())
  207. {
  208. if(i.GetType() == cmCacheManager::INTERNAL ||
  209. i.GetType() == cmCacheManager::STATIC)
  210. {
  211. continue;
  212. }
  213. QCMakeCacheProperty prop;
  214. prop.Key = i.GetName();
  215. prop.Help = i.GetProperty("HELPSTRING");
  216. prop.Value = i.GetValue();
  217. prop.Advanced = i.GetPropertyAsBool("ADVANCED");
  218. if(i.GetType() == cmCacheManager::BOOL)
  219. {
  220. prop.Type = QCMakeCacheProperty::BOOL;
  221. prop.Value = cmSystemTools::IsOn(i.GetValue());
  222. }
  223. else if(i.GetType() == cmCacheManager::PATH)
  224. {
  225. prop.Type = QCMakeCacheProperty::PATH;
  226. }
  227. else if(i.GetType() == cmCacheManager::FILEPATH)
  228. {
  229. prop.Type = QCMakeCacheProperty::FILEPATH;
  230. }
  231. else if(i.GetType() == cmCacheManager::STRING)
  232. {
  233. prop.Type = QCMakeCacheProperty::STRING;
  234. }
  235. ret.append(prop);
  236. }
  237. return ret;
  238. }
  239. void QCMake::interrupt()
  240. {
  241. cmSystemTools::SetFatalErrorOccured();
  242. }
  243. void QCMake::progressCallback(const char* msg, float percent, void* cd)
  244. {
  245. QCMake* self = reinterpret_cast<QCMake*>(cd);
  246. if(percent >= 0)
  247. {
  248. emit self->progressChanged(msg, percent);
  249. }
  250. else
  251. {
  252. emit self->outputMessage(msg);
  253. }
  254. }
  255. void QCMake::errorCallback(const char* msg, const char* /*title*/,
  256. bool& /*stop*/, void* cd)
  257. {
  258. QCMake* self = reinterpret_cast<QCMake*>(cd);
  259. emit self->errorMessage(msg);
  260. }
  261. QString QCMake::binaryDirectory() const
  262. {
  263. return this->BinaryDirectory;
  264. }
  265. QString QCMake::sourceDirectory() const
  266. {
  267. return this->SourceDirectory;
  268. }
  269. QString QCMake::generator() const
  270. {
  271. return this->Generator;
  272. }
  273. QStringList QCMake::availableGenerators() const
  274. {
  275. return this->AvailableGenerators;
  276. }
  277. void QCMake::deleteCache()
  278. {
  279. // delete cache
  280. this->CMakeInstance->GetCacheManager()->DeleteCache(this->BinaryDirectory.toAscii().data());
  281. // reload to make our cache empty
  282. this->CMakeInstance->GetCacheManager()->LoadCache(this->BinaryDirectory.toAscii().data());
  283. // emit no generator and no properties
  284. this->setGenerator(QString());
  285. QCMakeCachePropertyList props = this->properties();
  286. emit this->propertiesChanged(props);
  287. }
  288. void QCMake::reloadCache()
  289. {
  290. // emit that the cache was cleaned out
  291. QCMakeCachePropertyList props;
  292. emit this->propertiesChanged(props);
  293. // reload
  294. this->CMakeInstance->GetCacheManager()->LoadCache(this->BinaryDirectory.toAscii().data());
  295. // emit new cache properties
  296. props = this->properties();
  297. emit this->propertiesChanged(props);
  298. }