cmake.cxx 44 KB

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