cmake.cxx 7.4 KB

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