cmLocalGenerator.cxx 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782
  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. #if defined(CMAKE_BUILD_WITH_CMAKE)
  26. # define CM_LG_ENCODE_OBJECT_NAMES
  27. # include <cmsys/MD5.h>
  28. #endif
  29. #include <cmsys/System.h>
  30. #include <ctype.h> // for isalpha
  31. #include <assert.h>
  32. cmLocalGenerator::cmLocalGenerator()
  33. {
  34. this->Makefile = new cmMakefile;
  35. this->Makefile->SetLocalGenerator(this);
  36. this->Parent = 0;
  37. this->WindowsShell = false;
  38. this->WindowsVSIDE = false;
  39. this->WatcomWMake = false;
  40. this->MinGWMake = false;
  41. this->MSYSShell = false;
  42. this->IgnoreLibPrefix = false;
  43. this->UseRelativePaths = false;
  44. this->Configured = false;
  45. this->EmitUniversalBinaryFlags = true;
  46. this->IsMakefileGenerator = false;
  47. this->RelativePathsConfigured = false;
  48. this->PathConversionsSetup = false;
  49. }
  50. cmLocalGenerator::~cmLocalGenerator()
  51. {
  52. delete this->Makefile;
  53. }
  54. void cmLocalGenerator::Configure()
  55. {
  56. cmLocalGenerator* previousLg = this->GetGlobalGenerator()->GetCurrentLocalGenerator();
  57. this->GetGlobalGenerator()->SetCurrentLocalGenerator(this);
  58. // make sure the CMakeFiles dir is there
  59. std::string filesDir = this->Makefile->GetStartOutputDirectory();
  60. filesDir += cmake::GetCMakeFilesDirectory();
  61. cmSystemTools::MakeDirectory(filesDir.c_str());
  62. // find & read the list file
  63. std::string currentStart = this->Makefile->GetStartDirectory();
  64. currentStart += "/CMakeLists.txt";
  65. this->Makefile->ReadListFile(currentStart.c_str());
  66. // at the end of the ReadListFile handle any old style subdirs
  67. // first get all the subdirectories
  68. std::vector<cmLocalGenerator *> subdirs = this->GetChildren();
  69. // for each subdir recurse
  70. std::vector<cmLocalGenerator *>::iterator sdi = subdirs.begin();
  71. for (; sdi != subdirs.end(); ++sdi)
  72. {
  73. if (!(*sdi)->Configured)
  74. {
  75. this->Makefile->ConfigureSubDirectory(*sdi);
  76. }
  77. }
  78. // Check whether relative paths should be used for optionally
  79. // relative paths.
  80. this->UseRelativePaths = this->Makefile->IsOn("CMAKE_USE_RELATIVE_PATHS");
  81. this->Configured = true;
  82. this->GetGlobalGenerator()->SetCurrentLocalGenerator(previousLg);
  83. }
  84. void cmLocalGenerator::SetupPathConversions()
  85. {
  86. // Setup the current output directory components for use by
  87. // Convert
  88. std::string outdir;
  89. outdir =
  90. cmSystemTools::CollapseFullPath(this->Makefile->GetHomeDirectory());
  91. cmSystemTools::SplitPath(outdir.c_str(), this->HomeDirectoryComponents);
  92. outdir =
  93. cmSystemTools::CollapseFullPath(this->Makefile->GetStartDirectory());
  94. cmSystemTools::SplitPath(outdir.c_str(), this->StartDirectoryComponents);
  95. outdir = cmSystemTools::CollapseFullPath
  96. (this->Makefile->GetHomeOutputDirectory());
  97. cmSystemTools::SplitPath(outdir.c_str(),
  98. this->HomeOutputDirectoryComponents);
  99. outdir = cmSystemTools::CollapseFullPath
  100. (this->Makefile->GetStartOutputDirectory());
  101. cmSystemTools::SplitPath(outdir.c_str(),
  102. this->StartOutputDirectoryComponents);
  103. }
  104. void cmLocalGenerator::SetGlobalGenerator(cmGlobalGenerator *gg)
  105. {
  106. this->GlobalGenerator = gg;
  107. // setup the home directories
  108. this->Makefile->GetProperties().SetCMakeInstance(gg->GetCMakeInstance());
  109. this->Makefile->SetHomeDirectory(
  110. gg->GetCMakeInstance()->GetHomeDirectory());
  111. this->Makefile->SetHomeOutputDirectory(
  112. gg->GetCMakeInstance()->GetHomeOutputDirectory());
  113. }
  114. void cmLocalGenerator::ConfigureFinalPass()
  115. {
  116. this->Makefile->ConfigureFinalPass();
  117. }
  118. void cmLocalGenerator::TraceDependencies()
  119. {
  120. // Generate the rule files for each target.
  121. cmTargets& targets = this->Makefile->GetTargets();
  122. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  123. {
  124. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  125. // so don't build a projectfile for it
  126. if (strncmp(t->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0)
  127. {
  128. const char* projectFilename = 0;
  129. if (this->IsMakefileGenerator == false) // only use of this variable
  130. {
  131. projectFilename = t->second.GetName();
  132. }
  133. t->second.TraceDependencies(projectFilename);
  134. }
  135. }
  136. }
  137. void cmLocalGenerator::GenerateTestFiles()
  138. {
  139. if ( !this->Makefile->IsOn("CMAKE_TESTING_ENABLED") )
  140. {
  141. return;
  142. }
  143. std::string file = this->Makefile->GetStartOutputDirectory();
  144. file += "/";
  145. if ( this->Makefile->IsSet("CTEST_NEW_FORMAT") )
  146. {
  147. file += "CTestTestfile.cmake";
  148. }
  149. else
  150. {
  151. file += "DartTestfile.txt";
  152. }
  153. cmGeneratedFileStream fout(file.c_str());
  154. fout.SetCopyIfDifferent(true);
  155. fout << "# CMake generated Testfile for " << std::endl
  156. << "# Source directory: "
  157. << this->Makefile->GetStartDirectory() << std::endl
  158. << "# Build directory: "
  159. << this->Makefile->GetStartOutputDirectory() << std::endl
  160. << "# " << std::endl
  161. << "# This file replicates the SUBDIRS() and ADD_TEST() commands "
  162. << "from the source" << std::endl
  163. << "# tree CMakeLists.txt file, skipping any SUBDIRS() or "
  164. << "ADD_TEST() commands" << std::endl
  165. << "# that are excluded by CMake control structures, i.e. IF() "
  166. << "commands." << std::endl
  167. << "#" << std::endl
  168. << "# The next line is critical for Dart to work" << std::endl
  169. << "# Duh :-)" << std::endl << std::endl;
  170. const char* testIncludeFile =
  171. this->Makefile->GetProperty("TEST_INCLUDE_FILE");
  172. if ( testIncludeFile )
  173. {
  174. fout << "INCLUDE(\"" << testIncludeFile << "\")" << std::endl;
  175. }
  176. const std::vector<cmTest*> *tests = this->Makefile->GetTests();
  177. std::vector<cmTest*>::const_iterator it;
  178. for ( it = tests->begin(); it != tests->end(); ++ it )
  179. {
  180. cmTest* test = *it;
  181. fout << "ADD_TEST(";
  182. fout << test->GetName() << " \"" << test->GetCommand() << "\"";
  183. std::vector<cmStdString>::const_iterator argit;
  184. for (argit = test->GetArguments().begin();
  185. argit != test->GetArguments().end(); ++argit)
  186. {
  187. // Just double-quote all arguments so they are re-parsed
  188. // correctly by the test system.
  189. fout << " \"";
  190. for(std::string::const_iterator c = argit->begin();
  191. c != argit->end(); ++c)
  192. {
  193. // Escape quotes within arguments. We should escape
  194. // backslashes too but we cannot because it makes the result
  195. // inconsistent with previous behavior of this command.
  196. if((*c == '"'))
  197. {
  198. fout << '\\';
  199. }
  200. fout << *c;
  201. }
  202. fout << "\"";
  203. }
  204. fout << ")" << std::endl;
  205. cmPropertyMap::const_iterator pit;
  206. cmPropertyMap* mpit = &test->GetProperties();
  207. if ( mpit->size() )
  208. {
  209. fout << "SET_TESTS_PROPERTIES(" << test->GetName() << " PROPERTIES ";
  210. for ( pit = mpit->begin(); pit != mpit->end(); ++ pit )
  211. {
  212. fout << " " << pit->first.c_str() << " \"";
  213. const char* value = pit->second.GetValue();
  214. for ( ; *value; ++ value )
  215. {
  216. switch ( *value )
  217. {
  218. case '\\':
  219. case '"':
  220. case ' ':
  221. case '#':
  222. case '(':
  223. case ')':
  224. case '$':
  225. case '^':
  226. fout << "\\" << *value;
  227. break;
  228. case '\t':
  229. fout << "\\t";
  230. break;
  231. case '\n':
  232. fout << "\\n";
  233. break;
  234. case '\r':
  235. fout << "\\r";
  236. break;
  237. default:
  238. fout << *value;
  239. }
  240. }
  241. fout << "\"";
  242. }
  243. fout << ")" << std::endl;
  244. }
  245. }
  246. if ( this->Children.size())
  247. {
  248. fout << "SUBDIRS(";
  249. size_t i;
  250. std::string outDir = this->Makefile->GetStartOutputDirectory();
  251. outDir += "/";
  252. for(i = 0; i < this->Children.size(); ++i)
  253. {
  254. std::string binP =
  255. this->Children[i]->GetMakefile()->GetStartOutputDirectory();
  256. cmSystemTools::ReplaceString(binP, outDir.c_str(), "");
  257. if ( i > 0 )
  258. {
  259. fout << " ";
  260. }
  261. fout << binP.c_str();
  262. }
  263. fout << ")" << std::endl << std::endl;;
  264. }
  265. }
  266. //----------------------------------------------------------------------------
  267. void cmLocalGenerator::GenerateInstallRules()
  268. {
  269. // Compute the install prefix.
  270. const char* prefix = this->Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  271. #if defined(_WIN32) && !defined(__CYGWIN__)
  272. std::string prefix_win32;
  273. if(!prefix)
  274. {
  275. if(!cmSystemTools::GetEnv("SystemDrive", prefix_win32))
  276. {
  277. prefix_win32 = "C:";
  278. }
  279. const char* project_name = this->Makefile->GetDefinition("PROJECT_NAME");
  280. if(project_name && project_name[0])
  281. {
  282. prefix_win32 += "/Program Files/";
  283. prefix_win32 += project_name;
  284. }
  285. else
  286. {
  287. prefix_win32 += "/InstalledCMakeProject";
  288. }
  289. prefix = prefix_win32.c_str();
  290. }
  291. #else
  292. if (!prefix)
  293. {
  294. prefix = "/usr/local";
  295. }
  296. #endif
  297. // Compute the set of configurations.
  298. std::vector<std::string> configurationTypes;
  299. if(const char* types =
  300. this->Makefile->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
  301. {
  302. cmSystemTools::ExpandListArgument(types, configurationTypes);
  303. }
  304. const char* config = 0;
  305. if(configurationTypes.empty())
  306. {
  307. config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  308. }
  309. // Choose a default install configuration.
  310. const char* default_config = config;
  311. const char* default_order[] = {"RELEASE", "MINSIZEREL",
  312. "RELWITHDEBINFO", "DEBUG", 0};
  313. for(const char** c = default_order; *c && !default_config; ++c)
  314. {
  315. for(std::vector<std::string>::iterator i = configurationTypes.begin();
  316. i != configurationTypes.end(); ++i)
  317. {
  318. if(cmSystemTools::UpperCase(*i) == *c)
  319. {
  320. default_config = i->c_str();
  321. }
  322. }
  323. }
  324. if(!default_config && !configurationTypes.empty())
  325. {
  326. default_config = configurationTypes[0].c_str();
  327. }
  328. if(!default_config)
  329. {
  330. default_config = "Release";
  331. }
  332. // Create the install script file.
  333. std::string file = this->Makefile->GetStartOutputDirectory();
  334. std::string homedir = this->Makefile->GetHomeOutputDirectory();
  335. std::string currdir = this->Makefile->GetCurrentOutputDirectory();
  336. cmSystemTools::ConvertToUnixSlashes(file);
  337. cmSystemTools::ConvertToUnixSlashes(homedir);
  338. cmSystemTools::ConvertToUnixSlashes(currdir);
  339. int toplevel_install = 0;
  340. if ( currdir == homedir )
  341. {
  342. toplevel_install = 1;
  343. }
  344. file += "/cmake_install.cmake";
  345. cmGeneratedFileStream fout(file.c_str());
  346. fout.SetCopyIfDifferent(true);
  347. // Write the header.
  348. fout << "# Install script for directory: "
  349. << this->Makefile->GetCurrentDirectory() << std::endl << std::endl;
  350. fout << "# Set the install prefix" << std::endl
  351. << "IF(NOT DEFINED CMAKE_INSTALL_PREFIX)" << std::endl
  352. << " SET(CMAKE_INSTALL_PREFIX \"" << prefix << "\")" << std::endl
  353. << "ENDIF(NOT DEFINED CMAKE_INSTALL_PREFIX)" << std::endl
  354. << "STRING(REGEX REPLACE \"/$\" \"\" CMAKE_INSTALL_PREFIX "
  355. << "\"${CMAKE_INSTALL_PREFIX}\")" << std::endl
  356. << std::endl;
  357. // Write support code for generating per-configuration install rules.
  358. fout <<
  359. "# Set the install configuration name.\n"
  360. "IF(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)\n"
  361. " IF(BUILD_TYPE)\n"
  362. " STRING(REGEX REPLACE \"^[^A-Za-z0-9_]+\" \"\"\n"
  363. " CMAKE_INSTALL_CONFIG_NAME \"${BUILD_TYPE}\")\n"
  364. " ELSE(BUILD_TYPE)\n"
  365. " SET(CMAKE_INSTALL_CONFIG_NAME \"" << default_config << "\")\n"
  366. " ENDIF(BUILD_TYPE)\n"
  367. " MESSAGE(STATUS \"Install configuration: "
  368. "\\\"${CMAKE_INSTALL_CONFIG_NAME}\\\"\")\n"
  369. "ENDIF(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)\n"
  370. "\n";
  371. // Write support code for dealing with component-specific installs.
  372. fout <<
  373. "# Set the component getting installed.\n"
  374. "IF(NOT CMAKE_INSTALL_COMPONENT)\n"
  375. " IF(COMPONENT)\n"
  376. " MESSAGE(STATUS \"Install component: \\\"${COMPONENT}\\\"\")\n"
  377. " SET(CMAKE_INSTALL_COMPONENT \"${COMPONENT}\")\n"
  378. " ELSE(COMPONENT)\n"
  379. " SET(CMAKE_INSTALL_COMPONENT)\n"
  380. " ENDIF(COMPONENT)\n"
  381. "ENDIF(NOT CMAKE_INSTALL_COMPONENT)\n"
  382. "\n";
  383. // Copy user-specified install options to the install code.
  384. if(const char* so_no_exe =
  385. this->Makefile->GetDefinition("CMAKE_INSTALL_SO_NO_EXE"))
  386. {
  387. fout <<
  388. "# Install shared libraries without execute permission?\n"
  389. "IF(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)\n"
  390. " SET(CMAKE_INSTALL_SO_NO_EXE \"" << so_no_exe << "\")\n"
  391. "ENDIF(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)\n"
  392. "\n";
  393. }
  394. // Ask each install generator to write its code.
  395. std::vector<cmInstallGenerator*> const& installers =
  396. this->Makefile->GetInstallGenerators();
  397. for(std::vector<cmInstallGenerator*>::const_iterator
  398. gi = installers.begin();
  399. gi != installers.end(); ++gi)
  400. {
  401. (*gi)->Generate(fout, config, configurationTypes);
  402. }
  403. // Write rules from old-style specification stored in targets.
  404. this->GenerateTargetInstallRules(fout, config, configurationTypes);
  405. // Include install scripts from subdirectories.
  406. if(!this->Children.empty())
  407. {
  408. fout << "IF(NOT CMAKE_INSTALL_LOCAL_ONLY)\n";
  409. fout << " # Include the install script for each subdirectory.\n";
  410. for(std::vector<cmLocalGenerator*>::const_iterator
  411. ci = this->Children.begin(); ci != this->Children.end(); ++ci)
  412. {
  413. if(!(*ci)->GetMakefile()->GetPropertyAsBool("EXCLUDE_FROM_ALL"))
  414. {
  415. std::string odir = (*ci)->GetMakefile()->GetStartOutputDirectory();
  416. cmSystemTools::ConvertToUnixSlashes(odir);
  417. fout << " INCLUDE(\"" << odir.c_str()
  418. << "/cmake_install.cmake\")" << std::endl;
  419. }
  420. }
  421. fout << "\n";
  422. fout << "ENDIF(NOT CMAKE_INSTALL_LOCAL_ONLY)\n";
  423. }
  424. // Record the install manifest.
  425. if ( toplevel_install )
  426. {
  427. fout <<
  428. "IF(CMAKE_INSTALL_COMPONENT)\n"
  429. " SET(CMAKE_INSTALL_MANIFEST \"install_manifest_"
  430. "${CMAKE_INSTALL_COMPONENT}.txt\")\n"
  431. "ELSE(CMAKE_INSTALL_COMPONENT)\n"
  432. " SET(CMAKE_INSTALL_MANIFEST \"install_manifest.txt\")\n"
  433. "ENDIF(CMAKE_INSTALL_COMPONENT)\n";
  434. fout
  435. << "FILE(WRITE \""
  436. << homedir.c_str() << "/${CMAKE_INSTALL_MANIFEST}\" "
  437. << "\"\")" << std::endl;
  438. fout
  439. << "FOREACH(file ${CMAKE_INSTALL_MANIFEST_FILES})" << std::endl
  440. << " FILE(APPEND \""
  441. << homedir.c_str() << "/${CMAKE_INSTALL_MANIFEST}\" "
  442. << "\"${file}\\n\")" << std::endl
  443. << "ENDFOREACH(file)" << std::endl;
  444. }
  445. }
  446. //----------------------------------------------------------------------------
  447. void cmLocalGenerator::GenerateTargetManifest(cmTargetManifest& manifest)
  448. {
  449. // Collect the set of configuration types.
  450. std::vector<std::string> configNames;
  451. if(const char* configurationTypes =
  452. this->Makefile->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
  453. {
  454. cmSystemTools::ExpandListArgument(configurationTypes, configNames);
  455. }
  456. else if(const char* buildType =
  457. this->Makefile->GetDefinition("CMAKE_BUILD_TYPE"))
  458. {
  459. if(*buildType)
  460. {
  461. configNames.push_back(buildType);
  462. }
  463. }
  464. // Add our targets to the manifest for each configuration.
  465. cmTargets& targets = this->Makefile->GetTargets();
  466. for(cmTargets::iterator t = targets.begin(); t != targets.end(); ++t)
  467. {
  468. cmTarget& target = t->second;
  469. cmTarget::TargetType type = target.GetType();
  470. if(type == cmTarget::STATIC_LIBRARY ||
  471. type == cmTarget::SHARED_LIBRARY ||
  472. type == cmTarget::MODULE_LIBRARY ||
  473. type == cmTarget::EXECUTABLE)
  474. {
  475. if(configNames.empty())
  476. {
  477. manifest[""].insert(target.GetFullPath(0, false));
  478. if(type == cmTarget::SHARED_LIBRARY &&
  479. this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"))
  480. {
  481. manifest[""].insert(target.GetFullPath(0, true));
  482. }
  483. }
  484. else
  485. {
  486. for(std::vector<std::string>::iterator ci = configNames.begin();
  487. ci != configNames.end(); ++ci)
  488. {
  489. const char* config = ci->c_str();
  490. manifest[config].insert(target.GetFullPath(config, false));
  491. if(type == cmTarget::SHARED_LIBRARY &&
  492. this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"))
  493. {
  494. manifest[config].insert(target.GetFullPath(config, true));
  495. }
  496. }
  497. }
  498. }
  499. }
  500. }
  501. void cmLocalGenerator::AddCustomCommandToCreateObject(const char* ofname,
  502. const char* lang,
  503. cmSourceFile& source,
  504. cmTarget& )
  505. {
  506. std::string objectDir = cmSystemTools::GetFilenamePath(std::string(ofname));
  507. objectDir = this->Convert(objectDir.c_str(),START_OUTPUT,SHELL);
  508. std::string objectFile = this->Convert(ofname,START_OUTPUT,SHELL);
  509. std::string sourceFile =
  510. this->Convert(source.GetFullPath().c_str(),START_OUTPUT,SHELL,true);
  511. std::string varString = "CMAKE_";
  512. varString += lang;
  513. varString += "_COMPILE_OBJECT";
  514. std::vector<std::string> rules;
  515. rules.push_back(this->Makefile->GetRequiredDefinition(varString.c_str()));
  516. varString = "CMAKE_";
  517. varString += lang;
  518. varString += "_FLAGS";
  519. std::string flags;
  520. flags += this->Makefile->GetSafeDefinition(varString.c_str());
  521. flags += " ";
  522. flags += this->GetIncludeFlags(lang);
  523. // Construct the command lines.
  524. cmCustomCommandLines commandLines;
  525. std::vector<std::string> commands;
  526. cmSystemTools::ExpandList(rules, commands);
  527. cmLocalGenerator::RuleVariables vars;
  528. vars.Language = lang;
  529. vars.Source = sourceFile.c_str();
  530. vars.Object = objectFile.c_str();
  531. vars.ObjectDir = objectDir.c_str();
  532. vars.Flags = flags.c_str();
  533. for(std::vector<std::string>::iterator i = commands.begin();
  534. i != commands.end(); ++i)
  535. {
  536. // Expand the full command line string.
  537. this->ExpandRuleVariables(*i, vars);
  538. // Parse the string to get the custom command line.
  539. cmCustomCommandLine commandLine;
  540. std::vector<cmStdString> cmd = cmSystemTools::ParseArguments(i->c_str());
  541. for(std::vector<cmStdString>::iterator a = cmd.begin();
  542. a != cmd.end(); ++a)
  543. {
  544. commandLine.push_back(*a);
  545. }
  546. // Store this command line.
  547. commandLines.push_back(commandLine);
  548. }
  549. // Check for extra object-file dependencies.
  550. std::vector<std::string> depends;
  551. const char* additionalDeps = source.GetProperty("OBJECT_DEPENDS");
  552. if(additionalDeps)
  553. {
  554. cmSystemTools::ExpandListArgument(additionalDeps, depends);
  555. }
  556. // Generate a meaningful comment for the command.
  557. std::string comment = "Building ";
  558. comment += lang;
  559. comment += " object ";
  560. comment += this->Convert(ofname, START_OUTPUT);
  561. // Add the custom command to build the object file.
  562. this->Makefile->AddCustomCommandToOutput(
  563. ofname,
  564. depends,
  565. source.GetFullPath().c_str(),
  566. commandLines,
  567. comment.c_str(),
  568. this->Makefile->GetStartOutputDirectory()
  569. );
  570. }
  571. void cmLocalGenerator::AddBuildTargetRule(const char* llang, cmTarget& target)
  572. {
  573. cmStdString objs;
  574. std::vector<std::string> objVector;
  575. // Add all the sources outputs to the depends of the target
  576. std::vector<cmSourceFile*> const& classes = target.GetSourceFiles();
  577. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  578. i != classes.end(); ++i)
  579. {
  580. cmSourceFile* sf = *i;
  581. if(!sf->GetCustomCommand() &&
  582. !sf->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  583. !sf->GetPropertyAsBool("EXTERNAL_OBJECT"))
  584. {
  585. std::string::size_type dir_len = 0;
  586. dir_len += strlen(this->Makefile->GetCurrentOutputDirectory());
  587. dir_len += 1;
  588. std::string obj = this->GetObjectFileNameWithoutTarget(*sf, dir_len);
  589. if(!obj.empty())
  590. {
  591. std::string ofname = this->Makefile->GetCurrentOutputDirectory();
  592. ofname += "/";
  593. ofname += obj;
  594. objVector.push_back(ofname);
  595. this->AddCustomCommandToCreateObject(ofname.c_str(),
  596. llang, *(*i), target);
  597. objs += this->Convert(ofname.c_str(),START_OUTPUT,MAKEFILE);
  598. objs += " ";
  599. }
  600. }
  601. }
  602. std::string createRule = "CMAKE_";
  603. createRule += llang;
  604. createRule += target.GetCreateRuleVariable();
  605. std::string targetName = target.GetFullName();
  606. // Executable :
  607. // Shared Library:
  608. // Static Library:
  609. // Shared Module:
  610. std::string linkLibs; // should be set
  611. std::string flags; // should be set
  612. std::string linkFlags; // should be set
  613. this->GetTargetFlags(linkLibs, flags, linkFlags, target);
  614. cmLocalGenerator::RuleVariables vars;
  615. vars.Language = llang;
  616. vars.Objects = objs.c_str();
  617. vars.ObjectDir = ".";
  618. vars.Target = targetName.c_str();
  619. vars.LinkLibraries = linkLibs.c_str();
  620. vars.Flags = flags.c_str();
  621. vars.LinkFlags = linkFlags.c_str();
  622. std::string langFlags;
  623. this->AddLanguageFlags(langFlags, llang, 0);
  624. vars.LanguageCompileFlags = langFlags.c_str();
  625. cmCustomCommandLines commandLines;
  626. std::vector<std::string> rules;
  627. rules.push_back(this->Makefile->GetRequiredDefinition(createRule.c_str()));
  628. std::vector<std::string> commands;
  629. cmSystemTools::ExpandList(rules, commands);
  630. for(std::vector<std::string>::iterator i = commands.begin();
  631. i != commands.end(); ++i)
  632. {
  633. // Expand the full command line string.
  634. this->ExpandRuleVariables(*i, vars);
  635. // Parse the string to get the custom command line.
  636. cmCustomCommandLine commandLine;
  637. std::vector<cmStdString> cmd = cmSystemTools::ParseArguments(i->c_str());
  638. for(std::vector<cmStdString>::iterator a = cmd.begin();
  639. a != cmd.end(); ++a)
  640. {
  641. commandLine.push_back(*a);
  642. }
  643. // Store this command line.
  644. commandLines.push_back(commandLine);
  645. }
  646. std::string targetFullPath = target.GetFullPath();
  647. // Generate a meaningful comment for the command.
  648. std::string comment = "Linking ";
  649. comment += llang;
  650. comment += " target ";
  651. comment += this->Convert(targetFullPath.c_str(), START_OUTPUT);
  652. this->Makefile->AddCustomCommandToOutput(
  653. targetFullPath.c_str(),
  654. objVector,
  655. 0,
  656. commandLines,
  657. comment.c_str(),
  658. this->Makefile->GetStartOutputDirectory()
  659. );
  660. target.AddSourceFile
  661. (this->Makefile->GetSource(targetFullPath.c_str()));
  662. }
  663. void cmLocalGenerator
  664. ::CreateCustomTargetsAndCommands(std::set<cmStdString> const& lang)
  665. {
  666. cmTargets &tgts = this->Makefile->GetTargets();
  667. for(cmTargets::iterator l = tgts.begin();
  668. l != tgts.end(); l++)
  669. {
  670. cmTarget& target = l->second;
  671. switch(target.GetType())
  672. {
  673. case cmTarget::STATIC_LIBRARY:
  674. case cmTarget::SHARED_LIBRARY:
  675. case cmTarget::MODULE_LIBRARY:
  676. case cmTarget::EXECUTABLE:
  677. {
  678. const char* llang =
  679. target.GetLinkerLanguage(this->GetGlobalGenerator());
  680. if(!llang)
  681. {
  682. cmSystemTools::Error
  683. ("CMake can not determine linker language for target:",
  684. target.GetName());
  685. return;
  686. }
  687. // if the language is not in the set lang then create custom
  688. // commands to build the target
  689. if(lang.count(llang) == 0)
  690. {
  691. this->AddBuildTargetRule(llang, target);
  692. }
  693. }
  694. break;
  695. case cmTarget::UTILITY:
  696. case cmTarget::GLOBAL_TARGET:
  697. case cmTarget::INSTALL_FILES:
  698. case cmTarget::INSTALL_PROGRAMS:
  699. case cmTarget::INSTALL_DIRECTORY:
  700. break;
  701. }
  702. }
  703. }
  704. // List of variables that are replaced when
  705. // rules are expanced. These variables are
  706. // replaced in the form <var> with GetSafeDefinition(var).
  707. // ${LANG} is replaced in the variable first with all enabled
  708. // languages.
  709. static const char* ruleReplaceVars[] =
  710. {
  711. "CMAKE_${LANG}_COMPILER",
  712. "CMAKE_SHARED_LIBRARY_CREATE_${LANG}_FLAGS",
  713. "CMAKE_SHARED_MODULE_CREATE_${LANG}_FLAGS",
  714. "CMAKE_SHARED_MODULE_${LANG}_FLAGS",
  715. "CMAKE_SHARED_LIBRARY_${LANG}_FLAGS",
  716. "CMAKE_${LANG}_LINK_FLAGS",
  717. "CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG",
  718. "CMAKE_${LANG}_ARCHIVE",
  719. "CMAKE_AR",
  720. "CMAKE_CURRENT_SOURCE_DIR",
  721. "CMAKE_CURRENT_BINARY_DIR",
  722. "CMAKE_RANLIB",
  723. "CMAKE_LINKER",
  724. 0
  725. };
  726. std::string
  727. cmLocalGenerator::ExpandRuleVariable(std::string const& variable,
  728. const RuleVariables& replaceValues)
  729. {
  730. if(replaceValues.LinkFlags)
  731. {
  732. if(variable == "LINK_FLAGS")
  733. {
  734. return replaceValues.LinkFlags;
  735. }
  736. }
  737. if(replaceValues.Flags)
  738. {
  739. if(variable == "FLAGS")
  740. {
  741. return replaceValues.Flags;
  742. }
  743. }
  744. if(replaceValues.Source)
  745. {
  746. if(variable == "SOURCE")
  747. {
  748. return replaceValues.Source;
  749. }
  750. }
  751. if(replaceValues.PreprocessedSource)
  752. {
  753. if(variable == "PREPROCESSED_SOURCE")
  754. {
  755. return replaceValues.PreprocessedSource;
  756. }
  757. }
  758. if(replaceValues.AssemblySource)
  759. {
  760. if(variable == "ASSEMBLY_SOURCE")
  761. {
  762. return replaceValues.AssemblySource;
  763. }
  764. }
  765. if(replaceValues.Object)
  766. {
  767. if(variable == "OBJECT")
  768. {
  769. return replaceValues.Object;
  770. }
  771. }
  772. if(replaceValues.ObjectDir)
  773. {
  774. if(variable == "OBJECT_DIR")
  775. {
  776. return replaceValues.ObjectDir;
  777. }
  778. }
  779. if(replaceValues.Objects)
  780. {
  781. if(variable == "OBJECTS")
  782. {
  783. return replaceValues.Objects;
  784. }
  785. }
  786. if(replaceValues.ObjectsQuoted)
  787. {
  788. if(variable == "OBJECTS_QUOTED")
  789. {
  790. return replaceValues.ObjectsQuoted;
  791. }
  792. }
  793. if(replaceValues.TargetPDB )
  794. {
  795. if(variable == "TARGET_PDB")
  796. {
  797. return replaceValues.TargetPDB;
  798. }
  799. }
  800. if(replaceValues.Target)
  801. {
  802. if(variable == "TARGET_QUOTED")
  803. {
  804. std::string targetQuoted = replaceValues.Target;
  805. if(targetQuoted.size() && targetQuoted[0] != '\"')
  806. {
  807. targetQuoted = '\"';
  808. targetQuoted += replaceValues.Target;
  809. targetQuoted += '\"';
  810. }
  811. return targetQuoted;
  812. }
  813. if(replaceValues.LanguageCompileFlags)
  814. {
  815. if(variable == "LANGUAGE_COMPILE_FLAGS")
  816. {
  817. return replaceValues.LanguageCompileFlags;
  818. }
  819. }
  820. if(replaceValues.Target)
  821. {
  822. if(variable == "TARGET")
  823. {
  824. return replaceValues.Target;
  825. }
  826. }
  827. if(variable == "TARGET_IMPLIB")
  828. {
  829. return this->TargetImplib;
  830. }
  831. if(variable == "TARGET_VERSION_MAJOR")
  832. {
  833. if(replaceValues.TargetVersionMajor)
  834. {
  835. return replaceValues.TargetVersionMajor;
  836. }
  837. else
  838. {
  839. return "0";
  840. }
  841. }
  842. if(variable == "TARGET_VERSION_MINOR")
  843. {
  844. if(replaceValues.TargetVersionMinor)
  845. {
  846. return replaceValues.TargetVersionMinor;
  847. }
  848. else
  849. {
  850. return "0";
  851. }
  852. }
  853. if(replaceValues.Target)
  854. {
  855. if(variable == "TARGET_BASE")
  856. {
  857. // Strip the last extension off the target name.
  858. std::string targetBase = replaceValues.Target;
  859. std::string::size_type pos = targetBase.rfind(".");
  860. if(pos != targetBase.npos)
  861. {
  862. return targetBase.substr(0, pos);
  863. }
  864. else
  865. {
  866. return targetBase;
  867. }
  868. }
  869. }
  870. }
  871. if(replaceValues.TargetSOName)
  872. {
  873. if(variable == "TARGET_SONAME")
  874. {
  875. if(replaceValues.Language)
  876. {
  877. std::string name = "CMAKE_SHARED_LIBRARY_SONAME_";
  878. name += replaceValues.Language;
  879. name += "_FLAG";
  880. if(this->Makefile->GetDefinition(name.c_str()))
  881. {
  882. return replaceValues.TargetSOName;
  883. }
  884. }
  885. return "";
  886. }
  887. }
  888. if(replaceValues.TargetInstallNameDir)
  889. {
  890. if(variable == "TARGET_INSTALLNAME_DIR")
  891. {
  892. return replaceValues.TargetInstallNameDir;
  893. }
  894. }
  895. if(replaceValues.LinkLibraries)
  896. {
  897. if(variable == "LINK_LIBRARIES")
  898. {
  899. return replaceValues.LinkLibraries;
  900. }
  901. }
  902. std::vector<std::string> enabledLanguages;
  903. this->GlobalGenerator->GetEnabledLanguages(enabledLanguages);
  904. // loop over language specific replace variables
  905. int pos = 0;
  906. while(ruleReplaceVars[pos])
  907. {
  908. for(std::vector<std::string>::iterator i = enabledLanguages.begin();
  909. i != enabledLanguages.end(); ++i)
  910. {
  911. const char* lang = i->c_str();
  912. std::string actualReplace = ruleReplaceVars[pos];
  913. // If this is the compiler then look for the extra variable
  914. // _COMPILER_ARG1 which must be the first argument to the compiler
  915. const char* compilerArg1 = 0;
  916. if(actualReplace == "CMAKE_${LANG}_COMPILER")
  917. {
  918. std::string arg1 = actualReplace + "_ARG1";
  919. cmSystemTools::ReplaceString(arg1, "${LANG}", lang);
  920. compilerArg1 = this->Makefile->GetDefinition(arg1.c_str());
  921. }
  922. if(actualReplace.find("${LANG}") != actualReplace.npos)
  923. {
  924. cmSystemTools::ReplaceString(actualReplace, "${LANG}", lang);
  925. }
  926. if(actualReplace == variable)
  927. {
  928. std::string replace =
  929. this->Makefile->GetSafeDefinition(variable.c_str());
  930. // if the variable is not a FLAG then treat it like a path
  931. if(variable.find("_FLAG") == variable.npos)
  932. {
  933. std::string ret = this->ConvertToOutputForExisting(replace.c_str());
  934. // if there is a required first argument to the compiler add it
  935. // to the compiler string
  936. if(compilerArg1)
  937. {
  938. ret += " ";
  939. ret += compilerArg1;
  940. }
  941. return ret;
  942. }
  943. return replace;
  944. }
  945. }
  946. pos++;
  947. }
  948. return variable;
  949. }
  950. void
  951. cmLocalGenerator::ExpandRuleVariables(std::string& s,
  952. const RuleVariables& replaceValues)
  953. {
  954. std::vector<std::string> enabledLanguages;
  955. this->GlobalGenerator->GetEnabledLanguages(enabledLanguages);
  956. std::string::size_type start = s.find('<');
  957. // no variables to expand
  958. if(start == s.npos)
  959. {
  960. return;
  961. }
  962. std::string::size_type pos = 0;
  963. std::string expandedInput;
  964. while(start != s.npos && start < s.size()-2)
  965. {
  966. std::string::size_type end = s.find('>', start);
  967. // if we find a < with no > we are done
  968. if(end == s.npos)
  969. {
  970. return;
  971. }
  972. char c = s[start+1];
  973. // if the next char after the < is not A-Za-z then
  974. // skip it and try to find the next < in the string
  975. if(!isalpha(c))
  976. {
  977. start = s.find('<', start+1);
  978. }
  979. else
  980. {
  981. // extract the var
  982. std::string var = s.substr(start+1, end - start-1);
  983. std::string replace = this->ExpandRuleVariable(var,
  984. replaceValues);
  985. expandedInput += s.substr(pos, start-pos);
  986. expandedInput += replace;
  987. // move to next one
  988. start = s.find('<', start+var.size()+2);
  989. pos = end+1;
  990. }
  991. }
  992. // add the rest of the input
  993. expandedInput += s.substr(pos, s.size()-pos);
  994. s = expandedInput;
  995. }
  996. std::string
  997. cmLocalGenerator::ConvertToOutputForExisting(const char* p)
  998. {
  999. std::string ret = p;
  1000. if(this->WindowsShell && ret.find(' ') != ret.npos
  1001. && cmSystemTools::FileExists(p))
  1002. {
  1003. if(cmSystemTools::GetShortPath(p, ret))
  1004. {
  1005. return this->Convert(ret.c_str(), NONE, SHELL, true);
  1006. }
  1007. }
  1008. return this->Convert(p, START_OUTPUT, SHELL, true);
  1009. }
  1010. const char* cmLocalGenerator::GetIncludeFlags(const char* lang)
  1011. {
  1012. if(!lang)
  1013. {
  1014. return "";
  1015. }
  1016. if(this->LanguageToIncludeFlags.count(lang))
  1017. {
  1018. return this->LanguageToIncludeFlags[lang].c_str();
  1019. }
  1020. cmOStringStream includeFlags;
  1021. std::vector<std::string> includes;
  1022. this->GetIncludeDirectories(includes);
  1023. std::vector<std::string>::iterator i;
  1024. std::string flagVar = "CMAKE_INCLUDE_FLAG_";
  1025. flagVar += lang;
  1026. const char* includeFlag =
  1027. this->Makefile->GetSafeDefinition(flagVar.c_str());
  1028. flagVar = "CMAKE_INCLUDE_FLAG_SEP_";
  1029. flagVar += lang;
  1030. const char* sep = this->Makefile->GetDefinition(flagVar.c_str());
  1031. bool quotePaths = false;
  1032. if(this->Makefile->GetDefinition("CMAKE_QUOTE_INCLUDE_PATHS"))
  1033. {
  1034. quotePaths = true;
  1035. }
  1036. bool repeatFlag = true;
  1037. // should the include flag be repeated like ie. -IA -IB
  1038. if(!sep)
  1039. {
  1040. sep = " ";
  1041. }
  1042. else
  1043. {
  1044. // if there is a separator then the flag is not repeated but is only
  1045. // given once i.e. -classpath a:b:c
  1046. repeatFlag = false;
  1047. }
  1048. // Support special system include flag if it is available and the
  1049. // normal flag is repeated for each directory.
  1050. std::string sysFlagVar = "CMAKE_INCLUDE_SYSTEM_FLAG_";
  1051. sysFlagVar += lang;
  1052. const char* sysIncludeFlag = 0;
  1053. if(repeatFlag)
  1054. {
  1055. sysIncludeFlag = this->Makefile->GetDefinition(sysFlagVar.c_str());
  1056. }
  1057. bool flagUsed = false;
  1058. std::set<cmStdString> emitted;
  1059. #ifdef __APPLE__
  1060. emitted.insert("/System/Library/Frameworks");
  1061. #endif
  1062. for(i = includes.begin(); i != includes.end(); ++i)
  1063. {
  1064. #ifdef __APPLE__
  1065. if(cmSystemTools::IsPathToFramework(i->c_str()))
  1066. {
  1067. std::string frameworkDir = *i;
  1068. frameworkDir += "/../";
  1069. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
  1070. if(emitted.insert(frameworkDir).second)
  1071. {
  1072. includeFlags
  1073. << "-F"
  1074. << this->ConvertToOutputForExisting(frameworkDir.c_str()) << " ";
  1075. }
  1076. continue;
  1077. }
  1078. #endif
  1079. std::string include = *i;
  1080. if(!flagUsed || repeatFlag)
  1081. {
  1082. if(sysIncludeFlag &&
  1083. this->Makefile->IsSystemIncludeDirectory(i->c_str()))
  1084. {
  1085. includeFlags << sysIncludeFlag;
  1086. }
  1087. else
  1088. {
  1089. includeFlags << includeFlag;
  1090. }
  1091. flagUsed = true;
  1092. }
  1093. std::string includePath = this->ConvertToOutputForExisting(i->c_str());
  1094. if(quotePaths && includePath.size() && includePath[0] != '\"')
  1095. {
  1096. includeFlags << "\"";
  1097. }
  1098. includeFlags << includePath;
  1099. if(quotePaths && includePath.size() && includePath[0] != '\"')
  1100. {
  1101. includeFlags << "\"";
  1102. }
  1103. includeFlags << sep;
  1104. }
  1105. std::string flags = includeFlags.str();
  1106. // remove trailing separators
  1107. if((sep[0] != ' ') && flags[flags.size()-1] == sep[0])
  1108. {
  1109. flags[flags.size()-1] = ' ';
  1110. }
  1111. flags += this->Makefile->GetDefineFlags();
  1112. this->LanguageToIncludeFlags[lang] = flags;
  1113. // Use this temorary variable for the return value to work-around a
  1114. // bogus GCC 2.95 warning.
  1115. const char* ret = this->LanguageToIncludeFlags[lang].c_str();
  1116. return ret;
  1117. }
  1118. //----------------------------------------------------------------------------
  1119. void cmLocalGenerator::GetIncludeDirectories(std::vector<std::string>& dirs,
  1120. bool filter_system_dirs)
  1121. {
  1122. // Need to decide whether to automatically include the source and
  1123. // binary directories at the beginning of the include path.
  1124. bool includeSourceDir = false;
  1125. bool includeBinaryDir = false;
  1126. // When automatic include directories are requested for a build then
  1127. // include the source and binary directories at the beginning of the
  1128. // include path to approximate include file behavior for an
  1129. // in-source build. This does not account for the case of a source
  1130. // file in a subdirectory of the current source directory but we
  1131. // cannot fix this because not all native build tools support
  1132. // per-source-file include paths.
  1133. if(this->Makefile->IsOn("CMAKE_INCLUDE_CURRENT_DIR"))
  1134. {
  1135. includeSourceDir = true;
  1136. includeBinaryDir = true;
  1137. }
  1138. // CMake versions below 2.0 would add the source tree to the -I path
  1139. // automatically. Preserve compatibility.
  1140. const char* versionValue =
  1141. this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  1142. int major = 0;
  1143. int minor = 0;
  1144. if(versionValue && sscanf(versionValue, "%d.%d", &major, &minor) != 2)
  1145. {
  1146. versionValue = 0;
  1147. }
  1148. if(versionValue && major < 2)
  1149. {
  1150. includeSourceDir = true;
  1151. }
  1152. // Hack for VTK 4.0 - 4.4 which depend on the old behavior but do
  1153. // not set the backwards compatibility level automatically.
  1154. const char* vtkSourceDir =
  1155. this->Makefile->GetDefinition("VTK_SOURCE_DIR");
  1156. if(vtkSourceDir)
  1157. {
  1158. const char* vtk_major =
  1159. this->Makefile->GetDefinition("VTK_MAJOR_VERSION");
  1160. const char* vtk_minor =
  1161. this->Makefile->GetDefinition("VTK_MINOR_VERSION");
  1162. vtk_major = vtk_major? vtk_major : "4";
  1163. vtk_minor = vtk_minor? vtk_minor : "4";
  1164. int vmajor = 0;
  1165. int vminor = 0;
  1166. if(sscanf(vtk_major, "%d", &vmajor) &&
  1167. sscanf(vtk_minor, "%d", &vminor) && vmajor == 4 && vminor <= 4)
  1168. {
  1169. includeSourceDir = true;
  1170. }
  1171. }
  1172. // Do not repeat an include path.
  1173. std::set<cmStdString> emitted;
  1174. // Store the automatic include paths.
  1175. if(includeBinaryDir)
  1176. {
  1177. dirs.push_back(this->Makefile->GetStartOutputDirectory());
  1178. emitted.insert(this->Makefile->GetStartOutputDirectory());
  1179. }
  1180. if(includeSourceDir)
  1181. {
  1182. if(emitted.find(this->Makefile->GetStartDirectory()) == emitted.end())
  1183. {
  1184. dirs.push_back(this->Makefile->GetStartDirectory());
  1185. emitted.insert(this->Makefile->GetStartDirectory());
  1186. }
  1187. }
  1188. if(filter_system_dirs)
  1189. {
  1190. // Do not explicitly add the standard include path "/usr/include".
  1191. // This can cause problems with certain standard library
  1192. // implementations because the wrong headers may be found first.
  1193. emitted.insert("/usr/include");
  1194. if(const char* implicitIncludes = this->Makefile->GetDefinition
  1195. ("CMAKE_PLATFORM_IMPLICIT_INCLUDE_DIRECTORIES"))
  1196. {
  1197. std::vector<std::string> implicitIncludeVec;
  1198. cmSystemTools::ExpandListArgument(implicitIncludes, implicitIncludeVec);
  1199. for(unsigned int k = 0; k < implicitIncludeVec.size(); ++k)
  1200. {
  1201. emitted.insert(implicitIncludeVec[k]);
  1202. }
  1203. }
  1204. }
  1205. // Get the project-specified include directories.
  1206. std::vector<std::string>& includes =
  1207. this->Makefile->GetIncludeDirectories();
  1208. // Support putting all the in-project include directories first if
  1209. // it is requested by the project.
  1210. if(this->Makefile->IsOn("CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE"))
  1211. {
  1212. const char* topSourceDir = this->Makefile->GetHomeDirectory();
  1213. const char* topBinaryDir = this->Makefile->GetHomeOutputDirectory();
  1214. for(std::vector<std::string>::iterator i = includes.begin();
  1215. i != includes.end(); ++i)
  1216. {
  1217. // Emit this directory only if it is a subdirectory of the
  1218. // top-level source or binary tree.
  1219. if(cmSystemTools::ComparePath(i->c_str(), topSourceDir) ||
  1220. cmSystemTools::ComparePath(i->c_str(), topBinaryDir) ||
  1221. cmSystemTools::IsSubDirectory(i->c_str(), topSourceDir) ||
  1222. cmSystemTools::IsSubDirectory(i->c_str(), topBinaryDir))
  1223. {
  1224. if(emitted.insert(*i).second)
  1225. {
  1226. dirs.push_back(*i);
  1227. }
  1228. }
  1229. }
  1230. }
  1231. // Construct the final ordered include directory list.
  1232. for(std::vector<std::string>::iterator i = includes.begin();
  1233. i != includes.end(); ++i)
  1234. {
  1235. if(emitted.insert(*i).second)
  1236. {
  1237. dirs.push_back(*i);
  1238. }
  1239. }
  1240. }
  1241. void cmLocalGenerator::GetTargetFlags(std::string& linkLibs,
  1242. std::string& flags,
  1243. std::string& linkFlags,
  1244. cmTarget& target)
  1245. {
  1246. std::string buildType =
  1247. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1248. buildType = cmSystemTools::UpperCase(buildType);
  1249. const char* libraryLinkVariable =
  1250. "CMAKE_SHARED_LINKER_FLAGS"; // default to shared library
  1251. switch(target.GetType())
  1252. {
  1253. case cmTarget::STATIC_LIBRARY:
  1254. {
  1255. const char* targetLinkFlags =
  1256. target.GetProperty("STATIC_LIBRARY_FLAGS");
  1257. if(targetLinkFlags)
  1258. {
  1259. linkFlags += targetLinkFlags;
  1260. linkFlags += " ";
  1261. }
  1262. }
  1263. break;
  1264. case cmTarget::MODULE_LIBRARY:
  1265. libraryLinkVariable = "CMAKE_MODULE_LINKER_FLAGS";
  1266. case cmTarget::SHARED_LIBRARY:
  1267. {
  1268. linkFlags = this->Makefile->GetSafeDefinition(libraryLinkVariable);
  1269. linkFlags += " ";
  1270. if(buildType.size())
  1271. {
  1272. std::string build = libraryLinkVariable;
  1273. build += "_";
  1274. build += buildType;
  1275. linkFlags += this->Makefile->GetSafeDefinition(build.c_str());
  1276. linkFlags += " ";
  1277. }
  1278. if(this->Makefile->IsOn("WIN32") &&
  1279. !(this->Makefile->IsOn("CYGWIN") || this->Makefile->IsOn("MINGW")))
  1280. {
  1281. const std::vector<cmSourceFile*>& sources = target.GetSourceFiles();
  1282. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1283. i != sources.end(); ++i)
  1284. {
  1285. cmSourceFile* sf = *i;
  1286. if(sf->GetExtension() == "def")
  1287. {
  1288. linkFlags +=
  1289. this->Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1290. linkFlags += this->Convert(sf->GetFullPath().c_str(),
  1291. START_OUTPUT, SHELL);
  1292. linkFlags += " ";
  1293. }
  1294. }
  1295. }
  1296. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  1297. if(targetLinkFlags)
  1298. {
  1299. linkFlags += targetLinkFlags;
  1300. linkFlags += " ";
  1301. std::string configLinkFlags = targetLinkFlags;
  1302. configLinkFlags += buildType;
  1303. targetLinkFlags = target.GetProperty(configLinkFlags.c_str());
  1304. if(targetLinkFlags)
  1305. {
  1306. linkFlags += targetLinkFlags;
  1307. linkFlags += " ";
  1308. }
  1309. }
  1310. cmOStringStream linklibsStr;
  1311. this->OutputLinkLibraries(linklibsStr, target, false);
  1312. linkLibs = linklibsStr.str();
  1313. }
  1314. break;
  1315. case cmTarget::EXECUTABLE:
  1316. {
  1317. linkFlags +=
  1318. this->Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  1319. linkFlags += " ";
  1320. if(buildType.size())
  1321. {
  1322. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  1323. build += buildType;
  1324. linkFlags += this->Makefile->GetSafeDefinition(build.c_str());
  1325. linkFlags += " ";
  1326. }
  1327. const char* linkLanguage =
  1328. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1329. if(!linkLanguage)
  1330. {
  1331. cmSystemTools::Error
  1332. ("CMake can not determine linker language for target:",
  1333. target.GetName());
  1334. return;
  1335. }
  1336. std::string langVar = "CMAKE_";
  1337. langVar += linkLanguage;
  1338. std::string flagsVar = langVar + "_FLAGS";
  1339. std::string sharedFlagsVar = "CMAKE_SHARED_LIBRARY_";
  1340. sharedFlagsVar += linkLanguage;
  1341. sharedFlagsVar += "_FLAGS";
  1342. flags += this->Makefile->GetSafeDefinition(flagsVar.c_str());
  1343. flags += " ";
  1344. flags += this->Makefile->GetSafeDefinition(sharedFlagsVar.c_str());
  1345. flags += " ";
  1346. cmOStringStream linklibs;
  1347. this->OutputLinkLibraries(linklibs, target, false);
  1348. linkLibs = linklibs.str();
  1349. if(cmSystemTools::IsOn
  1350. (this->Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1351. {
  1352. std::string sFlagVar = std::string("CMAKE_SHARED_BUILD_")
  1353. + linkLanguage + std::string("_FLAGS");
  1354. linkFlags += this->Makefile->GetSafeDefinition(sFlagVar.c_str());
  1355. linkFlags += " ";
  1356. }
  1357. if ( target.GetPropertyAsBool("WIN32_EXECUTABLE") )
  1358. {
  1359. linkFlags +=
  1360. this->Makefile->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  1361. linkFlags += " ";
  1362. }
  1363. else
  1364. {
  1365. linkFlags +=
  1366. this->Makefile->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  1367. linkFlags += " ";
  1368. }
  1369. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  1370. if(targetLinkFlags)
  1371. {
  1372. linkFlags += targetLinkFlags;
  1373. linkFlags += " ";
  1374. std::string configLinkFlags = targetLinkFlags;
  1375. configLinkFlags += buildType;
  1376. targetLinkFlags = target.GetProperty(configLinkFlags.c_str());
  1377. if(targetLinkFlags)
  1378. {
  1379. linkFlags += targetLinkFlags;
  1380. linkFlags += " ";
  1381. }
  1382. }
  1383. }
  1384. break;
  1385. case cmTarget::UTILITY:
  1386. case cmTarget::GLOBAL_TARGET:
  1387. case cmTarget::INSTALL_FILES:
  1388. case cmTarget::INSTALL_PROGRAMS:
  1389. case cmTarget::INSTALL_DIRECTORY:
  1390. break;
  1391. }
  1392. }
  1393. /**
  1394. * Output the linking rules on a command line. For executables,
  1395. * targetLibrary should be a NULL pointer. For libraries, it should point
  1396. * to the name of the library. This will not link a library against itself.
  1397. */
  1398. void cmLocalGenerator::OutputLinkLibraries(std::ostream& fout,
  1399. cmTarget& tgt,
  1400. bool relink)
  1401. {
  1402. // Try to emit each search path once
  1403. std::set<cmStdString> emitted;
  1404. // Embed runtime search paths if possible and if required.
  1405. bool outputRuntime = true;
  1406. std::string runtimeFlag;
  1407. std::string runtimeSep;
  1408. const char* config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  1409. const char* linkLanguage =
  1410. tgt.GetLinkerLanguage(this->GetGlobalGenerator());
  1411. if(!linkLanguage)
  1412. {
  1413. cmSystemTools::
  1414. Error("CMake can not determine linker language for target:",
  1415. tgt.GetName());
  1416. return;
  1417. }
  1418. std::string runTimeFlagVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
  1419. runTimeFlagVar += linkLanguage;
  1420. runTimeFlagVar += "_FLAG";
  1421. std::string runTimeFlagSepVar = runTimeFlagVar + "_SEP";
  1422. runtimeFlag = this->Makefile->GetSafeDefinition(runTimeFlagVar.c_str());
  1423. runtimeSep = this->Makefile->GetSafeDefinition(runTimeFlagSepVar.c_str());
  1424. // concatenate all paths or no?
  1425. bool runtimeConcatenate = ( runtimeSep!="" );
  1426. if(runtimeFlag == "" || this->Makefile->IsOn("CMAKE_SKIP_RPATH"))
  1427. {
  1428. outputRuntime = false;
  1429. }
  1430. // Some search paths should never be emitted
  1431. emitted.insert("");
  1432. if(const char* implicitLinks =
  1433. (this->Makefile->GetDefinition
  1434. ("CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES")))
  1435. {
  1436. std::vector<std::string> implicitLinkVec;
  1437. cmSystemTools::ExpandListArgument(implicitLinks, implicitLinkVec);
  1438. for(unsigned int k = 0; k < implicitLinkVec.size(); ++k)
  1439. {
  1440. emitted.insert(implicitLinkVec[k]);
  1441. }
  1442. }
  1443. std::string libPathFlag =
  1444. this->Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
  1445. std::string libLinkFlag =
  1446. this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_FLAG");
  1447. // collect all the flags needed for linking libraries
  1448. std::string linkLibs;
  1449. // Flags to link an executable to shared libraries.
  1450. std::string linkFlagsVar = "CMAKE_SHARED_LIBRARY_LINK_";
  1451. linkFlagsVar += linkLanguage;
  1452. linkFlagsVar += "_FLAGS";
  1453. if( tgt.GetType() == cmTarget::EXECUTABLE )
  1454. {
  1455. linkLibs = this->Makefile->GetSafeDefinition(linkFlagsVar.c_str());
  1456. linkLibs += " ";
  1457. }
  1458. // Compute the link library and directory information.
  1459. std::vector<cmStdString> libNames;
  1460. std::vector<cmStdString> libDirs;
  1461. this->ComputeLinkInformation(tgt, config, libNames, libDirs);
  1462. // Select whether to generate an rpath for the install tree or the
  1463. // build tree.
  1464. bool linking_for_install =
  1465. relink || tgt.GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH");
  1466. bool use_install_rpath =
  1467. outputRuntime && tgt.HaveInstallTreeRPATH() && linking_for_install;
  1468. bool use_build_rpath =
  1469. outputRuntime && tgt.HaveBuildTreeRPATH() && !linking_for_install;
  1470. bool use_link_rpath =
  1471. outputRuntime && linking_for_install &&
  1472. tgt.GetPropertyAsBool("INSTALL_RPATH_USE_LINK_PATH");
  1473. // Construct the RPATH.
  1474. std::vector<std::string> runtimeDirs;
  1475. if(use_install_rpath)
  1476. {
  1477. const char* install_rpath = tgt.GetProperty("INSTALL_RPATH");
  1478. cmSystemTools::ExpandListArgument(install_rpath, runtimeDirs);
  1479. for(unsigned int i=0; i < runtimeDirs.size(); ++i)
  1480. {
  1481. runtimeDirs[i] =
  1482. this->Convert(runtimeDirs[i].c_str(), FULL, SHELL, false);
  1483. }
  1484. }
  1485. // Append the library search path flags.
  1486. for(std::vector<cmStdString>::const_iterator libDir = libDirs.begin();
  1487. libDir != libDirs.end(); ++libDir)
  1488. {
  1489. std::string libpath = this->ConvertToOutputForExisting(libDir->c_str());
  1490. if(emitted.insert(libpath).second)
  1491. {
  1492. std::string fullLibPath;
  1493. if(!this->WindowsShell && this->UseRelativePaths)
  1494. {
  1495. fullLibPath = "\"`cd ";
  1496. }
  1497. fullLibPath += libpath;
  1498. if(!this->WindowsShell && this->UseRelativePaths)
  1499. {
  1500. fullLibPath += ";pwd`\"";
  1501. }
  1502. std::string::size_type pos = libDir->find(libPathFlag.c_str());
  1503. if((pos == std::string::npos || pos > 0)
  1504. && libDir->find("${") == std::string::npos)
  1505. {
  1506. linkLibs += libPathFlag;
  1507. linkLibs += fullLibPath;
  1508. linkLibs += " ";
  1509. // Put this directory in the rpath if using build-tree rpath
  1510. // support or if using the link path as an rpath.
  1511. if(use_build_rpath)
  1512. {
  1513. runtimeDirs.push_back(fullLibPath);
  1514. }
  1515. else if(use_link_rpath)
  1516. {
  1517. // Do not add any path inside the source or build tree.
  1518. const char* topSourceDir = this->Makefile->GetHomeDirectory();
  1519. const char* topBinaryDir = this->Makefile->GetHomeOutputDirectory();
  1520. if(!cmSystemTools::ComparePath(libDir->c_str(), topSourceDir) &&
  1521. !cmSystemTools::ComparePath(libDir->c_str(), topBinaryDir) &&
  1522. !cmSystemTools::IsSubDirectory(libDir->c_str(), topSourceDir) &&
  1523. !cmSystemTools::IsSubDirectory(libDir->c_str(), topBinaryDir))
  1524. {
  1525. runtimeDirs.push_back(fullLibPath);
  1526. }
  1527. }
  1528. }
  1529. }
  1530. }
  1531. // Append the link libraries.
  1532. for(std::vector<cmStdString>::iterator lib = libNames.begin();
  1533. lib != libNames.end(); ++lib)
  1534. {
  1535. linkLibs += *lib;
  1536. linkLibs += " ";
  1537. }
  1538. fout << linkLibs;
  1539. if(!runtimeDirs.empty())
  1540. {
  1541. // For the runtime search directories, do a "-Wl,-rpath,a:b:c" or
  1542. // a "-R a -R b -R c" type link line
  1543. fout << runtimeFlag;
  1544. std::vector<std::string>::iterator itr = runtimeDirs.begin();
  1545. fout << *itr;
  1546. ++itr;
  1547. for( ; itr != runtimeDirs.end(); ++itr )
  1548. {
  1549. if(runtimeConcatenate)
  1550. {
  1551. fout << runtimeSep << *itr;
  1552. }
  1553. else
  1554. {
  1555. fout << " " << runtimeFlag << *itr;
  1556. }
  1557. }
  1558. fout << " ";
  1559. }
  1560. // Add standard libraries for this language.
  1561. std::string standardLibsVar = "CMAKE_";
  1562. standardLibsVar += linkLanguage;
  1563. standardLibsVar += "_STANDARD_LIBRARIES";
  1564. if(const char* stdLibs =
  1565. this->Makefile->GetDefinition(standardLibsVar.c_str()))
  1566. {
  1567. fout << stdLibs << " ";
  1568. }
  1569. }
  1570. //----------------------------------------------------------------------------
  1571. void cmLocalGenerator
  1572. ::ComputeLinkInformation(cmTarget& target,
  1573. const char* config,
  1574. std::vector<cmStdString>& outLibs,
  1575. std::vector<cmStdString>& outDirs,
  1576. std::vector<cmStdString>* fullPathLibs)
  1577. {
  1578. // Compute which library configuration to link.
  1579. cmTarget::LinkLibraryType linkType = cmTarget::OPTIMIZED;
  1580. if(config && cmSystemTools::UpperCase(config) == "DEBUG")
  1581. {
  1582. linkType = cmTarget::DEBUG;
  1583. }
  1584. // Get the language used for linking.
  1585. const char* linkLanguage =
  1586. target.GetLinkerLanguage(this->GetGlobalGenerator());
  1587. // Check whether we should use an import library for linking a target.
  1588. bool implib =
  1589. this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX")?true:false;
  1590. // On platforms without import libraries there may be a special flag
  1591. // to use when creating a plugin (module) that obtains symbols from
  1592. // the program that will load it.
  1593. const char* loader_flag = 0;
  1594. if(!implib && target.GetType() == cmTarget::MODULE_LIBRARY)
  1595. {
  1596. if(!linkLanguage)
  1597. {
  1598. cmSystemTools::
  1599. Error("CMake can not determine linker language for target:",
  1600. target.GetName());
  1601. return;
  1602. }
  1603. std::string loader_flag_var = "CMAKE_SHARED_MODULE_LOADER_";
  1604. loader_flag_var += linkLanguage;
  1605. loader_flag_var += "_FLAG";
  1606. loader_flag = this->Makefile->GetDefinition(loader_flag_var.c_str());
  1607. }
  1608. // Get the list of libraries against which this target wants to link.
  1609. std::vector<std::string> linkLibraries;
  1610. const cmTarget::LinkLibraryVectorType& inLibs = target.GetLinkLibraries();
  1611. for(cmTarget::LinkLibraryVectorType::const_iterator j = inLibs.begin();
  1612. j != inLibs.end(); ++j)
  1613. {
  1614. // For backwards compatibility variables may have been expanded
  1615. // inside library names. Clean up the resulting name.
  1616. std::string lib = j->first;
  1617. std::string::size_type pos = lib.find_first_not_of(" \t\r\n");
  1618. if(pos != lib.npos)
  1619. {
  1620. lib = lib.substr(pos, lib.npos);
  1621. }
  1622. pos = lib.find_last_not_of(" \t\r\n");
  1623. if(pos != lib.npos)
  1624. {
  1625. lib = lib.substr(0, pos+1);
  1626. }
  1627. if(lib.empty())
  1628. {
  1629. continue;
  1630. }
  1631. // Link to a library if it is not the same target and is meant for
  1632. // this configuration type.
  1633. if((target.GetType() == cmTarget::EXECUTABLE ||
  1634. lib != target.GetName()) &&
  1635. (j->second == cmTarget::GENERAL || j->second == linkType))
  1636. {
  1637. // Compute the proper name to use to link this library.
  1638. cmTarget* tgt = this->GlobalGenerator->FindTarget(0, lib.c_str(), false);
  1639. bool impexe = (tgt &&
  1640. tgt->GetType() == cmTarget::EXECUTABLE &&
  1641. tgt->GetPropertyAsBool("ENABLE_EXPORTS"));
  1642. if(impexe && !implib && !loader_flag)
  1643. {
  1644. // Skip linking to executables on platforms with no import
  1645. // libraries or loader flags.
  1646. continue;
  1647. }
  1648. else if(tgt && (tgt->GetType() == cmTarget::STATIC_LIBRARY ||
  1649. tgt->GetType() == cmTarget::SHARED_LIBRARY ||
  1650. tgt->GetType() == cmTarget::MODULE_LIBRARY ||
  1651. impexe))
  1652. {
  1653. // This is a CMake target. Ask the target for its real name.
  1654. std::string linkItem;
  1655. if(impexe && loader_flag)
  1656. {
  1657. // This link item is an executable that may provide symbols
  1658. // used by this target. A special flag is needed on this
  1659. // platform. Add it now.
  1660. std::string exe = tgt->GetFullPath(config, implib);
  1661. linkItem += loader_flag;
  1662. linkItem += this->Convert(exe.c_str(), NONE, SHELL, false);
  1663. }
  1664. else
  1665. {
  1666. // Pass the full path to the target file but purposely leave
  1667. // off the per-configuration subdirectory. The link directory
  1668. // ordering knows how to deal with this.
  1669. linkItem += tgt->GetDirectory(0, implib);
  1670. // on apple if the FRAMEWORK prop is set, then
  1671. // do not add the target full name but just use the directory
  1672. // name
  1673. #ifdef __APPLE__
  1674. if(!tgt->GetPropertyAsBool("FRAMEWORK"))
  1675. #endif
  1676. {
  1677. linkItem += "/";
  1678. linkItem += tgt->GetFullName(config, implib);
  1679. }
  1680. }
  1681. linkLibraries.push_back(linkItem);
  1682. // For full path, use the true location.
  1683. if(fullPathLibs)
  1684. {
  1685. fullPathLibs->push_back(tgt->GetFullPath(config, implib));
  1686. }
  1687. }
  1688. else
  1689. {
  1690. // This is not a CMake target. Use the name given.
  1691. linkLibraries.push_back(lib);
  1692. // Add to the list of full paths if this library is one.
  1693. if(fullPathLibs &&
  1694. cmSystemTools::FileIsFullPath(lib.c_str()) &&
  1695. !cmSystemTools::FileIsDirectory(lib.c_str()))
  1696. {
  1697. fullPathLibs->push_back(lib);
  1698. }
  1699. }
  1700. }
  1701. }
  1702. // Get the list of directories the target wants to search for libraries.
  1703. const std::vector<std::string>&
  1704. linkDirectories = target.GetLinkDirectories();
  1705. // Lookup link type selection flags.
  1706. const char* static_link_type_flag = 0;
  1707. const char* shared_link_type_flag = 0;
  1708. const char* target_type_str = 0;
  1709. switch(target.GetType())
  1710. {
  1711. case cmTarget::EXECUTABLE: target_type_str = "EXE"; break;
  1712. case cmTarget::SHARED_LIBRARY: target_type_str = "SHARED_LIBRARY"; break;
  1713. case cmTarget::MODULE_LIBRARY: target_type_str = "SHARED_MODULE"; break;
  1714. default: break;
  1715. }
  1716. if(target_type_str)
  1717. {
  1718. if(!linkLanguage)
  1719. {
  1720. cmSystemTools::
  1721. Error("CMake can not determine linker language for target:",
  1722. target.GetName());
  1723. return;
  1724. }
  1725. std::string static_link_type_flag_var = "CMAKE_";
  1726. static_link_type_flag_var += target_type_str;
  1727. static_link_type_flag_var += "_LINK_STATIC_";
  1728. static_link_type_flag_var += linkLanguage;
  1729. static_link_type_flag_var += "_FLAGS";
  1730. static_link_type_flag =
  1731. this->Makefile->GetDefinition(static_link_type_flag_var.c_str());
  1732. std::string shared_link_type_flag_var = "CMAKE_";
  1733. shared_link_type_flag_var += target_type_str;
  1734. shared_link_type_flag_var += "_LINK_DYNAMIC_";
  1735. shared_link_type_flag_var += linkLanguage;
  1736. shared_link_type_flag_var += "_FLAGS";
  1737. shared_link_type_flag =
  1738. this->Makefile->GetDefinition(shared_link_type_flag_var.c_str());
  1739. }
  1740. // Compute the link directory order needed to link the libraries.
  1741. cmOrderLinkDirectories orderLibs;
  1742. orderLibs.SetLinkTypeInformation(cmOrderLinkDirectories::LinkShared,
  1743. static_link_type_flag,
  1744. shared_link_type_flag);
  1745. orderLibs.AddLinkPrefix(
  1746. this->Makefile->GetDefinition("CMAKE_STATIC_LIBRARY_PREFIX"));
  1747. orderLibs.AddLinkPrefix(
  1748. this->Makefile->GetDefinition("CMAKE_SHARED_LIBRARY_PREFIX"));
  1749. // Import library names should be matched and treated as shared
  1750. // libraries for the purposes of linking.
  1751. orderLibs.AddLinkExtension(
  1752. this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"),
  1753. cmOrderLinkDirectories::LinkShared);
  1754. orderLibs.AddLinkExtension(
  1755. this->Makefile->GetDefinition("CMAKE_STATIC_LIBRARY_SUFFIX"),
  1756. cmOrderLinkDirectories::LinkStatic);
  1757. orderLibs.AddLinkExtension(
  1758. this->Makefile->GetDefinition("CMAKE_SHARED_LIBRARY_SUFFIX"),
  1759. cmOrderLinkDirectories::LinkShared);
  1760. orderLibs.AddLinkExtension(
  1761. this->Makefile->GetDefinition("CMAKE_LINK_LIBRARY_SUFFIX"));
  1762. if(const char* linkSuffixes =
  1763. this->Makefile->GetDefinition("CMAKE_EXTRA_LINK_EXTENSIONS"))
  1764. {
  1765. std::vector<std::string> linkSuffixVec;
  1766. cmSystemTools::ExpandListArgument(linkSuffixes, linkSuffixVec);
  1767. for(std::vector<std::string>::iterator i = linkSuffixVec.begin();
  1768. i != linkSuffixVec.end(); ++i)
  1769. {
  1770. orderLibs.AddLinkExtension(i->c_str());
  1771. }
  1772. }
  1773. std::string configSubdir;
  1774. cmGlobalGenerator* gg = this->GetGlobalGenerator();
  1775. gg->AppendDirectoryForConfig("", config, "", configSubdir);
  1776. orderLibs.SetLinkInformation(target.GetName(),
  1777. linkLibraries,
  1778. linkDirectories,
  1779. gg->GetTargetManifest(),
  1780. configSubdir.c_str());
  1781. orderLibs.DetermineLibraryPathOrder();
  1782. std::vector<cmStdString> orderedLibs;
  1783. orderLibs.GetLinkerInformation(outDirs, orderedLibs);
  1784. // Make sure libraries are linked with the proper syntax.
  1785. std::string libLinkFlag =
  1786. this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_FLAG");
  1787. std::string libLinkSuffix =
  1788. this->Makefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_SUFFIX");
  1789. for(std::vector<cmStdString>::iterator l = orderedLibs.begin();
  1790. l != orderedLibs.end(); ++l)
  1791. {
  1792. std::string lib = *l;
  1793. if(lib[0] == '-' || lib[0] == '$' || lib[0] == '`')
  1794. {
  1795. // The library is linked with special syntax by the user.
  1796. outLibs.push_back(lib);
  1797. }
  1798. else
  1799. {
  1800. // Generate the proper link syntax.
  1801. lib = libLinkFlag;
  1802. lib += *l;
  1803. lib += libLinkSuffix;
  1804. outLibs.push_back(lib);
  1805. }
  1806. }
  1807. }
  1808. //----------------------------------------------------------------------------
  1809. void cmLocalGenerator::AddLanguageFlags(std::string& flags,
  1810. const char* lang,
  1811. const char* config)
  1812. {
  1813. // Add language-specific flags.
  1814. std::string flagsVar = "CMAKE_";
  1815. flagsVar += lang;
  1816. flagsVar += "_FLAGS";
  1817. if(this->EmitUniversalBinaryFlags)
  1818. {
  1819. const char* osxArch =
  1820. this->Makefile->GetDefinition("CMAKE_OSX_ARCHITECTURES");
  1821. const char* sysroot =
  1822. this->Makefile->GetDefinition("CMAKE_OSX_SYSROOT");
  1823. if(osxArch && sysroot && lang && lang[0] =='C')
  1824. {
  1825. std::vector<std::string> archs;
  1826. cmSystemTools::ExpandListArgument(std::string(osxArch),
  1827. archs);
  1828. if((archs.size() > 1)
  1829. || ((archs.size()== 1) && this->Makefile->IsOn("CMAKE_DO_TRY_COMPILE")))
  1830. {
  1831. for( std::vector<std::string>::iterator i = archs.begin();
  1832. i != archs.end(); ++i)
  1833. {
  1834. flags += " -arch ";
  1835. flags += *i;
  1836. }
  1837. flags += " -isysroot ";
  1838. flags += sysroot;
  1839. }
  1840. }
  1841. }
  1842. this->AddConfigVariableFlags(flags, flagsVar.c_str(), config);
  1843. }
  1844. //----------------------------------------------------------------------------
  1845. std::string cmLocalGenerator::GetRealDependency(const char* inName,
  1846. const char* config)
  1847. {
  1848. // Older CMake code may specify the dependency using the target
  1849. // output file rather than the target name. Such code would have
  1850. // been written before there was support for target properties that
  1851. // modify the name so stripping down to just the file name should
  1852. // produce the target name in this case.
  1853. std::string name = cmSystemTools::GetFilenameName(inName);
  1854. if(cmSystemTools::GetFilenameLastExtension(name) == ".exe")
  1855. {
  1856. name = cmSystemTools::GetFilenameWithoutLastExtension(name);
  1857. }
  1858. // Look for a CMake target with the given name.
  1859. if(cmTarget* target = this->GlobalGenerator->FindTarget(0,name.c_str(),true))
  1860. {
  1861. // make sure it is not just a coincidence that the target name
  1862. // found is part of the inName
  1863. if(cmSystemTools::FileIsFullPath(inName))
  1864. {
  1865. std::string tLocation = target->GetLocation(config);
  1866. tLocation = cmSystemTools::GetFilenamePath(tLocation);
  1867. std::string depLocation = cmSystemTools::GetFilenamePath(
  1868. std::string(inName));
  1869. depLocation = cmSystemTools::CollapseFullPath(depLocation.c_str());
  1870. tLocation = cmSystemTools::CollapseFullPath(tLocation.c_str());
  1871. if(depLocation != tLocation)
  1872. {
  1873. // it is a full path to a depend that has the same name
  1874. // as a target but is in a different location so do not use
  1875. // the target as the depend
  1876. return inName;
  1877. }
  1878. }
  1879. switch (target->GetType())
  1880. {
  1881. case cmTarget::EXECUTABLE:
  1882. case cmTarget::STATIC_LIBRARY:
  1883. case cmTarget::SHARED_LIBRARY:
  1884. case cmTarget::MODULE_LIBRARY:
  1885. {
  1886. // Get the location of the target's output file and depend on it.
  1887. if(const char* location = target->GetLocation(config))
  1888. {
  1889. return location;
  1890. }
  1891. }
  1892. break;
  1893. case cmTarget::UTILITY:
  1894. case cmTarget::GLOBAL_TARGET:
  1895. // Depending on a utility target may not work but just trust
  1896. // the user to have given a valid name.
  1897. return inName;
  1898. case cmTarget::INSTALL_FILES:
  1899. case cmTarget::INSTALL_PROGRAMS:
  1900. case cmTarget::INSTALL_DIRECTORY:
  1901. break;
  1902. }
  1903. }
  1904. // The name was not that of a CMake target. It must name a file.
  1905. if(cmSystemTools::FileIsFullPath(inName))
  1906. {
  1907. // This is a full path. Return it as given.
  1908. return inName;
  1909. }
  1910. // Treat the name as relative to the source directory in which it
  1911. // was given.
  1912. name = this->Makefile->GetCurrentDirectory();
  1913. name += "/";
  1914. name += inName;
  1915. return name;
  1916. }
  1917. //----------------------------------------------------------------------------
  1918. std::string cmLocalGenerator::GetRealLocation(const char* inName,
  1919. const char* config)
  1920. {
  1921. std::string outName=inName;
  1922. // Look for a CMake target with the given name, which is an executable
  1923. // and which can be run
  1924. cmTarget* target = this->GlobalGenerator->FindTarget(0, inName, true);
  1925. if ((target != 0)
  1926. && (target->GetType() == cmTarget::EXECUTABLE)
  1927. && ((this->Makefile->IsOn("CMAKE_CROSSCOMPILING") == false)
  1928. || (target->IsImported() == true)))
  1929. {
  1930. outName = target->GetLocation( config );
  1931. }
  1932. return outName;
  1933. }
  1934. //----------------------------------------------------------------------------
  1935. void cmLocalGenerator::AddSharedFlags(std::string& flags,
  1936. const char* lang,
  1937. bool shared)
  1938. {
  1939. std::string flagsVar;
  1940. // Add flags for dealing with shared libraries for this language.
  1941. if(shared)
  1942. {
  1943. flagsVar = "CMAKE_SHARED_LIBRARY_";
  1944. flagsVar += lang;
  1945. flagsVar += "_FLAGS";
  1946. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  1947. }
  1948. // Add flags specific to shared builds.
  1949. if(cmSystemTools::IsOn(this->Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1950. {
  1951. flagsVar = "CMAKE_SHARED_BUILD_";
  1952. flagsVar += lang;
  1953. flagsVar += "_FLAGS";
  1954. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  1955. }
  1956. }
  1957. //----------------------------------------------------------------------------
  1958. void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
  1959. const char* var,
  1960. const char* config)
  1961. {
  1962. // Add the flags from the variable itself.
  1963. std::string flagsVar = var;
  1964. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  1965. // Add the flags from the build-type specific variable.
  1966. if(config && *config)
  1967. {
  1968. flagsVar += "_";
  1969. flagsVar += cmSystemTools::UpperCase(config);
  1970. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  1971. }
  1972. }
  1973. //----------------------------------------------------------------------------
  1974. void cmLocalGenerator::AppendFlags(std::string& flags,
  1975. const char* newFlags)
  1976. {
  1977. if(newFlags && *newFlags)
  1978. {
  1979. std::string newf = newFlags;
  1980. if(flags.size())
  1981. {
  1982. flags += " ";
  1983. }
  1984. flags += newFlags;
  1985. }
  1986. }
  1987. //----------------------------------------------------------------------------
  1988. std::string
  1989. cmLocalGenerator::ConstructComment(const cmCustomCommand& cc,
  1990. const char* default_comment)
  1991. {
  1992. // Check for a comment provided with the command.
  1993. if(cc.GetComment())
  1994. {
  1995. return cc.GetComment();
  1996. }
  1997. // Construct a reasonable default comment if possible.
  1998. if(!cc.GetOutputs().empty())
  1999. {
  2000. std::string comment;
  2001. comment = "Generating ";
  2002. const char* sep = "";
  2003. for(std::vector<std::string>::const_iterator o = cc.GetOutputs().begin();
  2004. o != cc.GetOutputs().end(); ++o)
  2005. {
  2006. comment += sep;
  2007. comment += this->Convert(o->c_str(), cmLocalGenerator::START_OUTPUT);
  2008. sep = ", ";
  2009. }
  2010. return comment;
  2011. }
  2012. // Otherwise use the provided default.
  2013. return default_comment;
  2014. }
  2015. //----------------------------------------------------------------------------
  2016. std::string
  2017. cmLocalGenerator::ConvertToOptionallyRelativeOutputPath(const char* remote)
  2018. {
  2019. return this->Convert(remote, START_OUTPUT, SHELL, true);
  2020. }
  2021. //----------------------------------------------------------------------------
  2022. std::string cmLocalGenerator::Convert(const char* source,
  2023. RelativeRoot relative,
  2024. OutputFormat output,
  2025. bool optional)
  2026. {
  2027. // Make sure the relative path conversion components are set.
  2028. if(!this->PathConversionsSetup)
  2029. {
  2030. this->SetupPathConversions();
  2031. this->PathConversionsSetup = true;
  2032. }
  2033. // Convert the path to a relative path.
  2034. std::string result = source;
  2035. if (!optional || this->UseRelativePaths)
  2036. {
  2037. switch (relative)
  2038. {
  2039. case HOME:
  2040. //result = cmSystemTools::CollapseFullPath(result.c_str());
  2041. result = this->ConvertToRelativePath(this->HomeDirectoryComponents,
  2042. result.c_str());
  2043. break;
  2044. case START:
  2045. //result = cmSystemTools::CollapseFullPath(result.c_str());
  2046. result = this->ConvertToRelativePath(this->StartDirectoryComponents,
  2047. result.c_str());
  2048. break;
  2049. case HOME_OUTPUT:
  2050. //result = cmSystemTools::CollapseFullPath(result.c_str());
  2051. result =
  2052. this->ConvertToRelativePath(this->HomeOutputDirectoryComponents,
  2053. result.c_str());
  2054. break;
  2055. case START_OUTPUT:
  2056. //result = cmSystemTools::CollapseFullPath(result.c_str());
  2057. result =
  2058. this->ConvertToRelativePath(this->StartOutputDirectoryComponents,
  2059. result.c_str());
  2060. break;
  2061. case FULL:
  2062. result = cmSystemTools::CollapseFullPath(result.c_str());
  2063. break;
  2064. case NONE:
  2065. break;
  2066. }
  2067. }
  2068. // Now convert it to an output path.
  2069. if (output == MAKEFILE)
  2070. {
  2071. result = cmSystemTools::ConvertToOutputPath(result.c_str());
  2072. }
  2073. if( output == SHELL)
  2074. {
  2075. // For the MSYS shell convert drive letters to posix paths, so
  2076. // that c:/some/path becomes /c/some/path. This is needed to
  2077. // avoid problems with the shell path translation.
  2078. if(this->MSYSShell)
  2079. {
  2080. if(result.size() > 2 && result[1] == ':')
  2081. {
  2082. result[1] = result[0];
  2083. result[0] = '/';
  2084. }
  2085. }
  2086. if(this->WindowsShell)
  2087. {
  2088. std::string::size_type pos = 0;
  2089. while((pos = result.find('/', pos)) != std::string::npos)
  2090. {
  2091. result[pos] = '\\';
  2092. pos++;
  2093. }
  2094. }
  2095. result = this->EscapeForShell(result.c_str(), true, false);
  2096. }
  2097. return result;
  2098. }
  2099. //----------------------------------------------------------------------------
  2100. std::string cmLocalGenerator::FindRelativePathTopSource()
  2101. {
  2102. // Relative path conversion within a single tree managed by CMake is
  2103. // safe. We can use our parent relative path top if and only if
  2104. // this is a subdirectory of that top.
  2105. if(cmLocalGenerator* parent = this->GetParent())
  2106. {
  2107. std::string parentTop = parent->FindRelativePathTopSource();
  2108. if(cmSystemTools::IsSubDirectory(
  2109. this->Makefile->GetStartDirectory(), parentTop.c_str()))
  2110. {
  2111. return parentTop;
  2112. }
  2113. }
  2114. // Otherwise this directory itself is the new top.
  2115. return this->Makefile->GetStartDirectory();
  2116. }
  2117. //----------------------------------------------------------------------------
  2118. std::string cmLocalGenerator::FindRelativePathTopBinary()
  2119. {
  2120. // Relative path conversion within a single tree managed by CMake is
  2121. // safe. We can use our parent relative path top if and only if
  2122. // this is a subdirectory of that top.
  2123. if(cmLocalGenerator* parent = this->GetParent())
  2124. {
  2125. std::string parentTop = parent->FindRelativePathTopBinary();
  2126. if(cmSystemTools::IsSubDirectory(
  2127. this->Makefile->GetStartOutputDirectory(), parentTop.c_str()))
  2128. {
  2129. return parentTop;
  2130. }
  2131. }
  2132. // Otherwise this directory itself is the new top.
  2133. return this->Makefile->GetStartOutputDirectory();
  2134. }
  2135. //----------------------------------------------------------------------------
  2136. void cmLocalGenerator::ConfigureRelativePaths()
  2137. {
  2138. // Relative path conversion inside the source tree is not used to
  2139. // construct relative paths passed to build tools so it is safe to
  2140. // even when the source is a network path.
  2141. std::string source = this->FindRelativePathTopSource();
  2142. this->RelativePathTopSource = source;
  2143. // The current working directory on Windows cannot be a network
  2144. // path. Therefore relative paths cannot work when the binary tree
  2145. // is a network path.
  2146. std::string binary = this->FindRelativePathTopBinary();
  2147. if(binary.size() < 2 || binary.substr(0, 2) != "//")
  2148. {
  2149. this->RelativePathTopBinary = binary;
  2150. }
  2151. else
  2152. {
  2153. this->RelativePathTopBinary = "";
  2154. }
  2155. }
  2156. //----------------------------------------------------------------------------
  2157. static bool cmLocalGeneratorNotAbove(const char* a, const char* b)
  2158. {
  2159. return (cmSystemTools::ComparePath(a, b) ||
  2160. cmSystemTools::IsSubDirectory(a, b));
  2161. }
  2162. //----------------------------------------------------------------------------
  2163. std::string
  2164. cmLocalGenerator
  2165. ::ConvertToRelativePath(const std::vector<std::string>& local,
  2166. const char* in_remote)
  2167. {
  2168. // The path should never be quoted.
  2169. assert(in_remote[0] != '\"');
  2170. // The local path should never have a trailing slash.
  2171. assert(local.size() > 0 && !(local[local.size()-1] == ""));
  2172. // If the path is already relative then just return the path.
  2173. if(!cmSystemTools::FileIsFullPath(in_remote))
  2174. {
  2175. return in_remote;
  2176. }
  2177. // Make sure relative path conversion is configured.
  2178. if(!this->RelativePathsConfigured)
  2179. {
  2180. this->ConfigureRelativePaths();
  2181. this->RelativePathsConfigured = true;
  2182. }
  2183. // Skip conversion if the path and local are not both in the source
  2184. // or both in the binary tree.
  2185. std::string local_path = cmSystemTools::JoinPath(local);
  2186. if(!((cmLocalGeneratorNotAbove(local_path.c_str(),
  2187. this->RelativePathTopBinary.c_str()) &&
  2188. cmLocalGeneratorNotAbove(in_remote,
  2189. this->RelativePathTopBinary.c_str())) ||
  2190. (cmLocalGeneratorNotAbove(local_path.c_str(),
  2191. this->RelativePathTopSource.c_str()) &&
  2192. cmLocalGeneratorNotAbove(in_remote,
  2193. this->RelativePathTopSource.c_str()))))
  2194. {
  2195. return in_remote;
  2196. }
  2197. // Identify the longest shared path component between the remote
  2198. // path and the local path.
  2199. std::vector<std::string> remote;
  2200. cmSystemTools::SplitPath(in_remote, remote);
  2201. unsigned int common=0;
  2202. while(common < remote.size() &&
  2203. common < local.size() &&
  2204. cmSystemTools::ComparePath(remote[common].c_str(),
  2205. local[common].c_str()))
  2206. {
  2207. ++common;
  2208. }
  2209. // If no part of the path is in common then return the full path.
  2210. if(common == 0)
  2211. {
  2212. return in_remote;
  2213. }
  2214. // If the entire path is in common then just return a ".".
  2215. if(common == remote.size() &&
  2216. common == local.size())
  2217. {
  2218. return ".";
  2219. }
  2220. // If the entire path is in common except for a trailing slash then
  2221. // just return a "./".
  2222. if(common+1 == remote.size() &&
  2223. remote[common].size() == 0 &&
  2224. common == local.size())
  2225. {
  2226. return "./";
  2227. }
  2228. // Construct the relative path.
  2229. std::string relative;
  2230. // First add enough ../ to get up to the level of the shared portion
  2231. // of the path. Leave off the trailing slash. Note that the last
  2232. // component of local will never be empty because local should never
  2233. // have a trailing slash.
  2234. for(unsigned int i=common; i < local.size(); ++i)
  2235. {
  2236. relative += "..";
  2237. if(i < local.size()-1)
  2238. {
  2239. relative += "/";
  2240. }
  2241. }
  2242. // Now add the portion of the destination path that is not included
  2243. // in the shared portion of the path. Add a slash the first time
  2244. // only if there was already something in the path. If there was a
  2245. // trailing slash in the input then the last iteration of the loop
  2246. // will add a slash followed by an empty string which will preserve
  2247. // the trailing slash in the output.
  2248. for(unsigned int i=common; i < remote.size(); ++i)
  2249. {
  2250. if(relative.size() > 0)
  2251. {
  2252. relative += "/";
  2253. }
  2254. relative += remote[i];
  2255. }
  2256. // Finally return the path.
  2257. return relative;
  2258. }
  2259. //----------------------------------------------------------------------------
  2260. void
  2261. cmLocalGenerator
  2262. ::GenerateTargetInstallRules(
  2263. std::ostream& os, const char* config,
  2264. std::vector<std::string> const& configurationTypes)
  2265. {
  2266. // Convert the old-style install specification from each target to
  2267. // an install generator and run it.
  2268. cmTargets& tgts = this->Makefile->GetTargets();
  2269. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); ++l)
  2270. {
  2271. // Include the user-specified pre-install script for this target.
  2272. if(const char* preinstall = l->second.GetProperty("PRE_INSTALL_SCRIPT"))
  2273. {
  2274. cmInstallScriptGenerator g(preinstall);
  2275. g.Generate(os, config, configurationTypes);
  2276. }
  2277. // Install this target if a destination is given.
  2278. if(l->second.GetInstallPath() != "")
  2279. {
  2280. // Compute the full install destination. Note that converting
  2281. // to unix slashes also removes any trailing slash.
  2282. std::string destination = "${CMAKE_INSTALL_PREFIX}";
  2283. destination += l->second.GetInstallPath();
  2284. cmSystemTools::ConvertToUnixSlashes(destination);
  2285. // Generate the proper install generator for this target type.
  2286. switch(l->second.GetType())
  2287. {
  2288. case cmTarget::EXECUTABLE:
  2289. case cmTarget::STATIC_LIBRARY:
  2290. case cmTarget::MODULE_LIBRARY:
  2291. {
  2292. // Use a target install generator.
  2293. cmInstallTargetGenerator g(l->second, destination.c_str(), false);
  2294. g.Generate(os, config, configurationTypes);
  2295. }
  2296. break;
  2297. case cmTarget::SHARED_LIBRARY:
  2298. {
  2299. #if defined(_WIN32) || defined(__CYGWIN__)
  2300. // Special code to handle DLL. Install the import library
  2301. // to the normal destination and the DLL to the runtime
  2302. // destination.
  2303. cmInstallTargetGenerator g1(l->second, destination.c_str(), true);
  2304. g1.Generate(os, config, configurationTypes);
  2305. destination = "${CMAKE_INSTALL_PREFIX}";
  2306. destination += l->second.GetRuntimeInstallPath();
  2307. cmSystemTools::ConvertToUnixSlashes(destination);
  2308. cmInstallTargetGenerator g2(l->second, destination.c_str(), false);
  2309. g2.Generate(os, config, configurationTypes);
  2310. #else
  2311. // Use a target install generator.
  2312. cmInstallTargetGenerator g(l->second, destination.c_str(), false);
  2313. g.Generate(os, config, configurationTypes);
  2314. #endif
  2315. }
  2316. break;
  2317. default:
  2318. break;
  2319. }
  2320. }
  2321. // Include the user-specified post-install script for this target.
  2322. if(const char* postinstall = l->second.GetProperty("POST_INSTALL_SCRIPT"))
  2323. {
  2324. cmInstallScriptGenerator g(postinstall);
  2325. g.Generate(os, config, configurationTypes);
  2326. }
  2327. }
  2328. }
  2329. #if defined(CM_LG_ENCODE_OBJECT_NAMES)
  2330. static std::string cmLocalGeneratorMD5(const char* input)
  2331. {
  2332. char md5out[32];
  2333. cmsysMD5* md5 = cmsysMD5_New();
  2334. cmsysMD5_Initialize(md5);
  2335. cmsysMD5_Append(md5, reinterpret_cast<unsigned char const*>(input), -1);
  2336. cmsysMD5_FinalizeHex(md5, md5out);
  2337. cmsysMD5_Delete(md5);
  2338. return std::string(md5out, 32);
  2339. }
  2340. static bool
  2341. cmLocalGeneratorShortenObjectName(std::string& objName,
  2342. std::string::size_type max_len)
  2343. {
  2344. // Replace the beginning of the path portion of the object name with
  2345. // its own md5 sum.
  2346. std::string::size_type pos = objName.find('/', objName.size()-max_len+32);
  2347. if(pos != objName.npos)
  2348. {
  2349. std::string md5name = cmLocalGeneratorMD5(objName.substr(0, pos).c_str());
  2350. md5name += objName.substr(pos);
  2351. objName = md5name;
  2352. // The object name is now short enough.
  2353. return true;
  2354. }
  2355. else
  2356. {
  2357. // The object name could not be shortened enough.
  2358. return false;
  2359. }
  2360. }
  2361. static bool cmLocalGeneratorCheckObjectName(std::string& objName,
  2362. std::string::size_type dir_len)
  2363. {
  2364. // Choose a maximum file name length.
  2365. #if defined(_WIN32) || defined(__CYGWIN__)
  2366. std::string::size_type const max_total_len = 250;
  2367. #else
  2368. std::string::size_type const max_total_len = 1000;
  2369. #endif
  2370. // Enforce the maximum file name length if possible.
  2371. std::string::size_type max_obj_len = max_total_len;
  2372. if(dir_len < max_total_len)
  2373. {
  2374. max_obj_len = max_total_len - dir_len;
  2375. if(objName.size() > max_obj_len)
  2376. {
  2377. // The current object file name is too long. Try to shorten it.
  2378. return cmLocalGeneratorShortenObjectName(objName, max_obj_len);
  2379. }
  2380. else
  2381. {
  2382. // The object file name is short enough.
  2383. return true;
  2384. }
  2385. }
  2386. else
  2387. {
  2388. // The build directory in which the object will be stored is
  2389. // already too deep.
  2390. return false;
  2391. }
  2392. }
  2393. #endif
  2394. //----------------------------------------------------------------------------
  2395. std::string&
  2396. cmLocalGenerator
  2397. ::CreateSafeUniqueObjectFileName(const char* sin,
  2398. std::string::size_type dir_len)
  2399. {
  2400. // Look for an existing mapped name for this object file.
  2401. std::map<cmStdString,cmStdString>::iterator it =
  2402. this->UniqueObjectNamesMap.find(sin);
  2403. // If no entry exists create one.
  2404. if(it == this->UniqueObjectNamesMap.end())
  2405. {
  2406. // Start with the original name.
  2407. std::string ssin = sin;
  2408. // Avoid full paths by removing leading slashes.
  2409. std::string::size_type pos = 0;
  2410. for(;pos < ssin.size() && ssin[pos] == '/'; ++pos);
  2411. ssin = ssin.substr(pos);
  2412. // Avoid full paths by removing colons.
  2413. cmSystemTools::ReplaceString(ssin, ":", "_");
  2414. // Avoid relative paths that go up the tree.
  2415. cmSystemTools::ReplaceString(ssin, "../", "__/");
  2416. // Avoid spaces.
  2417. cmSystemTools::ReplaceString(ssin, " ", "_");
  2418. // Mangle the name if necessary.
  2419. if(this->Makefile->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES"))
  2420. {
  2421. bool done;
  2422. int cc = 0;
  2423. char rpstr[100];
  2424. sprintf(rpstr, "_p_");
  2425. cmSystemTools::ReplaceString(ssin, "+", rpstr);
  2426. std::string sssin = sin;
  2427. do
  2428. {
  2429. done = true;
  2430. for ( it = this->UniqueObjectNamesMap.begin();
  2431. it != this->UniqueObjectNamesMap.end();
  2432. ++ it )
  2433. {
  2434. if ( it->second == ssin )
  2435. {
  2436. done = false;
  2437. }
  2438. }
  2439. if ( done )
  2440. {
  2441. break;
  2442. }
  2443. sssin = ssin;
  2444. cmSystemTools::ReplaceString(ssin, "_p_", rpstr);
  2445. sprintf(rpstr, "_p%d_", cc++);
  2446. }
  2447. while ( !done );
  2448. }
  2449. #if defined(CM_LG_ENCODE_OBJECT_NAMES)
  2450. cmLocalGeneratorCheckObjectName(ssin, dir_len);
  2451. #else
  2452. (void)dir_len;
  2453. #endif
  2454. // Insert the newly mapped object file name.
  2455. std::map<cmStdString, cmStdString>::value_type e(sin, ssin);
  2456. it = this->UniqueObjectNamesMap.insert(e).first;
  2457. }
  2458. // Return the map entry.
  2459. return it->second;
  2460. }
  2461. //----------------------------------------------------------------------------
  2462. std::string
  2463. cmLocalGenerator
  2464. ::GetObjectFileNameWithoutTarget(const cmSourceFile& source,
  2465. std::string::size_type dir_len)
  2466. {
  2467. // Construct the object file name using the full path to the source
  2468. // file which is its only unique identification.
  2469. const char* fullPath = source.GetFullPath().c_str();
  2470. // Try referencing the source relative to the source tree.
  2471. std::string relFromSource = this->Convert(fullPath, START);
  2472. assert(!relFromSource.empty());
  2473. bool relSource = !cmSystemTools::FileIsFullPath(relFromSource.c_str());
  2474. bool subSource = relSource && relFromSource[0] != '.';
  2475. // Try referencing the source relative to the binary tree.
  2476. std::string relFromBinary = this->Convert(fullPath, START_OUTPUT);
  2477. assert(!relFromBinary.empty());
  2478. bool relBinary = !cmSystemTools::FileIsFullPath(relFromBinary.c_str());
  2479. bool subBinary = relBinary && relFromBinary[0] != '.';
  2480. // Select a nice-looking reference to the source file to construct
  2481. // the object file name.
  2482. std::string objectName;
  2483. if((relSource && !relBinary) || (subSource && !subBinary))
  2484. {
  2485. objectName = relFromSource;
  2486. }
  2487. else if((relBinary && !relSource) || (subBinary && !subSource))
  2488. {
  2489. objectName = relFromBinary;
  2490. }
  2491. else if(relFromBinary.length() < relFromSource.length())
  2492. {
  2493. objectName = relFromBinary;
  2494. }
  2495. else
  2496. {
  2497. objectName = relFromSource;
  2498. }
  2499. // if it is still a full path check for the try compile case
  2500. // try compile never have in source sources, and should not
  2501. // have conflicting source file names in the same target
  2502. if(cmSystemTools::FileIsFullPath(objectName.c_str()))
  2503. {
  2504. if(this->GetGlobalGenerator()->GetCMakeInstance()->GetIsInTryCompile())
  2505. {
  2506. objectName = cmSystemTools::GetFilenameName(source.GetFullPath());
  2507. }
  2508. }
  2509. // Replace the original source file extension with the object file
  2510. // extension.
  2511. if(!source.GetPropertyAsBool("KEEP_EXTENSION"))
  2512. {
  2513. // Remove the original extension.
  2514. std::string::size_type dot_pos = objectName.rfind(".");
  2515. if(dot_pos != std::string::npos)
  2516. {
  2517. objectName = objectName.substr(0, dot_pos);
  2518. }
  2519. // Store the new extension.
  2520. objectName +=
  2521. this->GlobalGenerator->GetLanguageOutputExtension(source);
  2522. }
  2523. // Convert to a safe name.
  2524. return this->CreateSafeUniqueObjectFileName(objectName.c_str(), dir_len);
  2525. }
  2526. //----------------------------------------------------------------------------
  2527. const char*
  2528. cmLocalGenerator
  2529. ::GetSourceFileLanguage(const cmSourceFile& source)
  2530. {
  2531. return source.GetLanguage();
  2532. }
  2533. //----------------------------------------------------------------------------
  2534. std::string cmLocalGenerator::EscapeForShellOldStyle(const char* str)
  2535. {
  2536. std::string result;
  2537. bool forceOn = cmSystemTools::GetForceUnixPaths();
  2538. if(forceOn && this->WindowsShell)
  2539. {
  2540. cmSystemTools::SetForceUnixPaths(false);
  2541. }
  2542. result = cmSystemTools::EscapeSpaces(str);
  2543. if(forceOn && this->WindowsShell)
  2544. {
  2545. cmSystemTools::SetForceUnixPaths(true);
  2546. }
  2547. return result;
  2548. }
  2549. //----------------------------------------------------------------------------
  2550. std::string cmLocalGenerator::EscapeForShell(const char* str, bool makeVars,
  2551. bool forEcho)
  2552. {
  2553. // Compute the flags for the target shell environment.
  2554. int flags = 0;
  2555. if(this->WindowsVSIDE)
  2556. {
  2557. flags |= cmsysSystem_Shell_Flag_VSIDE;
  2558. }
  2559. else
  2560. {
  2561. flags |= cmsysSystem_Shell_Flag_Make;
  2562. }
  2563. if(makeVars)
  2564. {
  2565. flags |= cmsysSystem_Shell_Flag_AllowMakeVariables;
  2566. }
  2567. if(forEcho)
  2568. {
  2569. flags |= cmsysSystem_Shell_Flag_EchoWindows;
  2570. }
  2571. if(this->WatcomWMake)
  2572. {
  2573. flags |= cmsysSystem_Shell_Flag_WatcomWMake;
  2574. }
  2575. if(this->MinGWMake)
  2576. {
  2577. flags |= cmsysSystem_Shell_Flag_MinGWMake;
  2578. }
  2579. // Compute the buffer size needed.
  2580. int size = (this->WindowsShell ?
  2581. cmsysSystem_Shell_GetArgumentSizeForWindows(str, flags) :
  2582. cmsysSystem_Shell_GetArgumentSizeForUnix(str, flags));
  2583. // Compute the shell argument itself.
  2584. std::vector<char> arg(size);
  2585. if(this->WindowsShell)
  2586. {
  2587. cmsysSystem_Shell_GetArgumentForWindows(str, &arg[0], flags);
  2588. }
  2589. else
  2590. {
  2591. cmsysSystem_Shell_GetArgumentForUnix(str, &arg[0], flags);
  2592. }
  2593. return std::string(&arg[0]);
  2594. }
  2595. void
  2596. cmLocalGenerator::GetTargetObjectFileDirectories(cmTarget* ,
  2597. std::vector<std::string>&
  2598. )
  2599. {
  2600. cmSystemTools::Error("GetTargetObjectFileDirectories"
  2601. " called on cmLocalGenerator");
  2602. }