cmake.cxx 13 KB

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