cmake.cxx 116 KB

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