1
0

cmake.cxx 82 KB

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