cmLocalGenerator.cxx 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704
  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 a build then
  824. // include the source and binary directories at the beginning of the
  825. // include path to approximate include file behavior for an
  826. // in-source build. This does not account for the case of a source
  827. // file in a subdirectory of the current source directory but we
  828. // cannot fix this because not all native build tools support
  829. // per-source-file include paths.
  830. if(this->Makefile->IsOn("CMAKE_INCLUDE_CURRENT_DIR"))
  831. {
  832. includeSourceDir = true;
  833. includeBinaryDir = true;
  834. }
  835. // CMake versions below 2.0 would add the source tree to the -I path
  836. // automatically. Preserve compatibility.
  837. const char* versionValue =
  838. this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  839. int major = 0;
  840. int minor = 0;
  841. if(versionValue && sscanf(versionValue, "%d.%d", &major, &minor) != 2)
  842. {
  843. versionValue = 0;
  844. }
  845. if(versionValue && major < 2)
  846. {
  847. includeSourceDir = true;
  848. }
  849. // Hack for VTK 4.0 - 4.4 which depend on the old behavior but do
  850. // not set the backwards compatibility level automatically.
  851. const char* vtkSourceDir =
  852. this->Makefile->GetDefinition("VTK_SOURCE_DIR");
  853. if(vtkSourceDir)
  854. {
  855. const char* vtk_major = this->Makefile->GetDefinition("VTK_MAJOR_VERSION");
  856. const char* vtk_minor = this->Makefile->GetDefinition("VTK_MINOR_VERSION");
  857. vtk_major = vtk_major? vtk_major : "4";
  858. vtk_minor = vtk_minor? vtk_minor : "4";
  859. int vmajor = 0;
  860. int vminor = 0;
  861. if(sscanf(vtk_major, "%d", &vmajor) && sscanf(vtk_minor, "%d", &vminor) &&
  862. vmajor == 4 && vminor <= 4)
  863. {
  864. includeSourceDir = true;
  865. }
  866. }
  867. // Do not repeat an include path.
  868. std::set<cmStdString> emitted;
  869. // Store the automatic include paths.
  870. if(includeBinaryDir)
  871. {
  872. dirs.push_back(this->Makefile->GetStartOutputDirectory());
  873. emitted.insert(this->Makefile->GetStartOutputDirectory());
  874. }
  875. if(includeSourceDir)
  876. {
  877. if(emitted.find(this->Makefile->GetStartDirectory()) == emitted.end())
  878. {
  879. dirs.push_back(this->Makefile->GetStartDirectory());
  880. emitted.insert(this->Makefile->GetStartDirectory());
  881. }
  882. }
  883. // Do not explicitly add the standard include path "/usr/include".
  884. // This can cause problems with certain standard library
  885. // implementations because the wrong headers may be found first.
  886. emitted.insert("/usr/include");
  887. if(const char* implicitIncludes =
  888. this->Makefile->GetDefinition("CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES"))
  889. {
  890. std::vector<std::string> implicitIncludeVec;
  891. cmSystemTools::ExpandListArgument(implicitIncludes, implicitIncludeVec);
  892. for(unsigned int k = 0; k < implicitIncludeVec.size(); ++k)
  893. {
  894. emitted.insert(implicitIncludeVec[k]);
  895. }
  896. }
  897. // Construct the ordered list.
  898. std::vector<std::string>& includes = this->Makefile->GetIncludeDirectories();
  899. for(std::vector<std::string>::iterator i = includes.begin();
  900. i != includes.end(); ++i)
  901. {
  902. if(emitted.find(*i) == emitted.end())
  903. {
  904. dirs.push_back(*i);
  905. emitted.insert(*i);
  906. }
  907. }
  908. }
  909. void cmLocalGenerator::GetTargetFlags(std::string& linkLibs,
  910. std::string& flags,
  911. std::string& linkFlags,
  912. cmTarget& target)
  913. {
  914. std::string buildType = this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  915. buildType = cmSystemTools::UpperCase(buildType);
  916. const char* libraryLinkVariable = "CMAKE_SHARED_LINKER_FLAGS"; // default to shared library
  917. switch(target.GetType())
  918. {
  919. case cmTarget::STATIC_LIBRARY:
  920. {
  921. const char* targetLinkFlags = target.GetProperty("STATIC_LIBRARY_FLAGS");
  922. if(targetLinkFlags)
  923. {
  924. linkFlags += targetLinkFlags;
  925. linkFlags += " ";
  926. }
  927. }
  928. break;
  929. case cmTarget::MODULE_LIBRARY:
  930. libraryLinkVariable = "CMAKE_MODULE_LINKER_FLAGS";
  931. case cmTarget::SHARED_LIBRARY:
  932. {
  933. linkFlags = this->Makefile->GetSafeDefinition(libraryLinkVariable);
  934. linkFlags += " ";
  935. if(buildType.size())
  936. {
  937. std::string build = libraryLinkVariable;
  938. build += "_";
  939. build += buildType;
  940. linkFlags += this->Makefile->GetSafeDefinition(build.c_str());
  941. linkFlags += " ";
  942. }
  943. if(this->Makefile->IsOn("WIN32") && !(this->Makefile->IsOn("CYGWIN") || this->Makefile->IsOn("MINGW")))
  944. {
  945. const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
  946. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  947. i != sources.end(); ++i)
  948. {
  949. if((*i)->GetSourceExtension() == "def")
  950. {
  951. linkFlags += this->Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  952. linkFlags += this->Convert((*i)->GetFullPath().c_str(),START_OUTPUT,MAKEFILE);
  953. linkFlags += " ";
  954. }
  955. }
  956. }
  957. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  958. if(targetLinkFlags)
  959. {
  960. linkFlags += targetLinkFlags;
  961. linkFlags += " ";
  962. }
  963. cmOStringStream linklibsStr;
  964. this->OutputLinkLibraries(linklibsStr, target, false);
  965. linkLibs = linklibsStr.str();
  966. }
  967. break;
  968. case cmTarget::EXECUTABLE:
  969. {
  970. linkFlags += this->Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  971. linkFlags += " ";
  972. if(buildType.size())
  973. {
  974. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  975. build += buildType;
  976. linkFlags += this->Makefile->GetSafeDefinition(build.c_str());
  977. linkFlags += " ";
  978. }
  979. const char* linkLanguage = target.GetLinkerLanguage(this->GetGlobalGenerator());
  980. if(!linkLanguage)
  981. {
  982. cmSystemTools::Error("CMake can not determine linker language for target:",
  983. target.GetName());
  984. return;
  985. }
  986. std::string langVar = "CMAKE_";
  987. langVar += linkLanguage;
  988. std::string flagsVar = langVar + "_FLAGS";
  989. std::string sharedFlagsVar = "CMAKE_SHARED_LIBRARY_";
  990. sharedFlagsVar += linkLanguage;
  991. sharedFlagsVar += "_FLAGS";
  992. flags += this->Makefile->GetSafeDefinition(flagsVar.c_str());
  993. flags += " ";
  994. flags += this->Makefile->GetSafeDefinition(sharedFlagsVar.c_str());
  995. flags += " ";
  996. cmOStringStream linklibs;
  997. this->OutputLinkLibraries(linklibs, target, false);
  998. linkLibs = linklibs.str();
  999. if(cmSystemTools::IsOn(this->Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1000. {
  1001. std::string sFlagVar = std::string("CMAKE_SHARED_BUILD_") + linkLanguage
  1002. + std::string("_FLAGS");
  1003. linkFlags += this->Makefile->GetSafeDefinition(sFlagVar.c_str());
  1004. linkFlags += " ";
  1005. }
  1006. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  1007. {
  1008. linkFlags += this->Makefile->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  1009. linkFlags += " ";
  1010. }
  1011. else
  1012. {
  1013. linkFlags += this->Makefile->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  1014. linkFlags += " ";
  1015. }
  1016. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  1017. if(targetLinkFlags)
  1018. {
  1019. linkFlags += targetLinkFlags;
  1020. linkFlags += " ";
  1021. }
  1022. }
  1023. break;
  1024. case cmTarget::UTILITY:
  1025. case cmTarget::GLOBAL_TARGET:
  1026. case cmTarget::INSTALL_FILES:
  1027. case cmTarget::INSTALL_PROGRAMS:
  1028. break;
  1029. }
  1030. }
  1031. /**
  1032. * Output the linking rules on a command line. For executables,
  1033. * targetLibrary should be a NULL pointer. For libraries, it should point
  1034. * to the name of the library. This will not link a library against itself.
  1035. */
  1036. void cmLocalGenerator::OutputLinkLibraries(std::ostream& fout,
  1037. cmTarget& tgt,
  1038. bool relink)
  1039. {
  1040. // Try to emit each search path once
  1041. std::set<cmStdString> emitted;
  1042. // Embed runtime search paths if possible and if required.
  1043. bool outputRuntime = true;
  1044. std::string runtimeFlag;
  1045. std::string runtimeSep;
  1046. const char* config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  1047. const char* linkLanguage = tgt.GetLinkerLanguage(this->GetGlobalGenerator());
  1048. if(!linkLanguage)
  1049. {
  1050. cmSystemTools::
  1051. Error("CMake can not determine linker language for target:",
  1052. tgt.GetName());
  1053. return;
  1054. }
  1055. std::string runTimeFlagVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
  1056. runTimeFlagVar += linkLanguage;
  1057. runTimeFlagVar += "_FLAG";
  1058. std::string runTimeFlagSepVar = runTimeFlagVar + "_SEP";
  1059. runtimeFlag = this->Makefile->GetSafeDefinition(runTimeFlagVar.c_str());
  1060. runtimeSep = this->Makefile->GetSafeDefinition(runTimeFlagSepVar.c_str());
  1061. // concatenate all paths or no?
  1062. bool runtimeConcatenate = ( runtimeSep!="" );
  1063. if(runtimeFlag == "" || this->Makefile->IsOn("CMAKE_SKIP_RPATH"))
  1064. {
  1065. outputRuntime = false;
  1066. }
  1067. // Some search paths should never be emitted
  1068. emitted.insert("");
  1069. emitted.insert("/usr/lib");
  1070. std::string libPathFlag = this->Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
  1071. std::string libLinkFlag = this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_FLAG");
  1072. // collect all the flags needed for linking libraries
  1073. std::string linkLibs;
  1074. // Flags to link an executable to shared libraries.
  1075. std::string linkFlagsVar = "CMAKE_SHARED_LIBRARY_LINK_";
  1076. linkFlagsVar += linkLanguage;
  1077. linkFlagsVar += "_FLAGS";
  1078. if( tgt.GetType() == cmTarget::EXECUTABLE )
  1079. {
  1080. linkLibs = this->Makefile->GetSafeDefinition(linkFlagsVar.c_str());
  1081. linkLibs += " ";
  1082. }
  1083. // Compute the link library and directory information.
  1084. std::vector<cmStdString> libNames;
  1085. std::vector<cmStdString> libDirs;
  1086. this->ComputeLinkInformation(tgt, config, libNames, libDirs);
  1087. // Select whether to generate an rpath for the install tree or the
  1088. // build tree.
  1089. bool linking_for_install =
  1090. relink || tgt.GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH");
  1091. bool use_install_rpath =
  1092. outputRuntime && tgt.HaveInstallTreeRPATH() && linking_for_install;
  1093. bool use_build_rpath =
  1094. outputRuntime && tgt.HaveBuildTreeRPATH() && !linking_for_install;
  1095. // Construct the RPATH.
  1096. std::vector<std::string> runtimeDirs;
  1097. if(use_install_rpath)
  1098. {
  1099. const char* install_rpath = tgt.GetProperty("INSTALL_RPATH");
  1100. cmSystemTools::ExpandListArgument(install_rpath, runtimeDirs);
  1101. for(unsigned int i=0; i < runtimeDirs.size(); ++i)
  1102. {
  1103. runtimeDirs[i] =
  1104. this->Convert(runtimeDirs[i].c_str(), FULL, SHELL, false);
  1105. }
  1106. }
  1107. // Append the library search path flags.
  1108. for(std::vector<cmStdString>::const_iterator libDir = libDirs.begin();
  1109. libDir != libDirs.end(); ++libDir)
  1110. {
  1111. std::string libpath = this->ConvertToOutputForExisting(libDir->c_str());
  1112. if(emitted.insert(libpath).second)
  1113. {
  1114. std::string fullLibPath;
  1115. if(!this->WindowsShell && this->UseRelativePaths)
  1116. {
  1117. fullLibPath = "\"`cd ";
  1118. }
  1119. fullLibPath += libpath;
  1120. if(!this->WindowsShell && this->UseRelativePaths)
  1121. {
  1122. fullLibPath += ";pwd`\"";
  1123. }
  1124. std::string::size_type pos = libDir->find(libPathFlag.c_str());
  1125. if((pos == std::string::npos || pos > 0)
  1126. && libDir->find("${") == std::string::npos)
  1127. {
  1128. linkLibs += libPathFlag;
  1129. if(use_build_rpath)
  1130. {
  1131. runtimeDirs.push_back( fullLibPath );
  1132. }
  1133. }
  1134. linkLibs += fullLibPath;
  1135. linkLibs += " ";
  1136. }
  1137. }
  1138. // Append the link libraries.
  1139. for(std::vector<cmStdString>::iterator lib = libNames.begin();
  1140. lib != libNames.end(); ++lib)
  1141. {
  1142. linkLibs += *lib;
  1143. linkLibs += " ";
  1144. }
  1145. fout << linkLibs;
  1146. if(!runtimeDirs.empty())
  1147. {
  1148. // For the runtime search directories, do a "-Wl,-rpath,a:b:c" or
  1149. // a "-R a -R b -R c" type link line
  1150. fout << runtimeFlag;
  1151. std::vector<std::string>::iterator itr = runtimeDirs.begin();
  1152. fout << *itr;
  1153. ++itr;
  1154. for( ; itr != runtimeDirs.end(); ++itr )
  1155. {
  1156. if(runtimeConcatenate)
  1157. {
  1158. fout << runtimeSep << *itr;
  1159. }
  1160. else
  1161. {
  1162. fout << " " << runtimeFlag << *itr;
  1163. }
  1164. }
  1165. fout << " ";
  1166. }
  1167. if(this->Makefile->GetDefinition("CMAKE_STANDARD_LIBRARIES"))
  1168. {
  1169. fout << this->Makefile->GetDefinition("CMAKE_STANDARD_LIBRARIES") << " ";
  1170. }
  1171. }
  1172. //----------------------------------------------------------------------------
  1173. void
  1174. cmLocalGenerator::ComputeLinkInformation(cmTarget& target,
  1175. const char* config,
  1176. std::vector<cmStdString>& outLibs,
  1177. std::vector<cmStdString>& outDirs,
  1178. std::vector<cmStdString>* fullPathLibs)
  1179. {
  1180. // Compute which library configuration to link.
  1181. cmTarget::LinkLibraryType linkType = cmTarget::OPTIMIZED;
  1182. if(config && cmSystemTools::UpperCase(config) == "DEBUG")
  1183. {
  1184. linkType = cmTarget::DEBUG;
  1185. }
  1186. // Get the list of libraries against which this target wants to link.
  1187. std::vector<std::string> linkLibraries;
  1188. const cmTarget::LinkLibraryVectorType& inLibs = target.GetLinkLibraries();
  1189. for(cmTarget::LinkLibraryVectorType::const_iterator j = inLibs.begin();
  1190. j != inLibs.end(); ++j)
  1191. {
  1192. // For backwards compatibility variables may have been expanded
  1193. // inside library names. Clean up the resulting name.
  1194. std::string lib = j->first;
  1195. std::string::size_type pos = lib.find_first_not_of(" \t\r\n");
  1196. if(pos != lib.npos)
  1197. {
  1198. lib = lib.substr(pos, lib.npos);
  1199. }
  1200. pos = lib.find_last_not_of(" \t\r\n");
  1201. if(pos != lib.npos)
  1202. {
  1203. lib = lib.substr(0, pos+1);
  1204. }
  1205. if(lib.empty())
  1206. {
  1207. continue;
  1208. }
  1209. // Link to a library if it is not the same target and is meant for
  1210. // this configuration type.
  1211. if((target.GetType() == cmTarget::EXECUTABLE ||
  1212. lib != target.GetName()) &&
  1213. (j->second == cmTarget::GENERAL || j->second == linkType))
  1214. {
  1215. // Compute the proper name to use to link this library.
  1216. cmTarget* tgt = this->GlobalGenerator->FindTarget(0, lib.c_str());
  1217. if(tgt)
  1218. {
  1219. // This is a CMake target. Ask the target for its real name.
  1220. linkLibraries.push_back(tgt->GetFullName(config));
  1221. if(fullPathLibs)
  1222. {
  1223. fullPathLibs->push_back(tgt->GetFullPath(config));
  1224. }
  1225. }
  1226. else
  1227. {
  1228. // This is not a CMake target. Use the name given.
  1229. linkLibraries.push_back(lib);
  1230. }
  1231. }
  1232. }
  1233. // Get the list of directories the target wants to search for libraries.
  1234. const std::vector<std::string>&
  1235. linkDirectories = target.GetLinkDirectories();
  1236. // Compute the link directory order needed to link the libraries.
  1237. cmOrderLinkDirectories orderLibs;
  1238. orderLibs.SetLinkPrefix(
  1239. this->Makefile->GetDefinition("CMAKE_STATIC_LIBRARY_PREFIX"));
  1240. orderLibs.AddLinkExtension(
  1241. this->Makefile->GetDefinition("CMAKE_STATIC_LIBRARY_SUFFIX"));
  1242. orderLibs.AddLinkExtension(
  1243. this->Makefile->GetDefinition("CMAKE_SHARED_LIBRARY_SUFFIX"));
  1244. orderLibs.AddLinkExtension(
  1245. this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"));
  1246. orderLibs.AddLinkExtension(
  1247. this->Makefile->GetDefinition("CMAKE_LINK_LIBRARY_SUFFIX"));
  1248. if(const char* linkSuffixes =
  1249. this->Makefile->GetDefinition("CMAKE_EXTRA_LINK_EXTENSIONS"))
  1250. {
  1251. std::vector<std::string> linkSuffixVec;
  1252. cmSystemTools::ExpandListArgument(linkSuffixes, linkSuffixVec);
  1253. for(std::vector<std::string>::iterator i = linkSuffixVec.begin();
  1254. i != linkSuffixVec.end(); ++i)
  1255. {
  1256. orderLibs.AddLinkExtension(i->c_str());
  1257. }
  1258. }
  1259. orderLibs.SetLinkInformation(target.GetName(),
  1260. linkLibraries,
  1261. linkDirectories);
  1262. orderLibs.DetermineLibraryPathOrder();
  1263. std::vector<cmStdString> orderedLibs;
  1264. orderLibs.GetLinkerInformation(outDirs, orderedLibs);
  1265. if(fullPathLibs)
  1266. {
  1267. orderLibs.GetFullPathLibraries(*fullPathLibs);
  1268. }
  1269. // Make sure libraries are linked with the proper syntax.
  1270. std::string libLinkFlag =
  1271. this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_FLAG");
  1272. std::string libLinkSuffix =
  1273. this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_SUFFIX");
  1274. for(std::vector<cmStdString>::iterator l = orderedLibs.begin();
  1275. l != orderedLibs.end(); ++l)
  1276. {
  1277. std::string lib = *l;
  1278. if(lib[0] == '-' || lib[0] == '$' || lib[0] == '`')
  1279. {
  1280. // The library is linked with special syntax by the user.
  1281. outLibs.push_back(lib);
  1282. }
  1283. else
  1284. {
  1285. // Generate the proper link syntax.
  1286. lib = libLinkFlag;
  1287. lib += *l;
  1288. lib += libLinkSuffix;
  1289. outLibs.push_back(lib);
  1290. }
  1291. }
  1292. }
  1293. //----------------------------------------------------------------------------
  1294. void cmLocalGenerator::AddLanguageFlags(std::string& flags,
  1295. const char* lang,
  1296. const char* config)
  1297. {
  1298. // Add language-specific flags.
  1299. std::string flagsVar = "CMAKE_";
  1300. flagsVar += lang;
  1301. flagsVar += "_FLAGS";
  1302. this->AddConfigVariableFlags(flags, flagsVar.c_str(), config);
  1303. }
  1304. //----------------------------------------------------------------------------
  1305. std::string cmLocalGenerator::GetRealDependency(const char* inName,
  1306. const char* config)
  1307. {
  1308. // Older CMake code may specify the dependency using the target
  1309. // output file rather than the target name. Such code would have
  1310. // been written before there was support for target properties that
  1311. // modify the name so stripping down to just the file name should
  1312. // produce the target name in this case.
  1313. std::string name = cmSystemTools::GetFilenameName(inName);
  1314. if(cmSystemTools::GetFilenameLastExtension(name) == ".exe")
  1315. {
  1316. name = cmSystemTools::GetFilenameWithoutLastExtension(name);
  1317. }
  1318. // Look for a CMake target with the given name.
  1319. if(cmTarget* target = this->GlobalGenerator->FindTarget(0, name.c_str()))
  1320. {
  1321. switch (target->GetType())
  1322. {
  1323. case cmTarget::EXECUTABLE:
  1324. case cmTarget::STATIC_LIBRARY:
  1325. case cmTarget::SHARED_LIBRARY:
  1326. case cmTarget::MODULE_LIBRARY:
  1327. {
  1328. // Get the location of the target's output file and depend on it.
  1329. if(const char* location = target->GetLocation(config))
  1330. {
  1331. return location;
  1332. }
  1333. }
  1334. break;
  1335. case cmTarget::UTILITY:
  1336. case cmTarget::GLOBAL_TARGET:
  1337. // Depending on a utility target may not work but just trust
  1338. // the user to have given a valid name.
  1339. return inName;
  1340. case cmTarget::INSTALL_FILES:
  1341. case cmTarget::INSTALL_PROGRAMS:
  1342. break;
  1343. }
  1344. }
  1345. // The name was not that of a CMake target. It must name a file.
  1346. if(cmSystemTools::FileIsFullPath(inName))
  1347. {
  1348. // This is a full path. Return it as given.
  1349. return inName;
  1350. }
  1351. // Treat the name as relative to the source directory in which it
  1352. // was given.
  1353. name = this->Makefile->GetCurrentDirectory();
  1354. name += "/";
  1355. name += inName;
  1356. return name;
  1357. }
  1358. //----------------------------------------------------------------------------
  1359. void cmLocalGenerator::AddSharedFlags(std::string& flags,
  1360. const char* lang,
  1361. bool shared)
  1362. {
  1363. std::string flagsVar;
  1364. // Add flags for dealing with shared libraries for this language.
  1365. if(shared)
  1366. {
  1367. flagsVar = "CMAKE_SHARED_LIBRARY_";
  1368. flagsVar += lang;
  1369. flagsVar += "_FLAGS";
  1370. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  1371. }
  1372. // Add flags specific to shared builds.
  1373. if(cmSystemTools::IsOn(this->Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1374. {
  1375. flagsVar = "CMAKE_SHARED_BUILD_";
  1376. flagsVar += lang;
  1377. flagsVar += "_FLAGS";
  1378. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  1379. }
  1380. }
  1381. //----------------------------------------------------------------------------
  1382. void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
  1383. const char* var,
  1384. const char* config)
  1385. {
  1386. // Add the flags from the variable itself.
  1387. std::string flagsVar = var;
  1388. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  1389. // Add the flags from the build-type specific variable.
  1390. if(config && *config)
  1391. {
  1392. flagsVar += "_";
  1393. flagsVar += cmSystemTools::UpperCase(config);
  1394. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  1395. }
  1396. }
  1397. //----------------------------------------------------------------------------
  1398. void cmLocalGenerator::AppendFlags(std::string& flags,
  1399. const char* newFlags)
  1400. {
  1401. if(newFlags && *newFlags)
  1402. {
  1403. if(flags.size())
  1404. {
  1405. flags += " ";
  1406. }
  1407. flags += newFlags;
  1408. }
  1409. }
  1410. //----------------------------------------------------------------------------
  1411. std::string
  1412. cmLocalGenerator::ConstructScript(const cmCustomCommandLines& commandLines,
  1413. const char* workingDirectory,
  1414. const char* newline)
  1415. {
  1416. // Store the script in a string.
  1417. std::string script;
  1418. if(workingDirectory)
  1419. {
  1420. script += "cd ";
  1421. script += this->Convert(workingDirectory, START_OUTPUT, SHELL);
  1422. script += newline;
  1423. }
  1424. // Write each command on a single line.
  1425. for(cmCustomCommandLines::const_iterator cl = commandLines.begin();
  1426. cl != commandLines.end(); ++cl)
  1427. {
  1428. // Start with the command name.
  1429. const cmCustomCommandLine& commandLine = *cl;
  1430. script += this->Convert(commandLine[0].c_str(),START_OUTPUT,SHELL);
  1431. // Add the arguments.
  1432. for(unsigned int j=1;j < commandLine.size(); ++j)
  1433. {
  1434. script += " ";
  1435. script += cmSystemTools::EscapeSpaces(commandLine[j].c_str());
  1436. }
  1437. // End the line.
  1438. script += newline;
  1439. }
  1440. return script;
  1441. }
  1442. //----------------------------------------------------------------------------
  1443. std::string
  1444. cmLocalGenerator::ConvertToOptionallyRelativeOutputPath(const char* remote)
  1445. {
  1446. return this->Convert(remote, START_OUTPUT, SHELL, true);
  1447. }
  1448. //----------------------------------------------------------------------------
  1449. std::string cmLocalGenerator::Convert(const char* source,
  1450. RelativeRoot relative,
  1451. OutputFormat output,
  1452. bool optional)
  1453. {
  1454. // Convert the path to a relative path.
  1455. std::string result = source;
  1456. if (!optional || this->UseRelativePaths)
  1457. {
  1458. switch (relative)
  1459. {
  1460. case HOME:
  1461. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1462. result = this->GlobalGenerator->
  1463. ConvertToRelativePath(this->HomeDirectoryComponents, result.c_str());
  1464. break;
  1465. case START:
  1466. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1467. result = this->GlobalGenerator->
  1468. ConvertToRelativePath(this->StartDirectoryComponents, result.c_str());
  1469. break;
  1470. case HOME_OUTPUT:
  1471. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1472. result = this->GlobalGenerator->
  1473. ConvertToRelativePath(this->HomeOutputDirectoryComponents, result.c_str());
  1474. break;
  1475. case START_OUTPUT:
  1476. //result = cmSystemTools::CollapseFullPath(result.c_str());
  1477. result = this->GlobalGenerator->
  1478. ConvertToRelativePath(this->StartOutputDirectoryComponents, result.c_str());
  1479. break;
  1480. case FULL:
  1481. result = cmSystemTools::CollapseFullPath(result.c_str());
  1482. break;
  1483. case NONE:
  1484. break;
  1485. }
  1486. }
  1487. // Now convert it to an output path.
  1488. if (output == MAKEFILE)
  1489. {
  1490. result = cmSystemTools::ConvertToOutputPath(result.c_str());
  1491. }
  1492. if( output == SHELL)
  1493. {
  1494. // for shell commands if force unix is on, but this->WindowsShell
  1495. // is true, then turn off force unix paths for the output path
  1496. // so that the path is windows style and will work with windows
  1497. // cmd.exe.
  1498. bool forceOn = cmSystemTools::GetForceUnixPaths();
  1499. if(forceOn && this->WindowsShell)
  1500. {
  1501. cmSystemTools::SetForceUnixPaths(false);
  1502. }
  1503. result = cmSystemTools::ConvertToOutputPath(result.c_str());
  1504. if(forceOn && this->WindowsShell)
  1505. {
  1506. cmSystemTools::SetForceUnixPaths(true);
  1507. }
  1508. }
  1509. return result;
  1510. }
  1511. //----------------------------------------------------------------------------
  1512. void
  1513. cmLocalGenerator
  1514. ::GenerateTargetInstallRules(
  1515. std::ostream& os, const char* config,
  1516. std::vector<std::string> const& configurationTypes)
  1517. {
  1518. // Convert the old-style install specification from each target to
  1519. // an install generator and run it.
  1520. cmTargets& tgts = this->Makefile->GetTargets();
  1521. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); ++l)
  1522. {
  1523. // Include the user-specified pre-install script for this target.
  1524. if(const char* preinstall = l->second.GetProperty("PRE_INSTALL_SCRIPT"))
  1525. {
  1526. cmInstallScriptGenerator g(preinstall);
  1527. g.Generate(os, config, configurationTypes);
  1528. }
  1529. // Install this target if a destination is given.
  1530. if(l->second.GetInstallPath() != "")
  1531. {
  1532. // Compute the full install destination. Note that converting
  1533. // to unix slashes also removes any trailing slash.
  1534. std::string destination = "${CMAKE_INSTALL_PREFIX}";
  1535. destination += l->second.GetInstallPath();
  1536. cmSystemTools::ConvertToUnixSlashes(destination);
  1537. // Generate the proper install generator for this target type.
  1538. switch(l->second.GetType())
  1539. {
  1540. case cmTarget::EXECUTABLE:
  1541. case cmTarget::STATIC_LIBRARY:
  1542. case cmTarget::MODULE_LIBRARY:
  1543. {
  1544. // Use a target install generator.
  1545. cmInstallTargetGenerator g(l->second, destination.c_str(), false);
  1546. g.Generate(os, config, configurationTypes);
  1547. }
  1548. break;
  1549. case cmTarget::SHARED_LIBRARY:
  1550. {
  1551. #if defined(_WIN32) || defined(__CYGWIN__)
  1552. // Special code to handle DLL. Install the import library
  1553. // to the normal destination and the DLL to the runtime
  1554. // destination.
  1555. cmInstallTargetGenerator g1(l->second, destination.c_str(), true);
  1556. g1.Generate(os, config, configurationTypes);
  1557. destination = "${CMAKE_INSTALL_PREFIX}";
  1558. destination += l->second.GetRuntimeInstallPath();
  1559. cmSystemTools::ConvertToUnixSlashes(destination);
  1560. cmInstallTargetGenerator g2(l->second, destination.c_str(), false);
  1561. g2.Generate(os, config, configurationTypes);
  1562. #else
  1563. // Use a target install generator.
  1564. cmInstallTargetGenerator g(l->second, destination.c_str(), false);
  1565. g.Generate(os, config, configurationTypes);
  1566. #endif
  1567. }
  1568. break;
  1569. case cmTarget::INSTALL_FILES:
  1570. {
  1571. // Use a file install generator.
  1572. const char* no_permissions = "";
  1573. const char* no_rename = "";
  1574. cmInstallFilesGenerator g(l->second.GetSourceLists(),
  1575. destination.c_str(), false,
  1576. no_permissions, no_rename);
  1577. g.Generate(os, config, configurationTypes);
  1578. }
  1579. break;
  1580. case cmTarget::INSTALL_PROGRAMS:
  1581. {
  1582. // Use a file install generator.
  1583. const char* no_permissions = "";
  1584. const char* no_rename = "";
  1585. cmInstallFilesGenerator g(l->second.GetSourceLists(),
  1586. destination.c_str(), true,
  1587. no_permissions, no_rename);
  1588. g.Generate(os, config, configurationTypes);
  1589. }
  1590. break;
  1591. case cmTarget::UTILITY:
  1592. default:
  1593. break;
  1594. }
  1595. }
  1596. // Include the user-specified post-install script for this target.
  1597. if(const char* postinstall = l->second.GetProperty("POST_INSTALL_SCRIPT"))
  1598. {
  1599. cmInstallScriptGenerator g(postinstall);
  1600. g.Generate(os, config, configurationTypes);
  1601. }
  1602. }
  1603. }