QCMake.cxx 12 KB

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