cmake.cxx 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527
  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 "cmake.h"
  11. #include "cmAlgorithms.h"
  12. #include "cmCommands.h"
  13. #include "cmDocumentation.h"
  14. #include "cmDocumentationEntry.h"
  15. #include "cmDocumentationFormatter.h"
  16. #include "cmExternalMakefileProjectGenerator.h"
  17. #include "cmFileTimeComparison.h"
  18. #include "cmGeneratorTarget.h"
  19. #include "cmGlobalGenerator.h"
  20. #include "cmGlobalGeneratorFactory.h"
  21. #include "cmLocalGenerator.h"
  22. #include "cmMakefile.h"
  23. #include "cmMessenger.h"
  24. #include "cmState.h"
  25. #include "cmSystemTools.h"
  26. #include "cmTarget.h"
  27. #include "cmTargetLinkLibraryType.h"
  28. #include "cmUtils.hxx"
  29. #include "cmVersionConfig.h"
  30. #include "cm_auto_ptr.hxx"
  31. #if defined(CMAKE_BUILD_WITH_CMAKE)
  32. #include "cmGraphVizWriter.h"
  33. #include "cmVariableWatch.h"
  34. #include <cm_jsoncpp_value.h>
  35. #include <cm_jsoncpp_writer.h>
  36. #endif
  37. // only build kdevelop generator on non-windows platforms
  38. // when not bootstrapping cmake
  39. #if !defined(_WIN32)
  40. #if defined(CMAKE_BUILD_WITH_CMAKE)
  41. #define CMAKE_USE_KDEVELOP
  42. #endif
  43. #endif
  44. #if defined(CMAKE_BUILD_WITH_CMAKE)
  45. #define CMAKE_USE_ECLIPSE
  46. #endif
  47. #if defined(__MINGW32__) && !defined(CMAKE_BUILD_WITH_CMAKE)
  48. #define CMAKE_BOOT_MINGW
  49. #endif
  50. // include the generator
  51. #if defined(_WIN32) && !defined(__CYGWIN__)
  52. #if !defined(CMAKE_BOOT_MINGW)
  53. #include "cmGlobalBorlandMakefileGenerator.h"
  54. #include "cmGlobalGhsMultiGenerator.h"
  55. #include "cmGlobalJOMMakefileGenerator.h"
  56. #include "cmGlobalNMakeMakefileGenerator.h"
  57. #include "cmGlobalVisualStudio10Generator.h"
  58. #include "cmGlobalVisualStudio11Generator.h"
  59. #include "cmGlobalVisualStudio12Generator.h"
  60. #include "cmGlobalVisualStudio14Generator.h"
  61. #include "cmGlobalVisualStudio15Generator.h"
  62. #include "cmGlobalVisualStudio71Generator.h"
  63. #include "cmGlobalVisualStudio8Generator.h"
  64. #include "cmGlobalVisualStudio9Generator.h"
  65. #define CMAKE_HAVE_VS_GENERATORS
  66. #endif
  67. #include "cmGlobalMSYSMakefileGenerator.h"
  68. #include "cmGlobalMinGWMakefileGenerator.h"
  69. #else
  70. #endif
  71. #if defined(CMAKE_USE_WMAKE)
  72. #include "cmGlobalWatcomWMakeGenerator.h"
  73. #endif
  74. #include "cmGlobalUnixMakefileGenerator3.h"
  75. #if defined(CMAKE_BUILD_WITH_CMAKE)
  76. #include "cmGlobalNinjaGenerator.h"
  77. #endif
  78. #include "cmExtraCodeLiteGenerator.h"
  79. #if !defined(CMAKE_BOOT_MINGW)
  80. #include "cmExtraCodeBlocksGenerator.h"
  81. #endif
  82. #include "cmExtraKateGenerator.h"
  83. #include "cmExtraSublimeTextGenerator.h"
  84. #ifdef CMAKE_USE_KDEVELOP
  85. #include "cmGlobalKdevelopGenerator.h"
  86. #endif
  87. #ifdef CMAKE_USE_ECLIPSE
  88. #include "cmExtraEclipseCDT4Generator.h"
  89. #endif
  90. #if defined(__APPLE__)
  91. #if defined(CMAKE_BUILD_WITH_CMAKE)
  92. #include "cmGlobalXCodeGenerator.h"
  93. #define CMAKE_USE_XCODE 1
  94. #endif
  95. #include <sys/resource.h>
  96. #include <sys/time.h>
  97. #endif
  98. #include <sys/types.h>
  99. // include sys/stat.h after sys/types.h
  100. #include <sys/stat.h> // struct stat
  101. #include <algorithm>
  102. #include <cmsys/FStream.hxx>
  103. #include <cmsys/Glob.hxx>
  104. #include <cmsys/RegularExpression.hxx>
  105. #include <iostream>
  106. #include <sstream>
  107. #include <stdio.h>
  108. #include <stdlib.h>
  109. #include <string.h>
  110. #include <utility>
  111. class cmCommand;
  112. namespace {
  113. #if defined(CMAKE_BUILD_WITH_CMAKE)
  114. #ifdef CMake_HAVE_CXX_UNORDERED_MAP
  115. typedef std::unordered_map<std::string, Json::Value> JsonValueMapType;
  116. #else
  117. typedef cmsys::hash_map<std::string, Json::Value> JsonValueMapType;
  118. #endif
  119. #endif
  120. } // namespace
  121. static bool cmakeCheckStampFile(const char* stampName);
  122. static bool cmakeCheckStampList(const char* stampName);
  123. void cmWarnUnusedCliWarning(const std::string& variable, int /*unused*/,
  124. void* ctx, const char* /*unused*/,
  125. const cmMakefile* /*unused*/)
  126. {
  127. cmake* cm = reinterpret_cast<cmake*>(ctx);
  128. cm->MarkCliAsUsed(variable);
  129. }
  130. cmake::cmake()
  131. {
  132. this->Trace = false;
  133. this->TraceExpand = false;
  134. this->WarnUninitialized = false;
  135. this->WarnUnused = false;
  136. this->WarnUnusedCli = true;
  137. this->CheckSystemVars = false;
  138. this->DebugOutput = false;
  139. this->DebugTryCompile = false;
  140. this->ClearBuildSystem = false;
  141. this->FileComparison = new cmFileTimeComparison;
  142. this->State = new cmState;
  143. this->CurrentSnapshot = this->State->CreateBaseSnapshot();
  144. this->Messenger = new cmMessenger(this->State);
  145. #ifdef __APPLE__
  146. struct rlimit rlp;
  147. if (!getrlimit(RLIMIT_STACK, &rlp)) {
  148. if (rlp.rlim_cur != rlp.rlim_max) {
  149. rlp.rlim_cur = rlp.rlim_max;
  150. setrlimit(RLIMIT_STACK, &rlp);
  151. }
  152. }
  153. #endif
  154. this->GlobalGenerator = CM_NULLPTR;
  155. this->ProgressCallback = CM_NULLPTR;
  156. this->ProgressCallbackClientData = CM_NULLPTR;
  157. this->CurrentWorkingMode = NORMAL_MODE;
  158. #ifdef CMAKE_BUILD_WITH_CMAKE
  159. this->VariableWatch = new cmVariableWatch;
  160. #endif
  161. this->AddDefaultGenerators();
  162. this->AddDefaultExtraGenerators();
  163. this->AddDefaultCommands();
  164. // Make sure we can capture the build tool output.
  165. cmSystemTools::EnableVSConsoleOutput();
  166. // Set up a list of source and header extensions
  167. // these are used to find files when the extension
  168. // is not given
  169. // The "c" extension MUST precede the "C" extension.
  170. this->SourceFileExtensions.push_back("c");
  171. this->SourceFileExtensions.push_back("C");
  172. this->SourceFileExtensions.push_back("c++");
  173. this->SourceFileExtensions.push_back("cc");
  174. this->SourceFileExtensions.push_back("cpp");
  175. this->SourceFileExtensions.push_back("cxx");
  176. this->SourceFileExtensions.push_back("m");
  177. this->SourceFileExtensions.push_back("M");
  178. this->SourceFileExtensions.push_back("mm");
  179. this->HeaderFileExtensions.push_back("h");
  180. this->HeaderFileExtensions.push_back("hh");
  181. this->HeaderFileExtensions.push_back("h++");
  182. this->HeaderFileExtensions.push_back("hm");
  183. this->HeaderFileExtensions.push_back("hpp");
  184. this->HeaderFileExtensions.push_back("hxx");
  185. this->HeaderFileExtensions.push_back("in");
  186. this->HeaderFileExtensions.push_back("txx");
  187. }
  188. cmake::~cmake()
  189. {
  190. delete this->State;
  191. delete this->Messenger;
  192. if (this->GlobalGenerator) {
  193. delete this->GlobalGenerator;
  194. this->GlobalGenerator = CM_NULLPTR;
  195. }
  196. cmDeleteAll(this->Generators);
  197. #ifdef CMAKE_BUILD_WITH_CMAKE
  198. delete this->VariableWatch;
  199. #endif
  200. delete this->FileComparison;
  201. }
  202. std::string cmake::ReportCapabilities() const
  203. {
  204. std::string result;
  205. #if defined(CMAKE_BUILD_WITH_CMAKE)
  206. Json::Value obj = Json::objectValue;
  207. // Version information:
  208. Json::Value version = Json::objectValue;
  209. version["string"] = CMake_VERSION;
  210. version["major"] = CMake_VERSION_MAJOR;
  211. version["minor"] = CMake_VERSION_MINOR;
  212. version["suffix"] = CMake_VERSION_SUFFIX;
  213. version["isDirty"] = (CMake_VERSION_IS_DIRTY == 1);
  214. version["patch"] = CMake_VERSION_PATCH;
  215. obj["version"] = version;
  216. // Generators:
  217. std::vector<cmake::GeneratorInfo> generatorInfoList;
  218. this->GetRegisteredGenerators(generatorInfoList);
  219. JsonValueMapType generatorMap;
  220. for (std::vector<cmake::GeneratorInfo>::const_iterator i =
  221. generatorInfoList.begin();
  222. i != generatorInfoList.end(); ++i) {
  223. if (i->isAlias) { // skip aliases, they are there for compatibility reasons
  224. // only
  225. continue;
  226. }
  227. if (i->extraName.empty()) {
  228. Json::Value gen = Json::objectValue;
  229. gen["name"] = i->name;
  230. gen["toolsetSupport"] = i->supportsToolset;
  231. gen["platformSupport"] = i->supportsPlatform;
  232. gen["extraGenerators"] = Json::arrayValue;
  233. generatorMap[i->name] = gen;
  234. } else {
  235. Json::Value& gen = generatorMap[i->baseName];
  236. gen["extraGenerators"].append(i->extraName);
  237. }
  238. }
  239. Json::Value generators = Json::arrayValue;
  240. for (JsonValueMapType::const_iterator i = generatorMap.begin();
  241. i != generatorMap.end(); ++i) {
  242. generators.append(i->second);
  243. }
  244. obj["generators"] = generators;
  245. #if defined(HAVE_SERVER_MODE) && HAVE_SERVER_MODE
  246. obj["serverMode"] = true;
  247. #else
  248. obj["serverMode"] = false;
  249. #endif
  250. Json::FastWriter writer;
  251. result = writer.write(obj);
  252. #else
  253. result = "Not supported";
  254. #endif
  255. return result;
  256. }
  257. void cmake::CleanupCommandsAndMacros()
  258. {
  259. this->CurrentSnapshot = this->State->Reset();
  260. this->State->RemoveUserDefinedCommands();
  261. this->CurrentSnapshot.SetDefaultDefinitions();
  262. }
  263. // Parse the args
  264. bool cmake::SetCacheArgs(const std::vector<std::string>& args)
  265. {
  266. bool findPackageMode = false;
  267. for (unsigned int i = 1; i < args.size(); ++i) {
  268. std::string arg = args[i];
  269. if (arg.find("-D", 0) == 0) {
  270. std::string entry = arg.substr(2);
  271. if (entry.empty()) {
  272. ++i;
  273. if (i < args.size()) {
  274. entry = args[i];
  275. } else {
  276. cmSystemTools::Error("-D must be followed with VAR=VALUE.");
  277. return false;
  278. }
  279. }
  280. std::string var, value;
  281. cmState::CacheEntryType type = cmState::UNINITIALIZED;
  282. if (cmState::ParseCacheEntry(entry, var, value, type)) {
  283. // The value is transformed if it is a filepath for example, so
  284. // we can't compare whether the value is already in the cache until
  285. // after we call AddCacheEntry.
  286. bool haveValue = false;
  287. std::string cachedValue;
  288. if (this->WarnUnusedCli) {
  289. if (const char* v = this->State->GetInitializedCacheValue(var)) {
  290. haveValue = true;
  291. cachedValue = v;
  292. }
  293. }
  294. this->AddCacheEntry(var, value.c_str(),
  295. "No help, variable specified on the command line.",
  296. type);
  297. if (this->WarnUnusedCli) {
  298. if (!haveValue ||
  299. cachedValue != this->State->GetInitializedCacheValue(var)) {
  300. this->WatchUnusedCli(var);
  301. }
  302. }
  303. } else {
  304. std::cerr << "Parse error in command line argument: " << arg << "\n"
  305. << "Should be: VAR:type=value\n";
  306. cmSystemTools::Error("No cmake script provided.");
  307. return false;
  308. }
  309. } else if (cmHasLiteralPrefix(arg, "-W")) {
  310. std::string entry = arg.substr(2);
  311. if (entry.empty()) {
  312. ++i;
  313. if (i < args.size()) {
  314. entry = args[i];
  315. } else {
  316. cmSystemTools::Error("-W must be followed with [no-]<name>.");
  317. return false;
  318. }
  319. }
  320. std::string name;
  321. bool foundNo = false;
  322. bool foundError = false;
  323. unsigned int nameStartPosition = 0;
  324. if (entry.find("no-", nameStartPosition) == 0) {
  325. foundNo = true;
  326. nameStartPosition += 3;
  327. }
  328. if (entry.find("error=", nameStartPosition) == 0) {
  329. foundError = true;
  330. nameStartPosition += 6;
  331. }
  332. name = entry.substr(nameStartPosition);
  333. if (name.empty()) {
  334. cmSystemTools::Error("No warning name provided.");
  335. return false;
  336. }
  337. if (!foundNo && !foundError) {
  338. // -W<name>
  339. this->DiagLevels[name] = std::max(this->DiagLevels[name], DIAG_WARN);
  340. } else if (foundNo && !foundError) {
  341. // -Wno<name>
  342. this->DiagLevels[name] = DIAG_IGNORE;
  343. } else if (!foundNo && foundError) {
  344. // -Werror=<name>
  345. this->DiagLevels[name] = DIAG_ERROR;
  346. } else {
  347. // -Wno-error=<name>
  348. this->DiagLevels[name] = std::min(this->DiagLevels[name], DIAG_WARN);
  349. }
  350. } else if (arg.find("-U", 0) == 0) {
  351. std::string entryPattern = arg.substr(2);
  352. if (entryPattern.empty()) {
  353. ++i;
  354. if (i < args.size()) {
  355. entryPattern = args[i];
  356. } else {
  357. cmSystemTools::Error("-U must be followed with VAR.");
  358. return false;
  359. }
  360. }
  361. cmsys::RegularExpression regex(
  362. cmsys::Glob::PatternToRegex(entryPattern, true, true).c_str());
  363. // go through all cache entries and collect the vars which will be
  364. // removed
  365. std::vector<std::string> entriesToDelete;
  366. std::vector<std::string> cacheKeys = this->State->GetCacheEntryKeys();
  367. for (std::vector<std::string>::const_iterator it = cacheKeys.begin();
  368. it != cacheKeys.end(); ++it) {
  369. cmState::CacheEntryType t = this->State->GetCacheEntryType(*it);
  370. if (t != cmState::STATIC) {
  371. if (regex.find(it->c_str())) {
  372. entriesToDelete.push_back(*it);
  373. }
  374. }
  375. }
  376. // now remove them from the cache
  377. for (std::vector<std::string>::const_iterator currentEntry =
  378. entriesToDelete.begin();
  379. currentEntry != entriesToDelete.end(); ++currentEntry) {
  380. this->State->RemoveCacheEntry(*currentEntry);
  381. }
  382. } else if (arg.find("-C", 0) == 0) {
  383. std::string path = arg.substr(2);
  384. if (path.empty()) {
  385. ++i;
  386. if (i < args.size()) {
  387. path = args[i];
  388. } else {
  389. cmSystemTools::Error("-C must be followed by a file name.");
  390. return false;
  391. }
  392. }
  393. std::cout << "loading initial cache file " << path << "\n";
  394. this->ReadListFile(args, path.c_str());
  395. } else if (arg.find("-P", 0) == 0) {
  396. i++;
  397. if (i >= args.size()) {
  398. cmSystemTools::Error("-P must be followed by a file name.");
  399. return false;
  400. }
  401. std::string path = args[i];
  402. if (path.empty()) {
  403. cmSystemTools::Error("No cmake script provided.");
  404. return false;
  405. }
  406. this->ReadListFile(args, path.c_str());
  407. } else if (arg.find("--find-package", 0) == 0) {
  408. findPackageMode = true;
  409. }
  410. }
  411. if (findPackageMode) {
  412. return this->FindPackage(args);
  413. }
  414. return true;
  415. }
  416. void cmake::ReadListFile(const std::vector<std::string>& args,
  417. const char* path)
  418. {
  419. // if a generator was not yet created, temporarily create one
  420. cmGlobalGenerator* gg = this->GetGlobalGenerator();
  421. bool created = false;
  422. // if a generator was not specified use a generic one
  423. if (!gg) {
  424. gg = new cmGlobalGenerator(this);
  425. created = true;
  426. }
  427. // read in the list file to fill the cache
  428. if (path) {
  429. this->CurrentSnapshot = this->State->Reset();
  430. std::string homeDir = this->GetHomeDirectory();
  431. std::string homeOutputDir = this->GetHomeOutputDirectory();
  432. this->SetHomeDirectory(cmSystemTools::GetCurrentWorkingDirectory());
  433. this->SetHomeOutputDirectory(cmSystemTools::GetCurrentWorkingDirectory());
  434. cmState::Snapshot snapshot = this->GetCurrentSnapshot();
  435. snapshot.GetDirectory().SetCurrentBinary(
  436. cmSystemTools::GetCurrentWorkingDirectory());
  437. snapshot.GetDirectory().SetCurrentSource(
  438. cmSystemTools::GetCurrentWorkingDirectory());
  439. snapshot.SetDefaultDefinitions();
  440. CM_AUTO_PTR<cmMakefile> mf(new cmMakefile(gg, snapshot));
  441. if (this->GetWorkingMode() != NORMAL_MODE) {
  442. std::string file(cmSystemTools::CollapseFullPath(path));
  443. cmSystemTools::ConvertToUnixSlashes(file);
  444. mf->SetScriptModeFile(file.c_str());
  445. mf->SetArgcArgv(args);
  446. }
  447. if (!mf->ReadListFile(path)) {
  448. cmSystemTools::Error("Error processing file: ", path);
  449. }
  450. this->SetHomeDirectory(homeDir);
  451. this->SetHomeOutputDirectory(homeOutputDir);
  452. }
  453. // free generic one if generated
  454. if (created) {
  455. delete gg;
  456. }
  457. }
  458. bool cmake::FindPackage(const std::vector<std::string>& args)
  459. {
  460. this->SetHomeDirectory(cmSystemTools::GetCurrentWorkingDirectory());
  461. this->SetHomeOutputDirectory(cmSystemTools::GetCurrentWorkingDirectory());
  462. // if a generator was not yet created, temporarily create one
  463. cmGlobalGenerator* gg = new cmGlobalGenerator(this);
  464. this->SetGlobalGenerator(gg);
  465. cmState::Snapshot snapshot = this->GetCurrentSnapshot();
  466. snapshot.GetDirectory().SetCurrentBinary(
  467. cmSystemTools::GetCurrentWorkingDirectory());
  468. snapshot.GetDirectory().SetCurrentSource(
  469. cmSystemTools::GetCurrentWorkingDirectory());
  470. // read in the list file to fill the cache
  471. snapshot.SetDefaultDefinitions();
  472. cmMakefile* mf = new cmMakefile(gg, snapshot);
  473. gg->AddMakefile(mf);
  474. mf->SetArgcArgv(args);
  475. std::string systemFile = mf->GetModulesFile("CMakeFindPackageMode.cmake");
  476. mf->ReadListFile(systemFile.c_str());
  477. std::string language = mf->GetSafeDefinition("LANGUAGE");
  478. std::string mode = mf->GetSafeDefinition("MODE");
  479. std::string packageName = mf->GetSafeDefinition("NAME");
  480. bool packageFound = mf->IsOn("PACKAGE_FOUND");
  481. bool quiet = mf->IsOn("PACKAGE_QUIET");
  482. if (!packageFound) {
  483. if (!quiet) {
  484. printf("%s not found.\n", packageName.c_str());
  485. }
  486. } else if (mode == "EXIST") {
  487. if (!quiet) {
  488. printf("%s found.\n", packageName.c_str());
  489. }
  490. } else if (mode == "COMPILE") {
  491. std::string includes = mf->GetSafeDefinition("PACKAGE_INCLUDE_DIRS");
  492. std::vector<std::string> includeDirs;
  493. cmSystemTools::ExpandListArgument(includes, includeDirs);
  494. gg->CreateGenerationObjects();
  495. cmLocalGenerator* lg = gg->LocalGenerators[0];
  496. std::string includeFlags =
  497. lg->GetIncludeFlags(includeDirs, CM_NULLPTR, language);
  498. std::string definitions = mf->GetSafeDefinition("PACKAGE_DEFINITIONS");
  499. printf("%s %s\n", includeFlags.c_str(), definitions.c_str());
  500. } else if (mode == "LINK") {
  501. const char* targetName = "dummy";
  502. std::vector<std::string> srcs;
  503. cmTarget* tgt = mf->AddExecutable(targetName, srcs, true);
  504. tgt->SetProperty("LINKER_LANGUAGE", language.c_str());
  505. std::string libs = mf->GetSafeDefinition("PACKAGE_LIBRARIES");
  506. std::vector<std::string> libList;
  507. cmSystemTools::ExpandListArgument(libs, libList);
  508. for (std::vector<std::string>::const_iterator libIt = libList.begin();
  509. libIt != libList.end(); ++libIt) {
  510. mf->AddLinkLibraryForTarget(targetName, *libIt, GENERAL_LibraryType);
  511. }
  512. std::string buildType = mf->GetSafeDefinition("CMAKE_BUILD_TYPE");
  513. buildType = cmSystemTools::UpperCase(buildType);
  514. std::string linkLibs;
  515. std::string frameworkPath;
  516. std::string linkPath;
  517. std::string flags;
  518. std::string linkFlags;
  519. gg->CreateGenerationObjects();
  520. cmGeneratorTarget* gtgt = gg->FindGeneratorTarget(tgt->GetName());
  521. cmLocalGenerator* lg = gtgt->GetLocalGenerator();
  522. lg->GetTargetFlags(buildType, linkLibs, frameworkPath, linkPath, flags,
  523. linkFlags, gtgt, false);
  524. linkLibs = frameworkPath + linkPath + linkLibs;
  525. printf("%s\n", linkLibs.c_str());
  526. /* if ( use_win32 )
  527. {
  528. tgt->SetProperty("WIN32_EXECUTABLE", "ON");
  529. }
  530. if ( use_macbundle)
  531. {
  532. tgt->SetProperty("MACOSX_BUNDLE", "ON");
  533. }*/
  534. }
  535. // free generic one if generated
  536. // this->SetGlobalGenerator(0); // setting 0-pointer is not possible
  537. // delete gg; // this crashes inside the cmake instance
  538. return packageFound;
  539. }
  540. // Parse the args
  541. void cmake::SetArgs(const std::vector<std::string>& args,
  542. bool directoriesSetBefore)
  543. {
  544. bool directoriesSet = directoriesSetBefore;
  545. bool haveToolset = false;
  546. bool havePlatform = false;
  547. for (unsigned int i = 1; i < args.size(); ++i) {
  548. std::string arg = args[i];
  549. if (arg.find("-H", 0) == 0) {
  550. directoriesSet = true;
  551. std::string path = arg.substr(2);
  552. path = cmSystemTools::CollapseFullPath(path);
  553. cmSystemTools::ConvertToUnixSlashes(path);
  554. this->SetHomeDirectory(path);
  555. } else if (arg.find("-S", 0) == 0) {
  556. // There is no local generate anymore. Ignore -S option.
  557. } else if (arg.find("-O", 0) == 0) {
  558. // There is no local generate anymore. Ignore -O option.
  559. } else if (arg.find("-B", 0) == 0) {
  560. directoriesSet = true;
  561. std::string path = arg.substr(2);
  562. path = cmSystemTools::CollapseFullPath(path);
  563. cmSystemTools::ConvertToUnixSlashes(path);
  564. this->SetHomeOutputDirectory(path);
  565. } else if ((i < args.size() - 2) &&
  566. (arg.find("--check-build-system", 0) == 0)) {
  567. this->CheckBuildSystemArgument = args[++i];
  568. this->ClearBuildSystem = (atoi(args[++i].c_str()) > 0);
  569. } else if ((i < args.size() - 1) &&
  570. (arg.find("--check-stamp-file", 0) == 0)) {
  571. this->CheckStampFile = args[++i];
  572. } else if ((i < args.size() - 1) &&
  573. (arg.find("--check-stamp-list", 0) == 0)) {
  574. this->CheckStampList = args[++i];
  575. }
  576. #if defined(CMAKE_HAVE_VS_GENERATORS)
  577. else if ((i < args.size() - 1) &&
  578. (arg.find("--vs-solution-file", 0) == 0)) {
  579. this->VSSolutionFile = args[++i];
  580. }
  581. #endif
  582. else if (arg.find("-D", 0) == 0) {
  583. // skip for now
  584. } else if (arg.find("-U", 0) == 0) {
  585. // skip for now
  586. } else if (arg.find("-C", 0) == 0) {
  587. // skip for now
  588. } else if (arg.find("-P", 0) == 0) {
  589. // skip for now
  590. i++;
  591. } else if (arg.find("--find-package", 0) == 0) {
  592. // skip for now
  593. i++;
  594. } else if (arg.find("-W", 0) == 0) {
  595. // skip for now
  596. } else if (arg.find("--graphviz=", 0) == 0) {
  597. std::string path = arg.substr(strlen("--graphviz="));
  598. path = cmSystemTools::CollapseFullPath(path);
  599. cmSystemTools::ConvertToUnixSlashes(path);
  600. this->GraphVizFile = path;
  601. if (this->GraphVizFile.empty()) {
  602. cmSystemTools::Error("No file specified for --graphviz");
  603. }
  604. } else if (arg.find("--debug-trycompile", 0) == 0) {
  605. std::cout << "debug trycompile on\n";
  606. this->DebugTryCompileOn();
  607. } else if (arg.find("--debug-output", 0) == 0) {
  608. std::cout << "Running with debug output on.\n";
  609. this->SetDebugOutputOn(true);
  610. } else if (arg.find("--trace-expand", 0) == 0) {
  611. std::cout << "Running with expanded trace output on.\n";
  612. this->SetTrace(true);
  613. this->SetTraceExpand(true);
  614. } else if (arg.find("--trace-source=", 0) == 0) {
  615. std::string file = arg.substr(strlen("--trace-source="));
  616. cmSystemTools::ConvertToUnixSlashes(file);
  617. this->AddTraceSource(file);
  618. this->SetTrace(true);
  619. } else if (arg.find("--trace", 0) == 0) {
  620. std::cout << "Running with trace output on.\n";
  621. this->SetTrace(true);
  622. this->SetTraceExpand(false);
  623. } else if (arg.find("--warn-uninitialized", 0) == 0) {
  624. std::cout << "Warn about uninitialized values.\n";
  625. this->SetWarnUninitialized(true);
  626. } else if (arg.find("--warn-unused-vars", 0) == 0) {
  627. std::cout << "Finding unused variables.\n";
  628. this->SetWarnUnused(true);
  629. } else if (arg.find("--no-warn-unused-cli", 0) == 0) {
  630. std::cout << "Not searching for unused variables given on the "
  631. << "command line.\n";
  632. this->SetWarnUnusedCli(false);
  633. } else if (arg.find("--check-system-vars", 0) == 0) {
  634. std::cout << "Also check system files when warning about unused and "
  635. << "uninitialized variables.\n";
  636. this->SetCheckSystemVars(true);
  637. } else if (arg.find("-A", 0) == 0) {
  638. std::string value = arg.substr(2);
  639. if (value.empty()) {
  640. ++i;
  641. if (i >= args.size()) {
  642. cmSystemTools::Error("No platform specified for -A");
  643. return;
  644. }
  645. value = args[i];
  646. }
  647. if (havePlatform) {
  648. cmSystemTools::Error("Multiple -A options not allowed");
  649. return;
  650. }
  651. this->GeneratorPlatform = value;
  652. havePlatform = true;
  653. } else if (arg.find("-T", 0) == 0) {
  654. std::string value = arg.substr(2);
  655. if (value.empty()) {
  656. ++i;
  657. if (i >= args.size()) {
  658. cmSystemTools::Error("No toolset specified for -T");
  659. return;
  660. }
  661. value = args[i];
  662. }
  663. if (haveToolset) {
  664. cmSystemTools::Error("Multiple -T options not allowed");
  665. return;
  666. }
  667. this->GeneratorToolset = value;
  668. haveToolset = true;
  669. } else if (arg.find("-G", 0) == 0) {
  670. std::string value = arg.substr(2);
  671. if (value.empty()) {
  672. ++i;
  673. if (i >= args.size()) {
  674. cmSystemTools::Error("No generator specified for -G");
  675. this->PrintGeneratorList();
  676. return;
  677. }
  678. value = args[i];
  679. }
  680. cmGlobalGenerator* gen = this->CreateGlobalGenerator(value);
  681. if (!gen) {
  682. cmSystemTools::Error("Could not create named generator ",
  683. value.c_str());
  684. this->PrintGeneratorList();
  685. } else {
  686. this->SetGlobalGenerator(gen);
  687. }
  688. }
  689. // no option assume it is the path to the source
  690. else {
  691. directoriesSet = true;
  692. this->SetDirectoriesFromFile(arg.c_str());
  693. }
  694. }
  695. if (!directoriesSet) {
  696. this->SetHomeOutputDirectory(cmSystemTools::GetCurrentWorkingDirectory());
  697. this->SetHomeDirectory(cmSystemTools::GetCurrentWorkingDirectory());
  698. }
  699. }
  700. void cmake::SetDirectoriesFromFile(const char* arg)
  701. {
  702. // Check if the argument refers to a CMakeCache.txt or
  703. // CMakeLists.txt file.
  704. std::string listPath;
  705. std::string cachePath;
  706. bool argIsFile = false;
  707. if (cmSystemTools::FileIsDirectory(arg)) {
  708. std::string path = cmSystemTools::CollapseFullPath(arg);
  709. cmSystemTools::ConvertToUnixSlashes(path);
  710. std::string cacheFile = path;
  711. cacheFile += "/CMakeCache.txt";
  712. std::string listFile = path;
  713. listFile += "/CMakeLists.txt";
  714. if (cmSystemTools::FileExists(cacheFile.c_str())) {
  715. cachePath = path;
  716. }
  717. if (cmSystemTools::FileExists(listFile.c_str())) {
  718. listPath = path;
  719. }
  720. } else if (cmSystemTools::FileExists(arg)) {
  721. argIsFile = true;
  722. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  723. std::string name = cmSystemTools::GetFilenameName(fullPath);
  724. name = cmSystemTools::LowerCase(name);
  725. if (name == "cmakecache.txt") {
  726. cachePath = cmSystemTools::GetFilenamePath(fullPath);
  727. } else if (name == "cmakelists.txt") {
  728. listPath = cmSystemTools::GetFilenamePath(fullPath);
  729. }
  730. } else {
  731. // Specified file or directory does not exist. Try to set things
  732. // up to produce a meaningful error message.
  733. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  734. std::string name = cmSystemTools::GetFilenameName(fullPath);
  735. name = cmSystemTools::LowerCase(name);
  736. if (name == "cmakecache.txt" || name == "cmakelists.txt") {
  737. argIsFile = true;
  738. listPath = cmSystemTools::GetFilenamePath(fullPath);
  739. } else {
  740. listPath = fullPath;
  741. }
  742. }
  743. // If there is a CMakeCache.txt file, use its settings.
  744. if (!cachePath.empty()) {
  745. if (this->LoadCache(cachePath)) {
  746. const char* existingValue =
  747. this->State->GetCacheEntryValue("CMAKE_HOME_DIRECTORY");
  748. if (existingValue) {
  749. this->SetHomeOutputDirectory(cachePath);
  750. this->SetHomeDirectory(existingValue);
  751. return;
  752. }
  753. }
  754. }
  755. // If there is a CMakeLists.txt file, use it as the source tree.
  756. if (!listPath.empty()) {
  757. this->SetHomeDirectory(listPath);
  758. if (argIsFile) {
  759. // Source CMakeLists.txt file given. It was probably dropped
  760. // onto the executable in a GUI. Default to an in-source build.
  761. this->SetHomeOutputDirectory(listPath);
  762. } else {
  763. // Source directory given on command line. Use current working
  764. // directory as build tree.
  765. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  766. this->SetHomeOutputDirectory(cwd);
  767. }
  768. return;
  769. }
  770. // We didn't find a CMakeLists.txt or CMakeCache.txt file from the
  771. // argument. Assume it is the path to the source tree, and use the
  772. // current working directory as the build tree.
  773. std::string full = cmSystemTools::CollapseFullPath(arg);
  774. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  775. this->SetHomeDirectory(full);
  776. this->SetHomeOutputDirectory(cwd);
  777. }
  778. // at the end of this CMAKE_ROOT and CMAKE_COMMAND should be added to the
  779. // cache
  780. int cmake::AddCMakePaths()
  781. {
  782. // Save the value in the cache
  783. this->AddCacheEntry("CMAKE_COMMAND",
  784. cmSystemTools::GetCMakeCommand().c_str(),
  785. "Path to CMake executable.", cmState::INTERNAL);
  786. #ifdef CMAKE_BUILD_WITH_CMAKE
  787. this->AddCacheEntry("CMAKE_CTEST_COMMAND",
  788. cmSystemTools::GetCTestCommand().c_str(),
  789. "Path to ctest program executable.", cmState::INTERNAL);
  790. this->AddCacheEntry("CMAKE_CPACK_COMMAND",
  791. cmSystemTools::GetCPackCommand().c_str(),
  792. "Path to cpack program executable.", cmState::INTERNAL);
  793. #endif
  794. if (!cmSystemTools::FileExists(
  795. (cmSystemTools::GetCMakeRoot() + "/Modules/CMake.cmake").c_str())) {
  796. // couldn't find modules
  797. cmSystemTools::Error(
  798. "Could not find CMAKE_ROOT !!!\n"
  799. "CMake has most likely not been installed correctly.\n"
  800. "Modules directory not found in\n",
  801. cmSystemTools::GetCMakeRoot().c_str());
  802. return 0;
  803. }
  804. this->AddCacheEntry("CMAKE_ROOT", cmSystemTools::GetCMakeRoot().c_str(),
  805. "Path to CMake installation.", cmState::INTERNAL);
  806. return 1;
  807. }
  808. void cmake::AddDefaultExtraGenerators()
  809. {
  810. #if defined(CMAKE_BUILD_WITH_CMAKE)
  811. this->ExtraGenerators.push_back(cmExtraCodeBlocksGenerator::GetFactory());
  812. this->ExtraGenerators.push_back(cmExtraCodeLiteGenerator::GetFactory());
  813. this->ExtraGenerators.push_back(cmExtraSublimeTextGenerator::GetFactory());
  814. this->ExtraGenerators.push_back(cmExtraKateGenerator::GetFactory());
  815. #ifdef CMAKE_USE_ECLIPSE
  816. this->ExtraGenerators.push_back(cmExtraEclipseCDT4Generator::GetFactory());
  817. #endif
  818. #ifdef CMAKE_USE_KDEVELOP
  819. this->ExtraGenerators.push_back(cmGlobalKdevelopGenerator::GetFactory());
  820. #endif
  821. #endif
  822. }
  823. void cmake::GetRegisteredGenerators(
  824. std::vector<GeneratorInfo>& generators) const
  825. {
  826. for (RegisteredGeneratorsVector::const_iterator i = this->Generators.begin(),
  827. e = this->Generators.end();
  828. i != e; ++i) {
  829. std::vector<std::string> names;
  830. (*i)->GetGenerators(names);
  831. for (size_t j = 0; j < names.size(); ++j) {
  832. GeneratorInfo info;
  833. info.supportsToolset = (*i)->SupportsToolset();
  834. info.supportsPlatform = (*i)->SupportsPlatform();
  835. info.name = names[j];
  836. info.baseName = names[j];
  837. info.isAlias = false;
  838. generators.push_back(info);
  839. }
  840. }
  841. for (RegisteredExtraGeneratorsVector::const_iterator
  842. i = this->ExtraGenerators.begin(),
  843. e = this->ExtraGenerators.end();
  844. i != e; ++i) {
  845. const std::vector<std::string> genList =
  846. (*i)->GetSupportedGlobalGenerators();
  847. for (std::vector<std::string>::const_iterator gen = genList.begin();
  848. gen != genList.end(); ++gen) {
  849. GeneratorInfo info;
  850. info.name = cmExternalMakefileProjectGenerator::CreateFullGeneratorName(
  851. (*i)->GetName(), *gen);
  852. info.baseName = *gen;
  853. info.extraName = (*i)->GetName();
  854. info.supportsPlatform = false;
  855. info.supportsToolset = false;
  856. info.isAlias = false;
  857. generators.push_back(info);
  858. }
  859. for (std::vector<std::string>::const_iterator a = (*i)->Aliases.begin();
  860. a != (*i)->Aliases.end(); ++a) {
  861. GeneratorInfo info;
  862. info.name = *a;
  863. if (!genList.empty()) {
  864. info.baseName = genList.at(0);
  865. }
  866. info.extraName = (*i)->GetName();
  867. info.supportsPlatform = false;
  868. info.supportsToolset = false;
  869. info.isAlias = true;
  870. generators.push_back(info);
  871. }
  872. }
  873. }
  874. static std::pair<cmExternalMakefileProjectGenerator*, std::string>
  875. createExtraGenerator(
  876. const std::vector<cmExternalMakefileProjectGeneratorFactory*>& in,
  877. const std::string& name)
  878. {
  879. for (std::vector<cmExternalMakefileProjectGeneratorFactory*>::const_iterator
  880. i = in.begin();
  881. i != in.end(); ++i) {
  882. const std::vector<std::string> generators =
  883. (*i)->GetSupportedGlobalGenerators();
  884. if ((*i)->GetName() == name) { // Match aliases
  885. return std::make_pair((*i)->CreateExternalMakefileProjectGenerator(),
  886. generators.at(0));
  887. }
  888. for (std::vector<std::string>::const_iterator g = generators.begin();
  889. g != generators.end(); ++g) {
  890. const std::string fullName =
  891. cmExternalMakefileProjectGenerator::CreateFullGeneratorName(
  892. *g, (*i)->GetName());
  893. if (fullName == name) {
  894. return std::make_pair((*i)->CreateExternalMakefileProjectGenerator(),
  895. *g);
  896. }
  897. }
  898. }
  899. return std::make_pair(
  900. static_cast<cmExternalMakefileProjectGenerator*>(CM_NULLPTR), name);
  901. }
  902. cmGlobalGenerator* cmake::CreateGlobalGenerator(const std::string& gname)
  903. {
  904. std::pair<cmExternalMakefileProjectGenerator*, std::string> extra =
  905. createExtraGenerator(this->ExtraGenerators, gname);
  906. cmExternalMakefileProjectGenerator* extraGenerator = extra.first;
  907. const std::string name = extra.second;
  908. cmGlobalGenerator* generator = CM_NULLPTR;
  909. for (RegisteredGeneratorsVector::const_iterator i = this->Generators.begin();
  910. i != this->Generators.end(); ++i) {
  911. generator = (*i)->CreateGlobalGenerator(name, this);
  912. if (generator) {
  913. break;
  914. }
  915. }
  916. if (generator) {
  917. generator->SetExternalMakefileProjectGenerator(extraGenerator);
  918. } else {
  919. delete extraGenerator;
  920. }
  921. return generator;
  922. }
  923. void cmake::SetHomeDirectory(const std::string& dir)
  924. {
  925. this->State->SetSourceDirectory(dir);
  926. if (this->CurrentSnapshot.IsValid()) {
  927. this->CurrentSnapshot.SetDefinition("CMAKE_SOURCE_DIR", dir);
  928. }
  929. }
  930. const char* cmake::GetHomeDirectory() const
  931. {
  932. return this->State->GetSourceDirectory();
  933. }
  934. void cmake::SetHomeOutputDirectory(const std::string& dir)
  935. {
  936. this->State->SetBinaryDirectory(dir);
  937. if (this->CurrentSnapshot.IsValid()) {
  938. this->CurrentSnapshot.SetDefinition("CMAKE_BINARY_DIR", dir);
  939. }
  940. }
  941. const char* cmake::GetHomeOutputDirectory() const
  942. {
  943. return this->State->GetBinaryDirectory();
  944. }
  945. void cmake::SetGlobalGenerator(cmGlobalGenerator* gg)
  946. {
  947. if (!gg) {
  948. cmSystemTools::Error("Error SetGlobalGenerator called with null");
  949. return;
  950. }
  951. // delete the old generator
  952. if (this->GlobalGenerator) {
  953. delete this->GlobalGenerator;
  954. // restore the original environment variables CXX and CC
  955. // Restore CC
  956. std::string env = "CC=";
  957. if (!this->CCEnvironment.empty()) {
  958. env += this->CCEnvironment;
  959. }
  960. cmSystemTools::PutEnv(env);
  961. env = "CXX=";
  962. if (!this->CXXEnvironment.empty()) {
  963. env += this->CXXEnvironment;
  964. }
  965. cmSystemTools::PutEnv(env);
  966. }
  967. // set the new
  968. this->GlobalGenerator = gg;
  969. // set the global flag for unix style paths on cmSystemTools as soon as
  970. // the generator is set. This allows gmake to be used on windows.
  971. cmSystemTools::SetForceUnixPaths(this->GlobalGenerator->GetForceUnixPaths());
  972. // Save the environment variables CXX and CC
  973. if (!cmSystemTools::GetEnv("CXX", this->CXXEnvironment)) {
  974. this->CXXEnvironment = "";
  975. }
  976. if (!cmSystemTools::GetEnv("CC", this->CCEnvironment)) {
  977. this->CCEnvironment = "";
  978. }
  979. }
  980. int cmake::DoPreConfigureChecks()
  981. {
  982. // Make sure the Source directory contains a CMakeLists.txt file.
  983. std::string srcList = this->GetHomeDirectory();
  984. srcList += "/CMakeLists.txt";
  985. if (!cmSystemTools::FileExists(srcList.c_str())) {
  986. std::ostringstream err;
  987. if (cmSystemTools::FileIsDirectory(this->GetHomeDirectory())) {
  988. err << "The source directory \"" << this->GetHomeDirectory()
  989. << "\" does not appear to contain CMakeLists.txt.\n";
  990. } else if (cmSystemTools::FileExists(this->GetHomeDirectory())) {
  991. err << "The source directory \"" << this->GetHomeDirectory()
  992. << "\" is a file, not a directory.\n";
  993. } else {
  994. err << "The source directory \"" << this->GetHomeDirectory()
  995. << "\" does not exist.\n";
  996. }
  997. err << "Specify --help for usage, or press the help button on the CMake "
  998. "GUI.";
  999. cmSystemTools::Error(err.str().c_str());
  1000. return -2;
  1001. }
  1002. // do a sanity check on some values
  1003. if (this->State->GetInitializedCacheValue("CMAKE_HOME_DIRECTORY")) {
  1004. std::string cacheStart =
  1005. this->State->GetInitializedCacheValue("CMAKE_HOME_DIRECTORY");
  1006. cacheStart += "/CMakeLists.txt";
  1007. std::string currentStart = this->GetHomeDirectory();
  1008. currentStart += "/CMakeLists.txt";
  1009. if (!cmSystemTools::SameFile(cacheStart, currentStart)) {
  1010. std::string message = "The source \"";
  1011. message += currentStart;
  1012. message += "\" does not match the source \"";
  1013. message += cacheStart;
  1014. message += "\" used to generate cache. ";
  1015. message += "Re-run cmake with a different source directory.";
  1016. cmSystemTools::Error(message.c_str());
  1017. return -2;
  1018. }
  1019. } else {
  1020. return 0;
  1021. }
  1022. return 1;
  1023. }
  1024. struct SaveCacheEntry
  1025. {
  1026. std::string key;
  1027. std::string value;
  1028. std::string help;
  1029. cmState::CacheEntryType type;
  1030. };
  1031. int cmake::HandleDeleteCacheVariables(const std::string& var)
  1032. {
  1033. std::vector<std::string> argsSplit;
  1034. cmSystemTools::ExpandListArgument(std::string(var), argsSplit, true);
  1035. // erase the property to avoid infinite recursion
  1036. this->State->SetGlobalProperty("__CMAKE_DELETE_CACHE_CHANGE_VARS_", "");
  1037. if (this->State->GetIsInTryCompile()) {
  1038. return 0;
  1039. }
  1040. std::vector<SaveCacheEntry> saved;
  1041. std::ostringstream warning;
  1042. /* clang-format off */
  1043. warning
  1044. << "You have changed variables that require your cache to be deleted.\n"
  1045. << "Configure will be re-run and you may have to reset some variables.\n"
  1046. << "The following variables have changed:\n";
  1047. /* clang-format on */
  1048. for (std::vector<std::string>::iterator i = argsSplit.begin();
  1049. i != argsSplit.end(); ++i) {
  1050. SaveCacheEntry save;
  1051. save.key = *i;
  1052. warning << *i << "= ";
  1053. i++;
  1054. save.value = *i;
  1055. warning << *i << "\n";
  1056. const char* existingValue = this->State->GetCacheEntryValue(save.key);
  1057. if (existingValue) {
  1058. save.type = this->State->GetCacheEntryType(save.key);
  1059. if (const char* help =
  1060. this->State->GetCacheEntryProperty(save.key, "HELPSTRING")) {
  1061. save.help = help;
  1062. }
  1063. }
  1064. saved.push_back(save);
  1065. }
  1066. // remove the cache
  1067. this->DeleteCache(this->GetHomeOutputDirectory());
  1068. // load the empty cache
  1069. this->LoadCache();
  1070. // restore the changed compilers
  1071. for (std::vector<SaveCacheEntry>::iterator i = saved.begin();
  1072. i != saved.end(); ++i) {
  1073. this->AddCacheEntry(i->key, i->value.c_str(), i->help.c_str(), i->type);
  1074. }
  1075. cmSystemTools::Message(warning.str().c_str());
  1076. // avoid reconfigure if there were errors
  1077. if (!cmSystemTools::GetErrorOccuredFlag()) {
  1078. // re-run configure
  1079. return this->Configure();
  1080. }
  1081. return 0;
  1082. }
  1083. int cmake::Configure()
  1084. {
  1085. DiagLevel diagLevel;
  1086. if (this->DiagLevels.count("deprecated") == 1) {
  1087. diagLevel = this->DiagLevels["deprecated"];
  1088. if (diagLevel == DIAG_IGNORE) {
  1089. this->SetSuppressDeprecatedWarnings(true);
  1090. this->SetDeprecatedWarningsAsErrors(false);
  1091. } else if (diagLevel == DIAG_WARN) {
  1092. this->SetSuppressDeprecatedWarnings(false);
  1093. this->SetDeprecatedWarningsAsErrors(false);
  1094. } else if (diagLevel == DIAG_ERROR) {
  1095. this->SetSuppressDeprecatedWarnings(false);
  1096. this->SetDeprecatedWarningsAsErrors(true);
  1097. }
  1098. }
  1099. if (this->DiagLevels.count("dev") == 1) {
  1100. bool setDeprecatedVariables = false;
  1101. const char* cachedWarnDeprecated =
  1102. this->State->GetCacheEntryValue("CMAKE_WARN_DEPRECATED");
  1103. const char* cachedErrorDeprecated =
  1104. this->State->GetCacheEntryValue("CMAKE_ERROR_DEPRECATED");
  1105. // don't overwrite deprecated warning setting from a previous invocation
  1106. if (!cachedWarnDeprecated && !cachedErrorDeprecated) {
  1107. setDeprecatedVariables = true;
  1108. }
  1109. diagLevel = this->DiagLevels["dev"];
  1110. if (diagLevel == DIAG_IGNORE) {
  1111. this->SetSuppressDevWarnings(true);
  1112. this->SetDevWarningsAsErrors(false);
  1113. if (setDeprecatedVariables) {
  1114. this->SetSuppressDeprecatedWarnings(true);
  1115. this->SetDeprecatedWarningsAsErrors(false);
  1116. }
  1117. } else if (diagLevel == DIAG_WARN) {
  1118. this->SetSuppressDevWarnings(false);
  1119. this->SetDevWarningsAsErrors(false);
  1120. if (setDeprecatedVariables) {
  1121. this->SetSuppressDeprecatedWarnings(false);
  1122. this->SetDeprecatedWarningsAsErrors(false);
  1123. }
  1124. } else if (diagLevel == DIAG_ERROR) {
  1125. this->SetSuppressDevWarnings(false);
  1126. this->SetDevWarningsAsErrors(true);
  1127. if (setDeprecatedVariables) {
  1128. this->SetSuppressDeprecatedWarnings(false);
  1129. this->SetDeprecatedWarningsAsErrors(true);
  1130. }
  1131. }
  1132. }
  1133. int ret = this->ActualConfigure();
  1134. const char* delCacheVars =
  1135. this->State->GetGlobalProperty("__CMAKE_DELETE_CACHE_CHANGE_VARS_");
  1136. if (delCacheVars && delCacheVars[0] != 0) {
  1137. return this->HandleDeleteCacheVariables(delCacheVars);
  1138. }
  1139. return ret;
  1140. }
  1141. int cmake::ActualConfigure()
  1142. {
  1143. // Construct right now our path conversion table before it's too late:
  1144. this->UpdateConversionPathTable();
  1145. this->CleanupCommandsAndMacros();
  1146. int res = 0;
  1147. if (this->GetWorkingMode() == NORMAL_MODE) {
  1148. res = this->DoPreConfigureChecks();
  1149. }
  1150. if (res < 0) {
  1151. return -2;
  1152. }
  1153. if (!res) {
  1154. this->AddCacheEntry(
  1155. "CMAKE_HOME_DIRECTORY", this->GetHomeDirectory(),
  1156. "Source directory with the top level CMakeLists.txt file for this "
  1157. "project",
  1158. cmState::INTERNAL);
  1159. }
  1160. // no generator specified on the command line
  1161. if (!this->GlobalGenerator) {
  1162. const char* genName =
  1163. this->State->GetInitializedCacheValue("CMAKE_GENERATOR");
  1164. const char* extraGenName =
  1165. this->State->GetInitializedCacheValue("CMAKE_EXTRA_GENERATOR");
  1166. if (genName) {
  1167. std::string fullName =
  1168. cmExternalMakefileProjectGenerator::CreateFullGeneratorName(
  1169. genName, extraGenName ? extraGenName : "");
  1170. this->GlobalGenerator = this->CreateGlobalGenerator(fullName);
  1171. }
  1172. if (this->GlobalGenerator) {
  1173. // set the global flag for unix style paths on cmSystemTools as
  1174. // soon as the generator is set. This allows gmake to be used
  1175. // on windows.
  1176. cmSystemTools::SetForceUnixPaths(
  1177. this->GlobalGenerator->GetForceUnixPaths());
  1178. } else {
  1179. #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW)
  1180. std::string installedCompiler;
  1181. // Try to find the newest VS installed on the computer and
  1182. // use that as a default if -G is not specified
  1183. const std::string vsregBase =
  1184. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\";
  1185. std::vector<std::string> vsVerions;
  1186. vsVerions.push_back("VisualStudio\\");
  1187. vsVerions.push_back("VCExpress\\");
  1188. vsVerions.push_back("WDExpress\\");
  1189. struct VSRegistryEntryName
  1190. {
  1191. const char* MSVersion;
  1192. const char* GeneratorName;
  1193. };
  1194. VSRegistryEntryName version[] = {
  1195. /* clang-format needs this comment to break after the opening brace */
  1196. { "7.1", "Visual Studio 7 .NET 2003" },
  1197. { "8.0", "Visual Studio 8 2005" },
  1198. { "9.0", "Visual Studio 9 2008" },
  1199. { "10.0", "Visual Studio 10 2010" },
  1200. { "11.0", "Visual Studio 11 2012" },
  1201. { "12.0", "Visual Studio 12 2013" },
  1202. { "14.0", "Visual Studio 14 2015" },
  1203. { "15.0", "Visual Studio 15" },
  1204. { 0, 0 }
  1205. };
  1206. for (int i = 0; version[i].MSVersion != 0; i++) {
  1207. for (size_t b = 0; b < vsVerions.size(); b++) {
  1208. std::string reg = vsregBase + vsVerions[b] + version[i].MSVersion;
  1209. reg += ";InstallDir]";
  1210. cmSystemTools::ExpandRegistryValues(reg, cmSystemTools::KeyWOW64_32);
  1211. if (!(reg == "/registry")) {
  1212. installedCompiler = version[i].GeneratorName;
  1213. break;
  1214. }
  1215. }
  1216. }
  1217. cmGlobalGenerator* gen =
  1218. this->CreateGlobalGenerator(installedCompiler.c_str());
  1219. if (!gen) {
  1220. gen = new cmGlobalNMakeMakefileGenerator(this);
  1221. }
  1222. this->SetGlobalGenerator(gen);
  1223. std::cout << "-- Building for: " << gen->GetName() << "\n";
  1224. #else
  1225. this->SetGlobalGenerator(new cmGlobalUnixMakefileGenerator3(this));
  1226. #endif
  1227. }
  1228. if (!this->GlobalGenerator) {
  1229. cmSystemTools::Error("Could not create generator");
  1230. return -1;
  1231. }
  1232. }
  1233. const char* genName =
  1234. this->State->GetInitializedCacheValue("CMAKE_GENERATOR");
  1235. if (genName) {
  1236. if (!this->GlobalGenerator->MatchesGeneratorName(genName)) {
  1237. std::string message = "Error: generator : ";
  1238. message += this->GlobalGenerator->GetName();
  1239. message += "\nDoes not match the generator used previously: ";
  1240. message += genName;
  1241. message += "\nEither remove the CMakeCache.txt file and CMakeFiles "
  1242. "directory or choose a different binary directory.";
  1243. cmSystemTools::Error(message.c_str());
  1244. return -2;
  1245. }
  1246. }
  1247. if (!this->State->GetInitializedCacheValue("CMAKE_GENERATOR")) {
  1248. this->AddCacheEntry("CMAKE_GENERATOR",
  1249. this->GlobalGenerator->GetName().c_str(),
  1250. "Name of generator.", cmState::INTERNAL);
  1251. this->AddCacheEntry("CMAKE_EXTRA_GENERATOR",
  1252. this->GlobalGenerator->GetExtraGeneratorName().c_str(),
  1253. "Name of external makefile project generator.",
  1254. cmState::INTERNAL);
  1255. }
  1256. if (const char* platformName =
  1257. this->State->GetInitializedCacheValue("CMAKE_GENERATOR_PLATFORM")) {
  1258. if (this->GeneratorPlatform.empty()) {
  1259. this->GeneratorPlatform = platformName;
  1260. } else if (this->GeneratorPlatform != platformName) {
  1261. std::string message = "Error: generator platform: ";
  1262. message += this->GeneratorPlatform;
  1263. message += "\nDoes not match the platform used previously: ";
  1264. message += platformName;
  1265. message += "\nEither remove the CMakeCache.txt file and CMakeFiles "
  1266. "directory or choose a different binary directory.";
  1267. cmSystemTools::Error(message.c_str());
  1268. return -2;
  1269. }
  1270. } else {
  1271. this->AddCacheEntry("CMAKE_GENERATOR_PLATFORM",
  1272. this->GeneratorPlatform.c_str(),
  1273. "Name of generator platform.", cmState::INTERNAL);
  1274. }
  1275. if (const char* tsName =
  1276. this->State->GetInitializedCacheValue("CMAKE_GENERATOR_TOOLSET")) {
  1277. if (this->GeneratorToolset.empty()) {
  1278. this->GeneratorToolset = tsName;
  1279. } else if (this->GeneratorToolset != tsName) {
  1280. std::string message = "Error: generator toolset: ";
  1281. message += this->GeneratorToolset;
  1282. message += "\nDoes not match the toolset used previously: ";
  1283. message += tsName;
  1284. message += "\nEither remove the CMakeCache.txt file and CMakeFiles "
  1285. "directory or choose a different binary directory.";
  1286. cmSystemTools::Error(message.c_str());
  1287. return -2;
  1288. }
  1289. } else {
  1290. this->AddCacheEntry("CMAKE_GENERATOR_TOOLSET",
  1291. this->GeneratorToolset.c_str(),
  1292. "Name of generator toolset.", cmState::INTERNAL);
  1293. }
  1294. // reset any system configuration information, except for when we are
  1295. // InTryCompile. With TryCompile the system info is taken from the parent's
  1296. // info to save time
  1297. if (!this->State->GetIsInTryCompile()) {
  1298. this->GlobalGenerator->ClearEnabledLanguages();
  1299. this->TruncateOutputLog("CMakeOutput.log");
  1300. this->TruncateOutputLog("CMakeError.log");
  1301. }
  1302. // actually do the configure
  1303. this->GlobalGenerator->Configure();
  1304. // Before saving the cache
  1305. // if the project did not define one of the entries below, add them now
  1306. // so users can edit the values in the cache:
  1307. // We used to always present LIBRARY_OUTPUT_PATH and
  1308. // EXECUTABLE_OUTPUT_PATH. They are now documented as old-style and
  1309. // should no longer be used. Therefore we present them only if the
  1310. // project requires compatibility with CMake 2.4. We detect this
  1311. // here by looking for the old CMAKE_BACKWARDS_COMPATIBILITY
  1312. // variable created when CMP0001 is not set to NEW.
  1313. if (this->State->GetInitializedCacheValue("CMAKE_BACKWARDS_COMPATIBILITY")) {
  1314. if (!this->State->GetInitializedCacheValue("LIBRARY_OUTPUT_PATH")) {
  1315. this->AddCacheEntry(
  1316. "LIBRARY_OUTPUT_PATH", "",
  1317. "Single output directory for building all libraries.", cmState::PATH);
  1318. }
  1319. if (!this->State->GetInitializedCacheValue("EXECUTABLE_OUTPUT_PATH")) {
  1320. this->AddCacheEntry(
  1321. "EXECUTABLE_OUTPUT_PATH", "",
  1322. "Single output directory for building all executables.",
  1323. cmState::PATH);
  1324. }
  1325. }
  1326. cmMakefile* mf = this->GlobalGenerator->GetMakefiles()[0];
  1327. if (mf->IsOn("CTEST_USE_LAUNCHERS") &&
  1328. !this->State->GetGlobalProperty("RULE_LAUNCH_COMPILE")) {
  1329. cmSystemTools::Error(
  1330. "CTEST_USE_LAUNCHERS is enabled, but the "
  1331. "RULE_LAUNCH_COMPILE global property is not defined.\n"
  1332. "Did you forget to include(CTest) in the toplevel "
  1333. "CMakeLists.txt ?");
  1334. }
  1335. // only save the cache if there were no fatal errors
  1336. if (this->GetWorkingMode() == NORMAL_MODE) {
  1337. this->SaveCache(this->GetHomeOutputDirectory());
  1338. }
  1339. if (cmSystemTools::GetErrorOccuredFlag()) {
  1340. return -1;
  1341. }
  1342. return 0;
  1343. }
  1344. void cmake::PreLoadCMakeFiles()
  1345. {
  1346. std::vector<std::string> args;
  1347. std::string pre_load = this->GetHomeDirectory();
  1348. if (!pre_load.empty()) {
  1349. pre_load += "/PreLoad.cmake";
  1350. if (cmSystemTools::FileExists(pre_load.c_str())) {
  1351. this->ReadListFile(args, pre_load.c_str());
  1352. }
  1353. }
  1354. pre_load = this->GetHomeOutputDirectory();
  1355. if (!pre_load.empty()) {
  1356. pre_load += "/PreLoad.cmake";
  1357. if (cmSystemTools::FileExists(pre_load.c_str())) {
  1358. this->ReadListFile(args, pre_load.c_str());
  1359. }
  1360. }
  1361. }
  1362. // handle a command line invocation
  1363. int cmake::Run(const std::vector<std::string>& args, bool noconfigure)
  1364. {
  1365. // Process the arguments
  1366. this->SetArgs(args);
  1367. if (cmSystemTools::GetErrorOccuredFlag()) {
  1368. return -1;
  1369. }
  1370. // If we are given a stamp list file check if it is really out of date.
  1371. if (!this->CheckStampList.empty() &&
  1372. cmakeCheckStampList(this->CheckStampList.c_str())) {
  1373. return 0;
  1374. }
  1375. // If we are given a stamp file check if it is really out of date.
  1376. if (!this->CheckStampFile.empty() &&
  1377. cmakeCheckStampFile(this->CheckStampFile.c_str())) {
  1378. return 0;
  1379. }
  1380. if (this->GetWorkingMode() == NORMAL_MODE) {
  1381. // load the cache
  1382. if (this->LoadCache() < 0) {
  1383. cmSystemTools::Error("Error executing cmake::LoadCache(). Aborting.\n");
  1384. return -1;
  1385. }
  1386. } else {
  1387. this->AddCMakePaths();
  1388. }
  1389. // Add any cache args
  1390. if (!this->SetCacheArgs(args)) {
  1391. cmSystemTools::Error("Problem processing arguments. Aborting.\n");
  1392. return -1;
  1393. }
  1394. // In script mode we terminate after running the script.
  1395. if (this->GetWorkingMode() != NORMAL_MODE) {
  1396. if (cmSystemTools::GetErrorOccuredFlag()) {
  1397. return -1;
  1398. }
  1399. return 0;
  1400. }
  1401. // If MAKEFLAGS are given in the environment, remove the environment
  1402. // variable. This will prevent try-compile from succeeding when it
  1403. // should fail (if "-i" is an option). We cannot simply test
  1404. // whether "-i" is given and remove it because some make programs
  1405. // encode the MAKEFLAGS variable in a strange way.
  1406. if (cmSystemTools::HasEnv("MAKEFLAGS")) {
  1407. cmSystemTools::PutEnv("MAKEFLAGS=");
  1408. }
  1409. this->PreLoadCMakeFiles();
  1410. if (noconfigure) {
  1411. return 0;
  1412. }
  1413. // now run the global generate
  1414. // Check the state of the build system to see if we need to regenerate.
  1415. if (!this->CheckBuildSystem()) {
  1416. return 0;
  1417. }
  1418. int ret = this->Configure();
  1419. if (ret || this->GetWorkingMode() != NORMAL_MODE) {
  1420. #if defined(CMAKE_HAVE_VS_GENERATORS)
  1421. if (!this->VSSolutionFile.empty() && this->GlobalGenerator) {
  1422. // CMake is running to regenerate a Visual Studio build tree
  1423. // during a build from the VS IDE. The build files cannot be
  1424. // regenerated, so we should stop the build.
  1425. cmSystemTools::Message("CMake Configure step failed. "
  1426. "Build files cannot be regenerated correctly. "
  1427. "Attempting to stop IDE build.");
  1428. cmGlobalVisualStudioGenerator* gg =
  1429. static_cast<cmGlobalVisualStudioGenerator*>(this->GlobalGenerator);
  1430. gg->CallVisualStudioMacro(cmGlobalVisualStudioGenerator::MacroStop,
  1431. this->VSSolutionFile.c_str());
  1432. }
  1433. #endif
  1434. return ret;
  1435. }
  1436. ret = this->Generate();
  1437. std::string message = "Build files have been written to: ";
  1438. message += this->GetHomeOutputDirectory();
  1439. this->UpdateProgress(message.c_str(), -1);
  1440. return ret;
  1441. }
  1442. int cmake::Generate()
  1443. {
  1444. if (!this->GlobalGenerator) {
  1445. return -1;
  1446. }
  1447. if (!this->GlobalGenerator->Compute()) {
  1448. return -1;
  1449. }
  1450. this->GlobalGenerator->Generate();
  1451. if (!this->GraphVizFile.empty()) {
  1452. std::cout << "Generate graphviz: " << this->GraphVizFile << std::endl;
  1453. this->GenerateGraphViz(this->GraphVizFile.c_str());
  1454. }
  1455. if (this->WarnUnusedCli) {
  1456. this->RunCheckForUnusedVariables();
  1457. }
  1458. if (cmSystemTools::GetErrorOccuredFlag()) {
  1459. return -1;
  1460. }
  1461. // Save the cache again after a successful Generate so that any internal
  1462. // variables created during Generate are saved. (Specifically target GUIDs
  1463. // for the Visual Studio and Xcode generators.)
  1464. if (this->GetWorkingMode() == NORMAL_MODE) {
  1465. this->SaveCache(this->GetHomeOutputDirectory());
  1466. }
  1467. return 0;
  1468. }
  1469. void cmake::AddCacheEntry(const std::string& key, const char* value,
  1470. const char* helpString, int type)
  1471. {
  1472. this->State->AddCacheEntry(key, value, helpString,
  1473. cmState::CacheEntryType(type));
  1474. this->UnwatchUnusedCli(key);
  1475. }
  1476. const char* cmake::GetCacheDefinition(const std::string& name) const
  1477. {
  1478. return this->State->GetInitializedCacheValue(name);
  1479. }
  1480. void cmake::AddDefaultCommands()
  1481. {
  1482. std::vector<cmCommand*> commands;
  1483. GetBootstrapCommands1(commands);
  1484. GetBootstrapCommands2(commands);
  1485. GetPredefinedCommands(commands);
  1486. for (std::vector<cmCommand*>::iterator i = commands.begin();
  1487. i != commands.end(); ++i) {
  1488. this->State->AddCommand(*i);
  1489. }
  1490. }
  1491. void cmake::AddDefaultGenerators()
  1492. {
  1493. #if defined(_WIN32) && !defined(__CYGWIN__)
  1494. #if !defined(CMAKE_BOOT_MINGW)
  1495. this->Generators.push_back(cmGlobalVisualStudio15Generator::NewFactory());
  1496. this->Generators.push_back(cmGlobalVisualStudio14Generator::NewFactory());
  1497. this->Generators.push_back(cmGlobalVisualStudio12Generator::NewFactory());
  1498. this->Generators.push_back(cmGlobalVisualStudio11Generator::NewFactory());
  1499. this->Generators.push_back(cmGlobalVisualStudio10Generator::NewFactory());
  1500. this->Generators.push_back(cmGlobalVisualStudio9Generator::NewFactory());
  1501. this->Generators.push_back(cmGlobalVisualStudio8Generator::NewFactory());
  1502. this->Generators.push_back(cmGlobalVisualStudio71Generator::NewFactory());
  1503. this->Generators.push_back(cmGlobalBorlandMakefileGenerator::NewFactory());
  1504. this->Generators.push_back(cmGlobalNMakeMakefileGenerator::NewFactory());
  1505. this->Generators.push_back(cmGlobalJOMMakefileGenerator::NewFactory());
  1506. this->Generators.push_back(cmGlobalGhsMultiGenerator::NewFactory());
  1507. #endif
  1508. this->Generators.push_back(cmGlobalMSYSMakefileGenerator::NewFactory());
  1509. this->Generators.push_back(cmGlobalMinGWMakefileGenerator::NewFactory());
  1510. #endif
  1511. this->Generators.push_back(cmGlobalUnixMakefileGenerator3::NewFactory());
  1512. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1513. this->Generators.push_back(cmGlobalNinjaGenerator::NewFactory());
  1514. #endif
  1515. #if defined(CMAKE_USE_WMAKE)
  1516. this->Generators.push_back(cmGlobalWatcomWMakeGenerator::NewFactory());
  1517. #endif
  1518. #ifdef CMAKE_USE_XCODE
  1519. this->Generators.push_back(cmGlobalXCodeGenerator::NewFactory());
  1520. #endif
  1521. }
  1522. bool cmake::ParseCacheEntry(const std::string& entry, std::string& var,
  1523. std::string& value, cmState::CacheEntryType& type)
  1524. {
  1525. return cmState::ParseCacheEntry(entry, var, value, type);
  1526. }
  1527. int cmake::LoadCache()
  1528. {
  1529. // could we not read the cache
  1530. if (!this->LoadCache(this->GetHomeOutputDirectory())) {
  1531. // if it does exist, but isn't readable then warn the user
  1532. std::string cacheFile = this->GetHomeOutputDirectory();
  1533. cacheFile += "/CMakeCache.txt";
  1534. if (cmSystemTools::FileExists(cacheFile.c_str())) {
  1535. cmSystemTools::Error(
  1536. "There is a CMakeCache.txt file for the current binary tree but "
  1537. "cmake does not have permission to read it. Please check the "
  1538. "permissions of the directory you are trying to run CMake on.");
  1539. return -1;
  1540. }
  1541. }
  1542. // setup CMAKE_ROOT and CMAKE_COMMAND
  1543. if (!this->AddCMakePaths()) {
  1544. return -3;
  1545. }
  1546. return 0;
  1547. }
  1548. bool cmake::LoadCache(const std::string& path)
  1549. {
  1550. std::set<std::string> emptySet;
  1551. return this->LoadCache(path, true, emptySet, emptySet);
  1552. }
  1553. bool cmake::LoadCache(const std::string& path, bool internal,
  1554. std::set<std::string>& excludes,
  1555. std::set<std::string>& includes)
  1556. {
  1557. bool result = this->State->LoadCache(path, internal, excludes, includes);
  1558. static const char* entries[] = { "CMAKE_CACHE_MAJOR_VERSION",
  1559. "CMAKE_CACHE_MINOR_VERSION" };
  1560. for (const char* const* nameIt = cmArrayBegin(entries);
  1561. nameIt != cmArrayEnd(entries); ++nameIt) {
  1562. this->UnwatchUnusedCli(*nameIt);
  1563. }
  1564. return result;
  1565. }
  1566. bool cmake::SaveCache(const std::string& path)
  1567. {
  1568. bool result = this->State->SaveCache(path);
  1569. static const char* entries[] = { "CMAKE_CACHE_MAJOR_VERSION",
  1570. "CMAKE_CACHE_MINOR_VERSION",
  1571. "CMAKE_CACHE_PATCH_VERSION",
  1572. "CMAKE_CACHEFILE_DIR" };
  1573. for (const char* const* nameIt = cmArrayBegin(entries);
  1574. nameIt != cmArrayEnd(entries); ++nameIt) {
  1575. this->UnwatchUnusedCli(*nameIt);
  1576. }
  1577. return result;
  1578. }
  1579. bool cmake::DeleteCache(const std::string& path)
  1580. {
  1581. return this->State->DeleteCache(path);
  1582. }
  1583. void cmake::SetProgressCallback(ProgressCallbackType f, void* cd)
  1584. {
  1585. this->ProgressCallback = f;
  1586. this->ProgressCallbackClientData = cd;
  1587. }
  1588. void cmake::UpdateProgress(const char* msg, float prog)
  1589. {
  1590. if (this->ProgressCallback && !this->State->GetIsInTryCompile()) {
  1591. (*this->ProgressCallback)(msg, prog, this->ProgressCallbackClientData);
  1592. return;
  1593. }
  1594. }
  1595. bool cmake::GetIsInTryCompile() const
  1596. {
  1597. return this->State->GetIsInTryCompile();
  1598. }
  1599. void cmake::SetIsInTryCompile(bool b)
  1600. {
  1601. this->State->SetIsInTryCompile(b);
  1602. }
  1603. void cmake::GetGeneratorDocumentation(std::vector<cmDocumentationEntry>& v)
  1604. {
  1605. for (RegisteredGeneratorsVector::const_iterator i = this->Generators.begin();
  1606. i != this->Generators.end(); ++i) {
  1607. cmDocumentationEntry e;
  1608. (*i)->GetDocumentation(e);
  1609. v.push_back(e);
  1610. }
  1611. for (RegisteredExtraGeneratorsVector::const_iterator i =
  1612. this->ExtraGenerators.begin();
  1613. i != this->ExtraGenerators.end(); ++i) {
  1614. const std::string doc = (*i)->GetDocumentation();
  1615. const std::string name = (*i)->GetName();
  1616. // Aliases:
  1617. for (std::vector<std::string>::const_iterator a = (*i)->Aliases.begin();
  1618. a != (*i)->Aliases.end(); ++a) {
  1619. cmDocumentationEntry e;
  1620. e.Name = *a;
  1621. e.Brief = doc;
  1622. v.push_back(e);
  1623. }
  1624. // Full names:
  1625. const std::vector<std::string> generators =
  1626. (*i)->GetSupportedGlobalGenerators();
  1627. for (std::vector<std::string>::const_iterator g = generators.begin();
  1628. g != generators.end(); ++g) {
  1629. cmDocumentationEntry e;
  1630. e.Name =
  1631. cmExternalMakefileProjectGenerator::CreateFullGeneratorName(*g, name);
  1632. e.Brief = doc;
  1633. v.push_back(e);
  1634. }
  1635. }
  1636. }
  1637. void cmake::PrintGeneratorList()
  1638. {
  1639. #ifdef CMAKE_BUILD_WITH_CMAKE
  1640. cmDocumentation doc;
  1641. std::vector<cmDocumentationEntry> generators;
  1642. this->GetGeneratorDocumentation(generators);
  1643. doc.AppendSection("Generators", generators);
  1644. std::cerr << "\n";
  1645. doc.PrintDocumentation(cmDocumentation::ListGenerators, std::cerr);
  1646. #endif
  1647. }
  1648. void cmake::UpdateConversionPathTable()
  1649. {
  1650. // Update the path conversion table with any specified file:
  1651. const char* tablepath =
  1652. this->State->GetInitializedCacheValue("CMAKE_PATH_TRANSLATION_FILE");
  1653. if (tablepath) {
  1654. cmsys::ifstream table(tablepath);
  1655. if (!table) {
  1656. cmSystemTools::Error("CMAKE_PATH_TRANSLATION_FILE set to ", tablepath,
  1657. ". CMake can not open file.");
  1658. cmSystemTools::ReportLastSystemError("CMake can not open file.");
  1659. } else {
  1660. std::string a, b;
  1661. while (!table.eof()) {
  1662. // two entries per line
  1663. table >> a;
  1664. table >> b;
  1665. cmSystemTools::AddTranslationPath(a, b);
  1666. }
  1667. }
  1668. }
  1669. }
  1670. int cmake::CheckBuildSystem()
  1671. {
  1672. // We do not need to rerun CMake. Check dependency integrity.
  1673. const bool verbose = isCMakeVerbose();
  1674. // This method will check the integrity of the build system if the
  1675. // option was given on the command line. It reads the given file to
  1676. // determine whether CMake should rerun.
  1677. // If no file is provided for the check, we have to rerun.
  1678. if (this->CheckBuildSystemArgument.empty()) {
  1679. if (verbose) {
  1680. std::ostringstream msg;
  1681. msg << "Re-run cmake no build system arguments\n";
  1682. cmSystemTools::Stdout(msg.str().c_str());
  1683. }
  1684. return 1;
  1685. }
  1686. // If the file provided does not exist, we have to rerun.
  1687. if (!cmSystemTools::FileExists(this->CheckBuildSystemArgument.c_str())) {
  1688. if (verbose) {
  1689. std::ostringstream msg;
  1690. msg << "Re-run cmake missing file: " << this->CheckBuildSystemArgument
  1691. << "\n";
  1692. cmSystemTools::Stdout(msg.str().c_str());
  1693. }
  1694. return 1;
  1695. }
  1696. // Read the rerun check file and use it to decide whether to do the
  1697. // global generate.
  1698. cmake cm;
  1699. cm.SetHomeDirectory("");
  1700. cm.SetHomeOutputDirectory("");
  1701. cm.GetCurrentSnapshot().SetDefaultDefinitions();
  1702. cmGlobalGenerator gg(&cm);
  1703. CM_AUTO_PTR<cmMakefile> mf(new cmMakefile(&gg, cm.GetCurrentSnapshot()));
  1704. if (!mf->ReadListFile(this->CheckBuildSystemArgument.c_str()) ||
  1705. cmSystemTools::GetErrorOccuredFlag()) {
  1706. if (verbose) {
  1707. std::ostringstream msg;
  1708. msg << "Re-run cmake error reading : " << this->CheckBuildSystemArgument
  1709. << "\n";
  1710. cmSystemTools::Stdout(msg.str().c_str());
  1711. }
  1712. // There was an error reading the file. Just rerun.
  1713. return 1;
  1714. }
  1715. if (this->ClearBuildSystem) {
  1716. // Get the generator used for this build system.
  1717. const char* genName = mf->GetDefinition("CMAKE_DEPENDS_GENERATOR");
  1718. if (!genName || genName[0] == '\0') {
  1719. genName = "Unix Makefiles";
  1720. }
  1721. // Create the generator and use it to clear the dependencies.
  1722. CM_AUTO_PTR<cmGlobalGenerator> ggd(this->CreateGlobalGenerator(genName));
  1723. if (ggd.get()) {
  1724. cm.GetCurrentSnapshot().SetDefaultDefinitions();
  1725. CM_AUTO_PTR<cmMakefile> mfd(
  1726. new cmMakefile(ggd.get(), cm.GetCurrentSnapshot()));
  1727. CM_AUTO_PTR<cmLocalGenerator> lgd(ggd->CreateLocalGenerator(mfd.get()));
  1728. lgd->ClearDependencies(mfd.get(), verbose);
  1729. }
  1730. }
  1731. // If any byproduct of makefile generation is missing we must re-run.
  1732. std::vector<std::string> products;
  1733. if (const char* productStr = mf->GetDefinition("CMAKE_MAKEFILE_PRODUCTS")) {
  1734. cmSystemTools::ExpandListArgument(productStr, products);
  1735. }
  1736. for (std::vector<std::string>::const_iterator pi = products.begin();
  1737. pi != products.end(); ++pi) {
  1738. if (!(cmSystemTools::FileExists(pi->c_str()) ||
  1739. cmSystemTools::FileIsSymlink(*pi))) {
  1740. if (verbose) {
  1741. std::ostringstream msg;
  1742. msg << "Re-run cmake, missing byproduct: " << *pi << "\n";
  1743. cmSystemTools::Stdout(msg.str().c_str());
  1744. }
  1745. return 1;
  1746. }
  1747. }
  1748. // Get the set of dependencies and outputs.
  1749. std::vector<std::string> depends;
  1750. std::vector<std::string> outputs;
  1751. const char* dependsStr = mf->GetDefinition("CMAKE_MAKEFILE_DEPENDS");
  1752. const char* outputsStr = mf->GetDefinition("CMAKE_MAKEFILE_OUTPUTS");
  1753. if (dependsStr && outputsStr) {
  1754. cmSystemTools::ExpandListArgument(dependsStr, depends);
  1755. cmSystemTools::ExpandListArgument(outputsStr, outputs);
  1756. }
  1757. if (depends.empty() || outputs.empty()) {
  1758. // Not enough information was provided to do the test. Just rerun.
  1759. if (verbose) {
  1760. std::ostringstream msg;
  1761. msg << "Re-run cmake no CMAKE_MAKEFILE_DEPENDS "
  1762. "or CMAKE_MAKEFILE_OUTPUTS :\n";
  1763. cmSystemTools::Stdout(msg.str().c_str());
  1764. }
  1765. return 1;
  1766. }
  1767. // Find the newest dependency.
  1768. std::vector<std::string>::iterator dep = depends.begin();
  1769. std::string dep_newest = *dep++;
  1770. for (; dep != depends.end(); ++dep) {
  1771. int result = 0;
  1772. if (this->FileComparison->FileTimeCompare(dep_newest.c_str(), dep->c_str(),
  1773. &result)) {
  1774. if (result < 0) {
  1775. dep_newest = *dep;
  1776. }
  1777. } else {
  1778. if (verbose) {
  1779. std::ostringstream msg;
  1780. msg << "Re-run cmake: build system dependency is missing\n";
  1781. cmSystemTools::Stdout(msg.str().c_str());
  1782. }
  1783. return 1;
  1784. }
  1785. }
  1786. // Find the oldest output.
  1787. std::vector<std::string>::iterator out = outputs.begin();
  1788. std::string out_oldest = *out++;
  1789. for (; out != outputs.end(); ++out) {
  1790. int result = 0;
  1791. if (this->FileComparison->FileTimeCompare(out_oldest.c_str(), out->c_str(),
  1792. &result)) {
  1793. if (result > 0) {
  1794. out_oldest = *out;
  1795. }
  1796. } else {
  1797. if (verbose) {
  1798. std::ostringstream msg;
  1799. msg << "Re-run cmake: build system output is missing\n";
  1800. cmSystemTools::Stdout(msg.str().c_str());
  1801. }
  1802. return 1;
  1803. }
  1804. }
  1805. // If any output is older than any dependency then rerun.
  1806. {
  1807. int result = 0;
  1808. if (!this->FileComparison->FileTimeCompare(out_oldest.c_str(),
  1809. dep_newest.c_str(), &result) ||
  1810. result < 0) {
  1811. if (verbose) {
  1812. std::ostringstream msg;
  1813. msg << "Re-run cmake file: " << out_oldest
  1814. << " older than: " << dep_newest << "\n";
  1815. cmSystemTools::Stdout(msg.str().c_str());
  1816. }
  1817. return 1;
  1818. }
  1819. }
  1820. // No need to rerun.
  1821. return 0;
  1822. }
  1823. void cmake::TruncateOutputLog(const char* fname)
  1824. {
  1825. std::string fullPath = this->GetHomeOutputDirectory();
  1826. fullPath += "/";
  1827. fullPath += fname;
  1828. struct stat st;
  1829. if (::stat(fullPath.c_str(), &st)) {
  1830. return;
  1831. }
  1832. if (!this->State->GetInitializedCacheValue("CMAKE_CACHEFILE_DIR")) {
  1833. cmSystemTools::RemoveFile(fullPath);
  1834. return;
  1835. }
  1836. off_t fsize = st.st_size;
  1837. const off_t maxFileSize = 50 * 1024;
  1838. if (fsize < maxFileSize) {
  1839. // TODO: truncate file
  1840. return;
  1841. }
  1842. }
  1843. inline std::string removeQuotes(const std::string& s)
  1844. {
  1845. if (s[0] == '\"' && s[s.size() - 1] == '\"') {
  1846. return s.substr(1, s.size() - 2);
  1847. }
  1848. return s;
  1849. }
  1850. void cmake::MarkCliAsUsed(const std::string& variable)
  1851. {
  1852. this->UsedCliVariables[variable] = true;
  1853. }
  1854. void cmake::GenerateGraphViz(const char* fileName) const
  1855. {
  1856. #ifdef CMAKE_BUILD_WITH_CMAKE
  1857. CM_AUTO_PTR<cmGraphVizWriter> gvWriter(
  1858. new cmGraphVizWriter(this->GetGlobalGenerator()->GetLocalGenerators()));
  1859. std::string settingsFile = this->GetHomeOutputDirectory();
  1860. settingsFile += "/CMakeGraphVizOptions.cmake";
  1861. std::string fallbackSettingsFile = this->GetHomeDirectory();
  1862. fallbackSettingsFile += "/CMakeGraphVizOptions.cmake";
  1863. gvWriter->ReadSettings(settingsFile.c_str(), fallbackSettingsFile.c_str());
  1864. gvWriter->WritePerTargetFiles(fileName);
  1865. gvWriter->WriteTargetDependersFiles(fileName);
  1866. gvWriter->WriteGlobalFile(fileName);
  1867. #endif
  1868. }
  1869. void cmake::SetProperty(const std::string& prop, const char* value)
  1870. {
  1871. this->State->SetGlobalProperty(prop, value);
  1872. }
  1873. void cmake::AppendProperty(const std::string& prop, const char* value,
  1874. bool asString)
  1875. {
  1876. this->State->AppendGlobalProperty(prop, value, asString);
  1877. }
  1878. const char* cmake::GetProperty(const std::string& prop)
  1879. {
  1880. return this->State->GetGlobalProperty(prop);
  1881. }
  1882. bool cmake::GetPropertyAsBool(const std::string& prop)
  1883. {
  1884. return this->State->GetGlobalPropertyAsBool(prop);
  1885. }
  1886. cmInstalledFile* cmake::GetOrCreateInstalledFile(cmMakefile* mf,
  1887. const std::string& name)
  1888. {
  1889. std::map<std::string, cmInstalledFile>::iterator i =
  1890. this->InstalledFiles.find(name);
  1891. if (i != this->InstalledFiles.end()) {
  1892. cmInstalledFile& file = i->second;
  1893. return &file;
  1894. }
  1895. cmInstalledFile& file = this->InstalledFiles[name];
  1896. file.SetName(mf, name);
  1897. return &file;
  1898. }
  1899. cmInstalledFile const* cmake::GetInstalledFile(const std::string& name) const
  1900. {
  1901. std::map<std::string, cmInstalledFile>::const_iterator i =
  1902. this->InstalledFiles.find(name);
  1903. if (i != this->InstalledFiles.end()) {
  1904. cmInstalledFile const& file = i->second;
  1905. return &file;
  1906. }
  1907. return CM_NULLPTR;
  1908. }
  1909. int cmake::GetSystemInformation(std::vector<std::string>& args)
  1910. {
  1911. // so create the directory
  1912. std::string resultFile;
  1913. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  1914. std::string destPath = cwd + "/__cmake_systeminformation";
  1915. cmSystemTools::RemoveADirectory(destPath);
  1916. if (!cmSystemTools::MakeDirectory(destPath.c_str())) {
  1917. std::cerr << "Error: --system-information must be run from a "
  1918. "writable directory!\n";
  1919. return 1;
  1920. }
  1921. // process the arguments
  1922. bool writeToStdout = true;
  1923. for (unsigned int i = 1; i < args.size(); ++i) {
  1924. std::string arg = args[i];
  1925. if (arg.find("-G", 0) == 0) {
  1926. std::string value = arg.substr(2);
  1927. if (value.empty()) {
  1928. ++i;
  1929. if (i >= args.size()) {
  1930. cmSystemTools::Error("No generator specified for -G");
  1931. this->PrintGeneratorList();
  1932. return -1;
  1933. }
  1934. value = args[i];
  1935. }
  1936. cmGlobalGenerator* gen = this->CreateGlobalGenerator(value);
  1937. if (!gen) {
  1938. cmSystemTools::Error("Could not create named generator ",
  1939. value.c_str());
  1940. this->PrintGeneratorList();
  1941. } else {
  1942. this->SetGlobalGenerator(gen);
  1943. }
  1944. }
  1945. // no option assume it is the output file
  1946. else {
  1947. if (!cmSystemTools::FileIsFullPath(arg.c_str())) {
  1948. resultFile = cwd;
  1949. resultFile += "/";
  1950. }
  1951. resultFile += arg;
  1952. writeToStdout = false;
  1953. }
  1954. }
  1955. // we have to find the module directory, so we can copy the files
  1956. this->AddCMakePaths();
  1957. std::string modulesPath = cmSystemTools::GetCMakeRoot();
  1958. modulesPath += "/Modules";
  1959. std::string inFile = modulesPath;
  1960. inFile += "/SystemInformation.cmake";
  1961. std::string outFile = destPath;
  1962. outFile += "/CMakeLists.txt";
  1963. // Copy file
  1964. if (!cmSystemTools::cmCopyFile(inFile.c_str(), outFile.c_str())) {
  1965. std::cerr << "Error copying file \"" << inFile << "\" to \"" << outFile
  1966. << "\".\n";
  1967. return 1;
  1968. }
  1969. // do we write to a file or to stdout?
  1970. if (resultFile.empty()) {
  1971. resultFile = cwd;
  1972. resultFile += "/__cmake_systeminformation/results.txt";
  1973. }
  1974. // now run cmake on the CMakeLists file
  1975. cmSystemTools::ChangeDirectory(destPath);
  1976. std::vector<std::string> args2;
  1977. args2.push_back(args[0]);
  1978. args2.push_back(destPath);
  1979. std::string resultArg = "-DRESULT_FILE=";
  1980. resultArg += resultFile;
  1981. args2.push_back(resultArg);
  1982. int res = this->Run(args2, false);
  1983. if (res != 0) {
  1984. std::cerr << "Error: --system-information failed on internal CMake!\n";
  1985. return res;
  1986. }
  1987. // change back to the original directory
  1988. cmSystemTools::ChangeDirectory(cwd);
  1989. // echo results to stdout if needed
  1990. if (writeToStdout) {
  1991. FILE* fin = cmsys::SystemTools::Fopen(resultFile, "r");
  1992. if (fin) {
  1993. const int bufferSize = 4096;
  1994. char buffer[bufferSize];
  1995. size_t n;
  1996. while ((n = fread(buffer, 1, bufferSize, fin)) > 0) {
  1997. for (char* c = buffer; c < buffer + n; ++c) {
  1998. putc(*c, stdout);
  1999. }
  2000. fflush(stdout);
  2001. }
  2002. fclose(fin);
  2003. }
  2004. }
  2005. // clean up the directory
  2006. cmSystemTools::RemoveADirectory(destPath);
  2007. return 0;
  2008. }
  2009. static bool cmakeCheckStampFile(const char* stampName)
  2010. {
  2011. // The stamp file does not exist. Use the stamp dependencies to
  2012. // determine whether it is really out of date. This works in
  2013. // conjunction with cmLocalVisualStudio7Generator to avoid
  2014. // repeatedly re-running CMake when the user rebuilds the entire
  2015. // solution.
  2016. std::string stampDepends = stampName;
  2017. stampDepends += ".depend";
  2018. #if defined(_WIN32) || defined(__CYGWIN__)
  2019. cmsys::ifstream fin(stampDepends.c_str(), std::ios::in | std::ios::binary);
  2020. #else
  2021. cmsys::ifstream fin(stampDepends.c_str());
  2022. #endif
  2023. if (!fin) {
  2024. // The stamp dependencies file cannot be read. Just assume the
  2025. // build system is really out of date.
  2026. std::cout << "CMake is re-running because " << stampName
  2027. << " dependency file is missing.\n";
  2028. return false;
  2029. }
  2030. // Compare the stamp dependencies against the dependency file itself.
  2031. cmFileTimeComparison ftc;
  2032. std::string dep;
  2033. while (cmSystemTools::GetLineFromStream(fin, dep)) {
  2034. int result;
  2035. if (!dep.empty() && dep[0] != '#' &&
  2036. (!ftc.FileTimeCompare(stampDepends.c_str(), dep.c_str(), &result) ||
  2037. result < 0)) {
  2038. // The stamp depends file is older than this dependency. The
  2039. // build system is really out of date.
  2040. std::cout << "CMake is re-running because " << stampName
  2041. << " is out-of-date.\n";
  2042. std::cout << " the file '" << dep << "'\n";
  2043. std::cout << " is newer than '" << stampDepends << "'\n";
  2044. std::cout << " result='" << result << "'\n";
  2045. return false;
  2046. }
  2047. }
  2048. // The build system is up to date. The stamp file has been removed
  2049. // by the VS IDE due to a "rebuild" request. Restore it atomically.
  2050. std::ostringstream stampTempStream;
  2051. stampTempStream << stampName << ".tmp" << cmSystemTools::RandomSeed();
  2052. std::string stampTempString = stampTempStream.str();
  2053. const char* stampTemp = stampTempString.c_str();
  2054. {
  2055. // TODO: Teach cmGeneratedFileStream to use a random temp file (with
  2056. // multiple tries in unlikely case of conflict) and use that here.
  2057. cmsys::ofstream stamp(stampTemp);
  2058. stamp << "# CMake generation timestamp file for this directory.\n";
  2059. }
  2060. if (cmSystemTools::RenameFile(stampTemp, stampName)) {
  2061. // Notify the user why CMake is not re-running. It is safe to
  2062. // just print to stdout here because this code is only reachable
  2063. // through an undocumented flag used by the VS generator.
  2064. std::cout << "CMake does not need to re-run because " << stampName
  2065. << " is up-to-date.\n";
  2066. return true;
  2067. }
  2068. cmSystemTools::RemoveFile(stampTemp);
  2069. cmSystemTools::Error("Cannot restore timestamp ", stampName);
  2070. return false;
  2071. }
  2072. static bool cmakeCheckStampList(const char* stampList)
  2073. {
  2074. // If the stamp list does not exist CMake must rerun to generate it.
  2075. if (!cmSystemTools::FileExists(stampList)) {
  2076. std::cout << "CMake is re-running because generate.stamp.list "
  2077. << "is missing.\n";
  2078. return false;
  2079. }
  2080. cmsys::ifstream fin(stampList);
  2081. if (!fin) {
  2082. std::cout << "CMake is re-running because generate.stamp.list "
  2083. << "could not be read.\n";
  2084. return false;
  2085. }
  2086. // Check each stamp.
  2087. std::string stampName;
  2088. while (cmSystemTools::GetLineFromStream(fin, stampName)) {
  2089. if (!cmakeCheckStampFile(stampName.c_str())) {
  2090. return false;
  2091. }
  2092. }
  2093. return true;
  2094. }
  2095. void cmake::IssueMessage(cmake::MessageType t, std::string const& text,
  2096. cmListFileBacktrace const& backtrace) const
  2097. {
  2098. this->Messenger->IssueMessage(t, text, backtrace);
  2099. }
  2100. std::vector<std::string> cmake::GetDebugConfigs()
  2101. {
  2102. std::vector<std::string> configs;
  2103. if (const char* config_list =
  2104. this->State->GetGlobalProperty("DEBUG_CONFIGURATIONS")) {
  2105. // Expand the specified list and convert to upper-case.
  2106. cmSystemTools::ExpandListArgument(config_list, configs);
  2107. std::transform(configs.begin(), configs.end(), configs.begin(),
  2108. cmSystemTools::UpperCase);
  2109. }
  2110. // If no configurations were specified, use a default list.
  2111. if (configs.empty()) {
  2112. configs.push_back("DEBUG");
  2113. }
  2114. return configs;
  2115. }
  2116. cmMessenger* cmake::GetMessenger() const
  2117. {
  2118. return this->Messenger;
  2119. }
  2120. int cmake::Build(const std::string& dir, const std::string& target,
  2121. const std::string& config,
  2122. const std::vector<std::string>& nativeOptions, bool clean)
  2123. {
  2124. this->SetHomeDirectory("");
  2125. this->SetHomeOutputDirectory("");
  2126. if (!cmSystemTools::FileIsDirectory(dir)) {
  2127. std::cerr << "Error: " << dir << " is not a directory\n";
  2128. return 1;
  2129. }
  2130. std::string cachePath = dir;
  2131. cmSystemTools::ConvertToUnixSlashes(cachePath);
  2132. std::string cacheFile = cachePath;
  2133. cacheFile += "/CMakeCache.txt";
  2134. if (!cmSystemTools::FileExists(cacheFile.c_str())) {
  2135. // search in parent directories for cache
  2136. std::string cmakeFiles = cachePath;
  2137. cmakeFiles += "/CMakeFiles";
  2138. if (cmSystemTools::FileExists(cmakeFiles.c_str())) {
  2139. std::string cachePathFound =
  2140. cmSystemTools::FileExistsInParentDirectories("CMakeCache.txt",
  2141. cachePath.c_str(), "/");
  2142. if (!cachePathFound.empty()) {
  2143. cachePath = cmSystemTools::GetFilenamePath(cachePathFound);
  2144. }
  2145. }
  2146. }
  2147. if (!this->LoadCache(cachePath)) {
  2148. std::cerr << "Error: could not load cache\n";
  2149. return 1;
  2150. }
  2151. const char* cachedGenerator =
  2152. this->State->GetCacheEntryValue("CMAKE_GENERATOR");
  2153. if (!cachedGenerator) {
  2154. std::cerr << "Error: could not find CMAKE_GENERATOR in Cache\n";
  2155. return 1;
  2156. }
  2157. CM_AUTO_PTR<cmGlobalGenerator> gen(
  2158. this->CreateGlobalGenerator(cachedGenerator));
  2159. if (!gen.get()) {
  2160. std::cerr << "Error: could create CMAKE_GENERATOR \"" << cachedGenerator
  2161. << "\"\n";
  2162. return 1;
  2163. }
  2164. std::string output;
  2165. std::string projName;
  2166. const char* cachedProjectName =
  2167. this->State->GetCacheEntryValue("CMAKE_PROJECT_NAME");
  2168. if (!cachedProjectName) {
  2169. std::cerr << "Error: could not find CMAKE_PROJECT_NAME in Cache\n";
  2170. return 1;
  2171. }
  2172. projName = cachedProjectName;
  2173. bool verbose = false;
  2174. const char* cachedVerbose =
  2175. this->State->GetCacheEntryValue("CMAKE_VERBOSE_MAKEFILE");
  2176. if (cachedVerbose) {
  2177. verbose = cmSystemTools::IsOn(cachedVerbose);
  2178. }
  2179. return gen->Build("", dir, projName, target, output, "", config, clean,
  2180. false, verbose, 0, cmSystemTools::OUTPUT_PASSTHROUGH,
  2181. nativeOptions);
  2182. }
  2183. void cmake::WatchUnusedCli(const std::string& var)
  2184. {
  2185. #ifdef CMAKE_BUILD_WITH_CMAKE
  2186. this->VariableWatch->AddWatch(var, cmWarnUnusedCliWarning, this);
  2187. if (this->UsedCliVariables.find(var) == this->UsedCliVariables.end()) {
  2188. this->UsedCliVariables[var] = false;
  2189. }
  2190. #endif
  2191. }
  2192. void cmake::UnwatchUnusedCli(const std::string& var)
  2193. {
  2194. #ifdef CMAKE_BUILD_WITH_CMAKE
  2195. this->VariableWatch->RemoveWatch(var, cmWarnUnusedCliWarning);
  2196. this->UsedCliVariables.erase(var);
  2197. #endif
  2198. }
  2199. void cmake::RunCheckForUnusedVariables()
  2200. {
  2201. #ifdef CMAKE_BUILD_WITH_CMAKE
  2202. bool haveUnused = false;
  2203. std::ostringstream msg;
  2204. msg << "Manually-specified variables were not used by the project:";
  2205. for (std::map<std::string, bool>::const_iterator it =
  2206. this->UsedCliVariables.begin();
  2207. it != this->UsedCliVariables.end(); ++it) {
  2208. if (!it->second) {
  2209. haveUnused = true;
  2210. msg << "\n " << it->first;
  2211. }
  2212. }
  2213. if (haveUnused) {
  2214. this->IssueMessage(cmake::WARNING, msg.str());
  2215. }
  2216. #endif
  2217. }
  2218. bool cmake::GetSuppressDevWarnings() const
  2219. {
  2220. return this->Messenger->GetSuppressDevWarnings();
  2221. }
  2222. void cmake::SetSuppressDevWarnings(bool b)
  2223. {
  2224. std::string value;
  2225. // equivalent to -Wno-dev
  2226. if (b) {
  2227. value = "TRUE";
  2228. }
  2229. // equivalent to -Wdev
  2230. else {
  2231. value = "FALSE";
  2232. }
  2233. this->AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_WARNINGS", value.c_str(),
  2234. "Suppress Warnings that are meant for"
  2235. " the author of the CMakeLists.txt files.",
  2236. cmState::INTERNAL);
  2237. }
  2238. bool cmake::GetSuppressDeprecatedWarnings() const
  2239. {
  2240. return this->Messenger->GetSuppressDeprecatedWarnings();
  2241. }
  2242. void cmake::SetSuppressDeprecatedWarnings(bool b)
  2243. {
  2244. std::string value;
  2245. // equivalent to -Wno-deprecated
  2246. if (b) {
  2247. value = "FALSE";
  2248. }
  2249. // equivalent to -Wdeprecated
  2250. else {
  2251. value = "TRUE";
  2252. }
  2253. this->AddCacheEntry("CMAKE_WARN_DEPRECATED", value.c_str(),
  2254. "Whether to issue warnings for deprecated "
  2255. "functionality.",
  2256. cmState::INTERNAL);
  2257. }
  2258. bool cmake::GetDevWarningsAsErrors() const
  2259. {
  2260. return this->Messenger->GetDevWarningsAsErrors();
  2261. }
  2262. void cmake::SetDevWarningsAsErrors(bool b)
  2263. {
  2264. std::string value;
  2265. // equivalent to -Werror=dev
  2266. if (b) {
  2267. value = "FALSE";
  2268. }
  2269. // equivalent to -Wno-error=dev
  2270. else {
  2271. value = "TRUE";
  2272. }
  2273. this->AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_ERRORS", value.c_str(),
  2274. "Suppress errors that are meant for"
  2275. " the author of the CMakeLists.txt files.",
  2276. cmState::INTERNAL);
  2277. }
  2278. bool cmake::GetDeprecatedWarningsAsErrors() const
  2279. {
  2280. return this->Messenger->GetDeprecatedWarningsAsErrors();
  2281. }
  2282. void cmake::SetDeprecatedWarningsAsErrors(bool b)
  2283. {
  2284. std::string value;
  2285. // equivalent to -Werror=deprecated
  2286. if (b) {
  2287. value = "TRUE";
  2288. }
  2289. // equivalent to -Wno-error=deprecated
  2290. else {
  2291. value = "FALSE";
  2292. }
  2293. this->AddCacheEntry("CMAKE_ERROR_DEPRECATED", value.c_str(),
  2294. "Whether to issue deprecation errors for macros"
  2295. " and functions.",
  2296. cmState::INTERNAL);
  2297. }