cmLocalGenerator.cxx 54 KB

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