cmake.cxx 48 KB

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