QCMake.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 "cmState.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. cmState* state = this->CMakeInstance->GetState();
  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 = state->GetCacheEntryValue("CMAKE_HOME_DIRECTORY");
  98. if (homeDir)
  99. {
  100. setSourceDirectory(QString::fromLocal8Bit(homeDir));
  101. }
  102. const char* gen = state->GetCacheEntryValue("CMAKE_GENERATOR");
  103. if (gen)
  104. {
  105. const char* extraGen = state
  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->SetHomeOutputDirectory(this->BinaryDirectory.toLocal8Bit().data());
  128. this->CMakeInstance->SetGlobalGenerator(
  129. this->CMakeInstance->CreateGlobalGenerator(this->Generator.toLocal8Bit().data()));
  130. this->CMakeInstance->SetGeneratorPlatform("");
  131. this->CMakeInstance->SetGeneratorToolset("");
  132. this->CMakeInstance->LoadCache();
  133. this->CMakeInstance->SetSuppressDevWarnings(this->SuppressDevWarnings);
  134. this->CMakeInstance->SetWarnUninitialized(this->WarnUninitializedMode);
  135. this->CMakeInstance->SetWarnUnused(this->WarnUnusedMode);
  136. this->CMakeInstance->PreLoadCMakeFiles();
  137. InterruptFlag = 0;
  138. cmSystemTools::ResetErrorOccuredFlag();
  139. int err = this->CMakeInstance->Configure();
  140. #ifdef Q_OS_WIN
  141. SetErrorMode(lastErrorMode);
  142. #endif
  143. emit this->propertiesChanged(this->properties());
  144. emit this->configureDone(err);
  145. }
  146. void QCMake::generate()
  147. {
  148. #ifdef Q_OS_WIN
  149. UINT lastErrorMode = SetErrorMode(0);
  150. #endif
  151. InterruptFlag = 0;
  152. cmSystemTools::ResetErrorOccuredFlag();
  153. int err = this->CMakeInstance->Generate();
  154. #ifdef Q_OS_WIN
  155. SetErrorMode(lastErrorMode);
  156. #endif
  157. emit this->generateDone(err);
  158. }
  159. void QCMake::setProperties(const QCMakePropertyList& newProps)
  160. {
  161. QCMakePropertyList props = newProps;
  162. QStringList toremove;
  163. // set the value of properties
  164. cmState* state = this->CMakeInstance->GetState();
  165. std::vector<std::string> cacheKeys = state->GetCacheEntryKeys();
  166. for(std::vector<std::string>::const_iterator it = cacheKeys.begin();
  167. it != cacheKeys.end(); ++it)
  168. {
  169. cmState::CacheEntryType t = state->GetCacheEntryType(*it);
  170. if(t == cmState::INTERNAL ||
  171. t == cmState::STATIC)
  172. {
  173. continue;
  174. }
  175. QCMakeProperty prop;
  176. prop.Key = QString::fromLocal8Bit(it->c_str());
  177. int idx = props.indexOf(prop);
  178. if(idx == -1)
  179. {
  180. toremove.append(QString::fromLocal8Bit(it->c_str()));
  181. }
  182. else
  183. {
  184. prop = props[idx];
  185. if(prop.Value.type() == QVariant::Bool)
  186. {
  187. state->SetCacheEntryValue(*it, prop.Value.toBool() ? "ON" : "OFF");
  188. }
  189. else
  190. {
  191. state->SetCacheEntryValue(*it,
  192. prop.Value.toString().toLocal8Bit().data());
  193. }
  194. props.removeAt(idx);
  195. }
  196. }
  197. // remove some properites
  198. foreach(QString s, toremove)
  199. {
  200. this->CMakeInstance->UnwatchUnusedCli(s.toLocal8Bit().data());
  201. state->RemoveCacheEntry(s.toLocal8Bit().data());
  202. }
  203. // add some new properites
  204. foreach(QCMakeProperty s, props)
  205. {
  206. this->CMakeInstance->WatchUnusedCli(s.Key.toLocal8Bit().data());
  207. if(s.Type == QCMakeProperty::BOOL)
  208. {
  209. this->CMakeInstance->AddCacheEntry(s.Key.toLocal8Bit().data(),
  210. s.Value.toBool() ? "ON" : "OFF",
  211. s.Help.toLocal8Bit().data(),
  212. cmState::BOOL);
  213. }
  214. else if(s.Type == QCMakeProperty::STRING)
  215. {
  216. this->CMakeInstance->AddCacheEntry(s.Key.toLocal8Bit().data(),
  217. s.Value.toString().toLocal8Bit().data(),
  218. s.Help.toLocal8Bit().data(),
  219. cmState::STRING);
  220. }
  221. else if(s.Type == QCMakeProperty::PATH)
  222. {
  223. this->CMakeInstance->AddCacheEntry(s.Key.toLocal8Bit().data(),
  224. s.Value.toString().toLocal8Bit().data(),
  225. s.Help.toLocal8Bit().data(),
  226. cmState::PATH);
  227. }
  228. else if(s.Type == QCMakeProperty::FILEPATH)
  229. {
  230. this->CMakeInstance->AddCacheEntry(s.Key.toLocal8Bit().data(),
  231. s.Value.toString().toLocal8Bit().data(),
  232. s.Help.toLocal8Bit().data(),
  233. cmState::FILEPATH);
  234. }
  235. }
  236. this->CMakeInstance->SaveCache(this->BinaryDirectory.toLocal8Bit().data());
  237. }
  238. QCMakePropertyList QCMake::properties() const
  239. {
  240. QCMakePropertyList ret;
  241. cmState* state = this->CMakeInstance->GetState();
  242. std::vector<std::string> cacheKeys = state->GetCacheEntryKeys();
  243. for (std::vector<std::string>::const_iterator i = cacheKeys.begin();
  244. i != cacheKeys.end(); ++i)
  245. {
  246. cmState::CacheEntryType t = state->GetCacheEntryType(*i);
  247. if(t == cmState::INTERNAL ||
  248. t == cmState::STATIC ||
  249. t == cmState::UNINITIALIZED)
  250. {
  251. continue;
  252. }
  253. const char* cachedValue = state->GetCacheEntryValue(*i);
  254. QCMakeProperty prop;
  255. prop.Key = QString::fromLocal8Bit(i->c_str());
  256. prop.Help = QString::fromLocal8Bit(
  257. state->GetCacheEntryProperty(*i, "HELPSTRING"));
  258. prop.Value = QString::fromLocal8Bit(cachedValue);
  259. prop.Advanced = state->GetCacheEntryPropertyAsBool(*i, "ADVANCED");
  260. if(t == cmState::BOOL)
  261. {
  262. prop.Type = QCMakeProperty::BOOL;
  263. prop.Value = cmSystemTools::IsOn(cachedValue);
  264. }
  265. else if(t == cmState::PATH)
  266. {
  267. prop.Type = QCMakeProperty::PATH;
  268. }
  269. else if(t == cmState::FILEPATH)
  270. {
  271. prop.Type = QCMakeProperty::FILEPATH;
  272. }
  273. else if(t == cmState::STRING)
  274. {
  275. prop.Type = QCMakeProperty::STRING;
  276. const char* stringsProperty =
  277. state->GetCacheEntryProperty(*i, "STRINGS");
  278. if (stringsProperty)
  279. {
  280. prop.Strings = QString::fromLocal8Bit(stringsProperty).split(";");
  281. }
  282. }
  283. ret.append(prop);
  284. }
  285. return ret;
  286. }
  287. void QCMake::interrupt()
  288. {
  289. this->InterruptFlag.ref();
  290. }
  291. bool QCMake::interruptCallback(void* cd)
  292. {
  293. QCMake* self = reinterpret_cast<QCMake*>(cd);
  294. #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
  295. return self->InterruptFlag;
  296. #else
  297. return self->InterruptFlag.load();
  298. #endif
  299. }
  300. void QCMake::progressCallback(const char* msg, float percent, void* cd)
  301. {
  302. QCMake* self = reinterpret_cast<QCMake*>(cd);
  303. if(percent >= 0)
  304. {
  305. emit self->progressChanged(QString::fromLocal8Bit(msg), percent);
  306. }
  307. else
  308. {
  309. emit self->outputMessage(QString::fromLocal8Bit(msg));
  310. }
  311. QCoreApplication::processEvents();
  312. }
  313. void QCMake::messageCallback(const char* msg, const char* /*title*/,
  314. bool& /*stop*/, void* cd)
  315. {
  316. QCMake* self = reinterpret_cast<QCMake*>(cd);
  317. emit self->errorMessage(QString::fromLocal8Bit(msg));
  318. QCoreApplication::processEvents();
  319. }
  320. void QCMake::stdoutCallback(const char* msg, size_t len, void* cd)
  321. {
  322. QCMake* self = reinterpret_cast<QCMake*>(cd);
  323. emit self->outputMessage(QString::fromLocal8Bit(msg,int(len)));
  324. QCoreApplication::processEvents();
  325. }
  326. void QCMake::stderrCallback(const char* msg, size_t len, void* cd)
  327. {
  328. QCMake* self = reinterpret_cast<QCMake*>(cd);
  329. emit self->outputMessage(QString::fromLocal8Bit(msg,int(len)));
  330. QCoreApplication::processEvents();
  331. }
  332. QString QCMake::binaryDirectory() const
  333. {
  334. return this->BinaryDirectory;
  335. }
  336. QString QCMake::sourceDirectory() const
  337. {
  338. return this->SourceDirectory;
  339. }
  340. QString QCMake::generator() const
  341. {
  342. return this->Generator;
  343. }
  344. QStringList QCMake::availableGenerators() const
  345. {
  346. return this->AvailableGenerators;
  347. }
  348. void QCMake::deleteCache()
  349. {
  350. // delete cache
  351. this->CMakeInstance->DeleteCache(this->BinaryDirectory.toLocal8Bit().data());
  352. // reload to make our cache empty
  353. this->CMakeInstance->LoadCache(this->BinaryDirectory.toLocal8Bit().data());
  354. // emit no generator and no properties
  355. this->setGenerator(QString());
  356. QCMakePropertyList props = this->properties();
  357. emit this->propertiesChanged(props);
  358. }
  359. void QCMake::reloadCache()
  360. {
  361. // emit that the cache was cleaned out
  362. QCMakePropertyList props;
  363. emit this->propertiesChanged(props);
  364. // reload
  365. this->CMakeInstance->LoadCache(this->BinaryDirectory.toLocal8Bit().data());
  366. // emit new cache properties
  367. props = this->properties();
  368. emit this->propertiesChanged(props);
  369. }
  370. void QCMake::setDebugOutput(bool flag)
  371. {
  372. if(flag != this->CMakeInstance->GetDebugOutput())
  373. {
  374. this->CMakeInstance->SetDebugOutputOn(flag);
  375. emit this->debugOutputChanged(flag);
  376. }
  377. }
  378. bool QCMake::getDebugOutput() const
  379. {
  380. return this->CMakeInstance->GetDebugOutput();
  381. }
  382. void QCMake::setSuppressDevWarnings(bool value)
  383. {
  384. this->SuppressDevWarnings = value;
  385. }
  386. void QCMake::setWarnUninitializedMode(bool value)
  387. {
  388. this->WarnUninitializedMode = value;
  389. }
  390. void QCMake::setWarnUnusedMode(bool value)
  391. {
  392. this->WarnUnusedMode = value;
  393. }