cmake.cxx 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmake.h"
  14. #include "time.h"
  15. #include "cmCacheManager.h"
  16. #include "cmMakefile.h"
  17. #include "cmLocalGenerator.h"
  18. #include "cmCommands.h"
  19. #include "cmCommand.h"
  20. #include "cmFileTimeComparison.h"
  21. #include "cmGeneratedFileStream.h"
  22. #if defined(CMAKE_BUILD_WITH_CMAKE)
  23. # include "cmDependsFortran.h" // For -E cmake_copy_f90_mod callback.
  24. # include "cmVariableWatch.h"
  25. # include "cmVersion.h"
  26. #endif
  27. // only build kdevelop generator on non-windows platforms
  28. // when not bootstrapping cmake
  29. #if !defined(_WIN32)
  30. # if defined(CMAKE_BUILD_WITH_CMAKE)
  31. # define CMAKE_USE_KDEVELOP
  32. # endif
  33. #endif
  34. #if defined(__MINGW32__) && !defined(CMAKE_BUILD_WITH_CMAKE)
  35. # define CMAKE_BOOT_MINGW
  36. #endif
  37. // include the generator
  38. #if defined(_WIN32) && !defined(__CYGWIN__)
  39. # if !defined(CMAKE_BOOT_MINGW)
  40. # include "cmGlobalVisualStudio6Generator.h"
  41. # include "cmGlobalVisualStudio7Generator.h"
  42. # include "cmGlobalVisualStudio71Generator.h"
  43. # include "cmGlobalVisualStudio8Generator.h"
  44. # include "cmGlobalVisualStudio8Win64Generator.h"
  45. # include "cmGlobalBorlandMakefileGenerator.h"
  46. # include "cmGlobalNMakeMakefileGenerator.h"
  47. # include "cmGlobalWatcomWMakeGenerator.h"
  48. # endif
  49. # include "cmGlobalMSYSMakefileGenerator.h"
  50. # include "cmGlobalMinGWMakefileGenerator.h"
  51. # include "cmWin32ProcessExecution.h"
  52. #else
  53. #endif
  54. #include "cmGlobalUnixMakefileGenerator3.h"
  55. #ifdef CMAKE_USE_KDEVELOP
  56. # include "cmGlobalKdevelopGenerator.h"
  57. #endif
  58. #include <stdlib.h> // required for atoi
  59. #if defined( __APPLE__ )
  60. # if defined(CMAKE_BUILD_WITH_CMAKE)
  61. # include "cmGlobalXCodeGenerator.h"
  62. # define CMAKE_USE_XCODE 1
  63. # endif
  64. # include <sys/types.h>
  65. # include <sys/time.h>
  66. # include <sys/resource.h>
  67. #endif
  68. #include <sys/stat.h> // struct stat
  69. #include <memory> // auto_ptr
  70. void cmNeedBackwardsCompatibility(const std::string& variable,
  71. int access_type, void* )
  72. {
  73. #ifdef CMAKE_BUILD_WITH_CMAKE
  74. if (access_type == cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS)
  75. {
  76. std::string message = "An attempt was made to access a variable: ";
  77. message += variable;
  78. message +=
  79. " that has not been defined. Some variables were always defined "
  80. "by CMake in versions prior to 1.6. To fix this you might need to set "
  81. "the cache value of CMAKE_BACKWARDS_COMPATIBILITY to 1.4 or less. If "
  82. "you are writing a CMakeList file, (or have already set "
  83. "CMAKE_BACKWARDS_COMPATABILITY to 1.4 or less) then you probably need "
  84. "to include a CMake module to test for the feature this variable "
  85. "defines.";
  86. cmSystemTools::Error(message.c_str());
  87. }
  88. #else
  89. (void)variable;
  90. (void)access_type;
  91. #endif
  92. }
  93. cmake::cmake()
  94. {
  95. this->DebugTryCompile = false;
  96. this->ClearBuildSystem = false;
  97. this->FileComparison = new cmFileTimeComparison;
  98. #ifdef __APPLE__
  99. struct rlimit rlp;
  100. if(!getrlimit(RLIMIT_STACK, &rlp))
  101. {
  102. if(rlp.rlim_cur != rlp.rlim_max)
  103. {
  104. rlp.rlim_cur = rlp.rlim_max;
  105. setrlimit(RLIMIT_STACK, &rlp);
  106. }
  107. }
  108. #endif
  109. // If MAKEFLAGS are given in the environment, remove the environment
  110. // variable. This will prevent try-compile from succeeding when it
  111. // should fail (if "-i" is an option). We cannot simply test
  112. // whether "-i" is given and remove it because some make programs
  113. // encode the MAKEFLAGS variable in a strange way.
  114. if(getenv("MAKEFLAGS"))
  115. {
  116. cmSystemTools::PutEnv("MAKEFLAGS=");
  117. }
  118. this->Verbose = false;
  119. this->InTryCompile = false;
  120. this->CacheManager = new cmCacheManager;
  121. this->GlobalGenerator = 0;
  122. this->ProgressCallback = 0;
  123. this->ProgressCallbackClientData = 0;
  124. this->ScriptMode = false;
  125. #ifdef CMAKE_BUILD_WITH_CMAKE
  126. this->VariableWatch = new cmVariableWatch;
  127. this->VariableWatch->AddWatch("CMAKE_WORDS_BIGENDIAN",
  128. cmNeedBackwardsCompatibility);
  129. this->VariableWatch->AddWatch("CMAKE_SIZEOF_INT",
  130. cmNeedBackwardsCompatibility);
  131. this->VariableWatch->AddWatch("CMAKE_X_LIBS",
  132. cmNeedBackwardsCompatibility);
  133. #endif
  134. this->AddDefaultGenerators();
  135. this->AddDefaultCommands();
  136. // Make sure we can capture the build tool output.
  137. cmSystemTools::EnableVSConsoleOutput();
  138. }
  139. cmake::~cmake()
  140. {
  141. delete this->CacheManager;
  142. if (this->GlobalGenerator)
  143. {
  144. delete this->GlobalGenerator;
  145. this->GlobalGenerator = 0;
  146. }
  147. for(RegisteredCommandsMap::iterator j = this->Commands.begin();
  148. j != this->Commands.end(); ++j)
  149. {
  150. delete (*j).second;
  151. }
  152. #ifdef CMAKE_BUILD_WITH_CMAKE
  153. delete this->VariableWatch;
  154. #endif
  155. delete this->FileComparison;
  156. }
  157. void cmake::CleanupCommandsAndMacros()
  158. {
  159. std::vector<cmCommand*> commands;
  160. for(RegisteredCommandsMap::iterator j = this->Commands.begin();
  161. j != this->Commands.end(); ++j)
  162. {
  163. if ( !j->second->IsA("cmMacroHelperCommand") )
  164. {
  165. commands.push_back(j->second);
  166. }
  167. else
  168. {
  169. delete j->second;
  170. }
  171. }
  172. this->Commands.erase(this->Commands.begin(), this->Commands.end());
  173. std::vector<cmCommand*>::iterator it;
  174. for ( it = commands.begin(); it != commands.end();
  175. ++ it )
  176. {
  177. this->Commands[cmSystemTools::LowerCase((*it)->GetName())] = *it;
  178. }
  179. }
  180. bool cmake::CommandExists(const char* name) const
  181. {
  182. std::string sName = cmSystemTools::LowerCase(name);
  183. return (this->Commands.find(sName) != this->Commands.end());
  184. }
  185. cmCommand *cmake::GetCommand(const char *name)
  186. {
  187. cmCommand* rm = 0;
  188. std::string sName = cmSystemTools::LowerCase(name);
  189. RegisteredCommandsMap::iterator pos = this->Commands.find(sName);
  190. if (pos != this->Commands.end())
  191. {
  192. rm = (*pos).second;
  193. }
  194. return rm;
  195. }
  196. void cmake::RenameCommand(const char*oldName, const char* newName)
  197. {
  198. // if the command already exists, free the old one
  199. std::string sOldName = cmSystemTools::LowerCase(oldName);
  200. std::string sNewName = cmSystemTools::LowerCase(newName);
  201. RegisteredCommandsMap::iterator pos = this->Commands.find(sOldName);
  202. if ( pos == this->Commands.end() )
  203. {
  204. return;
  205. }
  206. cmCommand* cmd = pos->second;
  207. pos = this->Commands.find(sNewName);
  208. if (pos != this->Commands.end())
  209. {
  210. delete pos->second;
  211. this->Commands.erase(pos);
  212. }
  213. this->Commands.insert(RegisteredCommandsMap::value_type(sNewName, cmd));
  214. pos = this->Commands.find(sOldName);
  215. this->Commands.erase(pos);
  216. }
  217. void cmake::AddCommand(cmCommand* wg)
  218. {
  219. std::string name = cmSystemTools::LowerCase(wg->GetName());
  220. // if the command already exists, free the old one
  221. RegisteredCommandsMap::iterator pos = this->Commands.find(name);
  222. if (pos != this->Commands.end())
  223. {
  224. delete pos->second;
  225. this->Commands.erase(pos);
  226. }
  227. this->Commands.insert( RegisteredCommandsMap::value_type(name, wg));
  228. }
  229. // Parse the args
  230. bool cmake::SetCacheArgs(const std::vector<std::string>& args)
  231. {
  232. for(unsigned int i=1; i < args.size(); ++i)
  233. {
  234. std::string arg = args[i];
  235. if(arg.find("-D",0) == 0)
  236. {
  237. std::string entry = arg.substr(2);
  238. if(entry.size() == 0)
  239. {
  240. entry = args[++i];
  241. }
  242. std::string var, value;
  243. cmCacheManager::CacheEntryType type = cmCacheManager::UNINITIALIZED;
  244. if(cmCacheManager::ParseEntry(entry.c_str(), var, value, type) ||
  245. cmCacheManager::ParseEntry(entry.c_str(), var, value))
  246. {
  247. this->CacheManager->AddCacheEntry(var.c_str(), value.c_str(),
  248. "No help, variable specified on the command line.",
  249. type);
  250. }
  251. else
  252. {
  253. std::cerr << "Parse error in command line argument: " << arg << "\n"
  254. << "Should be: VAR:type=value\n";
  255. cmSystemTools::Error("No cmake scrpt provided.");
  256. return false;
  257. }
  258. }
  259. else if(arg.find("-C",0) == 0)
  260. {
  261. std::string path = arg.substr(2);
  262. if ( path.size() == 0 )
  263. {
  264. path = args[++i];
  265. }
  266. std::cerr << "loading initial cache file " << path.c_str() << "\n";
  267. this->ReadListFile(path.c_str());
  268. }
  269. else if(arg.find("-P",0) == 0)
  270. {
  271. i++;
  272. std::string path = args[i];
  273. if ( path.size() == 0 )
  274. {
  275. cmSystemTools::Error("No cmake script provided.");
  276. return false;
  277. }
  278. this->ReadListFile(path.c_str());
  279. }
  280. }
  281. return true;
  282. }
  283. void cmake::ReadListFile(const char *path)
  284. {
  285. // if a generator was not yet created, temporarily create one
  286. cmGlobalGenerator *gg = this->GetGlobalGenerator();
  287. bool created = false;
  288. // if a generator was not specified use a generic one
  289. if (!gg)
  290. {
  291. gg = new cmGlobalGenerator;
  292. gg->SetCMakeInstance(this);
  293. created = true;
  294. }
  295. // read in the list file to fill the cache
  296. if(path)
  297. {
  298. std::auto_ptr<cmLocalGenerator> lg(gg->CreateLocalGenerator());
  299. lg->SetGlobalGenerator(gg);
  300. lg->GetMakefile()->SetHomeOutputDirectory
  301. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  302. lg->GetMakefile()->SetStartOutputDirectory
  303. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  304. lg->GetMakefile()->SetHomeDirectory
  305. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  306. lg->GetMakefile()->SetStartDirectory
  307. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  308. if (!lg->GetMakefile()->ReadListFile(0, path))
  309. {
  310. std::cerr << "Error processing file:" << path << "\n";
  311. }
  312. }
  313. // free generic one if generated
  314. if (created)
  315. {
  316. delete gg;
  317. }
  318. }
  319. // Parse the args
  320. void cmake::SetArgs(const std::vector<std::string>& args)
  321. {
  322. bool directoriesSet = false;
  323. for(unsigned int i=1; i < args.size(); ++i)
  324. {
  325. std::string arg = args[i];
  326. if(arg.find("-H",0) == 0)
  327. {
  328. directoriesSet = true;
  329. std::string path = arg.substr(2);
  330. path = cmSystemTools::CollapseFullPath(path.c_str());
  331. cmSystemTools::ConvertToUnixSlashes(path);
  332. this->SetHomeDirectory(path.c_str());
  333. }
  334. else if(arg.find("-S",0) == 0)
  335. {
  336. // There is no local generate anymore. Ignore -S option.
  337. }
  338. else if(arg.find("-O",0) == 0)
  339. {
  340. // There is no local generate anymore. Ignore -O option.
  341. }
  342. else if(arg.find("-B",0) == 0)
  343. {
  344. directoriesSet = true;
  345. std::string path = arg.substr(2);
  346. path = cmSystemTools::CollapseFullPath(path.c_str());
  347. cmSystemTools::ConvertToUnixSlashes(path);
  348. this->SetHomeOutputDirectory(path.c_str());
  349. }
  350. else if((i < args.size()-1) && (arg.find("--check-build-system",0) == 0))
  351. {
  352. this->CheckBuildSystemArgument = args[++i];
  353. this->ClearBuildSystem = (atoi(args[++i].c_str()) > 0);
  354. }
  355. else if(arg.find("-V",0) == 0)
  356. {
  357. this->Verbose = true;
  358. }
  359. else if(arg.find("-D",0) == 0)
  360. {
  361. // skip for now
  362. }
  363. else if(arg.find("-C",0) == 0)
  364. {
  365. // skip for now
  366. }
  367. else if(arg.find("-P",0) == 0)
  368. {
  369. // skip for now
  370. i++;
  371. }
  372. else if(arg.find("--graphviz=",0) == 0)
  373. {
  374. std::string path = arg.substr(strlen("--graphviz="));
  375. path = cmSystemTools::CollapseFullPath(path.c_str());
  376. cmSystemTools::ConvertToUnixSlashes(path);
  377. this->GraphVizFile = path;
  378. if ( this->GraphVizFile.empty() )
  379. {
  380. cmSystemTools::Error("No file specified for --graphviz");
  381. }
  382. }
  383. else if(arg.find("--debug-trycompile",0) == 0)
  384. {
  385. std::cout << "debug trycompile on\n";
  386. this->DebugTryCompileOn();
  387. }
  388. else if(arg.find("-G",0) == 0)
  389. {
  390. std::string value = arg.substr(2);
  391. if(value.size() == 0)
  392. {
  393. value = args[++i];
  394. }
  395. cmGlobalGenerator* gen =
  396. this->CreateGlobalGenerator(value.c_str());
  397. if(!gen)
  398. {
  399. cmSystemTools::Error("Could not create named generator ",
  400. value.c_str());
  401. }
  402. else
  403. {
  404. this->SetGlobalGenerator(gen);
  405. }
  406. }
  407. // no option assume it is the path to the source
  408. else
  409. {
  410. directoriesSet = true;
  411. this->SetDirectoriesFromFile(arg.c_str());
  412. }
  413. }
  414. if(!directoriesSet)
  415. {
  416. this->SetHomeOutputDirectory
  417. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  418. this->SetStartOutputDirectory
  419. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  420. this->SetHomeDirectory
  421. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  422. this->SetStartDirectory
  423. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  424. }
  425. this->SetStartDirectory(this->GetHomeDirectory());
  426. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  427. }
  428. //----------------------------------------------------------------------------
  429. void cmake::SetDirectoriesFromFile(const char* arg)
  430. {
  431. // Check if the argument refers to a CMakeCache.txt or
  432. // CMakeLists.txt file.
  433. std::string listPath;
  434. std::string cachePath;
  435. bool argIsFile = false;
  436. if(cmSystemTools::FileIsDirectory(arg))
  437. {
  438. std::string path = cmSystemTools::CollapseFullPath(arg);
  439. cmSystemTools::ConvertToUnixSlashes(path);
  440. std::string cacheFile = path;
  441. cacheFile += "/CMakeCache.txt";
  442. std::string listFile = path;
  443. listFile += "/CMakeLists.txt";
  444. if(cmSystemTools::FileExists(cacheFile.c_str()))
  445. {
  446. cachePath = path;
  447. }
  448. if(cmSystemTools::FileExists(listFile.c_str()))
  449. {
  450. listPath = path;
  451. }
  452. }
  453. else if(cmSystemTools::FileExists(arg))
  454. {
  455. argIsFile = true;
  456. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  457. std::string name = cmSystemTools::GetFilenameName(fullPath.c_str());
  458. name = cmSystemTools::LowerCase(name);
  459. if(name == "cmakecache.txt")
  460. {
  461. cachePath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  462. }
  463. else if(name == "cmakelists.txt")
  464. {
  465. listPath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  466. }
  467. }
  468. else
  469. {
  470. // Specified file or directory does not exist. Try to set things
  471. // up to produce a meaningful error message.
  472. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  473. std::string name = cmSystemTools::GetFilenameName(fullPath.c_str());
  474. name = cmSystemTools::LowerCase(name);
  475. if(name == "cmakecache.txt" || name == "cmakelists.txt")
  476. {
  477. argIsFile = true;
  478. listPath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  479. }
  480. else
  481. {
  482. listPath = fullPath;
  483. }
  484. }
  485. // If there is a CMakeCache.txt file, use its settings.
  486. if(cachePath.length() > 0)
  487. {
  488. cmCacheManager* cachem = this->GetCacheManager();
  489. cmCacheManager::CacheIterator it = cachem->NewIterator();
  490. if(cachem->LoadCache(cachePath.c_str()) &&
  491. it.Find("CMAKE_HOME_DIRECTORY"))
  492. {
  493. this->SetHomeOutputDirectory(cachePath.c_str());
  494. this->SetStartOutputDirectory(cachePath.c_str());
  495. this->SetHomeDirectory(it.GetValue());
  496. this->SetStartDirectory(it.GetValue());
  497. return;
  498. }
  499. }
  500. // If there is a CMakeLists.txt file, use it as the source tree.
  501. if(listPath.length() > 0)
  502. {
  503. this->SetHomeDirectory(listPath.c_str());
  504. this->SetStartDirectory(listPath.c_str());
  505. if(argIsFile)
  506. {
  507. // Source CMakeLists.txt file given. It was probably dropped
  508. // onto the executable in a GUI. Default to an in-source build.
  509. this->SetHomeOutputDirectory(listPath.c_str());
  510. this->SetStartOutputDirectory(listPath.c_str());
  511. }
  512. else
  513. {
  514. // Source directory given on command line. Use current working
  515. // directory as build tree.
  516. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  517. this->SetHomeOutputDirectory(cwd.c_str());
  518. this->SetStartOutputDirectory(cwd.c_str());
  519. }
  520. return;
  521. }
  522. // We didn't find a CMakeLists.txt or CMakeCache.txt file from the
  523. // argument. Assume it is the path to the source tree, and use the
  524. // current working directory as the build tree.
  525. std::string full = cmSystemTools::CollapseFullPath(arg);
  526. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  527. this->SetHomeDirectory(full.c_str());
  528. this->SetStartDirectory(full.c_str());
  529. this->SetHomeOutputDirectory(cwd.c_str());
  530. this->SetStartOutputDirectory(cwd.c_str());
  531. }
  532. // at the end of this CMAKE_ROOT and CMAKE_COMMAND should be added to the
  533. // cache
  534. int cmake::AddCMakePaths(const char *arg0)
  535. {
  536. // Find our own executable.
  537. std::vector<cmStdString> failures;
  538. std::string cMakeSelf = arg0;
  539. cmSystemTools::ConvertToUnixSlashes(cMakeSelf);
  540. failures.push_back(cMakeSelf);
  541. cMakeSelf = cmSystemTools::FindProgram(cMakeSelf.c_str());
  542. cmSystemTools::ConvertToUnixSlashes(cMakeSelf);
  543. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  544. {
  545. #ifdef CMAKE_BUILD_DIR
  546. std::string intdir = ".";
  547. #ifdef CMAKE_INTDIR
  548. intdir = CMAKE_INTDIR;
  549. #endif
  550. cMakeSelf = CMAKE_BUILD_DIR;
  551. cMakeSelf += "/bin/";
  552. cMakeSelf += intdir;
  553. cMakeSelf += "/cmake";
  554. cMakeSelf += cmSystemTools::GetExecutableExtension();
  555. #endif
  556. }
  557. #ifdef CMAKE_PREFIX
  558. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  559. {
  560. failures.push_back(cMakeSelf);
  561. cMakeSelf = CMAKE_PREFIX "/bin/cmake";
  562. }
  563. #endif
  564. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  565. {
  566. failures.push_back(cMakeSelf);
  567. cmOStringStream msg;
  568. msg << "CMAKE can not find the command line program cmake.\n";
  569. msg << " argv[0] = \"" << arg0 << "\"\n";
  570. msg << " Attempted paths:\n";
  571. std::vector<cmStdString>::iterator i;
  572. for(i=failures.begin(); i != failures.end(); ++i)
  573. {
  574. msg << " \"" << i->c_str() << "\"\n";
  575. }
  576. cmSystemTools::Error(msg.str().c_str());
  577. return 0;
  578. }
  579. // Save the value in the cache
  580. this->CacheManager->AddCacheEntry
  581. ("CMAKE_COMMAND",cMakeSelf.c_str(), "Path to CMake executable.",
  582. cmCacheManager::INTERNAL);
  583. // Find and save the command to edit the cache
  584. std::string editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  585. "/ccmake" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  586. if( !cmSystemTools::FileExists(editCacheCommand.c_str()))
  587. {
  588. editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  589. "/CMakeSetup" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  590. }
  591. std::string ctestCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  592. "/ctest" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  593. if(cmSystemTools::FileExists(ctestCommand.c_str()))
  594. {
  595. this->CacheManager->AddCacheEntry
  596. ("CMAKE_CTEST_COMMAND", ctestCommand.c_str(),
  597. "Path to ctest program executable.", cmCacheManager::INTERNAL);
  598. }
  599. if(cmSystemTools::FileExists(editCacheCommand.c_str()))
  600. {
  601. this->CacheManager->AddCacheEntry
  602. ("CMAKE_EDIT_COMMAND", editCacheCommand.c_str(),
  603. "Path to cache edit program executable.", cmCacheManager::INTERNAL);
  604. }
  605. // do CMAKE_ROOT, look for the environment variable first
  606. std::string cMakeRoot;
  607. std::string modules;
  608. if (getenv("CMAKE_ROOT"))
  609. {
  610. cMakeRoot = getenv("CMAKE_ROOT");
  611. modules = cMakeRoot + "/Modules/CMake.cmake";
  612. }
  613. if(!cmSystemTools::FileExists(modules.c_str()))
  614. {
  615. // next try exe/..
  616. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  617. std::string::size_type slashPos = cMakeRoot.rfind("/");
  618. if(slashPos != std::string::npos)
  619. {
  620. cMakeRoot = cMakeRoot.substr(0, slashPos);
  621. }
  622. // is there no Modules direcory there?
  623. modules = cMakeRoot + "/Modules/CMake.cmake";
  624. }
  625. if (!cmSystemTools::FileExists(modules.c_str()))
  626. {
  627. // try exe/../share/cmake
  628. cMakeRoot += CMAKE_DATA_DIR;
  629. modules = cMakeRoot + "/Modules/CMake.cmake";
  630. }
  631. #ifdef CMAKE_ROOT_DIR
  632. if (!cmSystemTools::FileExists(modules.c_str()))
  633. {
  634. // try compiled in root directory
  635. cMakeRoot = CMAKE_ROOT_DIR;
  636. modules = cMakeRoot + "/Modules/CMake.cmake";
  637. }
  638. #endif
  639. #ifdef CMAKE_PREFIX
  640. if (!cmSystemTools::FileExists(modules.c_str()))
  641. {
  642. // try compiled in install prefix
  643. cMakeRoot = CMAKE_PREFIX CMAKE_DATA_DIR;
  644. modules = cMakeRoot + "/Modules/CMake.cmake";
  645. }
  646. #endif
  647. if (!cmSystemTools::FileExists(modules.c_str()))
  648. {
  649. // try
  650. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  651. cMakeRoot += CMAKE_DATA_DIR;
  652. modules = cMakeRoot + "/Modules/CMake.cmake";
  653. }
  654. if(!cmSystemTools::FileExists(modules.c_str()))
  655. {
  656. // next try exe
  657. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  658. // is there no Modules direcory there?
  659. modules = cMakeRoot + "/Modules/CMake.cmake";
  660. }
  661. if (!cmSystemTools::FileExists(modules.c_str()))
  662. {
  663. // couldn't find modules
  664. cmSystemTools::Error("Could not find CMAKE_ROOT !!!\n"
  665. "CMake has most likely not been installed correctly.\n"
  666. "Modules directory not found in\n",
  667. cMakeRoot.c_str());
  668. return 0;
  669. }
  670. this->CacheManager->AddCacheEntry
  671. ("CMAKE_ROOT", cMakeRoot.c_str(),
  672. "Path to CMake installation.", cmCacheManager::INTERNAL);
  673. #ifdef _WIN32
  674. std::string comspec = "cmw9xcom.exe";
  675. cmSystemTools::SetWindows9xComspecSubstitute(comspec.c_str());
  676. #endif
  677. return 1;
  678. }
  679. void CMakeCommandUsage(const char* program)
  680. {
  681. cmOStringStream errorStream;
  682. #ifdef CMAKE_BUILD_WITH_CMAKE
  683. errorStream
  684. << "cmake version " << cmVersion::GetCMakeVersion() << "\n";
  685. #else
  686. errorStream
  687. << "cmake bootstrap\n";
  688. #endif
  689. errorStream
  690. << "Usage: " << program << " -E [command] [arguments ...]\n"
  691. << "Available commands: \n"
  692. << " chdir dir cmd [args]... - run command in a given directory\n"
  693. << " copy file destination - copy file to destination (either file or "
  694. "directory)\n"
  695. << " copy_if_different in-file out-file - copy file if input has "
  696. "changed\n"
  697. << " copy_directory source destination - copy directory 'source' "
  698. "content to directory 'destination'\n"
  699. << " compare_files file1 file2 - check if file1 is same as file2\n"
  700. << " echo [string]... - displays arguments as text\n"
  701. << " environment - display the current enviroment\n"
  702. << " remove file1 file2 ... - remove the file(s)\n"
  703. << " tar [cxt][vfz] file.tar file/dir1 file/dir2 ... - create a tar.\n"
  704. << " time command [args] ... - run command and return elapsed time\n"
  705. #if defined(_WIN32) && !defined(__CYGWIN__)
  706. << " write_regv key value - write registry value\n"
  707. << " delete_regv key - delete registry value\n"
  708. << " comspec - on windows 9x use this for RunCommand\n"
  709. #else
  710. << " create_symlink old new - create a symbolic link new -> old\n"
  711. #endif
  712. ;
  713. cmSystemTools::Error(errorStream.str().c_str());
  714. }
  715. int cmake::ExecuteCMakeCommand(std::vector<std::string>& args)
  716. {
  717. if (args.size() > 1)
  718. {
  719. // Copy file
  720. if (args[1] == "copy" && args.size() == 4)
  721. {
  722. if(!cmSystemTools::cmCopyFile(args[2].c_str(), args[3].c_str()))
  723. {
  724. std::cerr << "Error copying file \"" << args[2].c_str()
  725. << "\" to \"" << args[3].c_str() << "\".\n";
  726. return 1;
  727. }
  728. return 0;
  729. }
  730. // Copy file if different.
  731. if (args[1] == "copy_if_different" && args.size() == 4)
  732. {
  733. if(!cmSystemTools::CopyFileIfDifferent(args[2].c_str(),
  734. args[3].c_str()))
  735. {
  736. std::cerr << "Error copying file (if different) from \""
  737. << args[2].c_str() << "\" to \"" << args[3].c_str()
  738. << "\".\n";
  739. return 1;
  740. }
  741. return 0;
  742. }
  743. // Copy directory content
  744. if (args[1] == "copy_directory" && args.size() == 4)
  745. {
  746. if(!cmSystemTools::CopyADirectory(args[2].c_str(), args[3].c_str()))
  747. {
  748. std::cerr << "Error copying directory from \""
  749. << args[2].c_str() << "\" to \"" << args[3].c_str()
  750. << "\".\n";
  751. return 1;
  752. }
  753. return 0;
  754. }
  755. // Compare files
  756. if (args[1] == "compare_files" && args.size() == 4)
  757. {
  758. if(cmSystemTools::FilesDiffer(args[2].c_str(), args[3].c_str()))
  759. {
  760. std::cerr << "Files \""
  761. << args[2].c_str() << "\" to \"" << args[3].c_str()
  762. << "\" are different.\n";
  763. return 1;
  764. }
  765. return 0;
  766. }
  767. // Echo string
  768. else if (args[1] == "echo" )
  769. {
  770. unsigned int cc;
  771. const char* space = "";
  772. for ( cc = 2; cc < args.size(); cc ++ )
  773. {
  774. std::cout << space << args[cc];
  775. space = " ";
  776. }
  777. std::cout << std::endl;
  778. return 0;
  779. }
  780. #if defined(CMAKE_BUILD_WITH_CMAKE)
  781. // Command to create a symbolic link. Fails on platforms not
  782. // supporting them.
  783. else if (args[1] == "environment" )
  784. {
  785. std::vector<std::string> env = cmSystemTools::GetEnvironmentVariables();
  786. std::vector<std::string>::iterator it;
  787. for ( it = env.begin(); it != env.end(); ++ it )
  788. {
  789. std::cout << it->c_str() << std::endl;
  790. }
  791. return 0;
  792. }
  793. #endif
  794. // Remove file
  795. else if (args[1] == "remove" && args.size() > 2)
  796. {
  797. for (std::string::size_type cc = 2; cc < args.size(); cc ++)
  798. {
  799. if(args[cc] != "-f")
  800. {
  801. if(args[cc] == "\\-f")
  802. {
  803. args[cc] = "-f";
  804. }
  805. cmSystemTools::RemoveFile(args[cc].c_str());
  806. }
  807. }
  808. return 0;
  809. }
  810. // Clock command
  811. else if (args[1] == "time" && args.size() > 2)
  812. {
  813. std::string command = args[2];
  814. for (std::string::size_type cc = 3; cc < args.size(); cc ++)
  815. {
  816. command += " ";
  817. command += args[cc];
  818. }
  819. clock_t clock_start, clock_finish;
  820. time_t time_start, time_finish;
  821. time(&time_start);
  822. clock_start = clock();
  823. cmSystemTools::RunSingleCommand(command.c_str());
  824. clock_finish = clock();
  825. time(&time_finish);
  826. double clocks_per_sec = (double)CLOCKS_PER_SEC;
  827. std::cout << "Elapsed time: "
  828. << (long)(time_finish - time_start) << " s. (time)"
  829. << ", "
  830. << (double)(clock_finish - clock_start) / clocks_per_sec
  831. << " s. (clock)"
  832. << "\n";
  833. return 0;
  834. }
  835. // Command to change directory and run a program.
  836. else if (args[1] == "chdir" && args.size() >= 4)
  837. {
  838. std::string directory = args[2];
  839. if(!cmSystemTools::FileExists(directory.c_str()))
  840. {
  841. cmSystemTools::Error("Directory does not exist for chdir command: ",
  842. args[2].c_str());
  843. return 0;
  844. }
  845. std::string command = "\"";
  846. command += args[3];
  847. command += "\"";
  848. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  849. {
  850. command += " \"";
  851. command += args[cc];
  852. command += "\"";
  853. }
  854. int retval = 0;
  855. int timeout = 0;
  856. if ( cmSystemTools::RunSingleCommand(command.c_str(), 0, &retval,
  857. directory.c_str(), true, timeout) )
  858. {
  859. return retval;
  860. }
  861. return 1;
  862. }
  863. // Command to create a symbolic link. Fails on platforms not
  864. // supporting them.
  865. else if (args[1] == "create_symlink" && args.size() == 4)
  866. {
  867. return cmSystemTools::CreateSymlink(args[2].c_str(),
  868. args[3].c_str())? 0:1;
  869. }
  870. // Internal CMake shared library support.
  871. else if (args[1] == "cmake_symlink_library" && args.size() == 5)
  872. {
  873. int result = 0;
  874. std::string realName = args[2];
  875. std::string soName = args[3];
  876. std::string name = args[4];
  877. if(soName != realName)
  878. {
  879. std::string fname = cmSystemTools::GetFilenameName(realName);
  880. if(cmSystemTools::FileExists(soName.c_str()))
  881. {
  882. cmSystemTools::RemoveFile(soName.c_str());
  883. }
  884. if(!cmSystemTools::CreateSymlink(fname.c_str(), soName.c_str()))
  885. {
  886. result = 1;
  887. }
  888. }
  889. if(name != soName)
  890. {
  891. std::string fname = cmSystemTools::GetFilenameName(soName);
  892. if(cmSystemTools::FileExists(soName.c_str()))
  893. {
  894. cmSystemTools::RemoveFile(name.c_str());
  895. }
  896. if(!cmSystemTools::CreateSymlink(fname.c_str(), name.c_str()))
  897. {
  898. result = 1;
  899. }
  900. }
  901. return result;
  902. }
  903. // Internal CMake versioned executable support.
  904. else if (args[1] == "cmake_symlink_executable" && args.size() == 4)
  905. {
  906. int result = 0;
  907. std::string realName = args[2];
  908. std::string name = args[3];
  909. if(name != realName)
  910. {
  911. std::string fname = cmSystemTools::GetFilenameName(realName);
  912. if(cmSystemTools::FileExists(realName.c_str()))
  913. {
  914. cmSystemTools::RemoveFile(name.c_str());
  915. }
  916. if(!cmSystemTools::CreateSymlink(fname.c_str(), name.c_str()))
  917. {
  918. result = 1;
  919. }
  920. }
  921. return result;
  922. }
  923. // Internal CMake dependency scanning support.
  924. else if (args[1] == "cmake_depends" && args.size() >= 6)
  925. {
  926. cmake cm;
  927. cmGlobalGenerator *ggd = cm.CreateGlobalGenerator(args[2].c_str());
  928. if (ggd)
  929. {
  930. ggd->SetCMakeInstance(&cm);
  931. std::auto_ptr<cmLocalGenerator> lgd(ggd->CreateLocalGenerator());
  932. lgd->SetGlobalGenerator(ggd);
  933. return lgd->ScanDependencies(args)? 0 : 2;
  934. }
  935. return 1;
  936. }
  937. // Tar files
  938. else if (args[1] == "tar" && args.size() > 3)
  939. {
  940. std::string flags = args[2];
  941. std::string outFile = args[3];
  942. std::vector<cmStdString> files;
  943. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  944. {
  945. files.push_back(args[cc]);
  946. }
  947. bool gzip = false;
  948. bool verbose = false;
  949. if ( flags.find_first_of('z') != flags.npos )
  950. {
  951. gzip = true;
  952. }
  953. if ( flags.find_first_of('v') != flags.npos )
  954. {
  955. verbose = true;
  956. }
  957. if ( flags.find_first_of('t') != flags.npos )
  958. {
  959. if ( !cmSystemTools::ListTar(outFile.c_str(), files, gzip, verbose) )
  960. {
  961. cmSystemTools::Error("Problem creating tar: ", outFile.c_str());
  962. return 1;
  963. }
  964. }
  965. else if ( flags.find_first_of('c') != flags.npos )
  966. {
  967. if ( !cmSystemTools::CreateTar(
  968. outFile.c_str(), files, gzip, verbose) )
  969. {
  970. cmSystemTools::Error("Problem creating tar: ", outFile.c_str());
  971. return 1;
  972. }
  973. }
  974. else if ( flags.find_first_of('x') != flags.npos )
  975. {
  976. if ( !cmSystemTools::ExtractTar(
  977. outFile.c_str(), files, gzip, verbose) )
  978. {
  979. cmSystemTools::Error("Problem extracting tar: ", outFile.c_str());
  980. return 1;
  981. }
  982. }
  983. return 0;
  984. }
  985. #if defined(CMAKE_BUILD_WITH_CMAKE)
  986. // Internal CMake Fortran module support.
  987. else if (args[1] == "cmake_copy_f90_mod" && args.size() >= 4)
  988. {
  989. return cmDependsFortran::CopyModule(args)? 0 : 1;
  990. }
  991. #endif
  992. #if defined(_WIN32) && !defined(__CYGWIN__)
  993. // Write registry value
  994. else if (args[1] == "write_regv" && args.size() > 3)
  995. {
  996. return cmSystemTools::WriteRegistryValue(args[2].c_str(),
  997. args[3].c_str()) ? 0 : 1;
  998. }
  999. // Delete registry value
  1000. else if (args[1] == "delete_regv" && args.size() > 2)
  1001. {
  1002. return cmSystemTools::DeleteRegistryValue(args[2].c_str()) ? 0 : 1;
  1003. }
  1004. // Remove file
  1005. else if (args[1] == "comspec" && args.size() > 2)
  1006. {
  1007. unsigned int cc;
  1008. std::string command = args[2];
  1009. for ( cc = 3; cc < args.size(); cc ++ )
  1010. {
  1011. command += " " + args[cc];
  1012. }
  1013. return cmWin32ProcessExecution::Windows9xHack(command.c_str());
  1014. }
  1015. #endif
  1016. }
  1017. ::CMakeCommandUsage(args[0].c_str());
  1018. return 1;
  1019. }
  1020. void cmake::GetRegisteredGenerators(std::vector<std::string>& names)
  1021. {
  1022. for(RegisteredGeneratorsMap::const_iterator i = this->Generators.begin();
  1023. i != this->Generators.end(); ++i)
  1024. {
  1025. names.push_back(i->first);
  1026. }
  1027. }
  1028. cmGlobalGenerator* cmake::CreateGlobalGenerator(const char* name)
  1029. {
  1030. RegisteredGeneratorsMap::const_iterator i = this->Generators.find(name);
  1031. if(i != this->Generators.end())
  1032. {
  1033. cmGlobalGenerator* generator = (i->second)();
  1034. generator->SetCMakeInstance(this);
  1035. return generator;
  1036. }
  1037. else
  1038. {
  1039. return 0;
  1040. }
  1041. }
  1042. void cmake::SetHomeDirectory(const char* dir)
  1043. {
  1044. this->cmHomeDirectory = dir;
  1045. cmSystemTools::ConvertToUnixSlashes(this->cmHomeDirectory);
  1046. }
  1047. void cmake::SetHomeOutputDirectory(const char* lib)
  1048. {
  1049. this->HomeOutputDirectory = lib;
  1050. cmSystemTools::ConvertToUnixSlashes(this->HomeOutputDirectory);
  1051. }
  1052. void cmake::SetGlobalGenerator(cmGlobalGenerator *gg)
  1053. {
  1054. // delete the old generator
  1055. if (this->GlobalGenerator)
  1056. {
  1057. delete this->GlobalGenerator;
  1058. // restore the original environment variables CXX and CC
  1059. // Restor CC
  1060. std::string env = "CC=";
  1061. if(this->CCEnvironment.size())
  1062. {
  1063. env += this->CCEnvironment;
  1064. }
  1065. cmSystemTools::PutEnv(env.c_str());
  1066. env = "CXX=";
  1067. if(this->CXXEnvironment.size())
  1068. {
  1069. env += this->CXXEnvironment;
  1070. }
  1071. cmSystemTools::PutEnv(env.c_str());
  1072. }
  1073. // set the new
  1074. this->GlobalGenerator = gg;
  1075. // set the global flag for unix style paths on cmSystemTools as
  1076. // soon as the generator is set. This allows gmake to be used
  1077. // on windows.
  1078. cmSystemTools::SetForceUnixPaths(this->GlobalGenerator->GetForceUnixPaths());
  1079. // Save the environment variables CXX and CC
  1080. const char* cxx = getenv("CXX");
  1081. const char* cc = getenv("CC");
  1082. if(cxx)
  1083. {
  1084. this->CXXEnvironment = cxx;
  1085. }
  1086. else
  1087. {
  1088. this->CXXEnvironment = "";
  1089. }
  1090. if(cc)
  1091. {
  1092. this->CCEnvironment = cc;
  1093. }
  1094. else
  1095. {
  1096. this->CCEnvironment = "";
  1097. }
  1098. // set the cmake instance just to be sure
  1099. gg->SetCMakeInstance(this);
  1100. }
  1101. int cmake::DoPreConfigureChecks()
  1102. {
  1103. // Make sure the Start directory contains a CMakeLists.txt file.
  1104. std::string srcList = this->GetHomeDirectory();
  1105. srcList += "/CMakeLists.txt";
  1106. if(!cmSystemTools::FileExists(srcList.c_str()))
  1107. {
  1108. cmOStringStream err;
  1109. if(cmSystemTools::FileIsDirectory(this->GetHomeDirectory()))
  1110. {
  1111. err << "The source directory \"" << this->GetHomeDirectory()
  1112. << "\" does not appear to contain CMakeLists.txt.\n";
  1113. }
  1114. else if(cmSystemTools::FileExists(this->GetHomeDirectory()))
  1115. {
  1116. err << "The source directory \"" << this->GetHomeDirectory()
  1117. << "\" is a file, not a directory.\n";
  1118. }
  1119. else
  1120. {
  1121. err << "The source directory \"" << this->GetHomeDirectory()
  1122. << "\" does not exist.\n";
  1123. }
  1124. err << "Specify --help for usage, or press the help button on the CMake "
  1125. "GUI.";
  1126. cmSystemTools::Error(err.str().c_str());
  1127. return -2;
  1128. }
  1129. // do a sanity check on some values
  1130. if(this->CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY"))
  1131. {
  1132. std::string cacheStart =
  1133. this->CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY");
  1134. cacheStart += "/CMakeLists.txt";
  1135. std::string currentStart = this->GetHomeDirectory();
  1136. currentStart += "/CMakeLists.txt";
  1137. if(!cmSystemTools::SameFile(cacheStart.c_str(), currentStart.c_str()))
  1138. {
  1139. std::string message = "The source \"";
  1140. message += currentStart;
  1141. message += "\" does not match the source \"";
  1142. message += cacheStart;
  1143. message += "\" used to generate cache. ";
  1144. message += "Re-run cmake with a different source directory.";
  1145. cmSystemTools::Error(message.c_str());
  1146. return -2;
  1147. }
  1148. }
  1149. else
  1150. {
  1151. return 0;
  1152. }
  1153. return 1;
  1154. }
  1155. int cmake::Configure()
  1156. {
  1157. // Construct right now our path conversion table before it's too late:
  1158. this->UpdateConversionPathTable();
  1159. this->CleanupCommandsAndMacros();
  1160. int res = 0;
  1161. if ( !this->ScriptMode )
  1162. {
  1163. res = this->DoPreConfigureChecks();
  1164. }
  1165. if ( res < 0 )
  1166. {
  1167. return -2;
  1168. }
  1169. if ( !res )
  1170. {
  1171. this->CacheManager->AddCacheEntry
  1172. ("CMAKE_HOME_DIRECTORY",
  1173. this->GetHomeDirectory(),
  1174. "Start directory with the top level CMakeLists.txt file for this "
  1175. "project",
  1176. cmCacheManager::INTERNAL);
  1177. }
  1178. // set the default BACKWARDS compatibility to the current version
  1179. if(!this->CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1180. {
  1181. char ver[256];
  1182. sprintf(ver,"%i.%i",cmMakefile::GetMajorVersion(),
  1183. cmMakefile::GetMinorVersion());
  1184. this->CacheManager->AddCacheEntry
  1185. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  1186. "For backwards compatibility, what version of CMake commands and "
  1187. "syntax should this version of CMake allow.",
  1188. cmCacheManager::STRING);
  1189. }
  1190. // no generator specified on the command line
  1191. if(!this->GlobalGenerator)
  1192. {
  1193. const char* genName = this->CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1194. if(genName)
  1195. {
  1196. this->GlobalGenerator = this->CreateGlobalGenerator(genName);
  1197. }
  1198. if(this->GlobalGenerator)
  1199. {
  1200. // set the global flag for unix style paths on cmSystemTools as
  1201. // soon as the generator is set. This allows gmake to be used
  1202. // on windows.
  1203. cmSystemTools::SetForceUnixPaths
  1204. (this->GlobalGenerator->GetForceUnixPaths());
  1205. }
  1206. else
  1207. {
  1208. #if defined(__BORLANDC__) && defined(_WIN32)
  1209. this->SetGlobalGenerator(new cmGlobalBorlandMakefileGenerator);
  1210. #elif defined(_WIN32) && !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW)
  1211. std::string installedCompiler;
  1212. std::string mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1213. "\\VisualStudio\\8.0\\Setup;Dbghelp_path]";
  1214. cmSystemTools::ExpandRegistryValues(mp);
  1215. if (!(mp == "/registry"))
  1216. {
  1217. installedCompiler = "Visual Studio 8 2005";
  1218. }
  1219. else
  1220. {
  1221. mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1222. "\\VisualStudio\\7.1;InstallDir]";
  1223. cmSystemTools::ExpandRegistryValues(mp);
  1224. if (!(mp == "/registry"))
  1225. {
  1226. installedCompiler = "Visual Studio 7 .NET 2003";
  1227. }
  1228. else
  1229. {
  1230. mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1231. "\\VisualStudio\\7.0;InstallDir]";
  1232. cmSystemTools::ExpandRegistryValues(mp);
  1233. if (!(mp == "/registry"))
  1234. {
  1235. installedCompiler = "Visual Studio 7";
  1236. }
  1237. else
  1238. {
  1239. installedCompiler = "Visual Studio 6";
  1240. }
  1241. }
  1242. }
  1243. cmGlobalGenerator* gen
  1244. = this->CreateGlobalGenerator(installedCompiler.c_str());
  1245. if(!gen)
  1246. {
  1247. gen = new cmGlobalNMakeMakefileGenerator;
  1248. }
  1249. this->SetGlobalGenerator(gen);
  1250. #else
  1251. this->SetGlobalGenerator(new cmGlobalUnixMakefileGenerator3);
  1252. #endif
  1253. }
  1254. if(!this->GlobalGenerator)
  1255. {
  1256. cmSystemTools::Error("Could not create generator");
  1257. return -1;
  1258. }
  1259. }
  1260. const char* genName = this->CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1261. if(genName)
  1262. {
  1263. if(strcmp(this->GlobalGenerator->GetName(), genName) != 0)
  1264. {
  1265. std::string message = "Error: generator : ";
  1266. message += this->GlobalGenerator->GetName();
  1267. message += "\nDoes not match the generator used previously: ";
  1268. message += genName;
  1269. message +=
  1270. "\nEither remove the CMakeCache.txt file or choose a different"
  1271. " binary directory.";
  1272. cmSystemTools::Error(message.c_str());
  1273. return -2;
  1274. }
  1275. }
  1276. if(!this->CacheManager->GetCacheValue("CMAKE_GENERATOR"))
  1277. {
  1278. this->CacheManager->AddCacheEntry("CMAKE_GENERATOR",
  1279. this->GlobalGenerator->GetName(),
  1280. "Name of generator.",
  1281. cmCacheManager::INTERNAL);
  1282. }
  1283. // reset any system configuration information, except for when we are
  1284. // InTryCompile. With TryCompile the system info is taken from the parent's
  1285. // info to save time
  1286. if (!this->InTryCompile)
  1287. {
  1288. this->GlobalGenerator->ClearEnabledLanguages();
  1289. }
  1290. this->CleanupWrittenFiles();
  1291. // Truncate log files
  1292. if (!this->InTryCompile)
  1293. {
  1294. this->TruncateOutputLog("CMakeOutput.log");
  1295. this->TruncateOutputLog("CMakeError.log");
  1296. }
  1297. // actually do the configure
  1298. this->GlobalGenerator->Configure();
  1299. // Before saving the cache
  1300. // if the project did not define one of the entries below, add them now
  1301. // so users can edit the values in the cache:
  1302. // LIBRARY_OUTPUT_PATH
  1303. // EXECUTABLE_OUTPUT_PATH
  1304. if(!this->CacheManager->GetCacheValue("LIBRARY_OUTPUT_PATH"))
  1305. {
  1306. this->CacheManager->AddCacheEntry
  1307. ("LIBRARY_OUTPUT_PATH", "",
  1308. "Single output directory for building all libraries.",
  1309. cmCacheManager::PATH);
  1310. }
  1311. if(!this->CacheManager->GetCacheValue("EXECUTABLE_OUTPUT_PATH"))
  1312. {
  1313. this->CacheManager->AddCacheEntry
  1314. ("EXECUTABLE_OUTPUT_PATH", "",
  1315. "Single output directory for building all executables.",
  1316. cmCacheManager::PATH);
  1317. }
  1318. if(!this->CacheManager->GetCacheValue("CMAKE_USE_RELATIVE_PATHS"))
  1319. {
  1320. this->CacheManager->AddCacheEntry
  1321. ("CMAKE_USE_RELATIVE_PATHS", false,
  1322. "If true, cmake will use relative paths in makefiles and projects.");
  1323. cmCacheManager::CacheIterator it =
  1324. this->CacheManager->GetCacheIterator("CMAKE_USE_RELATIVE_PATHS");
  1325. if ( !it.PropertyExists("ADVANCED") )
  1326. {
  1327. it.SetProperty("ADVANCED", "1");
  1328. }
  1329. }
  1330. if(cmSystemTools::GetFatalErrorOccured() &&
  1331. (!this->CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM") ||
  1332. cmSystemTools::IsOff(this->CacheManager->
  1333. GetCacheValue("CMAKE_MAKE_PROGRAM"))))
  1334. {
  1335. // We must have a bad generator selection. Wipe the cache entry so the
  1336. // user can select another.
  1337. this->CacheManager->RemoveCacheEntry("CMAKE_GENERATOR");
  1338. }
  1339. // only save the cache if there were no fatal errors
  1340. if ( !this->ScriptMode && !cmSystemTools::GetFatalErrorOccured() )
  1341. {
  1342. this->CacheManager->SaveCache(this->GetHomeOutputDirectory());
  1343. }
  1344. if ( !this->GraphVizFile.empty() )
  1345. {
  1346. std::cout << "Generate graphviz: " << this->GraphVizFile << std::endl;
  1347. this->GenerateGraphViz(this->GraphVizFile.c_str());
  1348. }
  1349. if(cmSystemTools::GetErrorOccuredFlag())
  1350. {
  1351. return -1;
  1352. }
  1353. return 0;
  1354. }
  1355. bool cmake::CacheVersionMatches()
  1356. {
  1357. const char* majv =
  1358. this->CacheManager->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION");
  1359. const char* minv =
  1360. this->CacheManager->GetCacheValue("CMAKE_CACHE_MINOR_VERSION");
  1361. const char* relv =
  1362. this->CacheManager->GetCacheValue("CMAKE_CACHE_RELEASE_VERSION");
  1363. bool cacheSameCMake = false;
  1364. if(majv &&
  1365. atoi(majv) == static_cast<int>(cmMakefile::GetMajorVersion())
  1366. && minv &&
  1367. atoi(minv) == static_cast<int>(cmMakefile::GetMinorVersion())
  1368. && relv && (strcmp(relv, cmMakefile::GetReleaseVersion()) == 0))
  1369. {
  1370. cacheSameCMake = true;
  1371. }
  1372. return cacheSameCMake;
  1373. }
  1374. void cmake::PreLoadCMakeFiles()
  1375. {
  1376. std::string pre_load = this->GetHomeDirectory();
  1377. if ( pre_load.size() > 0 )
  1378. {
  1379. pre_load += "/PreLoad.cmake";
  1380. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1381. {
  1382. this->ReadListFile(pre_load.c_str());
  1383. }
  1384. }
  1385. pre_load = this->GetHomeOutputDirectory();
  1386. if ( pre_load.size() > 0 )
  1387. {
  1388. pre_load += "/PreLoad.cmake";
  1389. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1390. {
  1391. this->ReadListFile(pre_load.c_str());
  1392. }
  1393. }
  1394. }
  1395. // handle a command line invocation
  1396. int cmake::Run(const std::vector<std::string>& args, bool noconfigure)
  1397. {
  1398. // Process the arguments
  1399. this->SetArgs(args);
  1400. if(cmSystemTools::GetErrorOccuredFlag())
  1401. {
  1402. CMakeCommandUsage(args[0].c_str());
  1403. return -1;
  1404. }
  1405. // set the cmake command
  1406. this->CMakeCommand = args[0];
  1407. if ( !this->ScriptMode )
  1408. {
  1409. // load the cache
  1410. if(this->LoadCache() < 0)
  1411. {
  1412. cmSystemTools::Error("Error executing cmake::LoadCache(). Aborting.\n");
  1413. return -1;
  1414. }
  1415. }
  1416. else
  1417. {
  1418. this->AddCMakePaths(this->CMakeCommand.c_str());
  1419. }
  1420. // Add any cache args
  1421. if ( !this->SetCacheArgs(args) )
  1422. {
  1423. cmSystemTools::Error("Problem processing arguments. Aborting.\n");
  1424. return -1;
  1425. }
  1426. // In script mode we terminate after running the script.
  1427. if(this->ScriptMode)
  1428. {
  1429. if(cmSystemTools::GetErrorOccuredFlag())
  1430. {
  1431. return -1;
  1432. }
  1433. else
  1434. {
  1435. return 0;
  1436. }
  1437. }
  1438. this->PreLoadCMakeFiles();
  1439. std::string systemFile = this->GetHomeOutputDirectory();
  1440. systemFile += "/CMakeSystem.cmake";
  1441. if ( noconfigure )
  1442. {
  1443. return 0;
  1444. }
  1445. // now run the global generate
  1446. // Check the state of the build system to see if we need to regenerate.
  1447. if(!this->CheckBuildSystem())
  1448. {
  1449. return 0;
  1450. }
  1451. // If we are doing global generate, we better set start and start
  1452. // output directory to the root of the project.
  1453. std::string oldstartdir = this->GetStartDirectory();
  1454. std::string oldstartoutputdir = this->GetStartOutputDirectory();
  1455. this->SetStartDirectory(this->GetHomeDirectory());
  1456. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  1457. int ret = this->Configure();
  1458. if (ret || this->ScriptMode)
  1459. {
  1460. return ret;
  1461. }
  1462. ret = this->Generate();
  1463. std::string message = "Build files have been written to: ";
  1464. message += this->GetHomeOutputDirectory();
  1465. this->UpdateProgress(message.c_str(), -1);
  1466. if(ret)
  1467. {
  1468. return ret;
  1469. }
  1470. this->SetStartDirectory(oldstartdir.c_str());
  1471. this->SetStartOutputDirectory(oldstartoutputdir.c_str());
  1472. return ret;
  1473. }
  1474. int cmake::Generate()
  1475. {
  1476. if(!this->GlobalGenerator)
  1477. {
  1478. return -1;
  1479. }
  1480. this->GlobalGenerator->Generate();
  1481. if(cmSystemTools::GetErrorOccuredFlag())
  1482. {
  1483. return -1;
  1484. }
  1485. return 0;
  1486. }
  1487. unsigned int cmake::GetMajorVersion()
  1488. {
  1489. return cmMakefile::GetMajorVersion();
  1490. }
  1491. unsigned int cmake::GetMinorVersion()
  1492. {
  1493. return cmMakefile::GetMinorVersion();
  1494. }
  1495. const char *cmake::GetReleaseVersion()
  1496. {
  1497. return cmMakefile::GetReleaseVersion();
  1498. }
  1499. void cmake::AddCacheEntry(const char* key, const char* value,
  1500. const char* helpString,
  1501. int type)
  1502. {
  1503. this->CacheManager->AddCacheEntry(key, value,
  1504. helpString,
  1505. cmCacheManager::CacheEntryType(type));
  1506. }
  1507. const char* cmake::GetCacheDefinition(const char* name) const
  1508. {
  1509. return this->CacheManager->GetCacheValue(name);
  1510. }
  1511. int cmake::DumpDocumentationToFile(std::ostream& f)
  1512. {
  1513. #ifdef CMAKE_BUILD_WITH_CMAKE
  1514. // Loop over all registered commands and print out documentation
  1515. const char *name;
  1516. const char *terse;
  1517. const char *full;
  1518. char tmp[1024];
  1519. sprintf(tmp,"Version %d.%d (%s)", cmake::GetMajorVersion(),
  1520. cmake::GetMinorVersion(), cmVersion::GetReleaseVersion().c_str());
  1521. f << "<html>\n";
  1522. f << "<h1>Documentation for commands of CMake " << tmp << "</h1>\n";
  1523. f << "<ul>\n";
  1524. for(RegisteredCommandsMap::iterator j = this->Commands.begin();
  1525. j != this->Commands.end(); ++j)
  1526. {
  1527. name = (*j).second->GetName();
  1528. terse = (*j).second->GetTerseDocumentation();
  1529. full = (*j).second->GetFullDocumentation();
  1530. f << "<li><b>" << name << "</b> - " << terse << std::endl
  1531. << "<br><i>Usage:</i> " << full << "</li>" << std::endl << std::endl;
  1532. }
  1533. f << "</ul></html>\n";
  1534. #else
  1535. (void)f;
  1536. #endif
  1537. return 1;
  1538. }
  1539. void cmake::AddDefaultCommands()
  1540. {
  1541. std::list<cmCommand*> commands;
  1542. GetBootstrapCommands(commands);
  1543. GetPredefinedCommands(commands);
  1544. for(std::list<cmCommand*>::iterator i = commands.begin();
  1545. i != commands.end(); ++i)
  1546. {
  1547. this->AddCommand(*i);
  1548. }
  1549. }
  1550. void cmake::AddDefaultGenerators()
  1551. {
  1552. #if defined(_WIN32) && !defined(__CYGWIN__)
  1553. # if !defined(CMAKE_BOOT_MINGW)
  1554. this->Generators[cmGlobalVisualStudio6Generator::GetActualName()] =
  1555. &cmGlobalVisualStudio6Generator::New;
  1556. this->Generators[cmGlobalVisualStudio7Generator::GetActualName()] =
  1557. &cmGlobalVisualStudio7Generator::New;
  1558. this->Generators[cmGlobalVisualStudio71Generator::GetActualName()] =
  1559. &cmGlobalVisualStudio71Generator::New;
  1560. this->Generators[cmGlobalVisualStudio8Generator::GetActualName()] =
  1561. &cmGlobalVisualStudio8Generator::New;
  1562. this->Generators[cmGlobalVisualStudio8Win64Generator::GetActualName()] =
  1563. &cmGlobalVisualStudio8Win64Generator::New;
  1564. this->Generators[cmGlobalBorlandMakefileGenerator::GetActualName()] =
  1565. &cmGlobalBorlandMakefileGenerator::New;
  1566. this->Generators[cmGlobalNMakeMakefileGenerator::GetActualName()] =
  1567. &cmGlobalNMakeMakefileGenerator::New;
  1568. this->Generators[cmGlobalWatcomWMakeGenerator::GetActualName()] =
  1569. &cmGlobalWatcomWMakeGenerator::New;
  1570. # endif
  1571. this->Generators[cmGlobalMSYSMakefileGenerator::GetActualName()] =
  1572. &cmGlobalMSYSMakefileGenerator::New;
  1573. this->Generators[cmGlobalMinGWMakefileGenerator::GetActualName()] =
  1574. &cmGlobalMinGWMakefileGenerator::New;
  1575. #endif
  1576. this->Generators[cmGlobalUnixMakefileGenerator3::GetActualName()] =
  1577. &cmGlobalUnixMakefileGenerator3::New;
  1578. #ifdef CMAKE_USE_XCODE
  1579. this->Generators[cmGlobalXCodeGenerator::GetActualName()] =
  1580. &cmGlobalXCodeGenerator::New;
  1581. #endif
  1582. #ifdef CMAKE_USE_KDEVELOP
  1583. this->Generators[cmGlobalKdevelopGenerator::GetActualName()] =
  1584. &cmGlobalKdevelopGenerator::New;
  1585. #endif
  1586. }
  1587. int cmake::LoadCache()
  1588. {
  1589. // could we not read the cache
  1590. if (!this->CacheManager->LoadCache(this->GetHomeOutputDirectory()))
  1591. {
  1592. // if it does exist, but isn;t readable then warn the user
  1593. std::string cacheFile = this->GetHomeOutputDirectory();
  1594. cacheFile += "/CMakeCache.txt";
  1595. if(cmSystemTools::FileExists(cacheFile.c_str()))
  1596. {
  1597. cmSystemTools::Error(
  1598. "There is a CMakeCache.txt file for the current binary tree but "
  1599. "cmake does not have permission to read it. Please check the "
  1600. "permissions of the directory you are trying to run CMake on.");
  1601. return -1;
  1602. }
  1603. }
  1604. if (this->CMakeCommand.size() < 2)
  1605. {
  1606. cmSystemTools::Error(
  1607. "cmake command was not specified prior to loading the cache in "
  1608. "cmake.cxx");
  1609. return -1;
  1610. }
  1611. // setup CMAKE_ROOT and CMAKE_COMMAND
  1612. if(!this->AddCMakePaths(this->CMakeCommand.c_str()))
  1613. {
  1614. return -3;
  1615. }
  1616. // set the default BACKWARDS compatibility to the current version
  1617. if(!this->CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1618. {
  1619. char ver[256];
  1620. sprintf(ver,"%i.%i",cmMakefile::GetMajorVersion(),
  1621. cmMakefile::GetMinorVersion());
  1622. this->CacheManager->AddCacheEntry
  1623. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  1624. "For backwards compatibility, what version of CMake commands and "
  1625. "syntax should this version of CMake allow.",
  1626. cmCacheManager::STRING);
  1627. }
  1628. return 0;
  1629. }
  1630. void cmake::SetProgressCallback(ProgressCallbackType f, void *cd)
  1631. {
  1632. this->ProgressCallback = f;
  1633. this->ProgressCallbackClientData = cd;
  1634. }
  1635. void cmake::UpdateProgress(const char *msg, float prog)
  1636. {
  1637. if(this->ProgressCallback && !this->InTryCompile)
  1638. {
  1639. (*this->ProgressCallback)(msg, prog, this->ProgressCallbackClientData);
  1640. return;
  1641. }
  1642. }
  1643. void cmake::GetCommandDocumentation(
  1644. std::vector<cmDocumentationEntry>& v) const
  1645. {
  1646. for(RegisteredCommandsMap::const_iterator j = this->Commands.begin();
  1647. j != this->Commands.end(); ++j)
  1648. {
  1649. cmDocumentationEntry e =
  1650. {
  1651. (*j).second->GetName(),
  1652. (*j).second->GetTerseDocumentation(),
  1653. (*j).second->GetFullDocumentation()
  1654. };
  1655. v.push_back(e);
  1656. }
  1657. cmDocumentationEntry empty = {0,0,0};
  1658. v.push_back(empty);
  1659. }
  1660. void cmake::GetGeneratorDocumentation(std::vector<cmDocumentationEntry>& v)
  1661. {
  1662. for(RegisteredGeneratorsMap::const_iterator i = this->Generators.begin();
  1663. i != this->Generators.end(); ++i)
  1664. {
  1665. cmDocumentationEntry e;
  1666. cmGlobalGenerator* generator = (i->second)();
  1667. generator->GetDocumentation(e);
  1668. delete generator;
  1669. v.push_back(e);
  1670. }
  1671. cmDocumentationEntry empty = {0,0,0};
  1672. v.push_back(empty);
  1673. }
  1674. void cmake::AddWrittenFile(const char* file)
  1675. {
  1676. this->WrittenFiles.insert(file);
  1677. }
  1678. bool cmake::HasWrittenFile(const char* file)
  1679. {
  1680. return this->WrittenFiles.find(file) != this->WrittenFiles.end();
  1681. }
  1682. void cmake::CleanupWrittenFiles()
  1683. {
  1684. this->WrittenFiles.clear();
  1685. }
  1686. void cmake::UpdateConversionPathTable()
  1687. {
  1688. // Update the path conversion table with any specified file:
  1689. const char* tablepath =
  1690. this->CacheManager->GetCacheValue("CMAKE_PATH_TRANSLATION_FILE");
  1691. if(tablepath)
  1692. {
  1693. std::ifstream table( tablepath );
  1694. if(!table)
  1695. {
  1696. cmSystemTools::Error("CMAKE_PATH_TRANSLATION_FILE set to ", tablepath,
  1697. ". CMake can not open file.");
  1698. cmSystemTools::ReportLastSystemError("CMake can not open file.");
  1699. }
  1700. else
  1701. {
  1702. std::string a, b;
  1703. while(!table.eof())
  1704. {
  1705. // two entries per line
  1706. table >> a; table >> b;
  1707. cmSystemTools::AddTranslationPath( a.c_str(), b.c_str());
  1708. }
  1709. }
  1710. }
  1711. }
  1712. //----------------------------------------------------------------------------
  1713. int cmake::CheckBuildSystem()
  1714. {
  1715. // This method will check the integrity of the build system if the
  1716. // option was given on the command line. It reads the given file to
  1717. // determine whether CMake should rerun. If it does rerun then the
  1718. // generation step will check the integrity of dependencies. If it
  1719. // does not then we need to check the integrity here.
  1720. // If no file is provided for the check, we have to rerun.
  1721. if(this->CheckBuildSystemArgument.size() == 0)
  1722. {
  1723. return 1;
  1724. }
  1725. // If the file provided does not exist, we have to rerun.
  1726. if(!cmSystemTools::FileExists(this->CheckBuildSystemArgument.c_str()))
  1727. {
  1728. return 1;
  1729. }
  1730. // Read the rerun check file and use it to decide whether to do the
  1731. // global generate.
  1732. cmake cm;
  1733. cmGlobalGenerator gg;
  1734. gg.SetCMakeInstance(&cm);
  1735. std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  1736. lg->SetGlobalGenerator(&gg);
  1737. cmMakefile* mf = lg->GetMakefile();
  1738. if(!mf->ReadListFile(0, this->CheckBuildSystemArgument.c_str()) ||
  1739. cmSystemTools::GetErrorOccuredFlag())
  1740. {
  1741. // There was an error reading the file. Just rerun.
  1742. return 1;
  1743. }
  1744. // Get the set of dependencies and outputs.
  1745. const char* dependsStr = mf->GetDefinition("CMAKE_MAKEFILE_DEPENDS");
  1746. const char* outputsStr = mf->GetDefinition("CMAKE_MAKEFILE_OUTPUTS");
  1747. if(!dependsStr || !outputsStr)
  1748. {
  1749. // Not enough information was provided to do the test. Just rerun.
  1750. return 1;
  1751. }
  1752. std::vector<std::string> depends;
  1753. std::vector<std::string> outputs;
  1754. cmSystemTools::ExpandListArgument(dependsStr, depends);
  1755. cmSystemTools::ExpandListArgument(outputsStr, outputs);
  1756. // If any output is older than any dependency then rerun.
  1757. for(std::vector<std::string>::iterator dep = depends.begin();
  1758. dep != depends.end(); ++dep)
  1759. {
  1760. for(std::vector<std::string>::iterator out = outputs.begin();
  1761. out != outputs.end(); ++out)
  1762. {
  1763. int result = 0;
  1764. if(!this->FileComparison->FileTimeCompare(out->c_str(),
  1765. dep->c_str(), &result) ||
  1766. result < 0)
  1767. {
  1768. return 1;
  1769. }
  1770. }
  1771. }
  1772. // We do not need to rerun CMake. Check dependency integrity. Use
  1773. // the make system's VERBOSE environment variable to enable verbose
  1774. // output.
  1775. bool verbose = cmSystemTools::GetEnv("VERBOSE") != 0;
  1776. // compute depends based on the generator specified
  1777. const char* genName = mf->GetDefinition("CMAKE_DEPENDS_GENERATOR");
  1778. if (!genName || genName[0] == '\0')
  1779. {
  1780. genName = "Unix Makefiles";
  1781. }
  1782. cmGlobalGenerator *ggd = this->CreateGlobalGenerator(genName);
  1783. if (ggd)
  1784. {
  1785. // Check the dependencies in case source files were removed.
  1786. std::auto_ptr<cmLocalGenerator> lgd(ggd->CreateLocalGenerator());
  1787. lgd->SetGlobalGenerator(ggd);
  1788. lgd->CheckDependencies(mf, verbose, this->ClearBuildSystem);
  1789. // Check for multiple output pairs.
  1790. ggd->CheckMultipleOutputs(mf, verbose);
  1791. }
  1792. // No need to rerun.
  1793. return 0;
  1794. }
  1795. //----------------------------------------------------------------------------
  1796. void cmake::TruncateOutputLog(const char* fname)
  1797. {
  1798. std::string fullPath = this->GetHomeOutputDirectory();
  1799. fullPath += "/";
  1800. fullPath += fname;
  1801. struct stat st;
  1802. if ( ::stat(fullPath.c_str(), &st) )
  1803. {
  1804. return;
  1805. }
  1806. if ( !this->CacheManager->GetCacheValue("CMAKE_CACHEFILE_DIR") )
  1807. {
  1808. cmSystemTools::RemoveFile(fullPath.c_str());
  1809. return;
  1810. }
  1811. size_t fsize = st.st_size;
  1812. const size_t maxFileSize = 50 * 1024;
  1813. if ( fsize < maxFileSize )
  1814. {
  1815. //TODO: truncate file
  1816. return;
  1817. }
  1818. }
  1819. inline std::string removeQuotes(const std::string& s)
  1820. {
  1821. if(s[0] == '\"' && s[s.size()-1] == '\"')
  1822. {
  1823. return s.substr(1, s.size()-2);
  1824. }
  1825. return s;
  1826. }
  1827. const char* cmake::GetCTestCommand()
  1828. {
  1829. if ( !this->CTestCommand.empty() )
  1830. {
  1831. return this->CTestCommand.c_str();
  1832. }
  1833. cmMakefile* mf
  1834. = this->GetGlobalGenerator()->GetLocalGenerator(0)->GetMakefile();
  1835. #ifdef CMAKE_BUILD_WITH_CMAKE
  1836. this->CTestCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1837. this->CTestCommand = removeQuotes(this->CTestCommand);
  1838. this->CTestCommand = cmSystemTools::GetFilenamePath(this->CTestCommand.c_str());
  1839. this->CTestCommand += "/";
  1840. this->CTestCommand += "ctest";
  1841. this->CTestCommand += cmSystemTools::GetExecutableExtension();
  1842. if(!cmSystemTools::FileExists(this->CTestCommand.c_str()))
  1843. {
  1844. this->CTestCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1845. this->CTestCommand = cmSystemTools::GetFilenamePath(this->CTestCommand.c_str());
  1846. this->CTestCommand += "/Debug/";
  1847. this->CTestCommand += "ctest";
  1848. this->CTestCommand += cmSystemTools::GetExecutableExtension();
  1849. }
  1850. if(!cmSystemTools::FileExists(this->CTestCommand.c_str()))
  1851. {
  1852. this->CTestCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1853. this->CTestCommand = cmSystemTools::GetFilenamePath(this->CTestCommand.c_str());
  1854. this->CTestCommand += "/Release/";
  1855. this->CTestCommand += "ctest";
  1856. this->CTestCommand += cmSystemTools::GetExecutableExtension();
  1857. }
  1858. #else
  1859. // Only for bootstrap
  1860. this->CTestCommand += mf->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  1861. this->CTestCommand += "/ctest";
  1862. this->CTestCommand += cmSystemTools::GetExecutableExtension();
  1863. #endif
  1864. if ( this->CTestCommand.empty() )
  1865. {
  1866. cmSystemTools::Error("Cannot find the CTest executable");
  1867. this->CTestCommand = "CTEST-COMMAND-NOT-FOUND";
  1868. }
  1869. return this->CTestCommand.c_str();
  1870. }
  1871. const char* cmake::GetCPackCommand()
  1872. {
  1873. if ( !this->CPackCommand.empty() )
  1874. {
  1875. return this->CPackCommand.c_str();
  1876. }
  1877. cmMakefile* mf
  1878. = this->GetGlobalGenerator()->GetLocalGenerator(0)->GetMakefile();
  1879. #ifdef CMAKE_BUILD_WITH_CMAKE
  1880. this->CPackCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1881. this->CPackCommand = removeQuotes(this->CPackCommand);
  1882. this->CPackCommand = cmSystemTools::GetFilenamePath(this->CPackCommand.c_str());
  1883. this->CPackCommand += "/";
  1884. this->CPackCommand += "cpack";
  1885. this->CPackCommand += cmSystemTools::GetExecutableExtension();
  1886. if(!cmSystemTools::FileExists(this->CPackCommand.c_str()))
  1887. {
  1888. this->CPackCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1889. this->CPackCommand = cmSystemTools::GetFilenamePath(this->CPackCommand.c_str());
  1890. this->CPackCommand += "/Debug/";
  1891. this->CPackCommand += "cpack";
  1892. this->CPackCommand += cmSystemTools::GetExecutableExtension();
  1893. }
  1894. if(!cmSystemTools::FileExists(this->CPackCommand.c_str()))
  1895. {
  1896. this->CPackCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1897. this->CPackCommand = cmSystemTools::GetFilenamePath(this->CPackCommand.c_str());
  1898. this->CPackCommand += "/Release/";
  1899. this->CPackCommand += "cpack";
  1900. this->CPackCommand += cmSystemTools::GetExecutableExtension();
  1901. }
  1902. if (!cmSystemTools::FileExists(this->CPackCommand.c_str()))
  1903. {
  1904. cmSystemTools::Error("Cannot find the CPack executable");
  1905. this->CPackCommand = "CPACK-COMMAND-NOT-FOUND";
  1906. }
  1907. #else
  1908. // Only for bootstrap
  1909. this->CPackCommand += mf->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  1910. this->CPackCommand += "/cpack";
  1911. this->CPackCommand += cmSystemTools::GetExecutableExtension();
  1912. #endif
  1913. return this->CPackCommand.c_str();
  1914. }
  1915. void cmake::GenerateGraphViz(const char* fileName)
  1916. {
  1917. cmGeneratedFileStream str(fileName);
  1918. if ( !str )
  1919. {
  1920. return;
  1921. }
  1922. cmake cm;
  1923. cmGlobalGenerator ggi;
  1924. ggi.SetCMakeInstance(&cm);
  1925. std::auto_ptr<cmLocalGenerator> lg(ggi.CreateLocalGenerator());
  1926. lg->SetGlobalGenerator(&ggi);
  1927. cmMakefile *mf = lg->GetMakefile();
  1928. std::string infile = this->GetHomeOutputDirectory();
  1929. infile += "/CMakeGraphVizOptions.cmake";
  1930. if ( !cmSystemTools::FileExists(infile.c_str()) )
  1931. {
  1932. infile = this->GetHomeDirectory();
  1933. infile += "/CMakeGraphVizOptions.cmake";
  1934. if ( !cmSystemTools::FileExists(infile.c_str()) )
  1935. {
  1936. infile = "";
  1937. }
  1938. }
  1939. if ( !infile.empty() )
  1940. {
  1941. if ( !mf->ReadListFile(0, infile.c_str()) )
  1942. {
  1943. cmSystemTools::Error("Problem opening GraphViz options file: ",
  1944. infile.c_str());
  1945. return;
  1946. }
  1947. std::cout << "Read GraphViz options file: " << infile.c_str()
  1948. << std::endl;
  1949. }
  1950. #define __set_if_not_set(var, value, cmakeDefinition) \
  1951. const char* var = mf->GetDefinition(cmakeDefinition); \
  1952. if ( !var ) \
  1953. { \
  1954. var = value; \
  1955. }
  1956. __set_if_not_set(graphType, "digraph", "GRAPHVIZ_GRAPH_TYPE");
  1957. __set_if_not_set(graphName, "GG", "GRAPHVIZ_GRAPH_NAME");
  1958. __set_if_not_set(graphHeader, "node [\n fontsize = \"12\"\n];",
  1959. "GRAPHVIZ_GRAPH_HEADER");
  1960. __set_if_not_set(graphNodePrefix, "node", "GRAPHVIZ_NODE_PREFIX");
  1961. const char* ignoreTargets = mf->GetDefinition("GRAPHVIZ_IGNORE_TARGETS");
  1962. std::set<cmStdString> ignoreTargetsSet;
  1963. if ( ignoreTargets )
  1964. {
  1965. std::vector<std::string> ignoreTargetsVector;
  1966. cmSystemTools::ExpandListArgument(ignoreTargets,ignoreTargetsVector);
  1967. std::vector<std::string>::iterator itvIt;
  1968. for ( itvIt = ignoreTargetsVector.begin();
  1969. itvIt != ignoreTargetsVector.end();
  1970. ++ itvIt )
  1971. {
  1972. ignoreTargetsSet.insert(itvIt->c_str());
  1973. }
  1974. }
  1975. str << graphType << " " << graphName << " {" << std::endl;
  1976. str << graphHeader << std::endl;
  1977. cmGlobalGenerator* gg = this->GetGlobalGenerator();
  1978. std::vector<cmLocalGenerator*> localGenerators;
  1979. gg->GetLocalGenerators(localGenerators);
  1980. std::vector<cmLocalGenerator*>::iterator lit;
  1981. // for target deps
  1982. // 1 - cmake target
  1983. // 2 - external target
  1984. // 0 - no deps
  1985. std::map<cmStdString, int> targetDeps;
  1986. std::map<cmStdString, cmTarget*> targetPtrs;
  1987. std::map<cmStdString, cmStdString> targetNamesNodes;
  1988. char tgtName[100];
  1989. int cnt = 0;
  1990. // First pass get the list of all cmake targets
  1991. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  1992. {
  1993. cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  1994. cmTargets::iterator tit;
  1995. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  1996. {
  1997. const char* realTargetName = tit->first.c_str();
  1998. if ( ignoreTargetsSet.find(realTargetName) != ignoreTargetsSet.end() )
  1999. {
  2000. // Skip ignored targets
  2001. continue;
  2002. }
  2003. //std::cout << "Found target: " << tit->first.c_str() << std::endl;
  2004. sprintf(tgtName, "%s%d", graphNodePrefix, cnt++);
  2005. targetNamesNodes[realTargetName] = tgtName;
  2006. targetPtrs[realTargetName] = &tit->second;
  2007. //str << " \"" << tgtName << "\" [ label=\"" << tit->first.c_str()
  2008. //<< "\" shape=\"box\"];" << std::endl;
  2009. }
  2010. }
  2011. // Ok, now find all the stuff we link to that is not in cmake
  2012. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  2013. {
  2014. cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  2015. cmTargets::iterator tit;
  2016. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  2017. {
  2018. const cmTarget::LinkLibraryVectorType* ll
  2019. = &(tit->second.GetOriginalLinkLibraries());
  2020. cmTarget::LinkLibraryVectorType::const_iterator llit;
  2021. const char* realTargetName = tit->first.c_str();
  2022. if ( ignoreTargetsSet.find(realTargetName) != ignoreTargetsSet.end() )
  2023. {
  2024. // Skip ignored targets
  2025. continue;
  2026. }
  2027. if ( ll->size() > 0 )
  2028. {
  2029. targetDeps[realTargetName] = 1;
  2030. }
  2031. for ( llit = ll->begin(); llit != ll->end(); ++ llit )
  2032. {
  2033. const char* libName = llit->first.c_str();
  2034. std::map<cmStdString, cmStdString>::iterator tarIt
  2035. = targetNamesNodes.find(libName);
  2036. if ( ignoreTargetsSet.find(libName) != ignoreTargetsSet.end() )
  2037. {
  2038. // Skip ignored targets
  2039. continue;
  2040. }
  2041. if ( tarIt == targetNamesNodes.end() )
  2042. {
  2043. sprintf(tgtName, "%s%d", graphNodePrefix, cnt++);
  2044. targetDeps[libName] = 2;
  2045. targetNamesNodes[libName] = tgtName;
  2046. //str << " \"" << tgtName << "\" [ label=\"" << libName
  2047. //<< "\" shape=\"ellipse\"];" << std::endl;
  2048. }
  2049. else
  2050. {
  2051. std::map<cmStdString, int>::iterator depIt
  2052. = targetDeps.find(libName);
  2053. if ( depIt == targetDeps.end() )
  2054. {
  2055. targetDeps[libName] = 1;
  2056. }
  2057. }
  2058. }
  2059. }
  2060. }
  2061. // Write out nodes
  2062. std::map<cmStdString, int>::iterator depIt;
  2063. for ( depIt = targetDeps.begin(); depIt != targetDeps.end(); ++ depIt )
  2064. {
  2065. const char* newTargetName = depIt->first.c_str();
  2066. std::map<cmStdString, cmStdString>::iterator tarIt
  2067. = targetNamesNodes.find(newTargetName);
  2068. if ( tarIt == targetNamesNodes.end() )
  2069. {
  2070. // We should not be here.
  2071. std::cout << __LINE__ << " Cannot find library: " << newTargetName
  2072. << " even though it was added in the previous pass" << std::endl;
  2073. abort();
  2074. }
  2075. str << " \"" << tarIt->second.c_str() << "\" [ label=\""
  2076. << newTargetName << "\" shape=\"";
  2077. if ( depIt->second == 1 )
  2078. {
  2079. std::map<cmStdString, cmTarget*>::iterator tarTypeIt= targetPtrs.find(
  2080. newTargetName);
  2081. if ( tarTypeIt == targetPtrs.end() )
  2082. {
  2083. // We should not be here.
  2084. std::cout << __LINE__ << " Cannot find library: " << newTargetName
  2085. << " even though it was added in the previous pass" << std::endl;
  2086. abort();
  2087. }
  2088. cmTarget* tg = tarTypeIt->second;
  2089. switch ( tg->GetType() )
  2090. {
  2091. case cmTarget::EXECUTABLE:
  2092. str << "house";
  2093. break;
  2094. case cmTarget::STATIC_LIBRARY:
  2095. str << "diamond";
  2096. break;
  2097. case cmTarget::SHARED_LIBRARY:
  2098. str << "polygon";
  2099. break;
  2100. case cmTarget::MODULE_LIBRARY:
  2101. str << "octagon";
  2102. break;
  2103. default:
  2104. str << "box";
  2105. }
  2106. }
  2107. else
  2108. {
  2109. str << "ellipse";
  2110. }
  2111. str << "\"];" << std::endl;
  2112. }
  2113. // Now generate the connectivity
  2114. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  2115. {
  2116. cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  2117. cmTargets::iterator tit;
  2118. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  2119. {
  2120. std::map<cmStdString, int>::iterator dependIt
  2121. = targetDeps.find(tit->first.c_str());
  2122. if ( dependIt == targetDeps.end() )
  2123. {
  2124. continue;
  2125. }
  2126. std::map<cmStdString, cmStdString>::iterator cmakeTarIt
  2127. = targetNamesNodes.find(tit->first.c_str());
  2128. const cmTarget::LinkLibraryVectorType* ll
  2129. = &(tit->second.GetOriginalLinkLibraries());
  2130. cmTarget::LinkLibraryVectorType::const_iterator llit;
  2131. for ( llit = ll->begin(); llit != ll->end(); ++ llit )
  2132. {
  2133. const char* libName = llit->first.c_str();
  2134. std::map<cmStdString, cmStdString>::iterator tarIt
  2135. = targetNamesNodes.find(libName);
  2136. if ( tarIt == targetNamesNodes.end() )
  2137. {
  2138. // We should not be here.
  2139. std::cout << __LINE__ << " Cannot find library: " << libName
  2140. << " even though it was added in the previous pass" << std::endl;
  2141. abort();
  2142. }
  2143. str << " \"" << cmakeTarIt->second.c_str() << "\" -> \""
  2144. << tarIt->second.c_str() << "\"" << std::endl;
  2145. }
  2146. }
  2147. }
  2148. // TODO: Use dotted or something for external libraries
  2149. //str << " \"node0\":f4 -> \"node12\"[color=\"#0000ff\" style=dotted]"
  2150. //<< std::endl;
  2151. //
  2152. str << "}" << std::endl;
  2153. }