QCMake.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "QCMake.h"
  11. #include <QDir>
  12. #include <QCoreApplication>
  13. #include "cmake.h"
  14. #include "cmCacheManager.h"
  15. #include "cmSystemTools.h"
  16. #include "cmExternalMakefileProjectGenerator.h"
  17. #ifdef Q_OS_WIN
  18. #include "qt_windows.h" // For SetErrorMode
  19. #endif
  20. QCMake::QCMake(QObject* p)
  21. : QObject(p)
  22. {
  23. this->SuppressDevWarnings = false;
  24. qRegisterMetaType<QCMakeProperty>();
  25. qRegisterMetaType<QCMakePropertyList>();
  26. QDir execDir(QCoreApplication::applicationDirPath());
  27. #if defined(Q_OS_MAC)
  28. if(execDir.exists("../bin/cmake"))
  29. {
  30. execDir.cd("../bin");
  31. }
  32. else
  33. {
  34. execDir.cd("../../../"); // path to cmake in build directory (need to fix for deployment)
  35. }
  36. #endif
  37. QString cmakeCommand = QString("cmake")+cmSystemTools::GetExecutableExtension();
  38. cmakeCommand = execDir.filePath(cmakeCommand);
  39. cmSystemTools::DisableRunCommandOutput();
  40. cmSystemTools::SetRunCommandHideConsole(true);
  41. cmSystemTools::SetErrorCallback(QCMake::errorCallback, this);
  42. cmSystemTools::FindExecutableDirectory(cmakeCommand.toAscii().data());
  43. this->CMakeInstance = new cmake;
  44. this->CMakeInstance->SetCMakeCommand(cmakeCommand.toAscii().data());
  45. #if defined(Q_OS_MAC)
  46. this->CMakeInstance->SetCMakeEditCommand("cmake-gui.app/Contents/MacOS/cmake-gui");
  47. #else
  48. this->CMakeInstance->SetCMakeEditCommand("cmake-gui");
  49. #endif
  50. this->CMakeInstance->SetProgressCallback(QCMake::progressCallback, this);
  51. std::vector<std::string> generators;
  52. this->CMakeInstance->GetRegisteredGenerators(generators);
  53. std::vector<std::string>::iterator iter;
  54. for(iter = generators.begin(); iter != generators.end(); ++iter)
  55. {
  56. // Skip the generator "KDevelop3", since there is also
  57. // "KDevelop3 - Unix Makefiles", which is the full and official name.
  58. // The short name is actually only still there since this was the name
  59. // in CMake 2.4, to keep "command line argument compatibility", but
  60. // this is not necessary in the GUI.
  61. if (*iter == "KDevelop3")
  62. {
  63. continue;
  64. }
  65. this->AvailableGenerators.append(iter->c_str());
  66. }
  67. }
  68. QCMake::~QCMake()
  69. {
  70. delete this->CMakeInstance;
  71. //cmDynamicLoader::FlushCache();
  72. }
  73. void QCMake::loadCache(const QString& dir)
  74. {
  75. this->setBinaryDirectory(dir);
  76. }
  77. void QCMake::setSourceDirectory(const QString& _dir)
  78. {
  79. QString dir =
  80. cmSystemTools::GetActualCaseForPath(_dir.toAscii().data()).c_str();
  81. if(this->SourceDirectory != dir)
  82. {
  83. this->SourceDirectory = QDir::fromNativeSeparators(dir);
  84. emit this->sourceDirChanged(this->SourceDirectory);
  85. }
  86. }
  87. void QCMake::setBinaryDirectory(const QString& _dir)
  88. {
  89. QString dir =
  90. cmSystemTools::GetActualCaseForPath(_dir.toAscii().data()).c_str();
  91. if(this->BinaryDirectory != dir)
  92. {
  93. this->BinaryDirectory = QDir::fromNativeSeparators(dir);
  94. emit this->binaryDirChanged(this->BinaryDirectory);
  95. cmCacheManager *cachem = this->CMakeInstance->GetCacheManager();
  96. this->setGenerator(QString());
  97. if(!this->CMakeInstance->GetCacheManager()->LoadCache(
  98. this->BinaryDirectory.toLocal8Bit().data()))
  99. {
  100. QDir testDir(this->BinaryDirectory);
  101. if(testDir.exists("CMakeCache.txt"))
  102. {
  103. cmSystemTools::Error("There is a CMakeCache.txt file for the current binary "
  104. "tree but cmake does not have permission to read it. "
  105. "Please check the permissions of the directory you are trying to run CMake on.");
  106. }
  107. }
  108. QCMakePropertyList props = this->properties();
  109. emit this->propertiesChanged(props);
  110. cmCacheManager::CacheIterator itm = cachem->NewIterator();
  111. if ( itm.Find("CMAKE_HOME_DIRECTORY"))
  112. {
  113. setSourceDirectory(itm.GetValue());
  114. }
  115. if ( itm.Find("CMAKE_GENERATOR"))
  116. {
  117. const char* extraGen = cachem->GetCacheValue("CMAKE_EXTRA_GENERATOR");
  118. std::string curGen = cmExternalMakefileProjectGenerator::
  119. CreateFullGeneratorName(itm.GetValue(), extraGen);
  120. this->setGenerator(curGen.c_str());
  121. }
  122. }
  123. }
  124. void QCMake::setGenerator(const QString& gen)
  125. {
  126. if(this->Generator != gen)
  127. {
  128. this->Generator = gen;
  129. emit this->generatorChanged(this->Generator);
  130. }
  131. }
  132. void QCMake::configure()
  133. {
  134. #ifdef Q_OS_WIN
  135. UINT lastErrorMode = SetErrorMode(0);
  136. #endif
  137. this->CMakeInstance->SetHomeDirectory(this->SourceDirectory.toAscii().data());
  138. this->CMakeInstance->SetStartDirectory(this->SourceDirectory.toAscii().data());
  139. this->CMakeInstance->SetHomeOutputDirectory(this->BinaryDirectory.toAscii().data());
  140. this->CMakeInstance->SetStartOutputDirectory(this->BinaryDirectory.toAscii().data());
  141. this->CMakeInstance->SetGlobalGenerator(
  142. this->CMakeInstance->CreateGlobalGenerator(this->Generator.toAscii().data()));
  143. this->CMakeInstance->LoadCache();
  144. this->CMakeInstance->SetSuppressDevWarnings(this->SuppressDevWarnings);
  145. this->CMakeInstance->PreLoadCMakeFiles();
  146. cmSystemTools::ResetErrorOccuredFlag();
  147. int err = this->CMakeInstance->Configure();
  148. #ifdef Q_OS_WIN
  149. SetErrorMode(lastErrorMode);
  150. #endif
  151. emit this->propertiesChanged(this->properties());
  152. emit this->configureDone(err);
  153. }
  154. void QCMake::generate()
  155. {
  156. #ifdef Q_OS_WIN
  157. UINT lastErrorMode = SetErrorMode(0);
  158. #endif
  159. cmSystemTools::ResetErrorOccuredFlag();
  160. int err = this->CMakeInstance->Generate();
  161. #ifdef Q_OS_WIN
  162. SetErrorMode(lastErrorMode);
  163. #endif
  164. emit this->generateDone(err);
  165. }
  166. void QCMake::setProperties(const QCMakePropertyList& newProps)
  167. {
  168. QCMakePropertyList props = newProps;
  169. QStringList toremove;
  170. // set the value of properties
  171. cmCacheManager *cachem = this->CMakeInstance->GetCacheManager();
  172. for(cmCacheManager::CacheIterator i = cachem->NewIterator();
  173. !i.IsAtEnd(); i.Next())
  174. {
  175. if(i.GetType() == cmCacheManager::INTERNAL ||
  176. i.GetType() == cmCacheManager::STATIC)
  177. {
  178. continue;
  179. }
  180. QCMakeProperty prop;
  181. prop.Key = i.GetName();
  182. int idx = props.indexOf(prop);
  183. if(idx == -1)
  184. {
  185. toremove.append(i.GetName());
  186. }
  187. else
  188. {
  189. prop = props[idx];
  190. if(prop.Value.type() == QVariant::Bool)
  191. {
  192. i.SetValue(prop.Value.toBool() ? "ON" : "OFF");
  193. }
  194. else
  195. {
  196. i.SetValue(prop.Value.toString().toAscii().data());
  197. }
  198. props.removeAt(idx);
  199. }
  200. }
  201. // remove some properites
  202. foreach(QString s, toremove)
  203. {
  204. cachem->RemoveCacheEntry(s.toAscii().data());
  205. }
  206. // add some new properites
  207. foreach(QCMakeProperty s, props)
  208. {
  209. if(s.Type == QCMakeProperty::BOOL)
  210. {
  211. this->CMakeInstance->AddCacheEntry(s.Key.toAscii().data(),
  212. s.Value.toBool() ? "ON" : "OFF",
  213. s.Help.toAscii().data(),
  214. cmCacheManager::BOOL);
  215. }
  216. else if(s.Type == QCMakeProperty::STRING)
  217. {
  218. this->CMakeInstance->AddCacheEntry(s.Key.toAscii().data(),
  219. s.Value.toString().toAscii().data(),
  220. s.Help.toAscii().data(),
  221. cmCacheManager::STRING);
  222. }
  223. else if(s.Type == QCMakeProperty::PATH)
  224. {
  225. this->CMakeInstance->AddCacheEntry(s.Key.toAscii().data(),
  226. s.Value.toString().toAscii().data(),
  227. s.Help.toAscii().data(),
  228. cmCacheManager::PATH);
  229. }
  230. else if(s.Type == QCMakeProperty::FILEPATH)
  231. {
  232. this->CMakeInstance->AddCacheEntry(s.Key.toAscii().data(),
  233. s.Value.toString().toAscii().data(),
  234. s.Help.toAscii().data(),
  235. cmCacheManager::FILEPATH);
  236. }
  237. }
  238. cachem->SaveCache(this->BinaryDirectory.toAscii().data());
  239. }
  240. QCMakePropertyList QCMake::properties() const
  241. {
  242. QCMakePropertyList ret;
  243. cmCacheManager *cachem = this->CMakeInstance->GetCacheManager();
  244. for(cmCacheManager::CacheIterator i = cachem->NewIterator();
  245. !i.IsAtEnd(); i.Next())
  246. {
  247. if(i.GetType() == cmCacheManager::INTERNAL ||
  248. i.GetType() == cmCacheManager::STATIC ||
  249. i.GetType() == cmCacheManager::UNINITIALIZED)
  250. {
  251. continue;
  252. }
  253. QCMakeProperty prop;
  254. prop.Key = i.GetName();
  255. prop.Help = i.GetProperty("HELPSTRING");
  256. prop.Value = i.GetValue();
  257. prop.Advanced = i.GetPropertyAsBool("ADVANCED");
  258. if(i.GetType() == cmCacheManager::BOOL)
  259. {
  260. prop.Type = QCMakeProperty::BOOL;
  261. prop.Value = cmSystemTools::IsOn(i.GetValue());
  262. }
  263. else if(i.GetType() == cmCacheManager::PATH)
  264. {
  265. prop.Type = QCMakeProperty::PATH;
  266. }
  267. else if(i.GetType() == cmCacheManager::FILEPATH)
  268. {
  269. prop.Type = QCMakeProperty::FILEPATH;
  270. }
  271. else if(i.GetType() == cmCacheManager::STRING)
  272. {
  273. prop.Type = QCMakeProperty::STRING;
  274. if (i.PropertyExists("STRINGS"))
  275. {
  276. prop.Strings = QString(i.GetProperty("STRINGS")).split(";");
  277. }
  278. }
  279. ret.append(prop);
  280. }
  281. return ret;
  282. }
  283. void QCMake::interrupt()
  284. {
  285. cmSystemTools::SetFatalErrorOccured();
  286. }
  287. void QCMake::progressCallback(const char* msg, float percent, void* cd)
  288. {
  289. QCMake* self = reinterpret_cast<QCMake*>(cd);
  290. if(percent >= 0)
  291. {
  292. emit self->progressChanged(msg, percent);
  293. }
  294. else
  295. {
  296. emit self->outputMessage(msg);
  297. }
  298. QCoreApplication::processEvents();
  299. }
  300. void QCMake::errorCallback(const char* msg, const char* /*title*/,
  301. bool& /*stop*/, void* cd)
  302. {
  303. QCMake* self = reinterpret_cast<QCMake*>(cd);
  304. emit self->errorMessage(msg);
  305. QCoreApplication::processEvents();
  306. }
  307. QString QCMake::binaryDirectory() const
  308. {
  309. return this->BinaryDirectory;
  310. }
  311. QString QCMake::sourceDirectory() const
  312. {
  313. return this->SourceDirectory;
  314. }
  315. QString QCMake::generator() const
  316. {
  317. return this->Generator;
  318. }
  319. QStringList QCMake::availableGenerators() const
  320. {
  321. return this->AvailableGenerators;
  322. }
  323. void QCMake::deleteCache()
  324. {
  325. // delete cache
  326. this->CMakeInstance->GetCacheManager()->DeleteCache(this->BinaryDirectory.toAscii().data());
  327. // reload to make our cache empty
  328. this->CMakeInstance->GetCacheManager()->LoadCache(this->BinaryDirectory.toAscii().data());
  329. // emit no generator and no properties
  330. this->setGenerator(QString());
  331. QCMakePropertyList props = this->properties();
  332. emit this->propertiesChanged(props);
  333. }
  334. void QCMake::reloadCache()
  335. {
  336. // emit that the cache was cleaned out
  337. QCMakePropertyList props;
  338. emit this->propertiesChanged(props);
  339. // reload
  340. this->CMakeInstance->GetCacheManager()->LoadCache(this->BinaryDirectory.toAscii().data());
  341. // emit new cache properties
  342. props = this->properties();
  343. emit this->propertiesChanged(props);
  344. }
  345. void QCMake::setDebugOutput(bool flag)
  346. {
  347. if(flag != this->CMakeInstance->GetDebugOutput())
  348. {
  349. this->CMakeInstance->SetDebugOutputOn(flag);
  350. emit this->debugOutputChanged(flag);
  351. }
  352. }
  353. bool QCMake::getDebugOutput() const
  354. {
  355. return this->CMakeInstance->GetDebugOutput();
  356. }
  357. void QCMake::setSuppressDevWarnings(bool value)
  358. {
  359. this->SuppressDevWarnings = value;
  360. }