cmake.cxx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. #else
  38. #include "cmUnixMakefileGenerator.h"
  39. #endif
  40. int main(int ac, char** av)
  41. {
  42. cmake foo;
  43. if(ac < 2)
  44. {
  45. foo.Usage(av[0]);
  46. return -1;
  47. }
  48. return foo.Generate(ac,av);
  49. }
  50. void cmake::Usage(const char* program)
  51. {
  52. std::cerr << "Usage: " << program << " srcdir "
  53. << "-Ssource_start_directory "
  54. << "-Ooutput_start_directory "
  55. << "-Hsource_home_directory "
  56. << "-Boutput_home_directory\n"
  57. << "Where start directories are the current place in the tree,"
  58. "and the home directories are the top.\n";
  59. }
  60. // Parse the args
  61. void cmake::SetArgs(cmMakefile& builder, int ac, char** av)
  62. {
  63. m_Local = false;
  64. // watch for cmake and cmake srcdir invocations
  65. if (ac <= 2)
  66. {
  67. std::string ;
  68. builder.SetHomeOutputDirectory
  69. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  70. builder.SetStartOutputDirectory
  71. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  72. if (ac == 2)
  73. {
  74. std::string hdir = cmSystemTools::GetCurrentWorkingDirectory()
  75. + "/" + av[1];
  76. builder.SetHomeDirectory
  77. (cmSystemTools::CollapseFullPath(hdir.c_str()).c_str());
  78. builder.SetStartDirectory
  79. (cmSystemTools::CollapseFullPath(hdir.c_str()).c_str());
  80. }
  81. else
  82. {
  83. builder.SetHomeDirectory
  84. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  85. builder.SetStartDirectory
  86. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  87. }
  88. }
  89. for(int i =1; i < ac; i++)
  90. {
  91. std::string arg = av[i];
  92. if(arg.find("-H",0) != std::string::npos)
  93. {
  94. std::string path = arg.substr(2);
  95. builder.SetHomeDirectory(path.c_str());
  96. }
  97. if(arg.find("-S",0) != std::string::npos)
  98. {
  99. m_Local = true;
  100. std::string path = arg.substr(2);
  101. builder.SetStartDirectory(path.c_str());
  102. }
  103. if(arg.find("-O",0) != std::string::npos)
  104. {
  105. std::string path = arg.substr(2);
  106. builder.SetStartOutputDirectory(path.c_str());
  107. }
  108. if(arg.find("-B",0) != std::string::npos)
  109. {
  110. std::string path = arg.substr(2);
  111. builder.SetHomeOutputDirectory(path.c_str());
  112. }
  113. if(arg.find("-D",0) == 0)
  114. {
  115. std::string value = arg.substr(2);
  116. builder.AddDefinition(value.c_str(), true);
  117. }
  118. if(arg.find("-V",0) == 0)
  119. {
  120. m_Verbose = true;
  121. }
  122. }
  123. if (!m_Local)
  124. {
  125. builder.SetStartDirectory(builder.GetHomeDirectory());
  126. builder.SetStartOutputDirectory(builder.GetHomeOutputDirectory());
  127. }
  128. }
  129. // at the end of this CMAKE_ROOT and CMAAKE_COMMAND should be added to the cache
  130. void cmake::AddCMakePaths(char **av)
  131. {
  132. // Find our own exectuable.
  133. #if defined(_WIN32) && !defined(__CYGWIN__)
  134. std::string cMakeSelf = "\""+cmSystemTools::FindProgram(av[0])+"\"";
  135. #else
  136. std::string cMakeSelf = cmSystemTools::FindProgram(av[0]);
  137. #endif
  138. // Save the value in the cache
  139. cmCacheManager::GetInstance()->AddCacheEntry("CMAKE_COMMAND",
  140. cMakeSelf.c_str(),
  141. "Path to CMake executable.",
  142. cmCacheManager::INTERNAL);
  143. // do CMAKE_ROOT, look for the environment variable first
  144. std::string cMakeRoot;
  145. if (getenv("CMAKE_ROOT"))
  146. {
  147. cMakeRoot = getenv("CMAKE_ROOT");
  148. }
  149. else
  150. {
  151. // next try exe/..
  152. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  153. std::string::size_type slashPos = cMakeRoot.rfind("/");
  154. if(slashPos != std::string::npos)
  155. {
  156. cMakeRoot = cMakeRoot.substr(0, slashPos);
  157. }
  158. // is there no Modules direcory there?
  159. std::string modules = cMakeRoot + "/Modules";
  160. if (!cmSystemTools::FileIsDirectory(modules.c_str()))
  161. {
  162. // try exe/../share/cmake
  163. modules = cMakeRoot + "/share/CMake/Modules";
  164. if (!cmSystemTools::FileIsDirectory(modules.c_str()))
  165. {
  166. #if !defined(_WIN32) || defined(__CYGWIN__)
  167. // try compiled in value on UNIX
  168. cMakeRoot = CMAKE_ROOT_DIR;
  169. modules = cMakeRoot + "/Modules";
  170. #endif
  171. if (!cmSystemTools::FileIsDirectory(modules.c_str()))
  172. {
  173. // couldn't find modules
  174. cmSystemTools::Error("Could not find CMAKE_ROOT !!!");
  175. return;
  176. }
  177. }
  178. else
  179. {
  180. cMakeRoot = cMakeRoot + "/share/CMake";
  181. }
  182. }
  183. }
  184. cmCacheManager::GetInstance()->AddCacheEntry
  185. ("CMAKE_ROOT", cMakeRoot.c_str(),
  186. "Path to CMake installation.", cmCacheManager::INTERNAL);
  187. }
  188. int cmake::Generate(int ac, char **av)
  189. {
  190. // Create a makefile
  191. cmMakefile mf;
  192. // extract the directory arguments
  193. cmake::SetArgs(mf, ac, av);
  194. // create the generator
  195. #if defined(_WIN32) && !defined(__CYGWIN__)
  196. cmMSProjectGenerator* gen = new cmMSProjectGenerator;
  197. #else
  198. cmUnixMakefileGenerator* gen = new cmUnixMakefileGenerator;
  199. #endif
  200. gen->SetLocal(m_Local);
  201. // Read and parse the input makefile
  202. mf.SetMakefileGenerator(gen);
  203. mf.MakeStartDirectoriesCurrent();
  204. cmCacheManager::GetInstance()->LoadCache(&mf);
  205. // setup CMAKE_ROOT and CMAKE_COMMAND
  206. this->AddCMakePaths(av);
  207. // compute system info
  208. gen->ComputeSystemInfo();
  209. // Transfer the cache into the makefile's definitions.
  210. cmCacheManager::GetInstance()->DefineCache(&mf);
  211. std::string lf = mf.GetStartDirectory();
  212. lf += "/CMakeLists.txt";
  213. if(!mf.ReadListFile(lf.c_str()))
  214. {
  215. this->Usage(av[0]);
  216. return -1;
  217. }
  218. mf.GenerateMakefile();
  219. cmCacheManager::GetInstance()->SaveCache(&mf);
  220. if(m_Verbose)
  221. {
  222. cmCacheManager::GetInstance()->PrintCache(std::cout);
  223. }
  224. if(cmSystemTools::GetErrorOccuredFlag())
  225. {
  226. return -1;
  227. }
  228. return 0;
  229. }