cmake.cxx 13 KB

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