cmake.cxx 49 KB

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