cmake.cxx 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719
  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 char* 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 char* name)
  956. {
  957. cmExternalMakefileProjectGenerator* extraGenerator = 0;
  958. RegisteredExtraGeneratorsMap::const_iterator extraGenIt =
  959. this->ExtraGenerators.find(name);
  960. if (extraGenIt != this->ExtraGenerators.end())
  961. {
  962. extraGenerator = (extraGenIt->second)();
  963. name = extraGenerator->GetGlobalGeneratorName(name);
  964. }
  965. cmGlobalGenerator* generator = 0;
  966. for (RegisteredGeneratorsVector::const_iterator i =
  967. this->Generators.begin(); i != this->Generators.end(); ++i)
  968. {
  969. generator = (*i)->CreateGlobalGenerator(name);
  970. if (generator)
  971. {
  972. break;
  973. }
  974. }
  975. if (generator)
  976. {
  977. generator->SetCMakeInstance(this);
  978. generator->SetExternalMakefileProjectGenerator(extraGenerator);
  979. }
  980. else
  981. {
  982. delete extraGenerator;
  983. }
  984. return generator;
  985. }
  986. void cmake::SetHomeDirectory(const std::string& dir)
  987. {
  988. this->cmHomeDirectory = dir;
  989. cmSystemTools::ConvertToUnixSlashes(this->cmHomeDirectory);
  990. }
  991. void cmake::SetHomeOutputDirectory(const std::string& lib)
  992. {
  993. this->HomeOutputDirectory = lib;
  994. cmSystemTools::ConvertToUnixSlashes(this->HomeOutputDirectory);
  995. }
  996. void cmake::SetGlobalGenerator(cmGlobalGenerator *gg)
  997. {
  998. if(!gg)
  999. {
  1000. cmSystemTools::Error("Error SetGlobalGenerator called with null");
  1001. return;
  1002. }
  1003. // delete the old generator
  1004. if (this->GlobalGenerator)
  1005. {
  1006. delete this->GlobalGenerator;
  1007. // restore the original environment variables CXX and CC
  1008. // Restore CC
  1009. std::string env = "CC=";
  1010. if(this->CCEnvironment.size())
  1011. {
  1012. env += this->CCEnvironment;
  1013. }
  1014. cmSystemTools::PutEnv(env.c_str());
  1015. env = "CXX=";
  1016. if(this->CXXEnvironment.size())
  1017. {
  1018. env += this->CXXEnvironment;
  1019. }
  1020. cmSystemTools::PutEnv(env.c_str());
  1021. }
  1022. // set the new
  1023. this->GlobalGenerator = gg;
  1024. // set the global flag for unix style paths on cmSystemTools as soon as
  1025. // the generator is set. This allows gmake to be used on windows.
  1026. cmSystemTools::SetForceUnixPaths
  1027. (this->GlobalGenerator->GetForceUnixPaths());
  1028. // Save the environment variables CXX and CC
  1029. const char* cxx = getenv("CXX");
  1030. const char* cc = getenv("CC");
  1031. if(cxx)
  1032. {
  1033. this->CXXEnvironment = cxx;
  1034. }
  1035. else
  1036. {
  1037. this->CXXEnvironment = "";
  1038. }
  1039. if(cc)
  1040. {
  1041. this->CCEnvironment = cc;
  1042. }
  1043. else
  1044. {
  1045. this->CCEnvironment = "";
  1046. }
  1047. // set the cmake instance just to be sure
  1048. gg->SetCMakeInstance(this);
  1049. }
  1050. int cmake::DoPreConfigureChecks()
  1051. {
  1052. // Make sure the Start directory contains a CMakeLists.txt file.
  1053. std::string srcList = this->GetHomeDirectory();
  1054. srcList += "/CMakeLists.txt";
  1055. if(!cmSystemTools::FileExists(srcList.c_str()))
  1056. {
  1057. cmOStringStream err;
  1058. if(cmSystemTools::FileIsDirectory(this->GetHomeDirectory()))
  1059. {
  1060. err << "The source directory \"" << this->GetHomeDirectory()
  1061. << "\" does not appear to contain CMakeLists.txt.\n";
  1062. }
  1063. else if(cmSystemTools::FileExists(this->GetHomeDirectory()))
  1064. {
  1065. err << "The source directory \"" << this->GetHomeDirectory()
  1066. << "\" is a file, not a directory.\n";
  1067. }
  1068. else
  1069. {
  1070. err << "The source directory \"" << this->GetHomeDirectory()
  1071. << "\" does not exist.\n";
  1072. }
  1073. err << "Specify --help for usage, or press the help button on the CMake "
  1074. "GUI.";
  1075. cmSystemTools::Error(err.str().c_str());
  1076. return -2;
  1077. }
  1078. // do a sanity check on some values
  1079. if(this->CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY"))
  1080. {
  1081. std::string cacheStart =
  1082. this->CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY");
  1083. cacheStart += "/CMakeLists.txt";
  1084. std::string currentStart = this->GetHomeDirectory();
  1085. currentStart += "/CMakeLists.txt";
  1086. if(!cmSystemTools::SameFile(cacheStart.c_str(), currentStart.c_str()))
  1087. {
  1088. std::string message = "The source \"";
  1089. message += currentStart;
  1090. message += "\" does not match the source \"";
  1091. message += cacheStart;
  1092. message += "\" used to generate cache. ";
  1093. message += "Re-run cmake with a different source directory.";
  1094. cmSystemTools::Error(message.c_str());
  1095. return -2;
  1096. }
  1097. }
  1098. else
  1099. {
  1100. return 0;
  1101. }
  1102. return 1;
  1103. }
  1104. struct SaveCacheEntry
  1105. {
  1106. std::string key;
  1107. std::string value;
  1108. std::string help;
  1109. cmCacheManager::CacheEntryType type;
  1110. };
  1111. int cmake::HandleDeleteCacheVariables(const std::string& var)
  1112. {
  1113. std::vector<std::string> argsSplit;
  1114. cmSystemTools::ExpandListArgument(std::string(var), argsSplit, true);
  1115. // erase the property to avoid infinite recursion
  1116. this->SetProperty("__CMAKE_DELETE_CACHE_CHANGE_VARS_", "");
  1117. if(this->GetIsInTryCompile())
  1118. {
  1119. return 0;
  1120. }
  1121. cmCacheManager::CacheIterator ci = this->CacheManager->NewIterator();
  1122. std::vector<SaveCacheEntry> saved;
  1123. cmOStringStream warning;
  1124. warning
  1125. << "You have changed variables that require your cache to be deleted.\n"
  1126. << "Configure will be re-run and you may have to reset some variables.\n"
  1127. << "The following variables have changed:\n";
  1128. for(std::vector<std::string>::iterator i = argsSplit.begin();
  1129. i != argsSplit.end(); ++i)
  1130. {
  1131. SaveCacheEntry save;
  1132. save.key = *i;
  1133. warning << *i << "= ";
  1134. i++;
  1135. save.value = *i;
  1136. warning << *i << "\n";
  1137. if(ci.Find(save.key.c_str()))
  1138. {
  1139. save.type = ci.GetType();
  1140. save.help = ci.GetProperty("HELPSTRING");
  1141. }
  1142. saved.push_back(save);
  1143. }
  1144. // remove the cache
  1145. this->CacheManager->DeleteCache(this->GetStartOutputDirectory());
  1146. // load the empty cache
  1147. this->LoadCache();
  1148. // restore the changed compilers
  1149. for(std::vector<SaveCacheEntry>::iterator i = saved.begin();
  1150. i != saved.end(); ++i)
  1151. {
  1152. this->AddCacheEntry(i->key.c_str(), i->value.c_str(),
  1153. i->help.c_str(), i->type);
  1154. }
  1155. cmSystemTools::Message(warning.str().c_str());
  1156. // avoid reconfigure if there were errors
  1157. if(!cmSystemTools::GetErrorOccuredFlag())
  1158. {
  1159. // re-run configure
  1160. return this->Configure();
  1161. }
  1162. return 0;
  1163. }
  1164. int cmake::Configure()
  1165. {
  1166. if(this->DoSuppressDevWarnings)
  1167. {
  1168. if(this->SuppressDevWarnings)
  1169. {
  1170. this->CacheManager->
  1171. AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_WARNINGS", "TRUE",
  1172. "Suppress Warnings that are meant for"
  1173. " the author of the CMakeLists.txt files.",
  1174. cmCacheManager::INTERNAL);
  1175. }
  1176. else
  1177. {
  1178. this->CacheManager->
  1179. AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_WARNINGS", "FALSE",
  1180. "Suppress Warnings that are meant for"
  1181. " the author of the CMakeLists.txt files.",
  1182. cmCacheManager::INTERNAL);
  1183. }
  1184. }
  1185. int ret = this->ActualConfigure();
  1186. const char* delCacheVars =
  1187. this->GetProperty("__CMAKE_DELETE_CACHE_CHANGE_VARS_");
  1188. if(delCacheVars && delCacheVars[0] != 0)
  1189. {
  1190. return this->HandleDeleteCacheVariables(delCacheVars);
  1191. }
  1192. return ret;
  1193. }
  1194. int cmake::ActualConfigure()
  1195. {
  1196. // Construct right now our path conversion table before it's too late:
  1197. this->UpdateConversionPathTable();
  1198. this->CleanupCommandsAndMacros();
  1199. int res = 0;
  1200. if ( this->GetWorkingMode() == NORMAL_MODE )
  1201. {
  1202. res = this->DoPreConfigureChecks();
  1203. }
  1204. if ( res < 0 )
  1205. {
  1206. return -2;
  1207. }
  1208. if ( !res )
  1209. {
  1210. this->CacheManager->AddCacheEntry
  1211. ("CMAKE_HOME_DIRECTORY",
  1212. this->GetHomeDirectory(),
  1213. "Start directory with the top level CMakeLists.txt file for this "
  1214. "project",
  1215. cmCacheManager::INTERNAL);
  1216. }
  1217. // no generator specified on the command line
  1218. if(!this->GlobalGenerator)
  1219. {
  1220. const char* genName =
  1221. this->CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1222. const char* extraGenName =
  1223. this->CacheManager->GetCacheValue("CMAKE_EXTRA_GENERATOR");
  1224. if(genName)
  1225. {
  1226. std::string fullName = cmExternalMakefileProjectGenerator::
  1227. CreateFullGeneratorName(genName, extraGenName);
  1228. this->GlobalGenerator = this->CreateGlobalGenerator(fullName.c_str());
  1229. }
  1230. if(this->GlobalGenerator)
  1231. {
  1232. // set the global flag for unix style paths on cmSystemTools as
  1233. // soon as the generator is set. This allows gmake to be used
  1234. // on windows.
  1235. cmSystemTools::SetForceUnixPaths
  1236. (this->GlobalGenerator->GetForceUnixPaths());
  1237. }
  1238. else
  1239. {
  1240. #if defined(__BORLANDC__) && defined(_WIN32)
  1241. this->SetGlobalGenerator(new cmGlobalBorlandMakefileGenerator);
  1242. #elif defined(_WIN32) && !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW)
  1243. std::string installedCompiler;
  1244. // Try to find the newest VS installed on the computer and
  1245. // use that as a default if -G is not specified
  1246. const std::string vsregBase =
  1247. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\";
  1248. std::vector<std::string> vsVerions;
  1249. vsVerions.push_back("VisualStudio\\");
  1250. vsVerions.push_back("VCExpress\\");
  1251. vsVerions.push_back("WDExpress\\");
  1252. struct VSRegistryEntryName
  1253. {
  1254. const char* MSVersion;
  1255. const char* GeneratorName;
  1256. };
  1257. VSRegistryEntryName version[] = {
  1258. {"6.0", "Visual Studio 6"},
  1259. {"7.0", "Visual Studio 7"},
  1260. {"7.1", "Visual Studio 7 .NET 2003"},
  1261. {"8.0", "Visual Studio 8 2005"},
  1262. {"9.0", "Visual Studio 9 2008"},
  1263. {"10.0", "Visual Studio 10 2010"},
  1264. {"11.0", "Visual Studio 11 2012"},
  1265. {"12.0", "Visual Studio 12 2013"},
  1266. {0, 0}};
  1267. for(int i=0; version[i].MSVersion != 0; i++)
  1268. {
  1269. for(size_t b=0; b < vsVerions.size(); b++)
  1270. {
  1271. std::string reg = vsregBase + vsVerions[b] + version[i].MSVersion;
  1272. reg += ";InstallDir]";
  1273. cmSystemTools::ExpandRegistryValues(reg,
  1274. cmSystemTools::KeyWOW64_32);
  1275. if (!(reg == "/registry"))
  1276. {
  1277. installedCompiler = version[i].GeneratorName;
  1278. break;
  1279. }
  1280. }
  1281. }
  1282. cmGlobalGenerator* gen
  1283. = this->CreateGlobalGenerator(installedCompiler.c_str());
  1284. if(!gen)
  1285. {
  1286. gen = new cmGlobalNMakeMakefileGenerator;
  1287. }
  1288. this->SetGlobalGenerator(gen);
  1289. std::cout << "-- Building for: " << gen->GetName() << "\n";
  1290. #else
  1291. this->SetGlobalGenerator(new cmGlobalUnixMakefileGenerator3);
  1292. #endif
  1293. }
  1294. if(!this->GlobalGenerator)
  1295. {
  1296. cmSystemTools::Error("Could not create generator");
  1297. return -1;
  1298. }
  1299. }
  1300. const char* genName = this->CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1301. if(genName)
  1302. {
  1303. if(!this->GlobalGenerator->MatchesGeneratorName(genName))
  1304. {
  1305. std::string message = "Error: generator : ";
  1306. message += this->GlobalGenerator->GetName();
  1307. message += "\nDoes not match the generator used previously: ";
  1308. message += genName;
  1309. message +=
  1310. "\nEither remove the CMakeCache.txt file or choose a different"
  1311. " binary directory.";
  1312. cmSystemTools::Error(message.c_str());
  1313. return -2;
  1314. }
  1315. }
  1316. if(!this->CacheManager->GetCacheValue("CMAKE_GENERATOR"))
  1317. {
  1318. this->CacheManager->AddCacheEntry("CMAKE_GENERATOR",
  1319. this->GlobalGenerator->GetName(),
  1320. "Name of generator.",
  1321. cmCacheManager::INTERNAL);
  1322. this->CacheManager->AddCacheEntry("CMAKE_EXTRA_GENERATOR",
  1323. this->GlobalGenerator->GetExtraGeneratorName(),
  1324. "Name of external makefile project generator.",
  1325. cmCacheManager::INTERNAL);
  1326. }
  1327. if(const char* tsName =
  1328. this->CacheManager->GetCacheValue("CMAKE_GENERATOR_TOOLSET"))
  1329. {
  1330. if(this->GeneratorToolset.empty())
  1331. {
  1332. this->GeneratorToolset = tsName;
  1333. }
  1334. else if(this->GeneratorToolset != tsName)
  1335. {
  1336. std::string message = "Error: generator toolset: ";
  1337. message += this->GeneratorToolset;
  1338. message += "\nDoes not match the toolset used previously: ";
  1339. message += tsName;
  1340. message +=
  1341. "\nEither remove the CMakeCache.txt file or choose a different"
  1342. " binary directory.";
  1343. cmSystemTools::Error(message.c_str());
  1344. return -2;
  1345. }
  1346. }
  1347. else
  1348. {
  1349. this->CacheManager->AddCacheEntry("CMAKE_GENERATOR_TOOLSET",
  1350. this->GeneratorToolset.c_str(),
  1351. "Name of generator toolset.",
  1352. cmCacheManager::INTERNAL);
  1353. }
  1354. if(!this->GeneratorToolset.empty() &&
  1355. !this->GlobalGenerator->SetGeneratorToolset(this->GeneratorToolset))
  1356. {
  1357. return -2;
  1358. }
  1359. // reset any system configuration information, except for when we are
  1360. // InTryCompile. With TryCompile the system info is taken from the parent's
  1361. // info to save time
  1362. if (!this->InTryCompile)
  1363. {
  1364. this->GlobalGenerator->ClearEnabledLanguages();
  1365. }
  1366. // Truncate log files
  1367. if (!this->InTryCompile)
  1368. {
  1369. this->TruncateOutputLog("CMakeOutput.log");
  1370. this->TruncateOutputLog("CMakeError.log");
  1371. }
  1372. // actually do the configure
  1373. this->GlobalGenerator->Configure();
  1374. // Before saving the cache
  1375. // if the project did not define one of the entries below, add them now
  1376. // so users can edit the values in the cache:
  1377. // We used to always present LIBRARY_OUTPUT_PATH and
  1378. // EXECUTABLE_OUTPUT_PATH. They are now documented as old-style and
  1379. // should no longer be used. Therefore we present them only if the
  1380. // project requires compatibility with CMake 2.4. We detect this
  1381. // here by looking for the old CMAKE_BACKWARDS_COMPATIBILITY
  1382. // variable created when CMP0001 is not set to NEW.
  1383. if(this->GetCacheManager()->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1384. {
  1385. if(!this->CacheManager->GetCacheValue("LIBRARY_OUTPUT_PATH"))
  1386. {
  1387. this->CacheManager->AddCacheEntry
  1388. ("LIBRARY_OUTPUT_PATH", "",
  1389. "Single output directory for building all libraries.",
  1390. cmCacheManager::PATH);
  1391. }
  1392. if(!this->CacheManager->GetCacheValue("EXECUTABLE_OUTPUT_PATH"))
  1393. {
  1394. this->CacheManager->AddCacheEntry
  1395. ("EXECUTABLE_OUTPUT_PATH", "",
  1396. "Single output directory for building all executables.",
  1397. cmCacheManager::PATH);
  1398. }
  1399. }
  1400. if(!this->CacheManager->GetCacheValue("CMAKE_USE_RELATIVE_PATHS"))
  1401. {
  1402. this->CacheManager->AddCacheEntry
  1403. ("CMAKE_USE_RELATIVE_PATHS", "OFF",
  1404. "If true, cmake will use relative paths in makefiles and projects.",
  1405. cmCacheManager::BOOL);
  1406. cmCacheManager::CacheIterator it =
  1407. this->CacheManager->GetCacheIterator("CMAKE_USE_RELATIVE_PATHS");
  1408. if ( !it.PropertyExists("ADVANCED") )
  1409. {
  1410. it.SetProperty("ADVANCED", "1");
  1411. }
  1412. }
  1413. if(cmSystemTools::GetFatalErrorOccured() &&
  1414. (!this->CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM") ||
  1415. cmSystemTools::IsOff(this->CacheManager->
  1416. GetCacheValue("CMAKE_MAKE_PROGRAM"))))
  1417. {
  1418. // We must have a bad generator selection. Wipe the cache entry so the
  1419. // user can select another.
  1420. this->CacheManager->RemoveCacheEntry("CMAKE_GENERATOR");
  1421. this->CacheManager->RemoveCacheEntry("CMAKE_EXTRA_GENERATOR");
  1422. }
  1423. cmMakefile* mf=this->GlobalGenerator->GetLocalGenerators()[0]->GetMakefile();
  1424. if (mf->IsOn("CTEST_USE_LAUNCHERS")
  1425. && !this->GetProperty("RULE_LAUNCH_COMPILE", cmProperty::GLOBAL))
  1426. {
  1427. cmSystemTools::Error("CTEST_USE_LAUNCHERS is enabled, but the "
  1428. "RULE_LAUNCH_COMPILE global property is not defined.\n"
  1429. "Did you forget to include(CTest) in the toplevel "
  1430. "CMakeLists.txt ?");
  1431. }
  1432. // only save the cache if there were no fatal errors
  1433. if ( this->GetWorkingMode() == NORMAL_MODE )
  1434. {
  1435. this->CacheManager->SaveCache(this->GetHomeOutputDirectory());
  1436. }
  1437. if(cmSystemTools::GetErrorOccuredFlag())
  1438. {
  1439. return -1;
  1440. }
  1441. return 0;
  1442. }
  1443. void cmake::PreLoadCMakeFiles()
  1444. {
  1445. std::vector<std::string> args;
  1446. std::string pre_load = this->GetHomeDirectory();
  1447. if ( pre_load.size() > 0 )
  1448. {
  1449. pre_load += "/PreLoad.cmake";
  1450. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1451. {
  1452. this->ReadListFile(args, pre_load.c_str());
  1453. }
  1454. }
  1455. pre_load = this->GetHomeOutputDirectory();
  1456. if ( pre_load.size() > 0 )
  1457. {
  1458. pre_load += "/PreLoad.cmake";
  1459. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1460. {
  1461. this->ReadListFile(args, pre_load.c_str());
  1462. }
  1463. }
  1464. }
  1465. // handle a command line invocation
  1466. int cmake::Run(const std::vector<std::string>& args, bool noconfigure)
  1467. {
  1468. // Process the arguments
  1469. this->SetArgs(args);
  1470. if(cmSystemTools::GetErrorOccuredFlag())
  1471. {
  1472. return -1;
  1473. }
  1474. // If we are given a stamp list file check if it is really out of date.
  1475. if(!this->CheckStampList.empty() &&
  1476. cmakeCheckStampList(this->CheckStampList.c_str()))
  1477. {
  1478. return 0;
  1479. }
  1480. // If we are given a stamp file check if it is really out of date.
  1481. if(!this->CheckStampFile.empty() &&
  1482. cmakeCheckStampFile(this->CheckStampFile.c_str()))
  1483. {
  1484. return 0;
  1485. }
  1486. if ( this->GetWorkingMode() == NORMAL_MODE )
  1487. {
  1488. // load the cache
  1489. if(this->LoadCache() < 0)
  1490. {
  1491. cmSystemTools::Error("Error executing cmake::LoadCache(). Aborting.\n");
  1492. return -1;
  1493. }
  1494. }
  1495. else
  1496. {
  1497. this->AddCMakePaths();
  1498. }
  1499. // Add any cache args
  1500. if ( !this->SetCacheArgs(args) )
  1501. {
  1502. cmSystemTools::Error("Problem processing arguments. Aborting.\n");
  1503. return -1;
  1504. }
  1505. // In script mode we terminate after running the script.
  1506. if(this->GetWorkingMode() != NORMAL_MODE)
  1507. {
  1508. if(cmSystemTools::GetErrorOccuredFlag())
  1509. {
  1510. return -1;
  1511. }
  1512. else
  1513. {
  1514. return 0;
  1515. }
  1516. }
  1517. // If MAKEFLAGS are given in the environment, remove the environment
  1518. // variable. This will prevent try-compile from succeeding when it
  1519. // should fail (if "-i" is an option). We cannot simply test
  1520. // whether "-i" is given and remove it because some make programs
  1521. // encode the MAKEFLAGS variable in a strange way.
  1522. if(getenv("MAKEFLAGS"))
  1523. {
  1524. cmSystemTools::PutEnv("MAKEFLAGS=");
  1525. }
  1526. this->PreLoadCMakeFiles();
  1527. if ( noconfigure )
  1528. {
  1529. return 0;
  1530. }
  1531. // now run the global generate
  1532. // Check the state of the build system to see if we need to regenerate.
  1533. if(!this->CheckBuildSystem())
  1534. {
  1535. return 0;
  1536. }
  1537. // If we are doing global generate, we better set start and start
  1538. // output directory to the root of the project.
  1539. std::string oldstartdir = this->GetStartDirectory();
  1540. std::string oldstartoutputdir = this->GetStartOutputDirectory();
  1541. this->SetStartDirectory(this->GetHomeDirectory());
  1542. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  1543. int ret = this->Configure();
  1544. if (ret || this->GetWorkingMode() != NORMAL_MODE)
  1545. {
  1546. #if defined(CMAKE_HAVE_VS_GENERATORS)
  1547. if(!this->VSSolutionFile.empty() && this->GlobalGenerator)
  1548. {
  1549. // CMake is running to regenerate a Visual Studio build tree
  1550. // during a build from the VS IDE. The build files cannot be
  1551. // regenerated, so we should stop the build.
  1552. cmSystemTools::Message(
  1553. "CMake Configure step failed. "
  1554. "Build files cannot be regenerated correctly. "
  1555. "Attempting to stop IDE build.");
  1556. cmGlobalVisualStudioGenerator* gg =
  1557. static_cast<cmGlobalVisualStudioGenerator*>(this->GlobalGenerator);
  1558. gg->CallVisualStudioMacro(cmGlobalVisualStudioGenerator::MacroStop,
  1559. this->VSSolutionFile.c_str());
  1560. }
  1561. #endif
  1562. return ret;
  1563. }
  1564. ret = this->Generate();
  1565. std::string message = "Build files have been written to: ";
  1566. message += this->GetHomeOutputDirectory();
  1567. this->UpdateProgress(message.c_str(), -1);
  1568. if(ret)
  1569. {
  1570. return ret;
  1571. }
  1572. this->SetStartDirectory(oldstartdir.c_str());
  1573. this->SetStartOutputDirectory(oldstartoutputdir.c_str());
  1574. return ret;
  1575. }
  1576. int cmake::Generate()
  1577. {
  1578. if(!this->GlobalGenerator)
  1579. {
  1580. return -1;
  1581. }
  1582. this->GlobalGenerator->Generate();
  1583. if ( !this->GraphVizFile.empty() )
  1584. {
  1585. std::cout << "Generate graphviz: " << this->GraphVizFile << std::endl;
  1586. this->GenerateGraphViz(this->GraphVizFile.c_str());
  1587. }
  1588. if(this->WarnUnusedCli)
  1589. {
  1590. this->RunCheckForUnusedVariables();
  1591. }
  1592. if(cmSystemTools::GetErrorOccuredFlag())
  1593. {
  1594. return -1;
  1595. }
  1596. // Save the cache again after a successful Generate so that any internal
  1597. // variables created during Generate are saved. (Specifically target GUIDs
  1598. // for the Visual Studio and Xcode generators.)
  1599. if ( this->GetWorkingMode() == NORMAL_MODE )
  1600. {
  1601. this->CacheManager->SaveCache(this->GetHomeOutputDirectory());
  1602. }
  1603. return 0;
  1604. }
  1605. void cmake::AddCacheEntry(const std::string& key, const char* value,
  1606. const char* helpString,
  1607. int type)
  1608. {
  1609. this->CacheManager->AddCacheEntry(key, value,
  1610. helpString,
  1611. cmCacheManager::CacheEntryType(type));
  1612. }
  1613. const char* cmake::GetCacheDefinition(const std::string& name) const
  1614. {
  1615. return this->CacheManager->GetCacheValue(name);
  1616. }
  1617. void cmake::AddDefaultCommands()
  1618. {
  1619. std::list<cmCommand*> commands;
  1620. GetBootstrapCommands1(commands);
  1621. GetBootstrapCommands2(commands);
  1622. GetPredefinedCommands(commands);
  1623. for(std::list<cmCommand*>::iterator i = commands.begin();
  1624. i != commands.end(); ++i)
  1625. {
  1626. this->AddCommand(*i);
  1627. }
  1628. }
  1629. void cmake::AddDefaultGenerators()
  1630. {
  1631. #if defined(_WIN32) && !defined(__CYGWIN__)
  1632. # if !defined(CMAKE_BOOT_MINGW)
  1633. this->Generators.push_back(
  1634. cmGlobalVisualStudio6Generator::NewFactory());
  1635. this->Generators.push_back(
  1636. cmGlobalVisualStudio7Generator::NewFactory());
  1637. this->Generators.push_back(
  1638. cmGlobalVisualStudio10Generator::NewFactory());
  1639. this->Generators.push_back(
  1640. cmGlobalVisualStudio11Generator::NewFactory());
  1641. this->Generators.push_back(
  1642. cmGlobalVisualStudio12Generator::NewFactory());
  1643. this->Generators.push_back(
  1644. cmGlobalVisualStudio71Generator::NewFactory());
  1645. this->Generators.push_back(
  1646. cmGlobalVisualStudio8Generator::NewFactory());
  1647. this->Generators.push_back(
  1648. cmGlobalVisualStudio9Generator::NewFactory());
  1649. this->Generators.push_back(
  1650. cmGlobalBorlandMakefileGenerator::NewFactory());
  1651. this->Generators.push_back(
  1652. cmGlobalNMakeMakefileGenerator::NewFactory());
  1653. this->Generators.push_back(
  1654. cmGlobalJOMMakefileGenerator::NewFactory());
  1655. this->Generators.push_back(
  1656. cmGlobalWatcomWMakeGenerator::NewFactory());
  1657. # endif
  1658. this->Generators.push_back(
  1659. cmGlobalMSYSMakefileGenerator::NewFactory());
  1660. this->Generators.push_back(
  1661. cmGlobalMinGWMakefileGenerator::NewFactory());
  1662. #endif
  1663. this->Generators.push_back(
  1664. cmGlobalUnixMakefileGenerator3::NewFactory());
  1665. this->Generators.push_back(
  1666. cmGlobalNinjaGenerator::NewFactory());
  1667. #ifdef CMAKE_USE_XCODE
  1668. this->Generators.push_back(
  1669. cmGlobalXCodeGenerator::NewFactory());
  1670. #endif
  1671. }
  1672. int cmake::LoadCache()
  1673. {
  1674. // could we not read the cache
  1675. if (!this->CacheManager->LoadCache(this->GetHomeOutputDirectory()))
  1676. {
  1677. // if it does exist, but isn't readable then warn the user
  1678. std::string cacheFile = this->GetHomeOutputDirectory();
  1679. cacheFile += "/CMakeCache.txt";
  1680. if(cmSystemTools::FileExists(cacheFile.c_str()))
  1681. {
  1682. cmSystemTools::Error(
  1683. "There is a CMakeCache.txt file for the current binary tree but "
  1684. "cmake does not have permission to read it. Please check the "
  1685. "permissions of the directory you are trying to run CMake on.");
  1686. return -1;
  1687. }
  1688. }
  1689. // setup CMAKE_ROOT and CMAKE_COMMAND
  1690. if(!this->AddCMakePaths())
  1691. {
  1692. return -3;
  1693. }
  1694. return 0;
  1695. }
  1696. void cmake::SetProgressCallback(ProgressCallbackType f, void *cd)
  1697. {
  1698. this->ProgressCallback = f;
  1699. this->ProgressCallbackClientData = cd;
  1700. }
  1701. void cmake::UpdateProgress(const char *msg, float prog)
  1702. {
  1703. if(this->ProgressCallback && !this->InTryCompile)
  1704. {
  1705. (*this->ProgressCallback)(msg, prog, this->ProgressCallbackClientData);
  1706. return;
  1707. }
  1708. }
  1709. void cmake::GetGeneratorDocumentation(std::vector<cmDocumentationEntry>& v)
  1710. {
  1711. for(RegisteredGeneratorsVector::const_iterator i =
  1712. this->Generators.begin(); i != this->Generators.end(); ++i)
  1713. {
  1714. cmDocumentationEntry e;
  1715. (*i)->GetDocumentation(e);
  1716. v.push_back(e);
  1717. }
  1718. for(RegisteredExtraGeneratorsMap::const_iterator i =
  1719. this->ExtraGenerators.begin(); i != this->ExtraGenerators.end(); ++i)
  1720. {
  1721. cmDocumentationEntry e;
  1722. cmExternalMakefileProjectGenerator* generator = (i->second)();
  1723. generator->GetDocumentation(e, i->first.c_str());
  1724. e.Name = i->first;
  1725. delete generator;
  1726. v.push_back(e);
  1727. }
  1728. }
  1729. void cmake::UpdateConversionPathTable()
  1730. {
  1731. // Update the path conversion table with any specified file:
  1732. const char* tablepath =
  1733. this->CacheManager->GetCacheValue("CMAKE_PATH_TRANSLATION_FILE");
  1734. if(tablepath)
  1735. {
  1736. cmsys::ifstream table( tablepath );
  1737. if(!table)
  1738. {
  1739. cmSystemTools::Error("CMAKE_PATH_TRANSLATION_FILE set to ", tablepath,
  1740. ". CMake can not open file.");
  1741. cmSystemTools::ReportLastSystemError("CMake can not open file.");
  1742. }
  1743. else
  1744. {
  1745. std::string a, b;
  1746. while(!table.eof())
  1747. {
  1748. // two entries per line
  1749. table >> a; table >> b;
  1750. cmSystemTools::AddTranslationPath( a.c_str(), b.c_str());
  1751. }
  1752. }
  1753. }
  1754. }
  1755. //----------------------------------------------------------------------------
  1756. int cmake::CheckBuildSystem()
  1757. {
  1758. // We do not need to rerun CMake. Check dependency integrity. Use
  1759. // the make system's VERBOSE environment variable to enable verbose
  1760. // output. This can be skipped by setting CMAKE_NO_VERBOSE (which is set
  1761. // by the Eclipse and KDevelop generators).
  1762. bool verbose = ((cmSystemTools::GetEnv("VERBOSE") != 0)
  1763. && (cmSystemTools::GetEnv("CMAKE_NO_VERBOSE") == 0));
  1764. // This method will check the integrity of the build system if the
  1765. // option was given on the command line. It reads the given file to
  1766. // determine whether CMake should rerun.
  1767. // If no file is provided for the check, we have to rerun.
  1768. if(this->CheckBuildSystemArgument.size() == 0)
  1769. {
  1770. if(verbose)
  1771. {
  1772. cmOStringStream msg;
  1773. msg << "Re-run cmake no build system arguments\n";
  1774. cmSystemTools::Stdout(msg.str().c_str());
  1775. }
  1776. return 1;
  1777. }
  1778. // If the file provided does not exist, we have to rerun.
  1779. if(!cmSystemTools::FileExists(this->CheckBuildSystemArgument.c_str()))
  1780. {
  1781. if(verbose)
  1782. {
  1783. cmOStringStream msg;
  1784. msg << "Re-run cmake missing file: "
  1785. << this->CheckBuildSystemArgument.c_str() << "\n";
  1786. cmSystemTools::Stdout(msg.str().c_str());
  1787. }
  1788. return 1;
  1789. }
  1790. // Read the rerun check file and use it to decide whether to do the
  1791. // global generate.
  1792. cmake cm;
  1793. cmGlobalGenerator gg;
  1794. gg.SetCMakeInstance(&cm);
  1795. cmsys::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  1796. cmMakefile* mf = lg->GetMakefile();
  1797. if(!mf->ReadListFile(0, this->CheckBuildSystemArgument.c_str()) ||
  1798. cmSystemTools::GetErrorOccuredFlag())
  1799. {
  1800. if(verbose)
  1801. {
  1802. cmOStringStream msg;
  1803. msg << "Re-run cmake error reading : "
  1804. << this->CheckBuildSystemArgument.c_str() << "\n";
  1805. cmSystemTools::Stdout(msg.str().c_str());
  1806. }
  1807. // There was an error reading the file. Just rerun.
  1808. return 1;
  1809. }
  1810. if(this->ClearBuildSystem)
  1811. {
  1812. // Get the generator used for this build system.
  1813. const char* genName = mf->GetDefinition("CMAKE_DEPENDS_GENERATOR");
  1814. if(!genName || genName[0] == '\0')
  1815. {
  1816. genName = "Unix Makefiles";
  1817. }
  1818. // Create the generator and use it to clear the dependencies.
  1819. cmsys::auto_ptr<cmGlobalGenerator>
  1820. ggd(this->CreateGlobalGenerator(genName));
  1821. if(ggd.get())
  1822. {
  1823. cmsys::auto_ptr<cmLocalGenerator> lgd(ggd->CreateLocalGenerator());
  1824. lgd->ClearDependencies(mf, verbose);
  1825. }
  1826. }
  1827. // If any byproduct of makefile generation is missing we must re-run.
  1828. std::vector<std::string> products;
  1829. if(const char* productStr = mf->GetDefinition("CMAKE_MAKEFILE_PRODUCTS"))
  1830. {
  1831. cmSystemTools::ExpandListArgument(productStr, products);
  1832. }
  1833. for(std::vector<std::string>::const_iterator pi = products.begin();
  1834. pi != products.end(); ++pi)
  1835. {
  1836. if(!(cmSystemTools::FileExists(pi->c_str()) ||
  1837. cmSystemTools::FileIsSymlink(pi->c_str())))
  1838. {
  1839. if(verbose)
  1840. {
  1841. cmOStringStream msg;
  1842. msg << "Re-run cmake, missing byproduct: " << *pi << "\n";
  1843. cmSystemTools::Stdout(msg.str().c_str());
  1844. }
  1845. return 1;
  1846. }
  1847. }
  1848. // Get the set of dependencies and outputs.
  1849. std::vector<std::string> depends;
  1850. std::vector<std::string> outputs;
  1851. const char* dependsStr = mf->GetDefinition("CMAKE_MAKEFILE_DEPENDS");
  1852. const char* outputsStr = mf->GetDefinition("CMAKE_MAKEFILE_OUTPUTS");
  1853. if(dependsStr && outputsStr)
  1854. {
  1855. cmSystemTools::ExpandListArgument(dependsStr, depends);
  1856. cmSystemTools::ExpandListArgument(outputsStr, outputs);
  1857. }
  1858. if(depends.empty() || outputs.empty())
  1859. {
  1860. // Not enough information was provided to do the test. Just rerun.
  1861. if(verbose)
  1862. {
  1863. cmOStringStream msg;
  1864. msg << "Re-run cmake no CMAKE_MAKEFILE_DEPENDS "
  1865. "or CMAKE_MAKEFILE_OUTPUTS :\n";
  1866. cmSystemTools::Stdout(msg.str().c_str());
  1867. }
  1868. return 1;
  1869. }
  1870. // Find the newest dependency.
  1871. std::vector<std::string>::iterator dep = depends.begin();
  1872. std::string dep_newest = *dep++;
  1873. for(;dep != depends.end(); ++dep)
  1874. {
  1875. int result = 0;
  1876. if(this->FileComparison->FileTimeCompare(dep_newest.c_str(),
  1877. dep->c_str(), &result))
  1878. {
  1879. if(result < 0)
  1880. {
  1881. dep_newest = *dep;
  1882. }
  1883. }
  1884. else
  1885. {
  1886. if(verbose)
  1887. {
  1888. cmOStringStream msg;
  1889. msg << "Re-run cmake: build system dependency is missing\n";
  1890. cmSystemTools::Stdout(msg.str().c_str());
  1891. }
  1892. return 1;
  1893. }
  1894. }
  1895. // Find the oldest output.
  1896. std::vector<std::string>::iterator out = outputs.begin();
  1897. std::string out_oldest = *out++;
  1898. for(;out != outputs.end(); ++out)
  1899. {
  1900. int result = 0;
  1901. if(this->FileComparison->FileTimeCompare(out_oldest.c_str(),
  1902. out->c_str(), &result))
  1903. {
  1904. if(result > 0)
  1905. {
  1906. out_oldest = *out;
  1907. }
  1908. }
  1909. else
  1910. {
  1911. if(verbose)
  1912. {
  1913. cmOStringStream msg;
  1914. msg << "Re-run cmake: build system output is missing\n";
  1915. cmSystemTools::Stdout(msg.str().c_str());
  1916. }
  1917. return 1;
  1918. }
  1919. }
  1920. // If any output is older than any dependency then rerun.
  1921. {
  1922. int result = 0;
  1923. if(!this->FileComparison->FileTimeCompare(out_oldest.c_str(),
  1924. dep_newest.c_str(),
  1925. &result) ||
  1926. result < 0)
  1927. {
  1928. if(verbose)
  1929. {
  1930. cmOStringStream msg;
  1931. msg << "Re-run cmake file: " << out_oldest.c_str()
  1932. << " older than: " << dep_newest.c_str() << "\n";
  1933. cmSystemTools::Stdout(msg.str().c_str());
  1934. }
  1935. return 1;
  1936. }
  1937. }
  1938. // No need to rerun.
  1939. return 0;
  1940. }
  1941. //----------------------------------------------------------------------------
  1942. void cmake::TruncateOutputLog(const char* fname)
  1943. {
  1944. std::string fullPath = this->GetHomeOutputDirectory();
  1945. fullPath += "/";
  1946. fullPath += fname;
  1947. struct stat st;
  1948. if ( ::stat(fullPath.c_str(), &st) )
  1949. {
  1950. return;
  1951. }
  1952. if ( !this->CacheManager->GetCacheValue("CMAKE_CACHEFILE_DIR") )
  1953. {
  1954. cmSystemTools::RemoveFile(fullPath.c_str());
  1955. return;
  1956. }
  1957. off_t fsize = st.st_size;
  1958. const off_t maxFileSize = 50 * 1024;
  1959. if ( fsize < maxFileSize )
  1960. {
  1961. //TODO: truncate file
  1962. return;
  1963. }
  1964. }
  1965. inline std::string removeQuotes(const std::string& s)
  1966. {
  1967. if(s[0] == '\"' && s[s.size()-1] == '\"')
  1968. {
  1969. return s.substr(1, s.size()-2);
  1970. }
  1971. return s;
  1972. }
  1973. void cmake::MarkCliAsUsed(const std::string& variable)
  1974. {
  1975. this->UsedCliVariables[variable] = true;
  1976. }
  1977. void cmake::GenerateGraphViz(const char* fileName) const
  1978. {
  1979. #ifdef CMAKE_BUILD_WITH_CMAKE
  1980. cmsys::auto_ptr<cmGraphVizWriter> gvWriter(
  1981. new cmGraphVizWriter(this->GetGlobalGenerator()->GetLocalGenerators()));
  1982. std::string settingsFile = this->GetHomeOutputDirectory();
  1983. settingsFile += "/CMakeGraphVizOptions.cmake";
  1984. std::string fallbackSettingsFile = this->GetHomeDirectory();
  1985. fallbackSettingsFile += "/CMakeGraphVizOptions.cmake";
  1986. gvWriter->ReadSettings(settingsFile.c_str(), fallbackSettingsFile.c_str());
  1987. gvWriter->WritePerTargetFiles(fileName);
  1988. gvWriter->WriteTargetDependersFiles(fileName);
  1989. gvWriter->WriteGlobalFile(fileName);
  1990. #endif
  1991. }
  1992. void cmake::DefineProperty(const std::string& name,
  1993. cmProperty::ScopeType scope,
  1994. const char *ShortDescription,
  1995. const char *FullDescription,
  1996. bool chained)
  1997. {
  1998. this->PropertyDefinitions[scope].DefineProperty(name,scope,ShortDescription,
  1999. FullDescription,
  2000. chained);
  2001. }
  2002. cmPropertyDefinition *cmake
  2003. ::GetPropertyDefinition(const std::string& name,
  2004. cmProperty::ScopeType scope)
  2005. {
  2006. if (this->IsPropertyDefined(name,scope))
  2007. {
  2008. return &(this->PropertyDefinitions[scope][name]);
  2009. }
  2010. return 0;
  2011. }
  2012. bool cmake::IsPropertyDefined(const std::string& name,
  2013. cmProperty::ScopeType scope)
  2014. {
  2015. return this->PropertyDefinitions[scope].IsPropertyDefined(name);
  2016. }
  2017. bool cmake::IsPropertyChained(const std::string& name,
  2018. cmProperty::ScopeType scope)
  2019. {
  2020. return this->PropertyDefinitions[scope].IsPropertyChained(name);
  2021. }
  2022. void cmake::SetProperty(const std::string& prop, const char* value)
  2023. {
  2024. // Special hook to invalidate cached value.
  2025. if(prop == "DEBUG_CONFIGURATIONS")
  2026. {
  2027. this->DebugConfigs.clear();
  2028. }
  2029. this->Properties.SetProperty(prop, value, cmProperty::GLOBAL);
  2030. }
  2031. void cmake::AppendProperty(const std::string& prop,
  2032. const char* value, bool asString)
  2033. {
  2034. // Special hook to invalidate cached value.
  2035. if(prop == "DEBUG_CONFIGURATIONS")
  2036. {
  2037. this->DebugConfigs.clear();
  2038. }
  2039. this->Properties.AppendProperty(prop, value, cmProperty::GLOBAL, asString);
  2040. }
  2041. const char *cmake::GetProperty(const std::string& prop)
  2042. {
  2043. return this->GetProperty(prop, cmProperty::GLOBAL);
  2044. }
  2045. const char *cmake::GetProperty(const std::string& prop,
  2046. cmProperty::ScopeType scope)
  2047. {
  2048. bool chain = false;
  2049. // watch for special properties
  2050. std::string output = "";
  2051. if ( prop == "CACHE_VARIABLES" )
  2052. {
  2053. cmCacheManager::CacheIterator cit =
  2054. this->GetCacheManager()->GetCacheIterator();
  2055. for ( cit.Begin(); !cit.IsAtEnd(); cit.Next() )
  2056. {
  2057. if ( output.size() )
  2058. {
  2059. output += ";";
  2060. }
  2061. output += cit.GetName();
  2062. }
  2063. this->SetProperty("CACHE_VARIABLES", output.c_str());
  2064. }
  2065. else if ( prop == "COMMANDS" )
  2066. {
  2067. cmake::RegisteredCommandsMap::iterator cmds
  2068. = this->GetCommands()->begin();
  2069. for (unsigned int cc=0 ; cmds != this->GetCommands()->end(); ++ cmds )
  2070. {
  2071. if ( cc > 0 )
  2072. {
  2073. output += ";";
  2074. }
  2075. output += cmds->first.c_str();
  2076. cc++;
  2077. }
  2078. this->SetProperty("COMMANDS",output.c_str());
  2079. }
  2080. else if ( prop == "IN_TRY_COMPILE" )
  2081. {
  2082. this->SetProperty("IN_TRY_COMPILE",
  2083. this->GetIsInTryCompile()? "1":"0");
  2084. }
  2085. else if ( prop == "ENABLED_LANGUAGES" )
  2086. {
  2087. std::string lang;
  2088. if(this->GlobalGenerator)
  2089. {
  2090. std::vector<std::string> enLangs;
  2091. this->GlobalGenerator->GetEnabledLanguages(enLangs);
  2092. const char* sep = "";
  2093. for(std::vector<std::string>::iterator i = enLangs.begin();
  2094. i != enLangs.end(); ++i)
  2095. {
  2096. lang += sep;
  2097. sep = ";";
  2098. lang += *i;
  2099. }
  2100. }
  2101. this->SetProperty("ENABLED_LANGUAGES", lang.c_str());
  2102. }
  2103. return this->Properties.GetPropertyValue(prop, scope, chain);
  2104. }
  2105. bool cmake::GetPropertyAsBool(const std::string& prop)
  2106. {
  2107. return cmSystemTools::IsOn(this->GetProperty(prop));
  2108. }
  2109. int cmake::GetSystemInformation(std::vector<std::string>& args)
  2110. {
  2111. // so create the directory
  2112. std::string resultFile;
  2113. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  2114. std::string destPath = cwd + "/__cmake_systeminformation";
  2115. cmSystemTools::RemoveADirectory(destPath.c_str());
  2116. if (!cmSystemTools::MakeDirectory(destPath.c_str()))
  2117. {
  2118. std::cerr << "Error: --system-information must be run from a "
  2119. "writable directory!\n";
  2120. return 1;
  2121. }
  2122. // process the arguments
  2123. bool writeToStdout = true;
  2124. for(unsigned int i=1; i < args.size(); ++i)
  2125. {
  2126. std::string arg = args[i];
  2127. if(arg.find("-V",0) == 0)
  2128. {
  2129. this->Verbose = true;
  2130. }
  2131. else if(arg.find("-G",0) == 0)
  2132. {
  2133. std::string value = arg.substr(2);
  2134. if(value.size() == 0)
  2135. {
  2136. ++i;
  2137. if(i >= args.size())
  2138. {
  2139. cmSystemTools::Error("No generator specified for -G");
  2140. return -1;
  2141. }
  2142. value = args[i];
  2143. }
  2144. cmGlobalGenerator* gen =
  2145. this->CreateGlobalGenerator(value.c_str());
  2146. if(!gen)
  2147. {
  2148. cmSystemTools::Error("Could not create named generator ",
  2149. value.c_str());
  2150. }
  2151. else
  2152. {
  2153. this->SetGlobalGenerator(gen);
  2154. }
  2155. }
  2156. // no option assume it is the output file
  2157. else
  2158. {
  2159. if (!cmSystemTools::FileIsFullPath(arg.c_str()))
  2160. {
  2161. resultFile = cwd;
  2162. resultFile += "/";
  2163. }
  2164. resultFile += arg;
  2165. writeToStdout = false;
  2166. }
  2167. }
  2168. // we have to find the module directory, so we can copy the files
  2169. this->AddCMakePaths();
  2170. std::string modulesPath =
  2171. this->CacheManager->GetCacheValue("CMAKE_ROOT");
  2172. modulesPath += "/Modules";
  2173. std::string inFile = modulesPath;
  2174. inFile += "/SystemInformation.cmake";
  2175. std::string outFile = destPath;
  2176. outFile += "/CMakeLists.txt";
  2177. // Copy file
  2178. if(!cmSystemTools::cmCopyFile(inFile.c_str(), outFile.c_str()))
  2179. {
  2180. std::cerr << "Error copying file \"" << inFile.c_str()
  2181. << "\" to \"" << outFile.c_str() << "\".\n";
  2182. return 1;
  2183. }
  2184. // do we write to a file or to stdout?
  2185. if (resultFile.size() == 0)
  2186. {
  2187. resultFile = cwd;
  2188. resultFile += "/__cmake_systeminformation/results.txt";
  2189. }
  2190. // now run cmake on the CMakeLists file
  2191. cmSystemTools::ChangeDirectory(destPath.c_str());
  2192. std::vector<std::string> args2;
  2193. args2.push_back(args[0]);
  2194. args2.push_back(destPath);
  2195. std::string resultArg = "-DRESULT_FILE=";
  2196. resultArg += resultFile;
  2197. args2.push_back(resultArg);
  2198. int res = this->Run(args2, false);
  2199. if (res != 0)
  2200. {
  2201. std::cerr << "Error: --system-information failed on internal CMake!\n";
  2202. return res;
  2203. }
  2204. // change back to the original directory
  2205. cmSystemTools::ChangeDirectory(cwd.c_str());
  2206. // echo results to stdout if needed
  2207. if (writeToStdout)
  2208. {
  2209. FILE* fin = cmsys::SystemTools::Fopen(resultFile.c_str(), "r");
  2210. if(fin)
  2211. {
  2212. const int bufferSize = 4096;
  2213. char buffer[bufferSize];
  2214. size_t n;
  2215. while((n = fread(buffer, 1, bufferSize, fin)) > 0)
  2216. {
  2217. for(char* c = buffer; c < buffer+n; ++c)
  2218. {
  2219. putc(*c, stdout);
  2220. }
  2221. fflush(stdout);
  2222. }
  2223. fclose(fin);
  2224. }
  2225. }
  2226. // clean up the directory
  2227. cmSystemTools::RemoveADirectory(destPath.c_str());
  2228. return 0;
  2229. }
  2230. //----------------------------------------------------------------------------
  2231. static bool cmakeCheckStampFile(const char* stampName)
  2232. {
  2233. // The stamp file does not exist. Use the stamp dependencies to
  2234. // determine whether it is really out of date. This works in
  2235. // conjunction with cmLocalVisualStudio7Generator to avoid
  2236. // repeatedly re-running CMake when the user rebuilds the entire
  2237. // solution.
  2238. std::string stampDepends = stampName;
  2239. stampDepends += ".depend";
  2240. #if defined(_WIN32) || defined(__CYGWIN__)
  2241. cmsys::ifstream fin(stampDepends.c_str(), std::ios::in | std::ios::binary);
  2242. #else
  2243. cmsys::ifstream fin(stampDepends.c_str(), std::ios::in);
  2244. #endif
  2245. if(!fin)
  2246. {
  2247. // The stamp dependencies file cannot be read. Just assume the
  2248. // build system is really out of date.
  2249. std::cout << "CMake is re-running because " << stampName
  2250. << " dependency file is missing.\n";
  2251. return false;
  2252. }
  2253. // Compare the stamp dependencies against the dependency file itself.
  2254. cmFileTimeComparison ftc;
  2255. std::string dep;
  2256. while(cmSystemTools::GetLineFromStream(fin, dep))
  2257. {
  2258. int result;
  2259. if(dep.length() >= 1 && dep[0] != '#' &&
  2260. (!ftc.FileTimeCompare(stampDepends.c_str(), dep.c_str(), &result)
  2261. || result < 0))
  2262. {
  2263. // The stamp depends file is older than this dependency. The
  2264. // build system is really out of date.
  2265. std::cout << "CMake is re-running because " << stampName
  2266. << " is out-of-date.\n";
  2267. std::cout << " the file '" << dep << "'\n";
  2268. std::cout << " is newer than '" << stampDepends << "'\n";
  2269. std::cout << " result='" << result << "'\n";
  2270. return false;
  2271. }
  2272. }
  2273. // The build system is up to date. The stamp file has been removed
  2274. // by the VS IDE due to a "rebuild" request. Restore it atomically.
  2275. cmOStringStream stampTempStream;
  2276. stampTempStream << stampName << ".tmp" << cmSystemTools::RandomSeed();
  2277. std::string stampTempString = stampTempStream.str();
  2278. const char* stampTemp = stampTempString.c_str();
  2279. {
  2280. // TODO: Teach cmGeneratedFileStream to use a random temp file (with
  2281. // multiple tries in unlikely case of conflict) and use that here.
  2282. cmsys::ofstream stamp(stampTemp);
  2283. stamp << "# CMake generation timestamp file for this directory.\n";
  2284. }
  2285. if(cmSystemTools::RenameFile(stampTemp, stampName))
  2286. {
  2287. // Notify the user why CMake is not re-running. It is safe to
  2288. // just print to stdout here because this code is only reachable
  2289. // through an undocumented flag used by the VS generator.
  2290. std::cout << "CMake does not need to re-run because "
  2291. << stampName << " is up-to-date.\n";
  2292. return true;
  2293. }
  2294. else
  2295. {
  2296. cmSystemTools::RemoveFile(stampTemp);
  2297. cmSystemTools::Error("Cannot restore timestamp ", stampName);
  2298. return false;
  2299. }
  2300. }
  2301. //----------------------------------------------------------------------------
  2302. static bool cmakeCheckStampList(const char* stampList)
  2303. {
  2304. // If the stamp list does not exist CMake must rerun to generate it.
  2305. if(!cmSystemTools::FileExists(stampList))
  2306. {
  2307. std::cout << "CMake is re-running because generate.stamp.list "
  2308. << "is missing.\n";
  2309. return false;
  2310. }
  2311. cmsys::ifstream fin(stampList);
  2312. if(!fin)
  2313. {
  2314. std::cout << "CMake is re-running because generate.stamp.list "
  2315. << "could not be read.\n";
  2316. return false;
  2317. }
  2318. // Check each stamp.
  2319. std::string stampName;
  2320. while(cmSystemTools::GetLineFromStream(fin, stampName))
  2321. {
  2322. if(!cmakeCheckStampFile(stampName.c_str()))
  2323. {
  2324. return false;
  2325. }
  2326. }
  2327. return true;
  2328. }
  2329. //----------------------------------------------------------------------------
  2330. void cmake::IssueMessage(cmake::MessageType t, std::string const& text,
  2331. cmListFileBacktrace const& backtrace)
  2332. {
  2333. cmOStringStream msg;
  2334. bool isError = false;
  2335. // Construct the message header.
  2336. if(t == cmake::FATAL_ERROR)
  2337. {
  2338. isError = true;
  2339. msg << "CMake Error";
  2340. }
  2341. else if(t == cmake::INTERNAL_ERROR)
  2342. {
  2343. isError = true;
  2344. msg << "CMake Internal Error (please report a bug)";
  2345. }
  2346. else if(t == cmake::LOG)
  2347. {
  2348. msg << "CMake Debug Log";
  2349. }
  2350. else if(t == cmake::DEPRECATION_ERROR)
  2351. {
  2352. msg << "CMake Deprecation Error";
  2353. isError = true;
  2354. }
  2355. else if (t == cmake::DEPRECATION_WARNING)
  2356. {
  2357. msg << "CMake Deprecation Warning";
  2358. }
  2359. else
  2360. {
  2361. msg << "CMake Warning";
  2362. if(t == cmake::AUTHOR_WARNING)
  2363. {
  2364. // Allow suppression of these warnings.
  2365. cmCacheManager::CacheIterator it = this->CacheManager
  2366. ->GetCacheIterator("CMAKE_SUPPRESS_DEVELOPER_WARNINGS");
  2367. if(!it.IsAtEnd() && it.GetValueAsBool())
  2368. {
  2369. return;
  2370. }
  2371. msg << " (dev)";
  2372. }
  2373. }
  2374. // Add the immediate context.
  2375. cmListFileBacktrace::const_iterator i = backtrace.begin();
  2376. if(i != backtrace.end())
  2377. {
  2378. cmListFileContext const& lfc = *i;
  2379. msg << (lfc.Line? " at ": " in ") << lfc;
  2380. ++i;
  2381. }
  2382. // Add the message text.
  2383. {
  2384. msg << ":\n";
  2385. cmDocumentationFormatter formatter;
  2386. formatter.SetIndent(" ");
  2387. formatter.PrintFormatted(msg, text.c_str());
  2388. }
  2389. // Add the rest of the context.
  2390. if(i != backtrace.end())
  2391. {
  2392. msg << "Call Stack (most recent call first):\n";
  2393. while(i != backtrace.end())
  2394. {
  2395. cmListFileContext const& lfc = *i;
  2396. msg << " " << lfc << "\n";
  2397. ++i;
  2398. }
  2399. }
  2400. // Add a note about warning suppression.
  2401. if(t == cmake::AUTHOR_WARNING)
  2402. {
  2403. msg <<
  2404. "This warning is for project developers. Use -Wno-dev to suppress it.";
  2405. }
  2406. // Add a terminating blank line.
  2407. msg << "\n";
  2408. // Output the message.
  2409. if(isError)
  2410. {
  2411. cmSystemTools::SetErrorOccured();
  2412. cmSystemTools::Message(msg.str().c_str(), "Error");
  2413. }
  2414. else
  2415. {
  2416. cmSystemTools::Message(msg.str().c_str(), "Warning");
  2417. }
  2418. }
  2419. //----------------------------------------------------------------------------
  2420. std::vector<std::string> const& cmake::GetDebugConfigs()
  2421. {
  2422. // Compute on-demand.
  2423. if(this->DebugConfigs.empty())
  2424. {
  2425. if(const char* config_list = this->GetProperty("DEBUG_CONFIGURATIONS"))
  2426. {
  2427. // Expand the specified list and convert to upper-case.
  2428. cmSystemTools::ExpandListArgument(config_list, this->DebugConfigs);
  2429. for(std::vector<std::string>::iterator i = this->DebugConfigs.begin();
  2430. i != this->DebugConfigs.end(); ++i)
  2431. {
  2432. *i = cmSystemTools::UpperCase(*i);
  2433. }
  2434. }
  2435. // If no configurations were specified, use a default list.
  2436. if(this->DebugConfigs.empty())
  2437. {
  2438. this->DebugConfigs.push_back("DEBUG");
  2439. }
  2440. }
  2441. return this->DebugConfigs;
  2442. }
  2443. int cmake::Build(const std::string& dir,
  2444. const std::string& target,
  2445. const std::string& config,
  2446. const std::vector<std::string>& nativeOptions,
  2447. bool clean)
  2448. {
  2449. if(!cmSystemTools::FileIsDirectory(dir.c_str()))
  2450. {
  2451. std::cerr << "Error: " << dir << " is not a directory\n";
  2452. return 1;
  2453. }
  2454. std::string cachePath = dir;
  2455. cmSystemTools::ConvertToUnixSlashes(cachePath);
  2456. cmCacheManager* cachem = this->GetCacheManager();
  2457. cmCacheManager::CacheIterator it = cachem->NewIterator();
  2458. if(!cachem->LoadCache(cachePath.c_str()))
  2459. {
  2460. std::cerr << "Error: could not load cache\n";
  2461. return 1;
  2462. }
  2463. if(!it.Find("CMAKE_GENERATOR"))
  2464. {
  2465. std::cerr << "Error: could find generator in Cache\n";
  2466. return 1;
  2467. }
  2468. cmsys::auto_ptr<cmGlobalGenerator> gen(
  2469. this->CreateGlobalGenerator(it.GetValue()));
  2470. std::string output;
  2471. std::string projName;
  2472. if(!it.Find("CMAKE_PROJECT_NAME"))
  2473. {
  2474. std::cerr << "Error: could not find CMAKE_PROJECT_NAME in Cache\n";
  2475. return 1;
  2476. }
  2477. projName = it.GetValue();
  2478. return gen->Build("", dir.c_str(),
  2479. projName.c_str(), target.c_str(),
  2480. &output,
  2481. 0,
  2482. config.c_str(), clean, false, 0,
  2483. cmSystemTools::OUTPUT_PASSTHROUGH,
  2484. nativeOptions);
  2485. }
  2486. void cmake::WatchUnusedCli(const std::string& var)
  2487. {
  2488. #ifdef CMAKE_BUILD_WITH_CMAKE
  2489. this->VariableWatch->AddWatch(var, cmWarnUnusedCliWarning, this);
  2490. if(this->UsedCliVariables.find(var) == this->UsedCliVariables.end())
  2491. {
  2492. this->UsedCliVariables[var] = false;
  2493. }
  2494. #endif
  2495. }
  2496. void cmake::UnwatchUnusedCli(const std::string& var)
  2497. {
  2498. #ifdef CMAKE_BUILD_WITH_CMAKE
  2499. this->VariableWatch->RemoveWatch(var, cmWarnUnusedCliWarning);
  2500. this->UsedCliVariables.erase(var);
  2501. #endif
  2502. }
  2503. void cmake::RunCheckForUnusedVariables()
  2504. {
  2505. #ifdef CMAKE_BUILD_WITH_CMAKE
  2506. bool haveUnused = false;
  2507. cmOStringStream msg;
  2508. msg << "Manually-specified variables were not used by the project:";
  2509. for(std::map<std::string, bool>::const_iterator
  2510. it = this->UsedCliVariables.begin();
  2511. it != this->UsedCliVariables.end(); ++it)
  2512. {
  2513. if(!it->second)
  2514. {
  2515. haveUnused = true;
  2516. msg << "\n " << it->first;
  2517. }
  2518. }
  2519. if(haveUnused)
  2520. {
  2521. this->IssueMessage(cmake::WARNING, msg.str(), cmListFileBacktrace());
  2522. }
  2523. #endif
  2524. }