QCMake.cxx 12 KB

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