cmake.cxx 78 KB

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