cmake.cxx 104 KB

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