cmake.cxx 17 KB

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