cmake.cxx 8.6 KB

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