cmake.cxx 80 KB

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