cmake.cxx 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734
  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. #if defined(CMAKE_BUILD_WITH_CMAKE)
  21. # include "cmDependsFortran.h" // For -E cmake_copy_f90_mod callback.
  22. # include "cmVariableWatch.h"
  23. # include "cmVersion.h"
  24. #endif
  25. // only build kdevelop generator on non-windows platforms
  26. // when not bootstrapping cmake
  27. #if !defined(_WIN32)
  28. # if defined(CMAKE_BUILD_WITH_CMAKE)
  29. # define CMAKE_USE_KDEVELOP
  30. # endif
  31. #endif
  32. // include the generator
  33. #if defined(_WIN32) && !defined(__CYGWIN__)
  34. # include "cmGlobalVisualStudio6Generator.h"
  35. # if !defined(__MINGW32__)
  36. # include "cmGlobalVisualStudio7Generator.h"
  37. # include "cmGlobalVisualStudio71Generator.h"
  38. # include "cmGlobalVisualStudio8Generator.h"
  39. # endif
  40. # include "cmGlobalBorlandMakefileGenerator.h"
  41. # include "cmGlobalNMakeMakefileGenerator.h"
  42. # include "cmWin32ProcessExecution.h"
  43. #else
  44. #endif
  45. #include "cmGlobalUnixMakefileGenerator3.h"
  46. #ifdef CMAKE_USE_KDEVELOP
  47. # include "cmGlobalKdevelopGenerator.h"
  48. #endif
  49. #include <stdlib.h> // required for atoi
  50. #ifdef __APPLE__
  51. # include "cmGlobalXCodeGenerator.h"
  52. # define CMAKE_USE_XCODE 1
  53. # include <sys/types.h>
  54. # include <sys/time.h>
  55. # include <sys/resource.h>
  56. #endif
  57. #include <sys/stat.h> // struct stat
  58. #include <memory> // auto_ptr
  59. void cmNeedBackwardsCompatibility(const std::string& variable,
  60. int access_type, void* )
  61. {
  62. #ifdef CMAKE_BUILD_WITH_CMAKE
  63. if (access_type == cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS)
  64. {
  65. std::string message = "An attempt was made to access a variable: ";
  66. message += variable;
  67. message += " that has not been defined. Some variables were always defined "
  68. "by CMake in versions prior to 1.6. To fix this you might need to set the "
  69. "cache value of CMAKE_BACKWARDS_COMPATIBILITY to 1.4 or less. If you are "
  70. "writing a CMakeList file, (or have already set "
  71. "CMAKE_BACKWARDS_COMPATABILITY to 1.4 or less) then you probably need to "
  72. "include a CMake module to test for the feature this variable defines.";
  73. cmSystemTools::Error(message.c_str());
  74. }
  75. #else
  76. (void)variable;
  77. (void)access_type;
  78. #endif
  79. }
  80. cmake::cmake()
  81. {
  82. m_DebugTryCompile = false;
  83. m_ClearBuildSystem = false;
  84. #ifdef __APPLE__
  85. struct rlimit rlp;
  86. if(!getrlimit(RLIMIT_STACK, &rlp))
  87. {
  88. if(rlp.rlim_cur != rlp.rlim_max)
  89. {
  90. rlp.rlim_cur = rlp.rlim_max;
  91. setrlimit(RLIMIT_STACK, &rlp);
  92. }
  93. }
  94. #endif
  95. // If MAKEFLAGS are given in the environment, remove the environment
  96. // variable. This will prevent try-compile from succeeding when it
  97. // should fail (if "-i" is an option). We cannot simply test
  98. // whether "-i" is given and remove it because some make programs
  99. // encode the MAKEFLAGS variable in a strange way.
  100. if(getenv("MAKEFLAGS"))
  101. {
  102. cmSystemTools::PutEnv("MAKEFLAGS=");
  103. }
  104. m_Verbose = false;
  105. m_InTryCompile = false;
  106. m_CacheManager = new cmCacheManager;
  107. m_GlobalGenerator = 0;
  108. m_ProgressCallback = 0;
  109. m_ProgressCallbackClientData = 0;
  110. m_ScriptMode = false;
  111. #ifdef CMAKE_BUILD_WITH_CMAKE
  112. m_VariableWatch = new cmVariableWatch;
  113. m_VariableWatch->AddWatch("CMAKE_WORDS_BIGENDIAN",
  114. cmNeedBackwardsCompatibility);
  115. m_VariableWatch->AddWatch("CMAKE_SIZEOF_INT",
  116. cmNeedBackwardsCompatibility);
  117. m_VariableWatch->AddWatch("CMAKE_X_LIBS",
  118. cmNeedBackwardsCompatibility);
  119. #endif
  120. this->AddDefaultGenerators();
  121. this->AddDefaultCommands();
  122. }
  123. cmake::~cmake()
  124. {
  125. delete m_CacheManager;
  126. if (m_GlobalGenerator)
  127. {
  128. delete m_GlobalGenerator;
  129. m_GlobalGenerator = 0;
  130. }
  131. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  132. j != m_Commands.end(); ++j)
  133. {
  134. delete (*j).second;
  135. }
  136. #ifdef CMAKE_BUILD_WITH_CMAKE
  137. delete m_VariableWatch;
  138. #endif
  139. }
  140. void cmake::CleanupCommandsAndMacros()
  141. {
  142. std::vector<cmCommand*> commands;
  143. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  144. j != m_Commands.end(); ++j)
  145. {
  146. if ( !j->second->IsA("cmMacroHelperCommand") )
  147. {
  148. commands.push_back(j->second);
  149. }
  150. else
  151. {
  152. delete j->second;
  153. }
  154. }
  155. m_Commands.erase(m_Commands.begin(), m_Commands.end());
  156. std::vector<cmCommand*>::iterator it;
  157. for ( it = commands.begin(); it != commands.end();
  158. ++ it )
  159. {
  160. m_Commands[cmSystemTools::LowerCase((*it)->GetName())] = *it;
  161. }
  162. }
  163. bool cmake::CommandExists(const char* name) const
  164. {
  165. std::string sName = cmSystemTools::LowerCase(name);
  166. return (m_Commands.find(sName) != m_Commands.end());
  167. }
  168. cmCommand *cmake::GetCommand(const char *name)
  169. {
  170. cmCommand* rm = 0;
  171. std::string sName = cmSystemTools::LowerCase(name);
  172. RegisteredCommandsMap::iterator pos = m_Commands.find(sName);
  173. if (pos != m_Commands.end())
  174. {
  175. rm = (*pos).second;
  176. }
  177. return rm;
  178. }
  179. void cmake::RenameCommand(const char*oldName, const char* newName)
  180. {
  181. // if the command already exists, free the old one
  182. std::string sOldName = cmSystemTools::LowerCase(oldName);
  183. std::string sNewName = cmSystemTools::LowerCase(newName);
  184. RegisteredCommandsMap::iterator pos = m_Commands.find(sOldName);
  185. if ( pos == m_Commands.end() )
  186. {
  187. return;
  188. }
  189. cmCommand* cmd = pos->second;
  190. pos = m_Commands.find(sNewName);
  191. if (pos != m_Commands.end())
  192. {
  193. delete pos->second;
  194. m_Commands.erase(pos);
  195. }
  196. m_Commands.insert(RegisteredCommandsMap::value_type(sNewName, cmd));
  197. pos = m_Commands.find(sOldName);
  198. m_Commands.erase(pos);
  199. }
  200. void cmake::AddCommand(cmCommand* wg)
  201. {
  202. std::string name = cmSystemTools::LowerCase(wg->GetName());
  203. // if the command already exists, free the old one
  204. RegisteredCommandsMap::iterator pos = m_Commands.find(name);
  205. if (pos != m_Commands.end())
  206. {
  207. delete pos->second;
  208. m_Commands.erase(pos);
  209. }
  210. m_Commands.insert( RegisteredCommandsMap::value_type(name, wg));
  211. }
  212. // Parse the args
  213. bool cmake::SetCacheArgs(const std::vector<std::string>& args)
  214. {
  215. for(unsigned int i=1; i < args.size(); ++i)
  216. {
  217. std::string arg = args[i];
  218. if(arg.find("-D",0) == 0)
  219. {
  220. std::string entry = arg.substr(2);
  221. std::string var, value;
  222. cmCacheManager::CacheEntryType type = cmCacheManager::UNINITIALIZED;
  223. if(cmCacheManager::ParseEntry(entry.c_str(), var, value, type) ||
  224. cmCacheManager::ParseEntry(entry.c_str(), var, value))
  225. {
  226. this->m_CacheManager->AddCacheEntry(var.c_str(), value.c_str(),
  227. "No help, variable specified on the command line.",
  228. type);
  229. }
  230. else
  231. {
  232. std::cerr << "Parse error in command line argument: " << arg << "\n"
  233. << "Should be: VAR:type=value\n";
  234. cmSystemTools::Error("No cmake scrpt provided.");
  235. return false;
  236. }
  237. }
  238. else if(arg.find("-C",0) == 0)
  239. {
  240. std::string path = arg.substr(2);
  241. if ( path.size() == 0 )
  242. {
  243. cmSystemTools::Error("No initial cache file provided.");
  244. return false;
  245. }
  246. std::cerr << "loading initial cache file " << path.c_str() << "\n";
  247. this->ReadListFile(path.c_str());
  248. }
  249. else if(arg.find("-P",0) == 0)
  250. {
  251. i++;
  252. std::string path = args[i];
  253. if ( path.size() == 0 )
  254. {
  255. cmSystemTools::Error("No cmake scrpt provided.");
  256. return false;
  257. }
  258. std::cerr << "Running cmake script file " << path.c_str() << "\n";
  259. this->ReadListFile(path.c_str());
  260. }
  261. }
  262. return true;
  263. }
  264. void cmake::ReadListFile(const char *path)
  265. {
  266. // if a generator was not yet created, temporarily create one
  267. cmGlobalGenerator *gg = this->GetGlobalGenerator();
  268. bool created = false;
  269. // if a generator was not specified use a generic one
  270. if (!gg)
  271. {
  272. gg = new cmGlobalGenerator;
  273. gg->SetCMakeInstance(this);
  274. created = true;
  275. }
  276. // read in the list file to fill the cache
  277. if(path)
  278. {
  279. std::auto_ptr<cmLocalGenerator> lg(gg->CreateLocalGenerator());
  280. lg->SetGlobalGenerator(gg);
  281. if (!lg->GetMakefile()->ReadListFile(0, path))
  282. {
  283. std::cerr << "Error in reading cmake initial cache file:"
  284. << path << "\n";
  285. }
  286. }
  287. // free generic one if generated
  288. if (created)
  289. {
  290. delete gg;
  291. }
  292. }
  293. // Parse the args
  294. void cmake::SetArgs(const std::vector<std::string>& args)
  295. {
  296. bool directoriesSet = false;
  297. for(unsigned int i=1; i < args.size(); ++i)
  298. {
  299. std::string arg = args[i];
  300. if(arg.find("-H",0) == 0)
  301. {
  302. directoriesSet = true;
  303. std::string path = arg.substr(2);
  304. path = cmSystemTools::CollapseFullPath(path.c_str());
  305. cmSystemTools::ConvertToUnixSlashes(path);
  306. this->SetHomeDirectory(path.c_str());
  307. }
  308. else if(arg.find("-S",0) == 0)
  309. {
  310. // There is no local generate anymore. Ignore -S option.
  311. }
  312. else if(arg.find("-O",0) == 0)
  313. {
  314. // There is no local generate anymore. Ignore -O option.
  315. }
  316. else if(arg.find("-B",0) == 0)
  317. {
  318. directoriesSet = true;
  319. std::string path = arg.substr(2);
  320. path = cmSystemTools::CollapseFullPath(path.c_str());
  321. cmSystemTools::ConvertToUnixSlashes(path);
  322. this->SetHomeOutputDirectory(path.c_str());
  323. }
  324. else if((i < args.size()-1) && (arg.find("--check-build-system",0) == 0))
  325. {
  326. m_CheckBuildSystem = args[++i];
  327. m_ClearBuildSystem = (atoi(args[++i].c_str()) > 0);
  328. }
  329. else if(arg.find("-V",0) == 0)
  330. {
  331. m_Verbose = true;
  332. }
  333. else if(arg.find("-D",0) == 0)
  334. {
  335. // skip for now
  336. }
  337. else if(arg.find("-C",0) == 0)
  338. {
  339. // skip for now
  340. }
  341. else if(arg.find("-P",0) == 0)
  342. {
  343. // skip for now
  344. i++;
  345. }
  346. else if(arg.find("--debug-trycompile",0) == 0)
  347. {
  348. std::cout << "debug trycompile on\n";
  349. this->DebugTryCompileOn();
  350. }
  351. else if(arg.find("-G",0) == 0)
  352. {
  353. std::string value = arg.substr(2);
  354. cmGlobalGenerator* gen =
  355. this->CreateGlobalGenerator(value.c_str());
  356. if(!gen)
  357. {
  358. cmSystemTools::Error("Could not create named generator ",
  359. value.c_str());
  360. }
  361. else
  362. {
  363. this->SetGlobalGenerator(gen);
  364. }
  365. }
  366. // no option assume it is the path to the source
  367. else
  368. {
  369. directoriesSet = true;
  370. this->SetDirectoriesFromFile(arg.c_str());
  371. }
  372. }
  373. if(!directoriesSet)
  374. {
  375. this->SetHomeOutputDirectory
  376. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  377. this->SetStartOutputDirectory
  378. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  379. this->SetHomeDirectory
  380. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  381. this->SetStartDirectory
  382. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  383. }
  384. this->SetStartDirectory(this->GetHomeDirectory());
  385. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  386. }
  387. //----------------------------------------------------------------------------
  388. void cmake::SetDirectoriesFromFile(const char* arg)
  389. {
  390. // Check if the argument refers to a CMakeCache.txt or
  391. // CMakeLists.txt file.
  392. std::string listPath;
  393. std::string cachePath;
  394. bool argIsFile = false;
  395. if(cmSystemTools::FileIsDirectory(arg))
  396. {
  397. std::string path = cmSystemTools::CollapseFullPath(arg);
  398. cmSystemTools::ConvertToUnixSlashes(path);
  399. std::string cacheFile = path;
  400. cacheFile += "/CMakeCache.txt";
  401. std::string listFile = path;
  402. listFile += "/CMakeLists.txt";
  403. if(cmSystemTools::FileExists(cacheFile.c_str()))
  404. {
  405. cachePath = path;
  406. }
  407. if(cmSystemTools::FileExists(listFile.c_str()))
  408. {
  409. listPath = path;
  410. }
  411. }
  412. else if(cmSystemTools::FileExists(arg))
  413. {
  414. argIsFile = true;
  415. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  416. std::string name = cmSystemTools::GetFilenameName(fullPath.c_str());
  417. name = cmSystemTools::LowerCase(name);
  418. if(name == "cmakecache.txt")
  419. {
  420. cachePath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  421. }
  422. else if(name == "cmakelists.txt")
  423. {
  424. listPath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  425. }
  426. }
  427. else
  428. {
  429. // Specified file or directory does not exist. Try to set things
  430. // up to produce a meaningful error message.
  431. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  432. std::string name = cmSystemTools::GetFilenameName(fullPath.c_str());
  433. name = cmSystemTools::LowerCase(name);
  434. if(name == "cmakecache.txt" || name == "cmakelists.txt")
  435. {
  436. argIsFile = true;
  437. listPath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  438. }
  439. else
  440. {
  441. listPath = fullPath;
  442. }
  443. }
  444. // If there is a CMakeCache.txt file, use its settings.
  445. if(cachePath.length() > 0)
  446. {
  447. cmCacheManager* cachem = this->GetCacheManager();
  448. cmCacheManager::CacheIterator it = cachem->NewIterator();
  449. if(cachem->LoadCache(cachePath.c_str()) && it.Find("CMAKE_HOME_DIRECTORY"))
  450. {
  451. this->SetHomeOutputDirectory(cachePath.c_str());
  452. this->SetStartOutputDirectory(cachePath.c_str());
  453. this->SetHomeDirectory(it.GetValue());
  454. this->SetStartDirectory(it.GetValue());
  455. return;
  456. }
  457. }
  458. // If there is a CMakeLists.txt file, use it as the source tree.
  459. if(listPath.length() > 0)
  460. {
  461. this->SetHomeDirectory(listPath.c_str());
  462. this->SetStartDirectory(listPath.c_str());
  463. if(argIsFile)
  464. {
  465. // Source CMakeLists.txt file given. It was probably dropped
  466. // onto the executable in a GUI. Default to an in-source build.
  467. this->SetHomeOutputDirectory(listPath.c_str());
  468. this->SetStartOutputDirectory(listPath.c_str());
  469. }
  470. else
  471. {
  472. // Source directory given on command line. Use current working
  473. // directory as build tree.
  474. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  475. this->SetHomeOutputDirectory(cwd.c_str());
  476. this->SetStartOutputDirectory(cwd.c_str());
  477. }
  478. return;
  479. }
  480. // We didn't find a CMakeLists.txt or CMakeCache.txt file from the
  481. // argument. Assume it is the path to the source tree, and use the
  482. // current working directory as the build tree.
  483. std::string full = cmSystemTools::CollapseFullPath(arg);
  484. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  485. this->SetHomeDirectory(full.c_str());
  486. this->SetStartDirectory(full.c_str());
  487. this->SetHomeOutputDirectory(cwd.c_str());
  488. this->SetStartOutputDirectory(cwd.c_str());
  489. }
  490. // at the end of this CMAKE_ROOT and CMAKE_COMMAND should be added to the cache
  491. int cmake::AddCMakePaths(const char *arg0)
  492. {
  493. // Find our own executable.
  494. std::vector<cmStdString> failures;
  495. std::string cMakeSelf = arg0;
  496. cmSystemTools::ConvertToUnixSlashes(cMakeSelf);
  497. failures.push_back(cMakeSelf);
  498. cMakeSelf = cmSystemTools::FindProgram(cMakeSelf.c_str());
  499. cmSystemTools::ConvertToUnixSlashes(cMakeSelf);
  500. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  501. {
  502. #ifdef CMAKE_BUILD_DIR
  503. std::string intdir = ".";
  504. #ifdef CMAKE_INTDIR
  505. intdir = CMAKE_INTDIR;
  506. #endif
  507. cMakeSelf = CMAKE_BUILD_DIR;
  508. cMakeSelf += "/bin/";
  509. cMakeSelf += intdir;
  510. cMakeSelf += "/cmake";
  511. cMakeSelf += cmSystemTools::GetExecutableExtension();
  512. #endif
  513. }
  514. #ifdef CMAKE_PREFIX
  515. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  516. {
  517. failures.push_back(cMakeSelf);
  518. cMakeSelf = CMAKE_PREFIX "/bin/cmake";
  519. }
  520. #endif
  521. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  522. {
  523. failures.push_back(cMakeSelf);
  524. cmOStringStream msg;
  525. msg << "CMAKE can not find the command line program cmake.\n";
  526. msg << " argv[0] = \"" << arg0 << "\"\n";
  527. msg << " Attempted paths:\n";
  528. std::vector<cmStdString>::iterator i;
  529. for(i=failures.begin(); i != failures.end(); ++i)
  530. {
  531. msg << " \"" << i->c_str() << "\"\n";
  532. }
  533. cmSystemTools::Error(msg.str().c_str());
  534. return 0;
  535. }
  536. // Save the value in the cache
  537. this->m_CacheManager->AddCacheEntry
  538. ("CMAKE_COMMAND",cMakeSelf.c_str(), "Path to CMake executable.",
  539. cmCacheManager::INTERNAL);
  540. // Find and save the command to edit the cache
  541. std::string editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  542. "/ccmake" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  543. if( !cmSystemTools::FileExists(editCacheCommand.c_str()))
  544. {
  545. editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  546. "/CMakeSetup" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  547. }
  548. std::string ctestCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  549. "/ctest" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  550. if(cmSystemTools::FileExists(ctestCommand.c_str()))
  551. {
  552. this->m_CacheManager->AddCacheEntry
  553. ("CMAKE_CTEST_COMMAND", ctestCommand.c_str(),
  554. "Path to ctest program executable.", cmCacheManager::INTERNAL);
  555. }
  556. if(cmSystemTools::FileExists(editCacheCommand.c_str()))
  557. {
  558. this->m_CacheManager->AddCacheEntry
  559. ("CMAKE_EDIT_COMMAND", editCacheCommand.c_str(),
  560. "Path to cache edit program executable.", cmCacheManager::INTERNAL);
  561. }
  562. // do CMAKE_ROOT, look for the environment variable first
  563. std::string cMakeRoot;
  564. std::string modules;
  565. if (getenv("CMAKE_ROOT"))
  566. {
  567. cMakeRoot = getenv("CMAKE_ROOT");
  568. modules = cMakeRoot + "/Modules/CMake.cmake";
  569. }
  570. if(!cmSystemTools::FileExists(modules.c_str()))
  571. {
  572. // next try exe/..
  573. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  574. std::string::size_type slashPos = cMakeRoot.rfind("/");
  575. if(slashPos != std::string::npos)
  576. {
  577. cMakeRoot = cMakeRoot.substr(0, slashPos);
  578. }
  579. // is there no Modules direcory there?
  580. modules = cMakeRoot + "/Modules/CMake.cmake";
  581. }
  582. if (!cmSystemTools::FileExists(modules.c_str()))
  583. {
  584. // try exe/../share/cmake
  585. cMakeRoot += CMAKE_DATA_DIR;
  586. modules = cMakeRoot + "/Modules/CMake.cmake";
  587. }
  588. #ifdef CMAKE_ROOT_DIR
  589. if (!cmSystemTools::FileExists(modules.c_str()))
  590. {
  591. // try compiled in root directory
  592. cMakeRoot = CMAKE_ROOT_DIR;
  593. modules = cMakeRoot + "/Modules/CMake.cmake";
  594. }
  595. #endif
  596. #ifdef CMAKE_PREFIX
  597. if (!cmSystemTools::FileExists(modules.c_str()))
  598. {
  599. // try compiled in install prefix
  600. cMakeRoot = CMAKE_PREFIX CMAKE_DATA_DIR;
  601. modules = cMakeRoot + "/Modules/CMake.cmake";
  602. }
  603. #endif
  604. if (!cmSystemTools::FileExists(modules.c_str()))
  605. {
  606. // try
  607. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  608. cMakeRoot += CMAKE_DATA_DIR;
  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. // is there no Modules direcory there?
  616. modules = cMakeRoot + "/Modules/CMake.cmake";
  617. }
  618. if (!cmSystemTools::FileExists(modules.c_str()))
  619. {
  620. // couldn't find modules
  621. cmSystemTools::Error("Could not find CMAKE_ROOT !!!\n"
  622. "CMake has most likely not been installed correctly.\n"
  623. "Modules directory not found in\n",
  624. cMakeRoot.c_str());
  625. return 0;
  626. }
  627. this->m_CacheManager->AddCacheEntry
  628. ("CMAKE_ROOT", cMakeRoot.c_str(),
  629. "Path to CMake installation.", cmCacheManager::INTERNAL);
  630. #ifdef _WIN32
  631. std::string comspec = "cmw9xcom.exe";
  632. cmSystemTools::SetWindows9xComspecSubstitute(comspec.c_str());
  633. #endif
  634. return 1;
  635. }
  636. void CMakeCommandUsage(const char* program)
  637. {
  638. cmOStringStream errorStream;
  639. #ifdef CMAKE_BUILD_WITH_CMAKE
  640. errorStream
  641. << "cmake version " << cmVersion::GetCMakeVersion() << "\n";
  642. #else
  643. errorStream
  644. << "cmake bootstrap\n";
  645. #endif
  646. errorStream
  647. << "Usage: " << program << " -E [command] [arguments ...]\n"
  648. << "Available commands: \n"
  649. << " chdir dir cmd [args]... - run command in a given directory\n"
  650. << " copy file destination - copy file to destination (either file or directory)\n"
  651. << " copy_if_different in-file out-file - copy file if input has changed\n"
  652. << " copy_directory source destination - copy directory 'source' content to directory 'destination'\n"
  653. << " echo [string]... - displays arguments as text\n"
  654. << " remove file1 file2 ... - remove the file(s)\n"
  655. << " time command [args] ... - run command and return elapsed time\n";
  656. #if defined(_WIN32) && !defined(__CYGWIN__)
  657. errorStream
  658. << " write_regv key value - write registry value\n"
  659. << " delete_regv key - delete registry value\n"
  660. << " comspec - on windows 9x use this for RunCommand\n";
  661. #endif
  662. cmSystemTools::Error(errorStream.str().c_str());
  663. }
  664. int cmake::CMakeCommand(std::vector<std::string>& args)
  665. {
  666. if (args.size() > 1)
  667. {
  668. // Copy file
  669. if (args[1] == "copy" && args.size() == 4)
  670. {
  671. if(!cmSystemTools::cmCopyFile(args[2].c_str(), args[3].c_str()))
  672. {
  673. std::cerr << "Error copying file \"" << args[2].c_str()
  674. << "\" to \"" << args[3].c_str() << "\".\n";
  675. return 1;
  676. }
  677. return 0;
  678. }
  679. // Copy file if different.
  680. if (args[1] == "copy_if_different" && args.size() == 4)
  681. {
  682. if(!cmSystemTools::CopyFileIfDifferent(args[2].c_str(), args[3].c_str()))
  683. {
  684. std::cerr << "Error copying file (if different) from \""
  685. << args[2].c_str() << "\" to \"" << args[3].c_str()
  686. << "\".\n";
  687. return 1;
  688. }
  689. return 0;
  690. }
  691. // Copy directory content
  692. if (args[1] == "copy_directory" && args.size() == 4)
  693. {
  694. if(!cmSystemTools::CopyADirectory(args[2].c_str(), args[3].c_str()))
  695. {
  696. std::cerr << "Error copying directory from \""
  697. << args[2].c_str() << "\" to \"" << args[3].c_str()
  698. << "\".\n";
  699. return 1;
  700. }
  701. return 0;
  702. }
  703. // Echo string
  704. else if (args[1] == "echo" )
  705. {
  706. unsigned int cc;
  707. for ( cc = 2; cc < args.size(); cc ++ )
  708. {
  709. std::cout << args[cc] << " ";
  710. }
  711. std::cout << std::endl;
  712. return 0;
  713. }
  714. // Remove file
  715. else if (args[1] == "remove" && args.size() > 2)
  716. {
  717. for (std::string::size_type cc = 2; cc < args.size(); cc ++)
  718. {
  719. if(args[cc] != "-f")
  720. {
  721. if(args[cc] == "\\-f")
  722. {
  723. args[cc] = "-f";
  724. }
  725. cmSystemTools::RemoveFile(args[cc].c_str());
  726. }
  727. }
  728. return 0;
  729. }
  730. // Clock command
  731. else if (args[1] == "time" && args.size() > 2)
  732. {
  733. std::string command = args[2];
  734. for (std::string::size_type cc = 3; cc < args.size(); cc ++)
  735. {
  736. command += " ";
  737. command += args[cc];
  738. }
  739. clock_t clock_start, clock_finish;
  740. time_t time_start, time_finish;
  741. time(&time_start);
  742. clock_start = clock();
  743. cmSystemTools::RunSingleCommand(command.c_str());
  744. clock_finish = clock();
  745. time(&time_finish);
  746. double clocks_per_sec = (double)CLOCKS_PER_SEC;
  747. std::cout << "Elapsed time: "
  748. << (long)(time_finish - time_start) << " s. (time)"
  749. << ", "
  750. << (double)(clock_finish - clock_start) / clocks_per_sec
  751. << " s. (clock)"
  752. << "\n";
  753. return 0;
  754. }
  755. // Command to change directory and run a program.
  756. else if (args[1] == "chdir" && args.size() >= 4)
  757. {
  758. std::string directory = args[2];
  759. if(!cmSystemTools::FileExists(directory.c_str()))
  760. {
  761. cmSystemTools::Error("Directory does not exist for chdir command: ",
  762. args[2].c_str());
  763. return 0;
  764. }
  765. std::string command = "\"";
  766. command += args[3];
  767. command += "\"";
  768. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  769. {
  770. command += " \"";
  771. command += args[cc];
  772. command += "\"";
  773. }
  774. int retval = 0;
  775. int timeout = 0;
  776. if ( cmSystemTools::RunSingleCommand(command.c_str(), 0, &retval,
  777. directory.c_str(), true, timeout) )
  778. {
  779. return retval;
  780. }
  781. return 1;
  782. }
  783. // Internal CMake shared library support.
  784. else if (args[1] == "cmake_symlink_library" && args.size() == 5)
  785. {
  786. int result = 0;
  787. std::string realName = args[2];
  788. std::string soName = args[3];
  789. std::string name = args[4];
  790. if(soName != realName)
  791. {
  792. std::string fname = cmSystemTools::GetFilenameName(realName);
  793. if(cmSystemTools::FileExists(soName.c_str()))
  794. {
  795. cmSystemTools::RemoveFile(soName.c_str());
  796. }
  797. if(!cmSystemTools::CreateSymlink(fname.c_str(), soName.c_str()))
  798. {
  799. result = 1;
  800. }
  801. }
  802. if(name != soName)
  803. {
  804. std::string fname = cmSystemTools::GetFilenameName(soName);
  805. if(cmSystemTools::FileExists(soName.c_str()))
  806. {
  807. cmSystemTools::RemoveFile(name.c_str());
  808. }
  809. if(!cmSystemTools::CreateSymlink(fname.c_str(), name.c_str()))
  810. {
  811. result = 1;
  812. }
  813. }
  814. return result;
  815. }
  816. // Internal CMake dependency scanning support.
  817. else if (args[1] == "cmake_depends" && args.size() >= 8)
  818. {
  819. cmake cm;
  820. cmGlobalGenerator *ggd = cm.CreateGlobalGenerator(args[2].c_str());
  821. ggd->SetCMakeInstance(&cm);
  822. if (ggd)
  823. {
  824. std::auto_ptr<cmLocalGenerator> lgd(ggd->CreateLocalGenerator());
  825. lgd->SetGlobalGenerator(ggd);
  826. return lgd->ScanDependencies(args)? 0 : 2;
  827. }
  828. return 1;
  829. }
  830. #if defined(CMAKE_BUILD_WITH_CMAKE)
  831. // Internal CMake Fortran module support.
  832. else if (args[1] == "cmake_copy_f90_mod" && args.size() >= 4)
  833. {
  834. return cmDependsFortran::CopyModule(args)? 0 : 1;
  835. }
  836. #endif
  837. #if defined(_WIN32) && !defined(__CYGWIN__)
  838. // Write registry value
  839. else if (args[1] == "write_regv" && args.size() > 3)
  840. {
  841. return cmSystemTools::WriteRegistryValue(args[2].c_str(),
  842. args[3].c_str()) ? 0 : 1;
  843. }
  844. // Delete registry value
  845. else if (args[1] == "delete_regv" && args.size() > 2)
  846. {
  847. return cmSystemTools::DeleteRegistryValue(args[2].c_str()) ? 0 : 1;
  848. }
  849. // Remove file
  850. else if (args[1] == "comspec" && args.size() > 2)
  851. {
  852. unsigned int cc;
  853. std::string command = args[2];
  854. for ( cc = 3; cc < args.size(); cc ++ )
  855. {
  856. command += " " + args[cc];
  857. }
  858. return cmWin32ProcessExecution::Windows9xHack(command.c_str());
  859. }
  860. #endif
  861. }
  862. ::CMakeCommandUsage(args[0].c_str());
  863. return 1;
  864. }
  865. void cmake::GetRegisteredGenerators(std::vector<std::string>& names)
  866. {
  867. for(RegisteredGeneratorsMap::const_iterator i = m_Generators.begin();
  868. i != m_Generators.end(); ++i)
  869. {
  870. names.push_back(i->first);
  871. }
  872. }
  873. cmGlobalGenerator* cmake::CreateGlobalGenerator(const char* name)
  874. {
  875. RegisteredGeneratorsMap::const_iterator i = m_Generators.find(name);
  876. if(i != m_Generators.end())
  877. {
  878. cmGlobalGenerator* generator = (i->second)();
  879. generator->SetCMakeInstance(this);
  880. return generator;
  881. }
  882. else
  883. {
  884. return 0;
  885. }
  886. }
  887. void cmake::SetHomeDirectory(const char* dir)
  888. {
  889. m_cmHomeDirectory = dir;
  890. cmSystemTools::ConvertToUnixSlashes(m_cmHomeDirectory);
  891. }
  892. void cmake::SetHomeOutputDirectory(const char* lib)
  893. {
  894. m_HomeOutputDirectory = lib;
  895. cmSystemTools::ConvertToUnixSlashes(m_HomeOutputDirectory);
  896. }
  897. void cmake::SetGlobalGenerator(cmGlobalGenerator *gg)
  898. {
  899. // delete the old generator
  900. if (m_GlobalGenerator)
  901. {
  902. delete m_GlobalGenerator;
  903. // restore the original environment variables CXX and CC
  904. // Restor CC
  905. std::string env = "CC=";
  906. if(m_CCEnvironment.size())
  907. {
  908. env += m_CCEnvironment;
  909. }
  910. cmSystemTools::PutEnv(env.c_str());
  911. env = "CXX=";
  912. if(m_CXXEnvironment.size())
  913. {
  914. env += m_CXXEnvironment;
  915. }
  916. cmSystemTools::PutEnv(env.c_str());
  917. }
  918. // set the new
  919. m_GlobalGenerator = gg;
  920. // set the global flag for unix style paths on cmSystemTools as
  921. // soon as the generator is set. This allows gmake to be used
  922. // on windows.
  923. cmSystemTools::SetForceUnixPaths(m_GlobalGenerator->GetForceUnixPaths());
  924. // Save the environment variables CXX and CC
  925. const char* cxx = getenv("CXX");
  926. const char* cc = getenv("CC");
  927. if(cxx)
  928. {
  929. m_CXXEnvironment = cxx;
  930. }
  931. else
  932. {
  933. m_CXXEnvironment = "";
  934. }
  935. if(cc)
  936. {
  937. m_CCEnvironment = cc;
  938. }
  939. else
  940. {
  941. m_CCEnvironment = "";
  942. }
  943. // set the cmake instance just to be sure
  944. gg->SetCMakeInstance(this);
  945. }
  946. int cmake::DoPreConfigureChecks()
  947. {
  948. // Make sure the Start directory contains a CMakeLists.txt file.
  949. std::string srcList = this->GetHomeDirectory();
  950. srcList += "/CMakeLists.txt";
  951. if(!cmSystemTools::FileExists(srcList.c_str()))
  952. {
  953. cmOStringStream err;
  954. if(cmSystemTools::FileIsDirectory(this->GetHomeDirectory()))
  955. {
  956. err << "The source directory \"" << this->GetHomeDirectory()
  957. << "\" does not appear to contain CMakeLists.txt.\n";
  958. }
  959. else if(cmSystemTools::FileExists(this->GetHomeDirectory()))
  960. {
  961. err << "The source directory \"" << this->GetHomeDirectory()
  962. << "\" is a file, not a directory.\n";
  963. }
  964. else
  965. {
  966. err << "The source directory \"" << this->GetHomeDirectory()
  967. << "\" does not exist.\n";
  968. }
  969. err << "Specify --help for usage, or press the help button on the CMake GUI.";
  970. cmSystemTools::Error(err.str().c_str());
  971. return -2;
  972. }
  973. // do a sanity check on some values
  974. if(m_CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY"))
  975. {
  976. std::string cacheStart =
  977. m_CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY");
  978. cacheStart += "/CMakeLists.txt";
  979. std::string currentStart = this->GetHomeDirectory();
  980. currentStart += "/CMakeLists.txt";
  981. if(!cmSystemTools::SameFile(cacheStart.c_str(), currentStart.c_str()))
  982. {
  983. std::string message = "The source \"";
  984. message += currentStart;
  985. message += "\" does not match the source \"";
  986. message += cacheStart;
  987. message += "\" used to generate cache. ";
  988. message += "Re-run cmake with a different source directory.";
  989. cmSystemTools::Error(message.c_str());
  990. return -2;
  991. }
  992. }
  993. else
  994. {
  995. return 0;
  996. }
  997. return 1;
  998. }
  999. int cmake::Configure()
  1000. {
  1001. // Construct right now our path conversion table before it's too late:
  1002. this->UpdateConversionPathTable();
  1003. this->CleanupCommandsAndMacros();
  1004. int res = 0;
  1005. if ( !m_ScriptMode )
  1006. {
  1007. res = this->DoPreConfigureChecks();
  1008. }
  1009. if ( res < 0 )
  1010. {
  1011. return -2;
  1012. }
  1013. if ( !res )
  1014. {
  1015. m_CacheManager->AddCacheEntry("CMAKE_HOME_DIRECTORY",
  1016. this->GetHomeDirectory(),
  1017. "Start directory with the top level CMakeLists.txt file for this project",
  1018. cmCacheManager::INTERNAL);
  1019. }
  1020. // set the default BACKWARDS compatibility to the current version
  1021. if(!m_CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1022. {
  1023. char ver[256];
  1024. sprintf(ver,"%i.%i",cmMakefile::GetMajorVersion(),
  1025. cmMakefile::GetMinorVersion());
  1026. this->m_CacheManager->AddCacheEntry
  1027. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  1028. "For backwards compatibility, what version of CMake commands and syntax should this version of CMake allow.",
  1029. cmCacheManager::STRING);
  1030. }
  1031. // no generator specified on the command line
  1032. if(!m_GlobalGenerator)
  1033. {
  1034. const char* genName = m_CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1035. if(genName)
  1036. {
  1037. m_GlobalGenerator = this->CreateGlobalGenerator(genName);
  1038. }
  1039. if(m_GlobalGenerator)
  1040. {
  1041. // set the global flag for unix style paths on cmSystemTools as
  1042. // soon as the generator is set. This allows gmake to be used
  1043. // on windows.
  1044. cmSystemTools::SetForceUnixPaths(m_GlobalGenerator->GetForceUnixPaths());
  1045. }
  1046. else
  1047. {
  1048. #if defined(__BORLANDC__) && defined(_WIN32)
  1049. this->SetGlobalGenerator(new cmGlobalBorlandMakefileGenerator);
  1050. #elif defined(_WIN32) && !defined(__CYGWIN__)
  1051. std::string installedCompiler;
  1052. std::string mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0\\Setup;Dbghelp_path]";
  1053. cmSystemTools::ExpandRegistryValues(mp);
  1054. if (!(mp == "/registry"))
  1055. {
  1056. installedCompiler = "Visual Studio 8 2005";
  1057. }
  1058. else
  1059. {
  1060. mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\7.1;InstallDir]";
  1061. cmSystemTools::ExpandRegistryValues(mp);
  1062. if (!(mp == "/registry"))
  1063. {
  1064. installedCompiler = "Visual Studio 7 .NET 2003";
  1065. }
  1066. else
  1067. {
  1068. mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\7.0;InstallDir]";
  1069. cmSystemTools::ExpandRegistryValues(mp);
  1070. if (!(mp == "/registry"))
  1071. {
  1072. installedCompiler = "Visual Studio 7";
  1073. }
  1074. else
  1075. {
  1076. installedCompiler = "Visual Studio 6";
  1077. }
  1078. }
  1079. }
  1080. cmGlobalGenerator* gen = this->CreateGlobalGenerator(installedCompiler.c_str());
  1081. if(!gen)
  1082. {
  1083. gen = new cmGlobalNMakeMakefileGenerator;
  1084. }
  1085. this->SetGlobalGenerator(gen);
  1086. #else
  1087. this->SetGlobalGenerator(new cmGlobalUnixMakefileGenerator3);
  1088. #endif
  1089. }
  1090. if(!m_GlobalGenerator)
  1091. {
  1092. cmSystemTools::Error("Could not create generator");
  1093. return -1;
  1094. }
  1095. }
  1096. const char* genName = m_CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1097. if(genName)
  1098. {
  1099. if(strcmp(m_GlobalGenerator->GetName(), genName) != 0)
  1100. {
  1101. std::string message = "Error: generator : ";
  1102. message += m_GlobalGenerator->GetName();
  1103. message += "\nDoes not match the generator used previously: ";
  1104. message += genName;
  1105. message +=
  1106. "\nEither remove the CMakeCache.txt file or choose a different"
  1107. " binary directory.";
  1108. cmSystemTools::Error(message.c_str());
  1109. return -2;
  1110. }
  1111. }
  1112. if(!m_CacheManager->GetCacheValue("CMAKE_GENERATOR"))
  1113. {
  1114. m_CacheManager->AddCacheEntry("CMAKE_GENERATOR", m_GlobalGenerator->GetName(),
  1115. "Name of generator.",
  1116. cmCacheManager::INTERNAL);
  1117. }
  1118. // reset any system configuration information, except for when we are
  1119. // InTryCompile. With TryCompile the system info is taken from the parent's
  1120. // info to save time
  1121. if (!m_InTryCompile)
  1122. {
  1123. m_GlobalGenerator->ClearEnabledLanguages();
  1124. }
  1125. this->CleanupWrittenFiles();
  1126. // Truncate log files
  1127. if (!m_InTryCompile)
  1128. {
  1129. this->TruncateOutputLog("CMakeOutput.log");
  1130. this->TruncateOutputLog("CMakeError.log");
  1131. }
  1132. // actually do the configure
  1133. m_GlobalGenerator->Configure();
  1134. // Before saving the cache
  1135. // if the project did not define one of the entries below, add them now
  1136. // so users can edit the values in the cache:
  1137. // LIBRARY_OUTPUT_PATH
  1138. // EXECUTABLE_OUTPUT_PATH
  1139. if(!m_CacheManager->GetCacheValue("LIBRARY_OUTPUT_PATH"))
  1140. {
  1141. m_CacheManager->AddCacheEntry("LIBRARY_OUTPUT_PATH", "",
  1142. "Single output directory for building all libraries.",
  1143. cmCacheManager::PATH);
  1144. }
  1145. if(!m_CacheManager->GetCacheValue("EXECUTABLE_OUTPUT_PATH"))
  1146. {
  1147. m_CacheManager->AddCacheEntry("EXECUTABLE_OUTPUT_PATH", "",
  1148. "Single output directory for building all executables.",
  1149. cmCacheManager::PATH);
  1150. }
  1151. if(!m_CacheManager->GetCacheValue("CMAKE_USE_RELATIVE_PATHS"))
  1152. {
  1153. m_CacheManager->AddCacheEntry("CMAKE_USE_RELATIVE_PATHS", false,
  1154. "If true, cmake will use relative paths in makefiles and projects.");
  1155. cmCacheManager::CacheIterator it =
  1156. m_CacheManager->GetCacheIterator("CMAKE_USE_RELATIVE_PATHS");
  1157. if ( !it.PropertyExists("ADVANCED") )
  1158. {
  1159. it.SetProperty("ADVANCED", "1");
  1160. }
  1161. }
  1162. if(cmSystemTools::GetFatalErrorOccured() &&
  1163. (!this->m_CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM") ||
  1164. cmSystemTools::IsOff(this->m_CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM"))))
  1165. {
  1166. // We must have a bad generator selection. Wipe the cache entry so the
  1167. // user can select another.
  1168. m_CacheManager->RemoveCacheEntry("CMAKE_GENERATOR");
  1169. }
  1170. if ( !m_ScriptMode )
  1171. {
  1172. this->m_CacheManager->SaveCache(this->GetHomeOutputDirectory());
  1173. }
  1174. if(cmSystemTools::GetErrorOccuredFlag())
  1175. {
  1176. return -1;
  1177. }
  1178. return 0;
  1179. }
  1180. bool cmake::CacheVersionMatches()
  1181. {
  1182. const char* majv = m_CacheManager->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION");
  1183. const char* minv = m_CacheManager->GetCacheValue("CMAKE_CACHE_MINOR_VERSION");
  1184. const char* relv = m_CacheManager->GetCacheValue("CMAKE_CACHE_RELEASE_VERSION");
  1185. bool cacheSameCMake = false;
  1186. if(majv &&
  1187. atoi(majv) == static_cast<int>(cmMakefile::GetMajorVersion())
  1188. && minv &&
  1189. atoi(minv) == static_cast<int>(cmMakefile::GetMinorVersion())
  1190. && relv && (strcmp(relv, cmMakefile::GetReleaseVersion()) == 0))
  1191. {
  1192. cacheSameCMake = true;
  1193. }
  1194. return cacheSameCMake;
  1195. }
  1196. void cmake::PreLoadCMakeFiles()
  1197. {
  1198. std::string pre_load = this->GetHomeDirectory();
  1199. if ( pre_load.size() > 0 )
  1200. {
  1201. pre_load += "/PreLoad.cmake";
  1202. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1203. {
  1204. this->ReadListFile(pre_load.c_str());
  1205. }
  1206. }
  1207. pre_load = this->GetHomeOutputDirectory();
  1208. if ( pre_load.size() > 0 )
  1209. {
  1210. pre_load += "/PreLoad.cmake";
  1211. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1212. {
  1213. this->ReadListFile(pre_load.c_str());
  1214. }
  1215. }
  1216. }
  1217. // handle a command line invocation
  1218. int cmake::Run(const std::vector<std::string>& args, bool noconfigure)
  1219. {
  1220. // Process the arguments
  1221. this->SetArgs(args);
  1222. if(cmSystemTools::GetErrorOccuredFlag())
  1223. {
  1224. CMakeCommandUsage(args[0].c_str());
  1225. return -1;
  1226. }
  1227. // set the cmake command
  1228. m_CMakeCommand = args[0];
  1229. if ( !m_ScriptMode )
  1230. {
  1231. // load the cache
  1232. if(this->LoadCache() < 0)
  1233. {
  1234. cmSystemTools::Error("Error executing cmake::LoadCache(). Aborting.\n");
  1235. return -1;
  1236. }
  1237. }
  1238. // Add any cache args
  1239. if ( !this->SetCacheArgs(args) )
  1240. {
  1241. cmSystemTools::Error("Problem processing arguments. Aborting.\n");
  1242. return -1;
  1243. }
  1244. // In script mode we terminate after running the script.
  1245. if(m_ScriptMode)
  1246. {
  1247. return 0;
  1248. }
  1249. this->PreLoadCMakeFiles();
  1250. std::string systemFile = this->GetHomeOutputDirectory();
  1251. systemFile += "/CMakeSystem.cmake";
  1252. if ( noconfigure )
  1253. {
  1254. return 0;
  1255. }
  1256. // now run the global generate
  1257. // Check the state of the build system to see if we need to regenerate.
  1258. if(!this->CheckBuildSystem())
  1259. {
  1260. return 0;
  1261. }
  1262. // If we are doing global generate, we better set start and start
  1263. // output directory to the root of the project.
  1264. std::string oldstartdir = this->GetStartDirectory();
  1265. std::string oldstartoutputdir = this->GetStartOutputDirectory();
  1266. this->SetStartDirectory(this->GetHomeDirectory());
  1267. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  1268. int ret = this->Configure();
  1269. if (ret || m_ScriptMode)
  1270. {
  1271. return ret;
  1272. }
  1273. ret = this->Generate();
  1274. std::string message = "Build files have been written to: ";
  1275. message += this->GetHomeOutputDirectory();
  1276. this->UpdateProgress(message.c_str(), -1);
  1277. if(ret)
  1278. {
  1279. return ret;
  1280. }
  1281. this->SetStartDirectory(oldstartdir.c_str());
  1282. this->SetStartOutputDirectory(oldstartoutputdir.c_str());
  1283. return ret;
  1284. }
  1285. int cmake::Generate()
  1286. {
  1287. if(!m_GlobalGenerator)
  1288. {
  1289. return -1;
  1290. }
  1291. m_GlobalGenerator->Generate();
  1292. if(cmSystemTools::GetErrorOccuredFlag())
  1293. {
  1294. return -1;
  1295. }
  1296. return 0;
  1297. }
  1298. unsigned int cmake::GetMajorVersion()
  1299. {
  1300. return cmMakefile::GetMajorVersion();
  1301. }
  1302. unsigned int cmake::GetMinorVersion()
  1303. {
  1304. return cmMakefile::GetMinorVersion();
  1305. }
  1306. const char *cmake::GetReleaseVersion()
  1307. {
  1308. return cmMakefile::GetReleaseVersion();
  1309. }
  1310. void cmake::AddCacheEntry(const char* key, const char* value,
  1311. const char* helpString,
  1312. int type)
  1313. {
  1314. m_CacheManager->AddCacheEntry(key, value,
  1315. helpString,
  1316. cmCacheManager::CacheEntryType(type));
  1317. }
  1318. const char* cmake::GetCacheDefinition(const char* name) const
  1319. {
  1320. return m_CacheManager->GetCacheValue(name);
  1321. }
  1322. int cmake::DumpDocumentationToFile(std::ostream& f)
  1323. {
  1324. #ifdef CMAKE_BUILD_WITH_CMAKE
  1325. // Loop over all registered commands and print out documentation
  1326. const char *name;
  1327. const char *terse;
  1328. const char *full;
  1329. char tmp[1024];
  1330. sprintf(tmp,"Version %d.%d (%s)", cmake::GetMajorVersion(),
  1331. cmake::GetMinorVersion(), cmVersion::GetReleaseVersion().c_str());
  1332. f << "<html>\n";
  1333. f << "<h1>Documentation for commands of CMake " << tmp << "</h1>\n";
  1334. f << "<ul>\n";
  1335. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  1336. j != m_Commands.end(); ++j)
  1337. {
  1338. name = (*j).second->GetName();
  1339. terse = (*j).second->GetTerseDocumentation();
  1340. full = (*j).second->GetFullDocumentation();
  1341. f << "<li><b>" << name << "</b> - " << terse << std::endl
  1342. << "<br><i>Usage:</i> " << full << "</li>" << std::endl << std::endl;
  1343. }
  1344. f << "</ul></html>\n";
  1345. #else
  1346. (void)f;
  1347. #endif
  1348. return 1;
  1349. }
  1350. void cmake::AddDefaultCommands()
  1351. {
  1352. std::list<cmCommand*> commands;
  1353. GetBootstrapCommands(commands);
  1354. GetPredefinedCommands(commands);
  1355. for(std::list<cmCommand*>::iterator i = commands.begin();
  1356. i != commands.end(); ++i)
  1357. {
  1358. this->AddCommand(*i);
  1359. }
  1360. }
  1361. void cmake::AddDefaultGenerators()
  1362. {
  1363. #if defined(_WIN32) && !defined(__CYGWIN__)
  1364. m_Generators[cmGlobalVisualStudio6Generator::GetActualName()] =
  1365. &cmGlobalVisualStudio6Generator::New;
  1366. #if !defined(__MINGW32__)
  1367. m_Generators[cmGlobalVisualStudio7Generator::GetActualName()] =
  1368. &cmGlobalVisualStudio7Generator::New;
  1369. m_Generators[cmGlobalVisualStudio71Generator::GetActualName()] =
  1370. &cmGlobalVisualStudio71Generator::New;
  1371. m_Generators[cmGlobalVisualStudio8Generator::GetActualName()] =
  1372. &cmGlobalVisualStudio8Generator::New;
  1373. #endif
  1374. m_Generators[cmGlobalBorlandMakefileGenerator::GetActualName()] =
  1375. &cmGlobalBorlandMakefileGenerator::New;
  1376. m_Generators[cmGlobalNMakeMakefileGenerator::GetActualName()] =
  1377. &cmGlobalNMakeMakefileGenerator::New;
  1378. #endif
  1379. m_Generators[cmGlobalUnixMakefileGenerator3::GetActualName()] =
  1380. &cmGlobalUnixMakefileGenerator3::New;
  1381. #ifdef CMAKE_USE_XCODE
  1382. m_Generators[cmGlobalXCodeGenerator::GetActualName()] =
  1383. &cmGlobalXCodeGenerator::New;
  1384. #endif
  1385. #ifdef CMAKE_USE_KDEVELOP
  1386. m_Generators[cmGlobalKdevelopGenerator::GetActualName()] =
  1387. &cmGlobalKdevelopGenerator::New;
  1388. #endif
  1389. }
  1390. int cmake::LoadCache()
  1391. {
  1392. m_CacheManager->LoadCache(this->GetHomeOutputDirectory());
  1393. if (m_CMakeCommand.size() < 2)
  1394. {
  1395. cmSystemTools::Error("cmake command was not specified prior to loading the cache in cmake.cxx");
  1396. return -1;
  1397. }
  1398. // setup CMAKE_ROOT and CMAKE_COMMAND
  1399. if(!this->AddCMakePaths(m_CMakeCommand.c_str()))
  1400. {
  1401. return -3;
  1402. }
  1403. // set the default BACKWARDS compatibility to the current version
  1404. if(!m_CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1405. {
  1406. char ver[256];
  1407. sprintf(ver,"%i.%i",cmMakefile::GetMajorVersion(),
  1408. cmMakefile::GetMinorVersion());
  1409. this->m_CacheManager->AddCacheEntry
  1410. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  1411. "For backwards compatibility, what version of CMake commands and syntax should this version of CMake allow.",
  1412. cmCacheManager::STRING);
  1413. }
  1414. return 0;
  1415. }
  1416. void cmake::SetProgressCallback(ProgressCallback f, void *cd)
  1417. {
  1418. m_ProgressCallback = f;
  1419. m_ProgressCallbackClientData = cd;
  1420. }
  1421. void cmake::UpdateProgress(const char *msg, float prog)
  1422. {
  1423. if(m_ProgressCallback && !m_InTryCompile)
  1424. {
  1425. (*m_ProgressCallback)(msg, prog, m_ProgressCallbackClientData);
  1426. return;
  1427. }
  1428. }
  1429. void cmake::GetCommandDocumentation(std::vector<cmDocumentationEntry>& v) const
  1430. {
  1431. for(RegisteredCommandsMap::const_iterator j = m_Commands.begin();
  1432. j != m_Commands.end(); ++j)
  1433. {
  1434. cmDocumentationEntry e =
  1435. {
  1436. (*j).second->GetName(),
  1437. (*j).second->GetTerseDocumentation(),
  1438. (*j).second->GetFullDocumentation()
  1439. };
  1440. v.push_back(e);
  1441. }
  1442. cmDocumentationEntry empty = {0,0,0};
  1443. v.push_back(empty);
  1444. }
  1445. void cmake::GetGeneratorDocumentation(std::vector<cmDocumentationEntry>& v)
  1446. {
  1447. for(RegisteredGeneratorsMap::const_iterator i = m_Generators.begin();
  1448. i != m_Generators.end(); ++i)
  1449. {
  1450. cmDocumentationEntry e;
  1451. cmGlobalGenerator* generator = (i->second)();
  1452. generator->GetDocumentation(e);
  1453. delete generator;
  1454. v.push_back(e);
  1455. }
  1456. cmDocumentationEntry empty = {0,0,0};
  1457. v.push_back(empty);
  1458. }
  1459. void cmake::AddWrittenFile(const char* file)
  1460. {
  1461. m_WrittenFiles.insert(file);
  1462. }
  1463. bool cmake::HasWrittenFile(const char* file)
  1464. {
  1465. return m_WrittenFiles.find(file) != m_WrittenFiles.end();
  1466. }
  1467. void cmake::CleanupWrittenFiles()
  1468. {
  1469. m_WrittenFiles.clear();
  1470. }
  1471. void cmake::UpdateConversionPathTable()
  1472. {
  1473. // Update the path conversion table with any specified file:
  1474. const char* tablepath =
  1475. m_CacheManager->GetCacheValue("CMAKE_PATH_TRANSLATION_FILE");
  1476. if(tablepath)
  1477. {
  1478. std::ifstream table( tablepath );
  1479. if(!table)
  1480. {
  1481. cmSystemTools::Error("CMAKE_PATH_TRANSLATION_FILE set to ", tablepath, ". CMake can not open file.");
  1482. cmSystemTools::ReportLastSystemError("CMake can not open file.");
  1483. }
  1484. else
  1485. {
  1486. std::string a, b;
  1487. while(!table.eof())
  1488. {
  1489. // two entries per line
  1490. table >> a; table >> b;
  1491. cmSystemTools::AddTranslationPath( a.c_str(), b.c_str());
  1492. }
  1493. }
  1494. }
  1495. }
  1496. //----------------------------------------------------------------------------
  1497. int cmake::CheckBuildSystem()
  1498. {
  1499. // This method will check the integrity of the build system if the
  1500. // option was given on the command line. It reads the given file to
  1501. // determine whether CMake should rerun. If it does rerun then the
  1502. // generation step will check the integrity of dependencies. If it
  1503. // does not then we need to check the integrity here.
  1504. // If no file is provided for the check, we have to rerun.
  1505. if(m_CheckBuildSystem.size() == 0)
  1506. {
  1507. return 1;
  1508. }
  1509. // If the file provided does not exist, we have to rerun.
  1510. if(!cmSystemTools::FileExists(m_CheckBuildSystem.c_str()))
  1511. {
  1512. return 1;
  1513. }
  1514. // Read the rerun check file and use it to decide whether to do the
  1515. // global generate.
  1516. cmake cm;
  1517. cmGlobalGenerator gg;
  1518. gg.SetCMakeInstance(&cm);
  1519. std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  1520. lg->SetGlobalGenerator(&gg);
  1521. cmMakefile* mf = lg->GetMakefile();
  1522. if(!mf->ReadListFile(0, m_CheckBuildSystem.c_str()) ||
  1523. cmSystemTools::GetErrorOccuredFlag())
  1524. {
  1525. // There was an error reading the file. Just rerun.
  1526. return 1;
  1527. }
  1528. // Get the set of dependencies and outputs.
  1529. const char* dependsStr = mf->GetDefinition("CMAKE_MAKEFILE_DEPENDS");
  1530. const char* outputsStr = mf->GetDefinition("CMAKE_MAKEFILE_OUTPUTS");
  1531. if(!dependsStr || !outputsStr)
  1532. {
  1533. // Not enough information was provided to do the test. Just rerun.
  1534. return 1;
  1535. }
  1536. std::vector<std::string> depends;
  1537. std::vector<std::string> outputs;
  1538. cmSystemTools::ExpandListArgument(dependsStr, depends);
  1539. cmSystemTools::ExpandListArgument(outputsStr, outputs);
  1540. // If any output is older than any dependency then rerun.
  1541. for(std::vector<std::string>::iterator dep = depends.begin();
  1542. dep != depends.end(); ++dep)
  1543. {
  1544. for(std::vector<std::string>::iterator out = outputs.begin();
  1545. out != outputs.end(); ++out)
  1546. {
  1547. int result = 0;
  1548. if(!cmSystemTools::FileTimeCompare(out->c_str(), dep->c_str(), &result) ||
  1549. result < 0)
  1550. {
  1551. return 1;
  1552. }
  1553. }
  1554. }
  1555. // We do not need to rerun CMake. Check dependency integrity. Use
  1556. // the make system's VERBOSE environment variable to enable verbose
  1557. // output.
  1558. bool verbose = cmSystemTools::GetEnv("VERBOSE") != 0;
  1559. // compute depends based on the generator specified
  1560. const char* genName = mf->GetDefinition("CMAKE_DEPENDS_GENERATOR");
  1561. if (!genName || genName[0] == '\0')
  1562. {
  1563. genName = "Unix Makefiles";
  1564. }
  1565. cmGlobalGenerator *ggd = this->CreateGlobalGenerator(genName);
  1566. if (ggd)
  1567. {
  1568. std::auto_ptr<cmLocalGenerator> lgd(ggd->CreateLocalGenerator());
  1569. lgd->SetGlobalGenerator(ggd);
  1570. lgd->CheckDependencies(mf, verbose, m_ClearBuildSystem);
  1571. }
  1572. // No need to rerun.
  1573. return 0;
  1574. }
  1575. //----------------------------------------------------------------------------
  1576. void cmake::TruncateOutputLog(const char* fname)
  1577. {
  1578. std::string fullPath = this->GetHomeOutputDirectory();
  1579. fullPath += "/";
  1580. fullPath += fname;
  1581. struct stat st;
  1582. if ( ::stat(fullPath.c_str(), &st) )
  1583. {
  1584. return;
  1585. }
  1586. if ( !m_CacheManager->GetCacheValue("CMAKE_CACHEFILE_DIR") )
  1587. {
  1588. cmSystemTools::RemoveFile(fullPath.c_str());
  1589. return;
  1590. }
  1591. size_t fsize = st.st_size;
  1592. const size_t maxFileSize = 50 * 1024;
  1593. if ( fsize < maxFileSize )
  1594. {
  1595. //TODO: truncate file
  1596. return;
  1597. }
  1598. }