QCMake.cxx 13 KB

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