QCMake.cxx 12 KB

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