cmLocalGenerator.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. std::string properties;
  171. const char* lib_version = l->second.GetProperty("VERSION");
  172. const char* lib_soversion = l->second.GetProperty("SOVERSION");
  173. if(!m_Makefile->GetDefinition("CMAKE_SHARED_LIBRARY_SONAME_C_FLAG"))
  174. {
  175. // Versioning is supported only for shared libraries and modules,
  176. // and then only when the platform supports an soname flag.
  177. lib_version = 0;
  178. lib_soversion = 0;
  179. }
  180. if ( lib_version )
  181. {
  182. properties += " VERSION ";
  183. properties += lib_version;
  184. }
  185. if ( lib_soversion )
  186. {
  187. properties += " SOVERSION ";
  188. properties += lib_soversion;
  189. }
  190. this->AddInstallRule(fout, dest, type, files, false, properties.c_str());
  191. }
  192. }
  193. break;
  194. case cmTarget::EXECUTABLE:
  195. fname = exeOutPath;
  196. fname += this->GetFullTargetName(l->first.c_str(), l->second);
  197. files = fname.c_str();
  198. this->AddInstallRule(fout, dest, type, files);
  199. break;
  200. case cmTarget::INSTALL_FILES:
  201. {
  202. std::string sourcePath = m_Makefile->GetCurrentDirectory();
  203. std::string binaryPath = m_Makefile->GetCurrentOutputDirectory();
  204. sourcePath += "/";
  205. binaryPath += "/";
  206. const std::vector<std::string> &sf = l->second.GetSourceLists();
  207. std::vector<std::string>::const_iterator i;
  208. for (i = sf.begin(); i != sf.end(); ++i)
  209. {
  210. std::string f = *i;
  211. if(f.substr(0, sourcePath.length()) == sourcePath)
  212. {
  213. f = f.substr(sourcePath.length());
  214. }
  215. else if(f.substr(0, binaryPath.length()) == binaryPath)
  216. {
  217. f = f.substr(binaryPath.length());
  218. }
  219. files = i->c_str();
  220. this->AddInstallRule(fout, dest, type, files);
  221. }
  222. }
  223. break;
  224. case cmTarget::INSTALL_PROGRAMS:
  225. {
  226. std::string sourcePath = m_Makefile->GetCurrentDirectory();
  227. std::string binaryPath = m_Makefile->GetCurrentOutputDirectory();
  228. sourcePath += "/";
  229. binaryPath += "/";
  230. const std::vector<std::string> &sf = l->second.GetSourceLists();
  231. std::vector<std::string>::const_iterator i;
  232. for (i = sf.begin(); i != sf.end(); ++i)
  233. {
  234. std::string f = *i;
  235. if(f.substr(0, sourcePath.length()) == sourcePath)
  236. {
  237. f = f.substr(sourcePath.length());
  238. }
  239. else if(f.substr(0, binaryPath.length()) == binaryPath)
  240. {
  241. f = f.substr(binaryPath.length());
  242. }
  243. files = i->c_str();
  244. this->AddInstallRule(fout, dest, type, files);
  245. }
  246. }
  247. break;
  248. case cmTarget::UTILITY:
  249. default:
  250. break;
  251. }
  252. }
  253. if ( postinstall )
  254. {
  255. fout << "INCLUDE(\"" << postinstall << "\")" << std::endl;
  256. }
  257. }
  258. cmMakefile* mf = this->GetMakefile();
  259. if ( !mf->GetSubDirectories().empty() )
  260. {
  261. const std::vector<std::pair<cmStdString, bool> >& subdirs = mf->GetSubDirectories();
  262. std::vector<std::pair<cmStdString, bool> >::const_iterator i = subdirs.begin();
  263. for(; i != subdirs.end(); ++i)
  264. {
  265. std::string odir = mf->GetCurrentOutputDirectory();
  266. odir += "/" + (*i).first;
  267. cmSystemTools::ConvertToUnixSlashes(odir);
  268. fout << "INCLUDE(\"" << odir.c_str()
  269. << "/cmake_install.cmake\")" << std::endl;
  270. }
  271. fout << std::endl;;
  272. }
  273. if ( toplevel_install )
  274. {
  275. fout << "FOREACH(file ${CMAKE_INSTALL_MANIFEST_FILES})" << std::endl
  276. << " FILE(APPEND \"" << homedir.c_str() << "/install_manifest.txt\" "
  277. << "\"${file}\\n\")" << std::endl
  278. << "ENDFOREACH(file)" << std::endl;
  279. }
  280. }
  281. void cmLocalGenerator::AddInstallRule(std::ostream& fout, const char* dest,
  282. int type, const char* files, bool optional /* = false */, const char* properties /* = 0 */)
  283. {
  284. std::string sfiles = files;
  285. std::string destination = dest;
  286. std::string stype;
  287. switch ( type )
  288. {
  289. case cmTarget::INSTALL_PROGRAMS: stype = "PROGRAM"; break;
  290. case cmTarget::EXECUTABLE: stype = "EXECUTABLE"; break;
  291. case cmTarget::STATIC_LIBRARY: stype = "STATIC_LIBRARY"; break;
  292. case cmTarget::SHARED_LIBRARY: stype = "SHARED_LIBRARY"; break;
  293. case cmTarget::MODULE_LIBRARY: stype = "MODULE"; break;
  294. case cmTarget::INSTALL_FILES:
  295. default: stype = "FILE"; break;
  296. }
  297. std::string fname = cmSystemTools::GetFilenameName(sfiles.c_str());
  298. fout
  299. << "MESSAGE(STATUS \"Installing " << destination.c_str()
  300. << "/" << fname.c_str() << "\")\n"
  301. << "FILE(INSTALL DESTINATION \"" << destination.c_str()
  302. << "\" TYPE " << stype.c_str() << (optional?" OPTIONAL":"") ;
  303. if ( properties && *properties )
  304. {
  305. fout << " PROPERTIES" << properties;
  306. }
  307. fout
  308. << " FILES \"" << sfiles.c_str() << "\")\n";
  309. }
  310. std::string cmLocalGenerator::GetFullTargetName(const char* n,
  311. const cmTarget& t)
  312. {
  313. const char* targetPrefix = t.GetProperty("PREFIX");
  314. const char* targetSuffix = t.GetProperty("SUFFIX");
  315. const char* prefixVar = 0;
  316. const char* suffixVar = 0;
  317. switch(t.GetType())
  318. {
  319. case cmTarget::STATIC_LIBRARY:
  320. prefixVar = "CMAKE_STATIC_LIBRARY_PREFIX";
  321. suffixVar = "CMAKE_STATIC_LIBRARY_SUFFIX";
  322. break;
  323. case cmTarget::SHARED_LIBRARY:
  324. prefixVar = "CMAKE_SHARED_LIBRARY_PREFIX";
  325. suffixVar = "CMAKE_SHARED_LIBRARY_SUFFIX";
  326. break;
  327. case cmTarget::MODULE_LIBRARY:
  328. prefixVar = "CMAKE_SHARED_MODULE_PREFIX";
  329. suffixVar = "CMAKE_SHARED_MODULE_SUFFIX";
  330. break;
  331. case cmTarget::EXECUTABLE:
  332. targetSuffix = cmSystemTools::GetExecutableExtension();
  333. case cmTarget::UTILITY:
  334. case cmTarget::INSTALL_FILES:
  335. case cmTarget::INSTALL_PROGRAMS:
  336. break;
  337. }
  338. // if there is no prefix on the target use the cmake definition
  339. if(!targetPrefix && prefixVar)
  340. {
  341. targetPrefix = m_Makefile->GetSafeDefinition(prefixVar);
  342. }
  343. // if there is no suffix on the target use the cmake definition
  344. if(!targetSuffix && suffixVar)
  345. {
  346. targetSuffix = m_Makefile->GetSafeDefinition(suffixVar);
  347. }
  348. std::string name = targetPrefix?targetPrefix:"";
  349. name += n;
  350. name += targetSuffix?targetSuffix:"";
  351. return name;
  352. }
  353. std::string cmLocalGenerator::ConvertToRelativeOutputPath(const char* p)
  354. {
  355. if ( !m_Makefile->IsOn("CMAKE_USE_RELATIVE_PATHS") )
  356. {
  357. return cmSystemTools::ConvertToOutputPath(p);
  358. }
  359. // do not use relative paths for network build trees
  360. // the network paths do not work
  361. const char* outputDirectory = m_Makefile->GetHomeOutputDirectory();
  362. if ( outputDirectory && *outputDirectory && *(outputDirectory+1) &&
  363. outputDirectory[0] == '/' && outputDirectory[1] == '/' )
  364. {
  365. return cmSystemTools::ConvertToOutputPath(p);
  366. }
  367. // The first time this is called, initialize all
  368. // the path ivars that are used. This can not
  369. // be moved to the constructor because all the paths are not set yet.
  370. if(m_CurrentOutputDirectory.size() == 0)
  371. {
  372. m_CurrentOutputDirectory = m_Makefile->GetCurrentOutputDirectory();
  373. m_HomeOutputDirectory = m_Makefile->GetHomeOutputDirectory();
  374. m_HomeDirectory = m_Makefile->GetHomeDirectory();
  375. if(m_RelativePathToSourceDir.size() == 0)
  376. {
  377. m_RelativePathToSourceDir = cmSystemTools::RelativePath(
  378. m_CurrentOutputDirectory.c_str(),
  379. m_HomeDirectory.c_str());
  380. std::string path = m_CurrentOutputDirectory;
  381. cmSystemTools::ReplaceString(path, m_HomeOutputDirectory.c_str(), "");
  382. unsigned i;
  383. m_RelativePathToBinaryDir = "";
  384. for(i =0; i < path.size(); ++i)
  385. {
  386. if(path[i] == '/')
  387. {
  388. m_RelativePathToBinaryDir += "../";
  389. }
  390. }
  391. }
  392. m_HomeOutputDirectoryNoSlash = m_HomeOutputDirectory;
  393. m_HomeOutputDirectory += "/";
  394. m_CurrentOutputDirectory += "/";
  395. }
  396. // Do the work of converting to a relative path
  397. std::string pathIn = p;
  398. if(pathIn.find('/') == pathIn.npos)
  399. {
  400. return pathIn;
  401. }
  402. if(pathIn.size() && pathIn[0] == '\"')
  403. {
  404. pathIn = pathIn.substr(1, pathIn.size()-2);
  405. }
  406. std::string ret = pathIn;
  407. if(m_CurrentOutputDirectory.size() <= ret.size())
  408. {
  409. std::string sub = ret.substr(0, m_CurrentOutputDirectory.size());
  410. if(
  411. #if defined(_WIN32) || defined(__APPLE__)
  412. cmSystemTools::LowerCase(sub) ==
  413. cmSystemTools::LowerCase(m_CurrentOutputDirectory)
  414. #else
  415. sub == m_CurrentOutputDirectory
  416. #endif
  417. )
  418. {
  419. ret = ret.substr(m_CurrentOutputDirectory.size(), ret.npos);
  420. }
  421. }
  422. if(m_HomeDirectory.size() <= ret.size())
  423. {
  424. std::string sub = ret.substr(0, m_HomeDirectory.size());
  425. if(
  426. #if defined(_WIN32) || defined(__APPLE__)
  427. cmSystemTools::LowerCase(sub) ==
  428. cmSystemTools::LowerCase(m_HomeDirectory)
  429. #else
  430. sub == m_HomeDirectory
  431. #endif
  432. )
  433. {
  434. ret = m_RelativePathToSourceDir + ret.substr(m_HomeDirectory.size(), ret.npos);
  435. }
  436. }
  437. if(m_HomeOutputDirectory.size() <= ret.size())
  438. {
  439. std::string sub = ret.substr(0, m_HomeOutputDirectory.size());
  440. if(
  441. #if defined(_WIN32) || defined(__APPLE__)
  442. cmSystemTools::LowerCase(sub) ==
  443. cmSystemTools::LowerCase(m_HomeOutputDirectory)
  444. #else
  445. sub == m_HomeOutputDirectory
  446. #endif
  447. )
  448. {
  449. ret = m_RelativePathToBinaryDir + ret.substr(m_HomeOutputDirectory.size(), ret.npos);
  450. }
  451. }
  452. std::string relpath = m_RelativePathToBinaryDir;
  453. if(relpath.size())
  454. {
  455. relpath.erase(relpath.size()-1, 1);
  456. }
  457. else
  458. {
  459. relpath = ".";
  460. }
  461. if(
  462. #if defined(_WIN32) || defined(__APPLE__)
  463. cmSystemTools::LowerCase(ret) ==
  464. cmSystemTools::LowerCase(m_HomeOutputDirectoryNoSlash)
  465. #else
  466. ret == m_HomeOutputDirectoryNoSlash
  467. #endif
  468. )
  469. {
  470. ret = relpath;
  471. }
  472. // Relative paths should always start in a '.', so add a './' if
  473. // necessary.
  474. if(ret.size()
  475. && ret[0] != '\"' && ret[0] != '/' && ret[0] != '.' && ret[0] != '$')
  476. {
  477. if(ret.size() > 1 && ret[1] != ':')
  478. {
  479. ret = std::string("./") + ret;
  480. }
  481. }
  482. ret = cmSystemTools::ConvertToOutputPath(ret.c_str());
  483. return ret;
  484. }