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. std::string destination;
  112. for(cmTargets::const_iterator l = tgts.begin();
  113. l != tgts.end(); l++)
  114. {
  115. if (l->second.GetInstallPath() != "")
  116. {
  117. destination = prefix + l->second.GetInstallPath();
  118. cmSystemTools::ConvertToUnixSlashes(destination);
  119. const char* dest = destination.c_str();
  120. int type = l->second.GetType();
  121. const char* preinstall = l->second.GetProperty("PRE_INSTALL_SCRIPT");
  122. const char* postinstall = l->second.GetProperty("POST_INSTALL_SCRIPT");
  123. if ( preinstall )
  124. {
  125. fout << "INCLUDE(\"" << preinstall << "\")" << std::endl;
  126. }
  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. if ( postinstall )
  223. {
  224. fout << "INCLUDE(\"" << postinstall << "\")" << std::endl;
  225. }
  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. const char* cmLocalGenerator::GetSafeDefinition(const char* def)
  276. {
  277. const char* ret = m_Makefile->GetDefinition(def);
  278. if(!ret)
  279. {
  280. return "";
  281. }
  282. return ret;
  283. }
  284. std::string cmLocalGenerator::GetFullTargetName(const char* n,
  285. const cmTarget& t)
  286. {
  287. const char* targetPrefix = t.GetProperty("PREFIX");
  288. const char* targetSuffix = t.GetProperty("SUFFIX");
  289. const char* prefixVar = 0;
  290. const char* suffixVar = 0;
  291. switch(t.GetType())
  292. {
  293. case cmTarget::STATIC_LIBRARY:
  294. prefixVar = "CMAKE_STATIC_LIBRARY_PREFIX";
  295. suffixVar = "CMAKE_STATIC_LIBRARY_SUFFIX";
  296. break;
  297. case cmTarget::SHARED_LIBRARY:
  298. prefixVar = "CMAKE_SHARED_LIBRARY_PREFIX";
  299. suffixVar = "CMAKE_SHARED_LIBRARY_SUFFIX";
  300. break;
  301. case cmTarget::MODULE_LIBRARY:
  302. prefixVar = "CMAKE_SHARED_MODULE_PREFIX";
  303. suffixVar = "CMAKE_SHARED_MODULE_SUFFIX";
  304. break;
  305. case cmTarget::EXECUTABLE:
  306. targetSuffix = cmSystemTools::GetExecutableExtension();
  307. case cmTarget::UTILITY:
  308. case cmTarget::INSTALL_FILES:
  309. case cmTarget::INSTALL_PROGRAMS:
  310. break;
  311. }
  312. // if there is no prefix on the target use the cmake definition
  313. if(!targetPrefix && prefixVar)
  314. {
  315. targetPrefix = this->GetSafeDefinition(prefixVar);
  316. }
  317. // if there is no suffix on the target use the cmake definition
  318. if(!targetSuffix && suffixVar)
  319. {
  320. targetSuffix = this->GetSafeDefinition(suffixVar);
  321. }
  322. std::string name = targetPrefix?targetPrefix:"";
  323. name += n;
  324. name += targetSuffix?targetSuffix:"";
  325. return name;
  326. }
  327. std::string cmLocalGenerator::ConvertToRelativeOutputPath(const char* p)
  328. {
  329. if ( !m_Makefile->IsOn("CMAKE_USE_RELATIVE_PATHS") )
  330. {
  331. return cmSystemTools::ConvertToOutputPath(p);
  332. }
  333. // do not use relative paths for network build trees
  334. // the network paths do not work
  335. const char* outputDirectory = m_Makefile->GetHomeOutputDirectory();
  336. if ( outputDirectory && *outputDirectory && *(outputDirectory+1) &&
  337. outputDirectory[0] == '/' && outputDirectory[1] == '/' )
  338. {
  339. return cmSystemTools::ConvertToOutputPath(p);
  340. }
  341. // The first time this is called, initialize all
  342. // the path ivars that are used. This can not
  343. // be moved to the constructor because all the paths are not set yet.
  344. if(m_CurrentOutputDirectory.size() == 0)
  345. {
  346. m_CurrentOutputDirectory = m_Makefile->GetCurrentOutputDirectory();
  347. m_HomeOutputDirectory = m_Makefile->GetHomeOutputDirectory();
  348. m_HomeDirectory = m_Makefile->GetHomeDirectory();
  349. if(m_RelativePathToSourceDir.size() == 0)
  350. {
  351. m_RelativePathToSourceDir = cmSystemTools::RelativePath(
  352. m_CurrentOutputDirectory.c_str(),
  353. m_HomeDirectory.c_str());
  354. std::string path = m_CurrentOutputDirectory;
  355. cmSystemTools::ReplaceString(path, m_HomeOutputDirectory.c_str(), "");
  356. unsigned i;
  357. m_RelativePathToBinaryDir = "";
  358. for(i =0; i < path.size(); ++i)
  359. {
  360. if(path[i] == '/')
  361. {
  362. m_RelativePathToBinaryDir += "../";
  363. }
  364. }
  365. }
  366. m_HomeOutputDirectoryNoSlash = m_HomeOutputDirectory;
  367. m_HomeOutputDirectory += "/";
  368. m_CurrentOutputDirectory += "/";
  369. }
  370. // Do the work of converting to a relative path
  371. std::string pathIn = p;
  372. if(pathIn.find('/') == pathIn.npos)
  373. {
  374. return pathIn;
  375. }
  376. if(pathIn.size() && pathIn[0] == '\"')
  377. {
  378. pathIn = pathIn.substr(1, pathIn.size()-2);
  379. }
  380. std::string ret = pathIn;
  381. if(m_CurrentOutputDirectory.size() <= ret.size())
  382. {
  383. std::string sub = ret.substr(0, m_CurrentOutputDirectory.size());
  384. if(
  385. #if defined(_WIN32) || defined(__APPLE__)
  386. cmSystemTools::LowerCase(sub) ==
  387. cmSystemTools::LowerCase(m_CurrentOutputDirectory)
  388. #else
  389. sub == m_CurrentOutputDirectory
  390. #endif
  391. )
  392. {
  393. ret = ret.substr(m_CurrentOutputDirectory.size(), ret.npos);
  394. }
  395. }
  396. if(m_HomeDirectory.size() <= ret.size())
  397. {
  398. std::string sub = ret.substr(0, m_HomeDirectory.size());
  399. if(
  400. #if defined(_WIN32) || defined(__APPLE__)
  401. cmSystemTools::LowerCase(sub) ==
  402. cmSystemTools::LowerCase(m_HomeDirectory)
  403. #else
  404. sub == m_HomeDirectory
  405. #endif
  406. )
  407. {
  408. ret = m_RelativePathToSourceDir + ret.substr(m_HomeDirectory.size(), ret.npos);
  409. }
  410. }
  411. if(m_HomeOutputDirectory.size() <= ret.size())
  412. {
  413. std::string sub = ret.substr(0, m_HomeOutputDirectory.size());
  414. if(
  415. #if defined(_WIN32) || defined(__APPLE__)
  416. cmSystemTools::LowerCase(sub) ==
  417. cmSystemTools::LowerCase(m_HomeOutputDirectory)
  418. #else
  419. sub == m_HomeOutputDirectory
  420. #endif
  421. )
  422. {
  423. ret = m_RelativePathToBinaryDir + ret.substr(m_HomeOutputDirectory.size(), ret.npos);
  424. }
  425. }
  426. std::string relpath = m_RelativePathToBinaryDir;
  427. if(relpath.size())
  428. {
  429. relpath.erase(relpath.size()-1, 1);
  430. }
  431. else
  432. {
  433. relpath = ".";
  434. }
  435. if(
  436. #if defined(_WIN32) || defined(__APPLE__)
  437. cmSystemTools::LowerCase(ret) ==
  438. cmSystemTools::LowerCase(m_HomeOutputDirectoryNoSlash)
  439. #else
  440. ret == m_HomeOutputDirectoryNoSlash
  441. #endif
  442. )
  443. {
  444. ret = relpath;
  445. }
  446. // Relative paths should always start in a '.', so add a './' if
  447. // necessary.
  448. if(ret.size()
  449. && ret[0] != '\"' && ret[0] != '/' && ret[0] != '.' && ret[0] != '$')
  450. {
  451. if(ret.size() > 1 && ret[1] != ':')
  452. {
  453. ret = std::string("./") + ret;
  454. }
  455. }
  456. ret = cmSystemTools::ConvertToOutputPath(ret.c_str());
  457. return ret;
  458. }