cmake.cxx 7.2 KB

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