QCMake.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. QDir execDir(QCoreApplication::applicationDirPath());
  29. #if defined(Q_OS_MAC)
  30. if(execDir.exists("../bin/cmake"))
  31. {
  32. execDir.cd("../bin");
  33. }
  34. else
  35. {
  36. execDir.cd("../../../"); // path to cmake in build directory (need to fix for deployment)
  37. }
  38. #endif
  39. QString cmakeCommand = QString("cmake")+cmSystemTools::GetExecutableExtension();
  40. cmakeCommand = execDir.filePath(cmakeCommand);
  41. cmSystemTools::DisableRunCommandOutput();
  42. cmSystemTools::SetRunCommandHideConsole(true);
  43. cmSystemTools::SetErrorCallback(QCMake::errorCallback, this);
  44. cmSystemTools::FindExecutableDirectory(cmakeCommand.toAscii().data());
  45. this->CMakeInstance = new cmake;
  46. this->CMakeInstance->SetCMakeCommand(cmakeCommand.toAscii().data());
  47. #if defined(Q_OS_MAC)
  48. this->CMakeInstance->SetCMakeEditCommand("cmake-gui.app/Contents/MacOS/cmake-gui");
  49. #else
  50. this->CMakeInstance->SetCMakeEditCommand("cmake-gui");
  51. #endif
  52. this->CMakeInstance->SetProgressCallback(QCMake::progressCallback, this);
  53. std::vector<std::string> generators;
  54. this->CMakeInstance->GetRegisteredGenerators(generators);
  55. std::vector<std::string>::iterator iter;
  56. for(iter = generators.begin(); iter != generators.end(); ++iter)
  57. {
  58. // Skip the generator "KDevelop3", since there is also
  59. // "KDevelop3 - Unix Makefiles", which is the full and official name.
  60. // The short name is actually only still there since this was the name
  61. // in CMake 2.4, to keep "command line argument compatibility", but
  62. // this is not necessary in the GUI.
  63. if (*iter == "KDevelop3")
  64. {
  65. continue;
  66. }
  67. this->AvailableGenerators.append(iter->c_str());
  68. }
  69. }
  70. QCMake::~QCMake()
  71. {
  72. delete this->CMakeInstance;
  73. //cmDynamicLoader::FlushCache();
  74. }
  75. void QCMake::loadCache(const QString& dir)
  76. {
  77. this->setBinaryDirectory(dir);
  78. }
  79. void QCMake::setSourceDirectory(const QString& _dir)
  80. {
  81. QString dir =
  82. cmSystemTools::GetActualCaseForPath(_dir.toAscii().data()).c_str();
  83. if(this->SourceDirectory != dir)
  84. {
  85. this->SourceDirectory = QDir::fromNativeSeparators(dir);
  86. emit this->sourceDirChanged(this->SourceDirectory);
  87. }
  88. }
  89. void QCMake::setBinaryDirectory(const QString& _dir)
  90. {
  91. QString dir =
  92. cmSystemTools::GetActualCaseForPath(_dir.toAscii().data()).c_str();
  93. if(this->BinaryDirectory != dir)
  94. {
  95. this->BinaryDirectory = QDir::fromNativeSeparators(dir);
  96. emit this->binaryDirChanged(this->BinaryDirectory);
  97. cmCacheManager *cachem = this->CMakeInstance->GetCacheManager();
  98. this->setGenerator(QString());
  99. if(!this->CMakeInstance->GetCacheManager()->LoadCache(
  100. this->BinaryDirectory.toLocal8Bit().data()))
  101. {
  102. QDir testDir(this->BinaryDirectory);
  103. if(testDir.exists("CMakeCache.txt"))
  104. {
  105. cmSystemTools::Error("There is a CMakeCache.txt file for the current binary "
  106. "tree but cmake does not have permission to read it. "
  107. "Please check the permissions of the directory you are trying to run CMake on.");
  108. }
  109. }
  110. QCMakePropertyList props = this->properties();
  111. emit this->propertiesChanged(props);
  112. cmCacheManager::CacheIterator itm = cachem->NewIterator();
  113. if ( itm.Find("CMAKE_HOME_DIRECTORY"))
  114. {
  115. setSourceDirectory(itm.GetValue());
  116. }
  117. if ( itm.Find("CMAKE_GENERATOR"))
  118. {
  119. const char* extraGen = cachem->GetCacheValue("CMAKE_EXTRA_GENERATOR");
  120. std::string curGen = cmExternalMakefileProjectGenerator::
  121. CreateFullGeneratorName(itm.GetValue(), extraGen);
  122. this->setGenerator(curGen.c_str());
  123. }
  124. }
  125. }
  126. void QCMake::setGenerator(const QString& gen)
  127. {
  128. if(this->Generator != gen)
  129. {
  130. this->Generator = gen;
  131. emit this->generatorChanged(this->Generator);
  132. }
  133. }
  134. void QCMake::configure()
  135. {
  136. #ifdef Q_OS_WIN
  137. UINT lastErrorMode = SetErrorMode(0);
  138. #endif
  139. this->CMakeInstance->SetHomeDirectory(this->SourceDirectory.toAscii().data());
  140. this->CMakeInstance->SetStartDirectory(this->SourceDirectory.toAscii().data());
  141. this->CMakeInstance->SetHomeOutputDirectory(this->BinaryDirectory.toAscii().data());
  142. this->CMakeInstance->SetStartOutputDirectory(this->BinaryDirectory.toAscii().data());
  143. this->CMakeInstance->SetGlobalGenerator(
  144. this->CMakeInstance->CreateGlobalGenerator(this->Generator.toAscii().data()));
  145. this->CMakeInstance->LoadCache();
  146. this->CMakeInstance->SetSuppressDevWarnings(this->SuppressDevWarnings);
  147. std::cerr << "set warn uninitialized " << this->WarnUninitializedMode << "\n";
  148. this->CMakeInstance->SetWarnUninitialized(this->WarnUninitializedMode);
  149. this->CMakeInstance->SetWarnUnused(this->WarnUnusedMode);
  150. this->CMakeInstance->PreLoadCMakeFiles();
  151. cmSystemTools::ResetErrorOccuredFlag();
  152. int err = this->CMakeInstance->Configure();
  153. #ifdef Q_OS_WIN
  154. SetErrorMode(lastErrorMode);
  155. #endif
  156. emit this->propertiesChanged(this->properties());
  157. emit this->configureDone(err);
  158. }
  159. void QCMake::generate()
  160. {
  161. #ifdef Q_OS_WIN
  162. UINT lastErrorMode = SetErrorMode(0);
  163. #endif
  164. cmSystemTools::ResetErrorOccuredFlag();
  165. int err = this->CMakeInstance->Generate();
  166. #ifdef Q_OS_WIN
  167. SetErrorMode(lastErrorMode);
  168. #endif
  169. emit this->generateDone(err);
  170. }
  171. void QCMake::setProperties(const QCMakePropertyList& newProps)
  172. {
  173. QCMakePropertyList props = newProps;
  174. QStringList toremove;
  175. // set the value of properties
  176. cmCacheManager *cachem = this->CMakeInstance->GetCacheManager();
  177. for(cmCacheManager::CacheIterator i = cachem->NewIterator();
  178. !i.IsAtEnd(); i.Next())
  179. {
  180. if(i.GetType() == cmCacheManager::INTERNAL ||
  181. i.GetType() == cmCacheManager::STATIC)
  182. {
  183. continue;
  184. }
  185. QCMakeProperty prop;
  186. prop.Key = i.GetName();
  187. int idx = props.indexOf(prop);
  188. if(idx == -1)
  189. {
  190. toremove.append(i.GetName());
  191. }
  192. else
  193. {
  194. prop = props[idx];
  195. if(prop.Value.type() == QVariant::Bool)
  196. {
  197. i.SetValue(prop.Value.toBool() ? "ON" : "OFF");
  198. }
  199. else
  200. {
  201. i.SetValue(prop.Value.toString().toAscii().data());
  202. }
  203. props.removeAt(idx);
  204. }
  205. }
  206. // remove some properites
  207. foreach(QString s, toremove)
  208. {
  209. cachem->RemoveCacheEntry(s.toAscii().data());
  210. }
  211. // add some new properites
  212. foreach(QCMakeProperty s, props)
  213. {
  214. if(s.Type == QCMakeProperty::BOOL)
  215. {
  216. this->CMakeInstance->AddCacheEntry(s.Key.toAscii().data(),
  217. s.Value.toBool() ? "ON" : "OFF",
  218. s.Help.toAscii().data(),
  219. cmCacheManager::BOOL);
  220. }
  221. else if(s.Type == QCMakeProperty::STRING)
  222. {
  223. this->CMakeInstance->AddCacheEntry(s.Key.toAscii().data(),
  224. s.Value.toString().toAscii().data(),
  225. s.Help.toAscii().data(),
  226. cmCacheManager::STRING);
  227. }
  228. else if(s.Type == QCMakeProperty::PATH)
  229. {
  230. this->CMakeInstance->AddCacheEntry(s.Key.toAscii().data(),
  231. s.Value.toString().toAscii().data(),
  232. s.Help.toAscii().data(),
  233. cmCacheManager::PATH);
  234. }
  235. else if(s.Type == QCMakeProperty::FILEPATH)
  236. {
  237. this->CMakeInstance->AddCacheEntry(s.Key.toAscii().data(),
  238. s.Value.toString().toAscii().data(),
  239. s.Help.toAscii().data(),
  240. cmCacheManager::FILEPATH);
  241. }
  242. }
  243. cachem->SaveCache(this->BinaryDirectory.toAscii().data());
  244. }
  245. QCMakePropertyList QCMake::properties() const
  246. {
  247. QCMakePropertyList ret;
  248. cmCacheManager *cachem = this->CMakeInstance->GetCacheManager();
  249. for(cmCacheManager::CacheIterator i = cachem->NewIterator();
  250. !i.IsAtEnd(); i.Next())
  251. {
  252. if(i.GetType() == cmCacheManager::INTERNAL ||
  253. i.GetType() == cmCacheManager::STATIC ||
  254. i.GetType() == cmCacheManager::UNINITIALIZED)
  255. {
  256. continue;
  257. }
  258. QCMakeProperty prop;
  259. prop.Key = i.GetName();
  260. prop.Help = i.GetProperty("HELPSTRING");
  261. prop.Value = i.GetValue();
  262. prop.Advanced = i.GetPropertyAsBool("ADVANCED");
  263. if(i.GetType() == cmCacheManager::BOOL)
  264. {
  265. prop.Type = QCMakeProperty::BOOL;
  266. prop.Value = cmSystemTools::IsOn(i.GetValue());
  267. }
  268. else if(i.GetType() == cmCacheManager::PATH)
  269. {
  270. prop.Type = QCMakeProperty::PATH;
  271. }
  272. else if(i.GetType() == cmCacheManager::FILEPATH)
  273. {
  274. prop.Type = QCMakeProperty::FILEPATH;
  275. }
  276. else if(i.GetType() == cmCacheManager::STRING)
  277. {
  278. prop.Type = QCMakeProperty::STRING;
  279. if (i.PropertyExists("STRINGS"))
  280. {
  281. prop.Strings = QString(i.GetProperty("STRINGS")).split(";");
  282. }
  283. }
  284. ret.append(prop);
  285. }
  286. return ret;
  287. }
  288. void QCMake::interrupt()
  289. {
  290. cmSystemTools::SetFatalErrorOccured();
  291. }
  292. void QCMake::progressCallback(const char* msg, float percent, void* cd)
  293. {
  294. QCMake* self = reinterpret_cast<QCMake*>(cd);
  295. if(percent >= 0)
  296. {
  297. emit self->progressChanged(msg, percent);
  298. }
  299. else
  300. {
  301. emit self->outputMessage(msg);
  302. }
  303. QCoreApplication::processEvents();
  304. }
  305. void QCMake::errorCallback(const char* msg, const char* /*title*/,
  306. bool& /*stop*/, void* cd)
  307. {
  308. QCMake* self = reinterpret_cast<QCMake*>(cd);
  309. emit self->errorMessage(msg);
  310. QCoreApplication::processEvents();
  311. }
  312. QString QCMake::binaryDirectory() const
  313. {
  314. return this->BinaryDirectory;
  315. }
  316. QString QCMake::sourceDirectory() const
  317. {
  318. return this->SourceDirectory;
  319. }
  320. QString QCMake::generator() const
  321. {
  322. return this->Generator;
  323. }
  324. QStringList QCMake::availableGenerators() const
  325. {
  326. return this->AvailableGenerators;
  327. }
  328. void QCMake::deleteCache()
  329. {
  330. // delete cache
  331. this->CMakeInstance->GetCacheManager()->DeleteCache(this->BinaryDirectory.toAscii().data());
  332. // reload to make our cache empty
  333. this->CMakeInstance->GetCacheManager()->LoadCache(this->BinaryDirectory.toAscii().data());
  334. // emit no generator and no properties
  335. this->setGenerator(QString());
  336. QCMakePropertyList props = this->properties();
  337. emit this->propertiesChanged(props);
  338. }
  339. void QCMake::reloadCache()
  340. {
  341. // emit that the cache was cleaned out
  342. QCMakePropertyList props;
  343. emit this->propertiesChanged(props);
  344. // reload
  345. this->CMakeInstance->GetCacheManager()->LoadCache(this->BinaryDirectory.toAscii().data());
  346. // emit new cache properties
  347. props = this->properties();
  348. emit this->propertiesChanged(props);
  349. }
  350. void QCMake::setDebugOutput(bool flag)
  351. {
  352. if(flag != this->CMakeInstance->GetDebugOutput())
  353. {
  354. this->CMakeInstance->SetDebugOutputOn(flag);
  355. emit this->debugOutputChanged(flag);
  356. }
  357. }
  358. bool QCMake::getDebugOutput() const
  359. {
  360. return this->CMakeInstance->GetDebugOutput();
  361. }
  362. void QCMake::setSuppressDevWarnings(bool value)
  363. {
  364. this->SuppressDevWarnings = value;
  365. }
  366. void QCMake::setWarnUninitializedMode(bool value)
  367. {
  368. this->WarnUninitializedMode = value;
  369. }
  370. void QCMake::setWarnUnusedMode(bool value)
  371. {
  372. this->WarnUnusedMode = value;
  373. }