cmake.cxx 91 KB

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