cmake.cxx 97 KB

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