cmLocalGenerator.cxx 48 KB

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