cmake.cxx 64 KB

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