QCMake.cxx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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->SetCMakeEditCommand("cmake-gui");
  40. this->CMakeInstance->SetProgressCallback(QCMake::progressCallback, this);
  41. std::vector<std::string> generators;
  42. this->CMakeInstance->GetRegisteredGenerators(generators);
  43. std::vector<std::string>::iterator iter;
  44. for(iter = generators.begin(); iter != generators.end(); ++iter)
  45. {
  46. this->AvailableGenerators.append(iter->c_str());
  47. }
  48. }
  49. QCMake::~QCMake()
  50. {
  51. delete this->CMakeInstance;
  52. //cmDynamicLoader::FlushCache();
  53. }
  54. void QCMake::loadCache(const QString& dir)
  55. {
  56. this->setBinaryDirectory(dir);
  57. }
  58. void QCMake::setSourceDirectory(const QString& dir)
  59. {
  60. if(this->SourceDirectory != dir)
  61. {
  62. this->SourceDirectory = dir;
  63. emit this->sourceDirChanged(dir);
  64. }
  65. }
  66. void QCMake::setBinaryDirectory(const QString& dir)
  67. {
  68. if(this->BinaryDirectory != dir)
  69. {
  70. cmCacheManager *cachem = this->CMakeInstance->GetCacheManager();
  71. this->BinaryDirectory = dir;
  72. this->setGenerator(QString());
  73. if(!this->CMakeInstance->GetCacheManager()->LoadCache(dir.toLocal8Bit().data()))
  74. {
  75. QDir testDir(dir);
  76. if(testDir.exists("CMakeCache.txt"))
  77. {
  78. cmSystemTools::Error("There is a CMakeCache.txt file for the current binary "
  79. "tree but cmake does not have permission to read it. "
  80. "Please check the permissions of the directory you are trying to run CMake on.");
  81. }
  82. }
  83. QCMakeCachePropertyList props = this->properties();
  84. emit this->propertiesChanged(props);
  85. cmCacheManager::CacheIterator itm = cachem->NewIterator();
  86. if ( itm.Find("CMAKE_HOME_DIRECTORY"))
  87. {
  88. setSourceDirectory(itm.GetValue());
  89. }
  90. if ( itm.Find("CMAKE_GENERATOR"))
  91. {
  92. const char* extraGen = cachem->GetCacheValue("CMAKE_EXTRA_GENERATOR");
  93. std::string curGen = cmExternalMakefileProjectGenerator::
  94. CreateFullGeneratorName(itm.GetValue(), extraGen);
  95. this->setGenerator(curGen.c_str());
  96. }
  97. }
  98. }
  99. void QCMake::setGenerator(const QString& gen)
  100. {
  101. if(this->Generator != gen)
  102. {
  103. this->Generator = gen;
  104. emit this->generatorChanged(this->Generator);
  105. }
  106. }
  107. void QCMake::configure()
  108. {
  109. this->CMakeInstance->SetHomeDirectory(this->SourceDirectory.toAscii().data());
  110. this->CMakeInstance->SetStartDirectory(this->SourceDirectory.toAscii().data());
  111. this->CMakeInstance->SetHomeOutputDirectory(this->BinaryDirectory.toAscii().data());
  112. this->CMakeInstance->SetStartOutputDirectory(this->BinaryDirectory.toAscii().data());
  113. this->CMakeInstance->SetGlobalGenerator(
  114. this->CMakeInstance->CreateGlobalGenerator(this->Generator.toAscii().data()));
  115. this->CMakeInstance->SetCMakeCommand(this->CMakeExecutable.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. {
  213. continue;
  214. }
  215. QCMakeCacheProperty prop;
  216. prop.Key = i.GetName();
  217. prop.Help = i.GetProperty("HELPSTRING");
  218. prop.Value = i.GetValue();
  219. prop.Advanced = i.GetPropertyAsBool("ADVANCED");
  220. if(i.GetType() == cmCacheManager::BOOL)
  221. {
  222. prop.Type = QCMakeCacheProperty::BOOL;
  223. prop.Value = cmSystemTools::IsOn(i.GetValue());
  224. }
  225. else if(i.GetType() == cmCacheManager::PATH)
  226. {
  227. prop.Type = QCMakeCacheProperty::PATH;
  228. }
  229. else if(i.GetType() == cmCacheManager::FILEPATH)
  230. {
  231. prop.Type = QCMakeCacheProperty::FILEPATH;
  232. }
  233. else if(i.GetType() == cmCacheManager::STRING)
  234. {
  235. prop.Type = QCMakeCacheProperty::STRING;
  236. }
  237. ret.append(prop);
  238. }
  239. return ret;
  240. }
  241. void QCMake::interrupt()
  242. {
  243. cmSystemTools::SetFatalErrorOccured();
  244. }
  245. void QCMake::progressCallback(const char* msg, float percent, void* cd)
  246. {
  247. QCMake* self = reinterpret_cast<QCMake*>(cd);
  248. if(percent >= 0)
  249. {
  250. emit self->progressChanged(msg, percent);
  251. }
  252. else
  253. {
  254. emit self->outputMessage(msg);
  255. }
  256. QCoreApplication::processEvents();
  257. }
  258. void QCMake::errorCallback(const char* msg, const char* /*title*/,
  259. bool& /*stop*/, void* cd)
  260. {
  261. QCMake* self = reinterpret_cast<QCMake*>(cd);
  262. emit self->errorMessage(msg);
  263. QCoreApplication::processEvents();
  264. }
  265. QString QCMake::binaryDirectory() const
  266. {
  267. return this->BinaryDirectory;
  268. }
  269. QString QCMake::sourceDirectory() const
  270. {
  271. return this->SourceDirectory;
  272. }
  273. QString QCMake::generator() const
  274. {
  275. return this->Generator;
  276. }
  277. QStringList QCMake::availableGenerators() const
  278. {
  279. return this->AvailableGenerators;
  280. }
  281. void QCMake::deleteCache()
  282. {
  283. // delete cache
  284. this->CMakeInstance->GetCacheManager()->DeleteCache(this->BinaryDirectory.toAscii().data());
  285. // reload to make our cache empty
  286. this->CMakeInstance->GetCacheManager()->LoadCache(this->BinaryDirectory.toAscii().data());
  287. // emit no generator and no properties
  288. this->setGenerator(QString());
  289. QCMakeCachePropertyList props = this->properties();
  290. emit this->propertiesChanged(props);
  291. }
  292. void QCMake::reloadCache()
  293. {
  294. // emit that the cache was cleaned out
  295. QCMakeCachePropertyList props;
  296. emit this->propertiesChanged(props);
  297. // reload
  298. this->CMakeInstance->GetCacheManager()->LoadCache(this->BinaryDirectory.toAscii().data());
  299. // emit new cache properties
  300. props = this->properties();
  301. emit this->propertiesChanged(props);
  302. }