cmLocalGenerator.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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. if ( libOutPath.size() == 0 )
  112. {
  113. // LIBRARY_OUTPUT_PATH not defined
  114. libOutPath = currdir + "/";
  115. }
  116. if ( exeOutPath.size() == 0 )
  117. {
  118. // EXECUTABLE_OUTPUT_PATH not defined
  119. exeOutPath = currdir + "/";
  120. }
  121. std::string destination;
  122. for(cmTargets::const_iterator l = tgts.begin();
  123. l != tgts.end(); l++)
  124. {
  125. const char* preinstall = l->second.GetProperty("PRE_INSTALL_SCRIPT");
  126. const char* postinstall = l->second.GetProperty("POST_INSTALL_SCRIPT");
  127. if ( preinstall )
  128. {
  129. fout << "INCLUDE(\"" << preinstall << "\")" << std::endl;
  130. }
  131. if (l->second.GetInstallPath() != "")
  132. {
  133. destination = prefix + l->second.GetInstallPath();
  134. cmSystemTools::ConvertToUnixSlashes(destination);
  135. const char* dest = destination.c_str();
  136. int type = l->second.GetType();
  137. std::string fname;
  138. const char* files;
  139. // now install the target
  140. switch (type)
  141. {
  142. case cmTarget::STATIC_LIBRARY:
  143. case cmTarget::MODULE_LIBRARY:
  144. fname = libOutPath;
  145. fname += this->GetFullTargetName(l->first.c_str(), l->second);
  146. files = fname.c_str();
  147. this->AddInstallRule(fout, dest, type, files);
  148. break;
  149. case cmTarget::SHARED_LIBRARY:
  150. {
  151. // Special code to handle DLL
  152. fname = libOutPath;
  153. fname += this->GetFullTargetName(l->first.c_str(), l->second);
  154. std::string ext = cmSystemTools::GetFilenameExtension(fname);
  155. ext = cmSystemTools::LowerCase(ext);
  156. if ( ext == ".dll" )
  157. {
  158. std::string libname = libOutPath;
  159. libname += cmSystemTools::GetFilenameWithoutExtension(fname);
  160. libname += ".lib";
  161. files = libname.c_str();
  162. this->AddInstallRule(fout, dest, cmTarget::STATIC_LIBRARY, files, true);
  163. std::string dlldest = prefix + l->second.GetRuntimeInstallPath();
  164. files = fname.c_str();
  165. this->AddInstallRule(fout, dlldest.c_str(), type, files);
  166. }
  167. else
  168. {
  169. files = fname.c_str();
  170. this->AddInstallRule(fout, dest, type, files);
  171. }
  172. }
  173. break;
  174. case cmTarget::EXECUTABLE:
  175. fname = exeOutPath;
  176. fname += this->GetFullTargetName(l->first.c_str(), l->second);
  177. files = fname.c_str();
  178. this->AddInstallRule(fout, dest, type, files);
  179. break;
  180. case cmTarget::INSTALL_FILES:
  181. {
  182. std::string sourcePath = m_Makefile->GetCurrentDirectory();
  183. std::string binaryPath = m_Makefile->GetCurrentOutputDirectory();
  184. sourcePath += "/";
  185. binaryPath += "/";
  186. const std::vector<std::string> &sf = l->second.GetSourceLists();
  187. std::vector<std::string>::const_iterator i;
  188. for (i = sf.begin(); i != sf.end(); ++i)
  189. {
  190. std::string f = *i;
  191. if(f.substr(0, sourcePath.length()) == sourcePath)
  192. {
  193. f = f.substr(sourcePath.length());
  194. }
  195. else if(f.substr(0, binaryPath.length()) == binaryPath)
  196. {
  197. f = f.substr(binaryPath.length());
  198. }
  199. files = i->c_str();
  200. this->AddInstallRule(fout, dest, type, files);
  201. }
  202. }
  203. break;
  204. case cmTarget::INSTALL_PROGRAMS:
  205. {
  206. std::string sourcePath = m_Makefile->GetCurrentDirectory();
  207. std::string binaryPath = m_Makefile->GetCurrentOutputDirectory();
  208. sourcePath += "/";
  209. binaryPath += "/";
  210. const std::vector<std::string> &sf = l->second.GetSourceLists();
  211. std::vector<std::string>::const_iterator i;
  212. for (i = sf.begin(); i != sf.end(); ++i)
  213. {
  214. std::string f = *i;
  215. if(f.substr(0, sourcePath.length()) == sourcePath)
  216. {
  217. f = f.substr(sourcePath.length());
  218. }
  219. else if(f.substr(0, binaryPath.length()) == binaryPath)
  220. {
  221. f = f.substr(binaryPath.length());
  222. }
  223. files = i->c_str();
  224. this->AddInstallRule(fout, dest, type, files);
  225. }
  226. }
  227. break;
  228. case cmTarget::UTILITY:
  229. default:
  230. break;
  231. }
  232. }
  233. if ( postinstall )
  234. {
  235. fout << "INCLUDE(\"" << postinstall << "\")" << std::endl;
  236. }
  237. }
  238. cmMakefile* mf = this->GetMakefile();
  239. if ( !mf->GetSubDirectories().empty() )
  240. {
  241. const std::vector<std::pair<cmStdString, bool> >& subdirs = mf->GetSubDirectories();
  242. std::vector<std::pair<cmStdString, bool> >::const_iterator i = subdirs.begin();
  243. for(; i != subdirs.end(); ++i)
  244. {
  245. std::string odir = mf->GetCurrentOutputDirectory();
  246. odir += "/" + (*i).first;
  247. cmSystemTools::ConvertToUnixSlashes(odir);
  248. fout << "INCLUDE(\"" << odir.c_str()
  249. << "/cmake_install.cmake\")" << std::endl;
  250. }
  251. fout << std::endl;;
  252. }
  253. if ( toplevel_install )
  254. {
  255. fout << "FOREACH(file ${CMAKE_INSTALL_MANIFEST_FILES})" << std::endl
  256. << " FILE(APPEND \"" << homedir.c_str() << "/install_manifest.txt\" "
  257. << "\"${file}\\n\")" << std::endl
  258. << "ENDFOREACH(file)" << std::endl;
  259. }
  260. }
  261. void cmLocalGenerator::AddInstallRule(std::ostream& fout, const char* dest,
  262. int type, const char* files, bool optional)
  263. {
  264. std::string sfiles = files;
  265. std::string destination = dest;
  266. std::string stype;
  267. switch ( type )
  268. {
  269. case cmTarget::INSTALL_PROGRAMS: stype = "PROGRAM"; break;
  270. case cmTarget::EXECUTABLE: stype = "EXECUTABLE"; break;
  271. case cmTarget::STATIC_LIBRARY: stype = "STATIC_LIBRARY"; break;
  272. case cmTarget::SHARED_LIBRARY: stype = "SHARED_LIBRARY"; break;
  273. case cmTarget::MODULE_LIBRARY: stype = "MODULE"; break;
  274. case cmTarget::INSTALL_FILES:
  275. default: stype = "FILE"; break;
  276. }
  277. std::string fname = cmSystemTools::GetFilenameName(sfiles.c_str());
  278. fout
  279. << "MESSAGE(STATUS \"Installing " << destination.c_str()
  280. << "/" << fname.c_str() << "\")\n"
  281. << "FILE(INSTALL DESTINATION \"" << destination.c_str()
  282. << "\" TYPE " << stype.c_str() << (optional?" OPTIONAL":"")
  283. << " FILES \"" << sfiles.c_str() << "\")\n";
  284. }
  285. std::string cmLocalGenerator::GetFullTargetName(const char* n,
  286. const cmTarget& t)
  287. {
  288. const char* targetPrefix = t.GetProperty("PREFIX");
  289. const char* targetSuffix = t.GetProperty("SUFFIX");
  290. const char* prefixVar = 0;
  291. const char* suffixVar = 0;
  292. switch(t.GetType())
  293. {
  294. case cmTarget::STATIC_LIBRARY:
  295. prefixVar = "CMAKE_STATIC_LIBRARY_PREFIX";
  296. suffixVar = "CMAKE_STATIC_LIBRARY_SUFFIX";
  297. break;
  298. case cmTarget::SHARED_LIBRARY:
  299. prefixVar = "CMAKE_SHARED_LIBRARY_PREFIX";
  300. suffixVar = "CMAKE_SHARED_LIBRARY_SUFFIX";
  301. break;
  302. case cmTarget::MODULE_LIBRARY:
  303. prefixVar = "CMAKE_SHARED_MODULE_PREFIX";
  304. suffixVar = "CMAKE_SHARED_MODULE_SUFFIX";
  305. break;
  306. case cmTarget::EXECUTABLE:
  307. targetSuffix = cmSystemTools::GetExecutableExtension();
  308. case cmTarget::UTILITY:
  309. case cmTarget::INSTALL_FILES:
  310. case cmTarget::INSTALL_PROGRAMS:
  311. break;
  312. }
  313. // if there is no prefix on the target use the cmake definition
  314. if(!targetPrefix && prefixVar)
  315. {
  316. targetPrefix = m_Makefile->GetSafeDefinition(prefixVar);
  317. }
  318. // if there is no suffix on the target use the cmake definition
  319. if(!targetSuffix && suffixVar)
  320. {
  321. targetSuffix = m_Makefile->GetSafeDefinition(suffixVar);
  322. }
  323. std::string name = targetPrefix?targetPrefix:"";
  324. name += n;
  325. name += targetSuffix?targetSuffix:"";
  326. return name;
  327. }
  328. std::string cmLocalGenerator::ConvertToRelativeOutputPath(const char* p)
  329. {
  330. if ( !m_Makefile->IsOn("CMAKE_USE_RELATIVE_PATHS") )
  331. {
  332. return cmSystemTools::ConvertToOutputPath(p);
  333. }
  334. // do not use relative paths for network build trees
  335. // the network paths do not work
  336. const char* outputDirectory = m_Makefile->GetHomeOutputDirectory();
  337. if ( outputDirectory && *outputDirectory && *(outputDirectory+1) &&
  338. outputDirectory[0] == '/' && outputDirectory[1] == '/' )
  339. {
  340. return cmSystemTools::ConvertToOutputPath(p);
  341. }
  342. // The first time this is called, initialize all
  343. // the path ivars that are used. This can not
  344. // be moved to the constructor because all the paths are not set yet.
  345. if(m_CurrentOutputDirectory.size() == 0)
  346. {
  347. m_CurrentOutputDirectory = m_Makefile->GetCurrentOutputDirectory();
  348. m_HomeOutputDirectory = m_Makefile->GetHomeOutputDirectory();
  349. m_HomeDirectory = m_Makefile->GetHomeDirectory();
  350. if(m_RelativePathToSourceDir.size() == 0)
  351. {
  352. m_RelativePathToSourceDir = cmSystemTools::RelativePath(
  353. m_CurrentOutputDirectory.c_str(),
  354. m_HomeDirectory.c_str());
  355. std::string path = m_CurrentOutputDirectory;
  356. cmSystemTools::ReplaceString(path, m_HomeOutputDirectory.c_str(), "");
  357. unsigned i;
  358. m_RelativePathToBinaryDir = "";
  359. for(i =0; i < path.size(); ++i)
  360. {
  361. if(path[i] == '/')
  362. {
  363. m_RelativePathToBinaryDir += "../";
  364. }
  365. }
  366. }
  367. m_HomeOutputDirectoryNoSlash = m_HomeOutputDirectory;
  368. m_HomeOutputDirectory += "/";
  369. m_CurrentOutputDirectory += "/";
  370. }
  371. // Do the work of converting to a relative path
  372. std::string pathIn = p;
  373. if(pathIn.find('/') == pathIn.npos)
  374. {
  375. return pathIn;
  376. }
  377. if(pathIn.size() && pathIn[0] == '\"')
  378. {
  379. pathIn = pathIn.substr(1, pathIn.size()-2);
  380. }
  381. std::string ret = pathIn;
  382. if(m_CurrentOutputDirectory.size() <= ret.size())
  383. {
  384. std::string sub = ret.substr(0, m_CurrentOutputDirectory.size());
  385. if(
  386. #if defined(_WIN32) || defined(__APPLE__)
  387. cmSystemTools::LowerCase(sub) ==
  388. cmSystemTools::LowerCase(m_CurrentOutputDirectory)
  389. #else
  390. sub == m_CurrentOutputDirectory
  391. #endif
  392. )
  393. {
  394. ret = ret.substr(m_CurrentOutputDirectory.size(), ret.npos);
  395. }
  396. }
  397. if(m_HomeDirectory.size() <= ret.size())
  398. {
  399. std::string sub = ret.substr(0, m_HomeDirectory.size());
  400. if(
  401. #if defined(_WIN32) || defined(__APPLE__)
  402. cmSystemTools::LowerCase(sub) ==
  403. cmSystemTools::LowerCase(m_HomeDirectory)
  404. #else
  405. sub == m_HomeDirectory
  406. #endif
  407. )
  408. {
  409. ret = m_RelativePathToSourceDir + ret.substr(m_HomeDirectory.size(), ret.npos);
  410. }
  411. }
  412. if(m_HomeOutputDirectory.size() <= ret.size())
  413. {
  414. std::string sub = ret.substr(0, m_HomeOutputDirectory.size());
  415. if(
  416. #if defined(_WIN32) || defined(__APPLE__)
  417. cmSystemTools::LowerCase(sub) ==
  418. cmSystemTools::LowerCase(m_HomeOutputDirectory)
  419. #else
  420. sub == m_HomeOutputDirectory
  421. #endif
  422. )
  423. {
  424. ret = m_RelativePathToBinaryDir + ret.substr(m_HomeOutputDirectory.size(), ret.npos);
  425. }
  426. }
  427. std::string relpath = m_RelativePathToBinaryDir;
  428. if(relpath.size())
  429. {
  430. relpath.erase(relpath.size()-1, 1);
  431. }
  432. else
  433. {
  434. relpath = ".";
  435. }
  436. if(
  437. #if defined(_WIN32) || defined(__APPLE__)
  438. cmSystemTools::LowerCase(ret) ==
  439. cmSystemTools::LowerCase(m_HomeOutputDirectoryNoSlash)
  440. #else
  441. ret == m_HomeOutputDirectoryNoSlash
  442. #endif
  443. )
  444. {
  445. ret = relpath;
  446. }
  447. // Relative paths should always start in a '.', so add a './' if
  448. // necessary.
  449. if(ret.size()
  450. && ret[0] != '\"' && ret[0] != '/' && ret[0] != '.' && ret[0] != '$')
  451. {
  452. if(ret.size() > 1 && ret[1] != ':')
  453. {
  454. ret = std::string("./") + ret;
  455. }
  456. }
  457. ret = cmSystemTools::ConvertToOutputPath(ret.c_str());
  458. return ret;
  459. }