QCMake.cxx 13 KB

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