cmake.cxx 81 KB

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