cmake.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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() << "\n";
  57. std::cerr << "Usage: " << program << " [srcdir] [options]\n"
  58. << "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";
  59. std::cerr << "Options are:\n";
  60. std::cerr << "\n-i (puts cmake in wizard mode, not available for ccmake)\n";
  61. std::cerr << "\n-DVAR:TYPE=VALUE (create a cache file entry)\n";
  62. std::cerr << "\n-Cpath_to_initial_cache (a cmake list file that is used to pre-load the cache with values.)\n";
  63. std::cerr << "\n[-GgeneratorName] (where generator name can be one of these: ";
  64. std::vector<std::string> names;
  65. cmMakefileGenerator::GetRegisteredGenerators(names);
  66. for(std::vector<std::string>::iterator i =names.begin();
  67. i != names.end(); ++i)
  68. {
  69. std::cerr << "\"" << i->c_str() << "\" ";
  70. }
  71. std::cerr << ")\n";
  72. }
  73. // Parse the args
  74. void cmake::SetCacheArgs(cmMakefile& builder,
  75. const std::vector<std::string>& args)
  76. {
  77. for(unsigned int i=1; i < args.size(); ++i)
  78. {
  79. std::string arg = args[i];
  80. if(arg.find("-D",0) == 0)
  81. {
  82. std::string entry = arg.substr(2);
  83. std::string var, value;
  84. cmCacheManager::CacheEntryType type;
  85. if(cmCacheManager::ParseEntry(entry.c_str(), var, value, type))
  86. {
  87. cmCacheManager::GetInstance()->AddCacheEntry(
  88. var.c_str(),
  89. cmSystemTools::EscapeSpaces(value.c_str()).c_str(),
  90. "No help, variable specified on the command line.",
  91. type);
  92. }
  93. else
  94. {
  95. std::cerr << "Parse error in command line argument: " << arg << "\n"
  96. << "Should be: VAR:type=value\n";
  97. }
  98. }
  99. else if(arg.find("-C",0) == 0)
  100. {
  101. std::string path = arg.substr(2);
  102. std::cerr << "loading initial cache file " << path.c_str() << "\n";
  103. if(!builder.ReadListFile(path.c_str()))
  104. {
  105. std::cerr << "Error in reading cmake initial cache file:"
  106. << path.c_str() << "\n";
  107. }
  108. }
  109. }
  110. }
  111. // Parse the args
  112. void cmake::SetArgs(cmMakefile& builder, const std::vector<std::string>& args)
  113. {
  114. m_Local = false;
  115. bool directoriesSet = false;
  116. // watch for cmake and cmake srcdir invocations
  117. if (args.size() <= 2)
  118. {
  119. directoriesSet = true;
  120. builder.SetHomeOutputDirectory
  121. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  122. builder.SetStartOutputDirectory
  123. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  124. if (args.size() == 2)
  125. {
  126. builder.SetHomeDirectory
  127. (cmSystemTools::CollapseFullPath(args[1].c_str()).c_str());
  128. builder.SetStartDirectory
  129. (cmSystemTools::CollapseFullPath(args[1].c_str()).c_str());
  130. }
  131. else
  132. {
  133. builder.SetHomeDirectory
  134. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  135. builder.SetStartDirectory
  136. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  137. }
  138. }
  139. for(unsigned int i=1; i < args.size(); ++i)
  140. {
  141. std::string arg = args[i];
  142. if(arg.find("-H",0) == 0)
  143. {
  144. directoriesSet = true;
  145. std::string path = arg.substr(2);
  146. builder.SetHomeDirectory(path.c_str());
  147. }
  148. else if(arg.find("-S",0) == 0)
  149. {
  150. directoriesSet = true;
  151. m_Local = true;
  152. std::string path = arg.substr(2);
  153. builder.SetStartDirectory(path.c_str());
  154. }
  155. else if(arg.find("-O",0) == 0)
  156. {
  157. directoriesSet = true;
  158. std::string path = arg.substr(2);
  159. builder.SetStartOutputDirectory(path.c_str());
  160. }
  161. else if(arg.find("-B",0) == 0)
  162. {
  163. directoriesSet = true;
  164. std::string path = arg.substr(2);
  165. builder.SetHomeOutputDirectory(path.c_str());
  166. }
  167. else if(arg.find("-V",0) == 0)
  168. {
  169. m_Verbose = true;
  170. }
  171. else if(arg.find("-D",0) == 0)
  172. {
  173. // skip for now
  174. }
  175. else if(arg.find("-C",0) == 0)
  176. {
  177. // skip for now
  178. }
  179. else if(arg.find("-G",0) == 0)
  180. {
  181. std::string value = arg.substr(2);
  182. cmMakefileGenerator* gen =
  183. cmMakefileGenerator::CreateGenerator(value.c_str());
  184. if(!gen)
  185. {
  186. cmSystemTools::Error("Could not create named generator ",
  187. value.c_str());
  188. }
  189. else
  190. {
  191. builder.SetMakefileGenerator(gen);
  192. }
  193. }
  194. // no option assume it is the path to the source
  195. else
  196. {
  197. directoriesSet = true;
  198. builder.SetHomeOutputDirectory
  199. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  200. builder.SetStartOutputDirectory
  201. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  202. builder.SetHomeDirectory
  203. (cmSystemTools::CollapseFullPath(arg.c_str()).c_str());
  204. builder.SetStartDirectory
  205. (cmSystemTools::CollapseFullPath(arg.c_str()).c_str());
  206. }
  207. }
  208. if(!directoriesSet)
  209. {
  210. builder.SetHomeOutputDirectory
  211. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  212. builder.SetStartOutputDirectory
  213. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  214. builder.SetHomeDirectory
  215. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  216. builder.SetStartDirectory
  217. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  218. }
  219. if (!m_Local)
  220. {
  221. builder.SetStartDirectory(builder.GetHomeDirectory());
  222. builder.SetStartOutputDirectory(builder.GetHomeOutputDirectory());
  223. }
  224. }
  225. // at the end of this CMAKE_ROOT and CMAKE_COMMAND should be added to the cache
  226. void cmake::AddCMakePaths(const std::vector<std::string>& args)
  227. {
  228. // Find our own executable.
  229. std::string cMakeSelf = args[0];
  230. cmSystemTools::ConvertToUnixSlashes(cMakeSelf);
  231. cMakeSelf = cmSystemTools::FindProgram(cMakeSelf.c_str());
  232. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  233. {
  234. #ifdef CMAKE_BUILD_DIR
  235. cMakeSelf = CMAKE_BUILD_DIR;
  236. cMakeSelf += "/Source/cmake";
  237. #endif
  238. }
  239. #ifdef CMAKE_PREFIX
  240. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  241. {
  242. cMakeSelf = CMAKE_PREFIX "/bin/cmake";
  243. }
  244. #endif
  245. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  246. {
  247. cmSystemTools::Error("CMAKE can not find the command line program cmake. "
  248. "Attempted path: ", cMakeSelf.c_str());
  249. return;
  250. }
  251. // Save the value in the cache
  252. cmCacheManager::GetInstance()->AddCacheEntry
  253. ("CMAKE_COMMAND",
  254. cmSystemTools::EscapeSpaces(cMakeSelf.c_str()).c_str(),
  255. "Path to CMake executable.",
  256. cmCacheManager::INTERNAL);
  257. // do CMAKE_ROOT, look for the environment variable first
  258. std::string cMakeRoot;
  259. std::string modules;
  260. if (getenv("CMAKE_ROOT"))
  261. {
  262. cMakeRoot = getenv("CMAKE_ROOT");
  263. modules = cMakeRoot + "/Modules/FindVTK.cmake";
  264. }
  265. if(!cmSystemTools::FileExists(modules.c_str()))
  266. {
  267. // next try exe/..
  268. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  269. std::string::size_type slashPos = cMakeRoot.rfind("/");
  270. if(slashPos != std::string::npos)
  271. {
  272. cMakeRoot = cMakeRoot.substr(0, slashPos);
  273. }
  274. // is there no Modules direcory there?
  275. modules = cMakeRoot + "/Modules/FindVTK.cmake";
  276. }
  277. if (!cmSystemTools::FileExists(modules.c_str()))
  278. {
  279. // try exe/../share/cmake
  280. cMakeRoot += "/share/CMake";
  281. modules = cMakeRoot + "/Modules/FindVTK.cmake";
  282. }
  283. #ifdef CMAKE_ROOT_DIR
  284. if (!cmSystemTools::FileExists(modules.c_str()))
  285. {
  286. // try compiled in root directory
  287. cMakeRoot = CMAKE_ROOT_DIR;
  288. modules = cMakeRoot + "/Modules/FindVTK.cmake";
  289. }
  290. #endif
  291. #ifdef CMAKE_PREFIX
  292. if (!cmSystemTools::FileExists(modules.c_str()))
  293. {
  294. // try compiled in install prefix
  295. cMakeRoot = CMAKE_PREFIX "/share/CMake";
  296. modules = cMakeRoot + "/Modules/FindVTK.cmake";
  297. }
  298. #endif
  299. if (!cmSystemTools::FileExists(modules.c_str()))
  300. {
  301. // try
  302. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  303. cMakeRoot += "/share/CMake";
  304. modules = cMakeRoot + "/Modules/FindVTK.cmake";
  305. }
  306. if (!cmSystemTools::FileExists(modules.c_str()))
  307. {
  308. // couldn't find modules
  309. cmSystemTools::Error("Could not find CMAKE_ROOT !!!\n",
  310. "Modules directory not in directory:\n",
  311. modules.c_str());
  312. return;
  313. }
  314. cmCacheManager::GetInstance()->AddCacheEntry
  315. ("CMAKE_ROOT", cMakeRoot.c_str(),
  316. "Path to CMake installation.", cmCacheManager::INTERNAL);
  317. }
  318. int cmake::Generate(const std::vector<std::string>& args, bool buildMakefiles)
  319. {
  320. if(args.size() == 1 && !cmSystemTools::FileExists("CMakeLists.txt"))
  321. {
  322. this->Usage(args[0].c_str());
  323. return -1;
  324. }
  325. // look for obvious request for help
  326. for(unsigned int i=1; i < args.size(); ++i)
  327. {
  328. std::string arg = args[i];
  329. if(arg.find("-help",0) != std::string::npos ||
  330. arg.find("--help",0) != std::string::npos ||
  331. arg.find("/?",0) != std::string::npos ||
  332. arg.find("-usage",0) != std::string::npos)
  333. {
  334. this->Usage(args[0].c_str());
  335. return -1;
  336. }
  337. }
  338. // Create a makefile
  339. cmMakefile mf;
  340. // extract the directory arguments, could create a Generator
  341. this->SetArgs(mf, args);
  342. // Read and parse the input makefile
  343. mf.MakeStartDirectoriesCurrent();
  344. cmCacheManager::GetInstance()->LoadCache(&mf);
  345. // extract command line arguments that might add cache entries
  346. this->SetCacheArgs(mf, args);
  347. // no generator specified on the command line
  348. if(!mf.GetMakefileGenerator())
  349. {
  350. cmMakefileGenerator* gen;
  351. const char* genName = mf.GetDefinition("CMAKE_GENERATOR");
  352. if(genName)
  353. {
  354. gen = cmMakefileGenerator::CreateGenerator(genName);
  355. }
  356. else
  357. {
  358. #if defined(__BORLANDC__)
  359. gen = new cmBorlandMakefileGenerator;
  360. #elif defined(_WIN32) && !defined(__CYGWIN__)
  361. gen = new cmMSProjectGenerator;
  362. #else
  363. gen = new cmUnixMakefileGenerator;
  364. #endif
  365. }
  366. if(!gen)
  367. {
  368. cmSystemTools::Error("Could not create generator");
  369. return -1;
  370. }
  371. mf.SetMakefileGenerator(gen);
  372. // add the
  373. }
  374. cmMakefileGenerator* gen = mf.GetMakefileGenerator();
  375. gen->SetLocal(m_Local);
  376. if(!mf.GetDefinition("CMAKE_GENERATOR"))
  377. {
  378. mf.AddCacheDefinition("CMAKE_GENERATOR",
  379. gen->GetName(),
  380. "Name of generator.",
  381. cmCacheManager::INTERNAL);
  382. }
  383. // setup CMAKE_ROOT and CMAKE_COMMAND
  384. this->AddCMakePaths(args);
  385. // compute system info
  386. gen->ComputeSystemInfo();
  387. // Transfer the cache into the makefile's definitions.
  388. cmCacheManager::GetInstance()->DefineCache(&mf);
  389. std::string lf = mf.GetStartDirectory();
  390. lf += "/CMakeLists.txt";
  391. if(!mf.ReadListFile(lf.c_str()))
  392. {
  393. this->Usage(args[0].c_str());
  394. return -1;
  395. }
  396. // if buildMakefiles, then call GenerateMakefile
  397. if(buildMakefiles)
  398. {
  399. mf.GenerateMakefile();
  400. }
  401. else // do not build, but let the commands finalize
  402. {
  403. std::vector<cmMakefile*> makefiles;
  404. mf.FindSubDirectoryCMakeListsFiles(makefiles);
  405. for(std::vector<cmMakefile*>::iterator i = makefiles.begin();
  406. i != makefiles.end(); ++i)
  407. {
  408. cmMakefile* mf = *i;
  409. mf->FinalPass();
  410. delete mf;
  411. }
  412. mf.FinalPass();
  413. }
  414. // Before saving the cache
  415. // if the project did not define one of the entries below, add them now
  416. // so users can edit the values in the cache:
  417. // LIBRARY_OUTPUT_PATH
  418. // EXECUTABLE_OUTPUT_PATH
  419. if(!cmCacheManager::GetInstance()->GetCacheValue("LIBRARY_OUTPUT_PATH"))
  420. {
  421. cmCacheManager::GetInstance()->AddCacheEntry("LIBRARY_OUTPUT_PATH", "",
  422. "Single output directory for building all libraries.",
  423. cmCacheManager::PATH);
  424. }
  425. if(!cmCacheManager::GetInstance()->GetCacheValue("EXECUTABLE_OUTPUT_PATH"))
  426. {
  427. cmCacheManager::GetInstance()->AddCacheEntry("EXECUTABLE_OUTPUT_PATH", "",
  428. "Single output directory for building all executables.",
  429. cmCacheManager::PATH);
  430. }
  431. cmCacheManager::GetInstance()->SaveCache(&mf);
  432. if(m_Verbose)
  433. {
  434. cmCacheManager::GetInstance()->PrintCache(std::cout);
  435. }
  436. if(cmSystemTools::GetErrorOccuredFlag())
  437. {
  438. return -1;
  439. }
  440. return 0;
  441. }