cmake.cxx 81 KB

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