cmake.cxx 82 KB

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