cmake.cxx 86 KB

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