cmLocalGenerator.cxx 53 KB

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