QCMake.cxx 12 KB

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