QCMake.cxx 13 KB

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