cmake.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2001 Insight Consortium
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. * The name of the Insight Consortium, nor the names of any consortium members,
  17. nor of any contributors, may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. * Modified source versions must be plainly marked as such, and must not be
  20. misrepresented as being the original software.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. =========================================================================*/
  32. #include "cmake.h"
  33. #include "cmCacheManager.h"
  34. // include the generator
  35. #if defined(_WIN32) && !defined(__CYGWIN__)
  36. #include "cmMSProjectGenerator.h"
  37. #include "cmBorlandMakefileGenerator.h"
  38. #include "cmNMakeMakefileGenerator.h"
  39. #else
  40. #include "cmUnixMakefileGenerator.h"
  41. #endif
  42. cmake::cmake()
  43. {
  44. m_Verbose = false;
  45. #if defined(_WIN32) && !defined(__CYGWIN__)
  46. cmMakefileGenerator::RegisterGenerator(new cmMSProjectGenerator);
  47. cmMakefileGenerator::RegisterGenerator(new cmNMakeMakefileGenerator);
  48. cmMakefileGenerator::RegisterGenerator(new cmBorlandMakefileGenerator);
  49. #else
  50. cmMakefileGenerator::RegisterGenerator(new cmUnixMakefileGenerator);
  51. #endif
  52. }
  53. void cmake::Usage(const char* program)
  54. {
  55. std::cerr << "cmake version " << cmMakefile::GetMajorVersion()
  56. << "." << cmMakefile::GetMinorVersion() << " - "
  57. << cmMakefile::GetReleaseVersion() << "\n";
  58. std::cerr << "Usage: " << program << " [srcdir] [options]\n"
  59. << "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";
  60. std::cerr << "Options are:\n";
  61. std::cerr << "\n-i (puts cmake in wizard mode, not available for ccmake)\n";
  62. std::cerr << "\n-DVAR:TYPE=VALUE (create a cache file entry)\n";
  63. std::cerr << "\n-Cpath_to_initial_cache (a cmake list file that is used to pre-load the cache with values.)\n";
  64. std::cerr << "\n[-GgeneratorName] (where generator name can be one of these: ";
  65. std::vector<std::string> names;
  66. cmMakefileGenerator::GetRegisteredGenerators(names);
  67. for(std::vector<std::string>::iterator i =names.begin();
  68. i != names.end(); ++i)
  69. {
  70. std::cerr << "\"" << i->c_str() << "\" ";
  71. }
  72. std::cerr << ")\n";
  73. }
  74. // Parse the args
  75. void cmake::SetCacheArgs(cmMakefile& builder,
  76. const std::vector<std::string>& args)
  77. {
  78. for(unsigned int i=1; i < args.size(); ++i)
  79. {
  80. std::string arg = args[i];
  81. if(arg.find("-D",0) == 0)
  82. {
  83. std::string entry = arg.substr(2);
  84. std::string var, value;
  85. cmCacheManager::CacheEntryType type;
  86. if(cmCacheManager::ParseEntry(entry.c_str(), var, value, type))
  87. {
  88. cmCacheManager::GetInstance()->AddCacheEntry(
  89. var.c_str(),
  90. cmSystemTools::EscapeSpaces(value.c_str()).c_str(),
  91. "No help, variable specified on the command line.",
  92. type);
  93. }
  94. else
  95. {
  96. std::cerr << "Parse error in command line argument: " << arg << "\n"
  97. << "Should be: VAR:type=value\n";
  98. }
  99. }
  100. else if(arg.find("-C",0) == 0)
  101. {
  102. std::string path = arg.substr(2);
  103. std::cerr << "loading initial cache file " << path.c_str() << "\n";
  104. if(!builder.ReadListFile(path.c_str()))
  105. {
  106. std::cerr << "Error in reading cmake initial cache file:"
  107. << path.c_str() << "\n";
  108. }
  109. }
  110. }
  111. }
  112. // Parse the args
  113. void cmake::SetArgs(cmMakefile& builder, const std::vector<std::string>& args)
  114. {
  115. m_Local = false;
  116. bool directoriesSet = false;
  117. // watch for cmake and cmake srcdir invocations
  118. if (args.size() <= 2)
  119. {
  120. directoriesSet = true;
  121. builder.SetHomeOutputDirectory
  122. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  123. builder.SetStartOutputDirectory
  124. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  125. if (args.size() == 2)
  126. {
  127. builder.SetHomeDirectory
  128. (cmSystemTools::CollapseFullPath(args[1].c_str()).c_str());
  129. builder.SetStartDirectory
  130. (cmSystemTools::CollapseFullPath(args[1].c_str()).c_str());
  131. }
  132. else
  133. {
  134. builder.SetHomeDirectory
  135. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  136. builder.SetStartDirectory
  137. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  138. }
  139. }
  140. for(unsigned int i=1; i < args.size(); ++i)
  141. {
  142. std::string arg = args[i];
  143. if(arg.find("-H",0) == 0)
  144. {
  145. directoriesSet = true;
  146. std::string path = arg.substr(2);
  147. builder.SetHomeDirectory(path.c_str());
  148. }
  149. else if(arg.find("-S",0) == 0)
  150. {
  151. directoriesSet = true;
  152. m_Local = true;
  153. std::string path = arg.substr(2);
  154. builder.SetStartDirectory(path.c_str());
  155. }
  156. else if(arg.find("-O",0) == 0)
  157. {
  158. directoriesSet = true;
  159. std::string path = arg.substr(2);
  160. builder.SetStartOutputDirectory(path.c_str());
  161. }
  162. else if(arg.find("-B",0) == 0)
  163. {
  164. directoriesSet = true;
  165. std::string path = arg.substr(2);
  166. builder.SetHomeOutputDirectory(path.c_str());
  167. }
  168. else if(arg.find("-V",0) == 0)
  169. {
  170. m_Verbose = true;
  171. }
  172. else if(arg.find("-D",0) == 0)
  173. {
  174. // skip for now
  175. }
  176. else if(arg.find("-C",0) == 0)
  177. {
  178. // skip for now
  179. }
  180. else if(arg.find("-G",0) == 0)
  181. {
  182. std::string value = arg.substr(2);
  183. cmMakefileGenerator* gen =
  184. cmMakefileGenerator::CreateGenerator(value.c_str());
  185. if(!gen)
  186. {
  187. cmSystemTools::Error("Could not create named generator ",
  188. value.c_str());
  189. }
  190. else
  191. {
  192. builder.SetMakefileGenerator(gen);
  193. }
  194. }
  195. // no option assume it is the path to the source
  196. else
  197. {
  198. directoriesSet = true;
  199. builder.SetHomeOutputDirectory
  200. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  201. builder.SetStartOutputDirectory
  202. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  203. builder.SetHomeDirectory
  204. (cmSystemTools::CollapseFullPath(arg.c_str()).c_str());
  205. builder.SetStartDirectory
  206. (cmSystemTools::CollapseFullPath(arg.c_str()).c_str());
  207. }
  208. }
  209. if(!directoriesSet)
  210. {
  211. builder.SetHomeOutputDirectory
  212. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  213. builder.SetStartOutputDirectory
  214. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  215. builder.SetHomeDirectory
  216. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  217. builder.SetStartDirectory
  218. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  219. }
  220. if (!m_Local)
  221. {
  222. builder.SetStartDirectory(builder.GetHomeDirectory());
  223. builder.SetStartOutputDirectory(builder.GetHomeOutputDirectory());
  224. }
  225. }
  226. // at the end of this CMAKE_ROOT and CMAKE_COMMAND should be added to the cache
  227. void cmake::AddCMakePaths(const std::vector<std::string>& args)
  228. {
  229. // Find our own executable.
  230. std::string cMakeSelf = args[0];
  231. cmSystemTools::ConvertToUnixSlashes(cMakeSelf);
  232. cMakeSelf = cmSystemTools::FindProgram(cMakeSelf.c_str());
  233. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  234. {
  235. #ifdef CMAKE_BUILD_DIR
  236. cMakeSelf = CMAKE_BUILD_DIR;
  237. cMakeSelf += "/Source/cmake";
  238. #endif
  239. }
  240. #ifdef CMAKE_PREFIX
  241. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  242. {
  243. cMakeSelf = CMAKE_PREFIX "/bin/cmake";
  244. }
  245. #endif
  246. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  247. {
  248. cmSystemTools::Error("CMAKE can not find the command line program cmake. "
  249. "Attempted path: ", cMakeSelf.c_str());
  250. return;
  251. }
  252. // Save the value in the cache
  253. cmCacheManager::GetInstance()->AddCacheEntry
  254. ("CMAKE_COMMAND",
  255. cmSystemTools::EscapeSpaces(cMakeSelf.c_str()).c_str(),
  256. "Path to CMake executable.",
  257. cmCacheManager::INTERNAL);
  258. // do CMAKE_ROOT, look for the environment variable first
  259. std::string cMakeRoot;
  260. std::string modules;
  261. if (getenv("CMAKE_ROOT"))
  262. {
  263. cMakeRoot = getenv("CMAKE_ROOT");
  264. modules = cMakeRoot + "/Modules/FindVTK.cmake";
  265. }
  266. if(!cmSystemTools::FileExists(modules.c_str()))
  267. {
  268. // next try exe/..
  269. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  270. std::string::size_type slashPos = cMakeRoot.rfind("/");
  271. if(slashPos != std::string::npos)
  272. {
  273. cMakeRoot = cMakeRoot.substr(0, slashPos);
  274. }
  275. // is there no Modules direcory there?
  276. modules = cMakeRoot + "/Modules/FindVTK.cmake";
  277. }
  278. if (!cmSystemTools::FileExists(modules.c_str()))
  279. {
  280. // try exe/../share/cmake
  281. cMakeRoot += "/share/CMake";
  282. modules = cMakeRoot + "/Modules/FindVTK.cmake";
  283. }
  284. #ifdef CMAKE_ROOT_DIR
  285. if (!cmSystemTools::FileExists(modules.c_str()))
  286. {
  287. // try compiled in root directory
  288. cMakeRoot = CMAKE_ROOT_DIR;
  289. modules = cMakeRoot + "/Modules/FindVTK.cmake";
  290. }
  291. #endif
  292. #ifdef CMAKE_PREFIX
  293. if (!cmSystemTools::FileExists(modules.c_str()))
  294. {
  295. // try compiled in install prefix
  296. cMakeRoot = CMAKE_PREFIX "/share/CMake";
  297. modules = cMakeRoot + "/Modules/FindVTK.cmake";
  298. }
  299. #endif
  300. if (!cmSystemTools::FileExists(modules.c_str()))
  301. {
  302. // try
  303. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  304. cMakeRoot += "/share/CMake";
  305. modules = cMakeRoot + "/Modules/FindVTK.cmake";
  306. }
  307. if (!cmSystemTools::FileExists(modules.c_str()))
  308. {
  309. // couldn't find modules
  310. cmSystemTools::Error("Could not find CMAKE_ROOT !!!\n",
  311. "Modules directory not in directory:\n",
  312. modules.c_str());
  313. return;
  314. }
  315. cmCacheManager::GetInstance()->AddCacheEntry
  316. ("CMAKE_ROOT", cMakeRoot.c_str(),
  317. "Path to CMake installation.", cmCacheManager::INTERNAL);
  318. }
  319. int cmake::Generate(const std::vector<std::string>& args, bool buildMakefiles)
  320. {
  321. if(args.size() == 1 && !cmSystemTools::FileExists("CMakeLists.txt"))
  322. {
  323. this->Usage(args[0].c_str());
  324. return -1;
  325. }
  326. // look for obvious request for help
  327. for(unsigned int i=1; i < args.size(); ++i)
  328. {
  329. std::string arg = args[i];
  330. if(arg.find("-help",0) != std::string::npos ||
  331. arg.find("--help",0) != std::string::npos ||
  332. arg.find("/?",0) != std::string::npos ||
  333. arg.find("-usage",0) != std::string::npos)
  334. {
  335. this->Usage(args[0].c_str());
  336. return -1;
  337. }
  338. }
  339. // Create a makefile
  340. cmMakefile mf;
  341. // extract the directory arguments, could create a Generator
  342. this->SetArgs(mf, args);
  343. // Read and parse the input makefile
  344. mf.MakeStartDirectoriesCurrent();
  345. cmCacheManager::GetInstance()->LoadCache(&mf);
  346. // extract command line arguments that might add cache entries
  347. this->SetCacheArgs(mf, args);
  348. // no generator specified on the command line
  349. if(!mf.GetMakefileGenerator())
  350. {
  351. cmMakefileGenerator* gen;
  352. const char* genName = mf.GetDefinition("CMAKE_GENERATOR");
  353. if(genName)
  354. {
  355. gen = cmMakefileGenerator::CreateGenerator(genName);
  356. }
  357. else
  358. {
  359. #if defined(__BORLANDC__)
  360. gen = new cmBorlandMakefileGenerator;
  361. #elif defined(_WIN32) && !defined(__CYGWIN__)
  362. gen = new cmMSProjectGenerator;
  363. #else
  364. gen = new cmUnixMakefileGenerator;
  365. #endif
  366. }
  367. if(!gen)
  368. {
  369. cmSystemTools::Error("Could not create generator");
  370. return -1;
  371. }
  372. mf.SetMakefileGenerator(gen);
  373. // add the
  374. }
  375. cmMakefileGenerator* gen = mf.GetMakefileGenerator();
  376. gen->SetLocal(m_Local);
  377. if(!mf.GetDefinition("CMAKE_GENERATOR"))
  378. {
  379. mf.AddCacheDefinition("CMAKE_GENERATOR",
  380. gen->GetName(),
  381. "Name of generator.",
  382. cmCacheManager::INTERNAL);
  383. }
  384. // setup CMAKE_ROOT and CMAKE_COMMAND
  385. this->AddCMakePaths(args);
  386. // compute system info
  387. gen->ComputeSystemInfo();
  388. // Transfer the cache into the makefile's definitions.
  389. cmCacheManager::GetInstance()->DefineCache(&mf);
  390. std::string lf = mf.GetStartDirectory();
  391. lf += "/CMakeLists.txt";
  392. if(!mf.ReadListFile(lf.c_str()))
  393. {
  394. this->Usage(args[0].c_str());
  395. return -1;
  396. }
  397. // if buildMakefiles, then call GenerateMakefile
  398. if(buildMakefiles)
  399. {
  400. mf.GenerateMakefile();
  401. }
  402. else // do not build, but let the commands finalize
  403. {
  404. std::vector<cmMakefile*> makefiles;
  405. mf.FindSubDirectoryCMakeListsFiles(makefiles);
  406. for(std::vector<cmMakefile*>::iterator i = makefiles.begin();
  407. i != makefiles.end(); ++i)
  408. {
  409. cmMakefile* mf = *i;
  410. mf->FinalPass();
  411. delete mf;
  412. }
  413. mf.FinalPass();
  414. }
  415. // Before saving the cache
  416. // if the project did not define one of the entries below, add them now
  417. // so users can edit the values in the cache:
  418. // LIBRARY_OUTPUT_PATH
  419. // EXECUTABLE_OUTPUT_PATH
  420. if(!cmCacheManager::GetInstance()->GetCacheValue("LIBRARY_OUTPUT_PATH"))
  421. {
  422. cmCacheManager::GetInstance()->AddCacheEntry("LIBRARY_OUTPUT_PATH", "",
  423. "Single output directory for building all libraries.",
  424. cmCacheManager::PATH);
  425. }
  426. if(!cmCacheManager::GetInstance()->GetCacheValue("EXECUTABLE_OUTPUT_PATH"))
  427. {
  428. cmCacheManager::GetInstance()->AddCacheEntry("EXECUTABLE_OUTPUT_PATH", "",
  429. "Single output directory for building all executables.",
  430. cmCacheManager::PATH);
  431. }
  432. cmCacheManager::GetInstance()->SaveCache(&mf);
  433. if(m_Verbose)
  434. {
  435. cmCacheManager::GetInstance()->PrintCache(std::cout);
  436. }
  437. if(cmSystemTools::GetErrorOccuredFlag())
  438. {
  439. return -1;
  440. }
  441. return 0;
  442. }