cmLocalGenerator.cxx 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmLocalGenerator.h"
  11. #include "cmComputeLinkInformation.h"
  12. #include "cmGeneratedFileStream.h"
  13. #include "cmGlobalGenerator.h"
  14. #include "cmInstallGenerator.h"
  15. #include "cmInstallFilesGenerator.h"
  16. #include "cmInstallScriptGenerator.h"
  17. #include "cmInstallTargetGenerator.h"
  18. #include "cmMakefile.h"
  19. #include "cmSourceFile.h"
  20. #include "cmTest.h"
  21. #include "cmTestGenerator.h"
  22. #include "cmVersion.h"
  23. #include "cmake.h"
  24. #if defined(CMAKE_BUILD_WITH_CMAKE)
  25. # define CM_LG_ENCODE_OBJECT_NAMES
  26. # include <cmsys/MD5.h>
  27. #endif
  28. #include <cmsys/System.h>
  29. #include <ctype.h> // for isalpha
  30. #include <assert.h>
  31. #if defined(__HAIKU__)
  32. #include <FindDirectory.h>
  33. #include <StorageDefs.h>
  34. #endif
  35. cmLocalGenerator::cmLocalGenerator()
  36. {
  37. this->Makefile = 0; // moved to after set on global
  38. this->Parent = 0;
  39. this->WindowsShell = false;
  40. this->WindowsVSIDE = false;
  41. this->WatcomWMake = false;
  42. this->MinGWMake = false;
  43. this->NMake = false;
  44. this->MSYSShell = false;
  45. this->LinkScriptShell = false;
  46. this->IgnoreLibPrefix = false;
  47. this->UseRelativePaths = false;
  48. this->Configured = false;
  49. this->EmitUniversalBinaryFlags = true;
  50. this->RelativePathsConfigured = false;
  51. this->PathConversionsSetup = false;
  52. this->BackwardsCompatibility = 0;
  53. this->BackwardsCompatibilityFinal = false;
  54. }
  55. cmLocalGenerator::~cmLocalGenerator()
  56. {
  57. delete this->Makefile;
  58. }
  59. //----------------------------------------------------------------------------
  60. class cmLocalGeneratorCurrent
  61. {
  62. cmGlobalGenerator* GG;
  63. cmLocalGenerator* LG;
  64. public:
  65. cmLocalGeneratorCurrent(cmLocalGenerator* lg)
  66. {
  67. this->GG = lg->GetGlobalGenerator();
  68. this->LG = this->GG->GetCurrentLocalGenerator();
  69. this->GG->SetCurrentLocalGenerator(lg);
  70. }
  71. ~cmLocalGeneratorCurrent()
  72. {
  73. this->GG->SetCurrentLocalGenerator(this->LG);
  74. }
  75. };
  76. //----------------------------------------------------------------------------
  77. void cmLocalGenerator::Configure()
  78. {
  79. // Manage the global generator's current local generator.
  80. cmLocalGeneratorCurrent clg(this);
  81. static_cast<void>(clg);
  82. // make sure the CMakeFiles dir is there
  83. std::string filesDir = this->Makefile->GetStartOutputDirectory();
  84. filesDir += cmake::GetCMakeFilesDirectory();
  85. cmSystemTools::MakeDirectory(filesDir.c_str());
  86. // find & read the list file
  87. this->ReadInputFile();
  88. // at the end of the ReadListFile handle any old style subdirs
  89. // first get all the subdirectories
  90. std::vector<cmLocalGenerator *> subdirs = this->GetChildren();
  91. // for each subdir recurse
  92. std::vector<cmLocalGenerator *>::iterator sdi = subdirs.begin();
  93. for (; sdi != subdirs.end(); ++sdi)
  94. {
  95. if (!(*sdi)->Configured)
  96. {
  97. this->Makefile->ConfigureSubDirectory(*sdi);
  98. }
  99. }
  100. this->Makefile->AddCMakeDependFilesFromUser();
  101. // Check whether relative paths should be used for optionally
  102. // relative paths.
  103. this->UseRelativePaths = this->Makefile->IsOn("CMAKE_USE_RELATIVE_PATHS");
  104. this->ComputeObjectMaxPath();
  105. this->Configured = true;
  106. }
  107. //----------------------------------------------------------------------------
  108. void cmLocalGenerator::ComputeObjectMaxPath()
  109. {
  110. // Choose a maximum object file name length.
  111. #if defined(_WIN32) || defined(__CYGWIN__)
  112. this->ObjectPathMax = 250;
  113. #else
  114. this->ObjectPathMax = 1000;
  115. #endif
  116. const char* plen = this->Makefile->GetDefinition("CMAKE_OBJECT_PATH_MAX");
  117. if(plen && *plen)
  118. {
  119. unsigned int pmax;
  120. if(sscanf(plen, "%u", &pmax) == 1)
  121. {
  122. if(pmax >= 128)
  123. {
  124. this->ObjectPathMax = pmax;
  125. }
  126. else
  127. {
  128. cmOStringStream w;
  129. w << "CMAKE_OBJECT_PATH_MAX is set to " << pmax
  130. << ", which is less than the minimum of 128. "
  131. << "The value will be ignored.";
  132. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  133. }
  134. }
  135. else
  136. {
  137. cmOStringStream w;
  138. w << "CMAKE_OBJECT_PATH_MAX is set to \"" << plen
  139. << "\", which fails to parse as a positive integer. "
  140. << "The value will be ignored.";
  141. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  142. }
  143. }
  144. this->ObjectMaxPathViolations.clear();
  145. }
  146. //----------------------------------------------------------------------------
  147. void cmLocalGenerator::ReadInputFile()
  148. {
  149. // Look for the CMakeLists.txt file.
  150. std::string currentStart = this->Makefile->GetStartDirectory();
  151. currentStart += "/CMakeLists.txt";
  152. if(cmSystemTools::FileExists(currentStart.c_str(), true))
  153. {
  154. this->Makefile->ReadListFile(currentStart.c_str());
  155. return;
  156. }
  157. if(!this->Parent)
  158. {
  159. return;
  160. }
  161. // The file is missing. Check policy CMP0014.
  162. cmMakefile* mf = this->Parent->GetMakefile();
  163. cmOStringStream e;
  164. e << "The source directory\n"
  165. << " " << this->Makefile->GetStartDirectory() << "\n"
  166. << "does not contain a CMakeLists.txt file.";
  167. switch (mf->GetPolicyStatus(cmPolicies::CMP0014))
  168. {
  169. case cmPolicies::WARN:
  170. // Print the warning.
  171. e << "\n"
  172. << "CMake does not support this case but it used "
  173. << "to work accidentally and is being allowed for "
  174. << "compatibility."
  175. << "\n"
  176. << mf->GetPolicies()->GetPolicyWarning(cmPolicies::CMP0014);
  177. mf->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  178. case cmPolicies::OLD:
  179. // OLD behavior does not warn.
  180. return;
  181. case cmPolicies::REQUIRED_IF_USED:
  182. case cmPolicies::REQUIRED_ALWAYS:
  183. e << "\n"
  184. << mf->GetPolicies()->GetRequiredPolicyError(cmPolicies::CMP0014);
  185. case cmPolicies::NEW:
  186. // NEW behavior prints the error.
  187. mf->IssueMessage(cmake::FATAL_ERROR, e.str());
  188. break;
  189. }
  190. }
  191. void cmLocalGenerator::SetupPathConversions()
  192. {
  193. // Setup the current output directory components for use by
  194. // Convert
  195. std::string outdir;
  196. outdir =
  197. cmSystemTools::CollapseFullPath(this->Makefile->GetHomeDirectory());
  198. cmSystemTools::SplitPath(outdir.c_str(), this->HomeDirectoryComponents);
  199. outdir =
  200. cmSystemTools::CollapseFullPath(this->Makefile->GetStartDirectory());
  201. cmSystemTools::SplitPath(outdir.c_str(), this->StartDirectoryComponents);
  202. outdir = cmSystemTools::CollapseFullPath
  203. (this->Makefile->GetHomeOutputDirectory());
  204. cmSystemTools::SplitPath(outdir.c_str(),
  205. this->HomeOutputDirectoryComponents);
  206. outdir = cmSystemTools::CollapseFullPath
  207. (this->Makefile->GetStartOutputDirectory());
  208. cmSystemTools::SplitPath(outdir.c_str(),
  209. this->StartOutputDirectoryComponents);
  210. }
  211. void cmLocalGenerator::SetGlobalGenerator(cmGlobalGenerator *gg)
  212. {
  213. this->GlobalGenerator = gg;
  214. this->Makefile = new cmMakefile;
  215. this->Makefile->SetLocalGenerator(this);
  216. // setup the home directories
  217. this->Makefile->GetProperties().SetCMakeInstance(gg->GetCMakeInstance());
  218. this->Makefile->SetHomeDirectory(
  219. gg->GetCMakeInstance()->GetHomeDirectory());
  220. this->Makefile->SetHomeOutputDirectory(
  221. gg->GetCMakeInstance()->GetHomeOutputDirectory());
  222. }
  223. void cmLocalGenerator::ConfigureFinalPass()
  224. {
  225. this->Makefile->ConfigureFinalPass();
  226. }
  227. void cmLocalGenerator::TraceDependencies()
  228. {
  229. // Generate the rule files for each target.
  230. cmGeneratorTargetsType targets = this->Makefile->GetGeneratorTargets();
  231. for(cmGeneratorTargetsType::iterator t = targets.begin();
  232. t != targets.end(); ++t)
  233. {
  234. if (t->second->Target->IsImported())
  235. {
  236. continue;
  237. }
  238. t->second->TraceDependencies();
  239. }
  240. }
  241. void cmLocalGenerator::GenerateTestFiles()
  242. {
  243. if ( !this->Makefile->IsOn("CMAKE_TESTING_ENABLED") )
  244. {
  245. return;
  246. }
  247. // Compute the set of configurations.
  248. std::vector<std::string> configurationTypes;
  249. const std::string& config =
  250. this->Makefile->GetConfigurations(configurationTypes, false);
  251. std::string file = this->Makefile->GetStartOutputDirectory();
  252. file += "/";
  253. file += "CTestTestfile.cmake";
  254. cmGeneratedFileStream fout(file.c_str());
  255. fout.SetCopyIfDifferent(true);
  256. fout << "# CMake generated Testfile for " << std::endl
  257. << "# Source directory: "
  258. << this->Makefile->GetStartDirectory() << std::endl
  259. << "# Build directory: "
  260. << this->Makefile->GetStartOutputDirectory() << std::endl
  261. << "# " << std::endl
  262. << "# This file includes the relevant testing commands "
  263. << "required for " << std::endl
  264. << "# testing this directory and lists subdirectories to "
  265. << "be tested as well." << std::endl;
  266. const char* testIncludeFile =
  267. this->Makefile->GetProperty("TEST_INCLUDE_FILE");
  268. if ( testIncludeFile )
  269. {
  270. fout << "include(\"" << testIncludeFile << "\")" << std::endl;
  271. }
  272. // Ask each test generator to write its code.
  273. std::vector<cmTestGenerator*> const&
  274. testers = this->Makefile->GetTestGenerators();
  275. for(std::vector<cmTestGenerator*>::const_iterator gi = testers.begin();
  276. gi != testers.end(); ++gi)
  277. {
  278. (*gi)->Generate(fout, config, configurationTypes);
  279. }
  280. if ( this->Children.size())
  281. {
  282. size_t i;
  283. for(i = 0; i < this->Children.size(); ++i)
  284. {
  285. // TODO: Use add_subdirectory instead?
  286. fout << "subdirs(";
  287. std::string outP =
  288. this->Children[i]->GetMakefile()->GetStartOutputDirectory();
  289. fout << this->Convert(outP.c_str(),START_OUTPUT);
  290. fout << ")" << std::endl;
  291. }
  292. }
  293. }
  294. //----------------------------------------------------------------------------
  295. void cmLocalGenerator::GenerateInstallRules()
  296. {
  297. // Compute the install prefix.
  298. const char* prefix = this->Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  299. #if defined(_WIN32) && !defined(__CYGWIN__)
  300. std::string prefix_win32;
  301. if(!prefix)
  302. {
  303. if(!cmSystemTools::GetEnv("SystemDrive", prefix_win32))
  304. {
  305. prefix_win32 = "C:";
  306. }
  307. const char* project_name = this->Makefile->GetDefinition("PROJECT_NAME");
  308. if(project_name && project_name[0])
  309. {
  310. prefix_win32 += "/Program Files/";
  311. prefix_win32 += project_name;
  312. }
  313. else
  314. {
  315. prefix_win32 += "/InstalledCMakeProject";
  316. }
  317. prefix = prefix_win32.c_str();
  318. }
  319. #elif defined(__HAIKU__)
  320. char dir[B_PATH_NAME_LENGTH];
  321. if (!prefix)
  322. {
  323. if (find_directory(B_SYSTEM_DIRECTORY, -1, false, dir, sizeof(dir))
  324. == B_OK)
  325. {
  326. prefix = dir;
  327. }
  328. else
  329. {
  330. prefix = "/boot/system";
  331. }
  332. }
  333. #else
  334. if (!prefix)
  335. {
  336. prefix = "/usr/local";
  337. }
  338. #endif
  339. if (const char *stagingPrefix
  340. = this->Makefile->GetDefinition("CMAKE_STAGING_PREFIX"))
  341. {
  342. prefix = stagingPrefix;
  343. }
  344. // Compute the set of configurations.
  345. std::vector<std::string> configurationTypes;
  346. const std::string& config =
  347. this->Makefile->GetConfigurations(configurationTypes, false);
  348. // Choose a default install configuration.
  349. const char* default_config = config.c_str();
  350. const char* default_order[] = {"RELEASE", "MINSIZEREL",
  351. "RELWITHDEBINFO", "DEBUG", 0};
  352. for(const char** c = default_order; *c && !default_config; ++c)
  353. {
  354. for(std::vector<std::string>::iterator i = configurationTypes.begin();
  355. i != configurationTypes.end(); ++i)
  356. {
  357. if(cmSystemTools::UpperCase(*i) == *c)
  358. {
  359. default_config = i->c_str();
  360. }
  361. }
  362. }
  363. if(!default_config && !configurationTypes.empty())
  364. {
  365. default_config = configurationTypes[0].c_str();
  366. }
  367. if(!default_config)
  368. {
  369. default_config = "Release";
  370. }
  371. // Create the install script file.
  372. std::string file = this->Makefile->GetStartOutputDirectory();
  373. std::string homedir = this->Makefile->GetHomeOutputDirectory();
  374. std::string currdir = this->Makefile->GetCurrentOutputDirectory();
  375. cmSystemTools::ConvertToUnixSlashes(file);
  376. cmSystemTools::ConvertToUnixSlashes(homedir);
  377. cmSystemTools::ConvertToUnixSlashes(currdir);
  378. int toplevel_install = 0;
  379. if ( currdir == homedir )
  380. {
  381. toplevel_install = 1;
  382. }
  383. file += "/cmake_install.cmake";
  384. cmGeneratedFileStream fout(file.c_str());
  385. fout.SetCopyIfDifferent(true);
  386. // Write the header.
  387. fout << "# Install script for directory: "
  388. << this->Makefile->GetCurrentDirectory() << std::endl << std::endl;
  389. fout << "# Set the install prefix" << std::endl
  390. << "if(NOT DEFINED CMAKE_INSTALL_PREFIX)" << std::endl
  391. << " set(CMAKE_INSTALL_PREFIX \"" << prefix << "\")" << std::endl
  392. << "endif()" << std::endl
  393. << "string(REGEX REPLACE \"/$\" \"\" CMAKE_INSTALL_PREFIX "
  394. << "\"${CMAKE_INSTALL_PREFIX}\")" << std::endl
  395. << std::endl;
  396. // Write support code for generating per-configuration install rules.
  397. fout <<
  398. "# Set the install configuration name.\n"
  399. "if(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)\n"
  400. " if(BUILD_TYPE)\n"
  401. " string(REGEX REPLACE \"^[^A-Za-z0-9_]+\" \"\"\n"
  402. " CMAKE_INSTALL_CONFIG_NAME \"${BUILD_TYPE}\")\n"
  403. " else()\n"
  404. " set(CMAKE_INSTALL_CONFIG_NAME \"" << default_config << "\")\n"
  405. " endif()\n"
  406. " message(STATUS \"Install configuration: "
  407. "\\\"${CMAKE_INSTALL_CONFIG_NAME}\\\"\")\n"
  408. "endif()\n"
  409. "\n";
  410. // Write support code for dealing with component-specific installs.
  411. fout <<
  412. "# Set the component getting installed.\n"
  413. "if(NOT CMAKE_INSTALL_COMPONENT)\n"
  414. " if(COMPONENT)\n"
  415. " message(STATUS \"Install component: \\\"${COMPONENT}\\\"\")\n"
  416. " set(CMAKE_INSTALL_COMPONENT \"${COMPONENT}\")\n"
  417. " else()\n"
  418. " set(CMAKE_INSTALL_COMPONENT)\n"
  419. " endif()\n"
  420. "endif()\n"
  421. "\n";
  422. // Copy user-specified install options to the install code.
  423. if(const char* so_no_exe =
  424. this->Makefile->GetDefinition("CMAKE_INSTALL_SO_NO_EXE"))
  425. {
  426. fout <<
  427. "# Install shared libraries without execute permission?\n"
  428. "if(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)\n"
  429. " set(CMAKE_INSTALL_SO_NO_EXE \"" << so_no_exe << "\")\n"
  430. "endif()\n"
  431. "\n";
  432. }
  433. // Ask each install generator to write its code.
  434. std::vector<cmInstallGenerator*> const& installers =
  435. this->Makefile->GetInstallGenerators();
  436. for(std::vector<cmInstallGenerator*>::const_iterator
  437. gi = installers.begin();
  438. gi != installers.end(); ++gi)
  439. {
  440. (*gi)->Generate(fout, config, configurationTypes);
  441. }
  442. // Write rules from old-style specification stored in targets.
  443. this->GenerateTargetInstallRules(fout, config, configurationTypes);
  444. // Include install scripts from subdirectories.
  445. if(!this->Children.empty())
  446. {
  447. fout << "if(NOT CMAKE_INSTALL_LOCAL_ONLY)\n";
  448. fout << " # Include the install script for each subdirectory.\n";
  449. for(std::vector<cmLocalGenerator*>::const_iterator
  450. ci = this->Children.begin(); ci != this->Children.end(); ++ci)
  451. {
  452. if(!(*ci)->GetMakefile()->GetPropertyAsBool("EXCLUDE_FROM_ALL"))
  453. {
  454. std::string odir = (*ci)->GetMakefile()->GetStartOutputDirectory();
  455. cmSystemTools::ConvertToUnixSlashes(odir);
  456. fout << " include(\"" << odir.c_str()
  457. << "/cmake_install.cmake\")" << std::endl;
  458. }
  459. }
  460. fout << "\n";
  461. fout << "endif()\n\n";
  462. }
  463. // Record the install manifest.
  464. if ( toplevel_install )
  465. {
  466. fout <<
  467. "if(CMAKE_INSTALL_COMPONENT)\n"
  468. " set(CMAKE_INSTALL_MANIFEST \"install_manifest_"
  469. "${CMAKE_INSTALL_COMPONENT}.txt\")\n"
  470. "else()\n"
  471. " set(CMAKE_INSTALL_MANIFEST \"install_manifest.txt\")\n"
  472. "endif()\n\n";
  473. fout
  474. << "file(WRITE \""
  475. << homedir.c_str() << "/${CMAKE_INSTALL_MANIFEST}\" "
  476. << "\"\")" << std::endl;
  477. fout
  478. << "foreach(file ${CMAKE_INSTALL_MANIFEST_FILES})" << std::endl
  479. << " file(APPEND \""
  480. << homedir.c_str() << "/${CMAKE_INSTALL_MANIFEST}\" "
  481. << "\"${file}\\n\")" << std::endl
  482. << "endforeach()" << std::endl;
  483. }
  484. }
  485. //----------------------------------------------------------------------------
  486. void cmLocalGenerator::GenerateTargetManifest()
  487. {
  488. // Collect the set of configuration types.
  489. std::vector<std::string> configNames;
  490. this->Makefile->GetConfigurations(configNames);
  491. // Add our targets to the manifest for each configuration.
  492. cmGeneratorTargetsType targets = this->Makefile->GetGeneratorTargets();
  493. for(cmGeneratorTargetsType::iterator t = targets.begin();
  494. t != targets.end(); ++t)
  495. {
  496. cmGeneratorTarget& target = *t->second;
  497. if (target.Target->GetType() == cmTarget::INTERFACE_LIBRARY)
  498. {
  499. continue;
  500. }
  501. if (target.Target->IsImported())
  502. {
  503. continue;
  504. }
  505. if(configNames.empty())
  506. {
  507. target.GenerateTargetManifest("");
  508. }
  509. else
  510. {
  511. for(std::vector<std::string>::iterator ci = configNames.begin();
  512. ci != configNames.end(); ++ci)
  513. {
  514. const char* config = ci->c_str();
  515. target.GenerateTargetManifest(config);
  516. }
  517. }
  518. }
  519. }
  520. void cmLocalGenerator::AddCustomCommandToCreateObject(const char* ofname,
  521. const std::string& lang,
  522. cmSourceFile& source,
  523. cmGeneratorTarget& target)
  524. {
  525. std::string objectDir = cmSystemTools::GetFilenamePath(std::string(ofname));
  526. objectDir = this->Convert(objectDir.c_str(),START_OUTPUT,SHELL);
  527. std::string objectFile = this->Convert(ofname,START_OUTPUT,SHELL);
  528. std::string sourceFile =
  529. this->Convert(source.GetFullPath().c_str(),START_OUTPUT,SHELL,true);
  530. std::string varString = "CMAKE_";
  531. varString += lang;
  532. varString += "_COMPILE_OBJECT";
  533. std::vector<std::string> rules;
  534. rules.push_back(this->Makefile->GetRequiredDefinition(varString.c_str()));
  535. varString = "CMAKE_";
  536. varString += lang;
  537. varString += "_FLAGS";
  538. std::string flags;
  539. flags += this->Makefile->GetSafeDefinition(varString.c_str());
  540. flags += " ";
  541. {
  542. std::vector<std::string> includes;
  543. this->GetIncludeDirectories(includes, &target, lang);
  544. flags += this->GetIncludeFlags(includes, &target, lang);
  545. }
  546. flags += this->Makefile->GetDefineFlags();
  547. // Construct the command lines.
  548. cmCustomCommandLines commandLines;
  549. std::vector<std::string> commands;
  550. cmSystemTools::ExpandList(rules, commands);
  551. cmLocalGenerator::RuleVariables vars;
  552. vars.Language = lang.c_str();
  553. vars.Source = sourceFile.c_str();
  554. vars.Object = objectFile.c_str();
  555. vars.ObjectDir = objectDir.c_str();
  556. vars.Flags = flags.c_str();
  557. for(std::vector<std::string>::iterator i = commands.begin();
  558. i != commands.end(); ++i)
  559. {
  560. // Expand the full command line string.
  561. this->ExpandRuleVariables(*i, vars);
  562. // Parse the string to get the custom command line.
  563. cmCustomCommandLine commandLine;
  564. std::vector<std::string> cmd = cmSystemTools::ParseArguments(i->c_str());
  565. for(std::vector<std::string>::iterator a = cmd.begin();
  566. a != cmd.end(); ++a)
  567. {
  568. commandLine.push_back(*a);
  569. }
  570. // Store this command line.
  571. commandLines.push_back(commandLine);
  572. }
  573. // Check for extra object-file dependencies.
  574. std::vector<std::string> depends;
  575. const char* additionalDeps = source.GetProperty("OBJECT_DEPENDS");
  576. if(additionalDeps)
  577. {
  578. cmSystemTools::ExpandListArgument(additionalDeps, depends);
  579. }
  580. // Generate a meaningful comment for the command.
  581. std::string comment = "Building ";
  582. comment += lang;
  583. comment += " object ";
  584. comment += this->Convert(ofname, START_OUTPUT);
  585. // Add the custom command to build the object file.
  586. this->Makefile->AddCustomCommandToOutput(
  587. ofname,
  588. depends,
  589. source.GetFullPath().c_str(),
  590. commandLines,
  591. comment.c_str(),
  592. this->Makefile->GetStartOutputDirectory()
  593. );
  594. }
  595. void cmLocalGenerator::AddBuildTargetRule(const std::string& llang,
  596. cmGeneratorTarget& target)
  597. {
  598. std::string objs;
  599. std::vector<std::string> objVector;
  600. // Add all the sources outputs to the depends of the target
  601. std::vector<cmSourceFile*> classes;
  602. target.GetSourceFiles(classes);
  603. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  604. i != classes.end(); ++i)
  605. {
  606. cmSourceFile* sf = *i;
  607. if(!sf->GetCustomCommand() &&
  608. !sf->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  609. !sf->GetPropertyAsBool("EXTERNAL_OBJECT"))
  610. {
  611. std::string dir_max;
  612. dir_max += this->Makefile->GetCurrentOutputDirectory();
  613. dir_max += "/";
  614. std::string obj = this->GetObjectFileNameWithoutTarget(*sf, dir_max);
  615. if(!obj.empty())
  616. {
  617. std::string ofname = this->Makefile->GetCurrentOutputDirectory();
  618. ofname += "/";
  619. ofname += obj;
  620. objVector.push_back(ofname);
  621. this->AddCustomCommandToCreateObject(ofname.c_str(),
  622. llang, *(*i), target);
  623. objs += this->Convert(ofname.c_str(),START_OUTPUT,MAKEFILE);
  624. objs += " ";
  625. }
  626. }
  627. }
  628. std::string createRule = "CMAKE_";
  629. createRule += llang;
  630. createRule += target.GetCreateRuleVariable();
  631. std::string targetName = target.Target->GetFullName();
  632. // Executable :
  633. // Shared Library:
  634. // Static Library:
  635. // Shared Module:
  636. std::string linkLibs; // should be set
  637. std::string frameworkPath;
  638. std::string linkPath;
  639. std::string flags; // should be set
  640. std::string linkFlags; // should be set
  641. this->GetTargetFlags(linkLibs, frameworkPath, linkPath, flags, linkFlags,
  642. &target);
  643. linkLibs = frameworkPath + linkPath + linkLibs;
  644. cmLocalGenerator::RuleVariables vars;
  645. vars.Language = llang.c_str();
  646. vars.Objects = objs.c_str();
  647. vars.ObjectDir = ".";
  648. vars.Target = targetName.c_str();
  649. vars.LinkLibraries = linkLibs.c_str();
  650. vars.Flags = flags.c_str();
  651. vars.LinkFlags = linkFlags.c_str();
  652. std::string langFlags;
  653. this->AddLanguageFlags(langFlags, llang, "");
  654. this->AddArchitectureFlags(langFlags, &target, llang, "");
  655. vars.LanguageCompileFlags = langFlags.c_str();
  656. cmCustomCommandLines commandLines;
  657. std::vector<std::string> rules;
  658. rules.push_back(this->Makefile->GetRequiredDefinition(createRule.c_str()));
  659. std::vector<std::string> commands;
  660. cmSystemTools::ExpandList(rules, commands);
  661. for(std::vector<std::string>::iterator i = commands.begin();
  662. i != commands.end(); ++i)
  663. {
  664. // Expand the full command line string.
  665. this->ExpandRuleVariables(*i, vars);
  666. // Parse the string to get the custom command line.
  667. cmCustomCommandLine commandLine;
  668. std::vector<std::string> cmd = cmSystemTools::ParseArguments(i->c_str());
  669. for(std::vector<std::string>::iterator a = cmd.begin();
  670. a != cmd.end(); ++a)
  671. {
  672. commandLine.push_back(*a);
  673. }
  674. // Store this command line.
  675. commandLines.push_back(commandLine);
  676. }
  677. std::string targetFullPath = target.Target->GetFullPath();
  678. // Generate a meaningful comment for the command.
  679. std::string comment = "Linking ";
  680. comment += llang;
  681. comment += " target ";
  682. comment += this->Convert(targetFullPath.c_str(), START_OUTPUT);
  683. this->Makefile->AddCustomCommandToOutput(
  684. targetFullPath.c_str(),
  685. objVector,
  686. "",
  687. commandLines,
  688. comment.c_str(),
  689. this->Makefile->GetStartOutputDirectory()
  690. );
  691. target.Target->AddSourceFile
  692. (this->Makefile->GetSource(targetFullPath.c_str()));
  693. }
  694. void cmLocalGenerator
  695. ::CreateCustomTargetsAndCommands(std::set<std::string> const& lang)
  696. {
  697. cmGeneratorTargetsType tgts = this->Makefile->GetGeneratorTargets();
  698. for(cmGeneratorTargetsType::iterator l = tgts.begin();
  699. l != tgts.end(); l++)
  700. {
  701. if (l->first->IsImported())
  702. {
  703. continue;
  704. }
  705. cmGeneratorTarget& target = *l->second;
  706. switch(target.GetType())
  707. {
  708. case cmTarget::STATIC_LIBRARY:
  709. case cmTarget::SHARED_LIBRARY:
  710. case cmTarget::MODULE_LIBRARY:
  711. case cmTarget::EXECUTABLE:
  712. {
  713. std::string llang = target.Target->GetLinkerLanguage();
  714. if(llang.empty())
  715. {
  716. cmSystemTools::Error
  717. ("CMake can not determine linker language for target: ",
  718. target.Target->GetName().c_str());
  719. return;
  720. }
  721. // if the language is not in the set lang then create custom
  722. // commands to build the target
  723. if(lang.count(llang) == 0)
  724. {
  725. this->AddBuildTargetRule(llang, target);
  726. }
  727. }
  728. break;
  729. default:
  730. break;
  731. }
  732. }
  733. }
  734. // List of variables that are replaced when
  735. // rules are expanced. These variables are
  736. // replaced in the form <var> with GetSafeDefinition(var).
  737. // ${LANG} is replaced in the variable first with all enabled
  738. // languages.
  739. static const char* ruleReplaceVars[] =
  740. {
  741. "CMAKE_${LANG}_COMPILER",
  742. "CMAKE_SHARED_LIBRARY_CREATE_${LANG}_FLAGS",
  743. "CMAKE_SHARED_MODULE_CREATE_${LANG}_FLAGS",
  744. "CMAKE_SHARED_MODULE_${LANG}_FLAGS",
  745. "CMAKE_SHARED_LIBRARY_${LANG}_FLAGS",
  746. "CMAKE_${LANG}_LINK_FLAGS",
  747. "CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG",
  748. "CMAKE_${LANG}_ARCHIVE",
  749. "CMAKE_AR",
  750. "CMAKE_CURRENT_SOURCE_DIR",
  751. "CMAKE_CURRENT_BINARY_DIR",
  752. "CMAKE_RANLIB",
  753. "CMAKE_LINKER",
  754. "CMAKE_CL_SHOWINCLUDES_PREFIX",
  755. 0
  756. };
  757. std::string
  758. cmLocalGenerator::ExpandRuleVariable(std::string const& variable,
  759. const RuleVariables& replaceValues)
  760. {
  761. if(replaceValues.LinkFlags)
  762. {
  763. if(variable == "LINK_FLAGS")
  764. {
  765. return replaceValues.LinkFlags;
  766. }
  767. }
  768. if(replaceValues.Flags)
  769. {
  770. if(variable == "FLAGS")
  771. {
  772. return replaceValues.Flags;
  773. }
  774. }
  775. if(replaceValues.Source)
  776. {
  777. if(variable == "SOURCE")
  778. {
  779. return replaceValues.Source;
  780. }
  781. }
  782. if(replaceValues.PreprocessedSource)
  783. {
  784. if(variable == "PREPROCESSED_SOURCE")
  785. {
  786. return replaceValues.PreprocessedSource;
  787. }
  788. }
  789. if(replaceValues.AssemblySource)
  790. {
  791. if(variable == "ASSEMBLY_SOURCE")
  792. {
  793. return replaceValues.AssemblySource;
  794. }
  795. }
  796. if(replaceValues.Object)
  797. {
  798. if(variable == "OBJECT")
  799. {
  800. return replaceValues.Object;
  801. }
  802. }
  803. if(replaceValues.ObjectDir)
  804. {
  805. if(variable == "OBJECT_DIR")
  806. {
  807. return replaceValues.ObjectDir;
  808. }
  809. }
  810. if(replaceValues.Objects)
  811. {
  812. if(variable == "OBJECTS")
  813. {
  814. return replaceValues.Objects;
  815. }
  816. }
  817. if(replaceValues.ObjectsQuoted)
  818. {
  819. if(variable == "OBJECTS_QUOTED")
  820. {
  821. return replaceValues.ObjectsQuoted;
  822. }
  823. }
  824. if(replaceValues.Defines && variable == "DEFINES")
  825. {
  826. return replaceValues.Defines;
  827. }
  828. if(replaceValues.TargetPDB )
  829. {
  830. if(variable == "TARGET_PDB")
  831. {
  832. return replaceValues.TargetPDB;
  833. }
  834. }
  835. if(replaceValues.TargetCompilePDB)
  836. {
  837. if(variable == "TARGET_COMPILE_PDB")
  838. {
  839. return replaceValues.TargetCompilePDB;
  840. }
  841. }
  842. if(replaceValues.DependencyFile )
  843. {
  844. if(variable == "DEP_FILE")
  845. {
  846. return replaceValues.DependencyFile;
  847. }
  848. }
  849. if(replaceValues.Target)
  850. {
  851. if(variable == "TARGET_QUOTED")
  852. {
  853. std::string targetQuoted = replaceValues.Target;
  854. if(targetQuoted.size() && targetQuoted[0] != '\"')
  855. {
  856. targetQuoted = '\"';
  857. targetQuoted += replaceValues.Target;
  858. targetQuoted += '\"';
  859. }
  860. return targetQuoted;
  861. }
  862. if(variable == "TARGET_UNQUOTED")
  863. {
  864. std::string unquoted = replaceValues.Target;
  865. std::string::size_type sz = unquoted.size();
  866. if(sz > 2 && unquoted[0] == '\"' && unquoted[sz-1] == '\"')
  867. {
  868. unquoted = unquoted.substr(1, sz-2);
  869. }
  870. return unquoted;
  871. }
  872. if(replaceValues.LanguageCompileFlags)
  873. {
  874. if(variable == "LANGUAGE_COMPILE_FLAGS")
  875. {
  876. return replaceValues.LanguageCompileFlags;
  877. }
  878. }
  879. if(replaceValues.Target)
  880. {
  881. if(variable == "TARGET")
  882. {
  883. return replaceValues.Target;
  884. }
  885. }
  886. if(variable == "TARGET_IMPLIB")
  887. {
  888. return this->TargetImplib;
  889. }
  890. if(variable == "TARGET_VERSION_MAJOR")
  891. {
  892. if(replaceValues.TargetVersionMajor)
  893. {
  894. return replaceValues.TargetVersionMajor;
  895. }
  896. else
  897. {
  898. return "0";
  899. }
  900. }
  901. if(variable == "TARGET_VERSION_MINOR")
  902. {
  903. if(replaceValues.TargetVersionMinor)
  904. {
  905. return replaceValues.TargetVersionMinor;
  906. }
  907. else
  908. {
  909. return "0";
  910. }
  911. }
  912. if(replaceValues.Target)
  913. {
  914. if(variable == "TARGET_BASE")
  915. {
  916. // Strip the last extension off the target name.
  917. std::string targetBase = replaceValues.Target;
  918. std::string::size_type pos = targetBase.rfind(".");
  919. if(pos != targetBase.npos)
  920. {
  921. return targetBase.substr(0, pos);
  922. }
  923. else
  924. {
  925. return targetBase;
  926. }
  927. }
  928. }
  929. }
  930. if(variable == "TARGET_SONAME" || variable == "SONAME_FLAG" ||
  931. variable == "TARGET_INSTALLNAME_DIR")
  932. {
  933. // All these variables depend on TargetSOName
  934. if(replaceValues.TargetSOName)
  935. {
  936. if(variable == "TARGET_SONAME")
  937. {
  938. return replaceValues.TargetSOName;
  939. }
  940. if(variable == "SONAME_FLAG" && replaceValues.SONameFlag)
  941. {
  942. return replaceValues.SONameFlag;
  943. }
  944. if(replaceValues.TargetInstallNameDir &&
  945. variable == "TARGET_INSTALLNAME_DIR")
  946. {
  947. return replaceValues.TargetInstallNameDir;
  948. }
  949. }
  950. return "";
  951. }
  952. if(replaceValues.LinkLibraries)
  953. {
  954. if(variable == "LINK_LIBRARIES")
  955. {
  956. return replaceValues.LinkLibraries;
  957. }
  958. }
  959. if(replaceValues.Language)
  960. {
  961. if(variable == "LANGUAGE")
  962. {
  963. return replaceValues.Language;
  964. }
  965. }
  966. if(replaceValues.CMTarget)
  967. {
  968. if(variable == "TARGET_NAME")
  969. {
  970. return replaceValues.CMTarget->GetName();
  971. }
  972. if(variable == "TARGET_TYPE")
  973. {
  974. return cmTarget::GetTargetTypeName(replaceValues.CMTarget->GetType());
  975. }
  976. }
  977. if(replaceValues.Output)
  978. {
  979. if(variable == "OUTPUT")
  980. {
  981. return replaceValues.Output;
  982. }
  983. }
  984. if(variable == "CMAKE_COMMAND")
  985. {
  986. const char* cmcommand =
  987. this->GetMakefile()->GetDefinition("CMAKE_COMMAND");
  988. return this->Convert(cmcommand, FULL, SHELL);
  989. }
  990. std::vector<std::string> enabledLanguages;
  991. this->GlobalGenerator->GetEnabledLanguages(enabledLanguages);
  992. // loop over language specific replace variables
  993. int pos = 0;
  994. while(ruleReplaceVars[pos])
  995. {
  996. for(std::vector<std::string>::iterator i = enabledLanguages.begin();
  997. i != enabledLanguages.end(); ++i)
  998. {
  999. const char* lang = i->c_str();
  1000. std::string actualReplace = ruleReplaceVars[pos];
  1001. // If this is the compiler then look for the extra variable
  1002. // _COMPILER_ARG1 which must be the first argument to the compiler
  1003. const char* compilerArg1 = 0;
  1004. const char* compilerTarget = 0;
  1005. const char* compilerOptionTarget = 0;
  1006. const char* compilerExternalToolchain = 0;
  1007. const char* compilerOptionExternalToolchain = 0;
  1008. const char* compilerSysroot = 0;
  1009. const char* compilerOptionSysroot = 0;
  1010. if(actualReplace == "CMAKE_${LANG}_COMPILER")
  1011. {
  1012. std::string arg1 = actualReplace + "_ARG1";
  1013. cmSystemTools::ReplaceString(arg1, "${LANG}", lang);
  1014. compilerArg1 = this->Makefile->GetDefinition(arg1.c_str());
  1015. compilerTarget
  1016. = this->Makefile->GetDefinition(
  1017. (std::string("CMAKE_") + lang + "_COMPILER_TARGET").c_str());
  1018. compilerOptionTarget
  1019. = this->Makefile->GetDefinition(
  1020. (std::string("CMAKE_") + lang +
  1021. "_COMPILE_OPTIONS_TARGET").c_str());
  1022. compilerExternalToolchain
  1023. = this->Makefile->GetDefinition(
  1024. (std::string("CMAKE_") + lang +
  1025. "_COMPILER_EXTERNAL_TOOLCHAIN").c_str());
  1026. compilerOptionExternalToolchain
  1027. = this->Makefile->GetDefinition(
  1028. (std::string("CMAKE_") + lang +
  1029. "_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN").c_str());
  1030. compilerSysroot
  1031. = this->Makefile->GetDefinition("CMAKE_SYSROOT");
  1032. compilerOptionSysroot
  1033. = this->Makefile->GetDefinition(
  1034. (std::string("CMAKE_") + lang +
  1035. "_COMPILE_OPTIONS_SYSROOT").c_str());
  1036. }
  1037. if(actualReplace.find("${LANG}") != actualReplace.npos)
  1038. {
  1039. cmSystemTools::ReplaceString(actualReplace, "${LANG}", lang);
  1040. }
  1041. if(actualReplace == variable)
  1042. {
  1043. std::string replace =
  1044. this->Makefile->GetSafeDefinition(variable.c_str());
  1045. // if the variable is not a FLAG then treat it like a path
  1046. if(variable.find("_FLAG") == variable.npos)
  1047. {
  1048. std::string ret = this->ConvertToOutputForExisting(replace.c_str());
  1049. // if there is a required first argument to the compiler add it
  1050. // to the compiler string
  1051. if(compilerArg1)
  1052. {
  1053. ret += " ";
  1054. ret += compilerArg1;
  1055. }
  1056. if (compilerTarget && compilerOptionTarget)
  1057. {
  1058. ret += " ";
  1059. ret += compilerOptionTarget;
  1060. ret += compilerTarget;
  1061. }
  1062. if (compilerExternalToolchain && compilerOptionExternalToolchain)
  1063. {
  1064. ret += " ";
  1065. ret += compilerOptionExternalToolchain;
  1066. ret += this->EscapeForShell(compilerExternalToolchain, true);
  1067. }
  1068. if (compilerSysroot && compilerOptionSysroot)
  1069. {
  1070. ret += " ";
  1071. ret += compilerOptionSysroot;
  1072. ret += this->EscapeForShell(compilerSysroot, true);
  1073. }
  1074. return ret;
  1075. }
  1076. return replace;
  1077. }
  1078. }
  1079. pos++;
  1080. }
  1081. return variable;
  1082. }
  1083. void
  1084. cmLocalGenerator::ExpandRuleVariables(std::string& s,
  1085. const RuleVariables& replaceValues)
  1086. {
  1087. if(replaceValues.RuleLauncher)
  1088. {
  1089. this->InsertRuleLauncher(s, replaceValues.CMTarget,
  1090. replaceValues.RuleLauncher);
  1091. }
  1092. std::string::size_type start = s.find('<');
  1093. // no variables to expand
  1094. if(start == s.npos)
  1095. {
  1096. return;
  1097. }
  1098. std::string::size_type pos = 0;
  1099. std::string expandedInput;
  1100. while(start != s.npos && start < s.size()-2)
  1101. {
  1102. std::string::size_type end = s.find('>', start);
  1103. // if we find a < with no > we are done
  1104. if(end == s.npos)
  1105. {
  1106. return;
  1107. }
  1108. char c = s[start+1];
  1109. // if the next char after the < is not A-Za-z then
  1110. // skip it and try to find the next < in the string
  1111. if(!isalpha(c))
  1112. {
  1113. start = s.find('<', start+1);
  1114. }
  1115. else
  1116. {
  1117. // extract the var
  1118. std::string var = s.substr(start+1, end - start-1);
  1119. std::string replace = this->ExpandRuleVariable(var,
  1120. replaceValues);
  1121. expandedInput += s.substr(pos, start-pos);
  1122. expandedInput += replace;
  1123. // move to next one
  1124. start = s.find('<', start+var.size()+2);
  1125. pos = end+1;
  1126. }
  1127. }
  1128. // add the rest of the input
  1129. expandedInput += s.substr(pos, s.size()-pos);
  1130. s = expandedInput;
  1131. }
  1132. //----------------------------------------------------------------------------
  1133. const char* cmLocalGenerator::GetRuleLauncher(cmTarget* target,
  1134. const std::string& prop)
  1135. {
  1136. if(target)
  1137. {
  1138. return target->GetProperty(prop);
  1139. }
  1140. else
  1141. {
  1142. return this->Makefile->GetProperty(prop);
  1143. }
  1144. }
  1145. //----------------------------------------------------------------------------
  1146. void cmLocalGenerator::InsertRuleLauncher(std::string& s, cmTarget* target,
  1147. const std::string& prop)
  1148. {
  1149. if(const char* val = this->GetRuleLauncher(target, prop))
  1150. {
  1151. cmOStringStream wrapped;
  1152. wrapped << val << " " << s;
  1153. s = wrapped.str();
  1154. }
  1155. }
  1156. //----------------------------------------------------------------------------
  1157. std::string
  1158. cmLocalGenerator::ConvertToOutputForExistingCommon(const std::string& remote,
  1159. std::string const& result,
  1160. OutputFormat format)
  1161. {
  1162. // If this is a windows shell, the result has a space, and the path
  1163. // already exists, we can use a short-path to reference it without a
  1164. // space.
  1165. if(this->WindowsShell && result.find(' ') != result.npos &&
  1166. cmSystemTools::FileExists(remote.c_str()))
  1167. {
  1168. std::string tmp;
  1169. if(cmSystemTools::GetShortPath(remote.c_str(), tmp))
  1170. {
  1171. return this->Convert(tmp.c_str(), NONE, format, true);
  1172. }
  1173. }
  1174. // Otherwise, leave it unchanged.
  1175. return result;
  1176. }
  1177. //----------------------------------------------------------------------------
  1178. std::string
  1179. cmLocalGenerator::ConvertToOutputForExisting(const std::string& remote,
  1180. RelativeRoot local,
  1181. OutputFormat format)
  1182. {
  1183. // Perform standard conversion.
  1184. std::string result = this->Convert(remote, local, format, true);
  1185. // Consider short-path.
  1186. return this->ConvertToOutputForExistingCommon(remote, result, format);
  1187. }
  1188. //----------------------------------------------------------------------------
  1189. std::string
  1190. cmLocalGenerator::ConvertToOutputForExisting(RelativeRoot remote,
  1191. const std::string& local,
  1192. OutputFormat format)
  1193. {
  1194. // Perform standard conversion.
  1195. std::string result = this->Convert(remote, local, format, true);
  1196. // Consider short-path.
  1197. const char* remotePath = this->GetRelativeRootPath(remote);
  1198. return this->ConvertToOutputForExistingCommon(remotePath, result, format);
  1199. }
  1200. //----------------------------------------------------------------------------
  1201. std::string
  1202. cmLocalGenerator::ConvertToIncludeReference(std::string const& path,
  1203. OutputFormat format)
  1204. {
  1205. return this->ConvertToOutputForExisting(path.c_str(), START_OUTPUT, format);
  1206. }
  1207. //----------------------------------------------------------------------------
  1208. std::string cmLocalGenerator::GetIncludeFlags(
  1209. const std::vector<std::string> &includes,
  1210. cmGeneratorTarget* target,
  1211. const std::string& lang,
  1212. bool forResponseFile,
  1213. const std::string& config)
  1214. {
  1215. if(lang.empty())
  1216. {
  1217. return "";
  1218. }
  1219. OutputFormat shellFormat = forResponseFile? RESPONSE : SHELL;
  1220. cmOStringStream includeFlags;
  1221. std::string flagVar = "CMAKE_INCLUDE_FLAG_";
  1222. flagVar += lang;
  1223. const char* includeFlag =
  1224. this->Makefile->GetSafeDefinition(flagVar.c_str());
  1225. flagVar = "CMAKE_INCLUDE_FLAG_SEP_";
  1226. flagVar += lang;
  1227. const char* sep = this->Makefile->GetDefinition(flagVar.c_str());
  1228. bool quotePaths = false;
  1229. if(this->Makefile->GetDefinition("CMAKE_QUOTE_INCLUDE_PATHS"))
  1230. {
  1231. quotePaths = true;
  1232. }
  1233. bool repeatFlag = true;
  1234. // should the include flag be repeated like ie. -IA -IB
  1235. if(!sep)
  1236. {
  1237. sep = " ";
  1238. }
  1239. else
  1240. {
  1241. // if there is a separator then the flag is not repeated but is only
  1242. // given once i.e. -classpath a:b:c
  1243. repeatFlag = false;
  1244. }
  1245. // Support special system include flag if it is available and the
  1246. // normal flag is repeated for each directory.
  1247. std::string sysFlagVar = "CMAKE_INCLUDE_SYSTEM_FLAG_";
  1248. sysFlagVar += lang;
  1249. const char* sysIncludeFlag = 0;
  1250. if(repeatFlag)
  1251. {
  1252. sysIncludeFlag = this->Makefile->GetDefinition(sysFlagVar.c_str());
  1253. }
  1254. std::string fwSearchFlagVar = "CMAKE_";
  1255. fwSearchFlagVar += lang;
  1256. fwSearchFlagVar += "_FRAMEWORK_SEARCH_FLAG";
  1257. const char* fwSearchFlag =
  1258. this->Makefile->GetDefinition(fwSearchFlagVar.c_str());
  1259. bool flagUsed = false;
  1260. std::set<std::string> emitted;
  1261. #ifdef __APPLE__
  1262. emitted.insert("/System/Library/Frameworks");
  1263. #endif
  1264. std::vector<std::string>::const_iterator i;
  1265. for(i = includes.begin(); i != includes.end(); ++i)
  1266. {
  1267. if(fwSearchFlag && *fwSearchFlag && this->Makefile->IsOn("APPLE")
  1268. && cmSystemTools::IsPathToFramework(i->c_str()))
  1269. {
  1270. std::string frameworkDir = *i;
  1271. frameworkDir += "/../";
  1272. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
  1273. if(emitted.insert(frameworkDir).second)
  1274. {
  1275. includeFlags
  1276. << fwSearchFlag << this->Convert(frameworkDir.c_str(),
  1277. START_OUTPUT, shellFormat, true)
  1278. << " ";
  1279. }
  1280. continue;
  1281. }
  1282. if(!flagUsed || repeatFlag)
  1283. {
  1284. if(sysIncludeFlag && target &&
  1285. target->IsSystemIncludeDirectory(*i, config))
  1286. {
  1287. includeFlags << sysIncludeFlag;
  1288. }
  1289. else
  1290. {
  1291. includeFlags << includeFlag;
  1292. }
  1293. flagUsed = true;
  1294. }
  1295. std::string includePath =
  1296. this->ConvertToIncludeReference(*i, shellFormat);
  1297. if(quotePaths && includePath.size() && includePath[0] != '\"')
  1298. {
  1299. includeFlags << "\"";
  1300. }
  1301. includeFlags << includePath;
  1302. if(quotePaths && includePath.size() && includePath[0] != '\"')
  1303. {
  1304. includeFlags << "\"";
  1305. }
  1306. includeFlags << sep;
  1307. }
  1308. std::string flags = includeFlags.str();
  1309. // remove trailing separators
  1310. if((sep[0] != ' ') && flags.size()>0 && flags[flags.size()-1] == sep[0])
  1311. {
  1312. flags[flags.size()-1] = ' ';
  1313. }
  1314. return flags;
  1315. }
  1316. //----------------------------------------------------------------------------
  1317. void cmLocalGenerator::AddCompileDefinitions(std::set<std::string>& defines,
  1318. cmTarget const* target,
  1319. const std::string& config)
  1320. {
  1321. std::vector<std::string> targetDefines;
  1322. target->GetCompileDefinitions(targetDefines,
  1323. config);
  1324. this->AppendDefines(defines, targetDefines);
  1325. }
  1326. //----------------------------------------------------------------------------
  1327. void cmLocalGenerator::AddCompileOptions(
  1328. std::string& flags, cmTarget* target,
  1329. const std::string& lang, const std::string& config
  1330. )
  1331. {
  1332. std::string langFlagRegexVar = std::string("CMAKE_")+lang+"_FLAG_REGEX";
  1333. if(const char* langFlagRegexStr =
  1334. this->Makefile->GetDefinition(langFlagRegexVar.c_str()))
  1335. {
  1336. // Filter flags acceptable to this language.
  1337. cmsys::RegularExpression r(langFlagRegexStr);
  1338. std::vector<std::string> opts;
  1339. if(const char* targetFlags = target->GetProperty("COMPILE_FLAGS"))
  1340. {
  1341. cmSystemTools::ParseWindowsCommandLine(targetFlags, opts);
  1342. }
  1343. target->GetCompileOptions(opts, config);
  1344. for(std::vector<std::string>::const_iterator i = opts.begin();
  1345. i != opts.end(); ++i)
  1346. {
  1347. if(r.find(i->c_str()))
  1348. {
  1349. // (Re-)Escape this flag. COMPILE_FLAGS were already parsed
  1350. // as a command line above, and COMPILE_OPTIONS are escaped.
  1351. this->AppendFlagEscape(flags, i->c_str());
  1352. }
  1353. }
  1354. }
  1355. else
  1356. {
  1357. // Use all flags.
  1358. if(const char* targetFlags = target->GetProperty("COMPILE_FLAGS"))
  1359. {
  1360. // COMPILE_FLAGS are not escaped for historical reasons.
  1361. this->AppendFlags(flags, targetFlags);
  1362. }
  1363. std::vector<std::string> opts;
  1364. target->GetCompileOptions(opts, config);
  1365. for(std::vector<std::string>::const_iterator i = opts.begin();
  1366. i != opts.end(); ++i)
  1367. {
  1368. // COMPILE_OPTIONS are escaped.
  1369. this->AppendFlagEscape(flags, i->c_str());
  1370. }
  1371. }
  1372. }
  1373. //----------------------------------------------------------------------------
  1374. void cmLocalGenerator::GetIncludeDirectories(std::vector<std::string>& dirs,
  1375. cmGeneratorTarget* target,
  1376. const std::string& lang,
  1377. const std::string& config,
  1378. bool stripImplicitInclDirs
  1379. )
  1380. {
  1381. // Need to decide whether to automatically include the source and
  1382. // binary directories at the beginning of the include path.
  1383. bool includeSourceDir = false;
  1384. bool includeBinaryDir = false;
  1385. // When automatic include directories are requested for a build then
  1386. // include the source and binary directories at the beginning of the
  1387. // include path to approximate include file behavior for an
  1388. // in-source build. This does not account for the case of a source
  1389. // file in a subdirectory of the current source directory but we
  1390. // cannot fix this because not all native build tools support
  1391. // per-source-file include paths.
  1392. if(this->Makefile->IsOn("CMAKE_INCLUDE_CURRENT_DIR"))
  1393. {
  1394. includeSourceDir = true;
  1395. includeBinaryDir = true;
  1396. }
  1397. // Do not repeat an include path.
  1398. std::set<std::string> emitted;
  1399. // Store the automatic include paths.
  1400. if(includeBinaryDir)
  1401. {
  1402. if(emitted.find(
  1403. this->Makefile->GetStartOutputDirectory()) == emitted.end())
  1404. {
  1405. dirs.push_back(this->Makefile->GetStartOutputDirectory());
  1406. emitted.insert(this->Makefile->GetStartOutputDirectory());
  1407. }
  1408. }
  1409. if(includeSourceDir)
  1410. {
  1411. if(emitted.find(this->Makefile->GetStartDirectory()) == emitted.end())
  1412. {
  1413. dirs.push_back(this->Makefile->GetStartDirectory());
  1414. emitted.insert(this->Makefile->GetStartDirectory());
  1415. }
  1416. }
  1417. if(!target)
  1418. {
  1419. return;
  1420. }
  1421. std::string rootPath = this->Makefile->GetSafeDefinition("CMAKE_SYSROOT");
  1422. std::vector<std::string> implicitDirs;
  1423. // Load implicit include directories for this language.
  1424. std::string impDirVar = "CMAKE_";
  1425. impDirVar += lang;
  1426. impDirVar += "_IMPLICIT_INCLUDE_DIRECTORIES";
  1427. if(const char* value = this->Makefile->GetDefinition(impDirVar.c_str()))
  1428. {
  1429. std::vector<std::string> impDirVec;
  1430. cmSystemTools::ExpandListArgument(value, impDirVec);
  1431. for(std::vector<std::string>::const_iterator i = impDirVec.begin();
  1432. i != impDirVec.end(); ++i)
  1433. {
  1434. std::string d = rootPath + *i;
  1435. cmSystemTools::ConvertToUnixSlashes(d);
  1436. emitted.insert(d);
  1437. if (!stripImplicitInclDirs)
  1438. {
  1439. implicitDirs.push_back(*i);
  1440. }
  1441. }
  1442. }
  1443. // Get the target-specific include directories.
  1444. std::vector<std::string> includes;
  1445. includes = target->GetIncludeDirectories(config);
  1446. // Support putting all the in-project include directories first if
  1447. // it is requested by the project.
  1448. if(this->Makefile->IsOn("CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE"))
  1449. {
  1450. const char* topSourceDir = this->Makefile->GetHomeDirectory();
  1451. const char* topBinaryDir = this->Makefile->GetHomeOutputDirectory();
  1452. for(std::vector<std::string>::const_iterator i = includes.begin();
  1453. i != includes.end(); ++i)
  1454. {
  1455. // Emit this directory only if it is a subdirectory of the
  1456. // top-level source or binary tree.
  1457. if(cmSystemTools::ComparePath(i->c_str(), topSourceDir) ||
  1458. cmSystemTools::ComparePath(i->c_str(), topBinaryDir) ||
  1459. cmSystemTools::IsSubDirectory(i->c_str(), topSourceDir) ||
  1460. cmSystemTools::IsSubDirectory(i->c_str(), topBinaryDir))
  1461. {
  1462. if(emitted.insert(*i).second)
  1463. {
  1464. dirs.push_back(*i);
  1465. }
  1466. }
  1467. }
  1468. }
  1469. // Construct the final ordered include directory list.
  1470. for(std::vector<std::string>::const_iterator i = includes.begin();
  1471. i != includes.end(); ++i)
  1472. {
  1473. if(emitted.insert(*i).second)
  1474. {
  1475. dirs.push_back(*i);
  1476. }
  1477. }
  1478. for(std::vector<std::string>::const_iterator i = implicitDirs.begin();
  1479. i != implicitDirs.end(); ++i)
  1480. {
  1481. if(std::find(includes.begin(), includes.end(), *i) != includes.end())
  1482. {
  1483. dirs.push_back(*i);
  1484. }
  1485. }
  1486. }
  1487. void cmLocalGenerator::GetStaticLibraryFlags(std::string& flags,
  1488. std::string const& config,
  1489. cmTarget* target)
  1490. {
  1491. this->AppendFlags(flags,
  1492. this->Makefile->GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS"));
  1493. if(!config.empty())
  1494. {
  1495. std::string name = "CMAKE_STATIC_LINKER_FLAGS_" + config;
  1496. this->AppendFlags(flags, this->Makefile->GetSafeDefinition(name.c_str()));
  1497. }
  1498. this->AppendFlags(flags, target->GetProperty("STATIC_LIBRARY_FLAGS"));
  1499. if(!config.empty())
  1500. {
  1501. std::string name = "STATIC_LIBRARY_FLAGS_" + config;
  1502. this->AppendFlags(flags, target->GetProperty(name.c_str()));
  1503. }
  1504. }
  1505. void cmLocalGenerator::GetTargetFlags(std::string& linkLibs,
  1506. std::string& flags,
  1507. std::string& linkFlags,
  1508. std::string& frameworkPath,
  1509. std::string& linkPath,
  1510. cmGeneratorTarget* target)
  1511. {
  1512. std::string buildType =
  1513. this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1514. buildType = cmSystemTools::UpperCase(buildType);
  1515. const char* libraryLinkVariable =
  1516. "CMAKE_SHARED_LINKER_FLAGS"; // default to shared library
  1517. switch(target->GetType())
  1518. {
  1519. case cmTarget::STATIC_LIBRARY:
  1520. this->GetStaticLibraryFlags(linkFlags, buildType, target->Target);
  1521. break;
  1522. case cmTarget::MODULE_LIBRARY:
  1523. libraryLinkVariable = "CMAKE_MODULE_LINKER_FLAGS";
  1524. case cmTarget::SHARED_LIBRARY:
  1525. {
  1526. linkFlags = this->Makefile->GetSafeDefinition(libraryLinkVariable);
  1527. linkFlags += " ";
  1528. if(!buildType.empty())
  1529. {
  1530. std::string build = libraryLinkVariable;
  1531. build += "_";
  1532. build += buildType;
  1533. linkFlags += this->Makefile->GetSafeDefinition(build.c_str());
  1534. linkFlags += " ";
  1535. }
  1536. if(this->Makefile->IsOn("WIN32") &&
  1537. !(this->Makefile->IsOn("CYGWIN") || this->Makefile->IsOn("MINGW")))
  1538. {
  1539. std::vector<cmSourceFile*> sources;
  1540. target->GetSourceFiles(sources);
  1541. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1542. i != sources.end(); ++i)
  1543. {
  1544. cmSourceFile* sf = *i;
  1545. if(sf->GetExtension() == "def")
  1546. {
  1547. linkFlags +=
  1548. this->Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  1549. linkFlags += this->Convert(sf->GetFullPath().c_str(),
  1550. FULL, SHELL);
  1551. linkFlags += " ";
  1552. }
  1553. }
  1554. }
  1555. const char* targetLinkFlags = target->GetProperty("LINK_FLAGS");
  1556. if(targetLinkFlags)
  1557. {
  1558. linkFlags += targetLinkFlags;
  1559. linkFlags += " ";
  1560. }
  1561. if(!buildType.empty())
  1562. {
  1563. std::string configLinkFlags = "LINK_FLAGS_";
  1564. configLinkFlags += buildType;
  1565. targetLinkFlags = target->GetProperty(configLinkFlags.c_str());
  1566. if(targetLinkFlags)
  1567. {
  1568. linkFlags += targetLinkFlags;
  1569. linkFlags += " ";
  1570. }
  1571. }
  1572. this->OutputLinkLibraries(linkLibs, frameworkPath, linkPath,
  1573. *target, false, false);
  1574. }
  1575. break;
  1576. case cmTarget::EXECUTABLE:
  1577. {
  1578. linkFlags +=
  1579. this->Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  1580. linkFlags += " ";
  1581. if(!buildType.empty())
  1582. {
  1583. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  1584. build += buildType;
  1585. linkFlags += this->Makefile->GetSafeDefinition(build.c_str());
  1586. linkFlags += " ";
  1587. }
  1588. std::string linkLanguage = target->Target->GetLinkerLanguage();
  1589. if(linkLanguage.empty())
  1590. {
  1591. cmSystemTools::Error
  1592. ("CMake can not determine linker language for target: ",
  1593. target->Target->GetName().c_str());
  1594. return;
  1595. }
  1596. this->AddLanguageFlags(flags, linkLanguage, buildType);
  1597. this->OutputLinkLibraries(linkLibs, frameworkPath, linkPath,
  1598. *target, false, false);
  1599. if(cmSystemTools::IsOn
  1600. (this->Makefile->GetDefinition("BUILD_SHARED_LIBS")))
  1601. {
  1602. std::string sFlagVar = std::string("CMAKE_SHARED_BUILD_")
  1603. + linkLanguage + std::string("_FLAGS");
  1604. linkFlags += this->Makefile->GetSafeDefinition(sFlagVar.c_str());
  1605. linkFlags += " ";
  1606. }
  1607. if ( target->GetPropertyAsBool("WIN32_EXECUTABLE") )
  1608. {
  1609. linkFlags +=
  1610. this->Makefile->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  1611. linkFlags += " ";
  1612. }
  1613. else
  1614. {
  1615. linkFlags +=
  1616. this->Makefile->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  1617. linkFlags += " ";
  1618. }
  1619. if (target->Target->IsExecutableWithExports())
  1620. {
  1621. std::string exportFlagVar = "CMAKE_EXE_EXPORTS_";
  1622. exportFlagVar += linkLanguage;
  1623. exportFlagVar += "_FLAG";
  1624. linkFlags +=
  1625. this->Makefile->GetSafeDefinition(exportFlagVar.c_str());
  1626. linkFlags += " ";
  1627. }
  1628. const char* targetLinkFlags = target->GetProperty("LINK_FLAGS");
  1629. if(targetLinkFlags)
  1630. {
  1631. linkFlags += targetLinkFlags;
  1632. linkFlags += " ";
  1633. }
  1634. if(!buildType.empty())
  1635. {
  1636. std::string configLinkFlags = "LINK_FLAGS_";
  1637. configLinkFlags += buildType;
  1638. targetLinkFlags = target->GetProperty(configLinkFlags.c_str());
  1639. if(targetLinkFlags)
  1640. {
  1641. linkFlags += targetLinkFlags;
  1642. linkFlags += " ";
  1643. }
  1644. }
  1645. }
  1646. break;
  1647. default:
  1648. break;
  1649. }
  1650. }
  1651. std::string cmLocalGenerator::ConvertToLinkReference(std::string const& lib,
  1652. OutputFormat format)
  1653. {
  1654. #if defined(_WIN32) && !defined(__CYGWIN__)
  1655. // Work-ardound command line parsing limitations in MSVC 6.0 and
  1656. // Watcom.
  1657. if(this->Makefile->IsOn("MSVC60") || this->Makefile->IsOn("WATCOM"))
  1658. {
  1659. // Search for the last space.
  1660. std::string::size_type pos = lib.rfind(' ');
  1661. if(pos != lib.npos)
  1662. {
  1663. // Find the slash after the last space, if any.
  1664. pos = lib.find('/', pos);
  1665. // Convert the portion of the path with a space to a short path.
  1666. std::string sp;
  1667. if(cmSystemTools::GetShortPath(lib.substr(0, pos).c_str(), sp))
  1668. {
  1669. // Append the rest of the path with no space.
  1670. sp += lib.substr(pos);
  1671. // Convert to an output path.
  1672. return this->Convert(sp.c_str(), NONE, format);
  1673. }
  1674. }
  1675. }
  1676. #endif
  1677. // Normal behavior.
  1678. return this->Convert(lib.c_str(), START_OUTPUT, format);
  1679. }
  1680. /**
  1681. * Output the linking rules on a command line. For executables,
  1682. * targetLibrary should be a NULL pointer. For libraries, it should point
  1683. * to the name of the library. This will not link a library against itself.
  1684. */
  1685. void cmLocalGenerator::OutputLinkLibraries(std::string& linkLibraries,
  1686. std::string& frameworkPath,
  1687. std::string& linkPath,
  1688. cmGeneratorTarget &tgt,
  1689. bool relink,
  1690. bool forResponseFile)
  1691. {
  1692. OutputFormat shellFormat = forResponseFile? RESPONSE : SHELL;
  1693. bool escapeAllowMakeVars = !forResponseFile;
  1694. cmOStringStream fout;
  1695. const char* config = this->Makefile->GetDefinition("CMAKE_BUILD_TYPE");
  1696. cmComputeLinkInformation* pcli = tgt.Target->GetLinkInformation(config);
  1697. if(!pcli)
  1698. {
  1699. return;
  1700. }
  1701. cmComputeLinkInformation& cli = *pcli;
  1702. // Collect library linking flags command line options.
  1703. std::string linkLibs;
  1704. std::string linkLanguage = cli.GetLinkLanguage();
  1705. std::string libPathFlag =
  1706. this->Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
  1707. std::string libPathTerminator =
  1708. this->Makefile->GetSafeDefinition("CMAKE_LIBRARY_PATH_TERMINATOR");
  1709. // Flags to link an executable to shared libraries.
  1710. std::string linkFlagsVar = "CMAKE_SHARED_LIBRARY_LINK_";
  1711. linkFlagsVar += linkLanguage;
  1712. linkFlagsVar += "_FLAGS";
  1713. if( tgt.GetType() == cmTarget::EXECUTABLE )
  1714. {
  1715. linkLibs = this->Makefile->GetSafeDefinition(linkFlagsVar.c_str());
  1716. linkLibs += " ";
  1717. }
  1718. // Append the framework search path flags.
  1719. std::string fwSearchFlagVar = "CMAKE_";
  1720. fwSearchFlagVar += linkLanguage;
  1721. fwSearchFlagVar += "_FRAMEWORK_SEARCH_FLAG";
  1722. const char* fwSearchFlag =
  1723. this->Makefile->GetDefinition(fwSearchFlagVar.c_str());
  1724. if(fwSearchFlag && *fwSearchFlag)
  1725. {
  1726. std::vector<std::string> const& fwDirs = cli.GetFrameworkPaths();
  1727. for(std::vector<std::string>::const_iterator fdi = fwDirs.begin();
  1728. fdi != fwDirs.end(); ++fdi)
  1729. {
  1730. frameworkPath += fwSearchFlag;
  1731. frameworkPath += this->Convert(fdi->c_str(), NONE, shellFormat, false);
  1732. frameworkPath += " ";
  1733. }
  1734. }
  1735. // Append the library search path flags.
  1736. std::vector<std::string> const& libDirs = cli.GetDirectories();
  1737. for(std::vector<std::string>::const_iterator libDir = libDirs.begin();
  1738. libDir != libDirs.end(); ++libDir)
  1739. {
  1740. std::string libpath = this->ConvertToOutputForExisting(libDir->c_str(),
  1741. START_OUTPUT,
  1742. shellFormat);
  1743. linkPath += " " + libPathFlag;
  1744. linkPath += libpath;
  1745. linkPath += libPathTerminator;
  1746. linkPath += " ";
  1747. }
  1748. // Append the link items.
  1749. typedef cmComputeLinkInformation::ItemVector ItemVector;
  1750. ItemVector const& items = cli.GetItems();
  1751. for(ItemVector::const_iterator li = items.begin(); li != items.end(); ++li)
  1752. {
  1753. if(li->Target && li->Target->GetType() == cmTarget::INTERFACE_LIBRARY)
  1754. {
  1755. continue;
  1756. }
  1757. if(li->IsPath)
  1758. {
  1759. linkLibs += this->ConvertToLinkReference(li->Value, shellFormat);
  1760. }
  1761. else
  1762. {
  1763. linkLibs += li->Value;
  1764. }
  1765. linkLibs += " ";
  1766. }
  1767. // Write the library flags to the build rule.
  1768. fout << linkLibs;
  1769. // Get the RPATH entries.
  1770. std::vector<std::string> runtimeDirs;
  1771. cli.GetRPath(runtimeDirs, relink);
  1772. // Check what kind of rpath flags to use.
  1773. if(cli.GetRuntimeSep().empty())
  1774. {
  1775. // Each rpath entry gets its own option ("-R a -R b -R c")
  1776. std::string rpath;
  1777. for(std::vector<std::string>::iterator ri = runtimeDirs.begin();
  1778. ri != runtimeDirs.end(); ++ri)
  1779. {
  1780. rpath += cli.GetRuntimeFlag();
  1781. rpath += this->Convert(ri->c_str(), NONE, shellFormat, false);
  1782. rpath += " ";
  1783. }
  1784. fout << rpath;
  1785. }
  1786. else
  1787. {
  1788. // All rpath entries are combined ("-Wl,-rpath,a:b:c").
  1789. std::string rpath = cli.GetRPathString(relink);
  1790. // Store the rpath option in the stream.
  1791. if(!rpath.empty())
  1792. {
  1793. fout << cli.GetRuntimeFlag();
  1794. fout << this->EscapeForShell(rpath.c_str(), escapeAllowMakeVars);
  1795. fout << " ";
  1796. }
  1797. }
  1798. // Add the linker runtime search path if any.
  1799. std::string rpath_link = cli.GetRPathLinkString();
  1800. if(!cli.GetRPathLinkFlag().empty() && !rpath_link.empty())
  1801. {
  1802. fout << cli.GetRPathLinkFlag();
  1803. fout << this->EscapeForShell(rpath_link.c_str(), escapeAllowMakeVars);
  1804. fout << " ";
  1805. }
  1806. // Add standard libraries for this language.
  1807. std::string standardLibsVar = "CMAKE_";
  1808. standardLibsVar += cli.GetLinkLanguage();
  1809. standardLibsVar += "_STANDARD_LIBRARIES";
  1810. if(const char* stdLibs =
  1811. this->Makefile->GetDefinition(standardLibsVar.c_str()))
  1812. {
  1813. fout << stdLibs << " ";
  1814. }
  1815. linkLibraries = fout.str();
  1816. }
  1817. //----------------------------------------------------------------------------
  1818. void cmLocalGenerator::AddArchitectureFlags(std::string& flags,
  1819. cmGeneratorTarget* target,
  1820. const std::string& lang,
  1821. const std::string& config)
  1822. {
  1823. // Only add Mac OS X specific flags on Darwin platforms (OSX and iphone):
  1824. if(!this->Makefile->IsOn("APPLE"))
  1825. {
  1826. return;
  1827. }
  1828. if(this->EmitUniversalBinaryFlags)
  1829. {
  1830. std::vector<std::string> archs;
  1831. target->GetAppleArchs(config, archs);
  1832. const char* sysroot = this->Makefile->GetDefinition("CMAKE_OSX_SYSROOT");
  1833. if(sysroot && sysroot[0] == '/' && !sysroot[1])
  1834. { sysroot = 0; }
  1835. std::string sysrootFlagVar =
  1836. std::string("CMAKE_") + lang + "_SYSROOT_FLAG";
  1837. const char* sysrootFlag =
  1838. this->Makefile->GetDefinition(sysrootFlagVar.c_str());
  1839. const char* deploymentTarget =
  1840. this->Makefile->GetDefinition("CMAKE_OSX_DEPLOYMENT_TARGET");
  1841. std::string deploymentTargetFlagVar =
  1842. std::string("CMAKE_") + lang + "_OSX_DEPLOYMENT_TARGET_FLAG";
  1843. const char* deploymentTargetFlag =
  1844. this->Makefile->GetDefinition(deploymentTargetFlagVar.c_str());
  1845. if(!archs.empty() && !lang.empty() && (lang[0] =='C' || lang[0] == 'F'))
  1846. {
  1847. for(std::vector<std::string>::iterator i = archs.begin();
  1848. i != archs.end(); ++i)
  1849. {
  1850. flags += " -arch ";
  1851. flags += *i;
  1852. }
  1853. }
  1854. if(sysrootFlag && *sysrootFlag && sysroot && *sysroot)
  1855. {
  1856. flags += " ";
  1857. flags += sysrootFlag;
  1858. flags += " ";
  1859. flags += this->Convert(sysroot, NONE, SHELL);
  1860. }
  1861. if (deploymentTargetFlag && *deploymentTargetFlag &&
  1862. deploymentTarget && *deploymentTarget)
  1863. {
  1864. flags += " ";
  1865. flags += deploymentTargetFlag;
  1866. flags += deploymentTarget;
  1867. }
  1868. }
  1869. }
  1870. //----------------------------------------------------------------------------
  1871. void cmLocalGenerator::AddLanguageFlags(std::string& flags,
  1872. const std::string& lang,
  1873. const std::string& config)
  1874. {
  1875. // Add language-specific flags.
  1876. std::string flagsVar = "CMAKE_";
  1877. flagsVar += lang;
  1878. flagsVar += "_FLAGS";
  1879. this->AddConfigVariableFlags(flags, flagsVar.c_str(), config);
  1880. }
  1881. //----------------------------------------------------------------------------
  1882. bool cmLocalGenerator::GetRealDependency(const std::string& inName,
  1883. const std::string& config,
  1884. std::string& dep)
  1885. {
  1886. // Older CMake code may specify the dependency using the target
  1887. // output file rather than the target name. Such code would have
  1888. // been written before there was support for target properties that
  1889. // modify the name so stripping down to just the file name should
  1890. // produce the target name in this case.
  1891. std::string name = cmSystemTools::GetFilenameName(inName);
  1892. // If the input name is the empty string, there is no real
  1893. // dependency. Short-circuit the other checks:
  1894. if(name == "")
  1895. {
  1896. return false;
  1897. }
  1898. if(cmSystemTools::GetFilenameLastExtension(name) == ".exe")
  1899. {
  1900. name = cmSystemTools::GetFilenameWithoutLastExtension(name);
  1901. }
  1902. // Look for a CMake target with the given name.
  1903. if(cmTarget* target = this->Makefile->FindTargetToUse(name))
  1904. {
  1905. // make sure it is not just a coincidence that the target name
  1906. // found is part of the inName
  1907. if(cmSystemTools::FileIsFullPath(inName.c_str()))
  1908. {
  1909. std::string tLocation;
  1910. if(target->GetType() >= cmTarget::EXECUTABLE &&
  1911. target->GetType() <= cmTarget::MODULE_LIBRARY)
  1912. {
  1913. tLocation = target->GetLocation(config);
  1914. tLocation = cmSystemTools::GetFilenamePath(tLocation);
  1915. tLocation = cmSystemTools::CollapseFullPath(tLocation.c_str());
  1916. }
  1917. std::string depLocation = cmSystemTools::GetFilenamePath(
  1918. std::string(inName));
  1919. depLocation = cmSystemTools::CollapseFullPath(depLocation.c_str());
  1920. if(depLocation != tLocation)
  1921. {
  1922. // it is a full path to a depend that has the same name
  1923. // as a target but is in a different location so do not use
  1924. // the target as the depend
  1925. dep = inName;
  1926. return true;
  1927. }
  1928. }
  1929. switch (target->GetType())
  1930. {
  1931. case cmTarget::EXECUTABLE:
  1932. case cmTarget::STATIC_LIBRARY:
  1933. case cmTarget::SHARED_LIBRARY:
  1934. case cmTarget::MODULE_LIBRARY:
  1935. case cmTarget::UNKNOWN_LIBRARY:
  1936. dep = target->GetLocation(config);
  1937. return true;
  1938. case cmTarget::OBJECT_LIBRARY:
  1939. // An object library has no single file on which to depend.
  1940. // This was listed to get the target-level dependency.
  1941. return false;
  1942. case cmTarget::INTERFACE_LIBRARY:
  1943. // An interface library has no file on which to depend.
  1944. // This was listed to get the target-level dependency.
  1945. return false;
  1946. case cmTarget::UTILITY:
  1947. case cmTarget::GLOBAL_TARGET:
  1948. // A utility target has no file on which to depend. This was listed
  1949. // only to get the target-level dependency.
  1950. return false;
  1951. }
  1952. }
  1953. // The name was not that of a CMake target. It must name a file.
  1954. if(cmSystemTools::FileIsFullPath(inName.c_str()))
  1955. {
  1956. // This is a full path. Return it as given.
  1957. dep = inName;
  1958. return true;
  1959. }
  1960. // Check for a source file in this directory that matches the
  1961. // dependency.
  1962. if(cmSourceFile* sf = this->Makefile->GetSource(inName))
  1963. {
  1964. dep = sf->GetFullPath();
  1965. return true;
  1966. }
  1967. // Treat the name as relative to the source directory in which it
  1968. // was given.
  1969. dep = this->Makefile->GetCurrentDirectory();
  1970. dep += "/";
  1971. dep += inName;
  1972. return true;
  1973. }
  1974. //----------------------------------------------------------------------------
  1975. void cmLocalGenerator::AddSharedFlags(std::string& flags,
  1976. const std::string& lang,
  1977. bool shared)
  1978. {
  1979. std::string flagsVar;
  1980. // Add flags for dealing with shared libraries for this language.
  1981. if(shared)
  1982. {
  1983. flagsVar = "CMAKE_SHARED_LIBRARY_";
  1984. flagsVar += lang;
  1985. flagsVar += "_FLAGS";
  1986. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  1987. }
  1988. }
  1989. static void AddVisibilityCompileOption(std::string &flags, cmTarget* target,
  1990. cmLocalGenerator *lg,
  1991. const std::string& lang)
  1992. {
  1993. std::string l(lang);
  1994. std::string compileOption = "CMAKE_" + l + "_COMPILE_OPTIONS_VISIBILITY";
  1995. const char *opt = lg->GetMakefile()->GetDefinition(compileOption.c_str());
  1996. if (!opt)
  1997. {
  1998. return;
  1999. }
  2000. std::string flagDefine = l + "_VISIBILITY_PRESET";
  2001. const char *prop = target->GetProperty(flagDefine.c_str());
  2002. if (!prop)
  2003. {
  2004. return;
  2005. }
  2006. if (strcmp(prop, "hidden") != 0
  2007. && strcmp(prop, "default") != 0
  2008. && strcmp(prop, "protected") != 0
  2009. && strcmp(prop, "internal") != 0 )
  2010. {
  2011. cmOStringStream e;
  2012. e << "Target " << target->GetName() << " uses unsupported value \""
  2013. << prop << "\" for " << flagDefine << ".";
  2014. cmSystemTools::Error(e.str().c_str());
  2015. return;
  2016. }
  2017. std::string option = std::string(opt) + prop;
  2018. lg->AppendFlags(flags, option.c_str());
  2019. }
  2020. static void AddInlineVisibilityCompileOption(std::string &flags,
  2021. cmTarget* target,
  2022. cmLocalGenerator *lg)
  2023. {
  2024. std::string compileOption
  2025. = "CMAKE_CXX_COMPILE_OPTIONS_VISIBILITY_INLINES_HIDDEN";
  2026. const char *opt = lg->GetMakefile()->GetDefinition(compileOption.c_str());
  2027. if (!opt)
  2028. {
  2029. return;
  2030. }
  2031. bool prop = target->GetPropertyAsBool("VISIBILITY_INLINES_HIDDEN");
  2032. if (!prop)
  2033. {
  2034. return;
  2035. }
  2036. lg->AppendFlags(flags, opt);
  2037. }
  2038. //----------------------------------------------------------------------------
  2039. void cmLocalGenerator
  2040. ::AddVisibilityPresetFlags(std::string &flags, cmTarget* target,
  2041. const std::string& lang)
  2042. {
  2043. int targetType = target->GetType();
  2044. bool suitableTarget = ((targetType == cmTarget::SHARED_LIBRARY)
  2045. || (targetType == cmTarget::MODULE_LIBRARY)
  2046. || (target->IsExecutableWithExports()));
  2047. if (!suitableTarget)
  2048. {
  2049. return;
  2050. }
  2051. if (lang.empty())
  2052. {
  2053. return;
  2054. }
  2055. AddVisibilityCompileOption(flags, target, this, lang);
  2056. if(lang == "CXX")
  2057. {
  2058. AddInlineVisibilityCompileOption(flags, target, this);
  2059. }
  2060. }
  2061. //----------------------------------------------------------------------------
  2062. void cmLocalGenerator::AddCMP0018Flags(std::string &flags, cmTarget* target,
  2063. std::string const& lang,
  2064. const std::string& config)
  2065. {
  2066. int targetType = target->GetType();
  2067. bool shared = ((targetType == cmTarget::SHARED_LIBRARY) ||
  2068. (targetType == cmTarget::MODULE_LIBRARY));
  2069. if (this->GetShouldUseOldFlags(shared, lang))
  2070. {
  2071. this->AddSharedFlags(flags, lang.c_str(), shared);
  2072. }
  2073. else
  2074. {
  2075. if (target->GetType() == cmTarget::OBJECT_LIBRARY)
  2076. {
  2077. if (target->GetPropertyAsBool("POSITION_INDEPENDENT_CODE"))
  2078. {
  2079. this->AddPositionIndependentFlags(flags, lang, targetType);
  2080. }
  2081. return;
  2082. }
  2083. if (target->GetLinkInterfaceDependentBoolProperty(
  2084. "POSITION_INDEPENDENT_CODE",
  2085. config))
  2086. {
  2087. this->AddPositionIndependentFlags(flags, lang, targetType);
  2088. }
  2089. if (shared)
  2090. {
  2091. this->AppendFeatureOptions(flags, lang.c_str(), "DLL");
  2092. }
  2093. }
  2094. }
  2095. //----------------------------------------------------------------------------
  2096. bool cmLocalGenerator::GetShouldUseOldFlags(bool shared,
  2097. const std::string &lang) const
  2098. {
  2099. std::string originalFlags =
  2100. this->GlobalGenerator->GetSharedLibFlagsForLanguage(lang);
  2101. if (shared)
  2102. {
  2103. std::string flagsVar = "CMAKE_SHARED_LIBRARY_";
  2104. flagsVar += lang;
  2105. flagsVar += "_FLAGS";
  2106. const char* flags =
  2107. this->Makefile->GetSafeDefinition(flagsVar.c_str());
  2108. if (flags && flags != originalFlags)
  2109. {
  2110. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0018))
  2111. {
  2112. case cmPolicies::WARN:
  2113. {
  2114. cmOStringStream e;
  2115. e << "Variable " << flagsVar << " has been modified. CMake "
  2116. "will ignore the POSITION_INDEPENDENT_CODE target property for "
  2117. "shared libraries and will use the " << flagsVar << " variable "
  2118. "instead. This may cause errors if the original content of "
  2119. << flagsVar << " was removed.\n"
  2120. << this->Makefile->GetPolicies()->GetPolicyWarning(
  2121. cmPolicies::CMP0018);
  2122. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  2123. // fall through to OLD behaviour
  2124. }
  2125. case cmPolicies::OLD:
  2126. return true;
  2127. case cmPolicies::REQUIRED_IF_USED:
  2128. case cmPolicies::REQUIRED_ALWAYS:
  2129. case cmPolicies::NEW:
  2130. default:
  2131. return false;
  2132. }
  2133. }
  2134. }
  2135. return false;
  2136. }
  2137. //----------------------------------------------------------------------------
  2138. void cmLocalGenerator::AddPositionIndependentFlags(std::string& flags,
  2139. std::string const& lang,
  2140. int targetType)
  2141. {
  2142. const char* picFlags = 0;
  2143. if(targetType == cmTarget::EXECUTABLE)
  2144. {
  2145. std::string flagsVar = "CMAKE_";
  2146. flagsVar += lang;
  2147. flagsVar += "_COMPILE_OPTIONS_PIE";
  2148. picFlags = this->Makefile->GetSafeDefinition(flagsVar.c_str());
  2149. }
  2150. if (!picFlags)
  2151. {
  2152. std::string flagsVar = "CMAKE_";
  2153. flagsVar += lang;
  2154. flagsVar += "_COMPILE_OPTIONS_PIC";
  2155. picFlags = this->Makefile->GetSafeDefinition(flagsVar.c_str());
  2156. }
  2157. if (picFlags)
  2158. {
  2159. std::vector<std::string> options;
  2160. cmSystemTools::ExpandListArgument(picFlags, options);
  2161. for(std::vector<std::string>::const_iterator oi = options.begin();
  2162. oi != options.end(); ++oi)
  2163. {
  2164. this->AppendFlagEscape(flags, oi->c_str());
  2165. }
  2166. }
  2167. }
  2168. //----------------------------------------------------------------------------
  2169. void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
  2170. const std::string& var,
  2171. const std::string& config)
  2172. {
  2173. // Add the flags from the variable itself.
  2174. std::string flagsVar = var;
  2175. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  2176. // Add the flags from the build-type specific variable.
  2177. if(!config.empty())
  2178. {
  2179. flagsVar += "_";
  2180. flagsVar += cmSystemTools::UpperCase(config);
  2181. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar.c_str()));
  2182. }
  2183. }
  2184. //----------------------------------------------------------------------------
  2185. void cmLocalGenerator::AppendFlags(std::string& flags,
  2186. const char* newFlags)
  2187. {
  2188. if(newFlags && *newFlags)
  2189. {
  2190. std::string newf = newFlags;
  2191. if(flags.size())
  2192. {
  2193. flags += " ";
  2194. }
  2195. flags += newFlags;
  2196. }
  2197. }
  2198. //----------------------------------------------------------------------------
  2199. void cmLocalGenerator::AppendFlagEscape(std::string& flags,
  2200. const std::string& rawFlag)
  2201. {
  2202. this->AppendFlags(flags, this->EscapeForShell(rawFlag).c_str());
  2203. }
  2204. //----------------------------------------------------------------------------
  2205. void cmLocalGenerator::AppendDefines(std::set<std::string>& defines,
  2206. const char* defines_list)
  2207. {
  2208. // Short-circuit if there are no definitions.
  2209. if(!defines_list)
  2210. {
  2211. return;
  2212. }
  2213. // Expand the list of definitions.
  2214. std::vector<std::string> defines_vec;
  2215. cmSystemTools::ExpandListArgument(defines_list, defines_vec);
  2216. this->AppendDefines(defines, defines_vec);
  2217. }
  2218. //----------------------------------------------------------------------------
  2219. void cmLocalGenerator::AppendDefines(std::set<std::string>& defines,
  2220. const std::vector<std::string> &defines_vec)
  2221. {
  2222. for(std::vector<std::string>::const_iterator di = defines_vec.begin();
  2223. di != defines_vec.end(); ++di)
  2224. {
  2225. // Skip unsupported definitions.
  2226. if(!this->CheckDefinition(*di))
  2227. {
  2228. continue;
  2229. }
  2230. defines.insert(*di);
  2231. }
  2232. }
  2233. //----------------------------------------------------------------------------
  2234. void cmLocalGenerator::JoinDefines(const std::set<std::string>& defines,
  2235. std::string &definesString,
  2236. const std::string& lang)
  2237. {
  2238. // Lookup the define flag for the current language.
  2239. std::string dflag = "-D";
  2240. if(!lang.empty())
  2241. {
  2242. std::string defineFlagVar = "CMAKE_";
  2243. defineFlagVar += lang;
  2244. defineFlagVar += "_DEFINE_FLAG";
  2245. const char* df = this->Makefile->GetDefinition(defineFlagVar.c_str());
  2246. if(df && *df)
  2247. {
  2248. dflag = df;
  2249. }
  2250. }
  2251. std::set<std::string>::const_iterator defineIt = defines.begin();
  2252. const std::set<std::string>::const_iterator defineEnd = defines.end();
  2253. const char* itemSeparator = definesString.empty() ? "" : " ";
  2254. for( ; defineIt != defineEnd; ++defineIt)
  2255. {
  2256. // Append the definition with proper escaping.
  2257. std::string def = dflag;
  2258. if(this->WatcomWMake)
  2259. {
  2260. // The Watcom compiler does its own command line parsing instead
  2261. // of using the windows shell rules. Definitions are one of
  2262. // -DNAME
  2263. // -DNAME=<cpp-token>
  2264. // -DNAME="c-string with spaces and other characters(?@#$)"
  2265. //
  2266. // Watcom will properly parse each of these cases from the
  2267. // command line without any escapes. However we still have to
  2268. // get the '$' and '#' characters through WMake as '$$' and
  2269. // '$#'.
  2270. for(const char* c = defineIt->c_str(); *c; ++c)
  2271. {
  2272. if(*c == '$' || *c == '#')
  2273. {
  2274. def += '$';
  2275. }
  2276. def += *c;
  2277. }
  2278. }
  2279. else
  2280. {
  2281. // Make the definition appear properly on the command line. Use
  2282. // -DNAME="value" instead of -D"NAME=value" to help VS6 parser.
  2283. std::string::size_type eq = defineIt->find("=");
  2284. def += defineIt->substr(0, eq);
  2285. if(eq != defineIt->npos)
  2286. {
  2287. def += "=";
  2288. def += this->EscapeForShell(defineIt->c_str() + eq + 1, true);
  2289. }
  2290. }
  2291. definesString += itemSeparator;
  2292. itemSeparator = " ";
  2293. definesString += def;
  2294. }
  2295. }
  2296. //----------------------------------------------------------------------------
  2297. void cmLocalGenerator::AppendFeatureOptions(
  2298. std::string& flags, const std::string& lang, const char* feature)
  2299. {
  2300. std::string optVar = "CMAKE_";
  2301. optVar += lang;
  2302. optVar += "_COMPILE_OPTIONS_";
  2303. optVar += feature;
  2304. if(const char* optionList = this->Makefile->GetDefinition(optVar.c_str()))
  2305. {
  2306. std::vector<std::string> options;
  2307. cmSystemTools::ExpandListArgument(optionList, options);
  2308. for(std::vector<std::string>::const_iterator oi = options.begin();
  2309. oi != options.end(); ++oi)
  2310. {
  2311. this->AppendFlagEscape(flags, oi->c_str());
  2312. }
  2313. }
  2314. }
  2315. //----------------------------------------------------------------------------
  2316. std::string
  2317. cmLocalGenerator::ConstructComment(const cmCustomCommand& cc,
  2318. const char* default_comment)
  2319. {
  2320. // Check for a comment provided with the command.
  2321. if(cc.GetComment())
  2322. {
  2323. return cc.GetComment();
  2324. }
  2325. // Construct a reasonable default comment if possible.
  2326. if(!cc.GetOutputs().empty())
  2327. {
  2328. std::string comment;
  2329. comment = "Generating ";
  2330. const char* sep = "";
  2331. for(std::vector<std::string>::const_iterator o = cc.GetOutputs().begin();
  2332. o != cc.GetOutputs().end(); ++o)
  2333. {
  2334. comment += sep;
  2335. comment += this->Convert(o->c_str(), cmLocalGenerator::START_OUTPUT);
  2336. sep = ", ";
  2337. }
  2338. return comment;
  2339. }
  2340. // Otherwise use the provided default.
  2341. return default_comment;
  2342. }
  2343. //----------------------------------------------------------------------------
  2344. std::string
  2345. cmLocalGenerator::ConvertToOptionallyRelativeOutputPath(
  2346. const std::string& remote)
  2347. {
  2348. return this->Convert(remote, START_OUTPUT, SHELL, true);
  2349. }
  2350. //----------------------------------------------------------------------------
  2351. const char* cmLocalGenerator::GetRelativeRootPath(RelativeRoot relroot)
  2352. {
  2353. switch (relroot)
  2354. {
  2355. case HOME: return this->Makefile->GetHomeDirectory();
  2356. case START: return this->Makefile->GetStartDirectory();
  2357. case HOME_OUTPUT: return this->Makefile->GetHomeOutputDirectory();
  2358. case START_OUTPUT: return this->Makefile->GetStartOutputDirectory();
  2359. default: break;
  2360. }
  2361. return 0;
  2362. }
  2363. //----------------------------------------------------------------------------
  2364. std::string cmLocalGenerator::Convert(const std::string& source,
  2365. RelativeRoot relative,
  2366. OutputFormat output,
  2367. bool optional)
  2368. {
  2369. // Make sure the relative path conversion components are set.
  2370. if(!this->PathConversionsSetup)
  2371. {
  2372. this->SetupPathConversions();
  2373. this->PathConversionsSetup = true;
  2374. }
  2375. // Convert the path to a relative path.
  2376. std::string result = source;
  2377. if (!optional || this->UseRelativePaths)
  2378. {
  2379. switch (relative)
  2380. {
  2381. case HOME:
  2382. //result = cmSystemTools::CollapseFullPath(result.c_str());
  2383. result = this->ConvertToRelativePath(this->HomeDirectoryComponents,
  2384. result.c_str());
  2385. break;
  2386. case START:
  2387. //result = cmSystemTools::CollapseFullPath(result.c_str());
  2388. result = this->ConvertToRelativePath(this->StartDirectoryComponents,
  2389. result.c_str());
  2390. break;
  2391. case HOME_OUTPUT:
  2392. //result = cmSystemTools::CollapseFullPath(result.c_str());
  2393. result =
  2394. this->ConvertToRelativePath(this->HomeOutputDirectoryComponents,
  2395. result.c_str());
  2396. break;
  2397. case START_OUTPUT:
  2398. //result = cmSystemTools::CollapseFullPath(result.c_str());
  2399. result =
  2400. this->ConvertToRelativePath(this->StartOutputDirectoryComponents,
  2401. result.c_str());
  2402. break;
  2403. case FULL:
  2404. result = cmSystemTools::CollapseFullPath(result.c_str());
  2405. break;
  2406. case NONE:
  2407. break;
  2408. }
  2409. }
  2410. return this->ConvertToOutputFormat(result.c_str(), output);
  2411. }
  2412. //----------------------------------------------------------------------------
  2413. std::string cmLocalGenerator::ConvertToOutputFormat(const std::string& source,
  2414. OutputFormat output)
  2415. {
  2416. std::string result = source;
  2417. // Convert it to an output path.
  2418. if (output == MAKEFILE)
  2419. {
  2420. result = cmSystemTools::ConvertToOutputPath(result.c_str());
  2421. }
  2422. else if( output == SHELL)
  2423. {
  2424. // For the MSYS shell convert drive letters to posix paths, so
  2425. // that c:/some/path becomes /c/some/path. This is needed to
  2426. // avoid problems with the shell path translation.
  2427. if(this->MSYSShell && !this->LinkScriptShell)
  2428. {
  2429. if(result.size() > 2 && result[1] == ':')
  2430. {
  2431. result[1] = result[0];
  2432. result[0] = '/';
  2433. }
  2434. }
  2435. if(this->WindowsShell)
  2436. {
  2437. std::string::size_type pos = 0;
  2438. while((pos = result.find('/', pos)) != std::string::npos)
  2439. {
  2440. result[pos] = '\\';
  2441. pos++;
  2442. }
  2443. }
  2444. result = this->EscapeForShell(result.c_str(), true, false);
  2445. }
  2446. else if(output == RESPONSE)
  2447. {
  2448. result = this->EscapeForShell(result.c_str(), false, false);
  2449. }
  2450. return result;
  2451. }
  2452. //----------------------------------------------------------------------------
  2453. std::string cmLocalGenerator::Convert(RelativeRoot remote,
  2454. const std::string& local,
  2455. OutputFormat output,
  2456. bool optional)
  2457. {
  2458. const char* remotePath = this->GetRelativeRootPath(remote);
  2459. // The relative root must have a path (i.e. not FULL or NONE)
  2460. assert(remotePath != 0);
  2461. if(!local.empty() && (!optional || this->UseRelativePaths))
  2462. {
  2463. std::vector<std::string> components;
  2464. cmSystemTools::SplitPath(local.c_str(), components);
  2465. std::string result = this->ConvertToRelativePath(components, remotePath);
  2466. return this->ConvertToOutputFormat(result.c_str(), output);
  2467. }
  2468. else
  2469. {
  2470. return this->ConvertToOutputFormat(remotePath, output);
  2471. }
  2472. }
  2473. //----------------------------------------------------------------------------
  2474. std::string cmLocalGenerator::FindRelativePathTopSource()
  2475. {
  2476. // Relative path conversion within a single tree managed by CMake is
  2477. // safe. We can use our parent relative path top if and only if
  2478. // this is a subdirectory of that top.
  2479. if(cmLocalGenerator* parent = this->GetParent())
  2480. {
  2481. std::string parentTop = parent->FindRelativePathTopSource();
  2482. if(cmSystemTools::IsSubDirectory(
  2483. this->Makefile->GetStartDirectory(), parentTop.c_str()))
  2484. {
  2485. return parentTop;
  2486. }
  2487. }
  2488. // Otherwise this directory itself is the new top.
  2489. return this->Makefile->GetStartDirectory();
  2490. }
  2491. //----------------------------------------------------------------------------
  2492. std::string cmLocalGenerator::FindRelativePathTopBinary()
  2493. {
  2494. // Relative path conversion within a single tree managed by CMake is
  2495. // safe. We can use our parent relative path top if and only if
  2496. // this is a subdirectory of that top.
  2497. if(cmLocalGenerator* parent = this->GetParent())
  2498. {
  2499. std::string parentTop = parent->FindRelativePathTopBinary();
  2500. if(cmSystemTools::IsSubDirectory(
  2501. this->Makefile->GetStartOutputDirectory(), parentTop.c_str()))
  2502. {
  2503. return parentTop;
  2504. }
  2505. }
  2506. // Otherwise this directory itself is the new top.
  2507. return this->Makefile->GetStartOutputDirectory();
  2508. }
  2509. //----------------------------------------------------------------------------
  2510. void cmLocalGenerator::ConfigureRelativePaths()
  2511. {
  2512. // Relative path conversion inside the source tree is not used to
  2513. // construct relative paths passed to build tools so it is safe to
  2514. // even when the source is a network path.
  2515. std::string source = this->FindRelativePathTopSource();
  2516. this->RelativePathTopSource = source;
  2517. // The current working directory on Windows cannot be a network
  2518. // path. Therefore relative paths cannot work when the binary tree
  2519. // is a network path.
  2520. std::string binary = this->FindRelativePathTopBinary();
  2521. if(binary.size() < 2 || binary.substr(0, 2) != "//")
  2522. {
  2523. this->RelativePathTopBinary = binary;
  2524. }
  2525. else
  2526. {
  2527. this->RelativePathTopBinary = "";
  2528. }
  2529. }
  2530. //----------------------------------------------------------------------------
  2531. static bool cmLocalGeneratorNotAbove(const char* a, const char* b)
  2532. {
  2533. return (cmSystemTools::ComparePath(a, b) ||
  2534. cmSystemTools::IsSubDirectory(a, b));
  2535. }
  2536. //----------------------------------------------------------------------------
  2537. std::string
  2538. cmLocalGenerator::ConvertToRelativePath(const std::vector<std::string>& local,
  2539. const std::string& in_remote,
  2540. bool force)
  2541. {
  2542. // The path should never be quoted.
  2543. assert(in_remote[0] != '\"');
  2544. // The local path should never have a trailing slash.
  2545. assert(local.size() > 0 && !(local[local.size()-1] == ""));
  2546. // If the path is already relative then just return the path.
  2547. if(!cmSystemTools::FileIsFullPath(in_remote.c_str()))
  2548. {
  2549. return in_remote;
  2550. }
  2551. // Make sure relative path conversion is configured.
  2552. if(!this->RelativePathsConfigured)
  2553. {
  2554. this->ConfigureRelativePaths();
  2555. this->RelativePathsConfigured = true;
  2556. }
  2557. if(!force)
  2558. {
  2559. // Skip conversion if the path and local are not both in the source
  2560. // or both in the binary tree.
  2561. std::string local_path = cmSystemTools::JoinPath(local);
  2562. if(!((cmLocalGeneratorNotAbove(local_path.c_str(),
  2563. this->RelativePathTopBinary.c_str()) &&
  2564. cmLocalGeneratorNotAbove(in_remote.c_str(),
  2565. this->RelativePathTopBinary.c_str())) ||
  2566. (cmLocalGeneratorNotAbove(local_path.c_str(),
  2567. this->RelativePathTopSource.c_str()) &&
  2568. cmLocalGeneratorNotAbove(in_remote.c_str(),
  2569. this->RelativePathTopSource.c_str()))))
  2570. {
  2571. return in_remote;
  2572. }
  2573. }
  2574. // Identify the longest shared path component between the remote
  2575. // path and the local path.
  2576. std::vector<std::string> remote;
  2577. cmSystemTools::SplitPath(in_remote.c_str(), remote);
  2578. unsigned int common=0;
  2579. while(common < remote.size() &&
  2580. common < local.size() &&
  2581. cmSystemTools::ComparePath(remote[common].c_str(),
  2582. local[common].c_str()))
  2583. {
  2584. ++common;
  2585. }
  2586. // If no part of the path is in common then return the full path.
  2587. if(common == 0)
  2588. {
  2589. return in_remote;
  2590. }
  2591. // If the entire path is in common then just return a ".".
  2592. if(common == remote.size() &&
  2593. common == local.size())
  2594. {
  2595. return ".";
  2596. }
  2597. // If the entire path is in common except for a trailing slash then
  2598. // just return a "./".
  2599. if(common+1 == remote.size() &&
  2600. remote[common].size() == 0 &&
  2601. common == local.size())
  2602. {
  2603. return "./";
  2604. }
  2605. // Construct the relative path.
  2606. std::string relative;
  2607. // First add enough ../ to get up to the level of the shared portion
  2608. // of the path. Leave off the trailing slash. Note that the last
  2609. // component of local will never be empty because local should never
  2610. // have a trailing slash.
  2611. for(unsigned int i=common; i < local.size(); ++i)
  2612. {
  2613. relative += "..";
  2614. if(i < local.size()-1)
  2615. {
  2616. relative += "/";
  2617. }
  2618. }
  2619. // Now add the portion of the destination path that is not included
  2620. // in the shared portion of the path. Add a slash the first time
  2621. // only if there was already something in the path. If there was a
  2622. // trailing slash in the input then the last iteration of the loop
  2623. // will add a slash followed by an empty string which will preserve
  2624. // the trailing slash in the output.
  2625. for(unsigned int i=common; i < remote.size(); ++i)
  2626. {
  2627. if(relative.size() > 0)
  2628. {
  2629. relative += "/";
  2630. }
  2631. relative += remote[i];
  2632. }
  2633. // Finally return the path.
  2634. return relative;
  2635. }
  2636. //----------------------------------------------------------------------------
  2637. void
  2638. cmLocalGenerator
  2639. ::GenerateTargetInstallRules(
  2640. std::ostream& os, const std::string& config,
  2641. std::vector<std::string> const& configurationTypes)
  2642. {
  2643. // Convert the old-style install specification from each target to
  2644. // an install generator and run it.
  2645. cmTargets& tgts = this->Makefile->GetTargets();
  2646. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); ++l)
  2647. {
  2648. if (l->second.GetType() == cmTarget::INTERFACE_LIBRARY)
  2649. {
  2650. continue;
  2651. }
  2652. // Include the user-specified pre-install script for this target.
  2653. if(const char* preinstall = l->second.GetProperty("PRE_INSTALL_SCRIPT"))
  2654. {
  2655. cmInstallScriptGenerator g(preinstall, false, 0);
  2656. g.Generate(os, config, configurationTypes);
  2657. }
  2658. // Install this target if a destination is given.
  2659. if(l->second.GetInstallPath() != "")
  2660. {
  2661. // Compute the full install destination. Note that converting
  2662. // to unix slashes also removes any trailing slash.
  2663. // We also skip over the leading slash given by the user.
  2664. std::string destination = l->second.GetInstallPath().substr(1);
  2665. cmSystemTools::ConvertToUnixSlashes(destination);
  2666. if(destination.empty())
  2667. {
  2668. destination = ".";
  2669. }
  2670. // Generate the proper install generator for this target type.
  2671. switch(l->second.GetType())
  2672. {
  2673. case cmTarget::EXECUTABLE:
  2674. case cmTarget::STATIC_LIBRARY:
  2675. case cmTarget::MODULE_LIBRARY:
  2676. {
  2677. // Use a target install generator.
  2678. cmInstallTargetGenerator g(l->second, destination.c_str(), false);
  2679. g.Generate(os, config, configurationTypes);
  2680. }
  2681. break;
  2682. case cmTarget::SHARED_LIBRARY:
  2683. {
  2684. #if defined(_WIN32) || defined(__CYGWIN__)
  2685. // Special code to handle DLL. Install the import library
  2686. // to the normal destination and the DLL to the runtime
  2687. // destination.
  2688. cmInstallTargetGenerator g1(l->second, destination.c_str(), true);
  2689. g1.Generate(os, config, configurationTypes);
  2690. // We also skip over the leading slash given by the user.
  2691. destination = l->second.GetRuntimeInstallPath().substr(1);
  2692. cmSystemTools::ConvertToUnixSlashes(destination);
  2693. cmInstallTargetGenerator g2(l->second, destination.c_str(), false);
  2694. g2.Generate(os, config, configurationTypes);
  2695. #else
  2696. // Use a target install generator.
  2697. cmInstallTargetGenerator g(l->second, destination.c_str(), false);
  2698. g.Generate(os, config, configurationTypes);
  2699. #endif
  2700. }
  2701. break;
  2702. default:
  2703. break;
  2704. }
  2705. }
  2706. // Include the user-specified post-install script for this target.
  2707. if(const char* postinstall = l->second.GetProperty("POST_INSTALL_SCRIPT"))
  2708. {
  2709. cmInstallScriptGenerator g(postinstall, false, 0);
  2710. g.Generate(os, config, configurationTypes);
  2711. }
  2712. }
  2713. }
  2714. #if defined(CM_LG_ENCODE_OBJECT_NAMES)
  2715. static std::string cmLocalGeneratorMD5(const char* input)
  2716. {
  2717. char md5out[32];
  2718. cmsysMD5* md5 = cmsysMD5_New();
  2719. cmsysMD5_Initialize(md5);
  2720. cmsysMD5_Append(md5, reinterpret_cast<unsigned char const*>(input), -1);
  2721. cmsysMD5_FinalizeHex(md5, md5out);
  2722. cmsysMD5_Delete(md5);
  2723. return std::string(md5out, 32);
  2724. }
  2725. static bool
  2726. cmLocalGeneratorShortenObjectName(std::string& objName,
  2727. std::string::size_type max_len)
  2728. {
  2729. // Replace the beginning of the path portion of the object name with
  2730. // its own md5 sum.
  2731. std::string::size_type pos = objName.find('/', objName.size()-max_len+32);
  2732. if(pos != objName.npos)
  2733. {
  2734. std::string md5name = cmLocalGeneratorMD5(objName.substr(0, pos).c_str());
  2735. md5name += objName.substr(pos);
  2736. objName = md5name;
  2737. // The object name is now short enough.
  2738. return true;
  2739. }
  2740. else
  2741. {
  2742. // The object name could not be shortened enough.
  2743. return false;
  2744. }
  2745. }
  2746. static
  2747. bool cmLocalGeneratorCheckObjectName(std::string& objName,
  2748. std::string::size_type dir_len,
  2749. std::string::size_type max_total_len)
  2750. {
  2751. // Enforce the maximum file name length if possible.
  2752. std::string::size_type max_obj_len = max_total_len;
  2753. if(dir_len < max_total_len)
  2754. {
  2755. max_obj_len = max_total_len - dir_len;
  2756. if(objName.size() > max_obj_len)
  2757. {
  2758. // The current object file name is too long. Try to shorten it.
  2759. return cmLocalGeneratorShortenObjectName(objName, max_obj_len);
  2760. }
  2761. else
  2762. {
  2763. // The object file name is short enough.
  2764. return true;
  2765. }
  2766. }
  2767. else
  2768. {
  2769. // The build directory in which the object will be stored is
  2770. // already too deep.
  2771. return false;
  2772. }
  2773. }
  2774. #endif
  2775. //----------------------------------------------------------------------------
  2776. std::string&
  2777. cmLocalGenerator
  2778. ::CreateSafeUniqueObjectFileName(const std::string& sin,
  2779. std::string const& dir_max)
  2780. {
  2781. // Look for an existing mapped name for this object file.
  2782. std::map<std::string,std::string>::iterator it =
  2783. this->UniqueObjectNamesMap.find(sin);
  2784. // If no entry exists create one.
  2785. if(it == this->UniqueObjectNamesMap.end())
  2786. {
  2787. // Start with the original name.
  2788. std::string ssin = sin;
  2789. // Avoid full paths by removing leading slashes.
  2790. std::string::size_type pos = 0;
  2791. for(;pos < ssin.size() && ssin[pos] == '/'; ++pos)
  2792. {
  2793. }
  2794. ssin = ssin.substr(pos);
  2795. // Avoid full paths by removing colons.
  2796. cmSystemTools::ReplaceString(ssin, ":", "_");
  2797. // Avoid relative paths that go up the tree.
  2798. cmSystemTools::ReplaceString(ssin, "../", "__/");
  2799. // Avoid spaces.
  2800. cmSystemTools::ReplaceString(ssin, " ", "_");
  2801. // Mangle the name if necessary.
  2802. if(this->Makefile->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES"))
  2803. {
  2804. bool done;
  2805. int cc = 0;
  2806. char rpstr[100];
  2807. sprintf(rpstr, "_p_");
  2808. cmSystemTools::ReplaceString(ssin, "+", rpstr);
  2809. std::string sssin = sin;
  2810. do
  2811. {
  2812. done = true;
  2813. for ( it = this->UniqueObjectNamesMap.begin();
  2814. it != this->UniqueObjectNamesMap.end();
  2815. ++ it )
  2816. {
  2817. if ( it->second == ssin )
  2818. {
  2819. done = false;
  2820. }
  2821. }
  2822. if ( done )
  2823. {
  2824. break;
  2825. }
  2826. sssin = ssin;
  2827. cmSystemTools::ReplaceString(ssin, "_p_", rpstr);
  2828. sprintf(rpstr, "_p%d_", cc++);
  2829. }
  2830. while ( !done );
  2831. }
  2832. #if defined(CM_LG_ENCODE_OBJECT_NAMES)
  2833. if(!cmLocalGeneratorCheckObjectName(ssin, dir_max.size(),
  2834. this->ObjectPathMax))
  2835. {
  2836. // Warn if this is the first time the path has been seen.
  2837. if(this->ObjectMaxPathViolations.insert(dir_max).second)
  2838. {
  2839. cmOStringStream m;
  2840. m << "The object file directory\n"
  2841. << " " << dir_max << "\n"
  2842. << "has " << dir_max.size() << " characters. "
  2843. << "The maximum full path to an object file is "
  2844. << this->ObjectPathMax << " characters "
  2845. << "(see CMAKE_OBJECT_PATH_MAX). "
  2846. << "Object file\n"
  2847. << " " << ssin << "\n"
  2848. << "cannot be safely placed under this directory. "
  2849. << "The build may not work correctly.";
  2850. this->Makefile->IssueMessage(cmake::WARNING, m.str());
  2851. }
  2852. }
  2853. #else
  2854. (void)dir_max;
  2855. #endif
  2856. // Insert the newly mapped object file name.
  2857. std::map<std::string, std::string>::value_type e(sin, ssin);
  2858. it = this->UniqueObjectNamesMap.insert(e).first;
  2859. }
  2860. // Return the map entry.
  2861. return it->second;
  2862. }
  2863. //----------------------------------------------------------------------------
  2864. std::string
  2865. cmLocalGenerator
  2866. ::GetObjectFileNameWithoutTarget(const cmSourceFile& source,
  2867. std::string const& dir_max,
  2868. bool* hasSourceExtension)
  2869. {
  2870. // Construct the object file name using the full path to the source
  2871. // file which is its only unique identification.
  2872. const char* fullPath = source.GetFullPath().c_str();
  2873. // Try referencing the source relative to the source tree.
  2874. std::string relFromSource = this->Convert(fullPath, START);
  2875. assert(!relFromSource.empty());
  2876. bool relSource = !cmSystemTools::FileIsFullPath(relFromSource.c_str());
  2877. bool subSource = relSource && relFromSource[0] != '.';
  2878. // Try referencing the source relative to the binary tree.
  2879. std::string relFromBinary = this->Convert(fullPath, START_OUTPUT);
  2880. assert(!relFromBinary.empty());
  2881. bool relBinary = !cmSystemTools::FileIsFullPath(relFromBinary.c_str());
  2882. bool subBinary = relBinary && relFromBinary[0] != '.';
  2883. // Select a nice-looking reference to the source file to construct
  2884. // the object file name.
  2885. std::string objectName;
  2886. if((relSource && !relBinary) || (subSource && !subBinary))
  2887. {
  2888. objectName = relFromSource;
  2889. }
  2890. else if((relBinary && !relSource) || (subBinary && !subSource))
  2891. {
  2892. objectName = relFromBinary;
  2893. }
  2894. else if(relFromBinary.length() < relFromSource.length())
  2895. {
  2896. objectName = relFromBinary;
  2897. }
  2898. else
  2899. {
  2900. objectName = relFromSource;
  2901. }
  2902. // if it is still a full path check for the try compile case
  2903. // try compile never have in source sources, and should not
  2904. // have conflicting source file names in the same target
  2905. if(cmSystemTools::FileIsFullPath(objectName.c_str()))
  2906. {
  2907. if(this->GetGlobalGenerator()->GetCMakeInstance()->GetIsInTryCompile())
  2908. {
  2909. objectName = cmSystemTools::GetFilenameName(source.GetFullPath());
  2910. }
  2911. }
  2912. // Replace the original source file extension with the object file
  2913. // extension.
  2914. bool keptSourceExtension = true;
  2915. if(!source.GetPropertyAsBool("KEEP_EXTENSION"))
  2916. {
  2917. // Decide whether this language wants to replace the source
  2918. // extension with the object extension. For CMake 2.4
  2919. // compatibility do this by default.
  2920. bool replaceExt = this->NeedBackwardsCompatibility_2_4();
  2921. if(!replaceExt)
  2922. {
  2923. std::string lang = source.GetLanguage();
  2924. if(!lang.empty())
  2925. {
  2926. std::string repVar = "CMAKE_";
  2927. repVar += lang;
  2928. repVar += "_OUTPUT_EXTENSION_REPLACE";
  2929. replaceExt = this->Makefile->IsOn(repVar.c_str());
  2930. }
  2931. }
  2932. // Remove the source extension if it is to be replaced.
  2933. if(replaceExt)
  2934. {
  2935. keptSourceExtension = false;
  2936. std::string::size_type dot_pos = objectName.rfind(".");
  2937. if(dot_pos != std::string::npos)
  2938. {
  2939. objectName = objectName.substr(0, dot_pos);
  2940. }
  2941. }
  2942. // Store the new extension.
  2943. objectName +=
  2944. this->GlobalGenerator->GetLanguageOutputExtension(source);
  2945. }
  2946. if(hasSourceExtension)
  2947. {
  2948. *hasSourceExtension = keptSourceExtension;
  2949. }
  2950. // Convert to a safe name.
  2951. return this->CreateSafeUniqueObjectFileName(objectName.c_str(), dir_max);
  2952. }
  2953. //----------------------------------------------------------------------------
  2954. std::string
  2955. cmLocalGenerator
  2956. ::GetSourceFileLanguage(const cmSourceFile& source)
  2957. {
  2958. return source.GetLanguage();
  2959. }
  2960. //----------------------------------------------------------------------------
  2961. std::string cmLocalGenerator::EscapeForShellOldStyle(const std::string& str)
  2962. {
  2963. std::string result;
  2964. #if defined(_WIN32) && !defined(__CYGWIN__)
  2965. // if there are spaces
  2966. std::string temp = str;
  2967. if (temp.find(" ") != std::string::npos &&
  2968. temp.find("\"")==std::string::npos)
  2969. {
  2970. result = "\"";
  2971. result += str;
  2972. result += "\"";
  2973. return result;
  2974. }
  2975. return str;
  2976. #else
  2977. for(const char* ch = str.c_str(); *ch != '\0'; ++ch)
  2978. {
  2979. if(*ch == ' ')
  2980. {
  2981. result += '\\';
  2982. }
  2983. result += *ch;
  2984. }
  2985. return result;
  2986. #endif
  2987. }
  2988. //----------------------------------------------------------------------------
  2989. static bool cmLocalGeneratorIsShellOperator(const std::string& str)
  2990. {
  2991. static std::set<std::string> shellOperators;
  2992. if(shellOperators.empty())
  2993. {
  2994. shellOperators.insert("<");
  2995. shellOperators.insert(">");
  2996. shellOperators.insert("<<");
  2997. shellOperators.insert(">>");
  2998. shellOperators.insert("|");
  2999. shellOperators.insert("||");
  3000. shellOperators.insert("&&");
  3001. shellOperators.insert("&>");
  3002. shellOperators.insert("1>");
  3003. shellOperators.insert("2>");
  3004. shellOperators.insert("2>&1");
  3005. shellOperators.insert("1>&2");
  3006. }
  3007. return shellOperators.count(str) > 0;
  3008. }
  3009. //----------------------------------------------------------------------------
  3010. std::string cmLocalGenerator::EscapeForShell(const std::string& str,
  3011. bool makeVars,
  3012. bool forEcho)
  3013. {
  3014. // Do not escape shell operators.
  3015. if(cmLocalGeneratorIsShellOperator(str))
  3016. {
  3017. return str;
  3018. }
  3019. // Compute the flags for the target shell environment.
  3020. int flags = 0;
  3021. if(this->WindowsVSIDE)
  3022. {
  3023. flags |= cmsysSystem_Shell_Flag_VSIDE;
  3024. }
  3025. else if(!this->LinkScriptShell)
  3026. {
  3027. flags |= cmsysSystem_Shell_Flag_Make;
  3028. }
  3029. if(makeVars)
  3030. {
  3031. flags |= cmsysSystem_Shell_Flag_AllowMakeVariables;
  3032. }
  3033. if(forEcho)
  3034. {
  3035. flags |= cmsysSystem_Shell_Flag_EchoWindows;
  3036. }
  3037. if(this->WatcomWMake)
  3038. {
  3039. flags |= cmsysSystem_Shell_Flag_WatcomWMake;
  3040. }
  3041. if(this->MinGWMake)
  3042. {
  3043. flags |= cmsysSystem_Shell_Flag_MinGWMake;
  3044. }
  3045. if(this->NMake)
  3046. {
  3047. flags |= cmsysSystem_Shell_Flag_NMake;
  3048. }
  3049. // Compute the buffer size needed.
  3050. int size = (this->WindowsShell ?
  3051. cmsysSystem_Shell_GetArgumentSizeForWindows(str.c_str(), flags) :
  3052. cmsysSystem_Shell_GetArgumentSizeForUnix(str.c_str(), flags));
  3053. // Compute the shell argument itself.
  3054. std::vector<char> arg(size);
  3055. if(this->WindowsShell)
  3056. {
  3057. cmsysSystem_Shell_GetArgumentForWindows(str.c_str(), &arg[0], flags);
  3058. }
  3059. else
  3060. {
  3061. cmsysSystem_Shell_GetArgumentForUnix(str.c_str(), &arg[0], flags);
  3062. }
  3063. return std::string(&arg[0]);
  3064. }
  3065. //----------------------------------------------------------------------------
  3066. std::string cmLocalGenerator::EscapeForCMake(const std::string& str)
  3067. {
  3068. // Always double-quote the argument to take care of most escapes.
  3069. std::string result = "\"";
  3070. for(const char* c = str.c_str(); *c; ++c)
  3071. {
  3072. if(*c == '"')
  3073. {
  3074. // Escape the double quote to avoid ending the argument.
  3075. result += "\\\"";
  3076. }
  3077. else if(*c == '$')
  3078. {
  3079. // Escape the dollar to avoid expanding variables.
  3080. result += "\\$";
  3081. }
  3082. else if(*c == '\\')
  3083. {
  3084. // Escape the backslash to avoid other escapes.
  3085. result += "\\\\";
  3086. }
  3087. else
  3088. {
  3089. // Other characters will be parsed correctly.
  3090. result += *c;
  3091. }
  3092. }
  3093. result += "\"";
  3094. return result;
  3095. }
  3096. //----------------------------------------------------------------------------
  3097. cmLocalGenerator::FortranFormat
  3098. cmLocalGenerator::GetFortranFormat(const char* value)
  3099. {
  3100. FortranFormat format = FortranFormatNone;
  3101. if(value && *value)
  3102. {
  3103. std::vector<std::string> fmt;
  3104. cmSystemTools::ExpandListArgument(value, fmt);
  3105. for(std::vector<std::string>::iterator fi = fmt.begin();
  3106. fi != fmt.end(); ++fi)
  3107. {
  3108. if(*fi == "FIXED")
  3109. {
  3110. format = FortranFormatFixed;
  3111. }
  3112. if(*fi == "FREE")
  3113. {
  3114. format = FortranFormatFree;
  3115. }
  3116. }
  3117. }
  3118. return format;
  3119. }
  3120. //----------------------------------------------------------------------------
  3121. std::string
  3122. cmLocalGenerator::GetTargetDirectory(cmTarget const&) const
  3123. {
  3124. cmSystemTools::Error("GetTargetDirectory"
  3125. " called on cmLocalGenerator");
  3126. return "";
  3127. }
  3128. //----------------------------------------------------------------------------
  3129. cmIML_INT_uint64_t cmLocalGenerator::GetBackwardsCompatibility()
  3130. {
  3131. // The computed version may change until the project is fully
  3132. // configured.
  3133. if(!this->BackwardsCompatibilityFinal)
  3134. {
  3135. unsigned int major = 0;
  3136. unsigned int minor = 0;
  3137. unsigned int patch = 0;
  3138. if(const char* value
  3139. = this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY"))
  3140. {
  3141. switch(sscanf(value, "%u.%u.%u", &major, &minor, &patch))
  3142. {
  3143. case 2: patch = 0; break;
  3144. case 1: minor = 0; patch = 0; break;
  3145. default: break;
  3146. }
  3147. }
  3148. this->BackwardsCompatibility = CMake_VERSION_ENCODE(major, minor, patch);
  3149. this->BackwardsCompatibilityFinal = this->Configured;
  3150. }
  3151. return this->BackwardsCompatibility;
  3152. }
  3153. //----------------------------------------------------------------------------
  3154. bool cmLocalGenerator::NeedBackwardsCompatibility_2_4()
  3155. {
  3156. // Check the policy to decide whether to pay attention to this
  3157. // variable.
  3158. switch(this->Makefile->GetPolicyStatus(cmPolicies::CMP0001))
  3159. {
  3160. case cmPolicies::WARN:
  3161. // WARN is just OLD without warning because user code does not
  3162. // always affect whether this check is done.
  3163. case cmPolicies::OLD:
  3164. // Old behavior is to check the variable.
  3165. break;
  3166. case cmPolicies::NEW:
  3167. // New behavior is to ignore the variable.
  3168. return false;
  3169. case cmPolicies::REQUIRED_IF_USED:
  3170. case cmPolicies::REQUIRED_ALWAYS:
  3171. // This will never be the case because the only way to require
  3172. // the setting is to require the user to specify version policy
  3173. // 2.6 or higher. Once we add that requirement then this whole
  3174. // method can be removed anyway.
  3175. return false;
  3176. }
  3177. // Compatibility is needed if CMAKE_BACKWARDS_COMPATIBILITY is set
  3178. // equal to or lower than the given version.
  3179. cmIML_INT_uint64_t actual_compat = this->GetBackwardsCompatibility();
  3180. return (actual_compat &&
  3181. actual_compat <= CMake_VERSION_ENCODE(2, 4, 255));
  3182. }
  3183. //----------------------------------------------------------------------------
  3184. bool cmLocalGenerator::CheckDefinition(std::string const& define) const
  3185. {
  3186. // Many compilers do not support -DNAME(arg)=sdf so we disable it.
  3187. bool function_style = false;
  3188. for(const char* c = define.c_str(); *c && *c != '='; ++c)
  3189. {
  3190. if(*c == '(')
  3191. {
  3192. function_style = true;
  3193. break;
  3194. }
  3195. }
  3196. if(function_style)
  3197. {
  3198. cmOStringStream e;
  3199. e << "WARNING: Function-style preprocessor definitions may not be "
  3200. << "passed on the compiler command line because many compilers "
  3201. << "do not support it.\n"
  3202. << "CMake is dropping a preprocessor definition: " << define << "\n"
  3203. << "Consider defining the macro in a (configured) header file.\n";
  3204. cmSystemTools::Message(e.str().c_str());
  3205. return false;
  3206. }
  3207. // Many compilers do not support # in the value so we disable it.
  3208. if(define.find_first_of("#") != define.npos)
  3209. {
  3210. cmOStringStream e;
  3211. e << "WARNING: Preprocessor definitions containing '#' may not be "
  3212. << "passed on the compiler command line because many compilers "
  3213. << "do not support it.\n"
  3214. << "CMake is dropping a preprocessor definition: " << define << "\n"
  3215. << "Consider defining the macro in a (configured) header file.\n";
  3216. cmSystemTools::Message(e.str().c_str());
  3217. return false;
  3218. }
  3219. // Assume it is supported.
  3220. return true;
  3221. }
  3222. //----------------------------------------------------------------------------
  3223. static void cmLGInfoProp(cmMakefile* mf, cmTarget* target,
  3224. const std::string& prop)
  3225. {
  3226. if(const char* val = target->GetProperty(prop))
  3227. {
  3228. mf->AddDefinition(prop.c_str(), val);
  3229. }
  3230. }
  3231. //----------------------------------------------------------------------------
  3232. void cmLocalGenerator::GenerateAppleInfoPList(cmTarget* target,
  3233. const std::string& targetName,
  3234. const char* fname)
  3235. {
  3236. // Find the Info.plist template.
  3237. const char* in = target->GetProperty("MACOSX_BUNDLE_INFO_PLIST");
  3238. std::string inFile = (in && *in)? in : "MacOSXBundleInfo.plist.in";
  3239. if(!cmSystemTools::FileIsFullPath(inFile.c_str()))
  3240. {
  3241. std::string inMod = this->Makefile->GetModulesFile(inFile.c_str());
  3242. if(!inMod.empty())
  3243. {
  3244. inFile = inMod;
  3245. }
  3246. }
  3247. if(!cmSystemTools::FileExists(inFile.c_str(), true))
  3248. {
  3249. cmOStringStream e;
  3250. e << "Target " << target->GetName() << " Info.plist template \""
  3251. << inFile << "\" could not be found.";
  3252. cmSystemTools::Error(e.str().c_str());
  3253. return;
  3254. }
  3255. // Convert target properties to variables in an isolated makefile
  3256. // scope to configure the file. If properties are set they will
  3257. // override user make variables. If not the configuration will fall
  3258. // back to the directory-level values set by the user.
  3259. cmMakefile* mf = this->Makefile;
  3260. mf->PushScope();
  3261. mf->AddDefinition("MACOSX_BUNDLE_EXECUTABLE_NAME", targetName.c_str());
  3262. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_INFO_STRING");
  3263. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_ICON_FILE");
  3264. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_GUI_IDENTIFIER");
  3265. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_LONG_VERSION_STRING");
  3266. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_BUNDLE_NAME");
  3267. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_SHORT_VERSION_STRING");
  3268. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_BUNDLE_VERSION");
  3269. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_COPYRIGHT");
  3270. mf->ConfigureFile(inFile.c_str(), fname, false, false, false);
  3271. mf->PopScope();
  3272. }
  3273. //----------------------------------------------------------------------------
  3274. void cmLocalGenerator::GenerateFrameworkInfoPList(cmTarget* target,
  3275. const std::string& targetName,
  3276. const char* fname)
  3277. {
  3278. // Find the Info.plist template.
  3279. const char* in = target->GetProperty("MACOSX_FRAMEWORK_INFO_PLIST");
  3280. std::string inFile = (in && *in)? in : "MacOSXFrameworkInfo.plist.in";
  3281. if(!cmSystemTools::FileIsFullPath(inFile.c_str()))
  3282. {
  3283. std::string inMod = this->Makefile->GetModulesFile(inFile.c_str());
  3284. if(!inMod.empty())
  3285. {
  3286. inFile = inMod;
  3287. }
  3288. }
  3289. if(!cmSystemTools::FileExists(inFile.c_str(), true))
  3290. {
  3291. cmOStringStream e;
  3292. e << "Target " << target->GetName() << " Info.plist template \""
  3293. << inFile << "\" could not be found.";
  3294. cmSystemTools::Error(e.str().c_str());
  3295. return;
  3296. }
  3297. // Convert target properties to variables in an isolated makefile
  3298. // scope to configure the file. If properties are set they will
  3299. // override user make variables. If not the configuration will fall
  3300. // back to the directory-level values set by the user.
  3301. cmMakefile* mf = this->Makefile;
  3302. mf->PushScope();
  3303. mf->AddDefinition("MACOSX_FRAMEWORK_NAME", targetName.c_str());
  3304. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_ICON_FILE");
  3305. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_IDENTIFIER");
  3306. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_SHORT_VERSION_STRING");
  3307. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_BUNDLE_VERSION");
  3308. mf->ConfigureFile(inFile.c_str(), fname, false, false, false);
  3309. mf->PopScope();
  3310. }