cmGeneratorTarget.cxx 248 KB

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