cmGeneratorTarget.cxx 217 KB

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