cmLocalGenerator.cxx 92 KB

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