cmake.cxx 81 KB

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