cmGeneratorTarget.cxx 217 KB

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