QCMake.cxx 13 KB

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