cmGeneratorTarget.cxx 186 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmGeneratorTarget.h"
  4. #include "cmsys/RegularExpression.hxx"
  5. #include <algorithm>
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <iterator>
  9. #include <memory> // IWYU pragma: keep
  10. #include <queue>
  11. #include <sstream>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <unordered_set>
  16. #include "cmAlgorithms.h"
  17. #include "cmComputeLinkInformation.h"
  18. #include "cmCustomCommand.h"
  19. #include "cmCustomCommandGenerator.h"
  20. #include "cmCustomCommandLines.h"
  21. #include "cmGeneratorExpression.h"
  22. #include "cmGeneratorExpressionDAGChecker.h"
  23. #include "cmGlobalGenerator.h"
  24. #include "cmLocalGenerator.h"
  25. #include "cmMakefile.h"
  26. #include "cmPropertyMap.h"
  27. #include "cmSourceFile.h"
  28. #include "cmSourceFileLocation.h"
  29. #include "cmState.h"
  30. #include "cmSystemTools.h"
  31. #include "cmTarget.h"
  32. #include "cmTargetLinkLibraryType.h"
  33. #include "cmTargetPropertyComputer.h"
  34. #include "cmake.h"
  35. class cmMessenger;
  36. template <>
  37. const char* cmTargetPropertyComputer::GetSources<cmGeneratorTarget>(
  38. cmGeneratorTarget const* tgt, cmMessenger* /* messenger */,
  39. cmListFileBacktrace const& /* context */)
  40. {
  41. return tgt->GetSourcesProperty();
  42. }
  43. template <>
  44. const char*
  45. cmTargetPropertyComputer::ComputeLocationForBuild<cmGeneratorTarget>(
  46. cmGeneratorTarget const* tgt)
  47. {
  48. return tgt->GetLocation("");
  49. }
  50. template <>
  51. const char* cmTargetPropertyComputer::ComputeLocation<cmGeneratorTarget>(
  52. cmGeneratorTarget const* tgt, const std::string& config)
  53. {
  54. return tgt->GetLocation(config);
  55. }
  56. class cmGeneratorTarget::TargetPropertyEntry
  57. {
  58. static cmLinkImplItem NoLinkImplItem;
  59. public:
  60. TargetPropertyEntry(std::unique_ptr<cmCompiledGeneratorExpression> cge,
  61. cmLinkImplItem const& item = NoLinkImplItem)
  62. : ge(std::move(cge))
  63. , LinkImplItem(item)
  64. {
  65. }
  66. const std::unique_ptr<cmCompiledGeneratorExpression> ge;
  67. cmLinkImplItem const& LinkImplItem;
  68. };
  69. cmLinkImplItem cmGeneratorTarget::TargetPropertyEntry::NoLinkImplItem;
  70. void CreatePropertyGeneratorExpressions(
  71. cmStringRange entries, cmBacktraceRange backtraces,
  72. std::vector<cmGeneratorTarget::TargetPropertyEntry*>& items,
  73. bool evaluateForBuildsystem = false)
  74. {
  75. std::vector<cmListFileBacktrace>::const_iterator btIt = backtraces.begin();
  76. for (std::vector<std::string>::const_iterator it = entries.begin();
  77. it != entries.end(); ++it, ++btIt) {
  78. cmGeneratorExpression ge(*btIt);
  79. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(*it);
  80. cge->SetEvaluateForBuildsystem(evaluateForBuildsystem);
  81. items.push_back(
  82. new cmGeneratorTarget::TargetPropertyEntry(std::move(cge)));
  83. }
  84. }
  85. cmGeneratorTarget::cmGeneratorTarget(cmTarget* t, cmLocalGenerator* lg)
  86. : Target(t)
  87. , FortranModuleDirectoryCreated(false)
  88. , SourceFileFlagsConstructed(false)
  89. , PolicyWarnedCMP0022(false)
  90. , PolicyReportedCMP0069(false)
  91. , DebugIncludesDone(false)
  92. , DebugCompileOptionsDone(false)
  93. , DebugCompileFeaturesDone(false)
  94. , DebugCompileDefinitionsDone(false)
  95. , DebugLinkOptionsDone(false)
  96. , DebugSourcesDone(false)
  97. , LinkImplementationLanguageIsContextDependent(true)
  98. , UtilityItemsDone(false)
  99. {
  100. this->Makefile = this->Target->GetMakefile();
  101. this->LocalGenerator = lg;
  102. this->GlobalGenerator = this->LocalGenerator->GetGlobalGenerator();
  103. this->GlobalGenerator->ComputeTargetObjectDirectory(this);
  104. CreatePropertyGeneratorExpressions(t->GetIncludeDirectoriesEntries(),
  105. t->GetIncludeDirectoriesBacktraces(),
  106. this->IncludeDirectoriesEntries);
  107. CreatePropertyGeneratorExpressions(t->GetCompileOptionsEntries(),
  108. t->GetCompileOptionsBacktraces(),
  109. this->CompileOptionsEntries);
  110. CreatePropertyGeneratorExpressions(t->GetCompileFeaturesEntries(),
  111. t->GetCompileFeaturesBacktraces(),
  112. this->CompileFeaturesEntries);
  113. CreatePropertyGeneratorExpressions(t->GetCompileDefinitionsEntries(),
  114. t->GetCompileDefinitionsBacktraces(),
  115. this->CompileDefinitionsEntries);
  116. CreatePropertyGeneratorExpressions(t->GetLinkOptionsEntries(),
  117. t->GetLinkOptionsBacktraces(),
  118. this->LinkOptionsEntries);
  119. CreatePropertyGeneratorExpressions(t->GetSourceEntries(),
  120. t->GetSourceBacktraces(),
  121. this->SourceEntries, true);
  122. this->DLLPlatform =
  123. strcmp(this->Makefile->GetSafeDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX"),
  124. "") != 0;
  125. this->PolicyMap = t->PolicyMap;
  126. }
  127. cmGeneratorTarget::~cmGeneratorTarget()
  128. {
  129. cmDeleteAll(this->IncludeDirectoriesEntries);
  130. cmDeleteAll(this->CompileOptionsEntries);
  131. cmDeleteAll(this->CompileFeaturesEntries);
  132. cmDeleteAll(this->CompileDefinitionsEntries);
  133. cmDeleteAll(this->LinkOptionsEntries);
  134. cmDeleteAll(this->SourceEntries);
  135. cmDeleteAll(this->LinkInformation);
  136. }
  137. const char* cmGeneratorTarget::GetSourcesProperty() const
  138. {
  139. std::vector<std::string> values;
  140. for (TargetPropertyEntry* se : this->SourceEntries) {
  141. values.push_back(se->ge->GetInput());
  142. }
  143. static std::string value;
  144. value.clear();
  145. value = cmJoin(values, ";");
  146. return value.c_str();
  147. }
  148. cmGlobalGenerator* cmGeneratorTarget::GetGlobalGenerator() const
  149. {
  150. return this->GetLocalGenerator()->GetGlobalGenerator();
  151. }
  152. cmLocalGenerator* cmGeneratorTarget::GetLocalGenerator() const
  153. {
  154. return this->LocalGenerator;
  155. }
  156. cmStateEnums::TargetType cmGeneratorTarget::GetType() const
  157. {
  158. return this->Target->GetType();
  159. }
  160. const std::string& cmGeneratorTarget::GetName() const
  161. {
  162. return this->Target->GetName();
  163. }
  164. std::string cmGeneratorTarget::GetExportName() const
  165. {
  166. const char* exportName = this->GetProperty("EXPORT_NAME");
  167. if (exportName && *exportName) {
  168. if (!cmGeneratorExpression::IsValidTargetName(exportName)) {
  169. std::ostringstream e;
  170. e << "EXPORT_NAME property \"" << exportName << "\" for \""
  171. << this->GetName() << "\": is not valid.";
  172. cmSystemTools::Error(e.str().c_str());
  173. return "";
  174. }
  175. return exportName;
  176. }
  177. return this->GetName();
  178. }
  179. const char* cmGeneratorTarget::GetProperty(const std::string& prop) const
  180. {
  181. if (!cmTargetPropertyComputer::PassesWhitelist(
  182. this->GetType(), prop, this->Makefile->GetMessenger(),
  183. this->GetBacktrace())) {
  184. return nullptr;
  185. }
  186. if (const char* result = cmTargetPropertyComputer::GetProperty(
  187. this, prop, this->Makefile->GetMessenger(), this->GetBacktrace())) {
  188. return result;
  189. }
  190. if (cmSystemTools::GetFatalErrorOccured()) {
  191. return nullptr;
  192. }
  193. return this->Target->GetProperty(prop);
  194. }
  195. const char* cmGeneratorTarget::GetOutputTargetType(
  196. cmStateEnums::ArtifactType artifact) const
  197. {
  198. switch (this->GetType()) {
  199. case cmStateEnums::SHARED_LIBRARY:
  200. if (this->IsDLLPlatform()) {
  201. switch (artifact) {
  202. case cmStateEnums::RuntimeBinaryArtifact:
  203. // A DLL shared library is treated as a runtime target.
  204. return "RUNTIME";
  205. case cmStateEnums::ImportLibraryArtifact:
  206. // A DLL import library is treated as an archive target.
  207. return "ARCHIVE";
  208. }
  209. } else {
  210. // For non-DLL platforms shared libraries are treated as
  211. // library targets.
  212. return "LIBRARY";
  213. }
  214. break;
  215. case cmStateEnums::STATIC_LIBRARY:
  216. // Static libraries are always treated as archive targets.
  217. return "ARCHIVE";
  218. case cmStateEnums::MODULE_LIBRARY:
  219. switch (artifact) {
  220. case cmStateEnums::RuntimeBinaryArtifact:
  221. // Module libraries are always treated as library targets.
  222. return "LIBRARY";
  223. case cmStateEnums::ImportLibraryArtifact:
  224. // Module import libraries are treated as archive targets.
  225. return "ARCHIVE";
  226. }
  227. break;
  228. case cmStateEnums::OBJECT_LIBRARY:
  229. // Object libraries are always treated as object targets.
  230. return "OBJECT";
  231. case cmStateEnums::EXECUTABLE:
  232. switch (artifact) {
  233. case cmStateEnums::RuntimeBinaryArtifact:
  234. // Executables are always treated as runtime targets.
  235. return "RUNTIME";
  236. case cmStateEnums::ImportLibraryArtifact:
  237. // Executable import libraries are treated as archive targets.
  238. return "ARCHIVE";
  239. }
  240. break;
  241. default:
  242. break;
  243. }
  244. return "";
  245. }
  246. std::string cmGeneratorTarget::GetOutputName(
  247. const std::string& config, cmStateEnums::ArtifactType artifact) const
  248. {
  249. // Lookup/compute/cache the output name for this configuration.
  250. OutputNameKey key(config, artifact);
  251. cmGeneratorTarget::OutputNameMapType::iterator i =
  252. this->OutputNameMap.find(key);
  253. if (i == this->OutputNameMap.end()) {
  254. // Add empty name in map to detect potential recursion.
  255. OutputNameMapType::value_type entry(key, "");
  256. i = this->OutputNameMap.insert(entry).first;
  257. // Compute output name.
  258. std::vector<std::string> props;
  259. std::string type = this->GetOutputTargetType(artifact);
  260. std::string configUpper = cmSystemTools::UpperCase(config);
  261. if (!type.empty() && !configUpper.empty()) {
  262. // <ARCHIVE|LIBRARY|RUNTIME>_OUTPUT_NAME_<CONFIG>
  263. props.push_back(type + "_OUTPUT_NAME_" + configUpper);
  264. }
  265. if (!type.empty()) {
  266. // <ARCHIVE|LIBRARY|RUNTIME>_OUTPUT_NAME
  267. props.push_back(type + "_OUTPUT_NAME");
  268. }
  269. if (!configUpper.empty()) {
  270. // OUTPUT_NAME_<CONFIG>
  271. props.push_back("OUTPUT_NAME_" + configUpper);
  272. // <CONFIG>_OUTPUT_NAME
  273. props.push_back(configUpper + "_OUTPUT_NAME");
  274. }
  275. // OUTPUT_NAME
  276. props.push_back("OUTPUT_NAME");
  277. std::string outName;
  278. for (std::string const& p : props) {
  279. if (const char* outNameProp = this->GetProperty(p)) {
  280. outName = outNameProp;
  281. break;
  282. }
  283. }
  284. if (outName.empty()) {
  285. outName = this->GetName();
  286. }
  287. // Now evaluate genex and update the previously-prepared map entry.
  288. cmGeneratorExpression ge;
  289. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(outName);
  290. i->second = cge->Evaluate(this->LocalGenerator, config);
  291. } else if (i->second.empty()) {
  292. // An empty map entry indicates we have been called recursively
  293. // from the above block.
  294. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  295. cmake::FATAL_ERROR,
  296. "Target '" + this->GetName() + "' OUTPUT_NAME depends on itself.",
  297. this->GetBacktrace());
  298. }
  299. return i->second;
  300. }
  301. void cmGeneratorTarget::ClearSourcesCache()
  302. {
  303. this->KindedSourcesMap.clear();
  304. this->LinkImplementationLanguageIsContextDependent = true;
  305. this->Objects.clear();
  306. }
  307. void cmGeneratorTarget::AddSourceCommon(const std::string& src)
  308. {
  309. cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
  310. cmGeneratorExpression ge(lfbt);
  311. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(src);
  312. cge->SetEvaluateForBuildsystem(true);
  313. this->SourceEntries.push_back(new TargetPropertyEntry(std::move(cge)));
  314. this->ClearSourcesCache();
  315. }
  316. void cmGeneratorTarget::AddSource(const std::string& src)
  317. {
  318. this->Target->AddSource(src);
  319. this->AddSourceCommon(src);
  320. }
  321. void cmGeneratorTarget::AddTracedSources(std::vector<std::string> const& srcs)
  322. {
  323. this->Target->AddTracedSources(srcs);
  324. if (!srcs.empty()) {
  325. this->AddSourceCommon(cmJoin(srcs, ";"));
  326. }
  327. }
  328. void cmGeneratorTarget::AddIncludeDirectory(const std::string& src,
  329. bool before)
  330. {
  331. this->Target->InsertInclude(src, this->Makefile->GetBacktrace(), before);
  332. cmListFileBacktrace lfbt = this->Makefile->GetBacktrace();
  333. cmGeneratorExpression ge(lfbt);
  334. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(src);
  335. cge->SetEvaluateForBuildsystem(true);
  336. // Insert before begin/end
  337. std::vector<TargetPropertyEntry*>::iterator pos = before
  338. ? this->IncludeDirectoriesEntries.begin()
  339. : this->IncludeDirectoriesEntries.end();
  340. this->IncludeDirectoriesEntries.insert(
  341. pos, new TargetPropertyEntry(std::move(cge)));
  342. }
  343. std::vector<cmSourceFile*> const* cmGeneratorTarget::GetSourceDepends(
  344. cmSourceFile const* sf) const
  345. {
  346. SourceEntriesType::const_iterator i = this->SourceDepends.find(sf);
  347. if (i != this->SourceDepends.end()) {
  348. return &i->second.Depends;
  349. }
  350. return nullptr;
  351. }
  352. static void handleSystemIncludesDep(
  353. cmLocalGenerator* lg, cmGeneratorTarget const* depTgt,
  354. const std::string& config, cmGeneratorTarget const* headTarget,
  355. cmGeneratorExpressionDAGChecker* dagChecker,
  356. std::vector<std::string>& result, bool excludeImported,
  357. std::string const& language)
  358. {
  359. if (const char* dirs =
  360. depTgt->GetProperty("INTERFACE_SYSTEM_INCLUDE_DIRECTORIES")) {
  361. cmGeneratorExpression ge;
  362. cmSystemTools::ExpandListArgument(
  363. ge.Parse(dirs)->Evaluate(lg, config, false, headTarget, depTgt,
  364. dagChecker, language),
  365. result);
  366. }
  367. if (!depTgt->IsImported() || excludeImported) {
  368. return;
  369. }
  370. if (const char* dirs =
  371. depTgt->GetProperty("INTERFACE_INCLUDE_DIRECTORIES")) {
  372. cmGeneratorExpression ge;
  373. cmSystemTools::ExpandListArgument(
  374. ge.Parse(dirs)->Evaluate(lg, config, false, headTarget, depTgt,
  375. dagChecker, language),
  376. result);
  377. }
  378. }
  379. /* clang-format off */
  380. #define IMPLEMENT_VISIT(KIND) \
  381. { \
  382. KindedSources const& kinded = this->GetKindedSources(config); \
  383. for (SourceAndKind const& s : kinded.Sources) { \
  384. if (s.Kind == KIND) { \
  385. data.push_back(s.Source); \
  386. } \
  387. } \
  388. }
  389. /* clang-format on */
  390. void cmGeneratorTarget::GetObjectSources(
  391. std::vector<cmSourceFile const*>& data, const std::string& config) const
  392. {
  393. IMPLEMENT_VISIT(SourceKindObjectSource);
  394. if (!this->Objects.empty()) {
  395. return;
  396. }
  397. for (cmSourceFile const* it : data) {
  398. this->Objects[it];
  399. }
  400. this->LocalGenerator->ComputeObjectFilenames(this->Objects, this);
  401. }
  402. void cmGeneratorTarget::ComputeObjectMapping()
  403. {
  404. if (!this->Objects.empty()) {
  405. return;
  406. }
  407. std::vector<std::string> configs;
  408. this->Makefile->GetConfigurations(configs);
  409. if (configs.empty()) {
  410. configs.emplace_back();
  411. }
  412. for (std::string const& c : configs) {
  413. std::vector<cmSourceFile const*> sourceFiles;
  414. this->GetObjectSources(sourceFiles, c);
  415. }
  416. }
  417. const char* cmGeneratorTarget::GetFeature(const std::string& feature,
  418. const std::string& config) const
  419. {
  420. if (!config.empty()) {
  421. std::string featureConfig = feature;
  422. featureConfig += "_";
  423. featureConfig += cmSystemTools::UpperCase(config);
  424. if (const char* value = this->GetProperty(featureConfig)) {
  425. return value;
  426. }
  427. }
  428. if (const char* value = this->GetProperty(feature)) {
  429. return value;
  430. }
  431. return this->LocalGenerator->GetFeature(feature, config);
  432. }
  433. bool cmGeneratorTarget::IsIPOEnabled(std::string const& lang,
  434. std::string const& config) const
  435. {
  436. const char* feature = "INTERPROCEDURAL_OPTIMIZATION";
  437. const bool result = cmSystemTools::IsOn(this->GetFeature(feature, config));
  438. if (!result) {
  439. // 'INTERPROCEDURAL_OPTIMIZATION' is off, no need to check policies
  440. return false;
  441. }
  442. if (lang != "C" && lang != "CXX" && lang != "Fortran") {
  443. // We do not define IPO behavior for other languages.
  444. return false;
  445. }
  446. cmPolicies::PolicyStatus cmp0069 = this->GetPolicyStatusCMP0069();
  447. if (cmp0069 == cmPolicies::OLD || cmp0069 == cmPolicies::WARN) {
  448. if (this->Makefile->IsOn("_CMAKE_" + lang + "_IPO_LEGACY_BEHAVIOR")) {
  449. return true;
  450. }
  451. if (this->PolicyReportedCMP0069) {
  452. // problem is already reported, no need to issue a message
  453. return false;
  454. }
  455. const bool in_try_compile =
  456. this->LocalGenerator->GetCMakeInstance()->GetIsInTryCompile();
  457. if (cmp0069 == cmPolicies::WARN && !in_try_compile) {
  458. std::ostringstream w;
  459. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0069) << "\n";
  460. w << "INTERPROCEDURAL_OPTIMIZATION property will be ignored for target "
  461. << "'" << this->GetName() << "'.";
  462. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  463. cmake::AUTHOR_WARNING, w.str(), this->GetBacktrace());
  464. this->PolicyReportedCMP0069 = true;
  465. }
  466. return false;
  467. }
  468. // Note: check consistency with messages from CheckIPOSupported
  469. const char* message = nullptr;
  470. if (!this->Makefile->IsOn("_CMAKE_" + lang + "_IPO_SUPPORTED_BY_CMAKE")) {
  471. message = "CMake doesn't support IPO for current compiler";
  472. } else if (!this->Makefile->IsOn("_CMAKE_" + lang +
  473. "_IPO_MAY_BE_SUPPORTED_BY_COMPILER")) {
  474. message = "Compiler doesn't support IPO";
  475. } else if (!this->GlobalGenerator->IsIPOSupported()) {
  476. message = "CMake doesn't support IPO for current generator";
  477. }
  478. if (!message) {
  479. // No error/warning messages
  480. return true;
  481. }
  482. if (this->PolicyReportedCMP0069) {
  483. // problem is already reported, no need to issue a message
  484. return false;
  485. }
  486. this->PolicyReportedCMP0069 = true;
  487. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  488. cmake::FATAL_ERROR, message, this->GetBacktrace());
  489. return false;
  490. }
  491. const std::string& cmGeneratorTarget::GetObjectName(cmSourceFile const* file)
  492. {
  493. this->ComputeObjectMapping();
  494. return this->Objects[file];
  495. }
  496. const char* cmGeneratorTarget::GetCustomObjectExtension() const
  497. {
  498. static std::string extension;
  499. const bool has_ptx_extension =
  500. this->GetPropertyAsBool("CUDA_PTX_COMPILATION");
  501. if (has_ptx_extension) {
  502. extension = ".ptx";
  503. return extension.c_str();
  504. }
  505. return nullptr;
  506. }
  507. void cmGeneratorTarget::AddExplicitObjectName(cmSourceFile const* sf)
  508. {
  509. this->ExplicitObjectName.insert(sf);
  510. }
  511. bool cmGeneratorTarget::HasExplicitObjectName(cmSourceFile const* file) const
  512. {
  513. const_cast<cmGeneratorTarget*>(this)->ComputeObjectMapping();
  514. std::set<cmSourceFile const*>::const_iterator it =
  515. this->ExplicitObjectName.find(file);
  516. return it != this->ExplicitObjectName.end();
  517. }
  518. void cmGeneratorTarget::GetModuleDefinitionSources(
  519. std::vector<cmSourceFile const*>& data, const std::string& config) const
  520. {
  521. IMPLEMENT_VISIT(SourceKindModuleDefinition);
  522. }
  523. void cmGeneratorTarget::GetHeaderSources(
  524. std::vector<cmSourceFile const*>& data, const std::string& config) const
  525. {
  526. IMPLEMENT_VISIT(SourceKindHeader);
  527. }
  528. void cmGeneratorTarget::GetExtraSources(std::vector<cmSourceFile const*>& data,
  529. const std::string& config) const
  530. {
  531. IMPLEMENT_VISIT(SourceKindExtra);
  532. }
  533. void cmGeneratorTarget::GetCustomCommands(
  534. std::vector<cmSourceFile const*>& data, const std::string& config) const
  535. {
  536. IMPLEMENT_VISIT(SourceKindCustomCommand);
  537. }
  538. void cmGeneratorTarget::GetExternalObjects(
  539. std::vector<cmSourceFile const*>& data, const std::string& config) const
  540. {
  541. IMPLEMENT_VISIT(SourceKindExternalObject);
  542. }
  543. void cmGeneratorTarget::GetExpectedResxHeaders(std::set<std::string>& headers,
  544. const std::string& config) const
  545. {
  546. KindedSources const& kinded = this->GetKindedSources(config);
  547. headers = kinded.ExpectedResxHeaders;
  548. }
  549. void cmGeneratorTarget::GetResxSources(std::vector<cmSourceFile const*>& data,
  550. const std::string& config) const
  551. {
  552. IMPLEMENT_VISIT(SourceKindResx);
  553. }
  554. void cmGeneratorTarget::GetAppManifest(std::vector<cmSourceFile const*>& data,
  555. const std::string& config) const
  556. {
  557. IMPLEMENT_VISIT(SourceKindAppManifest);
  558. }
  559. void cmGeneratorTarget::GetManifests(std::vector<cmSourceFile const*>& data,
  560. const std::string& config) const
  561. {
  562. IMPLEMENT_VISIT(SourceKindManifest);
  563. }
  564. void cmGeneratorTarget::GetCertificates(std::vector<cmSourceFile const*>& data,
  565. const std::string& config) const
  566. {
  567. IMPLEMENT_VISIT(SourceKindCertificate);
  568. }
  569. void cmGeneratorTarget::GetExpectedXamlHeaders(std::set<std::string>& headers,
  570. const std::string& config) const
  571. {
  572. KindedSources const& kinded = this->GetKindedSources(config);
  573. headers = kinded.ExpectedXamlHeaders;
  574. }
  575. void cmGeneratorTarget::GetExpectedXamlSources(std::set<std::string>& srcs,
  576. const std::string& config) const
  577. {
  578. KindedSources const& kinded = this->GetKindedSources(config);
  579. srcs = kinded.ExpectedXamlSources;
  580. }
  581. std::set<cmLinkItem> const& cmGeneratorTarget::GetUtilityItems() const
  582. {
  583. if (!this->UtilityItemsDone) {
  584. this->UtilityItemsDone = true;
  585. std::set<std::string> const& utilities = this->GetUtilities();
  586. for (std::string const& i : utilities) {
  587. cmGeneratorTarget* gt =
  588. this->LocalGenerator->FindGeneratorTargetToUse(i);
  589. this->UtilityItems.insert(cmLinkItem(i, gt));
  590. }
  591. }
  592. return this->UtilityItems;
  593. }
  594. void cmGeneratorTarget::GetXamlSources(std::vector<cmSourceFile const*>& data,
  595. const std::string& config) const
  596. {
  597. IMPLEMENT_VISIT(SourceKindXaml);
  598. }
  599. const char* cmGeneratorTarget::GetLocation(const std::string& config) const
  600. {
  601. static std::string location;
  602. if (this->IsImported()) {
  603. location = this->Target->ImportedGetFullPath(
  604. config, cmStateEnums::RuntimeBinaryArtifact);
  605. } else {
  606. location = this->GetFullPath(config, cmStateEnums::RuntimeBinaryArtifact);
  607. }
  608. return location.c_str();
  609. }
  610. std::vector<cmCustomCommand> const& cmGeneratorTarget::GetPreBuildCommands()
  611. const
  612. {
  613. return this->Target->GetPreBuildCommands();
  614. }
  615. std::vector<cmCustomCommand> const& cmGeneratorTarget::GetPreLinkCommands()
  616. const
  617. {
  618. return this->Target->GetPreLinkCommands();
  619. }
  620. std::vector<cmCustomCommand> const& cmGeneratorTarget::GetPostBuildCommands()
  621. const
  622. {
  623. return this->Target->GetPostBuildCommands();
  624. }
  625. bool cmGeneratorTarget::IsImported() const
  626. {
  627. return this->Target->IsImported();
  628. }
  629. bool cmGeneratorTarget::IsImportedGloballyVisible() const
  630. {
  631. return this->Target->IsImportedGloballyVisible();
  632. }
  633. const char* cmGeneratorTarget::GetLocationForBuild() const
  634. {
  635. static std::string location;
  636. if (this->IsImported()) {
  637. location = this->Target->ImportedGetFullPath(
  638. "", cmStateEnums::RuntimeBinaryArtifact);
  639. return location.c_str();
  640. }
  641. // Now handle the deprecated build-time configuration location.
  642. location = this->GetDirectory();
  643. const char* cfgid = this->Makefile->GetDefinition("CMAKE_CFG_INTDIR");
  644. if (cfgid && strcmp(cfgid, ".") != 0) {
  645. location += "/";
  646. location += cfgid;
  647. }
  648. if (this->IsAppBundleOnApple()) {
  649. std::string macdir = this->BuildBundleDirectory("", "", FullLevel);
  650. if (!macdir.empty()) {
  651. location += "/";
  652. location += macdir;
  653. }
  654. }
  655. location += "/";
  656. location += this->GetFullName("", cmStateEnums::RuntimeBinaryArtifact);
  657. return location.c_str();
  658. }
  659. bool cmGeneratorTarget::IsSystemIncludeDirectory(
  660. const std::string& dir, const std::string& config,
  661. const std::string& language) const
  662. {
  663. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  664. std::string config_upper;
  665. if (!config.empty()) {
  666. config_upper = cmSystemTools::UpperCase(config);
  667. }
  668. typedef std::map<std::string, std::vector<std::string>> IncludeCacheType;
  669. IncludeCacheType::const_iterator iter =
  670. this->SystemIncludesCache.find(config_upper);
  671. if (iter == this->SystemIncludesCache.end()) {
  672. cmGeneratorExpressionDAGChecker dagChecker(
  673. this->GetName(), "SYSTEM_INCLUDE_DIRECTORIES", nullptr, nullptr);
  674. bool excludeImported = this->GetPropertyAsBool("NO_SYSTEM_FROM_IMPORTED");
  675. std::vector<std::string> result;
  676. for (std::string const& it : this->Target->GetSystemIncludeDirectories()) {
  677. cmGeneratorExpression ge;
  678. cmSystemTools::ExpandListArgument(
  679. ge.Parse(it)->Evaluate(this->LocalGenerator, config, false, this,
  680. &dagChecker, language),
  681. result);
  682. }
  683. std::vector<cmGeneratorTarget const*> const& deps =
  684. this->GetLinkImplementationClosure(config);
  685. for (cmGeneratorTarget const* dep : deps) {
  686. handleSystemIncludesDep(this->LocalGenerator, dep, config, this,
  687. &dagChecker, result, excludeImported, language);
  688. }
  689. std::for_each(result.begin(), result.end(),
  690. cmSystemTools::ConvertToUnixSlashes);
  691. std::sort(result.begin(), result.end());
  692. result.erase(std::unique(result.begin(), result.end()), result.end());
  693. IncludeCacheType::value_type entry(config_upper, result);
  694. iter = this->SystemIncludesCache.insert(entry).first;
  695. }
  696. return std::binary_search(iter->second.begin(), iter->second.end(), dir);
  697. }
  698. bool cmGeneratorTarget::GetPropertyAsBool(const std::string& prop) const
  699. {
  700. return this->Target->GetPropertyAsBool(prop);
  701. }
  702. static void AddInterfaceEntries(
  703. cmGeneratorTarget const* thisTarget, std::string const& config,
  704. std::string const& prop,
  705. std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries)
  706. {
  707. if (cmLinkImplementationLibraries const* impl =
  708. thisTarget->GetLinkImplementationLibraries(config)) {
  709. for (cmLinkImplItem const& lib : impl->Libraries) {
  710. if (lib.Target) {
  711. std::string genex = "$<TARGET_PROPERTY:" + lib + "," + prop + ">";
  712. cmGeneratorExpression ge(lib.Backtrace);
  713. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(genex);
  714. cge->SetEvaluateForBuildsystem(true);
  715. entries.push_back(
  716. new cmGeneratorTarget::TargetPropertyEntry(std::move(cge), lib));
  717. }
  718. }
  719. }
  720. }
  721. static void AddObjectEntries(
  722. cmGeneratorTarget const* thisTarget, std::string const& config,
  723. std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries)
  724. {
  725. if (cmLinkImplementationLibraries const* impl =
  726. thisTarget->GetLinkImplementationLibraries(config)) {
  727. for (cmLinkImplItem const& lib : impl->Libraries) {
  728. if (lib.Target &&
  729. lib.Target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  730. std::string genex = "$<TARGET_OBJECTS:" + lib + ">";
  731. cmGeneratorExpression ge(lib.Backtrace);
  732. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(genex);
  733. cge->SetEvaluateForBuildsystem(true);
  734. entries.push_back(
  735. new cmGeneratorTarget::TargetPropertyEntry(std::move(cge), lib));
  736. }
  737. }
  738. }
  739. }
  740. static bool processSources(
  741. cmGeneratorTarget const* tgt,
  742. const std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries,
  743. std::vector<std::string>& srcs, std::unordered_set<std::string>& uniqueSrcs,
  744. cmGeneratorExpressionDAGChecker* dagChecker, std::string const& config,
  745. bool debugSources)
  746. {
  747. cmMakefile* mf = tgt->Target->GetMakefile();
  748. bool contextDependent = false;
  749. for (cmGeneratorTarget::TargetPropertyEntry* entry : entries) {
  750. cmLinkImplItem const& item = entry->LinkImplItem;
  751. std::string const& targetName = item;
  752. std::vector<std::string> entrySources;
  753. cmSystemTools::ExpandListArgument(
  754. entry->ge->Evaluate(tgt->GetLocalGenerator(), config, false, tgt, tgt,
  755. dagChecker),
  756. entrySources);
  757. if (entry->ge->GetHadContextSensitiveCondition()) {
  758. contextDependent = true;
  759. }
  760. for (std::string& src : entrySources) {
  761. cmSourceFile* sf = mf->GetOrCreateSource(src);
  762. std::string e;
  763. std::string fullPath = sf->GetFullPath(&e);
  764. if (fullPath.empty()) {
  765. if (!e.empty()) {
  766. cmake* cm = tgt->GetLocalGenerator()->GetCMakeInstance();
  767. cm->IssueMessage(cmake::FATAL_ERROR, e, tgt->GetBacktrace());
  768. }
  769. return contextDependent;
  770. }
  771. if (!targetName.empty() && !cmSystemTools::FileIsFullPath(src)) {
  772. std::ostringstream err;
  773. if (!targetName.empty()) {
  774. err << "Target \"" << targetName
  775. << "\" contains relative path in its INTERFACE_SOURCES:\n \""
  776. << src << "\"";
  777. } else {
  778. err << "Found relative path while evaluating sources of \""
  779. << tgt->GetName() << "\":\n \"" << src << "\"\n";
  780. }
  781. tgt->GetLocalGenerator()->IssueMessage(cmake::FATAL_ERROR, err.str());
  782. return contextDependent;
  783. }
  784. src = fullPath;
  785. }
  786. std::string usedSources;
  787. for (std::string const& src : entrySources) {
  788. if (uniqueSrcs.insert(src).second) {
  789. srcs.push_back(src);
  790. if (debugSources) {
  791. usedSources += " * " + src + "\n";
  792. }
  793. }
  794. }
  795. if (!usedSources.empty()) {
  796. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  797. cmake::LOG,
  798. std::string("Used sources for target ") + tgt->GetName() + ":\n" +
  799. usedSources,
  800. entry->ge->GetBacktrace());
  801. }
  802. }
  803. return contextDependent;
  804. }
  805. void cmGeneratorTarget::GetSourceFiles(std::vector<std::string>& files,
  806. const std::string& config) const
  807. {
  808. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  809. if (!this->LocalGenerator->GetGlobalGenerator()->GetConfigureDoneCMP0026()) {
  810. // At configure-time, this method can be called as part of getting the
  811. // LOCATION property or to export() a file to be include()d. However
  812. // there is no cmGeneratorTarget at configure-time, so search the SOURCES
  813. // for TARGET_OBJECTS instead for backwards compatibility with OLD
  814. // behavior of CMP0024 and CMP0026 only.
  815. cmStringRange sourceEntries = this->Target->GetSourceEntries();
  816. for (std::string const& entry : sourceEntries) {
  817. std::vector<std::string> items;
  818. cmSystemTools::ExpandListArgument(entry, items);
  819. for (std::string const& item : items) {
  820. if (cmHasLiteralPrefix(item, "$<TARGET_OBJECTS:") &&
  821. item[item.size() - 1] == '>') {
  822. continue;
  823. }
  824. files.push_back(item);
  825. }
  826. }
  827. return;
  828. }
  829. std::vector<std::string> debugProperties;
  830. const char* debugProp =
  831. this->Makefile->GetDefinition("CMAKE_DEBUG_TARGET_PROPERTIES");
  832. if (debugProp) {
  833. cmSystemTools::ExpandListArgument(debugProp, debugProperties);
  834. }
  835. bool debugSources = !this->DebugSourcesDone &&
  836. std::find(debugProperties.begin(), debugProperties.end(), "SOURCES") !=
  837. debugProperties.end();
  838. if (this->LocalGenerator->GetGlobalGenerator()->GetConfigureDoneCMP0026()) {
  839. this->DebugSourcesDone = true;
  840. }
  841. cmGeneratorExpressionDAGChecker dagChecker(this->GetName(), "SOURCES",
  842. nullptr, nullptr);
  843. std::unordered_set<std::string> uniqueSrcs;
  844. bool contextDependentDirectSources =
  845. processSources(this, this->SourceEntries, files, uniqueSrcs, &dagChecker,
  846. config, debugSources);
  847. // Collect INTERFACE_SOURCES of all direct link-dependencies.
  848. std::vector<cmGeneratorTarget::TargetPropertyEntry*>
  849. linkInterfaceSourcesEntries;
  850. AddInterfaceEntries(this, config, "INTERFACE_SOURCES",
  851. linkInterfaceSourcesEntries);
  852. std::vector<std::string>::size_type numFilesBefore = files.size();
  853. bool contextDependentInterfaceSources =
  854. processSources(this, linkInterfaceSourcesEntries, files, uniqueSrcs,
  855. &dagChecker, config, debugSources);
  856. // Collect TARGET_OBJECTS of direct object link-dependencies.
  857. std::vector<cmGeneratorTarget::TargetPropertyEntry*> linkObjectsEntries;
  858. AddObjectEntries(this, config, linkObjectsEntries);
  859. std::vector<std::string>::size_type numFilesBefore2 = files.size();
  860. bool contextDependentObjects =
  861. processSources(this, linkObjectsEntries, files, uniqueSrcs, &dagChecker,
  862. config, debugSources);
  863. if (!contextDependentDirectSources &&
  864. !(contextDependentInterfaceSources && numFilesBefore < files.size()) &&
  865. !(contextDependentObjects && numFilesBefore2 < files.size())) {
  866. this->LinkImplementationLanguageIsContextDependent = false;
  867. }
  868. cmDeleteAll(linkInterfaceSourcesEntries);
  869. cmDeleteAll(linkObjectsEntries);
  870. }
  871. void cmGeneratorTarget::GetSourceFiles(std::vector<cmSourceFile*>& files,
  872. const std::string& config) const
  873. {
  874. if (!this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  875. // Since we are still configuring not all sources may exist yet,
  876. // so we need to avoid full source classification because that
  877. // requires the absolute paths to all sources to be determined.
  878. // Since this is only for compatibility with old policies that
  879. // projects should not depend on anymore, just compute the files
  880. // without memoizing them.
  881. std::vector<std::string> srcs;
  882. this->GetSourceFiles(srcs, config);
  883. std::set<cmSourceFile*> emitted;
  884. for (std::string const& s : srcs) {
  885. cmSourceFile* sf = this->Makefile->GetOrCreateSource(s);
  886. if (emitted.insert(sf).second) {
  887. files.push_back(sf);
  888. }
  889. }
  890. return;
  891. }
  892. KindedSources const& kinded = this->GetKindedSources(config);
  893. files.reserve(kinded.Sources.size());
  894. for (SourceAndKind const& si : kinded.Sources) {
  895. files.push_back(si.Source);
  896. }
  897. }
  898. void cmGeneratorTarget::GetSourceFilesWithoutObjectLibraries(
  899. std::vector<cmSourceFile*>& files, const std::string& config) const
  900. {
  901. KindedSources const& kinded = this->GetKindedSources(config);
  902. files.reserve(kinded.Sources.size());
  903. for (SourceAndKind const& si : kinded.Sources) {
  904. if (si.Source->GetObjectLibrary().empty()) {
  905. files.push_back(si.Source);
  906. }
  907. }
  908. }
  909. cmGeneratorTarget::KindedSources const& cmGeneratorTarget::GetKindedSources(
  910. std::string const& config) const
  911. {
  912. // If we already processed one configuration and found no dependenc
  913. // on configuration then always use the one result.
  914. if (!this->LinkImplementationLanguageIsContextDependent) {
  915. return this->KindedSourcesMap.begin()->second;
  916. }
  917. // Lookup any existing link implementation for this configuration.
  918. std::string const key = cmSystemTools::UpperCase(config);
  919. KindedSourcesMapType::iterator it = this->KindedSourcesMap.find(key);
  920. if (it != this->KindedSourcesMap.end()) {
  921. if (!it->second.Initialized) {
  922. std::ostringstream e;
  923. e << "The SOURCES of \"" << this->GetName()
  924. << "\" use a generator expression that depends on the "
  925. "SOURCES themselves.";
  926. this->GlobalGenerator->GetCMakeInstance()->IssueMessage(
  927. cmake::FATAL_ERROR, e.str(), this->GetBacktrace());
  928. static KindedSources empty;
  929. return empty;
  930. }
  931. return it->second;
  932. }
  933. // Add an entry to the map for this configuration.
  934. KindedSources& files = this->KindedSourcesMap[key];
  935. this->ComputeKindedSources(files, config);
  936. files.Initialized = true;
  937. return files;
  938. }
  939. void cmGeneratorTarget::ComputeKindedSources(KindedSources& files,
  940. std::string const& config) const
  941. {
  942. // Get the source file paths by string.
  943. std::vector<std::string> srcs;
  944. this->GetSourceFiles(srcs, config);
  945. cmsys::RegularExpression header_regex(CM_HEADER_REGEX);
  946. std::vector<cmSourceFile*> badObjLib;
  947. std::set<cmSourceFile*> emitted;
  948. for (std::string const& s : srcs) {
  949. // Create each source at most once.
  950. cmSourceFile* sf = this->Makefile->GetOrCreateSource(s);
  951. if (!emitted.insert(sf).second) {
  952. continue;
  953. }
  954. // Compute the kind (classification) of this source file.
  955. SourceKind kind;
  956. std::string ext = cmSystemTools::LowerCase(sf->GetExtension());
  957. if (sf->GetCustomCommand()) {
  958. kind = SourceKindCustomCommand;
  959. } else if (this->Target->GetType() == cmStateEnums::UTILITY) {
  960. kind = SourceKindExtra;
  961. } else if (sf->GetPropertyAsBool("HEADER_FILE_ONLY")) {
  962. kind = SourceKindHeader;
  963. } else if (sf->GetPropertyAsBool("EXTERNAL_OBJECT")) {
  964. kind = SourceKindExternalObject;
  965. } else if (!sf->GetLanguage().empty()) {
  966. kind = SourceKindObjectSource;
  967. } else if (ext == "def") {
  968. kind = SourceKindModuleDefinition;
  969. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  970. badObjLib.push_back(sf);
  971. }
  972. } else if (ext == "idl") {
  973. kind = SourceKindIDL;
  974. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  975. badObjLib.push_back(sf);
  976. }
  977. } else if (ext == "resx") {
  978. kind = SourceKindResx;
  979. // Build and save the name of the corresponding .h file
  980. // This relationship will be used later when building the project files.
  981. // Both names would have been auto generated from Visual Studio
  982. // where the user supplied the file name and Visual Studio
  983. // appended the suffix.
  984. std::string resx = sf->GetFullPath();
  985. std::string hFileName = resx.substr(0, resx.find_last_of('.')) + ".h";
  986. files.ExpectedResxHeaders.insert(hFileName);
  987. } else if (ext == "appxmanifest") {
  988. kind = SourceKindAppManifest;
  989. } else if (ext == "manifest") {
  990. kind = SourceKindManifest;
  991. } else if (ext == "pfx") {
  992. kind = SourceKindCertificate;
  993. } else if (ext == "xaml") {
  994. kind = SourceKindXaml;
  995. // Build and save the name of the corresponding .h and .cpp file
  996. // This relationship will be used later when building the project files.
  997. // Both names would have been auto generated from Visual Studio
  998. // where the user supplied the file name and Visual Studio
  999. // appended the suffix.
  1000. std::string xaml = sf->GetFullPath();
  1001. std::string hFileName = xaml + ".h";
  1002. std::string cppFileName = xaml + ".cpp";
  1003. files.ExpectedXamlHeaders.insert(hFileName);
  1004. files.ExpectedXamlSources.insert(cppFileName);
  1005. } else if (header_regex.find(sf->GetFullPath().c_str())) {
  1006. kind = SourceKindHeader;
  1007. } else {
  1008. kind = SourceKindExtra;
  1009. }
  1010. // Save this classified source file in the result vector.
  1011. files.Sources.push_back({ sf, kind });
  1012. }
  1013. if (!badObjLib.empty()) {
  1014. std::ostringstream e;
  1015. e << "OBJECT library \"" << this->GetName() << "\" contains:\n";
  1016. for (cmSourceFile* i : badObjLib) {
  1017. e << " " << i->GetLocation().GetName() << "\n";
  1018. }
  1019. e << "but may contain only sources that compile, header files, and "
  1020. "other files that would not affect linking of a normal library.";
  1021. this->GlobalGenerator->GetCMakeInstance()->IssueMessage(
  1022. cmake::FATAL_ERROR, e.str(), this->GetBacktrace());
  1023. }
  1024. }
  1025. std::vector<cmGeneratorTarget::AllConfigSource> const&
  1026. cmGeneratorTarget::GetAllConfigSources() const
  1027. {
  1028. if (this->AllConfigSources.empty()) {
  1029. this->ComputeAllConfigSources();
  1030. }
  1031. return this->AllConfigSources;
  1032. }
  1033. void cmGeneratorTarget::ComputeAllConfigSources() const
  1034. {
  1035. std::vector<std::string> configs;
  1036. this->Makefile->GetConfigurations(configs);
  1037. std::map<cmSourceFile const*, size_t> index;
  1038. for (size_t ci = 0; ci < configs.size(); ++ci) {
  1039. KindedSources const& sources = this->GetKindedSources(configs[ci]);
  1040. for (SourceAndKind const& src : sources.Sources) {
  1041. std::map<cmSourceFile const*, size_t>::iterator mi =
  1042. index.find(src.Source);
  1043. if (mi == index.end()) {
  1044. AllConfigSource acs;
  1045. acs.Source = src.Source;
  1046. acs.Kind = src.Kind;
  1047. this->AllConfigSources.push_back(std::move(acs));
  1048. std::map<cmSourceFile const*, size_t>::value_type entry(
  1049. src.Source, this->AllConfigSources.size() - 1);
  1050. mi = index.insert(entry).first;
  1051. }
  1052. this->AllConfigSources[mi->second].Configs.push_back(ci);
  1053. }
  1054. }
  1055. }
  1056. std::string cmGeneratorTarget::GetCompilePDBName(
  1057. const std::string& config) const
  1058. {
  1059. std::string prefix;
  1060. std::string base;
  1061. std::string suffix;
  1062. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  1063. prefix, base, suffix);
  1064. // Check for a per-configuration output directory target property.
  1065. std::string configUpper = cmSystemTools::UpperCase(config);
  1066. std::string configProp = "COMPILE_PDB_NAME_";
  1067. configProp += configUpper;
  1068. const char* config_name = this->GetProperty(configProp);
  1069. if (config_name && *config_name) {
  1070. return prefix + config_name + ".pdb";
  1071. }
  1072. const char* name = this->GetProperty("COMPILE_PDB_NAME");
  1073. if (name && *name) {
  1074. return prefix + name + ".pdb";
  1075. }
  1076. return "";
  1077. }
  1078. std::string cmGeneratorTarget::GetCompilePDBPath(
  1079. const std::string& config) const
  1080. {
  1081. std::string dir = this->GetCompilePDBDirectory(config);
  1082. std::string name = this->GetCompilePDBName(config);
  1083. if (dir.empty() && !name.empty() && this->HaveWellDefinedOutputFiles()) {
  1084. dir = this->GetPDBDirectory(config);
  1085. }
  1086. if (!dir.empty()) {
  1087. dir += "/";
  1088. }
  1089. return dir + name;
  1090. }
  1091. bool cmGeneratorTarget::HasSOName(const std::string& config) const
  1092. {
  1093. // soname is supported only for shared libraries and modules,
  1094. // and then only when the platform supports an soname flag.
  1095. return ((this->GetType() == cmStateEnums::SHARED_LIBRARY) &&
  1096. !this->GetPropertyAsBool("NO_SONAME") &&
  1097. this->Makefile->GetSONameFlag(this->GetLinkerLanguage(config)));
  1098. }
  1099. bool cmGeneratorTarget::NeedRelinkBeforeInstall(
  1100. const std::string& config) const
  1101. {
  1102. // Only executables and shared libraries can have an rpath and may
  1103. // need relinking.
  1104. if (this->GetType() != cmStateEnums::EXECUTABLE &&
  1105. this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  1106. this->GetType() != cmStateEnums::MODULE_LIBRARY) {
  1107. return false;
  1108. }
  1109. // If there is no install location this target will not be installed
  1110. // and therefore does not need relinking.
  1111. if (!this->Target->GetHaveInstallRule()) {
  1112. return false;
  1113. }
  1114. // If skipping all rpaths completely then no relinking is needed.
  1115. if (this->Makefile->IsOn("CMAKE_SKIP_RPATH")) {
  1116. return false;
  1117. }
  1118. // If building with the install-tree rpath no relinking is needed.
  1119. if (this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH")) {
  1120. return false;
  1121. }
  1122. // If chrpath is going to be used no relinking is needed.
  1123. if (this->IsChrpathUsed(config)) {
  1124. return false;
  1125. }
  1126. // Check for rpath support on this platform.
  1127. std::string ll = this->GetLinkerLanguage(config);
  1128. if (!ll.empty()) {
  1129. std::string flagVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
  1130. flagVar += ll;
  1131. flagVar += "_FLAG";
  1132. if (!this->Makefile->IsSet(flagVar)) {
  1133. // There is no rpath support on this platform so nothing needs
  1134. // relinking.
  1135. return false;
  1136. }
  1137. } else {
  1138. // No linker language is known. This error will be reported by
  1139. // other code.
  1140. return false;
  1141. }
  1142. // If either a build or install tree rpath is set then the rpath
  1143. // will likely change between the build tree and install tree and
  1144. // this target must be relinked.
  1145. bool have_rpath =
  1146. this->HaveBuildTreeRPATH(config) || this->HaveInstallTreeRPATH();
  1147. bool is_ninja =
  1148. this->LocalGenerator->GetGlobalGenerator()->GetName() == "Ninja";
  1149. if (have_rpath && is_ninja) {
  1150. std::ostringstream w;
  1151. /* clang-format off */
  1152. w <<
  1153. "The install of the " << this->GetName() << " target requires "
  1154. "changing an RPATH from the build tree, but this is not supported "
  1155. "with the Ninja generator unless on an ELF-based platform. The "
  1156. "CMAKE_BUILD_WITH_INSTALL_RPATH variable may be set to avoid this "
  1157. "relinking step."
  1158. ;
  1159. /* clang-format on */
  1160. cmake* cm = this->LocalGenerator->GetCMakeInstance();
  1161. cm->IssueMessage(cmake::FATAL_ERROR, w.str(), this->GetBacktrace());
  1162. }
  1163. return have_rpath;
  1164. }
  1165. bool cmGeneratorTarget::IsChrpathUsed(const std::string& config) const
  1166. {
  1167. // Only certain target types have an rpath.
  1168. if (!(this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  1169. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  1170. this->GetType() == cmStateEnums::EXECUTABLE)) {
  1171. return false;
  1172. }
  1173. // If the target will not be installed we do not need to change its
  1174. // rpath.
  1175. if (!this->Target->GetHaveInstallRule()) {
  1176. return false;
  1177. }
  1178. // Skip chrpath if skipping rpath altogether.
  1179. if (this->Makefile->IsOn("CMAKE_SKIP_RPATH")) {
  1180. return false;
  1181. }
  1182. // Skip chrpath if it does not need to be changed at install time.
  1183. if (this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH")) {
  1184. return false;
  1185. }
  1186. // Allow the user to disable builtin chrpath explicitly.
  1187. if (this->Makefile->IsOn("CMAKE_NO_BUILTIN_CHRPATH")) {
  1188. return false;
  1189. }
  1190. if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  1191. return true;
  1192. }
  1193. #if defined(CMAKE_USE_ELF_PARSER)
  1194. // Enable if the rpath flag uses a separator and the target uses ELF
  1195. // binaries.
  1196. std::string ll = this->GetLinkerLanguage(config);
  1197. if (!ll.empty()) {
  1198. std::string sepVar = "CMAKE_SHARED_LIBRARY_RUNTIME_";
  1199. sepVar += ll;
  1200. sepVar += "_FLAG_SEP";
  1201. const char* sep = this->Makefile->GetDefinition(sepVar);
  1202. if (sep && *sep) {
  1203. // TODO: Add ELF check to ABI detection and get rid of
  1204. // CMAKE_EXECUTABLE_FORMAT.
  1205. if (const char* fmt =
  1206. this->Makefile->GetDefinition("CMAKE_EXECUTABLE_FORMAT")) {
  1207. return strcmp(fmt, "ELF") == 0;
  1208. }
  1209. }
  1210. }
  1211. #endif
  1212. static_cast<void>(config);
  1213. return false;
  1214. }
  1215. bool cmGeneratorTarget::IsImportedSharedLibWithoutSOName(
  1216. const std::string& config) const
  1217. {
  1218. if (this->IsImported() && this->GetType() == cmStateEnums::SHARED_LIBRARY) {
  1219. if (cmGeneratorTarget::ImportInfo const* info =
  1220. this->GetImportInfo(config)) {
  1221. return info->NoSOName;
  1222. }
  1223. }
  1224. return false;
  1225. }
  1226. bool cmGeneratorTarget::HasMacOSXRpathInstallNameDir(
  1227. const std::string& config) const
  1228. {
  1229. bool install_name_is_rpath = false;
  1230. bool macosx_rpath = false;
  1231. if (!this->IsImported()) {
  1232. if (this->GetType() != cmStateEnums::SHARED_LIBRARY) {
  1233. return false;
  1234. }
  1235. const char* install_name = this->GetProperty("INSTALL_NAME_DIR");
  1236. bool use_install_name = this->MacOSXUseInstallNameDir();
  1237. if (install_name && use_install_name &&
  1238. std::string(install_name) == "@rpath") {
  1239. install_name_is_rpath = true;
  1240. } else if (install_name && use_install_name) {
  1241. return false;
  1242. }
  1243. if (!install_name_is_rpath) {
  1244. macosx_rpath = this->MacOSXRpathInstallNameDirDefault();
  1245. }
  1246. } else {
  1247. // Lookup the imported soname.
  1248. if (cmGeneratorTarget::ImportInfo const* info =
  1249. this->GetImportInfo(config)) {
  1250. if (!info->NoSOName && !info->SOName.empty()) {
  1251. if (info->SOName.find("@rpath/") == 0) {
  1252. install_name_is_rpath = true;
  1253. }
  1254. } else {
  1255. std::string install_name;
  1256. cmSystemTools::GuessLibraryInstallName(info->Location, install_name);
  1257. if (install_name.find("@rpath") != std::string::npos) {
  1258. install_name_is_rpath = true;
  1259. }
  1260. }
  1261. }
  1262. }
  1263. if (!install_name_is_rpath && !macosx_rpath) {
  1264. return false;
  1265. }
  1266. if (!this->Makefile->IsSet("CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG")) {
  1267. std::ostringstream w;
  1268. w << "Attempting to use ";
  1269. if (macosx_rpath) {
  1270. w << "MACOSX_RPATH";
  1271. } else {
  1272. w << "@rpath";
  1273. }
  1274. w << " without CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG being set.";
  1275. w << " This could be because you are using a Mac OS X version";
  1276. w << " less than 10.5 or because CMake's platform configuration is";
  1277. w << " corrupt.";
  1278. cmake* cm = this->LocalGenerator->GetCMakeInstance();
  1279. cm->IssueMessage(cmake::FATAL_ERROR, w.str(), this->GetBacktrace());
  1280. }
  1281. return true;
  1282. }
  1283. bool cmGeneratorTarget::MacOSXRpathInstallNameDirDefault() const
  1284. {
  1285. // we can't do rpaths when unsupported
  1286. if (!this->Makefile->IsSet("CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG")) {
  1287. return false;
  1288. }
  1289. const char* macosx_rpath_str = this->GetProperty("MACOSX_RPATH");
  1290. if (macosx_rpath_str) {
  1291. return this->GetPropertyAsBool("MACOSX_RPATH");
  1292. }
  1293. cmPolicies::PolicyStatus cmp0042 = this->GetPolicyStatusCMP0042();
  1294. if (cmp0042 == cmPolicies::WARN) {
  1295. this->LocalGenerator->GetGlobalGenerator()->AddCMP0042WarnTarget(
  1296. this->GetName());
  1297. }
  1298. return cmp0042 == cmPolicies::NEW;
  1299. }
  1300. bool cmGeneratorTarget::MacOSXUseInstallNameDir() const
  1301. {
  1302. const char* build_with_install_name =
  1303. this->GetProperty("BUILD_WITH_INSTALL_NAME_DIR");
  1304. if (build_with_install_name) {
  1305. return cmSystemTools::IsOn(build_with_install_name);
  1306. }
  1307. cmPolicies::PolicyStatus cmp0068 = this->GetPolicyStatusCMP0068();
  1308. if (cmp0068 == cmPolicies::NEW) {
  1309. return false;
  1310. }
  1311. bool use_install_name = this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH");
  1312. if (use_install_name && cmp0068 == cmPolicies::WARN) {
  1313. this->LocalGenerator->GetGlobalGenerator()->AddCMP0068WarnTarget(
  1314. this->GetName());
  1315. }
  1316. return use_install_name;
  1317. }
  1318. bool cmGeneratorTarget::CanGenerateInstallNameDir(
  1319. InstallNameType name_type) const
  1320. {
  1321. cmPolicies::PolicyStatus cmp0068 = this->GetPolicyStatusCMP0068();
  1322. if (cmp0068 == cmPolicies::NEW) {
  1323. return true;
  1324. }
  1325. bool skip = this->Makefile->IsOn("CMAKE_SKIP_RPATH");
  1326. if (name_type == INSTALL_NAME_FOR_INSTALL) {
  1327. skip |= this->Makefile->IsOn("CMAKE_SKIP_INSTALL_RPATH");
  1328. } else {
  1329. skip |= this->GetPropertyAsBool("SKIP_BUILD_RPATH");
  1330. }
  1331. if (skip && cmp0068 == cmPolicies::WARN) {
  1332. this->LocalGenerator->GetGlobalGenerator()->AddCMP0068WarnTarget(
  1333. this->GetName());
  1334. }
  1335. return !skip;
  1336. }
  1337. std::string cmGeneratorTarget::GetSOName(const std::string& config) const
  1338. {
  1339. if (this->IsImported()) {
  1340. // Lookup the imported soname.
  1341. if (cmGeneratorTarget::ImportInfo const* info =
  1342. this->GetImportInfo(config)) {
  1343. if (info->NoSOName) {
  1344. // The imported library has no builtin soname so the name
  1345. // searched at runtime will be just the filename.
  1346. return cmSystemTools::GetFilenameName(info->Location);
  1347. }
  1348. // Use the soname given if any.
  1349. if (info->SOName.find("@rpath/") == 0) {
  1350. return info->SOName.substr(6);
  1351. }
  1352. return info->SOName;
  1353. }
  1354. return "";
  1355. }
  1356. // Compute the soname that will be built.
  1357. std::string name;
  1358. std::string soName;
  1359. std::string realName;
  1360. std::string impName;
  1361. std::string pdbName;
  1362. this->GetLibraryNames(name, soName, realName, impName, pdbName, config);
  1363. return soName;
  1364. }
  1365. static bool shouldAddFullLevel(cmGeneratorTarget::BundleDirectoryLevel level)
  1366. {
  1367. return level == cmGeneratorTarget::FullLevel;
  1368. }
  1369. static bool shouldAddContentLevel(
  1370. cmGeneratorTarget::BundleDirectoryLevel level)
  1371. {
  1372. return level == cmGeneratorTarget::ContentLevel || shouldAddFullLevel(level);
  1373. }
  1374. std::string cmGeneratorTarget::GetAppBundleDirectory(
  1375. const std::string& config, BundleDirectoryLevel level) const
  1376. {
  1377. std::string fpath =
  1378. this->GetFullName(config, cmStateEnums::RuntimeBinaryArtifact);
  1379. fpath += ".";
  1380. const char* ext = this->GetProperty("BUNDLE_EXTENSION");
  1381. if (!ext) {
  1382. ext = "app";
  1383. }
  1384. fpath += ext;
  1385. if (shouldAddContentLevel(level) &&
  1386. !this->Makefile->PlatformIsAppleEmbedded()) {
  1387. fpath += "/Contents";
  1388. if (shouldAddFullLevel(level)) {
  1389. fpath += "/MacOS";
  1390. }
  1391. }
  1392. return fpath;
  1393. }
  1394. bool cmGeneratorTarget::IsBundleOnApple() const
  1395. {
  1396. return this->IsFrameworkOnApple() || this->IsAppBundleOnApple() ||
  1397. this->IsCFBundleOnApple();
  1398. }
  1399. std::string cmGeneratorTarget::GetCFBundleDirectory(
  1400. const std::string& config, BundleDirectoryLevel level) const
  1401. {
  1402. std::string fpath;
  1403. fpath += this->GetOutputName(config, cmStateEnums::RuntimeBinaryArtifact);
  1404. fpath += ".";
  1405. const char* ext = this->GetProperty("BUNDLE_EXTENSION");
  1406. if (!ext) {
  1407. if (this->IsXCTestOnApple()) {
  1408. ext = "xctest";
  1409. } else {
  1410. ext = "bundle";
  1411. }
  1412. }
  1413. fpath += ext;
  1414. if (shouldAddContentLevel(level) &&
  1415. !this->Makefile->PlatformIsAppleEmbedded()) {
  1416. fpath += "/Contents";
  1417. if (shouldAddFullLevel(level)) {
  1418. fpath += "/MacOS";
  1419. }
  1420. }
  1421. return fpath;
  1422. }
  1423. std::string cmGeneratorTarget::GetFrameworkDirectory(
  1424. const std::string& config, BundleDirectoryLevel level) const
  1425. {
  1426. std::string fpath;
  1427. fpath += this->GetOutputName(config, cmStateEnums::RuntimeBinaryArtifact);
  1428. fpath += ".";
  1429. const char* ext = this->GetProperty("BUNDLE_EXTENSION");
  1430. if (!ext) {
  1431. ext = "framework";
  1432. }
  1433. fpath += ext;
  1434. if (shouldAddFullLevel(level) &&
  1435. !this->Makefile->PlatformIsAppleEmbedded()) {
  1436. fpath += "/Versions/";
  1437. fpath += this->GetFrameworkVersion();
  1438. }
  1439. return fpath;
  1440. }
  1441. std::string cmGeneratorTarget::GetFullName(
  1442. const std::string& config, cmStateEnums::ArtifactType artifact) const
  1443. {
  1444. if (this->IsImported()) {
  1445. return this->GetFullNameImported(config, artifact);
  1446. }
  1447. return this->GetFullNameInternal(config, artifact);
  1448. }
  1449. std::string cmGeneratorTarget::GetInstallNameDirForBuildTree(
  1450. const std::string& config) const
  1451. {
  1452. if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  1453. // If building directly for installation then the build tree install_name
  1454. // is the same as the install tree.
  1455. if (this->MacOSXUseInstallNameDir()) {
  1456. return this->GetInstallNameDirForInstallTree();
  1457. }
  1458. // Use the build tree directory for the target.
  1459. if (this->CanGenerateInstallNameDir(INSTALL_NAME_FOR_BUILD)) {
  1460. std::string dir;
  1461. if (this->MacOSXRpathInstallNameDirDefault()) {
  1462. dir = "@rpath";
  1463. } else {
  1464. dir = this->GetDirectory(config);
  1465. }
  1466. dir += "/";
  1467. return dir;
  1468. }
  1469. }
  1470. return "";
  1471. }
  1472. std::string cmGeneratorTarget::GetInstallNameDirForInstallTree() const
  1473. {
  1474. if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  1475. std::string dir;
  1476. const char* install_name_dir = this->GetProperty("INSTALL_NAME_DIR");
  1477. if (this->CanGenerateInstallNameDir(INSTALL_NAME_FOR_INSTALL)) {
  1478. if (install_name_dir && *install_name_dir) {
  1479. dir = install_name_dir;
  1480. dir += "/";
  1481. }
  1482. }
  1483. if (!install_name_dir) {
  1484. if (this->MacOSXRpathInstallNameDirDefault()) {
  1485. dir = "@rpath/";
  1486. }
  1487. }
  1488. return dir;
  1489. }
  1490. return "";
  1491. }
  1492. cmListFileBacktrace cmGeneratorTarget::GetBacktrace() const
  1493. {
  1494. return this->Target->GetBacktrace();
  1495. }
  1496. const std::vector<std::string>& cmGeneratorTarget::GetLinkDirectories() const
  1497. {
  1498. return this->Target->GetLinkDirectories();
  1499. }
  1500. const std::set<std::string>& cmGeneratorTarget::GetUtilities() const
  1501. {
  1502. return this->Target->GetUtilities();
  1503. }
  1504. const cmListFileBacktrace* cmGeneratorTarget::GetUtilityBacktrace(
  1505. const std::string& u) const
  1506. {
  1507. return this->Target->GetUtilityBacktrace(u);
  1508. }
  1509. bool cmGeneratorTarget::HaveWellDefinedOutputFiles() const
  1510. {
  1511. return this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  1512. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  1513. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  1514. this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  1515. this->GetType() == cmStateEnums::EXECUTABLE;
  1516. }
  1517. const char* cmGeneratorTarget::GetExportMacro() const
  1518. {
  1519. // Define the symbol for targets that export symbols.
  1520. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  1521. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  1522. this->IsExecutableWithExports()) {
  1523. if (const char* custom_export_name = this->GetProperty("DEFINE_SYMBOL")) {
  1524. this->ExportMacro = custom_export_name;
  1525. } else {
  1526. std::string in = this->GetName();
  1527. in += "_EXPORTS";
  1528. this->ExportMacro = cmSystemTools::MakeCidentifier(in);
  1529. }
  1530. return this->ExportMacro.c_str();
  1531. }
  1532. return nullptr;
  1533. }
  1534. class cmTargetCollectLinkLanguages
  1535. {
  1536. public:
  1537. cmTargetCollectLinkLanguages(cmGeneratorTarget const* target,
  1538. const std::string& config,
  1539. std::unordered_set<std::string>& languages,
  1540. cmGeneratorTarget const* head)
  1541. : Config(config)
  1542. , Languages(languages)
  1543. , HeadTarget(head)
  1544. , Target(target)
  1545. {
  1546. this->Visited.insert(target);
  1547. }
  1548. void Visit(cmLinkItem const& item)
  1549. {
  1550. if (!item.Target) {
  1551. if (item.find("::") != std::string::npos) {
  1552. bool noMessage = false;
  1553. cmake::MessageType messageType = cmake::FATAL_ERROR;
  1554. std::ostringstream e;
  1555. switch (this->Target->GetLocalGenerator()->GetPolicyStatus(
  1556. cmPolicies::CMP0028)) {
  1557. case cmPolicies::WARN: {
  1558. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0028) << "\n";
  1559. messageType = cmake::AUTHOR_WARNING;
  1560. } break;
  1561. case cmPolicies::OLD:
  1562. noMessage = true;
  1563. case cmPolicies::REQUIRED_IF_USED:
  1564. case cmPolicies::REQUIRED_ALWAYS:
  1565. case cmPolicies::NEW:
  1566. // Issue the fatal message.
  1567. break;
  1568. }
  1569. if (!noMessage) {
  1570. e << "Target \"" << this->Target->GetName()
  1571. << "\" links to target \"" << item
  1572. << "\" but the target was not found. Perhaps a find_package() "
  1573. "call is missing for an IMPORTED target, or an ALIAS target is "
  1574. "missing?";
  1575. this->Target->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  1576. messageType, e.str(), this->Target->GetBacktrace());
  1577. }
  1578. }
  1579. return;
  1580. }
  1581. if (!this->Visited.insert(item.Target).second) {
  1582. return;
  1583. }
  1584. cmLinkInterface const* iface =
  1585. item.Target->GetLinkInterface(this->Config, this->HeadTarget);
  1586. if (!iface) {
  1587. return;
  1588. }
  1589. for (std::string const& language : iface->Languages) {
  1590. this->Languages.insert(language);
  1591. }
  1592. for (cmLinkItem const& lib : iface->Libraries) {
  1593. this->Visit(lib);
  1594. }
  1595. }
  1596. private:
  1597. std::string Config;
  1598. std::unordered_set<std::string>& Languages;
  1599. cmGeneratorTarget const* HeadTarget;
  1600. const cmGeneratorTarget* Target;
  1601. std::set<cmGeneratorTarget const*> Visited;
  1602. };
  1603. cmGeneratorTarget::LinkClosure const* cmGeneratorTarget::GetLinkClosure(
  1604. const std::string& config) const
  1605. {
  1606. std::string key(cmSystemTools::UpperCase(config));
  1607. LinkClosureMapType::iterator i = this->LinkClosureMap.find(key);
  1608. if (i == this->LinkClosureMap.end()) {
  1609. LinkClosure lc;
  1610. this->ComputeLinkClosure(config, lc);
  1611. LinkClosureMapType::value_type entry(key, lc);
  1612. i = this->LinkClosureMap.insert(entry).first;
  1613. }
  1614. return &i->second;
  1615. }
  1616. class cmTargetSelectLinker
  1617. {
  1618. int Preference;
  1619. cmGeneratorTarget const* Target;
  1620. cmGlobalGenerator* GG;
  1621. std::set<std::string> Preferred;
  1622. public:
  1623. cmTargetSelectLinker(cmGeneratorTarget const* target)
  1624. : Preference(0)
  1625. , Target(target)
  1626. {
  1627. this->GG = this->Target->GetLocalGenerator()->GetGlobalGenerator();
  1628. }
  1629. void Consider(const char* lang)
  1630. {
  1631. int preference = this->GG->GetLinkerPreference(lang);
  1632. if (preference > this->Preference) {
  1633. this->Preference = preference;
  1634. this->Preferred.clear();
  1635. }
  1636. if (preference == this->Preference) {
  1637. this->Preferred.insert(lang);
  1638. }
  1639. }
  1640. std::string Choose()
  1641. {
  1642. if (this->Preferred.empty()) {
  1643. return "";
  1644. }
  1645. if (this->Preferred.size() > 1) {
  1646. std::ostringstream e;
  1647. e << "Target " << this->Target->GetName()
  1648. << " contains multiple languages with the highest linker preference"
  1649. << " (" << this->Preference << "):\n";
  1650. for (std::string const& li : this->Preferred) {
  1651. e << " " << li << "\n";
  1652. }
  1653. e << "Set the LINKER_LANGUAGE property for this target.";
  1654. cmake* cm = this->Target->GetLocalGenerator()->GetCMakeInstance();
  1655. cm->IssueMessage(cmake::FATAL_ERROR, e.str(),
  1656. this->Target->GetBacktrace());
  1657. }
  1658. return *this->Preferred.begin();
  1659. }
  1660. };
  1661. void cmGeneratorTarget::ComputeLinkClosure(const std::string& config,
  1662. LinkClosure& lc) const
  1663. {
  1664. // Get languages built in this target.
  1665. std::unordered_set<std::string> languages;
  1666. cmLinkImplementation const* impl = this->GetLinkImplementation(config);
  1667. assert(impl);
  1668. for (std::string const& li : impl->Languages) {
  1669. languages.insert(li);
  1670. }
  1671. // Add interface languages from linked targets.
  1672. cmTargetCollectLinkLanguages cll(this, config, languages, this);
  1673. for (cmLinkImplItem const& lib : impl->Libraries) {
  1674. cll.Visit(lib);
  1675. }
  1676. // Store the transitive closure of languages.
  1677. for (std::string const& lang : languages) {
  1678. lc.Languages.push_back(lang);
  1679. }
  1680. // Choose the language whose linker should be used.
  1681. if (this->GetProperty("HAS_CXX")) {
  1682. lc.LinkerLanguage = "CXX";
  1683. } else if (const char* linkerLang = this->GetProperty("LINKER_LANGUAGE")) {
  1684. lc.LinkerLanguage = linkerLang;
  1685. } else {
  1686. // Find the language with the highest preference value.
  1687. cmTargetSelectLinker tsl(this);
  1688. // First select from the languages compiled directly in this target.
  1689. for (std::string const& l : impl->Languages) {
  1690. tsl.Consider(l.c_str());
  1691. }
  1692. // Now consider languages that propagate from linked targets.
  1693. for (std::string const& lang : languages) {
  1694. std::string propagates =
  1695. "CMAKE_" + lang + "_LINKER_PREFERENCE_PROPAGATES";
  1696. if (this->Makefile->IsOn(propagates)) {
  1697. tsl.Consider(lang.c_str());
  1698. }
  1699. }
  1700. lc.LinkerLanguage = tsl.Choose();
  1701. }
  1702. }
  1703. void cmGeneratorTarget::GetFullNameComponents(
  1704. std::string& prefix, std::string& base, std::string& suffix,
  1705. const std::string& config, cmStateEnums::ArtifactType artifact) const
  1706. {
  1707. this->GetFullNameInternal(config, artifact, prefix, base, suffix);
  1708. }
  1709. std::string cmGeneratorTarget::BuildBundleDirectory(
  1710. const std::string& base, const std::string& config,
  1711. BundleDirectoryLevel level) const
  1712. {
  1713. std::string fpath = base;
  1714. if (this->IsAppBundleOnApple()) {
  1715. fpath += this->GetAppBundleDirectory(config, level);
  1716. }
  1717. if (this->IsFrameworkOnApple()) {
  1718. fpath += this->GetFrameworkDirectory(config, level);
  1719. }
  1720. if (this->IsCFBundleOnApple()) {
  1721. fpath += this->GetCFBundleDirectory(config, level);
  1722. }
  1723. return fpath;
  1724. }
  1725. std::string cmGeneratorTarget::GetMacContentDirectory(
  1726. const std::string& config, cmStateEnums::ArtifactType artifact) const
  1727. {
  1728. // Start with the output directory for the target.
  1729. std::string fpath = this->GetDirectory(config, artifact);
  1730. fpath += "/";
  1731. BundleDirectoryLevel level = ContentLevel;
  1732. if (this->IsFrameworkOnApple()) {
  1733. // additional files with a framework go into the version specific
  1734. // directory
  1735. level = FullLevel;
  1736. }
  1737. fpath = this->BuildBundleDirectory(fpath, config, level);
  1738. return fpath;
  1739. }
  1740. std::string cmGeneratorTarget::GetEffectiveFolderName() const
  1741. {
  1742. std::string effectiveFolder;
  1743. if (!this->GlobalGenerator->UseFolderProperty()) {
  1744. return effectiveFolder;
  1745. }
  1746. const char* targetFolder = this->GetProperty("FOLDER");
  1747. if (targetFolder) {
  1748. effectiveFolder += targetFolder;
  1749. }
  1750. return effectiveFolder;
  1751. }
  1752. cmGeneratorTarget::CompileInfo const* cmGeneratorTarget::GetCompileInfo(
  1753. const std::string& config) const
  1754. {
  1755. // There is no compile information for imported targets.
  1756. if (this->IsImported()) {
  1757. return nullptr;
  1758. }
  1759. if (this->GetType() > cmStateEnums::OBJECT_LIBRARY) {
  1760. std::string msg = "cmTarget::GetCompileInfo called for ";
  1761. msg += this->GetName();
  1762. msg += " which has type ";
  1763. msg += cmState::GetTargetTypeName(this->GetType());
  1764. this->LocalGenerator->IssueMessage(cmake::INTERNAL_ERROR, msg);
  1765. return nullptr;
  1766. }
  1767. // Lookup/compute/cache the compile information for this configuration.
  1768. std::string config_upper;
  1769. if (!config.empty()) {
  1770. config_upper = cmSystemTools::UpperCase(config);
  1771. }
  1772. CompileInfoMapType::const_iterator i =
  1773. this->CompileInfoMap.find(config_upper);
  1774. if (i == this->CompileInfoMap.end()) {
  1775. CompileInfo info;
  1776. this->ComputePDBOutputDir("COMPILE_PDB", config, info.CompilePdbDir);
  1777. CompileInfoMapType::value_type entry(config_upper, info);
  1778. i = this->CompileInfoMap.insert(entry).first;
  1779. }
  1780. return &i->second;
  1781. }
  1782. cmGeneratorTarget::ModuleDefinitionInfo const*
  1783. cmGeneratorTarget::GetModuleDefinitionInfo(std::string const& config) const
  1784. {
  1785. // A module definition file only makes sense on certain target types.
  1786. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  1787. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  1788. !this->IsExecutableWithExports()) {
  1789. return nullptr;
  1790. }
  1791. // Lookup/compute/cache the compile information for this configuration.
  1792. std::string config_upper;
  1793. if (!config.empty()) {
  1794. config_upper = cmSystemTools::UpperCase(config);
  1795. }
  1796. ModuleDefinitionInfoMapType::const_iterator i =
  1797. this->ModuleDefinitionInfoMap.find(config_upper);
  1798. if (i == this->ModuleDefinitionInfoMap.end()) {
  1799. ModuleDefinitionInfo info;
  1800. this->ComputeModuleDefinitionInfo(config, info);
  1801. ModuleDefinitionInfoMapType::value_type entry(config_upper, info);
  1802. i = this->ModuleDefinitionInfoMap.insert(entry).first;
  1803. }
  1804. return &i->second;
  1805. }
  1806. void cmGeneratorTarget::ComputeModuleDefinitionInfo(
  1807. std::string const& config, ModuleDefinitionInfo& info) const
  1808. {
  1809. this->GetModuleDefinitionSources(info.Sources, config);
  1810. info.WindowsExportAllSymbols =
  1811. this->Makefile->IsOn("CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS") &&
  1812. this->GetPropertyAsBool("WINDOWS_EXPORT_ALL_SYMBOLS");
  1813. #if defined(_WIN32) && defined(CMAKE_BUILD_WITH_CMAKE)
  1814. info.DefFileGenerated =
  1815. info.WindowsExportAllSymbols || info.Sources.size() > 1;
  1816. #else
  1817. // Our __create_def helper is only available on Windows.
  1818. info.DefFileGenerated = false;
  1819. #endif
  1820. if (info.DefFileGenerated) {
  1821. info.DefFile = this->ObjectDirectory /* has slash */ + "exports.def";
  1822. } else if (!info.Sources.empty()) {
  1823. info.DefFile = info.Sources.front()->GetFullPath();
  1824. }
  1825. }
  1826. bool cmGeneratorTarget::IsDLLPlatform() const
  1827. {
  1828. return this->DLLPlatform;
  1829. }
  1830. void cmGeneratorTarget::GetAutoUicOptions(std::vector<std::string>& result,
  1831. const std::string& config) const
  1832. {
  1833. const char* prop =
  1834. this->GetLinkInterfaceDependentStringProperty("AUTOUIC_OPTIONS", config);
  1835. if (!prop) {
  1836. return;
  1837. }
  1838. cmGeneratorExpression ge;
  1839. cmGeneratorExpressionDAGChecker dagChecker(
  1840. this->GetName(), "AUTOUIC_OPTIONS", nullptr, nullptr);
  1841. cmSystemTools::ExpandListArgument(
  1842. ge.Parse(prop)->Evaluate(this->LocalGenerator, config, false, this,
  1843. &dagChecker),
  1844. result);
  1845. }
  1846. void processILibs(const std::string& config,
  1847. cmGeneratorTarget const* headTarget, cmLinkItem const& item,
  1848. cmGlobalGenerator* gg,
  1849. std::vector<cmGeneratorTarget const*>& tgts,
  1850. std::set<cmGeneratorTarget const*>& emitted)
  1851. {
  1852. if (item.Target && emitted.insert(item.Target).second) {
  1853. tgts.push_back(item.Target);
  1854. if (cmLinkInterfaceLibraries const* iface =
  1855. item.Target->GetLinkInterfaceLibraries(config, headTarget, true)) {
  1856. for (cmLinkItem const& lib : iface->Libraries) {
  1857. processILibs(config, headTarget, lib, gg, tgts, emitted);
  1858. }
  1859. }
  1860. }
  1861. }
  1862. const std::vector<const cmGeneratorTarget*>&
  1863. cmGeneratorTarget::GetLinkImplementationClosure(
  1864. const std::string& config) const
  1865. {
  1866. LinkImplClosure& tgts = this->LinkImplClosureMap[config];
  1867. if (!tgts.Done) {
  1868. tgts.Done = true;
  1869. std::set<cmGeneratorTarget const*> emitted;
  1870. cmLinkImplementationLibraries const* impl =
  1871. this->GetLinkImplementationLibraries(config);
  1872. for (cmLinkImplItem const& lib : impl->Libraries) {
  1873. processILibs(config, this, lib,
  1874. this->LocalGenerator->GetGlobalGenerator(), tgts, emitted);
  1875. }
  1876. }
  1877. return tgts;
  1878. }
  1879. class cmTargetTraceDependencies
  1880. {
  1881. public:
  1882. cmTargetTraceDependencies(cmGeneratorTarget* target);
  1883. void Trace();
  1884. private:
  1885. cmGeneratorTarget* GeneratorTarget;
  1886. cmMakefile* Makefile;
  1887. cmLocalGenerator* LocalGenerator;
  1888. cmGlobalGenerator const* GlobalGenerator;
  1889. typedef cmGeneratorTarget::SourceEntry SourceEntry;
  1890. SourceEntry* CurrentEntry;
  1891. std::queue<cmSourceFile*> SourceQueue;
  1892. std::set<cmSourceFile*> SourcesQueued;
  1893. typedef std::map<std::string, cmSourceFile*> NameMapType;
  1894. NameMapType NameMap;
  1895. std::vector<std::string> NewSources;
  1896. void QueueSource(cmSourceFile* sf);
  1897. void FollowName(std::string const& name);
  1898. void FollowNames(std::vector<std::string> const& names);
  1899. bool IsUtility(std::string const& dep);
  1900. void CheckCustomCommand(cmCustomCommand const& cc);
  1901. void CheckCustomCommands(const std::vector<cmCustomCommand>& commands);
  1902. void FollowCommandDepends(cmCustomCommand const& cc,
  1903. const std::string& config,
  1904. std::set<std::string>& emitted);
  1905. };
  1906. cmTargetTraceDependencies::cmTargetTraceDependencies(cmGeneratorTarget* target)
  1907. : GeneratorTarget(target)
  1908. {
  1909. // Convenience.
  1910. this->Makefile = target->Target->GetMakefile();
  1911. this->LocalGenerator = target->GetLocalGenerator();
  1912. this->GlobalGenerator = this->LocalGenerator->GetGlobalGenerator();
  1913. this->CurrentEntry = nullptr;
  1914. // Queue all the source files already specified for the target.
  1915. if (target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  1916. std::vector<std::string> configs;
  1917. this->Makefile->GetConfigurations(configs);
  1918. if (configs.empty()) {
  1919. configs.emplace_back();
  1920. }
  1921. std::set<cmSourceFile*> emitted;
  1922. for (std::string const& c : configs) {
  1923. std::vector<cmSourceFile*> sources;
  1924. this->GeneratorTarget->GetSourceFiles(sources, c);
  1925. for (cmSourceFile* sf : sources) {
  1926. const std::set<cmGeneratorTarget const*> tgts =
  1927. this->GlobalGenerator->GetFilenameTargetDepends(sf);
  1928. if (tgts.find(this->GeneratorTarget) != tgts.end()) {
  1929. std::ostringstream e;
  1930. e << "Evaluation output file\n \"" << sf->GetFullPath()
  1931. << "\"\ndepends on the sources of a target it is used in. This "
  1932. "is a dependency loop and is not allowed.";
  1933. this->GeneratorTarget->LocalGenerator->IssueMessage(
  1934. cmake::FATAL_ERROR, e.str());
  1935. return;
  1936. }
  1937. if (emitted.insert(sf).second &&
  1938. this->SourcesQueued.insert(sf).second) {
  1939. this->SourceQueue.push(sf);
  1940. }
  1941. }
  1942. }
  1943. }
  1944. // Queue pre-build, pre-link, and post-build rule dependencies.
  1945. this->CheckCustomCommands(this->GeneratorTarget->GetPreBuildCommands());
  1946. this->CheckCustomCommands(this->GeneratorTarget->GetPreLinkCommands());
  1947. this->CheckCustomCommands(this->GeneratorTarget->GetPostBuildCommands());
  1948. }
  1949. void cmTargetTraceDependencies::Trace()
  1950. {
  1951. // Process one dependency at a time until the queue is empty.
  1952. while (!this->SourceQueue.empty()) {
  1953. // Get the next source from the queue.
  1954. cmSourceFile* sf = this->SourceQueue.front();
  1955. this->SourceQueue.pop();
  1956. this->CurrentEntry = &this->GeneratorTarget->SourceDepends[sf];
  1957. // Queue dependencies added explicitly by the user.
  1958. if (const char* additionalDeps = sf->GetProperty("OBJECT_DEPENDS")) {
  1959. std::vector<std::string> objDeps;
  1960. cmSystemTools::ExpandListArgument(additionalDeps, objDeps);
  1961. for (std::string& objDep : objDeps) {
  1962. if (cmSystemTools::FileIsFullPath(objDep)) {
  1963. objDep = cmSystemTools::CollapseFullPath(objDep);
  1964. }
  1965. }
  1966. this->FollowNames(objDeps);
  1967. }
  1968. // Queue the source needed to generate this file, if any.
  1969. this->FollowName(sf->GetFullPath());
  1970. // Queue dependencies added programmatically by commands.
  1971. this->FollowNames(sf->GetDepends());
  1972. // Queue custom command dependencies.
  1973. if (cmCustomCommand const* cc = sf->GetCustomCommand()) {
  1974. this->CheckCustomCommand(*cc);
  1975. }
  1976. }
  1977. this->CurrentEntry = nullptr;
  1978. this->GeneratorTarget->AddTracedSources(this->NewSources);
  1979. }
  1980. void cmTargetTraceDependencies::QueueSource(cmSourceFile* sf)
  1981. {
  1982. if (this->SourcesQueued.insert(sf).second) {
  1983. this->SourceQueue.push(sf);
  1984. // Make sure this file is in the target at the end.
  1985. this->NewSources.push_back(sf->GetFullPath());
  1986. }
  1987. }
  1988. void cmTargetTraceDependencies::FollowName(std::string const& name)
  1989. {
  1990. NameMapType::iterator i = this->NameMap.find(name);
  1991. if (i == this->NameMap.end()) {
  1992. // Check if we know how to generate this file.
  1993. cmSourceFile* sf = this->Makefile->GetSourceFileWithOutput(name);
  1994. NameMapType::value_type entry(name, sf);
  1995. i = this->NameMap.insert(entry).first;
  1996. }
  1997. if (cmSourceFile* sf = i->second) {
  1998. // Record the dependency we just followed.
  1999. if (this->CurrentEntry) {
  2000. this->CurrentEntry->Depends.push_back(sf);
  2001. }
  2002. this->QueueSource(sf);
  2003. }
  2004. }
  2005. void cmTargetTraceDependencies::FollowNames(
  2006. std::vector<std::string> const& names)
  2007. {
  2008. for (std::string const& name : names) {
  2009. this->FollowName(name);
  2010. }
  2011. }
  2012. bool cmTargetTraceDependencies::IsUtility(std::string const& dep)
  2013. {
  2014. // Dependencies on targets (utilities) are supposed to be named by
  2015. // just the target name. However for compatibility we support
  2016. // naming the output file generated by the target (assuming there is
  2017. // no output-name property which old code would not have set). In
  2018. // that case the target name will be the file basename of the
  2019. // dependency.
  2020. std::string util = cmSystemTools::GetFilenameName(dep);
  2021. if (cmSystemTools::GetFilenameLastExtension(util) == ".exe") {
  2022. util = cmSystemTools::GetFilenameWithoutLastExtension(util);
  2023. }
  2024. // Check for a target with this name.
  2025. if (cmGeneratorTarget* t =
  2026. this->GeneratorTarget->GetLocalGenerator()->FindGeneratorTargetToUse(
  2027. util)) {
  2028. // If we find the target and the dep was given as a full path,
  2029. // then make sure it was not a full path to something else, and
  2030. // the fact that the name matched a target was just a coincidence.
  2031. if (cmSystemTools::FileIsFullPath(dep)) {
  2032. if (t->GetType() >= cmStateEnums::EXECUTABLE &&
  2033. t->GetType() <= cmStateEnums::MODULE_LIBRARY) {
  2034. // This is really only for compatibility so we do not need to
  2035. // worry about configuration names and output names.
  2036. std::string tLocation = t->GetLocationForBuild();
  2037. tLocation = cmSystemTools::GetFilenamePath(tLocation);
  2038. std::string depLocation = cmSystemTools::GetFilenamePath(dep);
  2039. depLocation = cmSystemTools::CollapseFullPath(depLocation);
  2040. tLocation = cmSystemTools::CollapseFullPath(tLocation);
  2041. if (depLocation == tLocation) {
  2042. this->GeneratorTarget->Target->AddUtility(util);
  2043. return true;
  2044. }
  2045. }
  2046. } else {
  2047. // The original name of the dependency was not a full path. It
  2048. // must name a target, so add the target-level dependency.
  2049. this->GeneratorTarget->Target->AddUtility(util);
  2050. return true;
  2051. }
  2052. }
  2053. // The dependency does not name a target built in this project.
  2054. return false;
  2055. }
  2056. void cmTargetTraceDependencies::CheckCustomCommand(cmCustomCommand const& cc)
  2057. {
  2058. // Transform command names that reference targets built in this
  2059. // project to corresponding target-level dependencies.
  2060. cmGeneratorExpression ge(cc.GetBacktrace());
  2061. // Add target-level dependencies referenced by generator expressions.
  2062. std::set<cmGeneratorTarget*> targets;
  2063. for (cmCustomCommandLine const& cCmdLine : cc.GetCommandLines()) {
  2064. std::string const& command = *cCmdLine.begin();
  2065. // Check for a target with this name.
  2066. if (cmGeneratorTarget* t =
  2067. this->LocalGenerator->FindGeneratorTargetToUse(command)) {
  2068. if (t->GetType() == cmStateEnums::EXECUTABLE) {
  2069. // The command refers to an executable target built in
  2070. // this project. Add the target-level dependency to make
  2071. // sure the executable is up to date before this custom
  2072. // command possibly runs.
  2073. this->GeneratorTarget->Target->AddUtility(command);
  2074. }
  2075. }
  2076. // Check for target references in generator expressions.
  2077. for (std::string const& cl : cCmdLine) {
  2078. const std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(cl);
  2079. cge->Evaluate(this->GeneratorTarget->GetLocalGenerator(), "", true);
  2080. std::set<cmGeneratorTarget*> geTargets = cge->GetTargets();
  2081. targets.insert(geTargets.begin(), geTargets.end());
  2082. }
  2083. }
  2084. for (cmGeneratorTarget* target : targets) {
  2085. this->GeneratorTarget->Target->AddUtility(target->GetName());
  2086. }
  2087. // Queue the custom command dependencies.
  2088. std::vector<std::string> configs;
  2089. std::set<std::string> emitted;
  2090. this->Makefile->GetConfigurations(configs);
  2091. if (configs.empty()) {
  2092. configs.emplace_back();
  2093. }
  2094. for (std::string const& conf : configs) {
  2095. this->FollowCommandDepends(cc, conf, emitted);
  2096. }
  2097. }
  2098. void cmTargetTraceDependencies::FollowCommandDepends(
  2099. cmCustomCommand const& cc, const std::string& config,
  2100. std::set<std::string>& emitted)
  2101. {
  2102. cmCustomCommandGenerator ccg(cc, config,
  2103. this->GeneratorTarget->LocalGenerator);
  2104. const std::vector<std::string>& depends = ccg.GetDepends();
  2105. for (std::string const& dep : depends) {
  2106. if (emitted.insert(dep).second) {
  2107. if (!this->IsUtility(dep)) {
  2108. // The dependency does not name a target and may be a file we
  2109. // know how to generate. Queue it.
  2110. this->FollowName(dep);
  2111. }
  2112. }
  2113. }
  2114. }
  2115. void cmTargetTraceDependencies::CheckCustomCommands(
  2116. const std::vector<cmCustomCommand>& commands)
  2117. {
  2118. for (cmCustomCommand const& command : commands) {
  2119. this->CheckCustomCommand(command);
  2120. }
  2121. }
  2122. void cmGeneratorTarget::TraceDependencies()
  2123. {
  2124. // CMake-generated targets have no dependencies to trace. Normally tracing
  2125. // would find nothing anyway, but when building CMake itself the "install"
  2126. // target command ends up referencing the "cmake" target but we do not
  2127. // really want the dependency because "install" depend on "all" anyway.
  2128. if (this->GetType() == cmStateEnums::GLOBAL_TARGET) {
  2129. return;
  2130. }
  2131. // Use a helper object to trace the dependencies.
  2132. cmTargetTraceDependencies tracer(this);
  2133. tracer.Trace();
  2134. }
  2135. std::string cmGeneratorTarget::GetCompilePDBDirectory(
  2136. const std::string& config) const
  2137. {
  2138. if (CompileInfo const* info = this->GetCompileInfo(config)) {
  2139. return info->CompilePdbDir;
  2140. }
  2141. return "";
  2142. }
  2143. void cmGeneratorTarget::GetAppleArchs(const std::string& config,
  2144. std::vector<std::string>& archVec) const
  2145. {
  2146. const char* archs = nullptr;
  2147. if (!config.empty()) {
  2148. std::string defVarName = "OSX_ARCHITECTURES_";
  2149. defVarName += cmSystemTools::UpperCase(config);
  2150. archs = this->GetProperty(defVarName);
  2151. }
  2152. if (!archs) {
  2153. archs = this->GetProperty("OSX_ARCHITECTURES");
  2154. }
  2155. if (archs) {
  2156. cmSystemTools::ExpandListArgument(std::string(archs), archVec);
  2157. }
  2158. }
  2159. //----------------------------------------------------------------------------
  2160. std::string cmGeneratorTarget::GetFeatureSpecificLinkRuleVariable(
  2161. std::string const& var, std::string const& lang,
  2162. std::string const& config) const
  2163. {
  2164. if (this->IsIPOEnabled(lang, config)) {
  2165. std::string varIPO = var + "_IPO";
  2166. if (this->Makefile->IsDefinitionSet(varIPO)) {
  2167. return varIPO;
  2168. }
  2169. }
  2170. return var;
  2171. }
  2172. //----------------------------------------------------------------------------
  2173. std::string cmGeneratorTarget::GetCreateRuleVariable(
  2174. std::string const& lang, std::string const& config) const
  2175. {
  2176. switch (this->GetType()) {
  2177. case cmStateEnums::STATIC_LIBRARY: {
  2178. std::string var = "CMAKE_" + lang + "_CREATE_STATIC_LIBRARY";
  2179. return this->GetFeatureSpecificLinkRuleVariable(var, lang, config);
  2180. }
  2181. case cmStateEnums::SHARED_LIBRARY:
  2182. return "CMAKE_" + lang + "_CREATE_SHARED_LIBRARY";
  2183. case cmStateEnums::MODULE_LIBRARY:
  2184. return "CMAKE_" + lang + "_CREATE_SHARED_MODULE";
  2185. case cmStateEnums::EXECUTABLE:
  2186. return "CMAKE_" + lang + "_LINK_EXECUTABLE";
  2187. default:
  2188. break;
  2189. }
  2190. return "";
  2191. }
  2192. static void processIncludeDirectories(
  2193. cmGeneratorTarget const* tgt,
  2194. const std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries,
  2195. std::vector<std::string>& includes,
  2196. std::unordered_set<std::string>& uniqueIncludes,
  2197. cmGeneratorExpressionDAGChecker* dagChecker, const std::string& config,
  2198. bool debugIncludes, const std::string& language)
  2199. {
  2200. for (cmGeneratorTarget::TargetPropertyEntry* entry : entries) {
  2201. cmLinkImplItem const& item = entry->LinkImplItem;
  2202. std::string const& targetName = item;
  2203. bool const fromImported = item.Target && item.Target->IsImported();
  2204. bool const checkCMP0027 = item.FromGenex;
  2205. std::vector<std::string> entryIncludes;
  2206. cmSystemTools::ExpandListArgument(
  2207. entry->ge->Evaluate(tgt->GetLocalGenerator(), config, false, tgt,
  2208. dagChecker, language),
  2209. entryIncludes);
  2210. std::string usedIncludes;
  2211. for (std::string& entryInclude : entryIncludes) {
  2212. if (fromImported && !cmSystemTools::FileExists(entryInclude)) {
  2213. std::ostringstream e;
  2214. cmake::MessageType messageType = cmake::FATAL_ERROR;
  2215. if (checkCMP0027) {
  2216. switch (tgt->GetPolicyStatusCMP0027()) {
  2217. case cmPolicies::WARN:
  2218. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0027) << "\n";
  2219. CM_FALLTHROUGH;
  2220. case cmPolicies::OLD:
  2221. messageType = cmake::AUTHOR_WARNING;
  2222. break;
  2223. case cmPolicies::REQUIRED_ALWAYS:
  2224. case cmPolicies::REQUIRED_IF_USED:
  2225. case cmPolicies::NEW:
  2226. break;
  2227. }
  2228. }
  2229. /* clang-format off */
  2230. e << "Imported target \"" << targetName << "\" includes "
  2231. "non-existent path\n \"" << entryInclude << "\"\nin its "
  2232. "INTERFACE_INCLUDE_DIRECTORIES. Possible reasons include:\n"
  2233. "* The path was deleted, renamed, or moved to another "
  2234. "location.\n"
  2235. "* An install or uninstall procedure did not complete "
  2236. "successfully.\n"
  2237. "* The installation package was faulty and references files it "
  2238. "does not provide.\n";
  2239. /* clang-format on */
  2240. tgt->GetLocalGenerator()->IssueMessage(messageType, e.str());
  2241. return;
  2242. }
  2243. if (!cmSystemTools::FileIsFullPath(entryInclude)) {
  2244. std::ostringstream e;
  2245. bool noMessage = false;
  2246. cmake::MessageType messageType = cmake::FATAL_ERROR;
  2247. if (!targetName.empty()) {
  2248. /* clang-format off */
  2249. e << "Target \"" << targetName << "\" contains relative "
  2250. "path in its INTERFACE_INCLUDE_DIRECTORIES:\n"
  2251. " \"" << entryInclude << "\"";
  2252. /* clang-format on */
  2253. } else {
  2254. switch (tgt->GetPolicyStatusCMP0021()) {
  2255. case cmPolicies::WARN: {
  2256. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0021) << "\n";
  2257. messageType = cmake::AUTHOR_WARNING;
  2258. } break;
  2259. case cmPolicies::OLD:
  2260. noMessage = true;
  2261. case cmPolicies::REQUIRED_IF_USED:
  2262. case cmPolicies::REQUIRED_ALWAYS:
  2263. case cmPolicies::NEW:
  2264. // Issue the fatal message.
  2265. break;
  2266. }
  2267. e << "Found relative path while evaluating include directories of "
  2268. "\""
  2269. << tgt->GetName() << "\":\n \"" << entryInclude << "\"\n";
  2270. }
  2271. if (!noMessage) {
  2272. tgt->GetLocalGenerator()->IssueMessage(messageType, e.str());
  2273. if (messageType == cmake::FATAL_ERROR) {
  2274. return;
  2275. }
  2276. }
  2277. }
  2278. if (!cmSystemTools::IsOff(entryInclude.c_str())) {
  2279. cmSystemTools::ConvertToUnixSlashes(entryInclude);
  2280. }
  2281. std::string inc = entryInclude;
  2282. if (uniqueIncludes.insert(inc).second) {
  2283. includes.push_back(inc);
  2284. if (debugIncludes) {
  2285. usedIncludes += " * " + inc + "\n";
  2286. }
  2287. }
  2288. }
  2289. if (!usedIncludes.empty()) {
  2290. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  2291. cmake::LOG,
  2292. std::string("Used includes for target ") + tgt->GetName() + ":\n" +
  2293. usedIncludes,
  2294. entry->ge->GetBacktrace());
  2295. }
  2296. }
  2297. }
  2298. std::vector<std::string> cmGeneratorTarget::GetIncludeDirectories(
  2299. const std::string& config, const std::string& lang) const
  2300. {
  2301. std::vector<std::string> includes;
  2302. std::unordered_set<std::string> uniqueIncludes;
  2303. cmGeneratorExpressionDAGChecker dagChecker(
  2304. this->GetName(), "INCLUDE_DIRECTORIES", nullptr, nullptr);
  2305. std::vector<std::string> debugProperties;
  2306. const char* debugProp =
  2307. this->Makefile->GetDefinition("CMAKE_DEBUG_TARGET_PROPERTIES");
  2308. if (debugProp) {
  2309. cmSystemTools::ExpandListArgument(debugProp, debugProperties);
  2310. }
  2311. bool debugIncludes = !this->DebugIncludesDone &&
  2312. std::find(debugProperties.begin(), debugProperties.end(),
  2313. "INCLUDE_DIRECTORIES") != debugProperties.end();
  2314. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  2315. this->DebugIncludesDone = true;
  2316. }
  2317. processIncludeDirectories(this, this->IncludeDirectoriesEntries, includes,
  2318. uniqueIncludes, &dagChecker, config, debugIncludes,
  2319. lang);
  2320. std::vector<cmGeneratorTarget::TargetPropertyEntry*>
  2321. linkInterfaceIncludeDirectoriesEntries;
  2322. AddInterfaceEntries(this, config, "INTERFACE_INCLUDE_DIRECTORIES",
  2323. linkInterfaceIncludeDirectoriesEntries);
  2324. if (this->Makefile->IsOn("APPLE")) {
  2325. cmLinkImplementationLibraries const* impl =
  2326. this->GetLinkImplementationLibraries(config);
  2327. for (cmLinkImplItem const& lib : impl->Libraries) {
  2328. std::string libDir = cmSystemTools::CollapseFullPath(lib);
  2329. static cmsys::RegularExpression frameworkCheck(
  2330. "(.*\\.framework)(/Versions/[^/]+)?/[^/]+$");
  2331. if (!frameworkCheck.find(libDir)) {
  2332. continue;
  2333. }
  2334. libDir = frameworkCheck.match(1);
  2335. cmGeneratorExpression ge;
  2336. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  2337. ge.Parse(libDir.c_str());
  2338. linkInterfaceIncludeDirectoriesEntries.push_back(
  2339. new cmGeneratorTarget::TargetPropertyEntry(std::move(cge)));
  2340. }
  2341. }
  2342. processIncludeDirectories(this, linkInterfaceIncludeDirectoriesEntries,
  2343. includes, uniqueIncludes, &dagChecker, config,
  2344. debugIncludes, lang);
  2345. cmDeleteAll(linkInterfaceIncludeDirectoriesEntries);
  2346. return includes;
  2347. }
  2348. enum class OptionsParse
  2349. {
  2350. None,
  2351. Shell
  2352. };
  2353. static void processOptionsInternal(
  2354. cmGeneratorTarget const* tgt,
  2355. const std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries,
  2356. std::vector<std::string>& options,
  2357. std::unordered_set<std::string>& uniqueOptions,
  2358. cmGeneratorExpressionDAGChecker* dagChecker, const std::string& config,
  2359. bool debugOptions, const char* logName, std::string const& language,
  2360. OptionsParse parse)
  2361. {
  2362. for (cmGeneratorTarget::TargetPropertyEntry* entry : entries) {
  2363. std::vector<std::string> entryOptions;
  2364. cmSystemTools::ExpandListArgument(
  2365. entry->ge->Evaluate(tgt->GetLocalGenerator(), config, false, tgt,
  2366. dagChecker, language),
  2367. entryOptions);
  2368. std::string usedOptions;
  2369. for (std::string const& opt : entryOptions) {
  2370. if (uniqueOptions.insert(opt).second) {
  2371. if (parse == OptionsParse::Shell &&
  2372. cmHasLiteralPrefix(opt, "SHELL:")) {
  2373. cmSystemTools::ParseUnixCommandLine(opt.c_str() + 6, options);
  2374. } else {
  2375. options.push_back(opt);
  2376. }
  2377. if (debugOptions) {
  2378. usedOptions += " * " + opt + "\n";
  2379. }
  2380. }
  2381. }
  2382. if (!usedOptions.empty()) {
  2383. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  2384. cmake::LOG,
  2385. std::string("Used ") + logName + std::string(" for target ") +
  2386. tgt->GetName() + ":\n" + usedOptions,
  2387. entry->ge->GetBacktrace());
  2388. }
  2389. }
  2390. }
  2391. static void processCompileOptions(
  2392. cmGeneratorTarget const* tgt,
  2393. const std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries,
  2394. std::vector<std::string>& options,
  2395. std::unordered_set<std::string>& uniqueOptions,
  2396. cmGeneratorExpressionDAGChecker* dagChecker, const std::string& config,
  2397. bool debugOptions, std::string const& language)
  2398. {
  2399. processOptionsInternal(tgt, entries, options, uniqueOptions, dagChecker,
  2400. config, debugOptions, "compile options", language,
  2401. OptionsParse::Shell);
  2402. }
  2403. void cmGeneratorTarget::GetCompileOptions(std::vector<std::string>& result,
  2404. const std::string& config,
  2405. const std::string& language) const
  2406. {
  2407. std::unordered_set<std::string> uniqueOptions;
  2408. cmGeneratorExpressionDAGChecker dagChecker(
  2409. this->GetName(), "COMPILE_OPTIONS", nullptr, nullptr);
  2410. std::vector<std::string> debugProperties;
  2411. const char* debugProp =
  2412. this->Makefile->GetDefinition("CMAKE_DEBUG_TARGET_PROPERTIES");
  2413. if (debugProp) {
  2414. cmSystemTools::ExpandListArgument(debugProp, debugProperties);
  2415. }
  2416. bool debugOptions = !this->DebugCompileOptionsDone &&
  2417. std::find(debugProperties.begin(), debugProperties.end(),
  2418. "COMPILE_OPTIONS") != debugProperties.end();
  2419. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  2420. this->DebugCompileOptionsDone = true;
  2421. }
  2422. processCompileOptions(this, this->CompileOptionsEntries, result,
  2423. uniqueOptions, &dagChecker, config, debugOptions,
  2424. language);
  2425. std::vector<cmGeneratorTarget::TargetPropertyEntry*>
  2426. linkInterfaceCompileOptionsEntries;
  2427. AddInterfaceEntries(this, config, "INTERFACE_COMPILE_OPTIONS",
  2428. linkInterfaceCompileOptionsEntries);
  2429. processCompileOptions(this, linkInterfaceCompileOptionsEntries, result,
  2430. uniqueOptions, &dagChecker, config, debugOptions,
  2431. language);
  2432. cmDeleteAll(linkInterfaceCompileOptionsEntries);
  2433. }
  2434. static void processCompileFeatures(
  2435. cmGeneratorTarget const* tgt,
  2436. const std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries,
  2437. std::vector<std::string>& options,
  2438. std::unordered_set<std::string>& uniqueOptions,
  2439. cmGeneratorExpressionDAGChecker* dagChecker, const std::string& config,
  2440. bool debugOptions)
  2441. {
  2442. processOptionsInternal(tgt, entries, options, uniqueOptions, dagChecker,
  2443. config, debugOptions, "compile features",
  2444. std::string(), OptionsParse::None);
  2445. }
  2446. void cmGeneratorTarget::GetCompileFeatures(std::vector<std::string>& result,
  2447. const std::string& config) const
  2448. {
  2449. std::unordered_set<std::string> uniqueFeatures;
  2450. cmGeneratorExpressionDAGChecker dagChecker(
  2451. this->GetName(), "COMPILE_FEATURES", nullptr, nullptr);
  2452. std::vector<std::string> debugProperties;
  2453. const char* debugProp =
  2454. this->Makefile->GetDefinition("CMAKE_DEBUG_TARGET_PROPERTIES");
  2455. if (debugProp) {
  2456. cmSystemTools::ExpandListArgument(debugProp, debugProperties);
  2457. }
  2458. bool debugFeatures = !this->DebugCompileFeaturesDone &&
  2459. std::find(debugProperties.begin(), debugProperties.end(),
  2460. "COMPILE_FEATURES") != debugProperties.end();
  2461. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  2462. this->DebugCompileFeaturesDone = true;
  2463. }
  2464. processCompileFeatures(this, this->CompileFeaturesEntries, result,
  2465. uniqueFeatures, &dagChecker, config, debugFeatures);
  2466. std::vector<cmGeneratorTarget::TargetPropertyEntry*>
  2467. linkInterfaceCompileFeaturesEntries;
  2468. AddInterfaceEntries(this, config, "INTERFACE_COMPILE_FEATURES",
  2469. linkInterfaceCompileFeaturesEntries);
  2470. processCompileFeatures(this, linkInterfaceCompileFeaturesEntries, result,
  2471. uniqueFeatures, &dagChecker, config, debugFeatures);
  2472. cmDeleteAll(linkInterfaceCompileFeaturesEntries);
  2473. }
  2474. static void processCompileDefinitions(
  2475. cmGeneratorTarget const* tgt,
  2476. const std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries,
  2477. std::vector<std::string>& options,
  2478. std::unordered_set<std::string>& uniqueOptions,
  2479. cmGeneratorExpressionDAGChecker* dagChecker, const std::string& config,
  2480. bool debugOptions, std::string const& language)
  2481. {
  2482. processOptionsInternal(tgt, entries, options, uniqueOptions, dagChecker,
  2483. config, debugOptions, "compile definitions", language,
  2484. OptionsParse::None);
  2485. }
  2486. void cmGeneratorTarget::GetCompileDefinitions(
  2487. std::vector<std::string>& list, const std::string& config,
  2488. const std::string& language) const
  2489. {
  2490. std::unordered_set<std::string> uniqueOptions;
  2491. cmGeneratorExpressionDAGChecker dagChecker(
  2492. this->GetName(), "COMPILE_DEFINITIONS", nullptr, nullptr);
  2493. std::vector<std::string> debugProperties;
  2494. const char* debugProp =
  2495. this->Makefile->GetDefinition("CMAKE_DEBUG_TARGET_PROPERTIES");
  2496. if (debugProp) {
  2497. cmSystemTools::ExpandListArgument(debugProp, debugProperties);
  2498. }
  2499. bool debugDefines = !this->DebugCompileDefinitionsDone &&
  2500. std::find(debugProperties.begin(), debugProperties.end(),
  2501. "COMPILE_DEFINITIONS") != debugProperties.end();
  2502. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  2503. this->DebugCompileDefinitionsDone = true;
  2504. }
  2505. processCompileDefinitions(this, this->CompileDefinitionsEntries, list,
  2506. uniqueOptions, &dagChecker, config, debugDefines,
  2507. language);
  2508. std::vector<cmGeneratorTarget::TargetPropertyEntry*>
  2509. linkInterfaceCompileDefinitionsEntries;
  2510. AddInterfaceEntries(this, config, "INTERFACE_COMPILE_DEFINITIONS",
  2511. linkInterfaceCompileDefinitionsEntries);
  2512. if (!config.empty()) {
  2513. std::string configPropName =
  2514. "COMPILE_DEFINITIONS_" + cmSystemTools::UpperCase(config);
  2515. const char* configProp = this->GetProperty(configPropName);
  2516. if (configProp) {
  2517. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0043)) {
  2518. case cmPolicies::WARN: {
  2519. std::ostringstream e;
  2520. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0043);
  2521. this->LocalGenerator->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  2522. CM_FALLTHROUGH;
  2523. }
  2524. case cmPolicies::OLD: {
  2525. cmGeneratorExpression ge;
  2526. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  2527. ge.Parse(configProp);
  2528. linkInterfaceCompileDefinitionsEntries.push_back(
  2529. new cmGeneratorTarget::TargetPropertyEntry(std::move(cge)));
  2530. } break;
  2531. case cmPolicies::NEW:
  2532. case cmPolicies::REQUIRED_ALWAYS:
  2533. case cmPolicies::REQUIRED_IF_USED:
  2534. break;
  2535. }
  2536. }
  2537. }
  2538. processCompileDefinitions(this, linkInterfaceCompileDefinitionsEntries, list,
  2539. uniqueOptions, &dagChecker, config, debugDefines,
  2540. language);
  2541. cmDeleteAll(linkInterfaceCompileDefinitionsEntries);
  2542. }
  2543. static void processLinkOptions(
  2544. cmGeneratorTarget const* tgt,
  2545. const std::vector<cmGeneratorTarget::TargetPropertyEntry*>& entries,
  2546. std::vector<std::string>& options,
  2547. std::unordered_set<std::string>& uniqueOptions,
  2548. cmGeneratorExpressionDAGChecker* dagChecker, const std::string& config,
  2549. bool debugOptions, std::string const& language)
  2550. {
  2551. processOptionsInternal(tgt, entries, options, uniqueOptions, dagChecker,
  2552. config, debugOptions, "link options", language,
  2553. OptionsParse::Shell);
  2554. }
  2555. void cmGeneratorTarget::GetLinkOptions(std::vector<std::string>& result,
  2556. const std::string& config,
  2557. const std::string& language) const
  2558. {
  2559. std::unordered_set<std::string> uniqueOptions;
  2560. cmGeneratorExpressionDAGChecker dagChecker(this->GetName(), "LINK_OPTIONS",
  2561. nullptr, nullptr);
  2562. std::vector<std::string> debugProperties;
  2563. const char* debugProp =
  2564. this->Makefile->GetDefinition("CMAKE_DEBUG_TARGET_PROPERTIES");
  2565. if (debugProp) {
  2566. cmSystemTools::ExpandListArgument(debugProp, debugProperties);
  2567. }
  2568. bool debugOptions = !this->DebugLinkOptionsDone &&
  2569. std::find(debugProperties.begin(), debugProperties.end(),
  2570. "LINK_OPTIONS") != debugProperties.end();
  2571. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  2572. this->DebugLinkOptionsDone = true;
  2573. }
  2574. processLinkOptions(this, this->LinkOptionsEntries, result, uniqueOptions,
  2575. &dagChecker, config, debugOptions, language);
  2576. std::vector<cmGeneratorTarget::TargetPropertyEntry*>
  2577. linkInterfaceLinkOptionsEntries;
  2578. AddInterfaceEntries(this, config, "INTERFACE_LINK_OPTIONS",
  2579. linkInterfaceLinkOptionsEntries);
  2580. processLinkOptions(this, linkInterfaceLinkOptionsEntries, result,
  2581. uniqueOptions, &dagChecker, config, debugOptions,
  2582. language);
  2583. cmDeleteAll(linkInterfaceLinkOptionsEntries);
  2584. }
  2585. void cmGeneratorTarget::ComputeTargetManifest(const std::string& config) const
  2586. {
  2587. if (this->IsImported()) {
  2588. return;
  2589. }
  2590. cmGlobalGenerator* gg = this->LocalGenerator->GetGlobalGenerator();
  2591. // Get the names.
  2592. std::string name;
  2593. std::string soName;
  2594. std::string realName;
  2595. std::string impName;
  2596. std::string pdbName;
  2597. if (this->GetType() == cmStateEnums::EXECUTABLE) {
  2598. this->GetExecutableNames(name, realName, impName, pdbName, config);
  2599. } else if (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  2600. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2601. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  2602. this->GetLibraryNames(name, soName, realName, impName, pdbName, config);
  2603. } else {
  2604. return;
  2605. }
  2606. // Get the directory.
  2607. std::string dir =
  2608. this->GetDirectory(config, cmStateEnums::RuntimeBinaryArtifact);
  2609. // Add each name.
  2610. std::string f;
  2611. if (!name.empty()) {
  2612. f = dir;
  2613. f += "/";
  2614. f += name;
  2615. gg->AddToManifest(f);
  2616. }
  2617. if (!soName.empty()) {
  2618. f = dir;
  2619. f += "/";
  2620. f += soName;
  2621. gg->AddToManifest(f);
  2622. }
  2623. if (!realName.empty()) {
  2624. f = dir;
  2625. f += "/";
  2626. f += realName;
  2627. gg->AddToManifest(f);
  2628. }
  2629. if (!pdbName.empty()) {
  2630. f = dir;
  2631. f += "/";
  2632. f += pdbName;
  2633. gg->AddToManifest(f);
  2634. }
  2635. if (!impName.empty()) {
  2636. f = this->GetDirectory(config, cmStateEnums::ImportLibraryArtifact);
  2637. f += "/";
  2638. f += impName;
  2639. gg->AddToManifest(f);
  2640. }
  2641. }
  2642. bool cmGeneratorTarget::ComputeCompileFeatures(std::string const& config) const
  2643. {
  2644. std::vector<std::string> features;
  2645. this->GetCompileFeatures(features, config);
  2646. for (std::string const& f : features) {
  2647. if (!this->Makefile->AddRequiredTargetFeature(this->Target, f)) {
  2648. return false;
  2649. }
  2650. }
  2651. return true;
  2652. }
  2653. std::string cmGeneratorTarget::GetImportedLibName(
  2654. std::string const& config) const
  2655. {
  2656. if (cmGeneratorTarget::ImportInfo const* info =
  2657. this->GetImportInfo(config)) {
  2658. return info->LibName;
  2659. }
  2660. return std::string();
  2661. }
  2662. std::string cmGeneratorTarget::GetFullPath(const std::string& config,
  2663. cmStateEnums::ArtifactType artifact,
  2664. bool realname) const
  2665. {
  2666. if (this->IsImported()) {
  2667. return this->Target->ImportedGetFullPath(config, artifact);
  2668. }
  2669. return this->NormalGetFullPath(config, artifact, realname);
  2670. }
  2671. std::string cmGeneratorTarget::NormalGetFullPath(
  2672. const std::string& config, cmStateEnums::ArtifactType artifact,
  2673. bool realname) const
  2674. {
  2675. std::string fpath = this->GetDirectory(config, artifact);
  2676. fpath += "/";
  2677. if (this->IsAppBundleOnApple()) {
  2678. fpath = this->BuildBundleDirectory(fpath, config, FullLevel);
  2679. fpath += "/";
  2680. }
  2681. // Add the full name of the target.
  2682. switch (artifact) {
  2683. case cmStateEnums::RuntimeBinaryArtifact:
  2684. if (realname) {
  2685. fpath += this->NormalGetRealName(config);
  2686. } else {
  2687. fpath +=
  2688. this->GetFullName(config, cmStateEnums::RuntimeBinaryArtifact);
  2689. }
  2690. break;
  2691. case cmStateEnums::ImportLibraryArtifact:
  2692. fpath += this->GetFullName(config, cmStateEnums::ImportLibraryArtifact);
  2693. break;
  2694. }
  2695. return fpath;
  2696. }
  2697. std::string cmGeneratorTarget::NormalGetRealName(
  2698. const std::string& config) const
  2699. {
  2700. // This should not be called for imported targets.
  2701. // TODO: Split cmTarget into a class hierarchy to get compile-time
  2702. // enforcement of the limited imported target API.
  2703. if (this->IsImported()) {
  2704. std::string msg = "NormalGetRealName called on imported target: ";
  2705. msg += this->GetName();
  2706. this->LocalGenerator->IssueMessage(cmake::INTERNAL_ERROR, msg);
  2707. }
  2708. if (this->GetType() == cmStateEnums::EXECUTABLE) {
  2709. // Compute the real name that will be built.
  2710. std::string name;
  2711. std::string realName;
  2712. std::string impName;
  2713. std::string pdbName;
  2714. this->GetExecutableNames(name, realName, impName, pdbName, config);
  2715. return realName;
  2716. }
  2717. // Compute the real name that will be built.
  2718. std::string name;
  2719. std::string soName;
  2720. std::string realName;
  2721. std::string impName;
  2722. std::string pdbName;
  2723. this->GetLibraryNames(name, soName, realName, impName, pdbName, config);
  2724. return realName;
  2725. }
  2726. void cmGeneratorTarget::GetLibraryNames(std::string& name, std::string& soName,
  2727. std::string& realName,
  2728. std::string& impName,
  2729. std::string& pdbName,
  2730. const std::string& config) const
  2731. {
  2732. // This should not be called for imported targets.
  2733. // TODO: Split cmTarget into a class hierarchy to get compile-time
  2734. // enforcement of the limited imported target API.
  2735. if (this->IsImported()) {
  2736. std::string msg = "GetLibraryNames called on imported target: ";
  2737. msg += this->GetName();
  2738. this->LocalGenerator->IssueMessage(cmake::INTERNAL_ERROR, msg);
  2739. return;
  2740. }
  2741. // Check for library version properties.
  2742. const char* version = this->GetProperty("VERSION");
  2743. const char* soversion = this->GetProperty("SOVERSION");
  2744. if (!this->HasSOName(config) ||
  2745. this->Makefile->IsOn("CMAKE_PLATFORM_NO_VERSIONED_SONAME") ||
  2746. this->IsFrameworkOnApple()) {
  2747. // Versioning is supported only for shared libraries and modules,
  2748. // and then only when the platform supports an soname flag.
  2749. version = nullptr;
  2750. soversion = nullptr;
  2751. }
  2752. if (version && !soversion) {
  2753. // The soversion must be set if the library version is set. Use
  2754. // the library version as the soversion.
  2755. soversion = version;
  2756. }
  2757. if (!version && soversion) {
  2758. // Use the soversion as the library version.
  2759. version = soversion;
  2760. }
  2761. // Get the components of the library name.
  2762. std::string prefix;
  2763. std::string base;
  2764. std::string suffix;
  2765. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  2766. prefix, base, suffix);
  2767. // The library name.
  2768. name = prefix + base + suffix;
  2769. if (this->IsFrameworkOnApple()) {
  2770. realName = prefix;
  2771. if (!this->Makefile->PlatformIsAppleEmbedded()) {
  2772. realName += "Versions/";
  2773. realName += this->GetFrameworkVersion();
  2774. realName += "/";
  2775. }
  2776. realName += base;
  2777. soName = realName;
  2778. } else {
  2779. // The library's soname.
  2780. this->ComputeVersionedName(soName, prefix, base, suffix, name, soversion);
  2781. // The library's real name on disk.
  2782. this->ComputeVersionedName(realName, prefix, base, suffix, name, version);
  2783. }
  2784. // The import library name.
  2785. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2786. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  2787. impName =
  2788. this->GetFullNameInternal(config, cmStateEnums::ImportLibraryArtifact);
  2789. } else {
  2790. impName.clear();
  2791. }
  2792. // The program database file name.
  2793. pdbName = this->GetPDBName(config);
  2794. }
  2795. void cmGeneratorTarget::GetExecutableNames(std::string& name,
  2796. std::string& realName,
  2797. std::string& impName,
  2798. std::string& pdbName,
  2799. const std::string& config) const
  2800. {
  2801. // This should not be called for imported targets.
  2802. // TODO: Split cmTarget into a class hierarchy to get compile-time
  2803. // enforcement of the limited imported target API.
  2804. if (this->IsImported()) {
  2805. std::string msg = "GetExecutableNames called on imported target: ";
  2806. msg += this->GetName();
  2807. this->LocalGenerator->IssueMessage(cmake::INTERNAL_ERROR, msg);
  2808. }
  2809. // This versioning is supported only for executables and then only
  2810. // when the platform supports symbolic links.
  2811. #if defined(_WIN32) && !defined(__CYGWIN__)
  2812. const char* version = 0;
  2813. #else
  2814. // Check for executable version properties.
  2815. const char* version = this->GetProperty("VERSION");
  2816. if (this->GetType() != cmStateEnums::EXECUTABLE ||
  2817. this->Makefile->IsOn("XCODE")) {
  2818. version = nullptr;
  2819. }
  2820. #endif
  2821. // Get the components of the executable name.
  2822. std::string prefix;
  2823. std::string base;
  2824. std::string suffix;
  2825. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  2826. prefix, base, suffix);
  2827. // The executable name.
  2828. name = prefix + base + suffix;
  2829. // The executable's real name on disk.
  2830. #if defined(__CYGWIN__)
  2831. realName = prefix + base;
  2832. #else
  2833. realName = name;
  2834. #endif
  2835. if (version) {
  2836. realName += "-";
  2837. realName += version;
  2838. }
  2839. #if defined(__CYGWIN__)
  2840. realName += suffix;
  2841. #endif
  2842. // The import library name.
  2843. impName =
  2844. this->GetFullNameInternal(config, cmStateEnums::ImportLibraryArtifact);
  2845. // The program database file name.
  2846. pdbName = this->GetPDBName(config);
  2847. }
  2848. std::string cmGeneratorTarget::GetFullNameInternal(
  2849. const std::string& config, cmStateEnums::ArtifactType artifact) const
  2850. {
  2851. std::string prefix;
  2852. std::string base;
  2853. std::string suffix;
  2854. this->GetFullNameInternal(config, artifact, prefix, base, suffix);
  2855. return prefix + base + suffix;
  2856. }
  2857. const char* cmGeneratorTarget::ImportedGetLocation(
  2858. const std::string& config) const
  2859. {
  2860. static std::string location;
  2861. assert(this->IsImported());
  2862. location = this->Target->ImportedGetFullPath(
  2863. config, cmStateEnums::RuntimeBinaryArtifact);
  2864. return location.c_str();
  2865. }
  2866. std::string cmGeneratorTarget::GetFullNameImported(
  2867. const std::string& config, cmStateEnums::ArtifactType artifact) const
  2868. {
  2869. return cmSystemTools::GetFilenameName(
  2870. this->Target->ImportedGetFullPath(config, artifact));
  2871. }
  2872. void cmGeneratorTarget::GetFullNameInternal(
  2873. const std::string& config, cmStateEnums::ArtifactType artifact,
  2874. std::string& outPrefix, std::string& outBase, std::string& outSuffix) const
  2875. {
  2876. // Use just the target name for non-main target types.
  2877. if (this->GetType() != cmStateEnums::STATIC_LIBRARY &&
  2878. this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  2879. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  2880. this->GetType() != cmStateEnums::EXECUTABLE) {
  2881. outPrefix.clear();
  2882. outBase = this->GetName();
  2883. outSuffix.clear();
  2884. return;
  2885. }
  2886. const bool isImportedLibraryArtifact =
  2887. (artifact == cmStateEnums::ImportLibraryArtifact);
  2888. // Return an empty name for the import library if this platform
  2889. // does not support import libraries.
  2890. if (isImportedLibraryArtifact &&
  2891. !this->Makefile->GetDefinition("CMAKE_IMPORT_LIBRARY_SUFFIX")) {
  2892. outPrefix.clear();
  2893. outBase.clear();
  2894. outSuffix.clear();
  2895. return;
  2896. }
  2897. // The implib option is only allowed for shared libraries, module
  2898. // libraries, and executables.
  2899. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  2900. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  2901. this->GetType() != cmStateEnums::EXECUTABLE) {
  2902. artifact = cmStateEnums::RuntimeBinaryArtifact;
  2903. }
  2904. // Compute the full name for main target types.
  2905. const char* targetPrefix =
  2906. (isImportedLibraryArtifact ? this->GetProperty("IMPORT_PREFIX")
  2907. : this->GetProperty("PREFIX"));
  2908. const char* targetSuffix =
  2909. (isImportedLibraryArtifact ? this->GetProperty("IMPORT_SUFFIX")
  2910. : this->GetProperty("SUFFIX"));
  2911. const char* configPostfix = nullptr;
  2912. if (!config.empty()) {
  2913. std::string configProp = cmSystemTools::UpperCase(config);
  2914. configProp += "_POSTFIX";
  2915. configPostfix = this->GetProperty(configProp);
  2916. // Mac application bundles and frameworks have no postfix.
  2917. if (configPostfix &&
  2918. (this->IsAppBundleOnApple() || this->IsFrameworkOnApple())) {
  2919. configPostfix = nullptr;
  2920. }
  2921. }
  2922. const char* prefixVar = this->Target->GetPrefixVariableInternal(artifact);
  2923. const char* suffixVar = this->Target->GetSuffixVariableInternal(artifact);
  2924. // Check for language-specific default prefix and suffix.
  2925. std::string ll = this->GetLinkerLanguage(config);
  2926. if (!ll.empty()) {
  2927. if (!targetSuffix && suffixVar && *suffixVar) {
  2928. std::string langSuff = suffixVar + std::string("_") + ll;
  2929. targetSuffix = this->Makefile->GetDefinition(langSuff);
  2930. }
  2931. if (!targetPrefix && prefixVar && *prefixVar) {
  2932. std::string langPrefix = prefixVar + std::string("_") + ll;
  2933. targetPrefix = this->Makefile->GetDefinition(langPrefix);
  2934. }
  2935. }
  2936. // if there is no prefix on the target use the cmake definition
  2937. if (!targetPrefix && prefixVar) {
  2938. targetPrefix = this->Makefile->GetSafeDefinition(prefixVar);
  2939. }
  2940. // if there is no suffix on the target use the cmake definition
  2941. if (!targetSuffix && suffixVar) {
  2942. targetSuffix = this->Makefile->GetSafeDefinition(suffixVar);
  2943. }
  2944. // frameworks have directory prefix but no suffix
  2945. std::string fw_prefix;
  2946. if (this->IsFrameworkOnApple()) {
  2947. fw_prefix = this->GetFrameworkDirectory(config, ContentLevel);
  2948. fw_prefix += "/";
  2949. targetPrefix = fw_prefix.c_str();
  2950. targetSuffix = nullptr;
  2951. }
  2952. if (this->IsCFBundleOnApple()) {
  2953. fw_prefix = this->GetCFBundleDirectory(config, FullLevel);
  2954. fw_prefix += "/";
  2955. targetPrefix = fw_prefix.c_str();
  2956. targetSuffix = nullptr;
  2957. }
  2958. // Begin the final name with the prefix.
  2959. outPrefix = targetPrefix ? targetPrefix : "";
  2960. // Append the target name or property-specified name.
  2961. outBase += this->GetOutputName(config, artifact);
  2962. // Append the per-configuration postfix.
  2963. outBase += configPostfix ? configPostfix : "";
  2964. // Name shared libraries with their version number on some platforms.
  2965. if (const char* soversion = this->GetProperty("SOVERSION")) {
  2966. if (this->GetType() == cmStateEnums::SHARED_LIBRARY &&
  2967. !isImportedLibraryArtifact &&
  2968. this->Makefile->IsOn("CMAKE_SHARED_LIBRARY_NAME_WITH_VERSION")) {
  2969. outBase += "-";
  2970. outBase += soversion;
  2971. }
  2972. }
  2973. // Append the suffix.
  2974. outSuffix = targetSuffix ? targetSuffix : "";
  2975. }
  2976. std::string cmGeneratorTarget::GetLinkerLanguage(
  2977. const std::string& config) const
  2978. {
  2979. return this->GetLinkClosure(config)->LinkerLanguage;
  2980. }
  2981. std::string cmGeneratorTarget::GetPDBName(const std::string& config) const
  2982. {
  2983. std::string prefix;
  2984. std::string base;
  2985. std::string suffix;
  2986. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  2987. prefix, base, suffix);
  2988. std::vector<std::string> props;
  2989. std::string configUpper = cmSystemTools::UpperCase(config);
  2990. if (!configUpper.empty()) {
  2991. // PDB_NAME_<CONFIG>
  2992. props.push_back("PDB_NAME_" + configUpper);
  2993. }
  2994. // PDB_NAME
  2995. props.push_back("PDB_NAME");
  2996. for (std::string const& p : props) {
  2997. if (const char* outName = this->GetProperty(p)) {
  2998. base = outName;
  2999. break;
  3000. }
  3001. }
  3002. return prefix + base + ".pdb";
  3003. }
  3004. std::string cmGeneratorTarget::GetObjectDirectory(
  3005. std::string const& config) const
  3006. {
  3007. std::string obj_dir =
  3008. this->GlobalGenerator->ExpandCFGIntDir(this->ObjectDirectory, config);
  3009. #if defined(__APPLE__)
  3010. // find and replace $(PROJECT_NAME) xcode placeholder
  3011. const std::string projectName = this->LocalGenerator->GetProjectName();
  3012. cmSystemTools::ReplaceString(obj_dir, "$(PROJECT_NAME)", projectName);
  3013. #endif
  3014. return obj_dir;
  3015. }
  3016. void cmGeneratorTarget::GetTargetObjectNames(
  3017. std::string const& config, std::vector<std::string>& objects) const
  3018. {
  3019. std::vector<cmSourceFile const*> objectSources;
  3020. this->GetObjectSources(objectSources, config);
  3021. std::map<cmSourceFile const*, std::string> mapping;
  3022. for (cmSourceFile const* sf : objectSources) {
  3023. mapping[sf];
  3024. }
  3025. this->LocalGenerator->ComputeObjectFilenames(mapping, this);
  3026. for (cmSourceFile const* src : objectSources) {
  3027. // Find the object file name corresponding to this source file.
  3028. std::map<cmSourceFile const*, std::string>::const_iterator map_it =
  3029. mapping.find(src);
  3030. // It must exist because we populated the mapping just above.
  3031. assert(!map_it->second.empty());
  3032. objects.push_back(map_it->second);
  3033. }
  3034. }
  3035. bool cmGeneratorTarget::StrictTargetComparison::operator()(
  3036. cmGeneratorTarget const* t1, cmGeneratorTarget const* t2) const
  3037. {
  3038. int nameResult = strcmp(t1->GetName().c_str(), t2->GetName().c_str());
  3039. if (nameResult == 0) {
  3040. return strcmp(t1->GetLocalGenerator()->GetCurrentBinaryDirectory(),
  3041. t2->GetLocalGenerator()->GetCurrentBinaryDirectory()) < 0;
  3042. }
  3043. return nameResult < 0;
  3044. }
  3045. struct cmGeneratorTarget::SourceFileFlags
  3046. cmGeneratorTarget::GetTargetSourceFileFlags(const cmSourceFile* sf) const
  3047. {
  3048. struct SourceFileFlags flags;
  3049. this->ConstructSourceFileFlags();
  3050. std::map<cmSourceFile const*, SourceFileFlags>::iterator si =
  3051. this->SourceFlagsMap.find(sf);
  3052. if (si != this->SourceFlagsMap.end()) {
  3053. flags = si->second;
  3054. } else {
  3055. // Handle the MACOSX_PACKAGE_LOCATION property on source files that
  3056. // were not listed in one of the other lists.
  3057. if (const char* location = sf->GetProperty("MACOSX_PACKAGE_LOCATION")) {
  3058. flags.MacFolder = location;
  3059. const bool stripResources =
  3060. this->GlobalGenerator->ShouldStripResourcePath(this->Makefile);
  3061. if (strcmp(location, "Resources") == 0) {
  3062. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  3063. if (stripResources) {
  3064. flags.MacFolder = "";
  3065. }
  3066. } else if (cmSystemTools::StringStartsWith(location, "Resources/")) {
  3067. flags.Type = cmGeneratorTarget::SourceFileTypeDeepResource;
  3068. if (stripResources) {
  3069. flags.MacFolder += strlen("Resources/");
  3070. }
  3071. } else {
  3072. flags.Type = cmGeneratorTarget::SourceFileTypeMacContent;
  3073. }
  3074. }
  3075. }
  3076. return flags;
  3077. }
  3078. void cmGeneratorTarget::ConstructSourceFileFlags() const
  3079. {
  3080. if (this->SourceFileFlagsConstructed) {
  3081. return;
  3082. }
  3083. this->SourceFileFlagsConstructed = true;
  3084. // Process public headers to mark the source files.
  3085. if (const char* files = this->GetProperty("PUBLIC_HEADER")) {
  3086. std::vector<std::string> relFiles;
  3087. cmSystemTools::ExpandListArgument(files, relFiles);
  3088. for (std::string const& relFile : relFiles) {
  3089. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  3090. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  3091. flags.MacFolder = "Headers";
  3092. flags.Type = cmGeneratorTarget::SourceFileTypePublicHeader;
  3093. }
  3094. }
  3095. }
  3096. // Process private headers after public headers so that they take
  3097. // precedence if a file is listed in both.
  3098. if (const char* files = this->GetProperty("PRIVATE_HEADER")) {
  3099. std::vector<std::string> relFiles;
  3100. cmSystemTools::ExpandListArgument(files, relFiles);
  3101. for (std::string const& relFile : relFiles) {
  3102. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  3103. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  3104. flags.MacFolder = "PrivateHeaders";
  3105. flags.Type = cmGeneratorTarget::SourceFileTypePrivateHeader;
  3106. }
  3107. }
  3108. }
  3109. // Mark sources listed as resources.
  3110. if (const char* files = this->GetProperty("RESOURCE")) {
  3111. std::vector<std::string> relFiles;
  3112. cmSystemTools::ExpandListArgument(files, relFiles);
  3113. for (std::string const& relFile : relFiles) {
  3114. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  3115. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  3116. flags.MacFolder = "";
  3117. if (!this->GlobalGenerator->ShouldStripResourcePath(this->Makefile)) {
  3118. flags.MacFolder = "Resources";
  3119. }
  3120. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  3121. }
  3122. }
  3123. }
  3124. }
  3125. const cmGeneratorTarget::CompatibleInterfacesBase&
  3126. cmGeneratorTarget::GetCompatibleInterfaces(std::string const& config) const
  3127. {
  3128. cmGeneratorTarget::CompatibleInterfaces& compat =
  3129. this->CompatibleInterfacesMap[config];
  3130. if (!compat.Done) {
  3131. compat.Done = true;
  3132. compat.PropsBool.insert("POSITION_INDEPENDENT_CODE");
  3133. compat.PropsString.insert("AUTOUIC_OPTIONS");
  3134. std::vector<cmGeneratorTarget const*> const& deps =
  3135. this->GetLinkImplementationClosure(config);
  3136. for (cmGeneratorTarget const* li : deps) {
  3137. #define CM_READ_COMPATIBLE_INTERFACE(X, x) \
  3138. if (const char* prop = li->GetProperty("COMPATIBLE_INTERFACE_" #X)) { \
  3139. std::vector<std::string> props; \
  3140. cmSystemTools::ExpandListArgument(prop, props); \
  3141. compat.Props##x.insert(props.begin(), props.end()); \
  3142. }
  3143. CM_READ_COMPATIBLE_INTERFACE(BOOL, Bool)
  3144. CM_READ_COMPATIBLE_INTERFACE(STRING, String)
  3145. CM_READ_COMPATIBLE_INTERFACE(NUMBER_MIN, NumberMin)
  3146. CM_READ_COMPATIBLE_INTERFACE(NUMBER_MAX, NumberMax)
  3147. #undef CM_READ_COMPATIBLE_INTERFACE
  3148. }
  3149. }
  3150. return compat;
  3151. }
  3152. bool cmGeneratorTarget::IsLinkInterfaceDependentBoolProperty(
  3153. const std::string& p, const std::string& config) const
  3154. {
  3155. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  3156. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  3157. return false;
  3158. }
  3159. return this->GetCompatibleInterfaces(config).PropsBool.count(p) > 0;
  3160. }
  3161. bool cmGeneratorTarget::IsLinkInterfaceDependentStringProperty(
  3162. const std::string& p, const std::string& config) const
  3163. {
  3164. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  3165. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  3166. return false;
  3167. }
  3168. return this->GetCompatibleInterfaces(config).PropsString.count(p) > 0;
  3169. }
  3170. bool cmGeneratorTarget::IsLinkInterfaceDependentNumberMinProperty(
  3171. const std::string& p, const std::string& config) const
  3172. {
  3173. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  3174. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  3175. return false;
  3176. }
  3177. return this->GetCompatibleInterfaces(config).PropsNumberMin.count(p) > 0;
  3178. }
  3179. bool cmGeneratorTarget::IsLinkInterfaceDependentNumberMaxProperty(
  3180. const std::string& p, const std::string& config) const
  3181. {
  3182. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  3183. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  3184. return false;
  3185. }
  3186. return this->GetCompatibleInterfaces(config).PropsNumberMax.count(p) > 0;
  3187. }
  3188. enum CompatibleType
  3189. {
  3190. BoolType,
  3191. StringType,
  3192. NumberMinType,
  3193. NumberMaxType
  3194. };
  3195. template <typename PropertyType>
  3196. PropertyType getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  3197. const std::string& prop,
  3198. const std::string& config,
  3199. CompatibleType, PropertyType*);
  3200. template <>
  3201. bool getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  3202. const std::string& prop,
  3203. const std::string& config,
  3204. CompatibleType /*unused*/,
  3205. bool* /*unused*/)
  3206. {
  3207. return tgt->GetLinkInterfaceDependentBoolProperty(prop, config);
  3208. }
  3209. template <>
  3210. const char* getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  3211. const std::string& prop,
  3212. const std::string& config,
  3213. CompatibleType t,
  3214. const char** /*unused*/)
  3215. {
  3216. switch (t) {
  3217. case BoolType:
  3218. assert(false &&
  3219. "String compatibility check function called for boolean");
  3220. return nullptr;
  3221. case StringType:
  3222. return tgt->GetLinkInterfaceDependentStringProperty(prop, config);
  3223. case NumberMinType:
  3224. return tgt->GetLinkInterfaceDependentNumberMinProperty(prop, config);
  3225. case NumberMaxType:
  3226. return tgt->GetLinkInterfaceDependentNumberMaxProperty(prop, config);
  3227. }
  3228. assert(false && "Unreachable!");
  3229. return nullptr;
  3230. }
  3231. template <typename PropertyType>
  3232. void checkPropertyConsistency(cmGeneratorTarget const* depender,
  3233. cmGeneratorTarget const* dependee,
  3234. const std::string& propName,
  3235. std::set<std::string>& emitted,
  3236. const std::string& config, CompatibleType t,
  3237. PropertyType* /*unused*/)
  3238. {
  3239. const char* prop = dependee->GetProperty(propName);
  3240. if (!prop) {
  3241. return;
  3242. }
  3243. std::vector<std::string> props;
  3244. cmSystemTools::ExpandListArgument(prop, props);
  3245. std::string pdir = cmSystemTools::GetCMakeRoot();
  3246. pdir += "/Help/prop_tgt/";
  3247. for (std::string const& p : props) {
  3248. std::string pname = cmSystemTools::HelpFileName(p);
  3249. std::string pfile = pdir + pname + ".rst";
  3250. if (cmSystemTools::FileExists(pfile, true)) {
  3251. std::ostringstream e;
  3252. e << "Target \"" << dependee->GetName() << "\" has property \"" << p
  3253. << "\" listed in its " << propName
  3254. << " property. "
  3255. "This is not allowed. Only user-defined properties may appear "
  3256. "listed in the "
  3257. << propName << " property.";
  3258. depender->GetLocalGenerator()->IssueMessage(cmake::FATAL_ERROR, e.str());
  3259. return;
  3260. }
  3261. if (emitted.insert(p).second) {
  3262. getLinkInterfaceDependentProperty<PropertyType>(depender, p, config, t,
  3263. nullptr);
  3264. if (cmSystemTools::GetErrorOccuredFlag()) {
  3265. return;
  3266. }
  3267. }
  3268. }
  3269. }
  3270. static std::string intersect(const std::set<std::string>& s1,
  3271. const std::set<std::string>& s2)
  3272. {
  3273. std::set<std::string> intersect;
  3274. std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(),
  3275. std::inserter(intersect, intersect.begin()));
  3276. if (!intersect.empty()) {
  3277. return *intersect.begin();
  3278. }
  3279. return "";
  3280. }
  3281. static std::string intersect(const std::set<std::string>& s1,
  3282. const std::set<std::string>& s2,
  3283. const std::set<std::string>& s3)
  3284. {
  3285. std::string result;
  3286. result = intersect(s1, s2);
  3287. if (!result.empty()) {
  3288. return result;
  3289. }
  3290. result = intersect(s1, s3);
  3291. if (!result.empty()) {
  3292. return result;
  3293. }
  3294. return intersect(s2, s3);
  3295. }
  3296. static std::string intersect(const std::set<std::string>& s1,
  3297. const std::set<std::string>& s2,
  3298. const std::set<std::string>& s3,
  3299. const std::set<std::string>& s4)
  3300. {
  3301. std::string result;
  3302. result = intersect(s1, s2);
  3303. if (!result.empty()) {
  3304. return result;
  3305. }
  3306. result = intersect(s1, s3);
  3307. if (!result.empty()) {
  3308. return result;
  3309. }
  3310. result = intersect(s1, s4);
  3311. if (!result.empty()) {
  3312. return result;
  3313. }
  3314. return intersect(s2, s3, s4);
  3315. }
  3316. void cmGeneratorTarget::CheckPropertyCompatibility(
  3317. cmComputeLinkInformation* info, const std::string& config) const
  3318. {
  3319. const cmComputeLinkInformation::ItemVector& deps = info->GetItems();
  3320. std::set<std::string> emittedBools;
  3321. static const std::string strBool = "COMPATIBLE_INTERFACE_BOOL";
  3322. std::set<std::string> emittedStrings;
  3323. static const std::string strString = "COMPATIBLE_INTERFACE_STRING";
  3324. std::set<std::string> emittedMinNumbers;
  3325. static const std::string strNumMin = "COMPATIBLE_INTERFACE_NUMBER_MIN";
  3326. std::set<std::string> emittedMaxNumbers;
  3327. static const std::string strNumMax = "COMPATIBLE_INTERFACE_NUMBER_MAX";
  3328. for (auto const& dep : deps) {
  3329. if (!dep.Target) {
  3330. continue;
  3331. }
  3332. checkPropertyConsistency<bool>(this, dep.Target, strBool, emittedBools,
  3333. config, BoolType, nullptr);
  3334. if (cmSystemTools::GetErrorOccuredFlag()) {
  3335. return;
  3336. }
  3337. checkPropertyConsistency<const char*>(this, dep.Target, strString,
  3338. emittedStrings, config, StringType,
  3339. nullptr);
  3340. if (cmSystemTools::GetErrorOccuredFlag()) {
  3341. return;
  3342. }
  3343. checkPropertyConsistency<const char*>(this, dep.Target, strNumMin,
  3344. emittedMinNumbers, config,
  3345. NumberMinType, nullptr);
  3346. if (cmSystemTools::GetErrorOccuredFlag()) {
  3347. return;
  3348. }
  3349. checkPropertyConsistency<const char*>(this, dep.Target, strNumMax,
  3350. emittedMaxNumbers, config,
  3351. NumberMaxType, nullptr);
  3352. if (cmSystemTools::GetErrorOccuredFlag()) {
  3353. return;
  3354. }
  3355. }
  3356. std::string prop = intersect(emittedBools, emittedStrings, emittedMinNumbers,
  3357. emittedMaxNumbers);
  3358. if (!prop.empty()) {
  3359. // Use a sorted std::vector to keep the error message sorted.
  3360. std::vector<std::string> props;
  3361. std::set<std::string>::const_iterator i = emittedBools.find(prop);
  3362. if (i != emittedBools.end()) {
  3363. props.push_back(strBool);
  3364. }
  3365. i = emittedStrings.find(prop);
  3366. if (i != emittedStrings.end()) {
  3367. props.push_back(strString);
  3368. }
  3369. i = emittedMinNumbers.find(prop);
  3370. if (i != emittedMinNumbers.end()) {
  3371. props.push_back(strNumMin);
  3372. }
  3373. i = emittedMaxNumbers.find(prop);
  3374. if (i != emittedMaxNumbers.end()) {
  3375. props.push_back(strNumMax);
  3376. }
  3377. std::sort(props.begin(), props.end());
  3378. std::string propsString = cmJoin(cmMakeRange(props).retreat(1), ", ");
  3379. propsString += " and the " + props.back();
  3380. std::ostringstream e;
  3381. e << "Property \"" << prop << "\" appears in both the " << propsString
  3382. << " property in the dependencies of target \"" << this->GetName()
  3383. << "\". This is not allowed. A property may only require compatibility "
  3384. "in a boolean interpretation, a numeric minimum, a numeric maximum "
  3385. "or a "
  3386. "string interpretation, but not a mixture.";
  3387. this->LocalGenerator->IssueMessage(cmake::FATAL_ERROR, e.str());
  3388. }
  3389. }
  3390. std::string compatibilityType(CompatibleType t)
  3391. {
  3392. switch (t) {
  3393. case BoolType:
  3394. return "Boolean compatibility";
  3395. case StringType:
  3396. return "String compatibility";
  3397. case NumberMaxType:
  3398. return "Numeric maximum compatibility";
  3399. case NumberMinType:
  3400. return "Numeric minimum compatibility";
  3401. }
  3402. assert(false && "Unreachable!");
  3403. return "";
  3404. }
  3405. std::string compatibilityAgree(CompatibleType t, bool dominant)
  3406. {
  3407. switch (t) {
  3408. case BoolType:
  3409. case StringType:
  3410. return dominant ? "(Disagree)\n" : "(Agree)\n";
  3411. case NumberMaxType:
  3412. case NumberMinType:
  3413. return dominant ? "(Dominant)\n" : "(Ignored)\n";
  3414. }
  3415. assert(false && "Unreachable!");
  3416. return "";
  3417. }
  3418. template <typename PropertyType>
  3419. PropertyType getTypedProperty(cmGeneratorTarget const* tgt,
  3420. const std::string& prop);
  3421. template <>
  3422. bool getTypedProperty<bool>(cmGeneratorTarget const* tgt,
  3423. const std::string& prop)
  3424. {
  3425. return tgt->GetPropertyAsBool(prop);
  3426. }
  3427. template <>
  3428. const char* getTypedProperty<const char*>(cmGeneratorTarget const* tgt,
  3429. const std::string& prop)
  3430. {
  3431. return tgt->GetProperty(prop);
  3432. }
  3433. template <typename PropertyType>
  3434. std::string valueAsString(PropertyType);
  3435. template <>
  3436. std::string valueAsString<bool>(bool value)
  3437. {
  3438. return value ? "TRUE" : "FALSE";
  3439. }
  3440. template <>
  3441. std::string valueAsString<const char*>(const char* value)
  3442. {
  3443. return value ? value : "(unset)";
  3444. }
  3445. template <typename PropertyType>
  3446. PropertyType impliedValue(PropertyType);
  3447. template <>
  3448. bool impliedValue<bool>(bool /*unused*/)
  3449. {
  3450. return false;
  3451. }
  3452. template <>
  3453. const char* impliedValue<const char*>(const char* /*unused*/)
  3454. {
  3455. return "";
  3456. }
  3457. template <typename PropertyType>
  3458. std::pair<bool, PropertyType> consistentProperty(PropertyType lhs,
  3459. PropertyType rhs,
  3460. CompatibleType t);
  3461. template <>
  3462. std::pair<bool, bool> consistentProperty(bool lhs, bool rhs,
  3463. CompatibleType /*unused*/)
  3464. {
  3465. return std::make_pair(lhs == rhs, lhs);
  3466. }
  3467. std::pair<bool, const char*> consistentStringProperty(const char* lhs,
  3468. const char* rhs)
  3469. {
  3470. const bool b = strcmp(lhs, rhs) == 0;
  3471. return std::make_pair(b, b ? lhs : nullptr);
  3472. }
  3473. std::pair<bool, const char*> consistentNumberProperty(const char* lhs,
  3474. const char* rhs,
  3475. CompatibleType t)
  3476. {
  3477. char* pEnd;
  3478. const char* const null_ptr = nullptr;
  3479. long lnum = strtol(lhs, &pEnd, 0);
  3480. if (pEnd == lhs || *pEnd != '\0' || errno == ERANGE) {
  3481. return std::pair<bool, const char*>(false, null_ptr);
  3482. }
  3483. long rnum = strtol(rhs, &pEnd, 0);
  3484. if (pEnd == rhs || *pEnd != '\0' || errno == ERANGE) {
  3485. return std::pair<bool, const char*>(false, null_ptr);
  3486. }
  3487. if (t == NumberMaxType) {
  3488. return std::make_pair(true, std::max(lnum, rnum) == lnum ? lhs : rhs);
  3489. }
  3490. return std::make_pair(true, std::min(lnum, rnum) == lnum ? lhs : rhs);
  3491. }
  3492. template <>
  3493. std::pair<bool, const char*> consistentProperty(const char* lhs,
  3494. const char* rhs,
  3495. CompatibleType t)
  3496. {
  3497. if (!lhs && !rhs) {
  3498. return std::make_pair(true, lhs);
  3499. }
  3500. if (!lhs) {
  3501. return std::make_pair(true, rhs);
  3502. }
  3503. if (!rhs) {
  3504. return std::make_pair(true, lhs);
  3505. }
  3506. const char* const null_ptr = nullptr;
  3507. switch (t) {
  3508. case BoolType:
  3509. assert(false && "consistentProperty for strings called with BoolType");
  3510. return std::pair<bool, const char*>(false, null_ptr);
  3511. case StringType:
  3512. return consistentStringProperty(lhs, rhs);
  3513. case NumberMinType:
  3514. case NumberMaxType:
  3515. return consistentNumberProperty(lhs, rhs, t);
  3516. }
  3517. assert(false && "Unreachable!");
  3518. return std::pair<bool, const char*>(false, null_ptr);
  3519. }
  3520. template <typename PropertyType>
  3521. PropertyType checkInterfacePropertyCompatibility(cmGeneratorTarget const* tgt,
  3522. const std::string& p,
  3523. const std::string& config,
  3524. const char* defaultValue,
  3525. CompatibleType t,
  3526. PropertyType* /*unused*/)
  3527. {
  3528. PropertyType propContent = getTypedProperty<PropertyType>(tgt, p);
  3529. std::vector<std::string> headPropKeys = tgt->GetPropertyKeys();
  3530. const bool explicitlySet =
  3531. std::find(headPropKeys.begin(), headPropKeys.end(), p) !=
  3532. headPropKeys.end();
  3533. const bool impliedByUse = tgt->IsNullImpliedByLinkLibraries(p);
  3534. assert((impliedByUse ^ explicitlySet) || (!impliedByUse && !explicitlySet));
  3535. std::vector<cmGeneratorTarget const*> const& deps =
  3536. tgt->GetLinkImplementationClosure(config);
  3537. if (deps.empty()) {
  3538. return propContent;
  3539. }
  3540. bool propInitialized = explicitlySet;
  3541. std::string report = " * Target \"";
  3542. report += tgt->GetName();
  3543. if (explicitlySet) {
  3544. report += "\" has property content \"";
  3545. report += valueAsString<PropertyType>(propContent);
  3546. report += "\"\n";
  3547. } else if (impliedByUse) {
  3548. report += "\" property is implied by use.\n";
  3549. } else {
  3550. report += "\" property not set.\n";
  3551. }
  3552. std::string interfaceProperty = "INTERFACE_" + p;
  3553. for (cmGeneratorTarget const* theTarget : deps) {
  3554. // An error should be reported if one dependency
  3555. // has INTERFACE_POSITION_INDEPENDENT_CODE ON and the other
  3556. // has INTERFACE_POSITION_INDEPENDENT_CODE OFF, or if the
  3557. // target itself has a POSITION_INDEPENDENT_CODE which disagrees
  3558. // with a dependency.
  3559. std::vector<std::string> propKeys = theTarget->GetPropertyKeys();
  3560. const bool ifaceIsSet = std::find(propKeys.begin(), propKeys.end(),
  3561. interfaceProperty) != propKeys.end();
  3562. PropertyType ifacePropContent =
  3563. getTypedProperty<PropertyType>(theTarget, interfaceProperty);
  3564. std::string reportEntry;
  3565. if (ifaceIsSet) {
  3566. reportEntry += " * Target \"";
  3567. reportEntry += theTarget->GetName();
  3568. reportEntry += "\" property value \"";
  3569. reportEntry += valueAsString<PropertyType>(ifacePropContent);
  3570. reportEntry += "\" ";
  3571. }
  3572. if (explicitlySet) {
  3573. if (ifaceIsSet) {
  3574. std::pair<bool, PropertyType> consistent =
  3575. consistentProperty(propContent, ifacePropContent, t);
  3576. report += reportEntry;
  3577. report += compatibilityAgree(t, propContent != consistent.second);
  3578. if (!consistent.first) {
  3579. std::ostringstream e;
  3580. e << "Property " << p << " on target \"" << tgt->GetName()
  3581. << "\" does\nnot match the "
  3582. "INTERFACE_"
  3583. << p
  3584. << " property requirement\nof "
  3585. "dependency \""
  3586. << theTarget->GetName() << "\".\n";
  3587. cmSystemTools::Error(e.str().c_str());
  3588. break;
  3589. }
  3590. propContent = consistent.second;
  3591. continue;
  3592. }
  3593. // Explicitly set on target and not set in iface. Can't disagree.
  3594. continue;
  3595. }
  3596. if (impliedByUse) {
  3597. propContent = impliedValue<PropertyType>(propContent);
  3598. if (ifaceIsSet) {
  3599. std::pair<bool, PropertyType> consistent =
  3600. consistentProperty(propContent, ifacePropContent, t);
  3601. report += reportEntry;
  3602. report += compatibilityAgree(t, propContent != consistent.second);
  3603. if (!consistent.first) {
  3604. std::ostringstream e;
  3605. e << "Property " << p << " on target \"" << tgt->GetName()
  3606. << "\" is\nimplied to be " << defaultValue
  3607. << " because it was used to determine the link libraries\n"
  3608. "already. The INTERFACE_"
  3609. << p << " property on\ndependency \"" << theTarget->GetName()
  3610. << "\" is in conflict.\n";
  3611. cmSystemTools::Error(e.str().c_str());
  3612. break;
  3613. }
  3614. propContent = consistent.second;
  3615. continue;
  3616. }
  3617. // Implicitly set on target and not set in iface. Can't disagree.
  3618. continue;
  3619. }
  3620. if (ifaceIsSet) {
  3621. if (propInitialized) {
  3622. std::pair<bool, PropertyType> consistent =
  3623. consistentProperty(propContent, ifacePropContent, t);
  3624. report += reportEntry;
  3625. report += compatibilityAgree(t, propContent != consistent.second);
  3626. if (!consistent.first) {
  3627. std::ostringstream e;
  3628. e << "The INTERFACE_" << p << " property of \""
  3629. << theTarget->GetName() << "\" does\nnot agree with the value of "
  3630. << p << " already determined\nfor \"" << tgt->GetName() << "\".\n";
  3631. cmSystemTools::Error(e.str().c_str());
  3632. break;
  3633. }
  3634. propContent = consistent.second;
  3635. continue;
  3636. }
  3637. report += reportEntry + "(Interface set)\n";
  3638. propContent = ifacePropContent;
  3639. propInitialized = true;
  3640. } else {
  3641. // Not set. Nothing to agree on.
  3642. continue;
  3643. }
  3644. }
  3645. tgt->ReportPropertyOrigin(p, valueAsString<PropertyType>(propContent),
  3646. report, compatibilityType(t));
  3647. return propContent;
  3648. }
  3649. bool cmGeneratorTarget::GetLinkInterfaceDependentBoolProperty(
  3650. const std::string& p, const std::string& config) const
  3651. {
  3652. return checkInterfacePropertyCompatibility<bool>(this, p, config, "FALSE",
  3653. BoolType, nullptr);
  3654. }
  3655. const char* cmGeneratorTarget::GetLinkInterfaceDependentStringProperty(
  3656. const std::string& p, const std::string& config) const
  3657. {
  3658. return checkInterfacePropertyCompatibility<const char*>(
  3659. this, p, config, "empty", StringType, nullptr);
  3660. }
  3661. const char* cmGeneratorTarget::GetLinkInterfaceDependentNumberMinProperty(
  3662. const std::string& p, const std::string& config) const
  3663. {
  3664. return checkInterfacePropertyCompatibility<const char*>(
  3665. this, p, config, "empty", NumberMinType, nullptr);
  3666. }
  3667. const char* cmGeneratorTarget::GetLinkInterfaceDependentNumberMaxProperty(
  3668. const std::string& p, const std::string& config) const
  3669. {
  3670. return checkInterfacePropertyCompatibility<const char*>(
  3671. this, p, config, "empty", NumberMaxType, nullptr);
  3672. }
  3673. cmComputeLinkInformation* cmGeneratorTarget::GetLinkInformation(
  3674. const std::string& config) const
  3675. {
  3676. // Lookup any existing information for this configuration.
  3677. std::string key(cmSystemTools::UpperCase(config));
  3678. cmTargetLinkInformationMap::iterator i = this->LinkInformation.find(key);
  3679. if (i == this->LinkInformation.end()) {
  3680. // Compute information for this configuration.
  3681. cmComputeLinkInformation* info =
  3682. new cmComputeLinkInformation(this, config);
  3683. if (!info || !info->Compute()) {
  3684. delete info;
  3685. info = nullptr;
  3686. }
  3687. // Store the information for this configuration.
  3688. cmTargetLinkInformationMap::value_type entry(key, info);
  3689. i = this->LinkInformation.insert(entry).first;
  3690. if (info) {
  3691. this->CheckPropertyCompatibility(info, config);
  3692. }
  3693. }
  3694. return i->second;
  3695. }
  3696. void cmGeneratorTarget::GetTargetVersion(int& major, int& minor) const
  3697. {
  3698. int patch;
  3699. this->GetTargetVersion(false, major, minor, patch);
  3700. }
  3701. void cmGeneratorTarget::GetTargetVersion(bool soversion, int& major,
  3702. int& minor, int& patch) const
  3703. {
  3704. // Set the default values.
  3705. major = 0;
  3706. minor = 0;
  3707. patch = 0;
  3708. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  3709. // Look for a VERSION or SOVERSION property.
  3710. const char* prop = soversion ? "SOVERSION" : "VERSION";
  3711. if (const char* version = this->GetProperty(prop)) {
  3712. // Try to parse the version number and store the results that were
  3713. // successfully parsed.
  3714. int parsed_major;
  3715. int parsed_minor;
  3716. int parsed_patch;
  3717. switch (sscanf(version, "%d.%d.%d", &parsed_major, &parsed_minor,
  3718. &parsed_patch)) {
  3719. case 3:
  3720. patch = parsed_patch;
  3721. CM_FALLTHROUGH;
  3722. case 2:
  3723. minor = parsed_minor;
  3724. CM_FALLTHROUGH;
  3725. case 1:
  3726. major = parsed_major;
  3727. CM_FALLTHROUGH;
  3728. default:
  3729. break;
  3730. }
  3731. }
  3732. }
  3733. std::string cmGeneratorTarget::GetFortranModuleDirectory(
  3734. std::string const& working_dir) const
  3735. {
  3736. if (!this->FortranModuleDirectoryCreated) {
  3737. this->FortranModuleDirectory =
  3738. this->CreateFortranModuleDirectory(working_dir);
  3739. this->FortranModuleDirectoryCreated = true;
  3740. }
  3741. return this->FortranModuleDirectory;
  3742. }
  3743. std::string cmGeneratorTarget::CreateFortranModuleDirectory(
  3744. std::string const& working_dir) const
  3745. {
  3746. std::string mod_dir;
  3747. std::string target_mod_dir;
  3748. if (const char* prop = this->GetProperty("Fortran_MODULE_DIRECTORY")) {
  3749. target_mod_dir = prop;
  3750. } else {
  3751. std::string const& default_mod_dir =
  3752. this->LocalGenerator->GetCurrentBinaryDirectory();
  3753. if (default_mod_dir != working_dir) {
  3754. target_mod_dir = default_mod_dir;
  3755. }
  3756. }
  3757. const char* moddir_flag =
  3758. this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_FLAG");
  3759. if (!target_mod_dir.empty() && moddir_flag) {
  3760. // Compute the full path to the module directory.
  3761. if (cmSystemTools::FileIsFullPath(target_mod_dir)) {
  3762. // Already a full path.
  3763. mod_dir = target_mod_dir;
  3764. } else {
  3765. // Interpret relative to the current output directory.
  3766. mod_dir = this->LocalGenerator->GetCurrentBinaryDirectory();
  3767. mod_dir += "/";
  3768. mod_dir += target_mod_dir;
  3769. }
  3770. // Make sure the module output directory exists.
  3771. cmSystemTools::MakeDirectory(mod_dir);
  3772. }
  3773. return mod_dir;
  3774. }
  3775. std::string cmGeneratorTarget::GetFrameworkVersion() const
  3776. {
  3777. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  3778. if (const char* fversion = this->GetProperty("FRAMEWORK_VERSION")) {
  3779. return fversion;
  3780. }
  3781. if (const char* tversion = this->GetProperty("VERSION")) {
  3782. return tversion;
  3783. }
  3784. return "A";
  3785. }
  3786. void cmGeneratorTarget::ComputeVersionedName(std::string& vName,
  3787. std::string const& prefix,
  3788. std::string const& base,
  3789. std::string const& suffix,
  3790. std::string const& name,
  3791. const char* version) const
  3792. {
  3793. vName = this->Makefile->IsOn("APPLE") ? (prefix + base) : name;
  3794. if (version) {
  3795. vName += ".";
  3796. vName += version;
  3797. }
  3798. vName += this->Makefile->IsOn("APPLE") ? suffix : std::string();
  3799. }
  3800. std::vector<std::string> cmGeneratorTarget::GetPropertyKeys() const
  3801. {
  3802. cmPropertyMap const& propsObject = this->Target->GetProperties();
  3803. std::vector<std::string> props;
  3804. props.reserve(propsObject.size());
  3805. for (auto const& it : propsObject) {
  3806. props.push_back(it.first);
  3807. }
  3808. return props;
  3809. }
  3810. void cmGeneratorTarget::ReportPropertyOrigin(
  3811. const std::string& p, const std::string& result, const std::string& report,
  3812. const std::string& compatibilityType) const
  3813. {
  3814. std::vector<std::string> debugProperties;
  3815. const char* debugProp = this->Target->GetMakefile()->GetDefinition(
  3816. "CMAKE_DEBUG_TARGET_PROPERTIES");
  3817. if (debugProp) {
  3818. cmSystemTools::ExpandListArgument(debugProp, debugProperties);
  3819. }
  3820. bool debugOrigin = !this->DebugCompatiblePropertiesDone[p] &&
  3821. std::find(debugProperties.begin(), debugProperties.end(), p) !=
  3822. debugProperties.end();
  3823. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3824. this->DebugCompatiblePropertiesDone[p] = true;
  3825. }
  3826. if (!debugOrigin) {
  3827. return;
  3828. }
  3829. std::string areport = compatibilityType;
  3830. areport += std::string(" of property \"") + p + "\" for target \"";
  3831. areport += std::string(this->GetName());
  3832. areport += "\" (result: \"";
  3833. areport += result;
  3834. areport += "\"):\n" + report;
  3835. this->LocalGenerator->GetCMakeInstance()->IssueMessage(cmake::LOG, areport);
  3836. }
  3837. void cmGeneratorTarget::LookupLinkItems(std::vector<std::string> const& names,
  3838. std::vector<cmLinkItem>& items) const
  3839. {
  3840. for (std::string const& n : names) {
  3841. std::string name = this->CheckCMP0004(n);
  3842. if (name == this->GetName() || name.empty()) {
  3843. continue;
  3844. }
  3845. items.emplace_back(name, this->FindTargetToLink(name));
  3846. }
  3847. }
  3848. void cmGeneratorTarget::ExpandLinkItems(
  3849. std::string const& prop, std::string const& value, std::string const& config,
  3850. cmGeneratorTarget const* headTarget, bool usage_requirements_only,
  3851. std::vector<cmLinkItem>& items, bool& hadHeadSensitiveCondition) const
  3852. {
  3853. cmGeneratorExpression ge;
  3854. cmGeneratorExpressionDAGChecker dagChecker(this->GetName(), prop, nullptr,
  3855. nullptr);
  3856. // The $<LINK_ONLY> expression may be in a link interface to specify private
  3857. // link dependencies that are otherwise excluded from usage requirements.
  3858. if (usage_requirements_only) {
  3859. dagChecker.SetTransitivePropertiesOnly();
  3860. }
  3861. std::vector<std::string> libs;
  3862. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(value);
  3863. cmSystemTools::ExpandListArgument(cge->Evaluate(this->LocalGenerator, config,
  3864. false, headTarget, this,
  3865. &dagChecker),
  3866. libs);
  3867. this->LookupLinkItems(libs, items);
  3868. hadHeadSensitiveCondition = cge->GetHadHeadSensitiveCondition();
  3869. }
  3870. cmLinkInterface const* cmGeneratorTarget::GetLinkInterface(
  3871. const std::string& config, cmGeneratorTarget const* head) const
  3872. {
  3873. // Imported targets have their own link interface.
  3874. if (this->IsImported()) {
  3875. return this->GetImportLinkInterface(config, head, false);
  3876. }
  3877. // Link interfaces are not supported for executables that do not
  3878. // export symbols.
  3879. if (this->GetType() == cmStateEnums::EXECUTABLE &&
  3880. !this->IsExecutableWithExports()) {
  3881. return nullptr;
  3882. }
  3883. // Lookup any existing link interface for this configuration.
  3884. cmHeadToLinkInterfaceMap& hm = this->GetHeadToLinkInterfaceMap(config);
  3885. // If the link interface does not depend on the head target
  3886. // then return the one we computed first.
  3887. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  3888. return &hm.begin()->second;
  3889. }
  3890. cmOptionalLinkInterface& iface = hm[head];
  3891. if (!iface.LibrariesDone) {
  3892. iface.LibrariesDone = true;
  3893. this->ComputeLinkInterfaceLibraries(config, iface, head, false);
  3894. }
  3895. if (!iface.AllDone) {
  3896. iface.AllDone = true;
  3897. if (iface.Exists) {
  3898. this->ComputeLinkInterface(config, iface, head);
  3899. }
  3900. }
  3901. return iface.Exists ? &iface : nullptr;
  3902. }
  3903. void cmGeneratorTarget::ComputeLinkInterface(
  3904. const std::string& config, cmOptionalLinkInterface& iface,
  3905. cmGeneratorTarget const* headTarget) const
  3906. {
  3907. if (iface.ExplicitLibraries) {
  3908. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  3909. this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  3910. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  3911. // Shared libraries may have runtime implementation dependencies
  3912. // on other shared libraries that are not in the interface.
  3913. std::unordered_set<std::string> emitted;
  3914. for (cmLinkItem const& lib : iface.Libraries) {
  3915. emitted.insert(lib);
  3916. }
  3917. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  3918. cmLinkImplementation const* impl = this->GetLinkImplementation(config);
  3919. for (cmLinkImplItem const& lib : impl->Libraries) {
  3920. if (emitted.insert(lib).second) {
  3921. if (lib.Target) {
  3922. // This is a runtime dependency on another shared library.
  3923. if (lib.Target->GetType() == cmStateEnums::SHARED_LIBRARY) {
  3924. iface.SharedDeps.push_back(lib);
  3925. }
  3926. } else {
  3927. // TODO: Recognize shared library file names. Perhaps this
  3928. // should be moved to cmComputeLinkInformation, but that creates
  3929. // a chicken-and-egg problem since this list is needed for its
  3930. // construction.
  3931. }
  3932. }
  3933. }
  3934. }
  3935. }
  3936. } else if (this->GetPolicyStatusCMP0022() == cmPolicies::WARN ||
  3937. this->GetPolicyStatusCMP0022() == cmPolicies::OLD) {
  3938. // The link implementation is the default link interface.
  3939. cmLinkImplementationLibraries const* impl =
  3940. this->GetLinkImplementationLibrariesInternal(config, headTarget);
  3941. iface.ImplementationIsInterface = true;
  3942. iface.WrongConfigLibraries = impl->WrongConfigLibraries;
  3943. }
  3944. if (this->LinkLanguagePropagatesToDependents()) {
  3945. // Targets using this archive need its language runtime libraries.
  3946. if (cmLinkImplementation const* impl =
  3947. this->GetLinkImplementation(config)) {
  3948. iface.Languages = impl->Languages;
  3949. }
  3950. }
  3951. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  3952. // Construct the property name suffix for this configuration.
  3953. std::string suffix = "_";
  3954. if (!config.empty()) {
  3955. suffix += cmSystemTools::UpperCase(config);
  3956. } else {
  3957. suffix += "NOCONFIG";
  3958. }
  3959. // How many repetitions are needed if this library has cyclic
  3960. // dependencies?
  3961. std::string propName = "LINK_INTERFACE_MULTIPLICITY";
  3962. propName += suffix;
  3963. if (const char* config_reps = this->GetProperty(propName)) {
  3964. sscanf(config_reps, "%u", &iface.Multiplicity);
  3965. } else if (const char* reps =
  3966. this->GetProperty("LINK_INTERFACE_MULTIPLICITY")) {
  3967. sscanf(reps, "%u", &iface.Multiplicity);
  3968. }
  3969. }
  3970. }
  3971. const cmLinkInterfaceLibraries* cmGeneratorTarget::GetLinkInterfaceLibraries(
  3972. const std::string& config, cmGeneratorTarget const* head,
  3973. bool usage_requirements_only) const
  3974. {
  3975. // Imported targets have their own link interface.
  3976. if (this->IsImported()) {
  3977. return this->GetImportLinkInterface(config, head, usage_requirements_only);
  3978. }
  3979. // Link interfaces are not supported for executables that do not
  3980. // export symbols.
  3981. if (this->GetType() == cmStateEnums::EXECUTABLE &&
  3982. !this->IsExecutableWithExports()) {
  3983. return nullptr;
  3984. }
  3985. // Lookup any existing link interface for this configuration.
  3986. std::string CONFIG = cmSystemTools::UpperCase(config);
  3987. cmHeadToLinkInterfaceMap& hm =
  3988. (usage_requirements_only
  3989. ? this->GetHeadToLinkInterfaceUsageRequirementsMap(config)
  3990. : this->GetHeadToLinkInterfaceMap(config));
  3991. // If the link interface does not depend on the head target
  3992. // then return the one we computed first.
  3993. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  3994. return &hm.begin()->second;
  3995. }
  3996. cmOptionalLinkInterface& iface = hm[head];
  3997. if (!iface.LibrariesDone) {
  3998. iface.LibrariesDone = true;
  3999. this->ComputeLinkInterfaceLibraries(config, iface, head,
  4000. usage_requirements_only);
  4001. }
  4002. return iface.Exists ? &iface : nullptr;
  4003. }
  4004. std::string cmGeneratorTarget::GetDirectory(
  4005. const std::string& config, cmStateEnums::ArtifactType artifact) const
  4006. {
  4007. if (this->IsImported()) {
  4008. // Return the directory from which the target is imported.
  4009. return cmSystemTools::GetFilenamePath(
  4010. this->Target->ImportedGetFullPath(config, artifact));
  4011. }
  4012. if (OutputInfo const* info = this->GetOutputInfo(config)) {
  4013. // Return the directory in which the target will be built.
  4014. switch (artifact) {
  4015. case cmStateEnums::RuntimeBinaryArtifact:
  4016. return info->OutDir;
  4017. case cmStateEnums::ImportLibraryArtifact:
  4018. return info->ImpDir;
  4019. }
  4020. }
  4021. return "";
  4022. }
  4023. bool cmGeneratorTarget::UsesDefaultOutputDir(
  4024. const std::string& config, cmStateEnums::ArtifactType artifact) const
  4025. {
  4026. std::string dir;
  4027. return this->ComputeOutputDir(config, artifact, dir);
  4028. }
  4029. cmGeneratorTarget::OutputInfo const* cmGeneratorTarget::GetOutputInfo(
  4030. const std::string& config) const
  4031. {
  4032. // There is no output information for imported targets.
  4033. if (this->IsImported()) {
  4034. return nullptr;
  4035. }
  4036. // Only libraries and executables have well-defined output files.
  4037. if (!this->HaveWellDefinedOutputFiles()) {
  4038. std::string msg = "cmGeneratorTarget::GetOutputInfo called for ";
  4039. msg += this->GetName();
  4040. msg += " which has type ";
  4041. msg += cmState::GetTargetTypeName(this->GetType());
  4042. this->LocalGenerator->IssueMessage(cmake::INTERNAL_ERROR, msg);
  4043. return nullptr;
  4044. }
  4045. // Lookup/compute/cache the output information for this configuration.
  4046. std::string config_upper;
  4047. if (!config.empty()) {
  4048. config_upper = cmSystemTools::UpperCase(config);
  4049. }
  4050. OutputInfoMapType::iterator i = this->OutputInfoMap.find(config_upper);
  4051. if (i == this->OutputInfoMap.end()) {
  4052. // Add empty info in map to detect potential recursion.
  4053. OutputInfo info;
  4054. OutputInfoMapType::value_type entry(config_upper, info);
  4055. i = this->OutputInfoMap.insert(entry).first;
  4056. // Compute output directories.
  4057. this->ComputeOutputDir(config, cmStateEnums::RuntimeBinaryArtifact,
  4058. info.OutDir);
  4059. this->ComputeOutputDir(config, cmStateEnums::ImportLibraryArtifact,
  4060. info.ImpDir);
  4061. if (!this->ComputePDBOutputDir("PDB", config, info.PdbDir)) {
  4062. info.PdbDir = info.OutDir;
  4063. }
  4064. // Now update the previously-prepared map entry.
  4065. i->second = info;
  4066. } else if (i->second.empty()) {
  4067. // An empty map entry indicates we have been called recursively
  4068. // from the above block.
  4069. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  4070. cmake::FATAL_ERROR,
  4071. "Target '" + this->GetName() + "' OUTPUT_DIRECTORY depends on itself.",
  4072. this->GetBacktrace());
  4073. return nullptr;
  4074. }
  4075. return &i->second;
  4076. }
  4077. bool cmGeneratorTarget::ComputeOutputDir(const std::string& config,
  4078. cmStateEnums::ArtifactType artifact,
  4079. std::string& out) const
  4080. {
  4081. bool usesDefaultOutputDir = false;
  4082. std::string conf = config;
  4083. // Look for a target property defining the target output directory
  4084. // based on the target type.
  4085. std::string targetTypeName = this->GetOutputTargetType(artifact);
  4086. const char* propertyName = nullptr;
  4087. std::string propertyNameStr = targetTypeName;
  4088. if (!propertyNameStr.empty()) {
  4089. propertyNameStr += "_OUTPUT_DIRECTORY";
  4090. propertyName = propertyNameStr.c_str();
  4091. }
  4092. // Check for a per-configuration output directory target property.
  4093. std::string configUpper = cmSystemTools::UpperCase(conf);
  4094. const char* configProp = nullptr;
  4095. std::string configPropStr = targetTypeName;
  4096. if (!configPropStr.empty()) {
  4097. configPropStr += "_OUTPUT_DIRECTORY_";
  4098. configPropStr += configUpper;
  4099. configProp = configPropStr.c_str();
  4100. }
  4101. // Select an output directory.
  4102. if (const char* config_outdir = this->GetProperty(configProp)) {
  4103. // Use the user-specified per-configuration output directory.
  4104. cmGeneratorExpression ge;
  4105. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  4106. ge.Parse(config_outdir);
  4107. out = cge->Evaluate(this->LocalGenerator, config);
  4108. // Skip per-configuration subdirectory.
  4109. conf.clear();
  4110. } else if (const char* outdir = this->GetProperty(propertyName)) {
  4111. // Use the user-specified output directory.
  4112. cmGeneratorExpression ge;
  4113. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(outdir);
  4114. out = cge->Evaluate(this->LocalGenerator, config);
  4115. // Skip per-configuration subdirectory if the value contained a
  4116. // generator expression.
  4117. if (out != outdir) {
  4118. conf.clear();
  4119. }
  4120. } else if (this->GetType() == cmStateEnums::EXECUTABLE) {
  4121. // Lookup the output path for executables.
  4122. out = this->Makefile->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  4123. } else if (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  4124. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4125. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  4126. // Lookup the output path for libraries.
  4127. out = this->Makefile->GetSafeDefinition("LIBRARY_OUTPUT_PATH");
  4128. }
  4129. if (out.empty()) {
  4130. // Default to the current output directory.
  4131. usesDefaultOutputDir = true;
  4132. out = ".";
  4133. }
  4134. // Convert the output path to a full path in case it is
  4135. // specified as a relative path. Treat a relative path as
  4136. // relative to the current output directory for this makefile.
  4137. out = (cmSystemTools::CollapseFullPath(
  4138. out, this->LocalGenerator->GetCurrentBinaryDirectory()));
  4139. // The generator may add the configuration's subdirectory.
  4140. if (!conf.empty()) {
  4141. bool useEPN =
  4142. this->GlobalGenerator->UseEffectivePlatformName(this->Makefile);
  4143. std::string suffix =
  4144. usesDefaultOutputDir && useEPN ? "${EFFECTIVE_PLATFORM_NAME}" : "";
  4145. this->LocalGenerator->GetGlobalGenerator()->AppendDirectoryForConfig(
  4146. "/", conf, suffix, out);
  4147. }
  4148. return usesDefaultOutputDir;
  4149. }
  4150. bool cmGeneratorTarget::ComputePDBOutputDir(const std::string& kind,
  4151. const std::string& config,
  4152. std::string& out) const
  4153. {
  4154. // Look for a target property defining the target output directory
  4155. // based on the target type.
  4156. const char* propertyName = nullptr;
  4157. std::string propertyNameStr = kind;
  4158. if (!propertyNameStr.empty()) {
  4159. propertyNameStr += "_OUTPUT_DIRECTORY";
  4160. propertyName = propertyNameStr.c_str();
  4161. }
  4162. std::string conf = config;
  4163. // Check for a per-configuration output directory target property.
  4164. std::string configUpper = cmSystemTools::UpperCase(conf);
  4165. const char* configProp = nullptr;
  4166. std::string configPropStr = kind;
  4167. if (!configPropStr.empty()) {
  4168. configPropStr += "_OUTPUT_DIRECTORY_";
  4169. configPropStr += configUpper;
  4170. configProp = configPropStr.c_str();
  4171. }
  4172. // Select an output directory.
  4173. if (const char* config_outdir = this->GetProperty(configProp)) {
  4174. // Use the user-specified per-configuration output directory.
  4175. cmGeneratorExpression ge;
  4176. std::unique_ptr<cmCompiledGeneratorExpression> cge =
  4177. ge.Parse(config_outdir);
  4178. out = cge->Evaluate(this->LocalGenerator, config);
  4179. // Skip per-configuration subdirectory.
  4180. conf.clear();
  4181. } else if (const char* outdir = this->GetProperty(propertyName)) {
  4182. // Use the user-specified output directory.
  4183. cmGeneratorExpression ge;
  4184. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(outdir);
  4185. out = cge->Evaluate(this->LocalGenerator, config);
  4186. // Skip per-configuration subdirectory if the value contained a
  4187. // generator expression.
  4188. if (out != outdir) {
  4189. conf.clear();
  4190. }
  4191. }
  4192. if (out.empty()) {
  4193. return false;
  4194. }
  4195. // Convert the output path to a full path in case it is
  4196. // specified as a relative path. Treat a relative path as
  4197. // relative to the current output directory for this makefile.
  4198. out = (cmSystemTools::CollapseFullPath(
  4199. out, this->LocalGenerator->GetCurrentBinaryDirectory()));
  4200. // The generator may add the configuration's subdirectory.
  4201. if (!conf.empty()) {
  4202. this->LocalGenerator->GetGlobalGenerator()->AppendDirectoryForConfig(
  4203. "/", conf, "", out);
  4204. }
  4205. return true;
  4206. }
  4207. bool cmGeneratorTarget::HaveInstallTreeRPATH() const
  4208. {
  4209. const char* install_rpath = this->GetProperty("INSTALL_RPATH");
  4210. return (install_rpath && *install_rpath) &&
  4211. !this->Makefile->IsOn("CMAKE_SKIP_INSTALL_RPATH");
  4212. }
  4213. void cmGeneratorTarget::ComputeLinkInterfaceLibraries(
  4214. const std::string& config, cmOptionalLinkInterface& iface,
  4215. cmGeneratorTarget const* headTarget, bool usage_requirements_only) const
  4216. {
  4217. // Construct the property name suffix for this configuration.
  4218. std::string suffix = "_";
  4219. if (!config.empty()) {
  4220. suffix += cmSystemTools::UpperCase(config);
  4221. } else {
  4222. suffix += "NOCONFIG";
  4223. }
  4224. // An explicit list of interface libraries may be set for shared
  4225. // libraries and executables that export symbols.
  4226. const char* explicitLibraries = nullptr;
  4227. std::string linkIfaceProp;
  4228. if (this->GetPolicyStatusCMP0022() != cmPolicies::OLD &&
  4229. this->GetPolicyStatusCMP0022() != cmPolicies::WARN) {
  4230. // CMP0022 NEW behavior is to use INTERFACE_LINK_LIBRARIES.
  4231. linkIfaceProp = "INTERFACE_LINK_LIBRARIES";
  4232. explicitLibraries = this->GetProperty(linkIfaceProp);
  4233. } else if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4234. this->IsExecutableWithExports()) {
  4235. // CMP0022 OLD behavior is to use LINK_INTERFACE_LIBRARIES if set on a
  4236. // shared lib or executable.
  4237. // Lookup the per-configuration property.
  4238. linkIfaceProp = "LINK_INTERFACE_LIBRARIES";
  4239. linkIfaceProp += suffix;
  4240. explicitLibraries = this->GetProperty(linkIfaceProp);
  4241. // If not set, try the generic property.
  4242. if (!explicitLibraries) {
  4243. linkIfaceProp = "LINK_INTERFACE_LIBRARIES";
  4244. explicitLibraries = this->GetProperty(linkIfaceProp);
  4245. }
  4246. }
  4247. if (explicitLibraries &&
  4248. this->GetPolicyStatusCMP0022() == cmPolicies::WARN &&
  4249. !this->PolicyWarnedCMP0022) {
  4250. // Compare the explicitly set old link interface properties to the
  4251. // preferred new link interface property one and warn if different.
  4252. const char* newExplicitLibraries =
  4253. this->GetProperty("INTERFACE_LINK_LIBRARIES");
  4254. if (newExplicitLibraries &&
  4255. strcmp(newExplicitLibraries, explicitLibraries) != 0) {
  4256. std::ostringstream w;
  4257. /* clang-format off */
  4258. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0022) << "\n"
  4259. "Target \"" << this->GetName() << "\" has an "
  4260. "INTERFACE_LINK_LIBRARIES property which differs from its " <<
  4261. linkIfaceProp << " properties."
  4262. "\n"
  4263. "INTERFACE_LINK_LIBRARIES:\n"
  4264. " " << newExplicitLibraries << "\n" <<
  4265. linkIfaceProp << ":\n"
  4266. " " << explicitLibraries << "\n";
  4267. /* clang-format on */
  4268. this->LocalGenerator->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  4269. this->PolicyWarnedCMP0022 = true;
  4270. }
  4271. }
  4272. // There is no implicit link interface for executables or modules
  4273. // so if none was explicitly set then there is no link interface.
  4274. if (!explicitLibraries &&
  4275. (this->GetType() == cmStateEnums::EXECUTABLE ||
  4276. (this->GetType() == cmStateEnums::MODULE_LIBRARY))) {
  4277. return;
  4278. }
  4279. iface.Exists = true;
  4280. iface.ExplicitLibraries = explicitLibraries;
  4281. if (explicitLibraries) {
  4282. // The interface libraries have been explicitly set.
  4283. this->ExpandLinkItems(linkIfaceProp, explicitLibraries, config, headTarget,
  4284. usage_requirements_only, iface.Libraries,
  4285. iface.HadHeadSensitiveCondition);
  4286. } else if (this->GetPolicyStatusCMP0022() == cmPolicies::WARN ||
  4287. this->GetPolicyStatusCMP0022() == cmPolicies::OLD)
  4288. // If CMP0022 is NEW then the plain tll signature sets the
  4289. // INTERFACE_LINK_LIBRARIES, so if we get here then the project
  4290. // cleared the property explicitly and we should not fall back
  4291. // to the link implementation.
  4292. {
  4293. // The link implementation is the default link interface.
  4294. cmLinkImplementationLibraries const* impl =
  4295. this->GetLinkImplementationLibrariesInternal(config, headTarget);
  4296. iface.Libraries.insert(iface.Libraries.end(), impl->Libraries.begin(),
  4297. impl->Libraries.end());
  4298. if (this->GetPolicyStatusCMP0022() == cmPolicies::WARN &&
  4299. !this->PolicyWarnedCMP0022 && !usage_requirements_only) {
  4300. // Compare the link implementation fallback link interface to the
  4301. // preferred new link interface property and warn if different.
  4302. std::vector<cmLinkItem> ifaceLibs;
  4303. static const std::string newProp = "INTERFACE_LINK_LIBRARIES";
  4304. if (const char* newExplicitLibraries = this->GetProperty(newProp)) {
  4305. bool hadHeadSensitiveConditionDummy = false;
  4306. this->ExpandLinkItems(newProp, newExplicitLibraries, config,
  4307. headTarget, usage_requirements_only, ifaceLibs,
  4308. hadHeadSensitiveConditionDummy);
  4309. }
  4310. if (ifaceLibs != iface.Libraries) {
  4311. std::string oldLibraries = cmJoin(impl->Libraries, ";");
  4312. std::string newLibraries = cmJoin(ifaceLibs, ";");
  4313. if (oldLibraries.empty()) {
  4314. oldLibraries = "(empty)";
  4315. }
  4316. if (newLibraries.empty()) {
  4317. newLibraries = "(empty)";
  4318. }
  4319. std::ostringstream w;
  4320. /* clang-format off */
  4321. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0022) << "\n"
  4322. "Target \"" << this->GetName() << "\" has an "
  4323. "INTERFACE_LINK_LIBRARIES property. "
  4324. "This should be preferred as the source of the link interface "
  4325. "for this library but because CMP0022 is not set CMake is "
  4326. "ignoring the property and using the link implementation "
  4327. "as the link interface instead."
  4328. "\n"
  4329. "INTERFACE_LINK_LIBRARIES:\n"
  4330. " " << newLibraries << "\n"
  4331. "Link implementation:\n"
  4332. " " << oldLibraries << "\n";
  4333. /* clang-format on */
  4334. this->LocalGenerator->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  4335. this->PolicyWarnedCMP0022 = true;
  4336. }
  4337. }
  4338. }
  4339. }
  4340. const cmLinkInterface* cmGeneratorTarget::GetImportLinkInterface(
  4341. const std::string& config, cmGeneratorTarget const* headTarget,
  4342. bool usage_requirements_only) const
  4343. {
  4344. cmGeneratorTarget::ImportInfo const* info = this->GetImportInfo(config);
  4345. if (!info) {
  4346. return nullptr;
  4347. }
  4348. std::string CONFIG = cmSystemTools::UpperCase(config);
  4349. cmHeadToLinkInterfaceMap& hm =
  4350. (usage_requirements_only
  4351. ? this->GetHeadToLinkInterfaceUsageRequirementsMap(config)
  4352. : this->GetHeadToLinkInterfaceMap(config));
  4353. // If the link interface does not depend on the head target
  4354. // then return the one we computed first.
  4355. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  4356. return &hm.begin()->second;
  4357. }
  4358. cmOptionalLinkInterface& iface = hm[headTarget];
  4359. if (!iface.AllDone) {
  4360. iface.AllDone = true;
  4361. iface.Multiplicity = info->Multiplicity;
  4362. cmSystemTools::ExpandListArgument(info->Languages, iface.Languages);
  4363. this->ExpandLinkItems(info->LibrariesProp, info->Libraries, config,
  4364. headTarget, usage_requirements_only, iface.Libraries,
  4365. iface.HadHeadSensitiveCondition);
  4366. std::vector<std::string> deps;
  4367. cmSystemTools::ExpandListArgument(info->SharedDeps, deps);
  4368. this->LookupLinkItems(deps, iface.SharedDeps);
  4369. }
  4370. return &iface;
  4371. }
  4372. cmGeneratorTarget::ImportInfo const* cmGeneratorTarget::GetImportInfo(
  4373. const std::string& config) const
  4374. {
  4375. // There is no imported information for non-imported targets.
  4376. if (!this->IsImported()) {
  4377. return nullptr;
  4378. }
  4379. // Lookup/compute/cache the import information for this
  4380. // configuration.
  4381. std::string config_upper;
  4382. if (!config.empty()) {
  4383. config_upper = cmSystemTools::UpperCase(config);
  4384. } else {
  4385. config_upper = "NOCONFIG";
  4386. }
  4387. ImportInfoMapType::const_iterator i = this->ImportInfoMap.find(config_upper);
  4388. if (i == this->ImportInfoMap.end()) {
  4389. ImportInfo info;
  4390. this->ComputeImportInfo(config_upper, info);
  4391. ImportInfoMapType::value_type entry(config_upper, info);
  4392. i = this->ImportInfoMap.insert(entry).first;
  4393. }
  4394. if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  4395. return &i->second;
  4396. }
  4397. // If the location is empty then the target is not available for
  4398. // this configuration.
  4399. if (i->second.Location.empty() && i->second.ImportLibrary.empty()) {
  4400. return nullptr;
  4401. }
  4402. // Return the import information.
  4403. return &i->second;
  4404. }
  4405. void cmGeneratorTarget::ComputeImportInfo(std::string const& desired_config,
  4406. ImportInfo& info) const
  4407. {
  4408. // This method finds information about an imported target from its
  4409. // properties. The "IMPORTED_" namespace is reserved for properties
  4410. // defined by the project exporting the target.
  4411. // Initialize members.
  4412. info.NoSOName = false;
  4413. const char* loc = nullptr;
  4414. const char* imp = nullptr;
  4415. std::string suffix;
  4416. if (!this->Target->GetMappedConfig(desired_config, &loc, &imp, suffix)) {
  4417. return;
  4418. }
  4419. // Get the link interface.
  4420. {
  4421. std::string linkProp = "INTERFACE_LINK_LIBRARIES";
  4422. const char* propertyLibs = this->GetProperty(linkProp);
  4423. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  4424. if (!propertyLibs) {
  4425. linkProp = "IMPORTED_LINK_INTERFACE_LIBRARIES";
  4426. linkProp += suffix;
  4427. propertyLibs = this->GetProperty(linkProp);
  4428. }
  4429. if (!propertyLibs) {
  4430. linkProp = "IMPORTED_LINK_INTERFACE_LIBRARIES";
  4431. propertyLibs = this->GetProperty(linkProp);
  4432. }
  4433. }
  4434. if (propertyLibs) {
  4435. info.LibrariesProp = linkProp;
  4436. info.Libraries = propertyLibs;
  4437. }
  4438. }
  4439. if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  4440. if (loc) {
  4441. info.LibName = loc;
  4442. }
  4443. return;
  4444. }
  4445. // A provided configuration has been chosen. Load the
  4446. // configuration's properties.
  4447. // Get the location.
  4448. if (loc) {
  4449. info.Location = loc;
  4450. } else {
  4451. std::string impProp = "IMPORTED_LOCATION";
  4452. impProp += suffix;
  4453. if (const char* config_location = this->GetProperty(impProp)) {
  4454. info.Location = config_location;
  4455. } else if (const char* location = this->GetProperty("IMPORTED_LOCATION")) {
  4456. info.Location = location;
  4457. }
  4458. }
  4459. // Get the soname.
  4460. if (this->GetType() == cmStateEnums::SHARED_LIBRARY) {
  4461. std::string soProp = "IMPORTED_SONAME";
  4462. soProp += suffix;
  4463. if (const char* config_soname = this->GetProperty(soProp)) {
  4464. info.SOName = config_soname;
  4465. } else if (const char* soname = this->GetProperty("IMPORTED_SONAME")) {
  4466. info.SOName = soname;
  4467. }
  4468. }
  4469. // Get the "no-soname" mark.
  4470. if (this->GetType() == cmStateEnums::SHARED_LIBRARY) {
  4471. std::string soProp = "IMPORTED_NO_SONAME";
  4472. soProp += suffix;
  4473. if (const char* config_no_soname = this->GetProperty(soProp)) {
  4474. info.NoSOName = cmSystemTools::IsOn(config_no_soname);
  4475. } else if (const char* no_soname =
  4476. this->GetProperty("IMPORTED_NO_SONAME")) {
  4477. info.NoSOName = cmSystemTools::IsOn(no_soname);
  4478. }
  4479. }
  4480. // Get the import library.
  4481. if (imp) {
  4482. info.ImportLibrary = imp;
  4483. } else if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4484. this->IsExecutableWithExports()) {
  4485. std::string impProp = "IMPORTED_IMPLIB";
  4486. impProp += suffix;
  4487. if (const char* config_implib = this->GetProperty(impProp)) {
  4488. info.ImportLibrary = config_implib;
  4489. } else if (const char* implib = this->GetProperty("IMPORTED_IMPLIB")) {
  4490. info.ImportLibrary = implib;
  4491. }
  4492. }
  4493. // Get the link dependencies.
  4494. {
  4495. std::string linkProp = "IMPORTED_LINK_DEPENDENT_LIBRARIES";
  4496. linkProp += suffix;
  4497. if (const char* config_libs = this->GetProperty(linkProp)) {
  4498. info.SharedDeps = config_libs;
  4499. } else if (const char* libs =
  4500. this->GetProperty("IMPORTED_LINK_DEPENDENT_LIBRARIES")) {
  4501. info.SharedDeps = libs;
  4502. }
  4503. }
  4504. // Get the link languages.
  4505. if (this->LinkLanguagePropagatesToDependents()) {
  4506. std::string linkProp = "IMPORTED_LINK_INTERFACE_LANGUAGES";
  4507. linkProp += suffix;
  4508. if (const char* config_libs = this->GetProperty(linkProp)) {
  4509. info.Languages = config_libs;
  4510. } else if (const char* libs =
  4511. this->GetProperty("IMPORTED_LINK_INTERFACE_LANGUAGES")) {
  4512. info.Languages = libs;
  4513. }
  4514. }
  4515. // Get information if target is managed assembly.
  4516. {
  4517. std::string linkProp = "IMPORTED_COMMON_LANGUAGE_RUNTIME";
  4518. if (auto pc = this->GetProperty(linkProp + suffix)) {
  4519. info.Managed = this->CheckManagedType(pc);
  4520. } else if (auto p = this->GetProperty(linkProp)) {
  4521. info.Managed = this->CheckManagedType(p);
  4522. }
  4523. }
  4524. // Get the cyclic repetition count.
  4525. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  4526. std::string linkProp = "IMPORTED_LINK_INTERFACE_MULTIPLICITY";
  4527. linkProp += suffix;
  4528. if (const char* config_reps = this->GetProperty(linkProp)) {
  4529. sscanf(config_reps, "%u", &info.Multiplicity);
  4530. } else if (const char* reps =
  4531. this->GetProperty("IMPORTED_LINK_INTERFACE_MULTIPLICITY")) {
  4532. sscanf(reps, "%u", &info.Multiplicity);
  4533. }
  4534. }
  4535. }
  4536. cmHeadToLinkInterfaceMap& cmGeneratorTarget::GetHeadToLinkInterfaceMap(
  4537. const std::string& config) const
  4538. {
  4539. std::string CONFIG = cmSystemTools::UpperCase(config);
  4540. return this->LinkInterfaceMap[CONFIG];
  4541. }
  4542. cmHeadToLinkInterfaceMap&
  4543. cmGeneratorTarget::GetHeadToLinkInterfaceUsageRequirementsMap(
  4544. const std::string& config) const
  4545. {
  4546. std::string CONFIG = cmSystemTools::UpperCase(config);
  4547. return this->LinkInterfaceUsageRequirementsOnlyMap[CONFIG];
  4548. }
  4549. const cmLinkImplementation* cmGeneratorTarget::GetLinkImplementation(
  4550. const std::string& config) const
  4551. {
  4552. // There is no link implementation for imported targets.
  4553. if (this->IsImported()) {
  4554. return nullptr;
  4555. }
  4556. std::string CONFIG = cmSystemTools::UpperCase(config);
  4557. cmOptionalLinkImplementation& impl = this->LinkImplMap[CONFIG][this];
  4558. if (!impl.LibrariesDone) {
  4559. impl.LibrariesDone = true;
  4560. this->ComputeLinkImplementationLibraries(config, impl, this);
  4561. }
  4562. if (!impl.LanguagesDone) {
  4563. impl.LanguagesDone = true;
  4564. this->ComputeLinkImplementationLanguages(config, impl);
  4565. }
  4566. return &impl;
  4567. }
  4568. bool cmGeneratorTarget::GetConfigCommonSourceFiles(
  4569. std::vector<cmSourceFile*>& files) const
  4570. {
  4571. std::vector<std::string> configs;
  4572. this->Makefile->GetConfigurations(configs);
  4573. if (configs.empty()) {
  4574. configs.emplace_back();
  4575. }
  4576. std::vector<std::string>::const_iterator it = configs.begin();
  4577. const std::string& firstConfig = *it;
  4578. this->GetSourceFilesWithoutObjectLibraries(files, firstConfig);
  4579. for (; it != configs.end(); ++it) {
  4580. std::vector<cmSourceFile*> configFiles;
  4581. this->GetSourceFilesWithoutObjectLibraries(configFiles, *it);
  4582. if (configFiles != files) {
  4583. std::string firstConfigFiles;
  4584. const char* sep = "";
  4585. for (cmSourceFile* f : files) {
  4586. firstConfigFiles += sep;
  4587. firstConfigFiles += f->GetFullPath();
  4588. sep = "\n ";
  4589. }
  4590. std::string thisConfigFiles;
  4591. sep = "";
  4592. for (cmSourceFile* f : configFiles) {
  4593. thisConfigFiles += sep;
  4594. thisConfigFiles += f->GetFullPath();
  4595. sep = "\n ";
  4596. }
  4597. std::ostringstream e;
  4598. /* clang-format off */
  4599. e << "Target \"" << this->GetName()
  4600. << "\" has source files which vary by "
  4601. "configuration. This is not supported by the \""
  4602. << this->GlobalGenerator->GetName()
  4603. << "\" generator.\n"
  4604. "Config \"" << firstConfig << "\":\n"
  4605. " " << firstConfigFiles << "\n"
  4606. "Config \"" << *it << "\":\n"
  4607. " " << thisConfigFiles << "\n";
  4608. /* clang-format on */
  4609. this->LocalGenerator->IssueMessage(cmake::FATAL_ERROR, e.str());
  4610. return false;
  4611. }
  4612. }
  4613. return true;
  4614. }
  4615. void cmGeneratorTarget::GetObjectLibrariesCMP0026(
  4616. std::vector<cmGeneratorTarget*>& objlibs) const
  4617. {
  4618. // At configure-time, this method can be called as part of getting the
  4619. // LOCATION property or to export() a file to be include()d. However
  4620. // there is no cmGeneratorTarget at configure-time, so search the SOURCES
  4621. // for TARGET_OBJECTS instead for backwards compatibility with OLD
  4622. // behavior of CMP0024 and CMP0026 only.
  4623. cmStringRange rng = this->Target->GetSourceEntries();
  4624. for (std::string const& entry : rng) {
  4625. std::vector<std::string> files;
  4626. cmSystemTools::ExpandListArgument(entry, files);
  4627. for (std::string const& li : files) {
  4628. if (cmHasLiteralPrefix(li, "$<TARGET_OBJECTS:") &&
  4629. li[li.size() - 1] == '>') {
  4630. std::string objLibName = li.substr(17, li.size() - 18);
  4631. if (cmGeneratorExpression::Find(objLibName) != std::string::npos) {
  4632. continue;
  4633. }
  4634. cmGeneratorTarget* objLib =
  4635. this->LocalGenerator->FindGeneratorTargetToUse(objLibName);
  4636. if (objLib) {
  4637. objlibs.push_back(objLib);
  4638. }
  4639. }
  4640. }
  4641. }
  4642. }
  4643. std::string cmGeneratorTarget::CheckCMP0004(std::string const& item) const
  4644. {
  4645. // Strip whitespace off the library names because we used to do this
  4646. // in case variables were expanded at generate time. We no longer
  4647. // do the expansion but users link to libraries like " ${VAR} ".
  4648. std::string lib = item;
  4649. std::string::size_type pos = lib.find_first_not_of(" \t\r\n");
  4650. if (pos != std::string::npos) {
  4651. lib = lib.substr(pos);
  4652. }
  4653. pos = lib.find_last_not_of(" \t\r\n");
  4654. if (pos != std::string::npos) {
  4655. lib = lib.substr(0, pos + 1);
  4656. }
  4657. if (lib != item) {
  4658. cmake* cm = this->LocalGenerator->GetCMakeInstance();
  4659. switch (this->GetPolicyStatusCMP0004()) {
  4660. case cmPolicies::WARN: {
  4661. std::ostringstream w;
  4662. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0004) << "\n"
  4663. << "Target \"" << this->GetName() << "\" links to item \"" << item
  4664. << "\" which has leading or trailing whitespace.";
  4665. cm->IssueMessage(cmake::AUTHOR_WARNING, w.str(), this->GetBacktrace());
  4666. }
  4667. case cmPolicies::OLD:
  4668. break;
  4669. case cmPolicies::NEW: {
  4670. std::ostringstream e;
  4671. e << "Target \"" << this->GetName() << "\" links to item \"" << item
  4672. << "\" which has leading or trailing whitespace. "
  4673. << "This is now an error according to policy CMP0004.";
  4674. cm->IssueMessage(cmake::FATAL_ERROR, e.str(), this->GetBacktrace());
  4675. } break;
  4676. case cmPolicies::REQUIRED_IF_USED:
  4677. case cmPolicies::REQUIRED_ALWAYS: {
  4678. std::ostringstream e;
  4679. e << cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0004) << "\n"
  4680. << "Target \"" << this->GetName() << "\" links to item \"" << item
  4681. << "\" which has leading or trailing whitespace.";
  4682. cm->IssueMessage(cmake::FATAL_ERROR, e.str(), this->GetBacktrace());
  4683. } break;
  4684. }
  4685. }
  4686. return lib;
  4687. }
  4688. void cmGeneratorTarget::GetLanguages(std::set<std::string>& languages,
  4689. const std::string& config) const
  4690. {
  4691. std::vector<cmSourceFile*> sourceFiles;
  4692. this->GetSourceFiles(sourceFiles, config);
  4693. for (cmSourceFile* src : sourceFiles) {
  4694. const std::string& lang = src->GetLanguage();
  4695. if (!lang.empty()) {
  4696. languages.insert(lang);
  4697. }
  4698. }
  4699. std::vector<cmGeneratorTarget*> objectLibraries;
  4700. std::vector<cmSourceFile const*> externalObjects;
  4701. if (!this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  4702. std::vector<cmGeneratorTarget*> objectTargets;
  4703. this->GetObjectLibrariesCMP0026(objectTargets);
  4704. objectLibraries.reserve(objectTargets.size());
  4705. for (cmGeneratorTarget* gt : objectTargets) {
  4706. objectLibraries.push_back(gt);
  4707. }
  4708. } else {
  4709. this->GetExternalObjects(externalObjects, config);
  4710. for (cmSourceFile const* extObj : externalObjects) {
  4711. std::string objLib = extObj->GetObjectLibrary();
  4712. if (cmGeneratorTarget* tgt =
  4713. this->LocalGenerator->FindGeneratorTargetToUse(objLib)) {
  4714. auto const objLibIt =
  4715. std::find_if(objectLibraries.cbegin(), objectLibraries.cend(),
  4716. [tgt](cmGeneratorTarget* t) { return t == tgt; });
  4717. if (objectLibraries.cend() == objLibIt) {
  4718. objectLibraries.push_back(tgt);
  4719. }
  4720. }
  4721. }
  4722. }
  4723. for (cmGeneratorTarget* objLib : objectLibraries) {
  4724. objLib->GetLanguages(languages, config);
  4725. }
  4726. }
  4727. bool cmGeneratorTarget::HasLanguage(std::string const& language,
  4728. std::string const& config,
  4729. bool exclusive) const
  4730. {
  4731. std::set<std::string> languages;
  4732. this->GetLanguages(languages, config);
  4733. // add linker language (if it is different from compiler languages)
  4734. languages.insert(this->GetLinkerLanguage(config));
  4735. return (languages.size() == 1 || !exclusive) &&
  4736. languages.count(language) > 0;
  4737. }
  4738. void cmGeneratorTarget::ComputeLinkImplementationLanguages(
  4739. const std::string& config, cmOptionalLinkImplementation& impl) const
  4740. {
  4741. // This target needs runtime libraries for its source languages.
  4742. std::set<std::string> languages;
  4743. // Get languages used in our source files.
  4744. this->GetLanguages(languages, config);
  4745. // Copy the set of languages to the link implementation.
  4746. impl.Languages.insert(impl.Languages.begin(), languages.begin(),
  4747. languages.end());
  4748. }
  4749. bool cmGeneratorTarget::HaveBuildTreeRPATH(const std::string& config) const
  4750. {
  4751. if (this->GetPropertyAsBool("SKIP_BUILD_RPATH")) {
  4752. return false;
  4753. }
  4754. if (this->GetProperty("BUILD_RPATH")) {
  4755. return true;
  4756. }
  4757. if (cmLinkImplementationLibraries const* impl =
  4758. this->GetLinkImplementationLibraries(config)) {
  4759. return !impl->Libraries.empty();
  4760. }
  4761. return false;
  4762. }
  4763. cmLinkImplementationLibraries const*
  4764. cmGeneratorTarget::GetLinkImplementationLibraries(
  4765. const std::string& config) const
  4766. {
  4767. return this->GetLinkImplementationLibrariesInternal(config, this);
  4768. }
  4769. cmLinkImplementationLibraries const*
  4770. cmGeneratorTarget::GetLinkImplementationLibrariesInternal(
  4771. const std::string& config, cmGeneratorTarget const* head) const
  4772. {
  4773. // There is no link implementation for imported targets.
  4774. if (this->IsImported()) {
  4775. return nullptr;
  4776. }
  4777. // Populate the link implementation libraries for this configuration.
  4778. std::string CONFIG = cmSystemTools::UpperCase(config);
  4779. HeadToLinkImplementationMap& hm = this->LinkImplMap[CONFIG];
  4780. // If the link implementation does not depend on the head target
  4781. // then return the one we computed first.
  4782. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  4783. return &hm.begin()->second;
  4784. }
  4785. cmOptionalLinkImplementation& impl = hm[head];
  4786. if (!impl.LibrariesDone) {
  4787. impl.LibrariesDone = true;
  4788. this->ComputeLinkImplementationLibraries(config, impl, head);
  4789. }
  4790. return &impl;
  4791. }
  4792. bool cmGeneratorTarget::IsNullImpliedByLinkLibraries(
  4793. const std::string& p) const
  4794. {
  4795. return this->LinkImplicitNullProperties.find(p) !=
  4796. this->LinkImplicitNullProperties.end();
  4797. }
  4798. void cmGeneratorTarget::ComputeLinkImplementationLibraries(
  4799. const std::string& config, cmOptionalLinkImplementation& impl,
  4800. cmGeneratorTarget const* head) const
  4801. {
  4802. cmStringRange entryRange = this->Target->GetLinkImplementationEntries();
  4803. cmBacktraceRange btRange = this->Target->GetLinkImplementationBacktraces();
  4804. cmBacktraceRange::const_iterator btIt = btRange.begin();
  4805. // Collect libraries directly linked in this configuration.
  4806. for (cmStringRange::const_iterator le = entryRange.begin(),
  4807. end = entryRange.end();
  4808. le != end; ++le, ++btIt) {
  4809. std::vector<std::string> llibs;
  4810. cmGeneratorExpressionDAGChecker dagChecker(
  4811. this->GetName(), "LINK_LIBRARIES", nullptr, nullptr);
  4812. cmGeneratorExpression ge(*btIt);
  4813. std::unique_ptr<cmCompiledGeneratorExpression> const cge = ge.Parse(*le);
  4814. std::string const evaluated =
  4815. cge->Evaluate(this->LocalGenerator, config, false, head, &dagChecker);
  4816. cmSystemTools::ExpandListArgument(evaluated, llibs);
  4817. if (cge->GetHadHeadSensitiveCondition()) {
  4818. impl.HadHeadSensitiveCondition = true;
  4819. }
  4820. for (std::string const& lib : llibs) {
  4821. // Skip entries that resolve to the target itself or are empty.
  4822. std::string name = this->CheckCMP0004(lib);
  4823. if (name == this->GetName() || name.empty()) {
  4824. if (name == this->GetName()) {
  4825. bool noMessage = false;
  4826. cmake::MessageType messageType = cmake::FATAL_ERROR;
  4827. std::ostringstream e;
  4828. switch (this->GetPolicyStatusCMP0038()) {
  4829. case cmPolicies::WARN: {
  4830. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0038) << "\n";
  4831. messageType = cmake::AUTHOR_WARNING;
  4832. } break;
  4833. case cmPolicies::OLD:
  4834. noMessage = true;
  4835. case cmPolicies::REQUIRED_IF_USED:
  4836. case cmPolicies::REQUIRED_ALWAYS:
  4837. case cmPolicies::NEW:
  4838. // Issue the fatal message.
  4839. break;
  4840. }
  4841. if (!noMessage) {
  4842. e << "Target \"" << this->GetName() << "\" links to itself.";
  4843. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  4844. messageType, e.str(), this->GetBacktrace());
  4845. if (messageType == cmake::FATAL_ERROR) {
  4846. return;
  4847. }
  4848. }
  4849. }
  4850. continue;
  4851. }
  4852. // The entry is meant for this configuration.
  4853. impl.Libraries.emplace_back(name, this->FindTargetToLink(name), *btIt,
  4854. evaluated != *le);
  4855. }
  4856. std::set<std::string> const& seenProps = cge->GetSeenTargetProperties();
  4857. for (std::string const& sp : seenProps) {
  4858. if (!this->GetProperty(sp)) {
  4859. this->LinkImplicitNullProperties.insert(sp);
  4860. }
  4861. }
  4862. cge->GetMaxLanguageStandard(this, this->MaxLanguageStandards);
  4863. }
  4864. // Get the list of configurations considered to be DEBUG.
  4865. std::vector<std::string> debugConfigs =
  4866. this->Makefile->GetCMakeInstance()->GetDebugConfigs();
  4867. cmTargetLinkLibraryType linkType =
  4868. CMP0003_ComputeLinkType(config, debugConfigs);
  4869. cmTarget::LinkLibraryVectorType const& oldllibs =
  4870. this->Target->GetOriginalLinkLibraries();
  4871. for (cmTarget::LibraryID const& oldllib : oldllibs) {
  4872. if (oldllib.second != GENERAL_LibraryType && oldllib.second != linkType) {
  4873. std::string name = this->CheckCMP0004(oldllib.first);
  4874. if (name == this->GetName() || name.empty()) {
  4875. continue;
  4876. }
  4877. // Support OLD behavior for CMP0003.
  4878. impl.WrongConfigLibraries.emplace_back(name,
  4879. this->FindTargetToLink(name));
  4880. }
  4881. }
  4882. }
  4883. cmGeneratorTarget* cmGeneratorTarget::FindTargetToLink(
  4884. std::string const& name) const
  4885. {
  4886. cmGeneratorTarget* tgt =
  4887. this->LocalGenerator->FindGeneratorTargetToUse(name);
  4888. // Skip targets that will not really be linked. This is probably a
  4889. // name conflict between an external library and an executable
  4890. // within the project.
  4891. if (tgt && tgt->GetType() == cmStateEnums::EXECUTABLE &&
  4892. !tgt->IsExecutableWithExports()) {
  4893. tgt = nullptr;
  4894. }
  4895. return tgt;
  4896. }
  4897. std::string cmGeneratorTarget::GetPDBDirectory(const std::string& config) const
  4898. {
  4899. if (OutputInfo const* info = this->GetOutputInfo(config)) {
  4900. // Return the directory in which the target will be built.
  4901. return info->PdbDir;
  4902. }
  4903. return "";
  4904. }
  4905. bool cmGeneratorTarget::HasImplibGNUtoMS(std::string const& config) const
  4906. {
  4907. return this->HasImportLibrary(config) && this->GetPropertyAsBool("GNUtoMS");
  4908. }
  4909. bool cmGeneratorTarget::GetImplibGNUtoMS(std::string const& config,
  4910. std::string const& gnuName,
  4911. std::string& out,
  4912. const char* newExt) const
  4913. {
  4914. if (this->HasImplibGNUtoMS(config) && gnuName.size() > 6 &&
  4915. gnuName.substr(gnuName.size() - 6) == ".dll.a") {
  4916. out = gnuName.substr(0, gnuName.size() - 6);
  4917. out += newExt ? newExt : ".lib";
  4918. return true;
  4919. }
  4920. return false;
  4921. }
  4922. bool cmGeneratorTarget::IsExecutableWithExports() const
  4923. {
  4924. return (this->GetType() == cmStateEnums::EXECUTABLE &&
  4925. this->GetPropertyAsBool("ENABLE_EXPORTS"));
  4926. }
  4927. bool cmGeneratorTarget::HasImportLibrary(std::string const& config) const
  4928. {
  4929. return (this->IsDLLPlatform() &&
  4930. (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4931. this->IsExecutableWithExports()) &&
  4932. // Assemblies which have only managed code do not have
  4933. // import libraries.
  4934. this->GetManagedType(config) != ManagedType::Managed);
  4935. }
  4936. std::string cmGeneratorTarget::GetSupportDirectory() const
  4937. {
  4938. std::string dir = this->LocalGenerator->GetCurrentBinaryDirectory();
  4939. dir += cmake::GetCMakeFilesDirectory();
  4940. dir += "/";
  4941. dir += this->GetName();
  4942. #if defined(__VMS)
  4943. dir += "_dir";
  4944. #else
  4945. dir += ".dir";
  4946. #endif
  4947. return dir;
  4948. }
  4949. bool cmGeneratorTarget::IsLinkable() const
  4950. {
  4951. return (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  4952. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4953. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  4954. this->GetType() == cmStateEnums::UNKNOWN_LIBRARY ||
  4955. this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  4956. this->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  4957. this->IsExecutableWithExports());
  4958. }
  4959. bool cmGeneratorTarget::IsFrameworkOnApple() const
  4960. {
  4961. return ((this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4962. this->GetType() == cmStateEnums::STATIC_LIBRARY) &&
  4963. this->Makefile->IsOn("APPLE") &&
  4964. this->GetPropertyAsBool("FRAMEWORK"));
  4965. }
  4966. bool cmGeneratorTarget::IsAppBundleOnApple() const
  4967. {
  4968. return (this->GetType() == cmStateEnums::EXECUTABLE &&
  4969. this->Makefile->IsOn("APPLE") &&
  4970. this->GetPropertyAsBool("MACOSX_BUNDLE"));
  4971. }
  4972. bool cmGeneratorTarget::IsXCTestOnApple() const
  4973. {
  4974. return (this->IsCFBundleOnApple() && this->GetPropertyAsBool("XCTEST"));
  4975. }
  4976. bool cmGeneratorTarget::IsCFBundleOnApple() const
  4977. {
  4978. return (this->GetType() == cmStateEnums::MODULE_LIBRARY &&
  4979. this->Makefile->IsOn("APPLE") && this->GetPropertyAsBool("BUNDLE"));
  4980. }
  4981. cmGeneratorTarget::ManagedType cmGeneratorTarget::CheckManagedType(
  4982. std::string const& propval) const
  4983. {
  4984. // The type of the managed assembly (mixed unmanaged C++ and C++/CLI,
  4985. // or only C++/CLI) does only depend on whether the property is an empty
  4986. // string or contains any value at all. In Visual Studio generators
  4987. // this propval is prepended with /clr[:] which results in:
  4988. //
  4989. // 1. propval does not exist: no /clr flag, unmanaged target, has import
  4990. // lib
  4991. // 2. empty propval: add /clr as flag, mixed unmanaged/managed
  4992. // target, has import lib
  4993. // 3. any value (safe,pure): add /clr:[propval] as flag, target with
  4994. // managed code only, no import lib
  4995. return propval.empty() ? ManagedType::Mixed : ManagedType::Managed;
  4996. }
  4997. cmGeneratorTarget::ManagedType cmGeneratorTarget::GetManagedType(
  4998. const std::string& config) const
  4999. {
  5000. // Only libraries and executables can be managed targets.
  5001. if (this->GetType() > cmStateEnums::SHARED_LIBRARY) {
  5002. return ManagedType::Undefined;
  5003. }
  5004. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  5005. return ManagedType::Native;
  5006. }
  5007. // Check imported target.
  5008. if (this->IsImported()) {
  5009. if (cmGeneratorTarget::ImportInfo const* info =
  5010. this->GetImportInfo(config)) {
  5011. return info->Managed;
  5012. }
  5013. return ManagedType::Undefined;
  5014. }
  5015. // Check for explicitly set clr target property.
  5016. if (auto* clr = this->GetProperty("COMMON_LANGUAGE_RUNTIME")) {
  5017. return this->CheckManagedType(clr);
  5018. }
  5019. // C# targets are always managed. This language specific check
  5020. // is added to avoid that the COMMON_LANGUAGE_RUNTIME target property
  5021. // has to be set manually for C# targets.
  5022. return this->HasLanguage("CSharp", config) ? ManagedType::Managed
  5023. : ManagedType::Native;
  5024. }