cmake.cxx 14 KB

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