cmake.cxx 66 KB

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