QCMake.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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(
  22. [this](const char* msg, const char* title) {
  23. this->messageCallback(msg, title);
  24. });
  25. cmSystemTools::SetStdoutCallback(
  26. [this](std::string const& msg) { this->stdoutCallback(msg); });
  27. cmSystemTools::SetStderrCallback(
  28. [this](std::string const& msg) { this->stderrCallback(msg); });
  29. this->CMakeInstance = new cmake(cmake::RoleProject, cmState::Project);
  30. this->CMakeInstance->SetCMakeEditCommand(
  31. cmSystemTools::GetCMakeGUICommand());
  32. this->CMakeInstance->SetProgressCallback(
  33. [this](const char* msg, float percent) {
  34. this->progressCallback(msg, percent);
  35. });
  36. cmSystemTools::SetInterruptCallback(
  37. [this] { return this->interruptCallback(); });
  38. std::vector<cmake::GeneratorInfo> generators;
  39. this->CMakeInstance->GetRegisteredGenerators(
  40. generators, /*includeNamesWithPlatform=*/false);
  41. for (cmake::GeneratorInfo const& gen : generators) {
  42. this->AvailableGenerators.push_back(gen);
  43. }
  44. }
  45. QCMake::~QCMake()
  46. {
  47. delete this->CMakeInstance;
  48. // cmDynamicLoader::FlushCache();
  49. }
  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. const char* homeDir = state->GetCacheEntryValue("CMAKE_HOME_DIRECTORY");
  88. if (homeDir) {
  89. setSourceDirectory(QString::fromLocal8Bit(homeDir));
  90. }
  91. const char* 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. const char* platform =
  101. state->GetCacheEntryValue("CMAKE_GENERATOR_PLATFORM");
  102. if (platform) {
  103. this->setPlatform(QString::fromLocal8Bit(platform));
  104. }
  105. const char* toolset = state->GetCacheEntryValue("CMAKE_GENERATOR_TOOLSET");
  106. if (toolset) {
  107. this->setToolset(QString::fromLocal8Bit(toolset));
  108. }
  109. checkOpenPossible();
  110. }
  111. }
  112. void QCMake::setGenerator(const QString& gen)
  113. {
  114. if (this->Generator != gen) {
  115. this->Generator = gen;
  116. emit this->generatorChanged(this->Generator);
  117. }
  118. }
  119. void QCMake::setPlatform(const QString& platform)
  120. {
  121. if (this->Platform != platform) {
  122. this->Platform = platform;
  123. emit this->platformChanged(this->Platform);
  124. }
  125. }
  126. void QCMake::setToolset(const QString& toolset)
  127. {
  128. if (this->Toolset != toolset) {
  129. this->Toolset = toolset;
  130. emit this->toolsetChanged(this->Toolset);
  131. }
  132. }
  133. void QCMake::configure()
  134. {
  135. #ifdef Q_OS_WIN
  136. UINT lastErrorMode = SetErrorMode(0);
  137. #endif
  138. this->CMakeInstance->SetHomeDirectory(
  139. this->SourceDirectory.toLocal8Bit().data());
  140. this->CMakeInstance->SetHomeOutputDirectory(
  141. this->BinaryDirectory.toLocal8Bit().data());
  142. this->CMakeInstance->SetGlobalGenerator(
  143. this->CMakeInstance->CreateGlobalGenerator(
  144. this->Generator.toLocal8Bit().data()));
  145. this->CMakeInstance->SetGeneratorPlatform(
  146. this->Platform.toLocal8Bit().data());
  147. this->CMakeInstance->SetGeneratorToolset(this->Toolset.toLocal8Bit().data());
  148. this->CMakeInstance->LoadCache();
  149. this->CMakeInstance->SetWarnUninitialized(this->WarnUninitializedMode);
  150. this->CMakeInstance->SetWarnUnused(this->WarnUnusedMode);
  151. this->CMakeInstance->PreLoadCMakeFiles();
  152. InterruptFlag = 0;
  153. cmSystemTools::ResetErrorOccuredFlag();
  154. int err = this->CMakeInstance->Configure();
  155. #ifdef Q_OS_WIN
  156. SetErrorMode(lastErrorMode);
  157. #endif
  158. emit this->propertiesChanged(this->properties());
  159. emit this->configureDone(err);
  160. }
  161. void QCMake::generate()
  162. {
  163. #ifdef Q_OS_WIN
  164. UINT lastErrorMode = SetErrorMode(0);
  165. #endif
  166. InterruptFlag = 0;
  167. cmSystemTools::ResetErrorOccuredFlag();
  168. int err = this->CMakeInstance->Generate();
  169. #ifdef Q_OS_WIN
  170. SetErrorMode(lastErrorMode);
  171. #endif
  172. emit this->generateDone(err);
  173. checkOpenPossible();
  174. }
  175. void QCMake::open()
  176. {
  177. #ifdef Q_OS_WIN
  178. UINT lastErrorMode = SetErrorMode(0);
  179. #endif
  180. InterruptFlag = 0;
  181. cmSystemTools::ResetErrorOccuredFlag();
  182. auto successful = this->CMakeInstance->Open(
  183. this->BinaryDirectory.toLocal8Bit().data(), false);
  184. #ifdef Q_OS_WIN
  185. SetErrorMode(lastErrorMode);
  186. #endif
  187. emit this->openDone(successful);
  188. }
  189. void QCMake::setProperties(const QCMakePropertyList& newProps)
  190. {
  191. QCMakePropertyList props = newProps;
  192. QStringList toremove;
  193. // set the value of properties
  194. cmState* state = this->CMakeInstance->GetState();
  195. std::vector<std::string> cacheKeys = state->GetCacheEntryKeys();
  196. for (std::string const& key : cacheKeys) {
  197. cmStateEnums::CacheEntryType t = state->GetCacheEntryType(key);
  198. if (t == cmStateEnums::INTERNAL || t == cmStateEnums::STATIC) {
  199. continue;
  200. }
  201. QCMakeProperty prop;
  202. prop.Key = QString::fromLocal8Bit(key.c_str());
  203. int idx = props.indexOf(prop);
  204. if (idx == -1) {
  205. toremove.append(QString::fromLocal8Bit(key.c_str()));
  206. } else {
  207. prop = props[idx];
  208. if (prop.Value.type() == QVariant::Bool) {
  209. state->SetCacheEntryValue(key, prop.Value.toBool() ? "ON" : "OFF");
  210. } else {
  211. state->SetCacheEntryValue(key,
  212. prop.Value.toString().toLocal8Bit().data());
  213. }
  214. props.removeAt(idx);
  215. }
  216. }
  217. // remove some properites
  218. foreach (QString const& s, toremove) {
  219. this->CMakeInstance->UnwatchUnusedCli(s.toLocal8Bit().data());
  220. state->RemoveCacheEntry(s.toLocal8Bit().data());
  221. }
  222. // add some new properites
  223. foreach (QCMakeProperty const& s, props) {
  224. this->CMakeInstance->WatchUnusedCli(s.Key.toLocal8Bit().data());
  225. if (s.Type == QCMakeProperty::BOOL) {
  226. this->CMakeInstance->AddCacheEntry(
  227. s.Key.toLocal8Bit().data(), s.Value.toBool() ? "ON" : "OFF",
  228. s.Help.toLocal8Bit().data(), cmStateEnums::BOOL);
  229. } else if (s.Type == QCMakeProperty::STRING) {
  230. this->CMakeInstance->AddCacheEntry(
  231. s.Key.toLocal8Bit().data(), s.Value.toString().toLocal8Bit().data(),
  232. s.Help.toLocal8Bit().data(), cmStateEnums::STRING);
  233. } else if (s.Type == QCMakeProperty::PATH) {
  234. this->CMakeInstance->AddCacheEntry(
  235. s.Key.toLocal8Bit().data(), s.Value.toString().toLocal8Bit().data(),
  236. s.Help.toLocal8Bit().data(), cmStateEnums::PATH);
  237. } else if (s.Type == QCMakeProperty::FILEPATH) {
  238. this->CMakeInstance->AddCacheEntry(
  239. s.Key.toLocal8Bit().data(), s.Value.toString().toLocal8Bit().data(),
  240. s.Help.toLocal8Bit().data(), cmStateEnums::FILEPATH);
  241. }
  242. }
  243. this->CMakeInstance->SaveCache(this->BinaryDirectory.toLocal8Bit().data());
  244. }
  245. QCMakePropertyList QCMake::properties() const
  246. {
  247. QCMakePropertyList ret;
  248. cmState* state = this->CMakeInstance->GetState();
  249. std::vector<std::string> cacheKeys = state->GetCacheEntryKeys();
  250. for (std::string const& key : cacheKeys) {
  251. cmStateEnums::CacheEntryType t = state->GetCacheEntryType(key);
  252. if (t == cmStateEnums::INTERNAL || t == cmStateEnums::STATIC ||
  253. t == cmStateEnums::UNINITIALIZED) {
  254. continue;
  255. }
  256. const char* cachedValue = state->GetCacheEntryValue(key);
  257. QCMakeProperty prop;
  258. prop.Key = QString::fromLocal8Bit(key.c_str());
  259. prop.Help =
  260. QString::fromLocal8Bit(state->GetCacheEntryProperty(key, "HELPSTRING"));
  261. prop.Value = QString::fromLocal8Bit(cachedValue);
  262. prop.Advanced = state->GetCacheEntryPropertyAsBool(key, "ADVANCED");
  263. if (t == cmStateEnums::BOOL) {
  264. prop.Type = QCMakeProperty::BOOL;
  265. prop.Value = cmSystemTools::IsOn(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. const char* stringsProperty =
  273. state->GetCacheEntryProperty(key, "STRINGS");
  274. if (stringsProperty) {
  275. prop.Strings = QString::fromLocal8Bit(stringsProperty).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. #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
  289. return this->InterruptFlag;
  290. #else
  291. return this->InterruptFlag.load();
  292. #endif
  293. }
  294. void QCMake::progressCallback(const char* msg, float percent)
  295. {
  296. if (percent >= 0) {
  297. emit this->progressChanged(QString::fromLocal8Bit(msg), percent);
  298. } else {
  299. emit this->outputMessage(QString::fromLocal8Bit(msg));
  300. }
  301. QCoreApplication::processEvents();
  302. }
  303. void QCMake::messageCallback(const char* msg, const char* /*title*/)
  304. {
  305. emit this->errorMessage(QString::fromLocal8Bit(msg));
  306. QCoreApplication::processEvents();
  307. }
  308. void QCMake::stdoutCallback(std::string const& msg)
  309. {
  310. emit this->outputMessage(QString::fromStdString(msg));
  311. QCoreApplication::processEvents();
  312. }
  313. void QCMake::stderrCallback(std::string const& msg)
  314. {
  315. emit this->outputMessage(QString::fromStdString(msg));
  316. QCoreApplication::processEvents();
  317. }
  318. QString QCMake::binaryDirectory() const
  319. {
  320. return this->BinaryDirectory;
  321. }
  322. QString QCMake::sourceDirectory() const
  323. {
  324. return this->SourceDirectory;
  325. }
  326. QString QCMake::generator() const
  327. {
  328. return this->Generator;
  329. }
  330. std::vector<cmake::GeneratorInfo> const& QCMake::availableGenerators() const
  331. {
  332. return AvailableGenerators;
  333. }
  334. void QCMake::deleteCache()
  335. {
  336. // delete cache
  337. this->CMakeInstance->DeleteCache(this->BinaryDirectory.toLocal8Bit().data());
  338. // reload to make our cache empty
  339. this->CMakeInstance->LoadCache(this->BinaryDirectory.toLocal8Bit().data());
  340. // emit no generator and no properties
  341. this->setGenerator(QString());
  342. this->setToolset(QString());
  343. QCMakePropertyList props = this->properties();
  344. emit this->propertiesChanged(props);
  345. }
  346. void QCMake::reloadCache()
  347. {
  348. // emit that the cache was cleaned out
  349. QCMakePropertyList props;
  350. emit this->propertiesChanged(props);
  351. // reload
  352. this->CMakeInstance->LoadCache(this->BinaryDirectory.toLocal8Bit().data());
  353. // emit new cache properties
  354. props = this->properties();
  355. emit this->propertiesChanged(props);
  356. }
  357. void QCMake::setDebugOutput(bool flag)
  358. {
  359. if (flag != this->CMakeInstance->GetDebugOutput()) {
  360. this->CMakeInstance->SetDebugOutputOn(flag);
  361. emit this->debugOutputChanged(flag);
  362. }
  363. }
  364. bool QCMake::getDebugOutput() const
  365. {
  366. return this->CMakeInstance->GetDebugOutput();
  367. }
  368. bool QCMake::getSuppressDevWarnings()
  369. {
  370. return this->CMakeInstance->GetSuppressDevWarnings();
  371. }
  372. void QCMake::setSuppressDevWarnings(bool value)
  373. {
  374. this->CMakeInstance->SetSuppressDevWarnings(value);
  375. }
  376. bool QCMake::getSuppressDeprecatedWarnings()
  377. {
  378. return this->CMakeInstance->GetSuppressDeprecatedWarnings();
  379. }
  380. void QCMake::setSuppressDeprecatedWarnings(bool value)
  381. {
  382. this->CMakeInstance->SetSuppressDeprecatedWarnings(value);
  383. }
  384. bool QCMake::getDevWarningsAsErrors()
  385. {
  386. return this->CMakeInstance->GetDevWarningsAsErrors();
  387. }
  388. void QCMake::setDevWarningsAsErrors(bool value)
  389. {
  390. this->CMakeInstance->SetDevWarningsAsErrors(value);
  391. }
  392. bool QCMake::getDeprecatedWarningsAsErrors()
  393. {
  394. return this->CMakeInstance->GetDeprecatedWarningsAsErrors();
  395. }
  396. void QCMake::setDeprecatedWarningsAsErrors(bool value)
  397. {
  398. this->CMakeInstance->SetDeprecatedWarningsAsErrors(value);
  399. }
  400. void QCMake::setWarnUninitializedMode(bool value)
  401. {
  402. this->WarnUninitializedMode = value;
  403. }
  404. void QCMake::setWarnUnusedMode(bool value)
  405. {
  406. this->WarnUnusedMode = value;
  407. }
  408. void QCMake::checkOpenPossible()
  409. {
  410. auto data = this->BinaryDirectory.toLocal8Bit().data();
  411. auto possible = this->CMakeInstance->Open(data, true);
  412. emit openPossible(possible);
  413. }