cmake.cxx 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmake.h"
  14. #include "time.h"
  15. #include "cmCacheManager.h"
  16. #include "cmMakefile.h"
  17. #include "cmLocalGenerator.h"
  18. #include "cmExternalMakefileProjectGenerator.h"
  19. #include "cmCommands.h"
  20. #include "cmCommand.h"
  21. #include "cmFileTimeComparison.h"
  22. #include "cmGeneratedFileStream.h"
  23. #include "cmSourceFile.h"
  24. #include "cmVersion.h"
  25. #include "cmTest.h"
  26. #if defined(CMAKE_BUILD_WITH_CMAKE)
  27. # include "cmDependsFortran.h" // For -E cmake_copy_f90_mod callback.
  28. # include "cmVariableWatch.h"
  29. # include <cmsys/Terminal.h>
  30. #endif
  31. #include <cmsys/Directory.hxx>
  32. #include <cmsys/Process.h>
  33. #include <cmsys/Glob.hxx>
  34. #include <cmsys/RegularExpression.hxx>
  35. // only build kdevelop generator on non-windows platforms
  36. // when not bootstrapping cmake
  37. #if !defined(_WIN32)
  38. # if defined(CMAKE_BUILD_WITH_CMAKE)
  39. # define CMAKE_USE_KDEVELOP
  40. # endif
  41. #endif
  42. #if defined(__MINGW32__) && !defined(CMAKE_BUILD_WITH_CMAKE)
  43. # define CMAKE_BOOT_MINGW
  44. #endif
  45. // include the generator
  46. #if defined(_WIN32) && !defined(__CYGWIN__)
  47. # if !defined(CMAKE_BOOT_MINGW)
  48. # include "cmGlobalVisualStudio6Generator.h"
  49. # include "cmGlobalVisualStudio7Generator.h"
  50. # include "cmGlobalVisualStudio71Generator.h"
  51. # include "cmGlobalVisualStudio8Generator.h"
  52. # include "cmGlobalVisualStudio8Win64Generator.h"
  53. # include "cmGlobalBorlandMakefileGenerator.h"
  54. # include "cmGlobalNMakeMakefileGenerator.h"
  55. # include "cmGlobalWatcomWMakeGenerator.h"
  56. # endif
  57. # include "cmGlobalMSYSMakefileGenerator.h"
  58. # include "cmGlobalMinGWMakefileGenerator.h"
  59. # include "cmWin32ProcessExecution.h"
  60. #else
  61. #endif
  62. #include "cmGlobalUnixMakefileGenerator3.h"
  63. #if !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW)
  64. # include "cmExtraCodeBlocksGenerator.h"
  65. #endif
  66. #ifdef CMAKE_USE_KDEVELOP
  67. # include "cmGlobalKdevelopGenerator.h"
  68. #endif
  69. #include <stdlib.h> // required for atoi
  70. #if defined( __APPLE__ )
  71. # if defined(CMAKE_BUILD_WITH_CMAKE)
  72. # include "cmGlobalXCodeGenerator.h"
  73. # define CMAKE_USE_XCODE 1
  74. # endif
  75. # include <sys/types.h>
  76. # include <sys/time.h>
  77. # include <sys/resource.h>
  78. #endif
  79. #include <sys/stat.h> // struct stat
  80. #include <memory> // auto_ptr
  81. void cmNeedBackwardsCompatibility(const std::string& variable,
  82. int access_type, void*, const char*, const cmMakefile*)
  83. {
  84. #ifdef CMAKE_BUILD_WITH_CMAKE
  85. if (access_type == cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS)
  86. {
  87. std::string message = "An attempt was made to access a variable: ";
  88. message += variable;
  89. message +=
  90. " that has not been defined. Some variables were always defined "
  91. "by CMake in versions prior to 1.6. To fix this you might need to set "
  92. "the cache value of CMAKE_BACKWARDS_COMPATIBILITY to 1.4 or less. If "
  93. "you are writing a CMakeList file, (or have already set "
  94. "CMAKE_BACKWARDS_COMPATABILITY to 1.4 or less) then you probably need "
  95. "to include a CMake module to test for the feature this variable "
  96. "defines.";
  97. cmSystemTools::Error(message.c_str());
  98. }
  99. #else
  100. (void)variable;
  101. (void)access_type;
  102. #endif
  103. }
  104. cmake::cmake()
  105. {
  106. this->DebugOutput = false;
  107. this->DebugTryCompile = false;
  108. this->ClearBuildSystem = false;
  109. this->FileComparison = new cmFileTimeComparison;
  110. this->Properties.SetCMakeInstance(this);
  111. // initialize properties
  112. cmSourceFile::DefineProperties(this);
  113. cmTarget::DefineProperties(this);
  114. cmMakefile::DefineProperties(this);
  115. cmTest::DefineProperties(this);
  116. cmake::DefineProperties(this);
  117. #ifdef __APPLE__
  118. struct rlimit rlp;
  119. if(!getrlimit(RLIMIT_STACK, &rlp))
  120. {
  121. if(rlp.rlim_cur != rlp.rlim_max)
  122. {
  123. rlp.rlim_cur = rlp.rlim_max;
  124. setrlimit(RLIMIT_STACK, &rlp);
  125. }
  126. }
  127. #endif
  128. // If MAKEFLAGS are given in the environment, remove the environment
  129. // variable. This will prevent try-compile from succeeding when it
  130. // should fail (if "-i" is an option). We cannot simply test
  131. // whether "-i" is given and remove it because some make programs
  132. // encode the MAKEFLAGS variable in a strange way.
  133. if(getenv("MAKEFLAGS"))
  134. {
  135. cmSystemTools::PutEnv("MAKEFLAGS=");
  136. }
  137. this->Verbose = false;
  138. this->InTryCompile = false;
  139. this->CacheManager = new cmCacheManager;
  140. this->GlobalGenerator = 0;
  141. this->ProgressCallback = 0;
  142. this->ProgressCallbackClientData = 0;
  143. this->ScriptMode = false;
  144. #ifdef CMAKE_BUILD_WITH_CMAKE
  145. this->VariableWatch = new cmVariableWatch;
  146. this->VariableWatch->AddWatch("CMAKE_WORDS_BIGENDIAN",
  147. cmNeedBackwardsCompatibility);
  148. this->VariableWatch->AddWatch("CMAKE_SIZEOF_INT",
  149. cmNeedBackwardsCompatibility);
  150. this->VariableWatch->AddWatch("CMAKE_X_LIBS",
  151. cmNeedBackwardsCompatibility);
  152. #endif
  153. this->AddDefaultGenerators();
  154. this->AddDefaultExtraGenerators();
  155. this->AddDefaultCommands();
  156. // Make sure we can capture the build tool output.
  157. cmSystemTools::EnableVSConsoleOutput();
  158. }
  159. cmake::~cmake()
  160. {
  161. delete this->CacheManager;
  162. if (this->GlobalGenerator)
  163. {
  164. delete this->GlobalGenerator;
  165. this->GlobalGenerator = 0;
  166. }
  167. for(RegisteredCommandsMap::iterator j = this->Commands.begin();
  168. j != this->Commands.end(); ++j)
  169. {
  170. delete (*j).second;
  171. }
  172. #ifdef CMAKE_BUILD_WITH_CMAKE
  173. delete this->VariableWatch;
  174. #endif
  175. delete this->FileComparison;
  176. }
  177. void cmake::CleanupCommandsAndMacros()
  178. {
  179. std::vector<cmCommand*> commands;
  180. for(RegisteredCommandsMap::iterator j = this->Commands.begin();
  181. j != this->Commands.end(); ++j)
  182. {
  183. if ( !j->second->IsA("cmMacroHelperCommand") )
  184. {
  185. commands.push_back(j->second);
  186. }
  187. else
  188. {
  189. delete j->second;
  190. }
  191. }
  192. this->Commands.erase(this->Commands.begin(), this->Commands.end());
  193. std::vector<cmCommand*>::iterator it;
  194. for ( it = commands.begin(); it != commands.end();
  195. ++ it )
  196. {
  197. this->Commands[cmSystemTools::LowerCase((*it)->GetName())] = *it;
  198. }
  199. }
  200. bool cmake::CommandExists(const char* name) const
  201. {
  202. std::string sName = cmSystemTools::LowerCase(name);
  203. return (this->Commands.find(sName) != this->Commands.end());
  204. }
  205. cmCommand *cmake::GetCommand(const char *name)
  206. {
  207. cmCommand* rm = 0;
  208. std::string sName = cmSystemTools::LowerCase(name);
  209. RegisteredCommandsMap::iterator pos = this->Commands.find(sName);
  210. if (pos != this->Commands.end())
  211. {
  212. rm = (*pos).second;
  213. }
  214. return rm;
  215. }
  216. void cmake::RenameCommand(const char*oldName, const char* newName)
  217. {
  218. // if the command already exists, free the old one
  219. std::string sOldName = cmSystemTools::LowerCase(oldName);
  220. std::string sNewName = cmSystemTools::LowerCase(newName);
  221. RegisteredCommandsMap::iterator pos = this->Commands.find(sOldName);
  222. if ( pos == this->Commands.end() )
  223. {
  224. return;
  225. }
  226. cmCommand* cmd = pos->second;
  227. pos = this->Commands.find(sNewName);
  228. if (pos != this->Commands.end())
  229. {
  230. delete pos->second;
  231. this->Commands.erase(pos);
  232. }
  233. this->Commands.insert(RegisteredCommandsMap::value_type(sNewName, cmd));
  234. pos = this->Commands.find(sOldName);
  235. this->Commands.erase(pos);
  236. }
  237. void cmake::RemoveCommand(const char* name)
  238. {
  239. std::string sName = cmSystemTools::LowerCase(name);
  240. RegisteredCommandsMap::iterator pos = this->Commands.find(sName);
  241. if ( pos != this->Commands.end() )
  242. {
  243. delete pos->second;
  244. this->Commands.erase(pos);
  245. }
  246. }
  247. void cmake::AddCommand(cmCommand* wg)
  248. {
  249. std::string name = cmSystemTools::LowerCase(wg->GetName());
  250. // if the command already exists, free the old one
  251. RegisteredCommandsMap::iterator pos = this->Commands.find(name);
  252. if (pos != this->Commands.end())
  253. {
  254. delete pos->second;
  255. this->Commands.erase(pos);
  256. }
  257. this->Commands.insert( RegisteredCommandsMap::value_type(name, wg));
  258. }
  259. // Parse the args
  260. bool cmake::SetCacheArgs(const std::vector<std::string>& args)
  261. {
  262. for(unsigned int i=1; i < args.size(); ++i)
  263. {
  264. std::string arg = args[i];
  265. if(arg.find("-D",0) == 0)
  266. {
  267. std::string entry = arg.substr(2);
  268. if(entry.size() == 0)
  269. {
  270. ++i;
  271. if(i < args.size())
  272. {
  273. entry = args[i];
  274. }
  275. else
  276. {
  277. cmSystemTools::Error("-D must be followed with VAR=VALUE.");
  278. return false;
  279. }
  280. }
  281. std::string var, value;
  282. cmCacheManager::CacheEntryType type = cmCacheManager::UNINITIALIZED;
  283. if(cmCacheManager::ParseEntry(entry.c_str(), var, value, type) ||
  284. cmCacheManager::ParseEntry(entry.c_str(), var, value))
  285. {
  286. this->CacheManager->AddCacheEntry(var.c_str(), value.c_str(),
  287. "No help, variable specified on the command line.", type);
  288. }
  289. else
  290. {
  291. std::cerr << "Parse error in command line argument: " << arg << "\n"
  292. << "Should be: VAR:type=value\n";
  293. cmSystemTools::Error("No cmake scrpt provided.");
  294. return false;
  295. }
  296. }
  297. else if(arg.find("-U",0) == 0)
  298. {
  299. std::string entryPattern = arg.substr(2);
  300. if(entryPattern.size() == 0)
  301. {
  302. ++i;
  303. if(i < args.size())
  304. {
  305. entryPattern = args[i];
  306. }
  307. else
  308. {
  309. cmSystemTools::Error("-U must be followed with VAR.");
  310. return false;
  311. }
  312. }
  313. cmsys::RegularExpression regex(
  314. cmsys::Glob::PatternToRegex(entryPattern.c_str(), true).c_str());
  315. //go through all cache entries and collect the vars which will be removed
  316. std::vector<std::string> entriesToDelete;
  317. cmCacheManager::CacheIterator it =
  318. this->CacheManager->GetCacheIterator();
  319. for ( it.Begin(); !it.IsAtEnd(); it.Next() )
  320. {
  321. cmCacheManager::CacheEntryType t = it.GetType();
  322. if(t != cmCacheManager::STATIC)
  323. {
  324. std::string entryName = it.GetName();
  325. if (regex.find(entryName.c_str()))
  326. {
  327. entriesToDelete.push_back(entryName);
  328. }
  329. }
  330. }
  331. // now remove them from the cache
  332. for(std::vector<std::string>::const_iterator currentEntry =
  333. entriesToDelete.begin();
  334. currentEntry != entriesToDelete.end();
  335. ++currentEntry)
  336. {
  337. this->CacheManager->RemoveCacheEntry(currentEntry->c_str());
  338. }
  339. }
  340. else if(arg.find("-C",0) == 0)
  341. {
  342. std::string path = arg.substr(2);
  343. if ( path.size() == 0 )
  344. {
  345. ++i;
  346. if(i < args.size())
  347. {
  348. path = args[i];
  349. }
  350. else
  351. {
  352. cmSystemTools::Error("-C must be followed by a file name.");
  353. return false;
  354. }
  355. }
  356. std::cerr << "loading initial cache file " << path.c_str() << "\n";
  357. this->ReadListFile(path.c_str());
  358. }
  359. else if(arg.find("-P",0) == 0)
  360. {
  361. i++;
  362. if(i >= args.size())
  363. {
  364. cmSystemTools::Error("-P must be followed by a file name.");
  365. return false;
  366. }
  367. std::string path = args[i];
  368. if ( path.size() == 0 )
  369. {
  370. cmSystemTools::Error("No cmake script provided.");
  371. return false;
  372. }
  373. this->ReadListFile(path.c_str());
  374. }
  375. }
  376. return true;
  377. }
  378. void cmake::ReadListFile(const char *path)
  379. {
  380. // if a generator was not yet created, temporarily create one
  381. cmGlobalGenerator *gg = this->GetGlobalGenerator();
  382. bool created = false;
  383. // if a generator was not specified use a generic one
  384. if (!gg)
  385. {
  386. gg = new cmGlobalGenerator;
  387. gg->SetCMakeInstance(this);
  388. created = true;
  389. }
  390. // read in the list file to fill the cache
  391. if(path)
  392. {
  393. std::auto_ptr<cmLocalGenerator> lg(gg->CreateLocalGenerator());
  394. lg->SetGlobalGenerator(gg);
  395. lg->GetMakefile()->SetHomeOutputDirectory
  396. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  397. lg->GetMakefile()->SetStartOutputDirectory
  398. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  399. lg->GetMakefile()->SetHomeDirectory
  400. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  401. lg->GetMakefile()->SetStartDirectory
  402. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  403. if (!lg->GetMakefile()->ReadListFile(0, path))
  404. {
  405. cmSystemTools::Error("Error processing file:", path);
  406. }
  407. }
  408. // free generic one if generated
  409. if (created)
  410. {
  411. delete gg;
  412. }
  413. }
  414. // Parse the args
  415. void cmake::SetArgs(const std::vector<std::string>& args)
  416. {
  417. bool directoriesSet = false;
  418. for(unsigned int i=1; i < args.size(); ++i)
  419. {
  420. std::string arg = args[i];
  421. if(arg.find("-H",0) == 0)
  422. {
  423. directoriesSet = true;
  424. std::string path = arg.substr(2);
  425. path = cmSystemTools::CollapseFullPath(path.c_str());
  426. cmSystemTools::ConvertToUnixSlashes(path);
  427. this->SetHomeDirectory(path.c_str());
  428. }
  429. else if(arg.find("-S",0) == 0)
  430. {
  431. // There is no local generate anymore. Ignore -S option.
  432. }
  433. else if(arg.find("-O",0) == 0)
  434. {
  435. // There is no local generate anymore. Ignore -O option.
  436. }
  437. else if(arg.find("-B",0) == 0)
  438. {
  439. directoriesSet = true;
  440. std::string path = arg.substr(2);
  441. path = cmSystemTools::CollapseFullPath(path.c_str());
  442. cmSystemTools::ConvertToUnixSlashes(path);
  443. this->SetHomeOutputDirectory(path.c_str());
  444. }
  445. else if((i < args.size()-1) && (arg.find("--check-build-system",0) == 0))
  446. {
  447. this->CheckBuildSystemArgument = args[++i];
  448. this->ClearBuildSystem = (atoi(args[++i].c_str()) > 0);
  449. }
  450. else if(arg.find("-V",0) == 0)
  451. {
  452. this->Verbose = true;
  453. }
  454. else if(arg.find("-D",0) == 0)
  455. {
  456. // skip for now
  457. }
  458. else if(arg.find("-U",0) == 0)
  459. {
  460. // skip for now
  461. }
  462. else if(arg.find("-C",0) == 0)
  463. {
  464. // skip for now
  465. }
  466. else if(arg.find("-P",0) == 0)
  467. {
  468. // skip for now
  469. i++;
  470. }
  471. else if(arg.find("--graphviz=",0) == 0)
  472. {
  473. std::string path = arg.substr(strlen("--graphviz="));
  474. path = cmSystemTools::CollapseFullPath(path.c_str());
  475. cmSystemTools::ConvertToUnixSlashes(path);
  476. this->GraphVizFile = path;
  477. if ( this->GraphVizFile.empty() )
  478. {
  479. cmSystemTools::Error("No file specified for --graphviz");
  480. }
  481. }
  482. else if(arg.find("--debug-trycompile",0) == 0)
  483. {
  484. std::cout << "debug trycompile on\n";
  485. this->DebugTryCompileOn();
  486. }
  487. else if(arg.find("--debug-output",0) == 0)
  488. {
  489. std::cout << "Running with debug output on.\n";
  490. this->DebugOutputOn();
  491. }
  492. else if(arg.find("-G",0) == 0)
  493. {
  494. std::string value = arg.substr(2);
  495. if(value.size() == 0)
  496. {
  497. ++i;
  498. if(i >= args.size())
  499. {
  500. cmSystemTools::Error("No generator specified for -G");
  501. return;
  502. }
  503. value = args[i];
  504. }
  505. cmGlobalGenerator* gen =
  506. this->CreateGlobalGenerator(value.c_str());
  507. if(!gen)
  508. {
  509. cmSystemTools::Error("Could not create named generator ",
  510. value.c_str());
  511. }
  512. else
  513. {
  514. this->SetGlobalGenerator(gen);
  515. }
  516. }
  517. // no option assume it is the path to the source
  518. else
  519. {
  520. directoriesSet = true;
  521. this->SetDirectoriesFromFile(arg.c_str());
  522. }
  523. }
  524. if(!directoriesSet)
  525. {
  526. this->SetHomeOutputDirectory
  527. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  528. this->SetStartOutputDirectory
  529. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  530. this->SetHomeDirectory
  531. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  532. this->SetStartDirectory
  533. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  534. }
  535. this->SetStartDirectory(this->GetHomeDirectory());
  536. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  537. }
  538. //----------------------------------------------------------------------------
  539. void cmake::SetDirectoriesFromFile(const char* arg)
  540. {
  541. // Check if the argument refers to a CMakeCache.txt or
  542. // CMakeLists.txt file.
  543. std::string listPath;
  544. std::string cachePath;
  545. bool argIsFile = false;
  546. if(cmSystemTools::FileIsDirectory(arg))
  547. {
  548. std::string path = cmSystemTools::CollapseFullPath(arg);
  549. cmSystemTools::ConvertToUnixSlashes(path);
  550. std::string cacheFile = path;
  551. cacheFile += "/CMakeCache.txt";
  552. std::string listFile = path;
  553. listFile += "/CMakeLists.txt";
  554. if(cmSystemTools::FileExists(cacheFile.c_str()))
  555. {
  556. cachePath = path;
  557. }
  558. if(cmSystemTools::FileExists(listFile.c_str()))
  559. {
  560. listPath = path;
  561. }
  562. }
  563. else if(cmSystemTools::FileExists(arg))
  564. {
  565. argIsFile = true;
  566. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  567. std::string name = cmSystemTools::GetFilenameName(fullPath.c_str());
  568. name = cmSystemTools::LowerCase(name);
  569. if(name == "cmakecache.txt")
  570. {
  571. cachePath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  572. }
  573. else if(name == "cmakelists.txt")
  574. {
  575. listPath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  576. }
  577. }
  578. else
  579. {
  580. // Specified file or directory does not exist. Try to set things
  581. // up to produce a meaningful error message.
  582. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  583. std::string name = cmSystemTools::GetFilenameName(fullPath.c_str());
  584. name = cmSystemTools::LowerCase(name);
  585. if(name == "cmakecache.txt" || name == "cmakelists.txt")
  586. {
  587. argIsFile = true;
  588. listPath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  589. }
  590. else
  591. {
  592. listPath = fullPath;
  593. }
  594. }
  595. // If there is a CMakeCache.txt file, use its settings.
  596. if(cachePath.length() > 0)
  597. {
  598. cmCacheManager* cachem = this->GetCacheManager();
  599. cmCacheManager::CacheIterator it = cachem->NewIterator();
  600. if(cachem->LoadCache(cachePath.c_str()) &&
  601. it.Find("CMAKE_HOME_DIRECTORY"))
  602. {
  603. this->SetHomeOutputDirectory(cachePath.c_str());
  604. this->SetStartOutputDirectory(cachePath.c_str());
  605. this->SetHomeDirectory(it.GetValue());
  606. this->SetStartDirectory(it.GetValue());
  607. return;
  608. }
  609. }
  610. // If there is a CMakeLists.txt file, use it as the source tree.
  611. if(listPath.length() > 0)
  612. {
  613. this->SetHomeDirectory(listPath.c_str());
  614. this->SetStartDirectory(listPath.c_str());
  615. if(argIsFile)
  616. {
  617. // Source CMakeLists.txt file given. It was probably dropped
  618. // onto the executable in a GUI. Default to an in-source build.
  619. this->SetHomeOutputDirectory(listPath.c_str());
  620. this->SetStartOutputDirectory(listPath.c_str());
  621. }
  622. else
  623. {
  624. // Source directory given on command line. Use current working
  625. // directory as build tree.
  626. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  627. this->SetHomeOutputDirectory(cwd.c_str());
  628. this->SetStartOutputDirectory(cwd.c_str());
  629. }
  630. return;
  631. }
  632. // We didn't find a CMakeLists.txt or CMakeCache.txt file from the
  633. // argument. Assume it is the path to the source tree, and use the
  634. // current working directory as the build tree.
  635. std::string full = cmSystemTools::CollapseFullPath(arg);
  636. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  637. this->SetHomeDirectory(full.c_str());
  638. this->SetStartDirectory(full.c_str());
  639. this->SetHomeOutputDirectory(cwd.c_str());
  640. this->SetStartOutputDirectory(cwd.c_str());
  641. }
  642. // at the end of this CMAKE_ROOT and CMAKE_COMMAND should be added to the
  643. // cache
  644. int cmake::AddCMakePaths(const char *arg0)
  645. {
  646. // Find our own executable.
  647. std::vector<cmStdString> failures;
  648. std::string cMakeSelf = arg0;
  649. cmSystemTools::ConvertToUnixSlashes(cMakeSelf);
  650. failures.push_back(cMakeSelf);
  651. cMakeSelf = cmSystemTools::FindProgram(cMakeSelf.c_str());
  652. cmSystemTools::ConvertToUnixSlashes(cMakeSelf);
  653. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  654. {
  655. #ifdef CMAKE_BUILD_DIR
  656. std::string intdir = ".";
  657. #ifdef CMAKE_INTDIR
  658. intdir = CMAKE_INTDIR;
  659. #endif
  660. cMakeSelf = CMAKE_BUILD_DIR;
  661. cMakeSelf += "/bin/";
  662. cMakeSelf += intdir;
  663. cMakeSelf += "/cmake";
  664. cMakeSelf += cmSystemTools::GetExecutableExtension();
  665. #endif
  666. }
  667. #ifdef CMAKE_PREFIX
  668. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  669. {
  670. failures.push_back(cMakeSelf);
  671. cMakeSelf = CMAKE_PREFIX "/bin/cmake";
  672. }
  673. #endif
  674. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  675. {
  676. failures.push_back(cMakeSelf);
  677. cmOStringStream msg;
  678. msg << "CMAKE can not find the command line program cmake.\n";
  679. msg << " argv[0] = \"" << arg0 << "\"\n";
  680. msg << " Attempted paths:\n";
  681. std::vector<cmStdString>::iterator i;
  682. for(i=failures.begin(); i != failures.end(); ++i)
  683. {
  684. msg << " \"" << i->c_str() << "\"\n";
  685. }
  686. cmSystemTools::Error(msg.str().c_str());
  687. return 0;
  688. }
  689. // Save the value in the cache
  690. this->CacheManager->AddCacheEntry
  691. ("CMAKE_COMMAND",cMakeSelf.c_str(), "Path to CMake executable.",
  692. cmCacheManager::INTERNAL);
  693. // Find and save the command to edit the cache
  694. std::string editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  695. "/ccmake" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  696. if( !cmSystemTools::FileExists(editCacheCommand.c_str()))
  697. {
  698. editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  699. "/CMakeSetup" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  700. }
  701. std::string ctestCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  702. "/ctest" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  703. if(cmSystemTools::FileExists(ctestCommand.c_str()))
  704. {
  705. this->CacheManager->AddCacheEntry
  706. ("CMAKE_CTEST_COMMAND", ctestCommand.c_str(),
  707. "Path to ctest program executable.", cmCacheManager::INTERNAL);
  708. }
  709. if(cmSystemTools::FileExists(editCacheCommand.c_str()))
  710. {
  711. this->CacheManager->AddCacheEntry
  712. ("CMAKE_EDIT_COMMAND", editCacheCommand.c_str(),
  713. "Path to cache edit program executable.", cmCacheManager::INTERNAL);
  714. }
  715. // do CMAKE_ROOT, look for the environment variable first
  716. std::string cMakeRoot;
  717. std::string modules;
  718. if (getenv("CMAKE_ROOT"))
  719. {
  720. cMakeRoot = getenv("CMAKE_ROOT");
  721. modules = cMakeRoot + "/Modules/CMake.cmake";
  722. }
  723. if(!cmSystemTools::FileExists(modules.c_str()))
  724. {
  725. // next try exe/..
  726. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  727. std::string::size_type slashPos = cMakeRoot.rfind("/");
  728. if(slashPos != std::string::npos)
  729. {
  730. cMakeRoot = cMakeRoot.substr(0, slashPos);
  731. }
  732. // is there no Modules direcory there?
  733. modules = cMakeRoot + "/Modules/CMake.cmake";
  734. }
  735. if (!cmSystemTools::FileExists(modules.c_str()))
  736. {
  737. // try exe/../share/cmake
  738. cMakeRoot += CMAKE_DATA_DIR;
  739. modules = cMakeRoot + "/Modules/CMake.cmake";
  740. }
  741. #ifdef CMAKE_ROOT_DIR
  742. if (!cmSystemTools::FileExists(modules.c_str()))
  743. {
  744. // try compiled in root directory
  745. cMakeRoot = CMAKE_ROOT_DIR;
  746. modules = cMakeRoot + "/Modules/CMake.cmake";
  747. }
  748. #endif
  749. #ifdef CMAKE_PREFIX
  750. if (!cmSystemTools::FileExists(modules.c_str()))
  751. {
  752. // try compiled in install prefix
  753. cMakeRoot = CMAKE_PREFIX CMAKE_DATA_DIR;
  754. modules = cMakeRoot + "/Modules/CMake.cmake";
  755. }
  756. #endif
  757. if (!cmSystemTools::FileExists(modules.c_str()))
  758. {
  759. // try
  760. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  761. cMakeRoot += CMAKE_DATA_DIR;
  762. modules = cMakeRoot + "/Modules/CMake.cmake";
  763. }
  764. if(!cmSystemTools::FileExists(modules.c_str()))
  765. {
  766. // next try exe
  767. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  768. // is there no Modules direcory there?
  769. modules = cMakeRoot + "/Modules/CMake.cmake";
  770. }
  771. if (!cmSystemTools::FileExists(modules.c_str()))
  772. {
  773. // couldn't find modules
  774. cmSystemTools::Error("Could not find CMAKE_ROOT !!!\n"
  775. "CMake has most likely not been installed correctly.\n"
  776. "Modules directory not found in\n",
  777. cMakeRoot.c_str());
  778. return 0;
  779. }
  780. this->CacheManager->AddCacheEntry
  781. ("CMAKE_ROOT", cMakeRoot.c_str(),
  782. "Path to CMake installation.", cmCacheManager::INTERNAL);
  783. #ifdef _WIN32
  784. std::string comspec = "cmw9xcom.exe";
  785. cmSystemTools::SetWindows9xComspecSubstitute(comspec.c_str());
  786. #endif
  787. return 1;
  788. }
  789. void CMakeCommandUsage(const char* program)
  790. {
  791. cmOStringStream errorStream;
  792. #ifdef CMAKE_BUILD_WITH_CMAKE
  793. errorStream
  794. << "cmake version " << cmVersion::GetCMakeVersion() << "\n";
  795. #else
  796. errorStream
  797. << "cmake bootstrap\n";
  798. #endif
  799. errorStream
  800. << "Usage: " << program << " -E [command] [arguments ...]\n"
  801. << "Available commands: \n"
  802. << " chdir dir cmd [args]... - run command in a given directory\n"
  803. << " copy file destination - copy file to destination (either file "
  804. "or directory)\n"
  805. << " copy_if_different in-file out-file - copy file if input has "
  806. "changed\n"
  807. << " copy_directory source destination - copy directory 'source' "
  808. "content to directory 'destination'\n"
  809. << " compare_files file1 file2 - check if file1 is same as file2\n"
  810. << " echo [string]... - displays arguments as text\n"
  811. << " echo_append [string]... - displays arguments as text but no new "
  812. "line\n"
  813. << " environment - display the current enviroment\n"
  814. << " make_directory dir - create a directory\n"
  815. << " md5sum file1 [...] - compute md5sum of files\n"
  816. << " remove_directory dir - remove a directory and its contents\n"
  817. << " remove [-f] file1 file2 ... - remove the file(s), use -f to force "
  818. "it\n"
  819. << " tar [cxt][vfz] file.tar file/dir1 file/dir2 ... - create a tar "
  820. "archive\n"
  821. << " time command [args] ... - run command and return elapsed time\n"
  822. #if defined(_WIN32) && !defined(__CYGWIN__)
  823. << " write_regv key value - write registry value\n"
  824. << " delete_regv key - delete registry value\n"
  825. << " comspec - on windows 9x use this for RunCommand\n"
  826. #else
  827. << " create_symlink old new - create a symbolic link new -> old\n"
  828. #endif
  829. ;
  830. cmSystemTools::Error(errorStream.str().c_str());
  831. }
  832. int cmake::ExecuteCMakeCommand(std::vector<std::string>& args)
  833. {
  834. if (args.size() > 1)
  835. {
  836. // Copy file
  837. if (args[1] == "copy" && args.size() == 4)
  838. {
  839. if(!cmSystemTools::cmCopyFile(args[2].c_str(), args[3].c_str()))
  840. {
  841. std::cerr << "Error copying file \"" << args[2].c_str()
  842. << "\" to \"" << args[3].c_str() << "\".\n";
  843. return 1;
  844. }
  845. return 0;
  846. }
  847. // Copy file if different.
  848. if (args[1] == "copy_if_different" && args.size() == 4)
  849. {
  850. if(!cmSystemTools::CopyFileIfDifferent(args[2].c_str(),
  851. args[3].c_str()))
  852. {
  853. std::cerr << "Error copying file (if different) from \""
  854. << args[2].c_str() << "\" to \"" << args[3].c_str()
  855. << "\".\n";
  856. return 1;
  857. }
  858. return 0;
  859. }
  860. // Copy directory content
  861. if (args[1] == "copy_directory" && args.size() == 4)
  862. {
  863. if(!cmSystemTools::CopyADirectory(args[2].c_str(), args[3].c_str()))
  864. {
  865. std::cerr << "Error copying directory from \""
  866. << args[2].c_str() << "\" to \"" << args[3].c_str()
  867. << "\".\n";
  868. return 1;
  869. }
  870. return 0;
  871. }
  872. // Compare files
  873. if (args[1] == "compare_files" && args.size() == 4)
  874. {
  875. if(cmSystemTools::FilesDiffer(args[2].c_str(), args[3].c_str()))
  876. {
  877. std::cerr << "Files \""
  878. << args[2].c_str() << "\" to \"" << args[3].c_str()
  879. << "\" are different.\n";
  880. return 1;
  881. }
  882. return 0;
  883. }
  884. // Echo string
  885. else if (args[1] == "echo" )
  886. {
  887. unsigned int cc;
  888. const char* space = "";
  889. for ( cc = 2; cc < args.size(); cc ++ )
  890. {
  891. std::cout << space << args[cc];
  892. space = " ";
  893. }
  894. std::cout << std::endl;
  895. return 0;
  896. }
  897. // Echo string no new line
  898. else if (args[1] == "echo_append" )
  899. {
  900. unsigned int cc;
  901. const char* space = "";
  902. for ( cc = 2; cc < args.size(); cc ++ )
  903. {
  904. std::cout << space << args[cc];
  905. space = " ";
  906. }
  907. return 0;
  908. }
  909. #if defined(CMAKE_BUILD_WITH_CMAKE)
  910. // Command to create a symbolic link. Fails on platforms not
  911. // supporting them.
  912. else if (args[1] == "environment" )
  913. {
  914. std::vector<std::string> env = cmSystemTools::GetEnvironmentVariables();
  915. std::vector<std::string>::iterator it;
  916. for ( it = env.begin(); it != env.end(); ++ it )
  917. {
  918. std::cout << it->c_str() << std::endl;
  919. }
  920. return 0;
  921. }
  922. #endif
  923. else if (args[1] == "make_directory" && args.size() == 3)
  924. {
  925. if(!cmSystemTools::MakeDirectory(args[2].c_str()))
  926. {
  927. std::cerr << "Error making directory \"" << args[2].c_str()
  928. << "\".\n";
  929. return 1;
  930. }
  931. return 0;
  932. }
  933. else if (args[1] == "remove_directory" && args.size() == 3)
  934. {
  935. if(!cmSystemTools::RemoveADirectory(args[2].c_str()))
  936. {
  937. std::cerr << "Error removing directory \"" << args[2].c_str()
  938. << "\".\n";
  939. return 1;
  940. }
  941. return 0;
  942. }
  943. // Remove file
  944. else if (args[1] == "remove" && args.size() > 2)
  945. {
  946. bool force = false;
  947. for (std::string::size_type cc = 2; cc < args.size(); cc ++)
  948. {
  949. if(args[cc] == "\\-f" || args[cc] == "-f")
  950. {
  951. force = true;
  952. }
  953. else
  954. {
  955. // Complain if the file could not be removed, still exists,
  956. // and the -f option was not given.
  957. if(!cmSystemTools::RemoveFile(args[cc].c_str()) && !force &&
  958. cmSystemTools::FileExists(args[cc].c_str()))
  959. {
  960. return 1;
  961. }
  962. }
  963. }
  964. return 0;
  965. }
  966. // Clock command
  967. else if (args[1] == "time" && args.size() > 2)
  968. {
  969. std::string command = args[2];
  970. for (std::string::size_type cc = 3; cc < args.size(); cc ++)
  971. {
  972. command += " ";
  973. command += args[cc];
  974. }
  975. clock_t clock_start, clock_finish;
  976. time_t time_start, time_finish;
  977. time(&time_start);
  978. clock_start = clock();
  979. cmSystemTools::RunSingleCommand(command.c_str());
  980. clock_finish = clock();
  981. time(&time_finish);
  982. double clocks_per_sec = static_cast<double>(CLOCKS_PER_SEC);
  983. std::cout << "Elapsed time: "
  984. << static_cast<long>(time_finish - time_start) << " s. (time)"
  985. << ", "
  986. << static_cast<double>(clock_finish - clock_start) / clocks_per_sec
  987. << " s. (clock)"
  988. << "\n";
  989. return 0;
  990. }
  991. // Command to calculate the md5sum of a file
  992. else if (args[1] == "md5sum" && args.size() >= 3)
  993. {
  994. char md5out[32];
  995. int retval = 0;
  996. for (std::string::size_type cc = 2; cc < args.size(); cc ++)
  997. {
  998. const char *filename = args[cc].c_str();
  999. // Cannot compute md5sum of a directory
  1000. if(cmSystemTools::FileIsDirectory(filename))
  1001. {
  1002. std::cerr << "Error: " << filename << " is a directory" << std::endl;
  1003. retval++;
  1004. }
  1005. else if(!cmSystemTools::ComputeFileMD5(filename, md5out))
  1006. {
  1007. // To mimic md5sum behavior in a shell:
  1008. std::cerr << filename << ": No such file or directory" << std::endl;
  1009. retval++;
  1010. }
  1011. else
  1012. {
  1013. std::cout << std::string(md5out,32) << " " << filename << std::endl;
  1014. }
  1015. }
  1016. return retval;
  1017. }
  1018. // Command to change directory and run a program.
  1019. else if (args[1] == "chdir" && args.size() >= 4)
  1020. {
  1021. std::string directory = args[2];
  1022. if(!cmSystemTools::FileExists(directory.c_str()))
  1023. {
  1024. cmSystemTools::Error("Directory does not exist for chdir command: ",
  1025. args[2].c_str());
  1026. return 0;
  1027. }
  1028. std::string command = "\"";
  1029. command += args[3];
  1030. command += "\"";
  1031. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  1032. {
  1033. command += " \"";
  1034. command += args[cc];
  1035. command += "\"";
  1036. }
  1037. int retval = 0;
  1038. int timeout = 0;
  1039. if ( cmSystemTools::RunSingleCommand(command.c_str(), 0, &retval,
  1040. directory.c_str(), true, timeout) )
  1041. {
  1042. return retval;
  1043. }
  1044. return 1;
  1045. }
  1046. // Command to start progress for a build
  1047. else if (args[1] == "cmake_progress_start" && args.size() == 4)
  1048. {
  1049. // bascially remove the directory
  1050. std::string dirName = args[2];
  1051. dirName += "/Progress";
  1052. cmSystemTools::RemoveADirectory(dirName.c_str());
  1053. int count = atoi(args[3].c_str());
  1054. if (count)
  1055. {
  1056. cmSystemTools::MakeDirectory(dirName.c_str());
  1057. // write the count into the directory
  1058. std::string fName = dirName;
  1059. fName += "/count.txt";
  1060. FILE *progFile = fopen(fName.c_str(),"w");
  1061. if (progFile)
  1062. {
  1063. fprintf(progFile,"%i\n",count);
  1064. fclose(progFile);
  1065. }
  1066. }
  1067. return 0;
  1068. }
  1069. // Command to report progress for a build
  1070. else if (args[1] == "cmake_progress_report" && args.size() >= 3)
  1071. {
  1072. std::string dirName = args[2];
  1073. dirName += "/Progress";
  1074. std::string fName;
  1075. FILE *progFile;
  1076. // read the count
  1077. fName = dirName;
  1078. fName += "/count.txt";
  1079. progFile = fopen(fName.c_str(),"r");
  1080. int count = 0;
  1081. if (!progFile)
  1082. {
  1083. return 0;
  1084. }
  1085. else
  1086. {
  1087. fscanf(progFile,"%i",&count);
  1088. fclose(progFile);
  1089. }
  1090. unsigned int i;
  1091. for (i = 3; i < args.size(); ++i)
  1092. {
  1093. fName = dirName;
  1094. fName += "/";
  1095. fName += args[i];
  1096. progFile = fopen(fName.c_str(),"w");
  1097. if (progFile)
  1098. {
  1099. fprintf(progFile,"empty");
  1100. fclose(progFile);
  1101. }
  1102. }
  1103. int fileNum = static_cast<int>
  1104. (cmsys::Directory::GetNumberOfFilesInDirectory(dirName.c_str()));
  1105. if (count > 0)
  1106. {
  1107. // print the progress
  1108. fprintf(stdout,"[%3i%%] ",((fileNum-3)*100)/count);
  1109. }
  1110. return 0;
  1111. }
  1112. // Command to create a symbolic link. Fails on platforms not
  1113. // supporting them.
  1114. else if (args[1] == "create_symlink" && args.size() == 4)
  1115. {
  1116. const char* destinationFileName = args[3].c_str();
  1117. if ( cmSystemTools::FileExists(destinationFileName) )
  1118. {
  1119. if ( cmSystemTools::FileIsSymlink(destinationFileName) )
  1120. {
  1121. if ( !cmSystemTools::RemoveFile(destinationFileName) ||
  1122. cmSystemTools::FileExists(destinationFileName) )
  1123. {
  1124. return 0;
  1125. }
  1126. }
  1127. else
  1128. {
  1129. return 0;
  1130. }
  1131. }
  1132. return cmSystemTools::CreateSymlink(args[2].c_str(),
  1133. args[3].c_str())? 0:1;
  1134. }
  1135. // Internal CMake shared library support.
  1136. else if (args[1] == "cmake_symlink_library" && args.size() == 5)
  1137. {
  1138. int result = 0;
  1139. std::string realName = args[2];
  1140. std::string soName = args[3];
  1141. std::string name = args[4];
  1142. if(soName != realName)
  1143. {
  1144. std::string fname = cmSystemTools::GetFilenameName(realName);
  1145. if(cmSystemTools::FileExists(soName.c_str()))
  1146. {
  1147. cmSystemTools::RemoveFile(soName.c_str());
  1148. }
  1149. if(!cmSystemTools::CreateSymlink(fname.c_str(), soName.c_str()))
  1150. {
  1151. result = 1;
  1152. }
  1153. }
  1154. if(name != soName)
  1155. {
  1156. std::string fname = cmSystemTools::GetFilenameName(soName);
  1157. if(cmSystemTools::FileExists(soName.c_str()))
  1158. {
  1159. cmSystemTools::RemoveFile(name.c_str());
  1160. }
  1161. if(!cmSystemTools::CreateSymlink(fname.c_str(), name.c_str()))
  1162. {
  1163. result = 1;
  1164. }
  1165. }
  1166. return result;
  1167. }
  1168. // Internal CMake versioned executable support.
  1169. else if (args[1] == "cmake_symlink_executable" && args.size() == 4)
  1170. {
  1171. int result = 0;
  1172. std::string realName = args[2];
  1173. std::string name = args[3];
  1174. if(name != realName)
  1175. {
  1176. std::string fname = cmSystemTools::GetFilenameName(realName);
  1177. if(cmSystemTools::FileExists(realName.c_str()))
  1178. {
  1179. cmSystemTools::RemoveFile(name.c_str());
  1180. }
  1181. if(!cmSystemTools::CreateSymlink(fname.c_str(), name.c_str()))
  1182. {
  1183. result = 1;
  1184. }
  1185. }
  1186. return result;
  1187. }
  1188. // Internal CMake dependency scanning support.
  1189. else if (args[1] == "cmake_depends" && args.size() >= 6)
  1190. {
  1191. // Create a cmake object instance to process dependencies.
  1192. cmake cm;
  1193. std::string gen;
  1194. std::string homeDir;
  1195. std::string startDir;
  1196. std::string homeOutDir;
  1197. std::string startOutDir;
  1198. std::string depInfo;
  1199. if(args.size() >= 8)
  1200. {
  1201. // Full signature:
  1202. //
  1203. // -E cmake_depends <generator>
  1204. // <home-src-dir> <start-src-dir>
  1205. // <home-out-dir> <start-out-dir>
  1206. // <dep-info>
  1207. //
  1208. // All paths are provided.
  1209. gen = args[2];
  1210. homeDir = args[3];
  1211. startDir = args[4];
  1212. homeOutDir = args[5];
  1213. startOutDir = args[6];
  1214. depInfo = args[7];
  1215. }
  1216. else
  1217. {
  1218. // Support older signature for existing makefiles:
  1219. //
  1220. // -E cmake_depends <generator>
  1221. // <home-out-dir> <start-out-dir>
  1222. // <dep-info>
  1223. //
  1224. // Just pretend the source directories are the same as the
  1225. // binary directories so at least scanning will work.
  1226. gen = args[2];
  1227. homeDir = args[3];
  1228. startDir = args[4];
  1229. homeOutDir = args[3];
  1230. startOutDir = args[3];
  1231. depInfo = args[5];
  1232. }
  1233. // Create a local generator configured for the directory in
  1234. // which dependencies will be scanned.
  1235. homeDir = cmSystemTools::CollapseFullPath(homeDir.c_str());
  1236. startDir = cmSystemTools::CollapseFullPath(startDir.c_str());
  1237. homeOutDir = cmSystemTools::CollapseFullPath(homeOutDir.c_str());
  1238. startOutDir = cmSystemTools::CollapseFullPath(startOutDir.c_str());
  1239. cm.SetHomeDirectory(homeDir.c_str());
  1240. cm.SetStartDirectory(startDir.c_str());
  1241. cm.SetHomeOutputDirectory(homeOutDir.c_str());
  1242. cm.SetStartOutputDirectory(startOutDir.c_str());
  1243. if(cmGlobalGenerator* ggd = cm.CreateGlobalGenerator(gen.c_str()))
  1244. {
  1245. cm.SetGlobalGenerator(ggd);
  1246. std::auto_ptr<cmLocalGenerator> lgd(ggd->CreateLocalGenerator());
  1247. lgd->SetGlobalGenerator(ggd);
  1248. lgd->GetMakefile()->SetStartDirectory(startDir.c_str());
  1249. lgd->GetMakefile()->SetStartOutputDirectory(startOutDir.c_str());
  1250. lgd->GetMakefile()->MakeStartDirectoriesCurrent();
  1251. // Actually scan dependencies.
  1252. return lgd->ScanDependencies(depInfo.c_str())? 0 : 2;
  1253. }
  1254. return 1;
  1255. }
  1256. // Internal CMake link script support.
  1257. else if (args[1] == "cmake_link_script" && args.size() >= 3)
  1258. {
  1259. return cmake::ExecuteLinkScript(args);
  1260. }
  1261. // Internal CMake unimplemented feature notification.
  1262. else if (args[1] == "cmake_unimplemented_variable")
  1263. {
  1264. std::cerr << "Feature not implemented for this platform.";
  1265. if(args.size() == 3)
  1266. {
  1267. std::cerr << " Variable " << args[2] << " is not set.";
  1268. }
  1269. std::cerr << std::endl;
  1270. return 1;
  1271. }
  1272. #ifdef CMAKE_BUILD_WITH_CMAKE
  1273. // Internal CMake color makefile support.
  1274. else if (args[1] == "cmake_echo_color")
  1275. {
  1276. return cmake::ExecuteEchoColor(args);
  1277. }
  1278. #endif
  1279. // Tar files
  1280. else if (args[1] == "tar" && args.size() > 3)
  1281. {
  1282. std::string flags = args[2];
  1283. std::string outFile = args[3];
  1284. std::vector<cmStdString> files;
  1285. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  1286. {
  1287. files.push_back(args[cc]);
  1288. }
  1289. bool gzip = false;
  1290. bool verbose = false;
  1291. if ( flags.find_first_of('z') != flags.npos )
  1292. {
  1293. gzip = true;
  1294. }
  1295. if ( flags.find_first_of('v') != flags.npos )
  1296. {
  1297. verbose = true;
  1298. }
  1299. if ( flags.find_first_of('t') != flags.npos )
  1300. {
  1301. if ( !cmSystemTools::ListTar(outFile.c_str(), files, gzip, verbose) )
  1302. {
  1303. cmSystemTools::Error("Problem creating tar: ", outFile.c_str());
  1304. return 1;
  1305. }
  1306. }
  1307. else if ( flags.find_first_of('c') != flags.npos )
  1308. {
  1309. if ( !cmSystemTools::CreateTar(
  1310. outFile.c_str(), files, gzip, verbose) )
  1311. {
  1312. cmSystemTools::Error("Problem creating tar: ", outFile.c_str());
  1313. return 1;
  1314. }
  1315. }
  1316. else if ( flags.find_first_of('x') != flags.npos )
  1317. {
  1318. if ( !cmSystemTools::ExtractTar(
  1319. outFile.c_str(), files, gzip, verbose) )
  1320. {
  1321. cmSystemTools::Error("Problem extracting tar: ", outFile.c_str());
  1322. return 1;
  1323. }
  1324. }
  1325. return 0;
  1326. }
  1327. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1328. // Internal CMake Fortran module support.
  1329. else if (args[1] == "cmake_copy_f90_mod" && args.size() >= 4)
  1330. {
  1331. return cmDependsFortran::CopyModule(args)? 0 : 1;
  1332. }
  1333. #endif
  1334. #if defined(_WIN32) && !defined(__CYGWIN__)
  1335. // Write registry value
  1336. else if (args[1] == "write_regv" && args.size() > 3)
  1337. {
  1338. return cmSystemTools::WriteRegistryValue(args[2].c_str(),
  1339. args[3].c_str()) ? 0 : 1;
  1340. }
  1341. // Delete registry value
  1342. else if (args[1] == "delete_regv" && args.size() > 2)
  1343. {
  1344. return cmSystemTools::DeleteRegistryValue(args[2].c_str()) ? 0 : 1;
  1345. }
  1346. // Remove file
  1347. else if (args[1] == "comspec" && args.size() > 2)
  1348. {
  1349. unsigned int cc;
  1350. std::string command = args[2];
  1351. for ( cc = 3; cc < args.size(); cc ++ )
  1352. {
  1353. command += " " + args[cc];
  1354. }
  1355. return cmWin32ProcessExecution::Windows9xHack(command.c_str());
  1356. }
  1357. #endif
  1358. }
  1359. ::CMakeCommandUsage(args[0].c_str());
  1360. return 1;
  1361. }
  1362. void cmake::AddExtraGenerator(const char* name,
  1363. CreateExtraGeneratorFunctionType newFunction)
  1364. {
  1365. cmExternalMakefileProjectGenerator* extraGenerator = newFunction();
  1366. const std::vector<std::string>& supportedGlobalGenerators =
  1367. extraGenerator->GetSupportedGlobalGenerators();
  1368. for(std::vector<std::string>::const_iterator
  1369. it = supportedGlobalGenerators.begin();
  1370. it != supportedGlobalGenerators.end();
  1371. ++it )
  1372. {
  1373. std::string fullName = cmExternalMakefileProjectGenerator::
  1374. CreateFullGeneratorName(it->c_str(), name);
  1375. this->ExtraGenerators[fullName.c_str()] = newFunction;
  1376. }
  1377. delete extraGenerator;
  1378. }
  1379. void cmake::AddDefaultExtraGenerators()
  1380. {
  1381. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1382. #if defined(_WIN32) && !defined(__CYGWIN__)
  1383. // e.g. kdevelop4 ?
  1384. #endif
  1385. #if !defined(__CYGWIN__)
  1386. this->AddExtraGenerator(cmExtraCodeBlocksGenerator::GetActualName(),
  1387. &cmExtraCodeBlocksGenerator::New);
  1388. #endif
  1389. #ifdef CMAKE_USE_KDEVELOP
  1390. this->AddExtraGenerator(cmGlobalKdevelopGenerator::GetActualName(),
  1391. &cmGlobalKdevelopGenerator::New);
  1392. // for kdevelop also add the generator with just the name of the
  1393. // extra generator, since it was this way since cmake 2.2
  1394. this->ExtraGenerators[cmGlobalKdevelopGenerator::GetActualName()]
  1395. = &cmGlobalKdevelopGenerator::New;
  1396. #endif
  1397. #endif
  1398. }
  1399. //----------------------------------------------------------------------------
  1400. void cmake::GetRegisteredGenerators(std::vector<std::string>& names)
  1401. {
  1402. for(RegisteredGeneratorsMap::const_iterator i = this->Generators.begin();
  1403. i != this->Generators.end(); ++i)
  1404. {
  1405. names.push_back(i->first);
  1406. }
  1407. }
  1408. cmGlobalGenerator* cmake::CreateGlobalGenerator(const char* name)
  1409. {
  1410. cmGlobalGenerator* generator = 0;
  1411. cmExternalMakefileProjectGenerator* extraGenerator = 0;
  1412. RegisteredGeneratorsMap::const_iterator genIt = this->Generators.find(name);
  1413. if(genIt == this->Generators.end())
  1414. {
  1415. RegisteredExtraGeneratorsMap::const_iterator extraGenIt =
  1416. this->ExtraGenerators.find(name);
  1417. if (extraGenIt == this->ExtraGenerators.end())
  1418. {
  1419. return 0;
  1420. }
  1421. extraGenerator = (extraGenIt->second)();
  1422. genIt=this->Generators.find(extraGenerator->GetGlobalGeneratorName(name));
  1423. if(genIt == this->Generators.end())
  1424. {
  1425. delete extraGenerator;
  1426. return 0;
  1427. }
  1428. }
  1429. generator = (genIt->second)();
  1430. generator->SetCMakeInstance(this);
  1431. generator->SetExternalMakefileProjectGenerator(extraGenerator);
  1432. return generator;
  1433. }
  1434. void cmake::SetHomeDirectory(const char* dir)
  1435. {
  1436. this->cmHomeDirectory = dir;
  1437. cmSystemTools::ConvertToUnixSlashes(this->cmHomeDirectory);
  1438. }
  1439. void cmake::SetHomeOutputDirectory(const char* lib)
  1440. {
  1441. this->HomeOutputDirectory = lib;
  1442. cmSystemTools::ConvertToUnixSlashes(this->HomeOutputDirectory);
  1443. }
  1444. void cmake::SetGlobalGenerator(cmGlobalGenerator *gg)
  1445. {
  1446. if(!gg)
  1447. {
  1448. cmSystemTools::Error("Error SetGlobalGenerator called with null");
  1449. return;
  1450. }
  1451. // delete the old generator
  1452. if (this->GlobalGenerator)
  1453. {
  1454. delete this->GlobalGenerator;
  1455. // restore the original environment variables CXX and CC
  1456. // Restor CC
  1457. std::string env = "CC=";
  1458. if(this->CCEnvironment.size())
  1459. {
  1460. env += this->CCEnvironment;
  1461. }
  1462. cmSystemTools::PutEnv(env.c_str());
  1463. env = "CXX=";
  1464. if(this->CXXEnvironment.size())
  1465. {
  1466. env += this->CXXEnvironment;
  1467. }
  1468. cmSystemTools::PutEnv(env.c_str());
  1469. }
  1470. // set the new
  1471. this->GlobalGenerator = gg;
  1472. // set the global flag for unix style paths on cmSystemTools as soon as
  1473. // the generator is set. This allows gmake to be used on windows.
  1474. cmSystemTools::SetForceUnixPaths
  1475. (this->GlobalGenerator->GetForceUnixPaths());
  1476. // Save the environment variables CXX and CC
  1477. const char* cxx = getenv("CXX");
  1478. const char* cc = getenv("CC");
  1479. if(cxx)
  1480. {
  1481. this->CXXEnvironment = cxx;
  1482. }
  1483. else
  1484. {
  1485. this->CXXEnvironment = "";
  1486. }
  1487. if(cc)
  1488. {
  1489. this->CCEnvironment = cc;
  1490. }
  1491. else
  1492. {
  1493. this->CCEnvironment = "";
  1494. }
  1495. // set the cmake instance just to be sure
  1496. gg->SetCMakeInstance(this);
  1497. }
  1498. int cmake::DoPreConfigureChecks()
  1499. {
  1500. // Make sure the Start directory contains a CMakeLists.txt file.
  1501. std::string srcList = this->GetHomeDirectory();
  1502. srcList += "/CMakeLists.txt";
  1503. if(!cmSystemTools::FileExists(srcList.c_str()))
  1504. {
  1505. cmOStringStream err;
  1506. if(cmSystemTools::FileIsDirectory(this->GetHomeDirectory()))
  1507. {
  1508. err << "The source directory \"" << this->GetHomeDirectory()
  1509. << "\" does not appear to contain CMakeLists.txt.\n";
  1510. }
  1511. else if(cmSystemTools::FileExists(this->GetHomeDirectory()))
  1512. {
  1513. err << "The source directory \"" << this->GetHomeDirectory()
  1514. << "\" is a file, not a directory.\n";
  1515. }
  1516. else
  1517. {
  1518. err << "The source directory \"" << this->GetHomeDirectory()
  1519. << "\" does not exist.\n";
  1520. }
  1521. err << "Specify --help for usage, or press the help button on the CMake "
  1522. "GUI.";
  1523. cmSystemTools::Error(err.str().c_str());
  1524. return -2;
  1525. }
  1526. // do a sanity check on some values
  1527. if(this->CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY"))
  1528. {
  1529. std::string cacheStart =
  1530. this->CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY");
  1531. cacheStart += "/CMakeLists.txt";
  1532. std::string currentStart = this->GetHomeDirectory();
  1533. currentStart += "/CMakeLists.txt";
  1534. if(!cmSystemTools::SameFile(cacheStart.c_str(), currentStart.c_str()))
  1535. {
  1536. std::string message = "The source \"";
  1537. message += currentStart;
  1538. message += "\" does not match the source \"";
  1539. message += cacheStart;
  1540. message += "\" used to generate cache. ";
  1541. message += "Re-run cmake with a different source directory.";
  1542. cmSystemTools::Error(message.c_str());
  1543. return -2;
  1544. }
  1545. }
  1546. else
  1547. {
  1548. return 0;
  1549. }
  1550. return 1;
  1551. }
  1552. int cmake::Configure()
  1553. {
  1554. // Construct right now our path conversion table before it's too late:
  1555. this->UpdateConversionPathTable();
  1556. this->CleanupCommandsAndMacros();
  1557. int res = 0;
  1558. if ( !this->ScriptMode )
  1559. {
  1560. res = this->DoPreConfigureChecks();
  1561. }
  1562. if ( res < 0 )
  1563. {
  1564. return -2;
  1565. }
  1566. if ( !res )
  1567. {
  1568. this->CacheManager->AddCacheEntry
  1569. ("CMAKE_HOME_DIRECTORY",
  1570. this->GetHomeDirectory(),
  1571. "Start directory with the top level CMakeLists.txt file for this "
  1572. "project",
  1573. cmCacheManager::INTERNAL);
  1574. }
  1575. // set the default BACKWARDS compatibility to the current version
  1576. if(!this->CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1577. {
  1578. char ver[256];
  1579. sprintf(ver,"%i.%i",cmVersion::GetMajorVersion(),
  1580. cmVersion::GetMinorVersion());
  1581. this->CacheManager->AddCacheEntry
  1582. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  1583. "For backwards compatibility, what version of CMake commands and "
  1584. "syntax should this version of CMake allow.",
  1585. cmCacheManager::STRING);
  1586. }
  1587. // no generator specified on the command line
  1588. if(!this->GlobalGenerator)
  1589. {
  1590. const char* genName =
  1591. this->CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1592. const char* extraGenName =
  1593. this->CacheManager->GetCacheValue("CMAKE_EXTRA_GENERATOR");
  1594. if(genName)
  1595. {
  1596. std::string fullName = cmExternalMakefileProjectGenerator::
  1597. CreateFullGeneratorName(genName, extraGenName);
  1598. this->GlobalGenerator = this->CreateGlobalGenerator(fullName.c_str());
  1599. }
  1600. if(this->GlobalGenerator)
  1601. {
  1602. // set the global flag for unix style paths on cmSystemTools as
  1603. // soon as the generator is set. This allows gmake to be used
  1604. // on windows.
  1605. cmSystemTools::SetForceUnixPaths
  1606. (this->GlobalGenerator->GetForceUnixPaths());
  1607. }
  1608. else
  1609. {
  1610. #if defined(__BORLANDC__) && defined(_WIN32)
  1611. this->SetGlobalGenerator(new cmGlobalBorlandMakefileGenerator);
  1612. #elif defined(_WIN32) && !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW)
  1613. std::string installedCompiler;
  1614. std::string mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1615. "\\VisualStudio\\8.0\\Setup;Dbghelp_path]";
  1616. cmSystemTools::ExpandRegistryValues(mp);
  1617. if (!(mp == "/registry"))
  1618. {
  1619. installedCompiler = "Visual Studio 8 2005";
  1620. }
  1621. else
  1622. {
  1623. mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1624. "\\VisualStudio\\7.1;InstallDir]";
  1625. cmSystemTools::ExpandRegistryValues(mp);
  1626. if (!(mp == "/registry"))
  1627. {
  1628. installedCompiler = "Visual Studio 7 .NET 2003";
  1629. }
  1630. else
  1631. {
  1632. mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1633. "\\VisualStudio\\7.0;InstallDir]";
  1634. cmSystemTools::ExpandRegistryValues(mp);
  1635. if (!(mp == "/registry"))
  1636. {
  1637. installedCompiler = "Visual Studio 7";
  1638. }
  1639. else
  1640. {
  1641. installedCompiler = "Visual Studio 6";
  1642. }
  1643. }
  1644. }
  1645. cmGlobalGenerator* gen
  1646. = this->CreateGlobalGenerator(installedCompiler.c_str());
  1647. if(!gen)
  1648. {
  1649. gen = new cmGlobalNMakeMakefileGenerator;
  1650. }
  1651. this->SetGlobalGenerator(gen);
  1652. #else
  1653. this->SetGlobalGenerator(new cmGlobalUnixMakefileGenerator3);
  1654. #endif
  1655. }
  1656. if(!this->GlobalGenerator)
  1657. {
  1658. cmSystemTools::Error("Could not create generator");
  1659. return -1;
  1660. }
  1661. }
  1662. const char* genName = this->CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1663. if(genName)
  1664. {
  1665. if(strcmp(this->GlobalGenerator->GetName(), genName) != 0)
  1666. {
  1667. std::string message = "Error: generator : ";
  1668. message += this->GlobalGenerator->GetName();
  1669. message += "\nDoes not match the generator used previously: ";
  1670. message += genName;
  1671. message +=
  1672. "\nEither remove the CMakeCache.txt file or choose a different"
  1673. " binary directory.";
  1674. cmSystemTools::Error(message.c_str());
  1675. return -2;
  1676. }
  1677. }
  1678. if(!this->CacheManager->GetCacheValue("CMAKE_GENERATOR"))
  1679. {
  1680. this->CacheManager->AddCacheEntry("CMAKE_GENERATOR",
  1681. this->GlobalGenerator->GetName(),
  1682. "Name of generator.",
  1683. cmCacheManager::INTERNAL);
  1684. this->CacheManager->AddCacheEntry("CMAKE_EXTRA_GENERATOR",
  1685. this->GlobalGenerator->GetExtraGeneratorName(),
  1686. "Name of external makefile project generator.",
  1687. cmCacheManager::INTERNAL);
  1688. }
  1689. // reset any system configuration information, except for when we are
  1690. // InTryCompile. With TryCompile the system info is taken from the parent's
  1691. // info to save time
  1692. if (!this->InTryCompile)
  1693. {
  1694. this->GlobalGenerator->ClearEnabledLanguages();
  1695. }
  1696. this->CleanupWrittenFiles();
  1697. // Truncate log files
  1698. if (!this->InTryCompile)
  1699. {
  1700. this->TruncateOutputLog("CMakeOutput.log");
  1701. this->TruncateOutputLog("CMakeError.log");
  1702. }
  1703. // actually do the configure
  1704. this->GlobalGenerator->Configure();
  1705. // Before saving the cache
  1706. // if the project did not define one of the entries below, add them now
  1707. // so users can edit the values in the cache:
  1708. // LIBRARY_OUTPUT_PATH
  1709. // EXECUTABLE_OUTPUT_PATH
  1710. if(!this->CacheManager->GetCacheValue("LIBRARY_OUTPUT_PATH"))
  1711. {
  1712. this->CacheManager->AddCacheEntry
  1713. ("LIBRARY_OUTPUT_PATH", "",
  1714. "Single output directory for building all libraries.",
  1715. cmCacheManager::PATH);
  1716. }
  1717. if(!this->CacheManager->GetCacheValue("EXECUTABLE_OUTPUT_PATH"))
  1718. {
  1719. this->CacheManager->AddCacheEntry
  1720. ("EXECUTABLE_OUTPUT_PATH", "",
  1721. "Single output directory for building all executables.",
  1722. cmCacheManager::PATH);
  1723. }
  1724. if(!this->CacheManager->GetCacheValue("CMAKE_USE_RELATIVE_PATHS"))
  1725. {
  1726. this->CacheManager->AddCacheEntry
  1727. ("CMAKE_USE_RELATIVE_PATHS", false,
  1728. "If true, cmake will use relative paths in makefiles and projects.");
  1729. cmCacheManager::CacheIterator it =
  1730. this->CacheManager->GetCacheIterator("CMAKE_USE_RELATIVE_PATHS");
  1731. if ( !it.PropertyExists("ADVANCED") )
  1732. {
  1733. it.SetProperty("ADVANCED", "1");
  1734. }
  1735. }
  1736. if(cmSystemTools::GetFatalErrorOccured() &&
  1737. (!this->CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM") ||
  1738. cmSystemTools::IsOff(this->CacheManager->
  1739. GetCacheValue("CMAKE_MAKE_PROGRAM"))))
  1740. {
  1741. // We must have a bad generator selection. Wipe the cache entry so the
  1742. // user can select another.
  1743. this->CacheManager->RemoveCacheEntry("CMAKE_GENERATOR");
  1744. this->CacheManager->RemoveCacheEntry("CMAKE_EXTRA_GENERATOR");
  1745. }
  1746. // only save the cache if there were no fatal errors
  1747. if ( !this->ScriptMode )
  1748. {
  1749. this->CacheManager->SaveCache(this->GetHomeOutputDirectory());
  1750. }
  1751. if ( !this->GraphVizFile.empty() )
  1752. {
  1753. std::cout << "Generate graphviz: " << this->GraphVizFile << std::endl;
  1754. this->GenerateGraphViz(this->GraphVizFile.c_str());
  1755. }
  1756. if(cmSystemTools::GetErrorOccuredFlag())
  1757. {
  1758. return -1;
  1759. }
  1760. return 0;
  1761. }
  1762. bool cmake::CacheVersionMatches()
  1763. {
  1764. const char* majv =
  1765. this->CacheManager->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION");
  1766. const char* minv =
  1767. this->CacheManager->GetCacheValue("CMAKE_CACHE_MINOR_VERSION");
  1768. const char* relv =
  1769. this->CacheManager->GetCacheValue("CMAKE_CACHE_RELEASE_VERSION");
  1770. bool cacheSameCMake = false;
  1771. if(majv &&
  1772. atoi(majv) == static_cast<int>(cmVersion::GetMajorVersion())
  1773. && minv &&
  1774. atoi(minv) == static_cast<int>(cmVersion::GetMinorVersion())
  1775. && relv && (strcmp(relv, cmVersion::GetReleaseVersion().c_str()) == 0))
  1776. {
  1777. cacheSameCMake = true;
  1778. }
  1779. return cacheSameCMake;
  1780. }
  1781. void cmake::PreLoadCMakeFiles()
  1782. {
  1783. std::string pre_load = this->GetHomeDirectory();
  1784. if ( pre_load.size() > 0 )
  1785. {
  1786. pre_load += "/PreLoad.cmake";
  1787. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1788. {
  1789. this->ReadListFile(pre_load.c_str());
  1790. }
  1791. }
  1792. pre_load = this->GetHomeOutputDirectory();
  1793. if ( pre_load.size() > 0 )
  1794. {
  1795. pre_load += "/PreLoad.cmake";
  1796. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1797. {
  1798. this->ReadListFile(pre_load.c_str());
  1799. }
  1800. }
  1801. }
  1802. // handle a command line invocation
  1803. int cmake::Run(const std::vector<std::string>& args, bool noconfigure)
  1804. {
  1805. // Process the arguments
  1806. this->SetArgs(args);
  1807. if(cmSystemTools::GetErrorOccuredFlag())
  1808. {
  1809. return -1;
  1810. }
  1811. // set the cmake command
  1812. this->CMakeCommand = args[0];
  1813. if ( !this->ScriptMode )
  1814. {
  1815. // load the cache
  1816. if(this->LoadCache() < 0)
  1817. {
  1818. cmSystemTools::Error("Error executing cmake::LoadCache(). Aborting.\n");
  1819. return -1;
  1820. }
  1821. }
  1822. else
  1823. {
  1824. this->AddCMakePaths(this->CMakeCommand.c_str());
  1825. }
  1826. // Add any cache args
  1827. if ( !this->SetCacheArgs(args) )
  1828. {
  1829. cmSystemTools::Error("Problem processing arguments. Aborting.\n");
  1830. return -1;
  1831. }
  1832. // In script mode we terminate after running the script.
  1833. if(this->ScriptMode)
  1834. {
  1835. if(cmSystemTools::GetErrorOccuredFlag())
  1836. {
  1837. return -1;
  1838. }
  1839. else
  1840. {
  1841. return 0;
  1842. }
  1843. }
  1844. this->PreLoadCMakeFiles();
  1845. std::string systemFile = this->GetHomeOutputDirectory();
  1846. systemFile += "/CMakeSystem.cmake";
  1847. if ( noconfigure )
  1848. {
  1849. return 0;
  1850. }
  1851. // now run the global generate
  1852. // Check the state of the build system to see if we need to regenerate.
  1853. if(!this->CheckBuildSystem())
  1854. {
  1855. return 0;
  1856. }
  1857. // If we are doing global generate, we better set start and start
  1858. // output directory to the root of the project.
  1859. std::string oldstartdir = this->GetStartDirectory();
  1860. std::string oldstartoutputdir = this->GetStartOutputDirectory();
  1861. this->SetStartDirectory(this->GetHomeDirectory());
  1862. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  1863. int ret = this->Configure();
  1864. if (ret || this->ScriptMode)
  1865. {
  1866. return ret;
  1867. }
  1868. ret = this->Generate();
  1869. std::string message = "Build files have been written to: ";
  1870. message += this->GetHomeOutputDirectory();
  1871. this->UpdateProgress(message.c_str(), -1);
  1872. if(ret)
  1873. {
  1874. return ret;
  1875. }
  1876. this->SetStartDirectory(oldstartdir.c_str());
  1877. this->SetStartOutputDirectory(oldstartoutputdir.c_str());
  1878. return ret;
  1879. }
  1880. int cmake::Generate()
  1881. {
  1882. if(!this->GlobalGenerator)
  1883. {
  1884. return -1;
  1885. }
  1886. this->GlobalGenerator->Generate();
  1887. if(cmSystemTools::GetErrorOccuredFlag())
  1888. {
  1889. return -1;
  1890. }
  1891. return 0;
  1892. }
  1893. void cmake::AddCacheEntry(const char* key, const char* value,
  1894. const char* helpString,
  1895. int type)
  1896. {
  1897. this->CacheManager->AddCacheEntry(key, value,
  1898. helpString,
  1899. cmCacheManager::CacheEntryType(type));
  1900. }
  1901. const char* cmake::GetCacheDefinition(const char* name) const
  1902. {
  1903. return this->CacheManager->GetCacheValue(name);
  1904. }
  1905. int cmake::DumpDocumentationToFile(std::ostream& f)
  1906. {
  1907. #ifdef CMAKE_BUILD_WITH_CMAKE
  1908. // Loop over all registered commands and print out documentation
  1909. const char *name;
  1910. const char *terse;
  1911. const char *full;
  1912. char tmp[1024];
  1913. sprintf(tmp,"Version %d.%d (%s)", cmVersion::GetMajorVersion(),
  1914. cmVersion::GetMinorVersion(),
  1915. cmVersion::GetReleaseVersion().c_str());
  1916. f << "<html>\n";
  1917. f << "<h1>Documentation for commands of CMake " << tmp << "</h1>\n";
  1918. f << "<ul>\n";
  1919. for(RegisteredCommandsMap::iterator j = this->Commands.begin();
  1920. j != this->Commands.end(); ++j)
  1921. {
  1922. name = (*j).second->GetName();
  1923. terse = (*j).second->GetTerseDocumentation();
  1924. full = (*j).second->GetFullDocumentation();
  1925. f << "<li><b>" << name << "</b> - " << terse << std::endl
  1926. << "<br><i>Usage:</i> " << full << "</li>" << std::endl << std::endl;
  1927. }
  1928. f << "</ul></html>\n";
  1929. #else
  1930. (void)f;
  1931. #endif
  1932. return 1;
  1933. }
  1934. void cmake::AddDefaultCommands()
  1935. {
  1936. std::list<cmCommand*> commands;
  1937. GetBootstrapCommands(commands);
  1938. GetPredefinedCommands(commands);
  1939. for(std::list<cmCommand*>::iterator i = commands.begin();
  1940. i != commands.end(); ++i)
  1941. {
  1942. this->AddCommand(*i);
  1943. }
  1944. }
  1945. void cmake::AddDefaultGenerators()
  1946. {
  1947. #if defined(_WIN32) && !defined(__CYGWIN__)
  1948. # if !defined(CMAKE_BOOT_MINGW)
  1949. this->Generators[cmGlobalVisualStudio6Generator::GetActualName()] =
  1950. &cmGlobalVisualStudio6Generator::New;
  1951. this->Generators[cmGlobalVisualStudio7Generator::GetActualName()] =
  1952. &cmGlobalVisualStudio7Generator::New;
  1953. this->Generators[cmGlobalVisualStudio71Generator::GetActualName()] =
  1954. &cmGlobalVisualStudio71Generator::New;
  1955. this->Generators[cmGlobalVisualStudio8Generator::GetActualName()] =
  1956. &cmGlobalVisualStudio8Generator::New;
  1957. this->Generators[cmGlobalVisualStudio8Win64Generator::GetActualName()] =
  1958. &cmGlobalVisualStudio8Win64Generator::New;
  1959. this->Generators[cmGlobalBorlandMakefileGenerator::GetActualName()] =
  1960. &cmGlobalBorlandMakefileGenerator::New;
  1961. this->Generators[cmGlobalNMakeMakefileGenerator::GetActualName()] =
  1962. &cmGlobalNMakeMakefileGenerator::New;
  1963. this->Generators[cmGlobalWatcomWMakeGenerator::GetActualName()] =
  1964. &cmGlobalWatcomWMakeGenerator::New;
  1965. # endif
  1966. this->Generators[cmGlobalMSYSMakefileGenerator::GetActualName()] =
  1967. &cmGlobalMSYSMakefileGenerator::New;
  1968. this->Generators[cmGlobalMinGWMakefileGenerator::GetActualName()] =
  1969. &cmGlobalMinGWMakefileGenerator::New;
  1970. #endif
  1971. this->Generators[cmGlobalUnixMakefileGenerator3::GetActualName()] =
  1972. &cmGlobalUnixMakefileGenerator3::New;
  1973. #ifdef CMAKE_USE_XCODE
  1974. this->Generators[cmGlobalXCodeGenerator::GetActualName()] =
  1975. &cmGlobalXCodeGenerator::New;
  1976. #endif
  1977. }
  1978. int cmake::LoadCache()
  1979. {
  1980. // could we not read the cache
  1981. if (!this->CacheManager->LoadCache(this->GetHomeOutputDirectory()))
  1982. {
  1983. // if it does exist, but isn;t readable then warn the user
  1984. std::string cacheFile = this->GetHomeOutputDirectory();
  1985. cacheFile += "/CMakeCache.txt";
  1986. if(cmSystemTools::FileExists(cacheFile.c_str()))
  1987. {
  1988. cmSystemTools::Error(
  1989. "There is a CMakeCache.txt file for the current binary tree but "
  1990. "cmake does not have permission to read it. Please check the "
  1991. "permissions of the directory you are trying to run CMake on.");
  1992. return -1;
  1993. }
  1994. }
  1995. if (this->CMakeCommand.size() < 2)
  1996. {
  1997. cmSystemTools::Error(
  1998. "cmake command was not specified prior to loading the cache in "
  1999. "cmake.cxx");
  2000. return -1;
  2001. }
  2002. // setup CMAKE_ROOT and CMAKE_COMMAND
  2003. if(!this->AddCMakePaths(this->CMakeCommand.c_str()))
  2004. {
  2005. return -3;
  2006. }
  2007. // set the default BACKWARDS compatibility to the current version
  2008. if(!this->CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  2009. {
  2010. char ver[256];
  2011. sprintf(ver,"%i.%i",cmVersion::GetMajorVersion(),
  2012. cmVersion::GetMinorVersion());
  2013. this->CacheManager->AddCacheEntry
  2014. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  2015. "For backwards compatibility, what version of CMake commands and "
  2016. "syntax should this version of CMake allow.",
  2017. cmCacheManager::STRING);
  2018. }
  2019. return 0;
  2020. }
  2021. void cmake::SetProgressCallback(ProgressCallbackType f, void *cd)
  2022. {
  2023. this->ProgressCallback = f;
  2024. this->ProgressCallbackClientData = cd;
  2025. }
  2026. void cmake::UpdateProgress(const char *msg, float prog)
  2027. {
  2028. if(this->ProgressCallback && !this->InTryCompile)
  2029. {
  2030. (*this->ProgressCallback)(msg, prog, this->ProgressCallbackClientData);
  2031. return;
  2032. }
  2033. }
  2034. void cmake::GetCommandDocumentation(std::vector<cmDocumentationEntry>& v,
  2035. bool withCurrentCommands,
  2036. bool withCompatCommands) const
  2037. {
  2038. for(RegisteredCommandsMap::const_iterator j = this->Commands.begin();
  2039. j != this->Commands.end(); ++j)
  2040. {
  2041. if ((( withCompatCommands == false) && ( (*j).second->IsDiscouraged()))
  2042. || ((withCurrentCommands == false) && (!(*j).second->IsDiscouraged())))
  2043. {
  2044. continue;
  2045. }
  2046. cmDocumentationEntry e =
  2047. {
  2048. (*j).second->GetName(),
  2049. (*j).second->GetTerseDocumentation(),
  2050. (*j).second->GetFullDocumentation()
  2051. };
  2052. v.push_back(e);
  2053. }
  2054. cmDocumentationEntry empty = {0,0,0};
  2055. v.push_back(empty);
  2056. }
  2057. void cmake::GetPropertiesDocumentation(std::vector<cmDocumentationEntry>& v,
  2058. cmProperty::ScopeType type)
  2059. {
  2060. // get the properties for cmake
  2061. this->PropertyDefinitions[type].GetPropertiesDocumentation(v);
  2062. cmDocumentationEntry empty = {0,0,0};
  2063. v.push_back(empty);
  2064. }
  2065. void cmake::GetGeneratorDocumentation(std::vector<cmDocumentationEntry>& v)
  2066. {
  2067. for(RegisteredGeneratorsMap::const_iterator i = this->Generators.begin();
  2068. i != this->Generators.end(); ++i)
  2069. {
  2070. cmDocumentationEntry e;
  2071. cmGlobalGenerator* generator = (i->second)();
  2072. generator->GetDocumentation(e);
  2073. delete generator;
  2074. v.push_back(e);
  2075. }
  2076. for(RegisteredExtraGeneratorsMap::const_iterator
  2077. i = this->ExtraGenerators.begin(); i != this->ExtraGenerators.end(); ++i)
  2078. {
  2079. cmDocumentationEntry e;
  2080. cmExternalMakefileProjectGenerator* generator = (i->second)();
  2081. generator->GetDocumentation(e, i->first.c_str());
  2082. e.name = i->first.c_str();
  2083. delete generator;
  2084. v.push_back(e);
  2085. }
  2086. cmDocumentationEntry empty = {0,0,0};
  2087. v.push_back(empty);
  2088. }
  2089. void cmake::AddWrittenFile(const char* file)
  2090. {
  2091. this->WrittenFiles.insert(file);
  2092. }
  2093. bool cmake::HasWrittenFile(const char* file)
  2094. {
  2095. return this->WrittenFiles.find(file) != this->WrittenFiles.end();
  2096. }
  2097. void cmake::CleanupWrittenFiles()
  2098. {
  2099. this->WrittenFiles.clear();
  2100. }
  2101. void cmake::UpdateConversionPathTable()
  2102. {
  2103. // Update the path conversion table with any specified file:
  2104. const char* tablepath =
  2105. this->CacheManager->GetCacheValue("CMAKE_PATH_TRANSLATION_FILE");
  2106. if(tablepath)
  2107. {
  2108. std::ifstream table( tablepath );
  2109. if(!table)
  2110. {
  2111. cmSystemTools::Error("CMAKE_PATH_TRANSLATION_FILE set to ", tablepath,
  2112. ". CMake can not open file.");
  2113. cmSystemTools::ReportLastSystemError("CMake can not open file.");
  2114. }
  2115. else
  2116. {
  2117. std::string a, b;
  2118. while(!table.eof())
  2119. {
  2120. // two entries per line
  2121. table >> a; table >> b;
  2122. cmSystemTools::AddTranslationPath( a.c_str(), b.c_str());
  2123. }
  2124. }
  2125. }
  2126. }
  2127. //----------------------------------------------------------------------------
  2128. int cmake::CheckBuildSystem()
  2129. {
  2130. // We do not need to rerun CMake. Check dependency integrity. Use
  2131. // the make system's VERBOSE environment variable to enable verbose
  2132. // output.
  2133. bool verbose = cmSystemTools::GetEnv("VERBOSE") != 0;
  2134. // This method will check the integrity of the build system if the
  2135. // option was given on the command line. It reads the given file to
  2136. // determine whether CMake should rerun.
  2137. // If no file is provided for the check, we have to rerun.
  2138. if(this->CheckBuildSystemArgument.size() == 0)
  2139. {
  2140. if(verbose)
  2141. {
  2142. cmOStringStream msg;
  2143. msg << "Re-run cmake no build system arguments\n";
  2144. cmSystemTools::Stdout(msg.str().c_str());
  2145. }
  2146. return 1;
  2147. }
  2148. // If the file provided does not exist, we have to rerun.
  2149. if(!cmSystemTools::FileExists(this->CheckBuildSystemArgument.c_str()))
  2150. {
  2151. if(verbose)
  2152. {
  2153. cmOStringStream msg;
  2154. msg << "Re-run cmake missing file: "
  2155. << this->CheckBuildSystemArgument.c_str() << "\n";
  2156. cmSystemTools::Stdout(msg.str().c_str());
  2157. }
  2158. return 1;
  2159. }
  2160. // Read the rerun check file and use it to decide whether to do the
  2161. // global generate.
  2162. cmake cm;
  2163. cmGlobalGenerator gg;
  2164. gg.SetCMakeInstance(&cm);
  2165. std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  2166. lg->SetGlobalGenerator(&gg);
  2167. cmMakefile* mf = lg->GetMakefile();
  2168. if(!mf->ReadListFile(0, this->CheckBuildSystemArgument.c_str()) ||
  2169. cmSystemTools::GetErrorOccuredFlag())
  2170. {
  2171. if(verbose)
  2172. {
  2173. cmOStringStream msg;
  2174. msg << "Re-run cmake error reading : "
  2175. << this->CheckBuildSystemArgument.c_str() << "\n";
  2176. cmSystemTools::Stdout(msg.str().c_str());
  2177. }
  2178. // There was an error reading the file. Just rerun.
  2179. return 1;
  2180. }
  2181. // Now that we know the generator used to build the project, use it
  2182. // to check the dependency integrity.
  2183. const char* genName = mf->GetDefinition("CMAKE_DEPENDS_GENERATOR");
  2184. if (!genName || genName[0] == '\0')
  2185. {
  2186. genName = "Unix Makefiles";
  2187. }
  2188. cmGlobalGenerator *ggd = this->CreateGlobalGenerator(genName);
  2189. if (ggd)
  2190. {
  2191. // Check the dependencies in case source files were removed.
  2192. std::auto_ptr<cmLocalGenerator> lgd(ggd->CreateLocalGenerator());
  2193. lgd->SetGlobalGenerator(ggd);
  2194. lgd->CheckDependencies(mf, verbose, this->ClearBuildSystem);
  2195. // Check for multiple output pairs.
  2196. ggd->CheckMultipleOutputs(mf, verbose);
  2197. }
  2198. // Get the set of dependencies and outputs.
  2199. const char* dependsStr = mf->GetDefinition("CMAKE_MAKEFILE_DEPENDS");
  2200. const char* outputsStr = mf->GetDefinition("CMAKE_MAKEFILE_OUTPUTS");
  2201. if(!dependsStr || !outputsStr)
  2202. {
  2203. // Not enough information was provided to do the test. Just rerun.
  2204. if(verbose)
  2205. {
  2206. cmOStringStream msg;
  2207. msg << "Re-run cmake no CMAKE_MAKEFILE_DEPENDS "
  2208. "or CMAKE_MAKEFILE_OUTPUTS :\n";
  2209. cmSystemTools::Stdout(msg.str().c_str());
  2210. }
  2211. return 1;
  2212. }
  2213. std::vector<std::string> depends;
  2214. std::vector<std::string> outputs;
  2215. cmSystemTools::ExpandListArgument(dependsStr, depends);
  2216. cmSystemTools::ExpandListArgument(outputsStr, outputs);
  2217. // If any output is older than any dependency then rerun.
  2218. for(std::vector<std::string>::iterator dep = depends.begin();
  2219. dep != depends.end(); ++dep)
  2220. {
  2221. for(std::vector<std::string>::iterator out = outputs.begin();
  2222. out != outputs.end(); ++out)
  2223. {
  2224. int result = 0;
  2225. if(!this->FileComparison->FileTimeCompare(out->c_str(),
  2226. dep->c_str(), &result) ||
  2227. result < 0)
  2228. {
  2229. if(verbose)
  2230. {
  2231. cmOStringStream msg;
  2232. msg << "Re-run cmake file: " << out->c_str()
  2233. << " older than: " << dep->c_str() << "\n";
  2234. cmSystemTools::Stdout(msg.str().c_str());
  2235. }
  2236. return 1;
  2237. }
  2238. }
  2239. }
  2240. // No need to rerun.
  2241. return 0;
  2242. }
  2243. //----------------------------------------------------------------------------
  2244. void cmake::TruncateOutputLog(const char* fname)
  2245. {
  2246. std::string fullPath = this->GetHomeOutputDirectory();
  2247. fullPath += "/";
  2248. fullPath += fname;
  2249. struct stat st;
  2250. if ( ::stat(fullPath.c_str(), &st) )
  2251. {
  2252. return;
  2253. }
  2254. if ( !this->CacheManager->GetCacheValue("CMAKE_CACHEFILE_DIR") )
  2255. {
  2256. cmSystemTools::RemoveFile(fullPath.c_str());
  2257. return;
  2258. }
  2259. size_t fsize = st.st_size;
  2260. const size_t maxFileSize = 50 * 1024;
  2261. if ( fsize < maxFileSize )
  2262. {
  2263. //TODO: truncate file
  2264. return;
  2265. }
  2266. }
  2267. inline std::string removeQuotes(const std::string& s)
  2268. {
  2269. if(s[0] == '\"' && s[s.size()-1] == '\"')
  2270. {
  2271. return s.substr(1, s.size()-2);
  2272. }
  2273. return s;
  2274. }
  2275. std::string cmake::FindCMakeProgram(const char* name) const
  2276. {
  2277. std::string path;
  2278. if ((name) && (*name))
  2279. {
  2280. const cmMakefile* mf
  2281. = this->GetGlobalGenerator()->GetLocalGenerators()[0]->GetMakefile();
  2282. #ifdef CMAKE_BUILD_WITH_CMAKE
  2283. path = mf->GetRequiredDefinition("CMAKE_COMMAND");
  2284. path = removeQuotes(path);
  2285. path = cmSystemTools::GetFilenamePath(path.c_str());
  2286. path += "/";
  2287. path += name;
  2288. path += cmSystemTools::GetExecutableExtension();
  2289. if(!cmSystemTools::FileExists(path.c_str()))
  2290. {
  2291. path = mf->GetRequiredDefinition("CMAKE_COMMAND");
  2292. path = cmSystemTools::GetFilenamePath(path.c_str());
  2293. path += "/Debug/";
  2294. path += name;
  2295. path += cmSystemTools::GetExecutableExtension();
  2296. }
  2297. if(!cmSystemTools::FileExists(path.c_str()))
  2298. {
  2299. path = mf->GetRequiredDefinition("CMAKE_COMMAND");
  2300. path = cmSystemTools::GetFilenamePath(path.c_str());
  2301. path += "/Release/";
  2302. path += name;
  2303. path += cmSystemTools::GetExecutableExtension();
  2304. }
  2305. #else
  2306. // Only for bootstrap
  2307. path += mf->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  2308. path += name;
  2309. path += cmSystemTools::GetExecutableExtension();
  2310. #endif
  2311. }
  2312. return path;
  2313. }
  2314. const char* cmake::GetCTestCommand()
  2315. {
  2316. if ( this->CTestCommand.empty() )
  2317. {
  2318. this->CTestCommand = this->FindCMakeProgram("ctest");
  2319. }
  2320. if ( this->CTestCommand.empty() )
  2321. {
  2322. cmSystemTools::Error("Cannot find the CTest executable");
  2323. this->CTestCommand = "CTEST-COMMAND-NOT-FOUND";
  2324. }
  2325. return this->CTestCommand.c_str();
  2326. }
  2327. const char* cmake::GetCPackCommand()
  2328. {
  2329. if ( this->CPackCommand.empty() )
  2330. {
  2331. this->CPackCommand = this->FindCMakeProgram("cpack");
  2332. }
  2333. if ( this->CPackCommand.empty() )
  2334. {
  2335. cmSystemTools::Error("Cannot find the CPack executable");
  2336. this->CPackCommand = "CPACK-COMMAND-NOT-FOUND";
  2337. }
  2338. return this->CPackCommand.c_str();
  2339. }
  2340. void cmake::GenerateGraphViz(const char* fileName) const
  2341. {
  2342. cmGeneratedFileStream str(fileName);
  2343. if ( !str )
  2344. {
  2345. return;
  2346. }
  2347. cmake cm;
  2348. cmGlobalGenerator ggi;
  2349. ggi.SetCMakeInstance(&cm);
  2350. std::auto_ptr<cmLocalGenerator> lg(ggi.CreateLocalGenerator());
  2351. lg->SetGlobalGenerator(&ggi);
  2352. cmMakefile *mf = lg->GetMakefile();
  2353. std::string infile = this->GetHomeOutputDirectory();
  2354. infile += "/CMakeGraphVizOptions.cmake";
  2355. if ( !cmSystemTools::FileExists(infile.c_str()) )
  2356. {
  2357. infile = this->GetHomeDirectory();
  2358. infile += "/CMakeGraphVizOptions.cmake";
  2359. if ( !cmSystemTools::FileExists(infile.c_str()) )
  2360. {
  2361. infile = "";
  2362. }
  2363. }
  2364. if ( !infile.empty() )
  2365. {
  2366. if ( !mf->ReadListFile(0, infile.c_str()) )
  2367. {
  2368. cmSystemTools::Error("Problem opening GraphViz options file: ",
  2369. infile.c_str());
  2370. return;
  2371. }
  2372. std::cout << "Read GraphViz options file: " << infile.c_str()
  2373. << std::endl;
  2374. }
  2375. #define __set_if_not_set(var, value, cmakeDefinition) \
  2376. const char* var = mf->GetDefinition(cmakeDefinition); \
  2377. if ( !var ) \
  2378. { \
  2379. var = value; \
  2380. }
  2381. __set_if_not_set(graphType, "digraph", "GRAPHVIZ_GRAPH_TYPE");
  2382. __set_if_not_set(graphName, "GG", "GRAPHVIZ_GRAPH_NAME");
  2383. __set_if_not_set(graphHeader, "node [\n fontsize = \"12\"\n];",
  2384. "GRAPHVIZ_GRAPH_HEADER");
  2385. __set_if_not_set(graphNodePrefix, "node", "GRAPHVIZ_NODE_PREFIX");
  2386. const char* ignoreTargets = mf->GetDefinition("GRAPHVIZ_IGNORE_TARGETS");
  2387. std::set<cmStdString> ignoreTargetsSet;
  2388. if ( ignoreTargets )
  2389. {
  2390. std::vector<std::string> ignoreTargetsVector;
  2391. cmSystemTools::ExpandListArgument(ignoreTargets,ignoreTargetsVector);
  2392. std::vector<std::string>::iterator itvIt;
  2393. for ( itvIt = ignoreTargetsVector.begin();
  2394. itvIt != ignoreTargetsVector.end();
  2395. ++ itvIt )
  2396. {
  2397. ignoreTargetsSet.insert(itvIt->c_str());
  2398. }
  2399. }
  2400. str << graphType << " " << graphName << " {" << std::endl;
  2401. str << graphHeader << std::endl;
  2402. const cmGlobalGenerator* gg = this->GetGlobalGenerator();
  2403. const std::vector<cmLocalGenerator*>& localGenerators =
  2404. gg->GetLocalGenerators();
  2405. std::vector<cmLocalGenerator*>::const_iterator lit;
  2406. // for target deps
  2407. // 1 - cmake target
  2408. // 2 - external target
  2409. // 0 - no deps
  2410. std::map<cmStdString, int> targetDeps;
  2411. std::map<cmStdString, const cmTarget*> targetPtrs;
  2412. std::map<cmStdString, cmStdString> targetNamesNodes;
  2413. int cnt = 0;
  2414. // First pass get the list of all cmake targets
  2415. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  2416. {
  2417. const cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  2418. cmTargets::const_iterator tit;
  2419. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  2420. {
  2421. const char* realTargetName = tit->first.c_str();
  2422. if ( ignoreTargetsSet.find(realTargetName) != ignoreTargetsSet.end() )
  2423. {
  2424. // Skip ignored targets
  2425. continue;
  2426. }
  2427. //std::cout << "Found target: " << tit->first.c_str() << std::endl;
  2428. cmOStringStream ostr;
  2429. ostr << graphNodePrefix << cnt++;
  2430. targetNamesNodes[realTargetName] = ostr.str();
  2431. targetPtrs[realTargetName] = &tit->second;
  2432. }
  2433. }
  2434. // Ok, now find all the stuff we link to that is not in cmake
  2435. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  2436. {
  2437. const cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  2438. cmTargets::const_iterator tit;
  2439. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  2440. {
  2441. const cmTarget::LinkLibraryVectorType* ll
  2442. = &(tit->second.GetOriginalLinkLibraries());
  2443. cmTarget::LinkLibraryVectorType::const_iterator llit;
  2444. const char* realTargetName = tit->first.c_str();
  2445. if ( ignoreTargetsSet.find(realTargetName) != ignoreTargetsSet.end() )
  2446. {
  2447. // Skip ignored targets
  2448. continue;
  2449. }
  2450. if ( ll->size() > 0 )
  2451. {
  2452. targetDeps[realTargetName] = 1;
  2453. }
  2454. for ( llit = ll->begin(); llit != ll->end(); ++ llit )
  2455. {
  2456. const char* libName = llit->first.c_str();
  2457. std::map<cmStdString, cmStdString>::const_iterator tarIt
  2458. = targetNamesNodes.find(libName);
  2459. if ( ignoreTargetsSet.find(libName) != ignoreTargetsSet.end() )
  2460. {
  2461. // Skip ignored targets
  2462. continue;
  2463. }
  2464. if ( tarIt == targetNamesNodes.end() )
  2465. {
  2466. cmOStringStream ostr;
  2467. ostr << graphNodePrefix << cnt++;
  2468. targetDeps[libName] = 2;
  2469. targetNamesNodes[libName] = ostr.str();
  2470. //str << " \"" << ostr.c_str() << "\" [ label=\"" << libName
  2471. //<< "\" shape=\"ellipse\"];" << std::endl;
  2472. }
  2473. else
  2474. {
  2475. std::map<cmStdString, int>::const_iterator depIt
  2476. = targetDeps.find(libName);
  2477. if ( depIt == targetDeps.end() )
  2478. {
  2479. targetDeps[libName] = 1;
  2480. }
  2481. }
  2482. }
  2483. }
  2484. }
  2485. // Write out nodes
  2486. std::map<cmStdString, int>::const_iterator depIt;
  2487. for ( depIt = targetDeps.begin(); depIt != targetDeps.end(); ++ depIt )
  2488. {
  2489. const char* newTargetName = depIt->first.c_str();
  2490. std::map<cmStdString, cmStdString>::const_iterator tarIt
  2491. = targetNamesNodes.find(newTargetName);
  2492. if ( tarIt == targetNamesNodes.end() )
  2493. {
  2494. // We should not be here.
  2495. std::cout << __LINE__ << " Cannot find library: " << newTargetName
  2496. << " even though it was added in the previous pass" << std::endl;
  2497. abort();
  2498. }
  2499. str << " \"" << tarIt->second.c_str() << "\" [ label=\""
  2500. << newTargetName << "\" shape=\"";
  2501. if ( depIt->second == 1 )
  2502. {
  2503. std::map<cmStdString, const cmTarget*>::const_iterator tarTypeIt =
  2504. targetPtrs.find(newTargetName);
  2505. if ( tarTypeIt == targetPtrs.end() )
  2506. {
  2507. // We should not be here.
  2508. std::cout << __LINE__ << " Cannot find library: " << newTargetName
  2509. << " even though it was added in the previous pass" << std::endl;
  2510. abort();
  2511. }
  2512. const cmTarget* tg = tarTypeIt->second;
  2513. switch ( tg->GetType() )
  2514. {
  2515. case cmTarget::EXECUTABLE:
  2516. str << "house";
  2517. break;
  2518. case cmTarget::STATIC_LIBRARY:
  2519. str << "diamond";
  2520. break;
  2521. case cmTarget::SHARED_LIBRARY:
  2522. str << "polygon";
  2523. break;
  2524. case cmTarget::MODULE_LIBRARY:
  2525. str << "octagon";
  2526. break;
  2527. default:
  2528. str << "box";
  2529. }
  2530. }
  2531. else
  2532. {
  2533. str << "ellipse";
  2534. }
  2535. str << "\"];" << std::endl;
  2536. }
  2537. // Now generate the connectivity
  2538. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  2539. {
  2540. const cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  2541. cmTargets::const_iterator tit;
  2542. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  2543. {
  2544. std::map<cmStdString, int>::iterator dependIt
  2545. = targetDeps.find(tit->first.c_str());
  2546. if ( dependIt == targetDeps.end() )
  2547. {
  2548. continue;
  2549. }
  2550. std::map<cmStdString, cmStdString>::iterator cmakeTarIt
  2551. = targetNamesNodes.find(tit->first.c_str());
  2552. const cmTarget::LinkLibraryVectorType* ll
  2553. = &(tit->second.GetOriginalLinkLibraries());
  2554. cmTarget::LinkLibraryVectorType::const_iterator llit;
  2555. for ( llit = ll->begin(); llit != ll->end(); ++ llit )
  2556. {
  2557. const char* libName = llit->first.c_str();
  2558. std::map<cmStdString, cmStdString>::const_iterator tarIt
  2559. = targetNamesNodes.find(libName);
  2560. if ( tarIt == targetNamesNodes.end() )
  2561. {
  2562. // We should not be here.
  2563. std::cout << __LINE__ << " Cannot find library: " << libName
  2564. << " even though it was added in the previous pass" << std::endl;
  2565. abort();
  2566. }
  2567. str << " \"" << cmakeTarIt->second.c_str() << "\" -> \""
  2568. << tarIt->second.c_str() << "\"" << std::endl;
  2569. }
  2570. }
  2571. }
  2572. // TODO: Use dotted or something for external libraries
  2573. //str << " \"node0\":f4 -> \"node12\"[color=\"#0000ff\" style=dotted]"
  2574. //<< std::endl;
  2575. //
  2576. str << "}" << std::endl;
  2577. }
  2578. //----------------------------------------------------------------------------
  2579. #ifdef CMAKE_BUILD_WITH_CMAKE
  2580. int cmake::ExecuteEchoColor(std::vector<std::string>& args)
  2581. {
  2582. // The arguments are
  2583. // argv[0] == <cmake-executable>
  2584. // argv[1] == cmake_echo_color
  2585. // On some platforms (an MSYS prompt) cmsysTerminal may not be able
  2586. // to determine whether the stream is displayed on a tty. In this
  2587. // case it assumes no unless we tell it otherwise. Since we want
  2588. // color messages to be displayed for users we will assume yes.
  2589. // However, we can test for some situations when the answer is most
  2590. // likely no.
  2591. int assumeTTY = cmsysTerminal_Color_AssumeTTY;
  2592. if(cmSystemTools::GetEnv("DART_TEST_FROM_DART") ||
  2593. cmSystemTools::GetEnv("DASHBOARD_TEST_FROM_CTEST") ||
  2594. cmSystemTools::GetEnv("CTEST_INTERACTIVE_DEBUG_MODE"))
  2595. {
  2596. // Avoid printing color escapes during dashboard builds.
  2597. assumeTTY = 0;
  2598. }
  2599. bool enabled = true;
  2600. int color = cmsysTerminal_Color_Normal;
  2601. bool newline = true;
  2602. for(unsigned int i=2; i < args.size(); ++i)
  2603. {
  2604. if(args[i].find("--switch=") == 0)
  2605. {
  2606. // Enable or disable color based on the switch value.
  2607. std::string value = args[i].substr(9);
  2608. if(!value.empty())
  2609. {
  2610. if(cmSystemTools::IsOn(value.c_str()))
  2611. {
  2612. enabled = true;
  2613. }
  2614. else
  2615. {
  2616. enabled = false;
  2617. }
  2618. }
  2619. }
  2620. else if(args[i] == "--normal")
  2621. {
  2622. color = cmsysTerminal_Color_Normal;
  2623. }
  2624. else if(args[i] == "--black")
  2625. {
  2626. color = cmsysTerminal_Color_ForegroundBlack;
  2627. }
  2628. else if(args[i] == "--red")
  2629. {
  2630. color = cmsysTerminal_Color_ForegroundRed;
  2631. }
  2632. else if(args[i] == "--green")
  2633. {
  2634. color = cmsysTerminal_Color_ForegroundGreen;
  2635. }
  2636. else if(args[i] == "--yellow")
  2637. {
  2638. color = cmsysTerminal_Color_ForegroundYellow;
  2639. }
  2640. else if(args[i] == "--blue")
  2641. {
  2642. color = cmsysTerminal_Color_ForegroundBlue;
  2643. }
  2644. else if(args[i] == "--magenta")
  2645. {
  2646. color = cmsysTerminal_Color_ForegroundMagenta;
  2647. }
  2648. else if(args[i] == "--cyan")
  2649. {
  2650. color = cmsysTerminal_Color_ForegroundCyan;
  2651. }
  2652. else if(args[i] == "--white")
  2653. {
  2654. color = cmsysTerminal_Color_ForegroundWhite;
  2655. }
  2656. else if(args[i] == "--bold")
  2657. {
  2658. color |= cmsysTerminal_Color_ForegroundBold;
  2659. }
  2660. else if(args[i] == "--no-newline")
  2661. {
  2662. newline = false;
  2663. }
  2664. else if(args[i] == "--newline")
  2665. {
  2666. newline = true;
  2667. }
  2668. else if(enabled)
  2669. {
  2670. // Color is enabled. Print with the current color.
  2671. cmsysTerminal_cfprintf(color | assumeTTY, stdout, "%s%s",
  2672. args[i].c_str(), newline? "\n" : "");
  2673. }
  2674. else
  2675. {
  2676. // Color is disabled. Print without color.
  2677. fprintf(stdout, "%s%s", args[i].c_str(), newline? "\n" : "");
  2678. }
  2679. }
  2680. return 0;
  2681. }
  2682. #else
  2683. int cmake::ExecuteEchoColor(std::vector<std::string>&)
  2684. {
  2685. return 1;
  2686. }
  2687. #endif
  2688. //----------------------------------------------------------------------------
  2689. int cmake::ExecuteLinkScript(std::vector<std::string>& args)
  2690. {
  2691. // The arguments are
  2692. // argv[0] == <cmake-executable>
  2693. // argv[1] == cmake_link_script
  2694. // argv[2] == <link-script-name>
  2695. // argv[3] == --verbose=?
  2696. bool verbose = false;
  2697. if(args.size() >= 4)
  2698. {
  2699. if(args[3].find("--verbose=") == 0)
  2700. {
  2701. if(!cmSystemTools::IsOff(args[3].substr(10).c_str()))
  2702. {
  2703. verbose = true;
  2704. }
  2705. }
  2706. }
  2707. // Allocate a process instance.
  2708. cmsysProcess* cp = cmsysProcess_New();
  2709. if(!cp)
  2710. {
  2711. std::cerr << "Error allocating process instance in link script."
  2712. << std::endl;
  2713. return 1;
  2714. }
  2715. // Children should share stdout and stderr with this process.
  2716. cmsysProcess_SetPipeShared(cp, cmsysProcess_Pipe_STDOUT, 1);
  2717. cmsysProcess_SetPipeShared(cp, cmsysProcess_Pipe_STDERR, 1);
  2718. // Run the command lines verbatim.
  2719. cmsysProcess_SetOption(cp, cmsysProcess_Option_Verbatim, 1);
  2720. // Read command lines from the script.
  2721. std::ifstream fin(args[2].c_str());
  2722. if(!fin)
  2723. {
  2724. std::cerr << "Error opening link script \""
  2725. << args[2] << "\"" << std::endl;
  2726. return 1;
  2727. }
  2728. // Run one command at a time.
  2729. std::string command;
  2730. int result = 0;
  2731. while(result == 0 && cmSystemTools::GetLineFromStream(fin, command))
  2732. {
  2733. // Setup this command line.
  2734. const char* cmd[2] = {command.c_str(), 0};
  2735. cmsysProcess_SetCommand(cp, cmd);
  2736. // Report the command if verbose output is enabled.
  2737. if(verbose)
  2738. {
  2739. std::cout << command << std::endl;
  2740. }
  2741. // Run the command and wait for it to exit.
  2742. cmsysProcess_Execute(cp);
  2743. cmsysProcess_WaitForExit(cp, 0);
  2744. // Report failure if any.
  2745. switch(cmsysProcess_GetState(cp))
  2746. {
  2747. case cmsysProcess_State_Exited:
  2748. {
  2749. int value = cmsysProcess_GetExitValue(cp);
  2750. if(value != 0)
  2751. {
  2752. result = value;
  2753. }
  2754. }
  2755. break;
  2756. case cmsysProcess_State_Exception:
  2757. std::cerr << "Error running link command: "
  2758. << cmsysProcess_GetExceptionString(cp) << std::endl;
  2759. result = 1;
  2760. break;
  2761. case cmsysProcess_State_Error:
  2762. std::cerr << "Error running link command: "
  2763. << cmsysProcess_GetErrorString(cp) << std::endl;
  2764. result = 2;
  2765. break;
  2766. default:
  2767. break;
  2768. };
  2769. }
  2770. // Free the process instance.
  2771. cmsysProcess_Delete(cp);
  2772. // Return the final resulting return value.
  2773. return result;
  2774. }
  2775. void cmake::DefineProperties(cmake *cm)
  2776. {
  2777. cm->DefineProperty
  2778. ("TARGET_SUPPORTS_SHARED_LIBS", cmProperty::GLOBAL,
  2779. "Does the target platform support shared libraries.",
  2780. "TARGET_SUPPORTS_SHARED_LIBS is a boolean specifying whether the target "
  2781. "platform supports shared libraries. Basically all current general "
  2782. "general purpose OS do so, the exception are usually embedded systems "
  2783. "with no or special OSs.");
  2784. cm->DefineProperty
  2785. ("FIND_LIBRARY_USE_LIB64_PATHS", cmProperty::GLOBAL,
  2786. "Whether FIND_LIBRARY should automatically search lib64 directories.",
  2787. "FIND_LIBRARY_USE_LIB64_PATHS is a boolean specifying whether the "
  2788. "FIND_LIBRARY command should automatically search the lib64 variant of "
  2789. "directories called lib in the search path when building 64-bit "
  2790. "binaries.");
  2791. }
  2792. void cmake::DefineProperty(const char *name, cmProperty::ScopeType scope,
  2793. const char *ShortDescription,
  2794. const char *FullDescription,
  2795. bool chained)
  2796. {
  2797. this->PropertyDefinitions[scope].DefineProperty(name,scope,ShortDescription,
  2798. FullDescription, chained);
  2799. }
  2800. bool cmake::IsPropertyDefined(const char *name, cmProperty::ScopeType scope)
  2801. {
  2802. return this->PropertyDefinitions[scope].IsPropertyDefined(name);
  2803. }
  2804. bool cmake::IsPropertyChained(const char *name, cmProperty::ScopeType scope)
  2805. {
  2806. return this->PropertyDefinitions[scope].IsPropertyChained(name);
  2807. }
  2808. void cmake::SetProperty(const char* prop, const char* value)
  2809. {
  2810. if (!prop)
  2811. {
  2812. return;
  2813. }
  2814. if (!value)
  2815. {
  2816. value = "NOTFOUND";
  2817. }
  2818. this->Properties.SetProperty(prop, value, cmProperty::TARGET);
  2819. }
  2820. const char *cmake::GetProperty(const char* prop)
  2821. {
  2822. return this->GetProperty(prop, cmProperty::GLOBAL);
  2823. }
  2824. const char *cmake::GetProperty(const char* prop, cmProperty::ScopeType scope)
  2825. {
  2826. bool chain = false;
  2827. // watch for special properties
  2828. std::string propname = prop;
  2829. std::string output = "";
  2830. if ( propname == "CACHE_VARIABLES" )
  2831. {
  2832. cmCacheManager::CacheIterator cit =
  2833. this->GetCacheManager()->GetCacheIterator();
  2834. for ( cit.Begin(); !cit.IsAtEnd(); cit.Next() )
  2835. {
  2836. if ( output.size() )
  2837. {
  2838. output += ";";
  2839. }
  2840. output += cit.GetName();
  2841. }
  2842. this->SetProperty("CACHE_VARIABLES", output.c_str());
  2843. }
  2844. else if ( propname == "COMMANDS" )
  2845. {
  2846. cmake::RegisteredCommandsMap::iterator cmds
  2847. = this->GetCommands()->begin();
  2848. for (unsigned int cc=0 ; cmds != this->GetCommands()->end(); ++ cmds )
  2849. {
  2850. if ( cc > 0 )
  2851. {
  2852. output += ";";
  2853. }
  2854. output += cmds->first.c_str();
  2855. cc++;
  2856. }
  2857. this->SetProperty("COMMANDS",output.c_str());
  2858. }
  2859. return this->Properties.GetPropertyValue(prop, scope, chain);
  2860. }
  2861. bool cmake::GetPropertyAsBool(const char* prop)
  2862. {
  2863. return cmSystemTools::IsOn(this->GetProperty(prop));
  2864. }
  2865. int cmake::GetSystemInformation(std::vector<std::string>& args)
  2866. {
  2867. // so create the directory
  2868. std::string resultFile;
  2869. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  2870. std::string destPath = cwd + "/__cmake_systeminformation";
  2871. cmSystemTools::RemoveADirectory(destPath.c_str());
  2872. if (!cmSystemTools::MakeDirectory(destPath.c_str()))
  2873. {
  2874. std::cerr << "Error: --system-information must be run from a "
  2875. "writable directory!\n";
  2876. return 1;
  2877. }
  2878. // process the arguments
  2879. bool writeToStdout = true;
  2880. for(unsigned int i=1; i < args.size(); ++i)
  2881. {
  2882. std::string arg = args[i];
  2883. if(arg.find("-V",0) == 0)
  2884. {
  2885. this->Verbose = true;
  2886. }
  2887. else if(arg.find("-G",0) == 0)
  2888. {
  2889. std::string value = arg.substr(2);
  2890. if(value.size() == 0)
  2891. {
  2892. ++i;
  2893. if(i >= args.size())
  2894. {
  2895. cmSystemTools::Error("No generator specified for -G");
  2896. return -1;
  2897. }
  2898. value = args[i];
  2899. }
  2900. cmGlobalGenerator* gen =
  2901. this->CreateGlobalGenerator(value.c_str());
  2902. if(!gen)
  2903. {
  2904. cmSystemTools::Error("Could not create named generator ",
  2905. value.c_str());
  2906. }
  2907. else
  2908. {
  2909. this->SetGlobalGenerator(gen);
  2910. }
  2911. }
  2912. // no option assume it is the output file
  2913. else
  2914. {
  2915. if (!cmSystemTools::FileIsFullPath(arg.c_str()))
  2916. {
  2917. resultFile = cwd;
  2918. resultFile += "/";
  2919. }
  2920. resultFile += arg;
  2921. writeToStdout = false;
  2922. }
  2923. }
  2924. // we have to find the module directory, so we can copy the files
  2925. this->AddCMakePaths(args[0].c_str());
  2926. std::string modulesPath =
  2927. this->CacheManager->GetCacheValue("CMAKE_ROOT");
  2928. modulesPath += "/Modules";
  2929. std::string inFile = modulesPath;
  2930. inFile += "/SystemInformation.cmake";
  2931. std::string outFile = destPath;
  2932. outFile += "/CMakeLists.txt";
  2933. // Copy file
  2934. if(!cmSystemTools::cmCopyFile(inFile.c_str(), outFile.c_str()))
  2935. {
  2936. std::cerr << "Error copying file \"" << inFile.c_str()
  2937. << "\" to \"" << outFile.c_str() << "\".\n";
  2938. return 1;
  2939. }
  2940. // do we write to a file or to stdout?
  2941. if (resultFile.size() == 0)
  2942. {
  2943. resultFile = cwd;
  2944. resultFile += "/__cmake_systeminformation/results.txt";
  2945. }
  2946. // now run cmake on the CMakeLists file
  2947. cmSystemTools::ChangeDirectory(destPath.c_str());
  2948. std::vector<std::string> args2;
  2949. args2.push_back(args[0]);
  2950. args2.push_back(destPath);
  2951. std::string resultArg = "-DRESULT_FILE=";
  2952. resultArg += resultFile;
  2953. args2.push_back(resultArg);
  2954. int res = this->Run(args2, false);
  2955. if (res != 0)
  2956. {
  2957. std::cerr << "Error: --system-information failed on internal CMake!\n";
  2958. return res;
  2959. }
  2960. // change back to the original directory
  2961. cmSystemTools::ChangeDirectory(cwd.c_str());
  2962. // echo results to stdout if needed
  2963. if (writeToStdout)
  2964. {
  2965. FILE* fin = fopen(resultFile.c_str(), "r");
  2966. if(fin)
  2967. {
  2968. const int bufferSize = 4096;
  2969. char buffer[bufferSize];
  2970. size_t n;
  2971. while((n = fread(buffer, 1, bufferSize, fin)) > 0)
  2972. {
  2973. for(char* c = buffer; c < buffer+n; ++c)
  2974. {
  2975. putc(*c, stdout);
  2976. }
  2977. fflush(stdout);
  2978. }
  2979. fclose(fin);
  2980. }
  2981. }
  2982. // clean up the directory
  2983. cmSystemTools::RemoveADirectory(destPath.c_str());
  2984. return 0;
  2985. }