cmLocalGenerator.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmLocalGenerator.h"
  14. #include "cmGlobalGenerator.h"
  15. #include "cmake.h"
  16. #include "cmMakefile.h"
  17. #include "cmGeneratedFileStream.h"
  18. cmLocalGenerator::cmLocalGenerator()
  19. {
  20. m_Makefile = new cmMakefile;
  21. m_Makefile->SetLocalGenerator(this);
  22. m_ExcludeFromAll = false;
  23. m_Parent = 0;
  24. }
  25. cmLocalGenerator::~cmLocalGenerator()
  26. {
  27. delete m_Makefile;
  28. }
  29. void cmLocalGenerator::Configure()
  30. {
  31. // set the PROJECT_SOURCE_DIR and PROJECT_BIN_DIR to default values
  32. // just in case the project does not include a PROJECT command
  33. m_Makefile->AddDefinition("PROJECT_BINARY_DIR",
  34. m_Makefile->GetHomeOutputDirectory());
  35. m_Makefile->AddDefinition("PROJECT_SOURCE_DIR",
  36. m_Makefile->GetHomeDirectory());
  37. // find & read the list file
  38. std::string currentStart = m_Makefile->GetStartDirectory();
  39. currentStart += "/CMakeLists.txt";
  40. m_Makefile->ReadListFile(currentStart.c_str());
  41. }
  42. void cmLocalGenerator::SetGlobalGenerator(cmGlobalGenerator *gg)
  43. {
  44. m_GlobalGenerator = gg;
  45. // setup the home directories
  46. m_Makefile->SetHomeDirectory(
  47. gg->GetCMakeInstance()->GetHomeDirectory());
  48. m_Makefile->SetHomeOutputDirectory(
  49. gg->GetCMakeInstance()->GetHomeOutputDirectory());
  50. }
  51. void cmLocalGenerator::ConfigureFinalPass()
  52. {
  53. m_Makefile->ConfigureFinalPass();
  54. }
  55. void cmLocalGenerator::GenerateInstallRules()
  56. {
  57. const cmTargets &tgts = m_Makefile->GetTargets();
  58. const char* prefix
  59. = m_Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  60. if (!prefix)
  61. {
  62. prefix = "/usr/local";
  63. }
  64. std::string file = m_Makefile->GetStartOutputDirectory();
  65. std::string homedir = m_Makefile->GetHomeOutputDirectory();
  66. std::string currdir = m_Makefile->GetCurrentOutputDirectory();
  67. cmSystemTools::ConvertToUnixSlashes(file);
  68. cmSystemTools::ConvertToUnixSlashes(homedir);
  69. cmSystemTools::ConvertToUnixSlashes(currdir);
  70. int toplevel_install = 0;
  71. if ( currdir == homedir )
  72. {
  73. toplevel_install = 1;
  74. }
  75. file += "/cmake_install.cmake";
  76. cmGeneratedFileStream tempFile(file.c_str());
  77. std::ostream& fout = tempFile.GetStream();
  78. fout << "# Install script for directory: " << m_Makefile->GetCurrentDirectory()
  79. << std::endl << std::endl;
  80. const char* cmakeDebugPosfix = m_Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX");
  81. if ( cmakeDebugPosfix )
  82. {
  83. fout << "SET(CMAKE_DEBUG_POSTFIX \"" << cmakeDebugPosfix << "\")"
  84. << std::endl << std::endl;
  85. }
  86. std::string libOutPath = "";
  87. if (m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  88. {
  89. libOutPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  90. if(libOutPath.size())
  91. {
  92. if(libOutPath[libOutPath.size() -1] != '/')
  93. {
  94. libOutPath += "/";
  95. }
  96. }
  97. }
  98. std::string exeOutPath = "";
  99. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  100. {
  101. exeOutPath =
  102. m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  103. if(exeOutPath.size())
  104. {
  105. if(exeOutPath[exeOutPath.size() -1] != '/')
  106. {
  107. exeOutPath += "/";
  108. }
  109. }
  110. }
  111. std::string destination;
  112. for(cmTargets::const_iterator l = tgts.begin();
  113. l != tgts.end(); l++)
  114. {
  115. const char* preinstall = l->second.GetProperty("PRE_INSTALL_SCRIPT");
  116. const char* postinstall = l->second.GetProperty("POST_INSTALL_SCRIPT");
  117. if ( preinstall )
  118. {
  119. fout << "INCLUDE(\"" << preinstall << "\")" << std::endl;
  120. }
  121. if (l->second.GetInstallPath() != "")
  122. {
  123. destination = prefix + l->second.GetInstallPath();
  124. cmSystemTools::ConvertToUnixSlashes(destination);
  125. const char* dest = destination.c_str();
  126. int type = l->second.GetType();
  127. std::string fname;
  128. const char* files;
  129. // now install the target
  130. switch (type)
  131. {
  132. case cmTarget::STATIC_LIBRARY:
  133. case cmTarget::MODULE_LIBRARY:
  134. fname = libOutPath;
  135. fname += this->GetFullTargetName(l->first.c_str(), l->second);
  136. files = fname.c_str();
  137. this->AddInstallRule(fout, dest, type, files);
  138. break;
  139. case cmTarget::SHARED_LIBRARY:
  140. {
  141. // Special code to handle DLL
  142. fname = libOutPath;
  143. fname += this->GetFullTargetName(l->first.c_str(), l->second);
  144. std::string ext = cmSystemTools::GetFilenameExtension(fname);
  145. ext = cmSystemTools::LowerCase(ext);
  146. if ( ext == ".dll" )
  147. {
  148. std::string libname = libOutPath;
  149. libname += cmSystemTools::GetFilenameWithoutExtension(fname);
  150. libname += ".lib";
  151. files = libname.c_str();
  152. this->AddInstallRule(fout, dest, cmTarget::STATIC_LIBRARY, files, true);
  153. std::string dlldest = prefix + l->second.GetRuntimeInstallPath();
  154. files = fname.c_str();
  155. this->AddInstallRule(fout, dlldest.c_str(), type, files);
  156. }
  157. else
  158. {
  159. files = fname.c_str();
  160. this->AddInstallRule(fout, dest, type, files);
  161. }
  162. }
  163. break;
  164. case cmTarget::EXECUTABLE:
  165. fname = exeOutPath;
  166. fname += this->GetFullTargetName(l->first.c_str(), l->second);
  167. files = fname.c_str();
  168. this->AddInstallRule(fout, dest, type, files);
  169. break;
  170. case cmTarget::INSTALL_FILES:
  171. {
  172. std::string sourcePath = m_Makefile->GetCurrentDirectory();
  173. std::string binaryPath = m_Makefile->GetCurrentOutputDirectory();
  174. sourcePath += "/";
  175. binaryPath += "/";
  176. const std::vector<std::string> &sf = l->second.GetSourceLists();
  177. std::vector<std::string>::const_iterator i;
  178. for (i = sf.begin(); i != sf.end(); ++i)
  179. {
  180. std::string f = *i;
  181. if(f.substr(0, sourcePath.length()) == sourcePath)
  182. {
  183. f = f.substr(sourcePath.length());
  184. }
  185. else if(f.substr(0, binaryPath.length()) == binaryPath)
  186. {
  187. f = f.substr(binaryPath.length());
  188. }
  189. files = i->c_str();
  190. this->AddInstallRule(fout, dest, type, files);
  191. }
  192. }
  193. break;
  194. case cmTarget::INSTALL_PROGRAMS:
  195. {
  196. std::string sourcePath = m_Makefile->GetCurrentDirectory();
  197. std::string binaryPath = m_Makefile->GetCurrentOutputDirectory();
  198. sourcePath += "/";
  199. binaryPath += "/";
  200. const std::vector<std::string> &sf = l->second.GetSourceLists();
  201. std::vector<std::string>::const_iterator i;
  202. for (i = sf.begin(); i != sf.end(); ++i)
  203. {
  204. std::string f = *i;
  205. if(f.substr(0, sourcePath.length()) == sourcePath)
  206. {
  207. f = f.substr(sourcePath.length());
  208. }
  209. else if(f.substr(0, binaryPath.length()) == binaryPath)
  210. {
  211. f = f.substr(binaryPath.length());
  212. }
  213. files = i->c_str();
  214. this->AddInstallRule(fout, dest, type, files);
  215. }
  216. }
  217. break;
  218. case cmTarget::UTILITY:
  219. default:
  220. break;
  221. }
  222. }
  223. if ( postinstall )
  224. {
  225. fout << "INCLUDE(\"" << postinstall << "\")" << std::endl;
  226. }
  227. }
  228. cmMakefile* mf = this->GetMakefile();
  229. if ( !mf->GetSubDirectories().empty() )
  230. {
  231. const std::vector<std::pair<cmStdString, bool> >& subdirs = mf->GetSubDirectories();
  232. std::vector<std::pair<cmStdString, bool> >::const_iterator i = subdirs.begin();
  233. for(; i != subdirs.end(); ++i)
  234. {
  235. std::string odir = mf->GetCurrentOutputDirectory();
  236. odir += "/" + (*i).first;
  237. cmSystemTools::ConvertToUnixSlashes(odir);
  238. fout << "INCLUDE(\"" << odir.c_str()
  239. << "/cmake_install.cmake\")" << std::endl;
  240. }
  241. fout << std::endl;;
  242. }
  243. if ( toplevel_install )
  244. {
  245. fout << "FOREACH(file ${CMAKE_INSTALL_MANIFEST_FILES})" << std::endl
  246. << " FILE(APPEND \"" << homedir.c_str() << "/install_manifest.txt\" "
  247. << "\"${file}\\n\")" << std::endl
  248. << "ENDFOREACH(file)" << std::endl;
  249. }
  250. }
  251. void cmLocalGenerator::AddInstallRule(std::ostream& fout, const char* dest,
  252. int type, const char* files, bool optional)
  253. {
  254. std::string sfiles = files;
  255. std::string destination = dest;
  256. std::string stype;
  257. switch ( type )
  258. {
  259. case cmTarget::INSTALL_PROGRAMS: stype = "PROGRAM"; break;
  260. case cmTarget::EXECUTABLE: stype = "EXECUTABLE"; break;
  261. case cmTarget::STATIC_LIBRARY: stype = "STATIC_LIBRARY"; break;
  262. case cmTarget::SHARED_LIBRARY: stype = "SHARED_LIBRARY"; break;
  263. case cmTarget::MODULE_LIBRARY: stype = "MODULE"; break;
  264. case cmTarget::INSTALL_FILES:
  265. default: stype = "FILE"; break;
  266. }
  267. std::string fname = cmSystemTools::GetFilenameName(sfiles.c_str());
  268. fout
  269. << "MESSAGE(STATUS \"Installing " << destination.c_str()
  270. << "/" << fname.c_str() << "\")\n"
  271. << "FILE(INSTALL DESTINATION \"" << destination.c_str()
  272. << "\" TYPE " << stype.c_str() << (optional?" OPTIONAL":"")
  273. << " FILES \"" << sfiles.c_str() << "\")\n";
  274. }
  275. std::string cmLocalGenerator::GetFullTargetName(const char* n,
  276. const cmTarget& t)
  277. {
  278. const char* targetPrefix = t.GetProperty("PREFIX");
  279. const char* targetSuffix = t.GetProperty("SUFFIX");
  280. const char* prefixVar = 0;
  281. const char* suffixVar = 0;
  282. switch(t.GetType())
  283. {
  284. case cmTarget::STATIC_LIBRARY:
  285. prefixVar = "CMAKE_STATIC_LIBRARY_PREFIX";
  286. suffixVar = "CMAKE_STATIC_LIBRARY_SUFFIX";
  287. break;
  288. case cmTarget::SHARED_LIBRARY:
  289. prefixVar = "CMAKE_SHARED_LIBRARY_PREFIX";
  290. suffixVar = "CMAKE_SHARED_LIBRARY_SUFFIX";
  291. break;
  292. case cmTarget::MODULE_LIBRARY:
  293. prefixVar = "CMAKE_SHARED_MODULE_PREFIX";
  294. suffixVar = "CMAKE_SHARED_MODULE_SUFFIX";
  295. break;
  296. case cmTarget::EXECUTABLE:
  297. targetSuffix = cmSystemTools::GetExecutableExtension();
  298. case cmTarget::UTILITY:
  299. case cmTarget::INSTALL_FILES:
  300. case cmTarget::INSTALL_PROGRAMS:
  301. break;
  302. }
  303. // if there is no prefix on the target use the cmake definition
  304. if(!targetPrefix && prefixVar)
  305. {
  306. targetPrefix = m_Makefile->GetSafeDefinition(prefixVar);
  307. }
  308. // if there is no suffix on the target use the cmake definition
  309. if(!targetSuffix && suffixVar)
  310. {
  311. targetSuffix = m_Makefile->GetSafeDefinition(suffixVar);
  312. }
  313. std::string name = targetPrefix?targetPrefix:"";
  314. name += n;
  315. name += targetSuffix?targetSuffix:"";
  316. return name;
  317. }
  318. std::string cmLocalGenerator::ConvertToRelativeOutputPath(const char* p)
  319. {
  320. if ( !m_Makefile->IsOn("CMAKE_USE_RELATIVE_PATHS") )
  321. {
  322. return cmSystemTools::ConvertToOutputPath(p);
  323. }
  324. // do not use relative paths for network build trees
  325. // the network paths do not work
  326. const char* outputDirectory = m_Makefile->GetHomeOutputDirectory();
  327. if ( outputDirectory && *outputDirectory && *(outputDirectory+1) &&
  328. outputDirectory[0] == '/' && outputDirectory[1] == '/' )
  329. {
  330. return cmSystemTools::ConvertToOutputPath(p);
  331. }
  332. // The first time this is called, initialize all
  333. // the path ivars that are used. This can not
  334. // be moved to the constructor because all the paths are not set yet.
  335. if(m_CurrentOutputDirectory.size() == 0)
  336. {
  337. m_CurrentOutputDirectory = m_Makefile->GetCurrentOutputDirectory();
  338. m_HomeOutputDirectory = m_Makefile->GetHomeOutputDirectory();
  339. m_HomeDirectory = m_Makefile->GetHomeDirectory();
  340. if(m_RelativePathToSourceDir.size() == 0)
  341. {
  342. m_RelativePathToSourceDir = cmSystemTools::RelativePath(
  343. m_CurrentOutputDirectory.c_str(),
  344. m_HomeDirectory.c_str());
  345. std::string path = m_CurrentOutputDirectory;
  346. cmSystemTools::ReplaceString(path, m_HomeOutputDirectory.c_str(), "");
  347. unsigned i;
  348. m_RelativePathToBinaryDir = "";
  349. for(i =0; i < path.size(); ++i)
  350. {
  351. if(path[i] == '/')
  352. {
  353. m_RelativePathToBinaryDir += "../";
  354. }
  355. }
  356. }
  357. m_HomeOutputDirectoryNoSlash = m_HomeOutputDirectory;
  358. m_HomeOutputDirectory += "/";
  359. m_CurrentOutputDirectory += "/";
  360. }
  361. // Do the work of converting to a relative path
  362. std::string pathIn = p;
  363. if(pathIn.find('/') == pathIn.npos)
  364. {
  365. return pathIn;
  366. }
  367. if(pathIn.size() && pathIn[0] == '\"')
  368. {
  369. pathIn = pathIn.substr(1, pathIn.size()-2);
  370. }
  371. std::string ret = pathIn;
  372. if(m_CurrentOutputDirectory.size() <= ret.size())
  373. {
  374. std::string sub = ret.substr(0, m_CurrentOutputDirectory.size());
  375. if(
  376. #if defined(_WIN32) || defined(__APPLE__)
  377. cmSystemTools::LowerCase(sub) ==
  378. cmSystemTools::LowerCase(m_CurrentOutputDirectory)
  379. #else
  380. sub == m_CurrentOutputDirectory
  381. #endif
  382. )
  383. {
  384. ret = ret.substr(m_CurrentOutputDirectory.size(), ret.npos);
  385. }
  386. }
  387. if(m_HomeDirectory.size() <= ret.size())
  388. {
  389. std::string sub = ret.substr(0, m_HomeDirectory.size());
  390. if(
  391. #if defined(_WIN32) || defined(__APPLE__)
  392. cmSystemTools::LowerCase(sub) ==
  393. cmSystemTools::LowerCase(m_HomeDirectory)
  394. #else
  395. sub == m_HomeDirectory
  396. #endif
  397. )
  398. {
  399. ret = m_RelativePathToSourceDir + ret.substr(m_HomeDirectory.size(), ret.npos);
  400. }
  401. }
  402. if(m_HomeOutputDirectory.size() <= ret.size())
  403. {
  404. std::string sub = ret.substr(0, m_HomeOutputDirectory.size());
  405. if(
  406. #if defined(_WIN32) || defined(__APPLE__)
  407. cmSystemTools::LowerCase(sub) ==
  408. cmSystemTools::LowerCase(m_HomeOutputDirectory)
  409. #else
  410. sub == m_HomeOutputDirectory
  411. #endif
  412. )
  413. {
  414. ret = m_RelativePathToBinaryDir + ret.substr(m_HomeOutputDirectory.size(), ret.npos);
  415. }
  416. }
  417. std::string relpath = m_RelativePathToBinaryDir;
  418. if(relpath.size())
  419. {
  420. relpath.erase(relpath.size()-1, 1);
  421. }
  422. else
  423. {
  424. relpath = ".";
  425. }
  426. if(
  427. #if defined(_WIN32) || defined(__APPLE__)
  428. cmSystemTools::LowerCase(ret) ==
  429. cmSystemTools::LowerCase(m_HomeOutputDirectoryNoSlash)
  430. #else
  431. ret == m_HomeOutputDirectoryNoSlash
  432. #endif
  433. )
  434. {
  435. ret = relpath;
  436. }
  437. // Relative paths should always start in a '.', so add a './' if
  438. // necessary.
  439. if(ret.size()
  440. && ret[0] != '\"' && ret[0] != '/' && ret[0] != '.' && ret[0] != '$')
  441. {
  442. if(ret.size() > 1 && ret[1] != ':')
  443. {
  444. ret = std::string("./") + ret;
  445. }
  446. }
  447. ret = cmSystemTools::ConvertToOutputPath(ret.c_str());
  448. return ret;
  449. }