cmLocalGenerator.cxx 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643
  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. #include "cmSourceFile.h"
  19. #include "cmOrderLinkDirectories.h"
  20. #include "cmTest.h"
  21. #include <ctype.h> // for isalpha
  22. cmLocalGenerator::cmLocalGenerator()
  23. {
  24. m_Makefile = new cmMakefile;
  25. m_Makefile->SetLocalGenerator(this);
  26. m_ExcludeFromAll = false;
  27. m_Parent = 0;
  28. m_WindowsShell = false;
  29. m_IgnoreLibPrefix = false;
  30. m_UseRelativePaths = false;
  31. this->Configured = false;
  32. }
  33. cmLocalGenerator::~cmLocalGenerator()
  34. {
  35. delete m_Makefile;
  36. }
  37. void cmLocalGenerator::Configure()
  38. {
  39. // make sure the CMakeFiles dir is there
  40. std::string filesDir = m_Makefile->GetStartOutputDirectory();
  41. filesDir += "/CMakeFiles";
  42. cmSystemTools::MakeDirectory(filesDir.c_str());
  43. // find & read the list file
  44. std::string currentStart = m_Makefile->GetStartDirectory();
  45. currentStart += "/CMakeLists.txt";
  46. m_Makefile->ReadListFile(currentStart.c_str());
  47. // at the end of the ReadListFile handle any old style subdirs
  48. // first get all the subdirectories
  49. std::vector<cmLocalGenerator *> subdirs = this->GetChildren();
  50. // for each subdir recurse
  51. std::vector<cmLocalGenerator *>::iterator sdi = subdirs.begin();
  52. for (; sdi != subdirs.end(); ++sdi)
  53. {
  54. if (!(*sdi)->Configured)
  55. {
  56. m_Makefile->ConfigureSubDirectory(*sdi);
  57. }
  58. }
  59. this->SetupPathConversions();
  60. // Check whether relative paths should be used for optionally
  61. // relative paths.
  62. m_UseRelativePaths = m_Makefile->IsOn("CMAKE_USE_RELATIVE_PATHS");
  63. this->Configured = true;
  64. }
  65. void cmLocalGenerator::SetupPathConversions()
  66. {
  67. // Setup the current output directory components for use by
  68. // Convert
  69. std::string outdir;
  70. outdir =
  71. cmSystemTools::CollapseFullPath(m_Makefile->GetHomeDirectory());
  72. cmSystemTools::SplitPath(outdir.c_str(), m_HomeDirectoryComponents);
  73. outdir =
  74. cmSystemTools::CollapseFullPath(m_Makefile->GetStartDirectory());
  75. cmSystemTools::SplitPath(outdir.c_str(), m_StartDirectoryComponents);
  76. outdir =
  77. cmSystemTools::CollapseFullPath(m_Makefile->GetHomeOutputDirectory());
  78. cmSystemTools::SplitPath(outdir.c_str(), m_HomeOutputDirectoryComponents);
  79. outdir =
  80. cmSystemTools::CollapseFullPath(m_Makefile->GetStartOutputDirectory());
  81. cmSystemTools::SplitPath(outdir.c_str(), m_StartOutputDirectoryComponents);
  82. }
  83. void cmLocalGenerator::SetGlobalGenerator(cmGlobalGenerator *gg)
  84. {
  85. m_GlobalGenerator = gg;
  86. // setup the home directories
  87. m_Makefile->SetHomeDirectory(
  88. gg->GetCMakeInstance()->GetHomeDirectory());
  89. m_Makefile->SetHomeOutputDirectory(
  90. gg->GetCMakeInstance()->GetHomeOutputDirectory());
  91. }
  92. void cmLocalGenerator::ConfigureFinalPass()
  93. {
  94. m_Makefile->ConfigureFinalPass();
  95. }
  96. void cmLocalGenerator::GenerateTestFiles()
  97. {
  98. if ( !m_Makefile->IsOn("CMAKE_TESTING_ENABLED") )
  99. {
  100. return;
  101. }
  102. std::string file = m_Makefile->GetStartOutputDirectory();
  103. file += "/";
  104. if ( m_Makefile->IsSet("CTEST_NEW_FORMAT") )
  105. {
  106. file += "CTestTestfile.cmake";
  107. }
  108. else
  109. {
  110. file += "DartTestfile.txt";
  111. }
  112. cmGeneratedFileStream fout(file.c_str());
  113. fout.SetCopyIfDifferent(true);
  114. fout << "# CMake generated Testfile for " << std::endl
  115. << "# Source directory: " << m_Makefile->GetStartDirectory() << std::endl
  116. << "# Build directory: " << m_Makefile->GetStartOutputDirectory() << std::endl
  117. << "# " << std::endl
  118. << "# This file replicates the SUBDIRS() and ADD_TEST() commands from the source" << std::endl
  119. << "# tree CMakeLists.txt file, skipping any SUBDIRS() or ADD_TEST() commands" << std::endl
  120. << "# that are excluded by CMake control structures, i.e. IF() commands." << std::endl
  121. << "#" << std::endl
  122. << "# The next line is critical for Dart to work" << std::endl
  123. << "# Duh :-)" << std::endl << std::endl;
  124. const char* testIncludeFile = m_Makefile->GetProperty("TEST_INCLUDE_FILE");
  125. if ( testIncludeFile )
  126. {
  127. fout << "INCLUDE(\"" << testIncludeFile << "\")" << std::endl;
  128. }
  129. const std::vector<cmTest*> *tests = m_Makefile->GetTests();
  130. std::vector<cmTest*>::const_iterator it;
  131. for ( it = tests->begin(); it != tests->end(); ++ it )
  132. {
  133. cmTest* test = *it;
  134. fout << "ADD_TEST(";
  135. fout << test->GetName() << " \"" << test->GetCommand() << "\"";
  136. std::vector<cmStdString>::const_iterator argit;
  137. for (argit = test->GetArguments().begin();
  138. argit != test->GetArguments().end(); ++argit)
  139. {
  140. // Just double-quote all arguments so they are re-parsed
  141. // correctly by the test system.
  142. fout << " \"";
  143. for(std::string::const_iterator c = argit->begin(); c != argit->end(); ++c)
  144. {
  145. // Escape quotes within arguments. We should escape
  146. // backslashes too but we cannot because it makes the result
  147. // inconsistent with previous behavior of this command.
  148. if((*c == '"'))
  149. {
  150. fout << '\\';
  151. }
  152. fout << *c;
  153. }
  154. fout << "\"";
  155. }
  156. fout << ")" << std::endl;
  157. std::map<cmStdString,cmStdString>::const_iterator pit;
  158. const std::map<cmStdString,cmStdString>* mpit = &test->GetProperties();
  159. if ( mpit->size() )
  160. {
  161. fout << "SET_TESTS_PROPERTIES(" << test->GetName() << " PROPERTIES ";
  162. for ( pit = mpit->begin(); pit != mpit->end(); ++ pit )
  163. {
  164. fout << " " << pit->first.c_str() << " \"";
  165. const char* value = pit->second.c_str();
  166. for ( ; *value; ++ value )
  167. {
  168. switch ( *value )
  169. {
  170. case '\\':
  171. case '"':
  172. case ' ':
  173. case '#':
  174. case '(':
  175. case ')':
  176. case '$':
  177. case '^':
  178. fout << "\\" << *value;
  179. break;
  180. case '\t':
  181. fout << "\\t";
  182. break;
  183. case '\n':
  184. fout << "\\n";
  185. break;
  186. case '\r':
  187. fout << "\\r";
  188. break;
  189. default:
  190. fout << *value;
  191. }
  192. }
  193. fout << "\"";
  194. }
  195. fout << ")" << std::endl;
  196. }
  197. }
  198. if ( this->Children.size())
  199. {
  200. fout << "SUBDIRS(";
  201. size_t i;
  202. std::string outDir = m_Makefile->GetStartOutputDirectory();
  203. outDir += "/";
  204. for(i = 0; i < this->Children.size(); ++i)
  205. {
  206. std::string binP = this->Children[i]->GetMakefile()->GetStartOutputDirectory();
  207. cmSystemTools::ReplaceString(binP, outDir.c_str(), "");
  208. if ( i > 0 )
  209. {
  210. fout << " ";
  211. }
  212. fout << binP.c_str();
  213. }
  214. fout << ")" << std::endl << std::endl;;
  215. }
  216. }
  217. void cmLocalGenerator::GenerateInstallRules()
  218. {
  219. cmTargets &tgts = m_Makefile->GetTargets();
  220. const char* prefix
  221. = m_Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  222. #if defined(_WIN32) && !defined(__CYGWIN__)
  223. std::string prefix_win32;
  224. if(!prefix)
  225. {
  226. if(!cmSystemTools::GetEnv("SystemDrive", prefix_win32))
  227. {
  228. prefix_win32 = "C:";
  229. }
  230. const char* project_name = m_Makefile->GetDefinition("PROJECT_NAME");
  231. if(project_name && project_name[0])
  232. {
  233. prefix_win32 += "/Program Files/";
  234. prefix_win32 += project_name;
  235. }
  236. else
  237. {
  238. prefix_win32 += "/InstalledCMakeProject";
  239. }
  240. prefix = prefix_win32.c_str();
  241. }
  242. #else
  243. if (!prefix)
  244. {
  245. prefix = "/usr/local";
  246. }
  247. #endif
  248. std::string file = m_Makefile->GetStartOutputDirectory();
  249. std::string homedir = m_Makefile->GetHomeOutputDirectory();
  250. std::string currdir = m_Makefile->GetCurrentOutputDirectory();
  251. cmSystemTools::ConvertToUnixSlashes(file);
  252. cmSystemTools::ConvertToUnixSlashes(homedir);
  253. cmSystemTools::ConvertToUnixSlashes(currdir);
  254. int toplevel_install = 0;
  255. if ( currdir == homedir )
  256. {
  257. toplevel_install = 1;
  258. }
  259. file += "/cmake_install.cmake";
  260. cmGeneratedFileStream fout(file.c_str());
  261. fout.SetCopyIfDifferent(true);
  262. fout << "# Install script for directory: " << m_Makefile->GetCurrentDirectory()
  263. << std::endl << std::endl;
  264. fout << "# Set the install prefix" << std::endl
  265. << "IF(NOT CMAKE_INSTALL_PREFIX)" << std::endl
  266. << " SET(CMAKE_INSTALL_PREFIX \"" << prefix << "\")" << std::endl
  267. << "ENDIF(NOT CMAKE_INSTALL_PREFIX)" << std::endl
  268. << std::endl;
  269. const char* cmakeDebugPosfix = m_Makefile->GetDefinition("CMAKE_DEBUG_POSTFIX");
  270. if ( cmakeDebugPosfix )
  271. {
  272. fout << "SET(CMAKE_DEBUG_POSTFIX \"" << cmakeDebugPosfix << "\")"
  273. << std::endl << std::endl;
  274. }
  275. std::string libOutPath = "";
  276. if (m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH"))
  277. {
  278. libOutPath = m_Makefile->GetDefinition("LIBRARY_OUTPUT_PATH");
  279. if(libOutPath.size())
  280. {
  281. if(libOutPath[libOutPath.size() -1] != '/')
  282. {
  283. libOutPath += "/";
  284. }
  285. }
  286. }
  287. std::string exeOutPath = "";
  288. if (m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  289. {
  290. exeOutPath =
  291. m_Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  292. if(exeOutPath.size())
  293. {
  294. if(exeOutPath[exeOutPath.size() -1] != '/')
  295. {
  296. exeOutPath += "/";
  297. }
  298. }
  299. }
  300. if ( libOutPath.size() == 0 )
  301. {
  302. // LIBRARY_OUTPUT_PATH not defined
  303. libOutPath = currdir + "/";
  304. }
  305. if ( exeOutPath.size() == 0 )
  306. {
  307. // EXECUTABLE_OUTPUT_PATH not defined
  308. exeOutPath = currdir + "/";
  309. }
  310. std::string destination;
  311. for(cmTargets::iterator l = tgts.begin();
  312. l != tgts.end(); l++)
  313. {
  314. const char* preinstall = l->second.GetProperty("PRE_INSTALL_SCRIPT");
  315. const char* postinstall = l->second.GetProperty("POST_INSTALL_SCRIPT");
  316. if ( preinstall )
  317. {
  318. fout << "INCLUDE(\"" << preinstall << "\")" << std::endl;
  319. }
  320. if (l->second.GetInstallPath() != "")
  321. {
  322. destination = "${CMAKE_INSTALL_PREFIX}/" + l->second.GetInstallPath();
  323. cmSystemTools::ConvertToUnixSlashes(destination);
  324. const char* dest = destination.c_str();
  325. int type = l->second.GetType();
  326. std::string fname;
  327. const char* files;
  328. // now install the target
  329. switch (type)
  330. {
  331. case cmTarget::STATIC_LIBRARY:
  332. case cmTarget::MODULE_LIBRARY:
  333. fname = libOutPath;
  334. fname += l->second.GetFullName();
  335. files = fname.c_str();
  336. this->AddInstallRule(fout, dest, type, files);
  337. break;
  338. case cmTarget::SHARED_LIBRARY:
  339. {
  340. // Special code to handle DLL
  341. fname = libOutPath;
  342. fname += l->second.GetFullName();
  343. std::string ext = cmSystemTools::GetFilenameExtension(fname);
  344. ext = cmSystemTools::LowerCase(ext);
  345. if ( ext == ".dll" )
  346. {
  347. std::string libname = libOutPath;
  348. libname += cmSystemTools::GetFilenameWithoutExtension(fname);
  349. libname += ".lib";
  350. files = libname.c_str();
  351. this->AddInstallRule(fout, dest, cmTarget::STATIC_LIBRARY, files, true);
  352. std::string dlldest = "${CMAKE_INSTALL_PREFIX}/" + l->second.GetRuntimeInstallPath();
  353. files = fname.c_str();
  354. this->AddInstallRule(fout, dlldest.c_str(), type, files);
  355. }
  356. else
  357. {
  358. files = fname.c_str();
  359. std::string properties;
  360. const char* lib_version = l->second.GetProperty("VERSION");
  361. const char* lib_soversion = l->second.GetProperty("SOVERSION");
  362. if(!m_Makefile->GetDefinition("CMAKE_SHARED_LIBRARY_SONAME_C_FLAG"))
  363. {
  364. // Versioning is supported only for shared libraries and modules,
  365. // and then only when the platform supports an soname flag.
  366. lib_version = 0;
  367. lib_soversion = 0;
  368. }
  369. if ( lib_version )
  370. {
  371. properties += " VERSION ";
  372. properties += lib_version;
  373. }
  374. if ( lib_soversion )
  375. {
  376. properties += " SOVERSION ";
  377. properties += lib_soversion;
  378. }
  379. this->AddInstallRule(fout, dest, type, files, false, properties.c_str());
  380. }
  381. }
  382. break;
  383. case cmTarget::EXECUTABLE:
  384. {
  385. std::string properties;
  386. #if defined(_WIN32) && !defined(__CYGWIN__)
  387. const char* exe_version = 0;
  388. #else
  389. const char* exe_version = l->second.GetProperty("VERSION");
  390. #endif
  391. if(exe_version)
  392. {
  393. properties += " VERSION ";
  394. properties += exe_version;
  395. }
  396. if(l->second.GetPropertyAsBool("MACOSX_BUNDLE"))
  397. {
  398. fname = exeOutPath;
  399. fname += l->second.GetFullName();
  400. std::string plist = fname;
  401. plist += ".app/Contents/Info.plist";
  402. fname += ".app/Contents/MacOS/";
  403. fname += l->second.GetName();
  404. files = fname.c_str();
  405. std::string bdest = dest;
  406. bdest += "/";
  407. bdest += l->second.GetName();
  408. std::string pdest = bdest;
  409. pdest += ".app/Contents";
  410. bdest += ".app/Contents/MacOS";
  411. // first install the actual executable
  412. this->AddInstallRule(fout, bdest.c_str(), type, files,
  413. false, properties.c_str());
  414. files = plist.c_str();
  415. // now install the Info.plist file
  416. this->AddInstallRule(fout, pdest.c_str(),
  417. cmTarget::INSTALL_FILES, files);
  418. }
  419. else
  420. {
  421. fname = exeOutPath;
  422. fname += l->second.GetFullName();
  423. files = fname.c_str();
  424. this->AddInstallRule(fout, dest, type, files, false,
  425. properties.c_str());
  426. }
  427. }
  428. break;
  429. case cmTarget::INSTALL_FILES:
  430. {
  431. std::string sourcePath = m_Makefile->GetCurrentDirectory();
  432. std::string binaryPath = m_Makefile->GetCurrentOutputDirectory();
  433. sourcePath += "/";
  434. binaryPath += "/";
  435. const std::vector<std::string> &sf = l->second.GetSourceLists();
  436. std::vector<std::string>::const_iterator i;
  437. for (i = sf.begin(); i != sf.end(); ++i)
  438. {
  439. std::string f = *i;
  440. if(f.substr(0, sourcePath.length()) == sourcePath)
  441. {
  442. f = f.substr(sourcePath.length());
  443. }
  444. else if(f.substr(0, binaryPath.length()) == binaryPath)
  445. {
  446. f = f.substr(binaryPath.length());
  447. }
  448. files = i->c_str();
  449. this->AddInstallRule(fout, dest, type, files);
  450. }
  451. }
  452. break;
  453. case cmTarget::INSTALL_PROGRAMS:
  454. {
  455. std::string sourcePath = m_Makefile->GetCurrentDirectory();
  456. std::string binaryPath = m_Makefile->GetCurrentOutputDirectory();
  457. sourcePath += "/";
  458. binaryPath += "/";
  459. const std::vector<std::string> &sf = l->second.GetSourceLists();
  460. std::vector<std::string>::const_iterator i;
  461. for (i = sf.begin(); i != sf.end(); ++i)
  462. {
  463. std::string f = *i;
  464. if(f.substr(0, sourcePath.length()) == sourcePath)
  465. {
  466. f = f.substr(sourcePath.length());
  467. }
  468. else if(f.substr(0, binaryPath.length()) == binaryPath)
  469. {
  470. f = f.substr(binaryPath.length());
  471. }
  472. files = i->c_str();
  473. this->AddInstallRule(fout, dest, type, files);
  474. }
  475. }
  476. break;
  477. case cmTarget::UTILITY:
  478. default:
  479. break;
  480. }
  481. }
  482. if ( postinstall )
  483. {
  484. fout << "INCLUDE(\"" << postinstall << "\")" << std::endl;
  485. }
  486. }
  487. if ( this->Children.size())
  488. {
  489. std::vector<cmLocalGenerator*>::const_iterator i = this->Children.begin();
  490. for(; i != this->Children.end(); ++i)
  491. {
  492. std::string odir = (*i)->GetMakefile()->GetStartOutputDirectory();
  493. cmSystemTools::ConvertToUnixSlashes(odir);
  494. fout << "INCLUDE(\"" << odir.c_str()
  495. << "/cmake_install.cmake\")" << std::endl;
  496. }
  497. fout << std::endl;;
  498. }
  499. if ( toplevel_install )
  500. {
  501. fout << "FILE(WRITE \"" << homedir.c_str() << "/install_manifest.txt\" "
  502. << "\"\")" << std::endl;
  503. fout << "FOREACH(file ${CMAKE_INSTALL_MANIFEST_FILES})" << std::endl
  504. << " FILE(APPEND \"" << homedir.c_str() << "/install_manifest.txt\" "
  505. << "\"${file}\\n\")" << std::endl
  506. << "ENDFOREACH(file)" << std::endl;
  507. }
  508. }
  509. void cmLocalGenerator::AddInstallRule(std::ostream& fout, const char* dest,
  510. int type, const char* files, bool optional /* = false */, const char* properties /* = 0 */)
  511. {
  512. std::string sfiles = files;
  513. std::string destination = dest;
  514. std::string stype;
  515. switch ( type )
  516. {
  517. case cmTarget::INSTALL_PROGRAMS: stype = "PROGRAM"; break;
  518. case cmTarget::EXECUTABLE: stype = "EXECUTABLE"; break;
  519. case cmTarget::STATIC_LIBRARY: stype = "STATIC_LIBRARY"; break;
  520. case cmTarget::SHARED_LIBRARY: stype = "SHARED_LIBRARY"; break;
  521. case cmTarget::MODULE_LIBRARY: stype = "MODULE"; break;
  522. case cmTarget::INSTALL_FILES:
  523. default: stype = "FILE"; break;
  524. }
  525. std::string fname = cmSystemTools::GetFilenameName(sfiles.c_str());
  526. fout
  527. << "MESSAGE(STATUS \"Installing " << destination.c_str()
  528. << "/" << fname.c_str() << "\")\n"
  529. << "FILE(INSTALL DESTINATION \"" << destination.c_str()
  530. << "\" TYPE " << stype.c_str() << (optional?" OPTIONAL":"") ;
  531. if ( properties && *properties )
  532. {
  533. fout << " PROPERTIES" << properties;
  534. }
  535. fout
  536. << " FILES \"" << sfiles.c_str() << "\")\n";
  537. }
  538. void cmLocalGenerator::AddCustomCommandToCreateObject(const char* ofname,
  539. const char* lang,
  540. cmSourceFile& source,
  541. cmTarget& )
  542. {
  543. std::string objectFile = this->Convert(ofname,START_OUTPUT,SHELL);
  544. std::string sourceFile =
  545. this->Convert(source.GetFullPath().c_str(),START_OUTPUT,SHELL,true);
  546. std::string varString = "CMAKE_";
  547. varString += lang;
  548. varString += "_COMPILE_OBJECT";
  549. std::vector<std::string> rules;
  550. rules.push_back(m_Makefile->GetRequiredDefinition(varString.c_str()));
  551. varString = "CMAKE_";
  552. varString += lang;
  553. varString += "_FLAGS";
  554. std::string flags;
  555. flags += m_Makefile->GetSafeDefinition(varString.c_str());
  556. flags += " ";
  557. flags += this->GetIncludeFlags(lang);
  558. std::vector<std::string> commands;
  559. cmSystemTools::ExpandList(rules, commands);
  560. for(std::vector<std::string>::iterator i = commands.begin();
  561. i != commands.end(); ++i)
  562. {
  563. this->ExpandRuleVariables(*i,
  564. lang,
  565. 0, // no objects
  566. 0, // no target
  567. 0, // no link libs
  568. sourceFile.c_str(),
  569. objectFile.c_str(),
  570. flags.c_str());
  571. }
  572. std::vector<std::string> sourceAndDeps;
  573. sourceAndDeps.push_back(sourceFile);
  574. if(commands.size() > 1)
  575. {
  576. cmSystemTools::Error("Currently custom rules can only have one command sorry ");
  577. }
  578. // Check for extra object-file dependencies.
  579. std::vector<std::string> depends;
  580. const char* additionalDeps = source.GetProperty("OBJECT_DEPENDS");
  581. if(additionalDeps)
  582. {
  583. cmSystemTools::ExpandListArgument(additionalDeps, depends);
  584. for(std::vector<std::string>::iterator i = depends.begin();
  585. i != depends.end(); ++i)
  586. {
  587. sourceAndDeps.push_back(this->Convert(i->c_str(),START_OUTPUT,SHELL));
  588. }
  589. }
  590. #if 0
  591. std::string command;
  592. std::string args;
  593. cmSystemTools::SplitProgramFromArgs(commands[0].c_str(), command, args);
  594. std::vector<std::string> argsv;
  595. argsv.push_back(args);
  596. m_Makefile->AddCustomCommandToOutput(ofname,
  597. command.c_str(),
  598. argsv,
  599. source.GetFullPath().c_str(),
  600. sourceAndDeps,
  601. "build from source");
  602. #endif
  603. }
  604. void cmLocalGenerator::AddBuildTargetRule(const char* llang, cmTarget& target)
  605. {
  606. cmStdString objs;
  607. std::vector<std::string> objVector;
  608. // Add all the sources outputs to the depends of the target
  609. std::vector<cmSourceFile*>& classes = target.GetSourceFiles();
  610. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  611. i != classes.end(); ++i)
  612. {
  613. if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  614. !(*i)->GetCustomCommand())
  615. {
  616. std::string outExt =
  617. m_GlobalGenerator->GetLanguageOutputExtensionFromExtension(
  618. (*i)->GetSourceExtension().c_str());
  619. if(outExt.size() && !(*i)->GetPropertyAsBool("EXTERNAL_OBJECT") )
  620. {
  621. std::string ofname = m_Makefile->GetCurrentOutputDirectory();
  622. ofname += "/";
  623. ofname += (*i)->GetSourceName() + outExt;
  624. objVector.push_back(ofname);
  625. this->AddCustomCommandToCreateObject(ofname.c_str(), llang, *(*i), target);
  626. objs += this->Convert(ofname.c_str(),START_OUTPUT,MAKEFILE);
  627. objs += " ";
  628. }
  629. }
  630. }
  631. std::string createRule = "CMAKE_";
  632. createRule += llang;
  633. createRule += target.GetCreateRuleVariable();
  634. std::string targetName = target.GetFullName();
  635. // Executable :
  636. // Shared Library:
  637. // Static Library:
  638. // Shared Module:
  639. std::string linkLibs; // should be set
  640. std::string flags; // should be set
  641. std::string linkFlags; // should be set
  642. this->GetTargetFlags(linkLibs, flags, linkFlags, target);
  643. std::string rule = m_Makefile->GetRequiredDefinition(createRule.c_str());
  644. this->ExpandRuleVariables(rule,
  645. llang, // language
  646. objs.c_str(), // objects
  647. targetName.c_str(), // target
  648. linkLibs.c_str(), // link libs
  649. 0, // source
  650. 0, // object
  651. flags.c_str(), // flags
  652. 0, // objects quoted
  653. 0, // target base name
  654. 0, // target so name,
  655. linkFlags.c_str() // link flags
  656. );
  657. #if 0
  658. std::string command;
  659. std::string args;
  660. cmSystemTools::SplitProgramFromArgs(rule.c_str(), command, args);
  661. // Just like ADD_CUSTOM_TARGET(foo ALL DEPENDS a.o b.o)
  662. // Add a custom command for generating each .o file
  663. cmCustomCommand cc(command.c_str(), args.c_str(), objVector,
  664. targetName.c_str(), 0);
  665. target.GetPostBuildCommands().push_back(cc);
  666. #endif
  667. }
  668. void cmLocalGenerator::CreateCustomTargetsAndCommands(std::set<cmStdString> const& lang)
  669. {
  670. cmTargets &tgts = m_Makefile->GetTargets();
  671. for(cmTargets::iterator l = tgts.begin();
  672. l != tgts.end(); l++)
  673. {
  674. cmTarget& target = l->second;
  675. switch(target.GetType())
  676. {
  677. case cmTarget::STATIC_LIBRARY:
  678. case cmTarget::SHARED_LIBRARY:
  679. case cmTarget::MODULE_LIBRARY:
  680. case cmTarget::EXECUTABLE:
  681. {
  682. const char* llang = target.GetLinkerLanguage(this->GetGlobalGenerator());
  683. if(!llang)
  684. {
  685. cmSystemTools::Error("CMake can not determine linker language for target:",
  686. target.GetName());
  687. return;
  688. }
  689. // if the language is not in the set lang then create custom
  690. // commands to build the target
  691. if(lang.count(llang) == 0)
  692. {
  693. this->AddBuildTargetRule(llang, target);
  694. }
  695. }
  696. break;
  697. case cmTarget::UTILITY:
  698. case cmTarget::INSTALL_FILES:
  699. case cmTarget::INSTALL_PROGRAMS:
  700. break;
  701. }
  702. }
  703. }
  704. // List of variables that are replaced when
  705. // rules are expanced. These variables are
  706. // replaced in the form <var> with GetSafeDefinition(var).
  707. // ${LANG} is replaced in the variable first with all enabled
  708. // languages.
  709. static const char* ruleReplaceVars[] =
  710. {
  711. "CMAKE_${LANG}_COMPILER",
  712. "CMAKE_SHARED_LIBRARY_CREATE_${LANG}_FLAGS",
  713. "CMAKE_SHARED_MODULE_CREATE_${LANG}_FLAGS",
  714. "CMAKE_SHARED_MODULE_${LANG}_FLAGS",
  715. "CMAKE_SHARED_LIBRARY_${LANG}_FLAGS",
  716. "CMAKE_${LANG}_LINK_FLAGS",
  717. "CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG",
  718. "CMAKE_${LANG}_ARCHIVE",
  719. "CMAKE_AR",
  720. "CMAKE_CURRENT_SOURCE_DIR",
  721. "CMAKE_CURRENT_BINARY_DIR",
  722. "CMAKE_RANLIB",
  723. 0
  724. };
  725. std::string
  726. cmLocalGenerator::ExpandRuleVariable(std::string const& variable,
  727. const char* lang,
  728. const char* objects,
  729. const char* target,
  730. const char* linkLibs,
  731. const char* source,
  732. const char* object,
  733. const char* flags,
  734. const char* objectsquoted,
  735. const char* targetBase,
  736. const char* targetSOName,
  737. const char* linkFlags)
  738. {
  739. if(linkFlags)
  740. {
  741. if(variable == "LINK_FLAGS")
  742. {
  743. return linkFlags;
  744. }
  745. }
  746. if(flags)
  747. {
  748. if(variable == "FLAGS")
  749. {
  750. return flags;
  751. }
  752. }
  753. if(source)
  754. {
  755. if(variable == "SOURCE")
  756. {
  757. return source;
  758. }
  759. }
  760. if(object)
  761. {
  762. if(variable == "OBJECT")
  763. {
  764. return object;
  765. }
  766. }
  767. if(objects)
  768. {
  769. if(variable == "OBJECTS")
  770. {
  771. return objects;
  772. }
  773. }
  774. if(objectsquoted)
  775. {
  776. if(variable == "OBJECTS_QUOTED")
  777. {
  778. return objectsquoted;
  779. }
  780. }
  781. if(target)
  782. {
  783. if(variable == "TARGET_QUOTED")
  784. {
  785. std::string targetQuoted = target;
  786. if(targetQuoted.size() && targetQuoted[0] != '\"')
  787. {
  788. targetQuoted = '\"';
  789. targetQuoted += target;
  790. targetQuoted += '\"';
  791. return targetQuoted;
  792. }
  793. }
  794. if(variable == "TARGET")
  795. {
  796. return target;
  797. }
  798. }
  799. if(targetBase)
  800. {
  801. if(variable == "TARGET_BASE.lib" || variable == "TARGET_BASE.dll")
  802. {
  803. // special case for quoted paths with spaces
  804. // if you see <TARGET_BASE>.lib then put the .lib inside
  805. // the quotes, same for .dll
  806. if((strlen(targetBase) > 1) && targetBase[0] == '\"')
  807. {
  808. std::string base = targetBase;
  809. base[base.size()-1] = '.';
  810. std::string baseLib = base + "lib\"";
  811. std::string baseDll = base + "dll\"";
  812. if(variable == "TARGET_BASE.lib" )
  813. {
  814. return baseLib;
  815. }
  816. if(variable == "TARGET_BASE.dll" )
  817. {
  818. return baseDll;
  819. }
  820. }
  821. }
  822. if(variable == "TARGET_BASE")
  823. {
  824. return targetBase;
  825. }
  826. }
  827. if(targetSOName)
  828. {
  829. if(variable == "TARGET_SONAME")
  830. {
  831. if(lang)
  832. {
  833. std::string name = "CMAKE_SHARED_LIBRARY_SONAME_";
  834. name += lang;
  835. name += "_FLAG";
  836. if(m_Makefile->GetDefinition(name.c_str()))
  837. {
  838. return targetSOName;
  839. }
  840. }
  841. return "";
  842. }
  843. }
  844. if(linkLibs)
  845. {
  846. if(variable == "LINK_LIBRARIES")
  847. {
  848. return linkLibs;
  849. }
  850. }
  851. std::vector<std::string> enabledLanguages;
  852. m_GlobalGenerator->GetEnabledLanguages(enabledLanguages);
  853. // loop over language specific replace variables
  854. int pos = 0;
  855. while(ruleReplaceVars[pos])
  856. {
  857. for(std::vector<std::string>::iterator i = enabledLanguages.begin();
  858. i != enabledLanguages.end(); ++i)
  859. {
  860. lang = i->c_str();
  861. std::string actualReplace = ruleReplaceVars[pos];
  862. // If this is the compiler then look for the extra variable
  863. // _COMPILER_ARG1 which must be the first argument to the compiler
  864. const char* compilerArg1 = 0;
  865. if(actualReplace == "CMAKE_${LANG}_COMPILER")
  866. {
  867. std::string arg1 = actualReplace + "_ARG1";
  868. cmSystemTools::ReplaceString(arg1, "${LANG}", lang);
  869. compilerArg1 = m_Makefile->GetDefinition(arg1.c_str());
  870. }
  871. if(actualReplace.find("${LANG}") != actualReplace.npos)
  872. {
  873. cmSystemTools::ReplaceString(actualReplace, "${LANG}", lang);
  874. }
  875. if(actualReplace == variable)
  876. {
  877. std::string replace = m_Makefile->GetSafeDefinition(variable.c_str());
  878. // if the variable is not a FLAG then treat it like a path
  879. if(variable.find("_FLAG") == variable.npos)
  880. {
  881. std::string ret = this->ConvertToOutputForExisting(replace.c_str());
  882. // if there is a required first argument to the compiler add it to the compiler string
  883. if(compilerArg1)
  884. {
  885. ret += " ";
  886. ret += compilerArg1;
  887. }
  888. return ret;
  889. }
  890. return replace;
  891. }
  892. }
  893. pos++;
  894. }
  895. return variable;
  896. }
  897. void
  898. cmLocalGenerator::ExpandRuleVariables(std::string& s,
  899. const char* lang,
  900. const char* objects,
  901. const char* target,
  902. const char* linkLibs,
  903. const char* source,
  904. const char* object,
  905. const char* flags,
  906. const char* objectsquoted,
  907. const char* targetBase,
  908. const char* targetSOName,
  909. const char* linkFlags)
  910. {
  911. std::vector<std::string> enabledLanguages;
  912. m_GlobalGenerator->GetEnabledLanguages(enabledLanguages);
  913. std::string::size_type start = s.find('<');
  914. // no variables to expand
  915. if(start == s.npos)
  916. {
  917. return;
  918. }
  919. std::string::size_type pos = 0;
  920. std::string expandedInput;
  921. while(start != s.npos && start < s.size()-2)
  922. {
  923. std::string::size_type end = s.find('>', start);
  924. // if we find a < with no > we are done
  925. if(end == s.npos)
  926. {
  927. return;
  928. }
  929. char c = s[start+1];
  930. // if the next char after the < is not A-Za-z then
  931. // skip it and try to find the next < in the string
  932. if(!isalpha(c))
  933. {
  934. start = s.find('<', start+1);
  935. }
  936. else
  937. {
  938. // extract the var
  939. std::string var = s.substr(start+1, end - start-1);
  940. std::string replace = this->ExpandRuleVariable(var, lang, objects,
  941. target, linkLibs,
  942. source, object, flags,
  943. objectsquoted,
  944. targetBase, targetSOName,
  945. linkFlags);
  946. expandedInput += s.substr(pos, start-pos);
  947. expandedInput += replace;
  948. // move to next one
  949. start = s.find('<', start+var.size()+2);
  950. pos = end+1;
  951. }
  952. }
  953. // add the rest of the input
  954. expandedInput += s.substr(pos, s.size()-pos);
  955. s = expandedInput;
  956. }
  957. std::string
  958. cmLocalGenerator::ConvertToOutputForExisting(const char* p)
  959. {
  960. std::string ret = this->Convert(p, START_OUTPUT, SHELL, true);
  961. // if there are spaces in the path, then get the short path version
  962. // if there is one
  963. if(ret.find(' ') != std::string::npos)
  964. {
  965. if(cmSystemTools::FileExists(p))
  966. {
  967. if(!cmSystemTools::GetShortPath(ret.c_str(), ret))
  968. {
  969. ret = this->Convert(p,START_OUTPUT,MAKEFILE,true);
  970. }
  971. }
  972. }
  973. return ret;
  974. }
  975. const char* cmLocalGenerator::GetIncludeFlags(const char* lang)
  976. {
  977. if(!lang)
  978. {
  979. return "";
  980. }
  981. if(m_LanguageToIncludeFlags.count(lang))
  982. {
  983. return m_LanguageToIncludeFlags[lang].c_str();
  984. }
  985. cmOStringStream includeFlags;
  986. std::vector<std::string> includes;
  987. this->GetIncludeDirectories(includes);
  988. std::vector<std::string>::iterator i;
  989. std::string flagVar = "CMAKE_INCLUDE_FLAG_";
  990. flagVar += lang;
  991. const char* includeFlag = m_Makefile->GetDefinition(flagVar.c_str());
  992. flagVar = "CMAKE_INCLUDE_FLAG_SEP_";
  993. flagVar += lang;
  994. const char* sep = m_Makefile->GetDefinition(flagVar.c_str());
  995. bool repeatFlag = true; // should the include flag be repeated like ie. -IA -IB
  996. if(!sep)
  997. {
  998. sep = " ";
  999. }
  1000. else
  1001. {
  1002. // if there is a separator then the flag is not repeated but is only given once
  1003. // i.e. -classpath a:b:c
  1004. repeatFlag = false;
  1005. }
  1006. bool flagUsed = false;
  1007. std::set<cmStdString> emitted;
  1008. for(i = includes.begin(); i != includes.end(); ++i)
  1009. {
  1010. #ifdef __APPLE__
  1011. if(cmSystemTools::IsPathToFramework(i->c_str()))
  1012. {
  1013. std::string frameworkDir = *i;
  1014. frameworkDir += "/../";
  1015. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
  1016. if(emitted.insert(frameworkDir).second)
  1017. {
  1018. includeFlags << "-F" << this->ConvertToOutputForExisting(frameworkDir.c_str()) << " ";
  1019. }
  1020. continue;
  1021. }
  1022. #endif
  1023. std::string include = *i;
  1024. if(!flagUsed || repeatFlag)
  1025. {
  1026. includeFlags << includeFlag;
  1027. flagUsed = true;
  1028. }
  1029. includeFlags << this->ConvertToOutputForExisting(i->c_str()) << sep;
  1030. }
  1031. std::string flags = includeFlags.str();
  1032. // remove trailing separators
  1033. if((sep[0] != ' ') && flags[flags.size()-1] == sep[0])
  1034. {
  1035. flags[flags.size()-1] = ' ';
  1036. }
  1037. flags += m_Makefile->GetDefineFlags();
  1038. m_LanguageToIncludeFlags[lang] = flags;
  1039. return m_LanguageToIncludeFlags[lang].c_str();
  1040. }
  1041. //----------------------------------------------------------------------------
  1042. void cmLocalGenerator::GetIncludeDirectories(std::vector<std::string>& dirs)
  1043. {
  1044. // Output Include paths
  1045. std::set<cmStdString> implicitIncludes;
  1046. // CMake versions below 2.0 would add the source tree to the -I path
  1047. // automatically. Preserve compatibility.
  1048. bool includeSourceDir = false;
  1049. const char* versionValue =
  1050. m_Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  1051. if(versionValue)
  1052. {
  1053. int major = 0;
  1054. int minor = 0;
  1055. if(sscanf(versionValue, "%d.%d", &major, &minor) == 2 && major < 2)
  1056. {
  1057. includeSourceDir = true;
  1058. }
  1059. }
  1060. const char* vtkSourceDir =
  1061. m_Makefile->GetDefinition("VTK_SOURCE_DIR");
  1062. if(vtkSourceDir)
  1063. {
  1064. // Special hack for VTK 4.0 - 4.4.
  1065. const char* vtk_major = m_Makefile->GetDefinition("VTK_MAJOR_VERSION");
  1066. const char* vtk_minor = m_Makefile->GetDefinition("VTK_MINOR_VERSION");
  1067. vtk_major = vtk_major? vtk_major : "4";
  1068. vtk_minor = vtk_minor? vtk_minor : "4";
  1069. int major = 0;
  1070. int minor = 0;
  1071. if(sscanf(vtk_major, "%d", &major) && sscanf(vtk_minor, "%d", &minor) &&
  1072. major == 4 && minor <= 4)
  1073. {
  1074. includeSourceDir = true;
  1075. }
  1076. }
  1077. if(includeSourceDir)
  1078. {
  1079. dirs.push_back(m_Makefile->GetStartDirectory());
  1080. }
  1081. // Do not explicitly add the standard include path "/usr/include".
  1082. // This can cause problems with certain standard library
  1083. // implementations because the wrong headers may be found first.
  1084. implicitIncludes.insert("/usr/include");
  1085. if(m_Makefile->GetDefinition("CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES"))
  1086. {
  1087. std::string arg = m_Makefile->GetDefinition("CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES");
  1088. std::vector<std::string> implicitIncludeVec;
  1089. cmSystemTools::ExpandListArgument(arg, implicitIncludeVec);
  1090. for(unsigned int k =0; k < implicitIncludeVec.size(); k++)
  1091. {
  1092. implicitIncludes.insert(implicitIncludeVec[k]);
  1093. }
  1094. }
  1095. // Construct the ordered list.
  1096. std::vector<std::string>& includes = m_Makefile->GetIncludeDirectories();
  1097. for(std::vector<std::string>::iterator i = includes.begin();
  1098. i != includes.end(); ++i)
  1099. {
  1100. if(implicitIncludes.find(*i) == implicitIncludes.end())
  1101. {
  1102. dirs.push_back(*i);
  1103. }
  1104. }
  1105. }
  1106. void cmLocalGenerator::GetTargetFlags(std::string& linkLibs,
  1107. std::string& flags,
  1108. std::string& linkFlags,
  1109. cmTarget& target)
  1110. {
  1111. std::string buildType = m_Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1112. buildType = cmSystemTools::UpperCase(buildType);
  1113. const char* libraryLinkVariable = "CMAKE_SHARED_LINKER_FLAGS"; // default to shared library
  1114. switch(target.GetType())
  1115. {
  1116. case cmTarget::STATIC_LIBRARY:
  1117. {
  1118. const char* targetLinkFlags = target.GetProperty("STATIC_LIBRARY_FLAGS");
  1119. if(targetLinkFlags)
  1120. {
  1121. linkFlags += targetLinkFlags;
  1122. linkFlags += " ";
  1123. }
  1124. }
  1125. break;
  1126. case cmTarget::MODULE_LIBRARY:
  1127. libraryLinkVariable = "CMAKE_MODULE_LINKER_FLAGS";
  1128. case cmTarget::SHARED_LIBRARY:
  1129. {
  1130. linkFlags = m_Makefile->GetSafeDefinition(libraryLinkVariable);
  1131. linkFlags += " ";
  1132. if(buildType.size())
  1133. {
  1134. std::string build = libraryLinkVariable;
  1135. build += "_";
  1136. build += buildType;
  1137. linkFlags += m_Makefile->GetSafeDefinition(build.c_str());
  1138. linkFlags += " ";
  1139. }
  1140. if(m_Makefile->IsOn("WIN32") && !(m_Makefile->IsOn("CYGWIN") || m_Makefile->IsOn("MINGW")))
  1141. {
  1142. const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
  1143. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1144. i != sources.end(); ++i)
  1145. {
  1146. if((*i)->GetSourceExtension() == "def")
  1147. {
  1148. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1149. linkFlags += this->Convert((*i)->GetFullPath().c_str(),START_OUTPUT,MAKEFILE);
  1150. linkFlags += " ";
  1151. }
  1152. }
  1153. }
  1154. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  1155. if(targetLinkFlags)
  1156. {
  1157. linkFlags += targetLinkFlags;
  1158. linkFlags += " ";
  1159. }
  1160. cmOStringStream linklibsStr;
  1161. this->OutputLinkLibraries(linklibsStr, target.GetName(), target);
  1162. linkLibs = linklibsStr.str();
  1163. }
  1164. break;
  1165. case cmTarget::EXECUTABLE:
  1166. {
  1167. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  1168. linkFlags += " ";
  1169. if(buildType.size())
  1170. {
  1171. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  1172. build += buildType;
  1173. linkFlags += m_Makefile->GetSafeDefinition(build.c_str());
  1174. linkFlags += " ";
  1175. }
  1176. const char* linkLanguage = target.GetLinkerLanguage(this->GetGlobalGenerator());
  1177. if(!linkLanguage)
  1178. {
  1179. cmSystemTools::Error("CMake can not determine linker language for target:",
  1180. target.GetName());
  1181. return;
  1182. }
  1183. std::string langVar = "CMAKE_";
  1184. langVar += linkLanguage;
  1185. std::string flagsVar = langVar + "_FLAGS";
  1186. std::string sharedFlagsVar = "CMAKE_SHARED_LIBRARY_";
  1187. sharedFlagsVar += linkLanguage;
  1188. sharedFlagsVar += "_FLAGS";
  1189. flags += m_Makefile->GetSafeDefinition(flagsVar.c_str());
  1190. flags += " ";
  1191. flags += m_Makefile->GetSafeDefinition(sharedFlagsVar.c_str());
  1192. flags += " ";
  1193. cmOStringStream linklibs;
  1194. this->OutputLinkLibraries(linklibs, 0, target);
  1195. linkLibs = linklibs.str();
  1196. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1197. {
  1198. std::string sFlagVar = std::string("CMAKE_SHARED_BUILD_") + linkLanguage
  1199. + std::string("_FLAGS");
  1200. linkFlags += m_Makefile->GetSafeDefinition(sFlagVar.c_str());
  1201. linkFlags += " ";
  1202. }
  1203. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  1204. {
  1205. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  1206. linkFlags += " ";
  1207. }
  1208. else
  1209. {
  1210. linkFlags += m_Makefile->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  1211. linkFlags += " ";
  1212. }
  1213. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  1214. if(targetLinkFlags)
  1215. {
  1216. linkFlags += targetLinkFlags;
  1217. linkFlags += " ";
  1218. }
  1219. }
  1220. break;
  1221. case cmTarget::UTILITY:
  1222. case cmTarget::INSTALL_FILES:
  1223. case cmTarget::INSTALL_PROGRAMS:
  1224. break;
  1225. }
  1226. }
  1227. /**
  1228. * Output the linking rules on a command line. For executables,
  1229. * targetLibrary should be a NULL pointer. For libraries, it should point
  1230. * to the name of the library. This will not link a library against itself.
  1231. */
  1232. void cmLocalGenerator::OutputLinkLibraries(std::ostream& fout,
  1233. const char* targetLibrary,
  1234. cmTarget &tgt)
  1235. {
  1236. // Try to emit each search path once
  1237. std::set<cmStdString> emitted;
  1238. // Embed runtime search paths if possible and if required.
  1239. bool outputRuntime = true;
  1240. std::string runtimeFlag;
  1241. std::string runtimeSep;
  1242. std::vector<std::string> runtimeDirs;
  1243. std::string buildType = m_Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1244. buildType = cmSystemTools::UpperCase(buildType);
  1245. cmTarget::LinkLibraryType cmakeBuildType = cmTarget::GENERAL;
  1246. if(buildType == "DEBUG")
  1247. {
  1248. cmakeBuildType = cmTarget::DEBUG;
  1249. }
  1250. else if(buildType.size())
  1251. {
  1252. cmakeBuildType = cmTarget::OPTIMIZED;
  1253. }
  1254. const char* linkLanguage = tgt.GetLinkerLanguage(this->GetGlobalGenerator());
  1255. if(!linkLanguage)
  1256. {
  1257. cmSystemTools::
  1258. Error("CMake can not determine linker language for target:",
  1259. tgt.GetName());
  1260. return;
  1261. }
  1262. std::string runTimeFlagVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
  1263. runTimeFlagVar += linkLanguage;
  1264. runTimeFlagVar += "_FLAG";
  1265. std::string runTimeFlagSepVar = runTimeFlagVar + "_SEP";
  1266. runtimeFlag = m_Makefile->GetSafeDefinition(runTimeFlagVar.c_str());
  1267. runtimeSep = m_Makefile->GetSafeDefinition(runTimeFlagSepVar.c_str());
  1268. // concatenate all paths or no?
  1269. bool runtimeConcatenate = ( runtimeSep!="" );
  1270. if(runtimeFlag == "" || m_Makefile->IsOn("CMAKE_SKIP_RPATH") )
  1271. {
  1272. outputRuntime = false;
  1273. }
  1274. // Some search paths should never be emitted
  1275. emitted.insert("");
  1276. emitted.insert("/usr/lib");
  1277. std::string libPathFlag = m_Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
  1278. std::string libLinkFlag = m_Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_FLAG");
  1279. // collect all the flags needed for linking libraries
  1280. std::string linkLibs;
  1281. // Flags to link an executable to shared libraries.
  1282. std::string linkFlagsVar = "CMAKE_SHARED_LIBRARY_LINK_";
  1283. linkFlagsVar += linkLanguage;
  1284. linkFlagsVar += "_FLAGS";
  1285. if( tgt.GetType() == cmTarget::EXECUTABLE )
  1286. {
  1287. linkLibs = m_Makefile->GetSafeDefinition(linkFlagsVar.c_str());
  1288. linkLibs += " ";
  1289. }
  1290. cmOrderLinkDirectories orderLibs;
  1291. std::string ext =
  1292. m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1293. if(ext.size())
  1294. {
  1295. orderLibs.AddLinkExtension(ext.c_str());
  1296. }
  1297. ext =
  1298. m_Makefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_PREFIX");
  1299. if(ext.size())
  1300. {
  1301. orderLibs.SetLinkPrefix(ext.c_str());
  1302. }
  1303. ext =
  1304. m_Makefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1305. if(ext.size())
  1306. {
  1307. orderLibs.AddLinkExtension(ext.c_str());
  1308. }
  1309. ext =
  1310. m_Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_SUFFIX");
  1311. if(ext.size())
  1312. {
  1313. orderLibs.AddLinkExtension(ext.c_str());
  1314. }
  1315. // compute the correct order for -L paths
  1316. orderLibs.SetLinkInformation(tgt, cmakeBuildType, targetLibrary);
  1317. orderLibs.DetermineLibraryPathOrder();
  1318. std::vector<cmStdString> libdirs;
  1319. std::vector<cmStdString> linkItems;
  1320. orderLibs.GetLinkerInformation(libdirs, linkItems);
  1321. for(std::vector<cmStdString>::const_iterator libDir = libdirs.begin();
  1322. libDir != libdirs.end(); ++libDir)
  1323. {
  1324. std::string libpath = this->ConvertToOutputForExisting(libDir->c_str());
  1325. if(emitted.insert(libpath).second)
  1326. {
  1327. std::string fullLibPath;
  1328. if(!m_WindowsShell && m_UseRelativePaths)
  1329. {
  1330. fullLibPath = "\"`cd ";
  1331. }
  1332. fullLibPath += libpath;
  1333. if(!m_WindowsShell && m_UseRelativePaths)
  1334. {
  1335. fullLibPath += ";pwd`\"";
  1336. }
  1337. std::string::size_type pos = libDir->find(libPathFlag.c_str());
  1338. if((pos == std::string::npos || pos > 0)
  1339. && libDir->find("${") == std::string::npos)
  1340. {
  1341. linkLibs += libPathFlag;
  1342. if(outputRuntime)
  1343. {
  1344. runtimeDirs.push_back( fullLibPath );
  1345. }
  1346. }
  1347. linkLibs += fullLibPath;
  1348. linkLibs += " ";
  1349. }
  1350. }
  1351. std::string linkSuffix =
  1352. m_Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_SUFFIX");
  1353. std::string regexp = ".*\\";
  1354. regexp += linkSuffix;
  1355. regexp += "$";
  1356. cmsys::RegularExpression hasSuffix(regexp.c_str());
  1357. std::string librariesLinked;
  1358. for(std::vector<cmStdString>::iterator lib = linkItems.begin();
  1359. lib != linkItems.end(); ++lib)
  1360. {
  1361. cmStdString& linkItem = *lib;
  1362. // check to see if the link item has a -l already
  1363. cmsys::RegularExpression reg("^([ \t]*\\-[lLWRBF])|([ \t]*\\-framework)|(\\${)|([ \t]*\\-pthread)|([ \t]*`)");
  1364. if(!reg.find(linkItem))
  1365. {
  1366. librariesLinked += libLinkFlag;
  1367. }
  1368. librariesLinked += linkItem;
  1369. if(linkSuffix.size() && !hasSuffix.find(linkItem))
  1370. {
  1371. librariesLinked += linkSuffix;
  1372. }
  1373. librariesLinked += " ";
  1374. }
  1375. linkLibs += librariesLinked;
  1376. fout << linkLibs;
  1377. if(outputRuntime && runtimeDirs.size()>0)
  1378. {
  1379. // For the runtime search directories, do a "-Wl,-rpath,a:b:c" or
  1380. // a "-R a -R b -R c" type link line
  1381. fout << runtimeFlag;
  1382. std::vector<std::string>::iterator itr = runtimeDirs.begin();
  1383. fout << *itr;
  1384. ++itr;
  1385. for( ; itr != runtimeDirs.end(); ++itr )
  1386. {
  1387. if(runtimeConcatenate)
  1388. {
  1389. fout << runtimeSep << *itr;
  1390. }
  1391. else
  1392. {
  1393. fout << " " << runtimeFlag << *itr;
  1394. }
  1395. }
  1396. fout << " ";
  1397. }
  1398. if(m_Makefile->GetDefinition("CMAKE_STANDARD_LIBRARIES"))
  1399. {
  1400. fout << m_Makefile->GetDefinition("CMAKE_STANDARD_LIBRARIES") << " ";
  1401. }
  1402. }
  1403. //----------------------------------------------------------------------------
  1404. void cmLocalGenerator::AddLanguageFlags(std::string& flags,
  1405. const char* lang)
  1406. {
  1407. // Add language-specific flags.
  1408. std::string flagsVar = "CMAKE_";
  1409. flagsVar += lang;
  1410. flagsVar += "_FLAGS";
  1411. this->AddConfigVariableFlags(flags, flagsVar.c_str());
  1412. }
  1413. //----------------------------------------------------------------------------
  1414. void cmLocalGenerator::AddSharedFlags(std::string& flags,
  1415. const char* lang,
  1416. bool shared)
  1417. {
  1418. std::string flagsVar;
  1419. // Add flags for dealing with shared libraries for this language.
  1420. if(shared)
  1421. {
  1422. flagsVar = "CMAKE_SHARED_LIBRARY_";
  1423. flagsVar += lang;
  1424. flagsVar += "_FLAGS";
  1425. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1426. }
  1427. // Add flags specific to shared builds.
  1428. if(cmSystemTools::IsOn(m_Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1429. {
  1430. flagsVar = "CMAKE_SHARED_BUILD_";
  1431. flagsVar += lang;
  1432. flagsVar += "_FLAGS";
  1433. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1434. }
  1435. }
  1436. //----------------------------------------------------------------------------
  1437. void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
  1438. const char* var)
  1439. {
  1440. // Add the flags from the variable itself.
  1441. std::string flagsVar = var;
  1442. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1443. // Add the flags from the build-type specific variable.
  1444. const char* buildType = m_Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  1445. if(buildType && *buildType)
  1446. {
  1447. flagsVar += "_";
  1448. flagsVar += cmSystemTools::UpperCase(buildType);
  1449. this->AppendFlags(flags, m_Makefile->GetDefinition(flagsVar.c_str()));
  1450. }
  1451. }
  1452. //----------------------------------------------------------------------------
  1453. void cmLocalGenerator::AppendFlags(std::string& flags,
  1454. const char* newFlags)
  1455. {
  1456. if(newFlags && *newFlags)
  1457. {
  1458. if(flags.size())
  1459. {
  1460. flags += " ";
  1461. }
  1462. flags += newFlags;
  1463. }
  1464. }
  1465. //----------------------------------------------------------------------------
  1466. std::string
  1467. cmLocalGenerator::ConstructScript(const cmCustomCommandLines& commandLines,
  1468. const char* newline)
  1469. {
  1470. // Store the script in a string.
  1471. std::string script;
  1472. // Write each command on a single line.
  1473. for(cmCustomCommandLines::const_iterator cl = commandLines.begin();
  1474. cl != commandLines.end(); ++cl)
  1475. {
  1476. // Start with the command name.
  1477. const cmCustomCommandLine& commandLine = *cl;
  1478. script += this->Convert(commandLine[0].c_str(),START_OUTPUT,SHELL);
  1479. // Add the arguments.
  1480. for(unsigned int j=1;j < commandLine.size(); ++j)
  1481. {
  1482. script += " ";
  1483. script += cmSystemTools::EscapeSpaces(commandLine[j].c_str());
  1484. }
  1485. // End the line.
  1486. script += newline;
  1487. }
  1488. return script;
  1489. }
  1490. //----------------------------------------------------------------------------
  1491. std::string
  1492. cmLocalGenerator::ConvertToOptionallyRelativeOutputPath(const char* remote)
  1493. {
  1494. return this->Convert(remote, START_OUTPUT, SHELL, true);
  1495. }
  1496. //----------------------------------------------------------------------------
  1497. std::string cmLocalGenerator::Convert(const char* source,
  1498. RelativeRoot relative,
  1499. OutputFormat output,
  1500. bool optional)
  1501. {
  1502. // Convert the path to a relative path.
  1503. std::string result = source;
  1504. if (!optional || m_UseRelativePaths)
  1505. {
  1506. switch (relative)
  1507. {
  1508. case HOME:
  1509. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1510. result = m_GlobalGenerator->
  1511. ConvertToRelativePath(m_HomeDirectoryComponents, result.c_str());
  1512. break;
  1513. case START:
  1514. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1515. result = m_GlobalGenerator->
  1516. ConvertToRelativePath(m_StartDirectoryComponents, result.c_str());
  1517. break;
  1518. case HOME_OUTPUT:
  1519. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1520. result = m_GlobalGenerator->
  1521. ConvertToRelativePath(m_HomeOutputDirectoryComponents, result.c_str());
  1522. break;
  1523. case START_OUTPUT:
  1524. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1525. result = m_GlobalGenerator->
  1526. ConvertToRelativePath(m_StartOutputDirectoryComponents, result.c_str());
  1527. break;
  1528. case FULL:
  1529. result = cmSystemTools::CollapseFullPath(result.c_str());
  1530. break;
  1531. case NONE:
  1532. break;
  1533. }
  1534. }
  1535. // Now convert it to an output path.
  1536. if (output == MAKEFILE)
  1537. {
  1538. result = cmSystemTools::ConvertToOutputPath(result.c_str());
  1539. }
  1540. if( output == SHELL)
  1541. {
  1542. // for shell commands if force unix is on, but m_WindowsShell
  1543. // is true, then turn off force unix paths for the output path
  1544. // so that the path is windows style and will work with windows
  1545. // cmd.exe.
  1546. bool forceOn = cmSystemTools::GetForceUnixPaths();
  1547. if(forceOn && m_WindowsShell)
  1548. {
  1549. cmSystemTools::SetForceUnixPaths(false);
  1550. }
  1551. result = cmSystemTools::ConvertToOutputPath(result.c_str());
  1552. if(forceOn && m_WindowsShell)
  1553. {
  1554. cmSystemTools::SetForceUnixPaths(true);
  1555. }
  1556. }
  1557. return result;
  1558. }