cmGeneratorTarget.cxx 248 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437
  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. std::set<cmSourceFile*> emitted;
  2492. std::vector<std::string> const& configs =
  2493. this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  2494. for (std::string const& c : configs) {
  2495. std::vector<cmSourceFile*> sources;
  2496. this->GeneratorTarget->GetSourceFiles(sources, c);
  2497. for (cmSourceFile* sf : sources) {
  2498. const std::set<cmGeneratorTarget const*> tgts =
  2499. this->GlobalGenerator->GetFilenameTargetDepends(sf);
  2500. if (cm::contains(tgts, this->GeneratorTarget)) {
  2501. std::ostringstream e;
  2502. e << "Evaluation output file\n \"" << sf->ResolveFullPath()
  2503. << "\"\ndepends on the sources of a target it is used in. This "
  2504. "is a dependency loop and is not allowed.";
  2505. this->GeneratorTarget->LocalGenerator->IssueMessage(
  2506. MessageType::FATAL_ERROR, e.str());
  2507. return;
  2508. }
  2509. if (emitted.insert(sf).second && this->SourcesQueued.insert(sf).second) {
  2510. this->SourceQueue.push(sf);
  2511. }
  2512. }
  2513. }
  2514. // Queue pre-build, pre-link, and post-build rule dependencies.
  2515. this->CheckCustomCommands(this->GeneratorTarget->GetPreBuildCommands());
  2516. this->CheckCustomCommands(this->GeneratorTarget->GetPreLinkCommands());
  2517. this->CheckCustomCommands(this->GeneratorTarget->GetPostBuildCommands());
  2518. }
  2519. void cmTargetTraceDependencies::Trace()
  2520. {
  2521. // Process one dependency at a time until the queue is empty.
  2522. while (!this->SourceQueue.empty()) {
  2523. // Get the next source from the queue.
  2524. cmSourceFile* sf = this->SourceQueue.front();
  2525. this->SourceQueue.pop();
  2526. this->CurrentEntry = &this->GeneratorTarget->SourceDepends[sf];
  2527. // Queue dependencies added explicitly by the user.
  2528. if (cmProp additionalDeps = sf->GetProperty("OBJECT_DEPENDS")) {
  2529. std::vector<std::string> objDeps = cmExpandedList(*additionalDeps);
  2530. for (std::string& objDep : objDeps) {
  2531. if (cmSystemTools::FileIsFullPath(objDep)) {
  2532. objDep = cmSystemTools::CollapseFullPath(objDep);
  2533. }
  2534. }
  2535. this->FollowNames(objDeps);
  2536. }
  2537. // Queue the source needed to generate this file, if any.
  2538. this->FollowName(sf->ResolveFullPath());
  2539. // Queue dependencies added programmatically by commands.
  2540. this->FollowNames(sf->GetDepends());
  2541. // Queue custom command dependencies.
  2542. if (cmCustomCommand const* cc = sf->GetCustomCommand()) {
  2543. this->CheckCustomCommand(*cc);
  2544. }
  2545. }
  2546. this->CurrentEntry = nullptr;
  2547. this->GeneratorTarget->AddTracedSources(this->NewSources);
  2548. }
  2549. void cmTargetTraceDependencies::QueueSource(cmSourceFile* sf)
  2550. {
  2551. if (this->SourcesQueued.insert(sf).second) {
  2552. this->SourceQueue.push(sf);
  2553. // Make sure this file is in the target at the end.
  2554. this->NewSources.push_back(sf->ResolveFullPath());
  2555. }
  2556. }
  2557. void cmTargetTraceDependencies::FollowName(std::string const& name)
  2558. {
  2559. // Use lower bound with key comparison to not repeat the search for the
  2560. // insert position if the name could not be found (which is the common case).
  2561. auto i = this->NameMap.lower_bound(name);
  2562. if (i == this->NameMap.end() || i->first != name) {
  2563. // Check if we know how to generate this file.
  2564. cmSourcesWithOutput sources = this->Makefile->GetSourcesWithOutput(name);
  2565. // If we failed to find a target or source and we have a relative path, it
  2566. // might be a valid source if made relative to the current binary
  2567. // directory.
  2568. if (!sources.Target && !sources.Source &&
  2569. !cmSystemTools::FileIsFullPath(name)) {
  2570. auto fullname =
  2571. cmStrCat(this->Makefile->GetCurrentBinaryDirectory(), '/', name);
  2572. fullname = cmSystemTools::CollapseFullPath(
  2573. fullname, this->Makefile->GetHomeOutputDirectory());
  2574. sources = this->Makefile->GetSourcesWithOutput(fullname);
  2575. }
  2576. i = this->NameMap.emplace_hint(i, name, sources);
  2577. }
  2578. if (cmTarget* t = i->second.Target) {
  2579. // The name is a byproduct of a utility target or a PRE_BUILD, PRE_LINK, or
  2580. // POST_BUILD command.
  2581. this->GeneratorTarget->Target->AddUtility(t->GetName(), false);
  2582. }
  2583. if (cmSourceFile* sf = i->second.Source) {
  2584. // For now only follow the dependency if the source file is not a
  2585. // byproduct. Semantics of byproducts in a non-Ninja context will have to
  2586. // be defined first.
  2587. if (!i->second.SourceIsByproduct) {
  2588. // Record the dependency we just followed.
  2589. if (this->CurrentEntry) {
  2590. this->CurrentEntry->Depends.push_back(sf);
  2591. }
  2592. this->QueueSource(sf);
  2593. }
  2594. }
  2595. }
  2596. void cmTargetTraceDependencies::FollowNames(
  2597. std::vector<std::string> const& names)
  2598. {
  2599. for (std::string const& name : names) {
  2600. this->FollowName(name);
  2601. }
  2602. }
  2603. bool cmTargetTraceDependencies::IsUtility(std::string const& dep)
  2604. {
  2605. // Dependencies on targets (utilities) are supposed to be named by
  2606. // just the target name. However for compatibility we support
  2607. // naming the output file generated by the target (assuming there is
  2608. // no output-name property which old code would not have set). In
  2609. // that case the target name will be the file basename of the
  2610. // dependency.
  2611. std::string util = cmSystemTools::GetFilenameName(dep);
  2612. if (cmSystemTools::GetFilenameLastExtension(util) == ".exe") {
  2613. util = cmSystemTools::GetFilenameWithoutLastExtension(util);
  2614. }
  2615. // Check for a target with this name.
  2616. if (cmGeneratorTarget* t =
  2617. this->GeneratorTarget->GetLocalGenerator()->FindGeneratorTargetToUse(
  2618. util)) {
  2619. // If we find the target and the dep was given as a full path,
  2620. // then make sure it was not a full path to something else, and
  2621. // the fact that the name matched a target was just a coincidence.
  2622. if (cmSystemTools::FileIsFullPath(dep)) {
  2623. if (t->GetType() >= cmStateEnums::EXECUTABLE &&
  2624. t->GetType() <= cmStateEnums::MODULE_LIBRARY) {
  2625. // This is really only for compatibility so we do not need to
  2626. // worry about configuration names and output names.
  2627. std::string tLocation = t->GetLocationForBuild();
  2628. tLocation = cmSystemTools::GetFilenamePath(tLocation);
  2629. std::string depLocation = cmSystemTools::GetFilenamePath(dep);
  2630. depLocation = cmSystemTools::CollapseFullPath(depLocation);
  2631. tLocation = cmSystemTools::CollapseFullPath(tLocation);
  2632. if (depLocation == tLocation) {
  2633. this->GeneratorTarget->Target->AddUtility(util, false);
  2634. return true;
  2635. }
  2636. }
  2637. } else {
  2638. // The original name of the dependency was not a full path. It
  2639. // must name a target, so add the target-level dependency.
  2640. this->GeneratorTarget->Target->AddUtility(util, false);
  2641. return true;
  2642. }
  2643. }
  2644. // The dependency does not name a target built in this project.
  2645. return false;
  2646. }
  2647. void cmTargetTraceDependencies::CheckCustomCommand(cmCustomCommand const& cc)
  2648. {
  2649. // Transform command names that reference targets built in this
  2650. // project to corresponding target-level dependencies.
  2651. cmGeneratorExpression ge(cc.GetBacktrace());
  2652. // Add target-level dependencies referenced by generator expressions.
  2653. std::set<cmGeneratorTarget*> targets;
  2654. for (cmCustomCommandLine const& cCmdLine : cc.GetCommandLines()) {
  2655. std::string const& command = cCmdLine.front();
  2656. // Check for a target with this name.
  2657. if (cmGeneratorTarget* t =
  2658. this->LocalGenerator->FindGeneratorTargetToUse(command)) {
  2659. if (t->GetType() == cmStateEnums::EXECUTABLE) {
  2660. // The command refers to an executable target built in
  2661. // this project. Add the target-level dependency to make
  2662. // sure the executable is up to date before this custom
  2663. // command possibly runs.
  2664. this->GeneratorTarget->Target->AddUtility(command, true);
  2665. }
  2666. }
  2667. // Check for target references in generator expressions.
  2668. for (std::string const& cl : cCmdLine) {
  2669. const std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(cl);
  2670. cge->SetQuiet(true);
  2671. cge->Evaluate(this->GeneratorTarget->GetLocalGenerator(), "");
  2672. std::set<cmGeneratorTarget*> geTargets = cge->GetTargets();
  2673. targets.insert(geTargets.begin(), geTargets.end());
  2674. }
  2675. }
  2676. for (cmGeneratorTarget* target : targets) {
  2677. this->GeneratorTarget->Target->AddUtility(target->GetName(), true);
  2678. }
  2679. // Queue the custom command dependencies.
  2680. std::set<std::string> emitted;
  2681. std::vector<std::string> const& configs =
  2682. this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  2683. for (std::string const& conf : configs) {
  2684. this->FollowCommandDepends(cc, conf, emitted);
  2685. }
  2686. }
  2687. void cmTargetTraceDependencies::FollowCommandDepends(
  2688. cmCustomCommand const& cc, const std::string& config,
  2689. std::set<std::string>& emitted)
  2690. {
  2691. cmCustomCommandGenerator ccg(cc, config,
  2692. this->GeneratorTarget->LocalGenerator);
  2693. const std::vector<std::string>& depends = ccg.GetDepends();
  2694. for (std::string const& dep : depends) {
  2695. if (emitted.insert(dep).second) {
  2696. if (!this->IsUtility(dep)) {
  2697. // The dependency does not name a target and may be a file we
  2698. // know how to generate. Queue it.
  2699. this->FollowName(dep);
  2700. }
  2701. }
  2702. }
  2703. }
  2704. void cmTargetTraceDependencies::CheckCustomCommands(
  2705. const std::vector<cmCustomCommand>& commands)
  2706. {
  2707. for (cmCustomCommand const& command : commands) {
  2708. this->CheckCustomCommand(command);
  2709. }
  2710. }
  2711. void cmGeneratorTarget::TraceDependencies()
  2712. {
  2713. // CMake-generated targets have no dependencies to trace. Normally tracing
  2714. // would find nothing anyway, but when building CMake itself the "install"
  2715. // target command ends up referencing the "cmake" target but we do not
  2716. // really want the dependency because "install" depend on "all" anyway.
  2717. if (this->GetType() == cmStateEnums::GLOBAL_TARGET) {
  2718. return;
  2719. }
  2720. // Use a helper object to trace the dependencies.
  2721. cmTargetTraceDependencies tracer(this);
  2722. tracer.Trace();
  2723. }
  2724. std::string cmGeneratorTarget::GetCompilePDBDirectory(
  2725. const std::string& config) const
  2726. {
  2727. if (CompileInfo const* info = this->GetCompileInfo(config)) {
  2728. return info->CompilePdbDir;
  2729. }
  2730. return "";
  2731. }
  2732. void cmGeneratorTarget::GetAppleArchs(const std::string& config,
  2733. std::vector<std::string>& archVec) const
  2734. {
  2735. cmProp archs = nullptr;
  2736. if (!config.empty()) {
  2737. std::string defVarName =
  2738. cmStrCat("OSX_ARCHITECTURES_", cmSystemTools::UpperCase(config));
  2739. archs = this->GetProperty(defVarName);
  2740. }
  2741. if (!archs) {
  2742. archs = this->GetProperty("OSX_ARCHITECTURES");
  2743. }
  2744. if (archs) {
  2745. cmExpandList(*archs, archVec);
  2746. }
  2747. }
  2748. void cmGeneratorTarget::AddCUDAArchitectureFlags(std::string& flags) const
  2749. {
  2750. const std::string& property = this->GetSafeProperty("CUDA_ARCHITECTURES");
  2751. if (property.empty()) {
  2752. switch (this->GetPolicyStatusCMP0104()) {
  2753. case cmPolicies::WARN:
  2754. if (!this->LocalGenerator->GetCMakeInstance()->GetIsInTryCompile()) {
  2755. this->Makefile->IssueMessage(
  2756. MessageType::AUTHOR_WARNING,
  2757. cmPolicies::GetPolicyWarning(cmPolicies::CMP0104) +
  2758. "\nCUDA_ARCHITECTURES is empty for target \"" + this->GetName() +
  2759. "\".");
  2760. }
  2761. CM_FALLTHROUGH;
  2762. case cmPolicies::OLD:
  2763. break;
  2764. default:
  2765. this->Makefile->IssueMessage(
  2766. MessageType::FATAL_ERROR,
  2767. "CUDA_ARCHITECTURES is empty for target \"" + this->GetName() +
  2768. "\".");
  2769. }
  2770. }
  2771. // If CUDA_ARCHITECTURES is false we don't add any architectures.
  2772. if (cmIsOff(property)) {
  2773. return;
  2774. }
  2775. struct CudaArchitecture
  2776. {
  2777. std::string name;
  2778. bool real{ true };
  2779. bool virtual_{ true };
  2780. };
  2781. std::vector<CudaArchitecture> architectures;
  2782. {
  2783. std::vector<std::string> options;
  2784. cmExpandList(property, options);
  2785. for (std::string& option : options) {
  2786. CudaArchitecture architecture;
  2787. // Architecture name is up to the first specifier.
  2788. std::size_t pos = option.find_first_of('-');
  2789. architecture.name = option.substr(0, pos);
  2790. if (pos != std::string::npos) {
  2791. cm::string_view specifier{ option.c_str() + pos + 1,
  2792. option.length() - pos - 1 };
  2793. if (specifier == "real") {
  2794. architecture.real = true;
  2795. architecture.virtual_ = false;
  2796. } else if (specifier == "virtual") {
  2797. architecture.real = false;
  2798. architecture.virtual_ = true;
  2799. } else {
  2800. this->Makefile->IssueMessage(
  2801. MessageType::FATAL_ERROR,
  2802. "Unknown CUDA architecture specifier \"" + std::string(specifier) +
  2803. "\".");
  2804. }
  2805. }
  2806. architectures.emplace_back(architecture);
  2807. }
  2808. }
  2809. std::string const& compiler =
  2810. this->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_ID");
  2811. if (compiler == "NVIDIA") {
  2812. for (CudaArchitecture& architecture : architectures) {
  2813. flags +=
  2814. " --generate-code=arch=compute_" + architecture.name + ",code=[";
  2815. if (architecture.virtual_) {
  2816. flags += "compute_" + architecture.name;
  2817. if (architecture.real) {
  2818. flags += ",";
  2819. }
  2820. }
  2821. if (architecture.real) {
  2822. flags += "sm_" + architecture.name;
  2823. }
  2824. flags += "]";
  2825. }
  2826. } else if (compiler == "Clang") {
  2827. for (CudaArchitecture& architecture : architectures) {
  2828. flags += " --cuda-gpu-arch=sm_" + architecture.name;
  2829. if (!architecture.real) {
  2830. Makefile->IssueMessage(
  2831. MessageType::WARNING,
  2832. "Clang doesn't support disabling CUDA real code generation.");
  2833. }
  2834. if (!architecture.virtual_) {
  2835. flags += " --no-cuda-include-ptx=sm_" + architecture.name;
  2836. }
  2837. }
  2838. }
  2839. }
  2840. void cmGeneratorTarget::AddCUDAToolkitFlags(std::string& flags) const
  2841. {
  2842. std::string const& compiler =
  2843. this->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_ID");
  2844. if (compiler == "Clang") {
  2845. // Pass CUDA toolkit explicitly to Clang.
  2846. // Clang's searching for the system CUDA toolkit isn't very good and it's
  2847. // expected the user will explicitly pass the toolkit path.
  2848. // This also avoids Clang having to search for the toolkit on every
  2849. // invocation.
  2850. std::string toolkitRoot =
  2851. this->Makefile->GetSafeDefinition("CMAKE_CUDA_COMPILER_LIBRARY_ROOT");
  2852. if (!toolkitRoot.empty()) {
  2853. flags += " --cuda-path=" +
  2854. this->LocalGenerator->ConvertToOutputFormat(toolkitRoot,
  2855. cmOutputConverter::SHELL);
  2856. }
  2857. }
  2858. }
  2859. //----------------------------------------------------------------------------
  2860. std::string cmGeneratorTarget::GetFeatureSpecificLinkRuleVariable(
  2861. std::string const& var, std::string const& lang,
  2862. std::string const& config) const
  2863. {
  2864. if (this->IsIPOEnabled(lang, config)) {
  2865. std::string varIPO = var + "_IPO";
  2866. if (this->Makefile->IsDefinitionSet(varIPO)) {
  2867. return varIPO;
  2868. }
  2869. }
  2870. return var;
  2871. }
  2872. //----------------------------------------------------------------------------
  2873. std::string cmGeneratorTarget::GetCreateRuleVariable(
  2874. std::string const& lang, std::string const& config) const
  2875. {
  2876. switch (this->GetType()) {
  2877. case cmStateEnums::STATIC_LIBRARY: {
  2878. std::string var = "CMAKE_" + lang + "_CREATE_STATIC_LIBRARY";
  2879. return this->GetFeatureSpecificLinkRuleVariable(var, lang, config);
  2880. }
  2881. case cmStateEnums::SHARED_LIBRARY:
  2882. return "CMAKE_" + lang + "_CREATE_SHARED_LIBRARY";
  2883. case cmStateEnums::MODULE_LIBRARY:
  2884. return "CMAKE_" + lang + "_CREATE_SHARED_MODULE";
  2885. case cmStateEnums::EXECUTABLE:
  2886. if (this->IsExecutableWithExports()) {
  2887. std::string linkExeWithExports =
  2888. "CMAKE_" + lang + "_LINK_EXECUTABLE_WITH_EXPORTS";
  2889. if (this->Makefile->IsDefinitionSet(linkExeWithExports)) {
  2890. return linkExeWithExports;
  2891. }
  2892. }
  2893. return "CMAKE_" + lang + "_LINK_EXECUTABLE";
  2894. default:
  2895. break;
  2896. }
  2897. return "";
  2898. }
  2899. namespace {
  2900. void processIncludeDirectories(cmGeneratorTarget const* tgt,
  2901. EvaluatedTargetPropertyEntries& entries,
  2902. std::vector<BT<std::string>>& includes,
  2903. std::unordered_set<std::string>& uniqueIncludes,
  2904. bool debugIncludes)
  2905. {
  2906. for (EvaluatedTargetPropertyEntry& entry : entries.Entries) {
  2907. cmLinkImplItem const& item = entry.LinkImplItem;
  2908. std::string const& targetName = item.AsStr();
  2909. bool const fromImported = item.Target && item.Target->IsImported();
  2910. bool const checkCMP0027 = item.FromGenex;
  2911. std::string usedIncludes;
  2912. for (std::string& entryInclude : entry.Values) {
  2913. if (fromImported && !cmSystemTools::FileExists(entryInclude)) {
  2914. std::ostringstream e;
  2915. MessageType messageType = MessageType::FATAL_ERROR;
  2916. if (checkCMP0027) {
  2917. switch (tgt->GetPolicyStatusCMP0027()) {
  2918. case cmPolicies::WARN:
  2919. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0027) << "\n";
  2920. CM_FALLTHROUGH;
  2921. case cmPolicies::OLD:
  2922. messageType = MessageType::AUTHOR_WARNING;
  2923. break;
  2924. case cmPolicies::REQUIRED_ALWAYS:
  2925. case cmPolicies::REQUIRED_IF_USED:
  2926. case cmPolicies::NEW:
  2927. break;
  2928. }
  2929. }
  2930. /* clang-format off */
  2931. e << "Imported target \"" << targetName << "\" includes "
  2932. "non-existent path\n \"" << entryInclude << "\"\nin its "
  2933. "INTERFACE_INCLUDE_DIRECTORIES. Possible reasons include:\n"
  2934. "* The path was deleted, renamed, or moved to another "
  2935. "location.\n"
  2936. "* An install or uninstall procedure did not complete "
  2937. "successfully.\n"
  2938. "* The installation package was faulty and references files it "
  2939. "does not provide.\n";
  2940. /* clang-format on */
  2941. tgt->GetLocalGenerator()->IssueMessage(messageType, e.str());
  2942. return;
  2943. }
  2944. if (!cmSystemTools::FileIsFullPath(entryInclude)) {
  2945. std::ostringstream e;
  2946. bool noMessage = false;
  2947. MessageType messageType = MessageType::FATAL_ERROR;
  2948. if (!targetName.empty()) {
  2949. /* clang-format off */
  2950. e << "Target \"" << targetName << "\" contains relative "
  2951. "path in its INTERFACE_INCLUDE_DIRECTORIES:\n"
  2952. " \"" << entryInclude << "\"";
  2953. /* clang-format on */
  2954. } else {
  2955. switch (tgt->GetPolicyStatusCMP0021()) {
  2956. case cmPolicies::WARN: {
  2957. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0021) << "\n";
  2958. messageType = MessageType::AUTHOR_WARNING;
  2959. } break;
  2960. case cmPolicies::OLD:
  2961. noMessage = true;
  2962. case cmPolicies::REQUIRED_IF_USED:
  2963. case cmPolicies::REQUIRED_ALWAYS:
  2964. case cmPolicies::NEW:
  2965. // Issue the fatal message.
  2966. break;
  2967. }
  2968. e << "Found relative path while evaluating include directories of "
  2969. "\""
  2970. << tgt->GetName() << "\":\n \"" << entryInclude << "\"\n";
  2971. }
  2972. if (!noMessage) {
  2973. tgt->GetLocalGenerator()->IssueMessage(messageType, e.str());
  2974. if (messageType == MessageType::FATAL_ERROR) {
  2975. return;
  2976. }
  2977. }
  2978. }
  2979. if (!cmIsOff(entryInclude)) {
  2980. cmSystemTools::ConvertToUnixSlashes(entryInclude);
  2981. }
  2982. if (uniqueIncludes.insert(entryInclude).second) {
  2983. includes.emplace_back(entryInclude, entry.Backtrace);
  2984. if (debugIncludes) {
  2985. usedIncludes += " * " + entryInclude + "\n";
  2986. }
  2987. }
  2988. }
  2989. if (!usedIncludes.empty()) {
  2990. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  2991. MessageType::LOG,
  2992. std::string("Used includes for target ") + tgt->GetName() + ":\n" +
  2993. usedIncludes,
  2994. entry.Backtrace);
  2995. }
  2996. }
  2997. }
  2998. }
  2999. std::vector<BT<std::string>> cmGeneratorTarget::GetIncludeDirectories(
  3000. const std::string& config, const std::string& lang) const
  3001. {
  3002. std::vector<BT<std::string>> includes;
  3003. std::unordered_set<std::string> uniqueIncludes;
  3004. cmGeneratorExpressionDAGChecker dagChecker(this, "INCLUDE_DIRECTORIES",
  3005. nullptr, nullptr);
  3006. std::vector<std::string> debugProperties;
  3007. this->Makefile->GetDefExpandList("CMAKE_DEBUG_TARGET_PROPERTIES",
  3008. debugProperties);
  3009. bool debugIncludes = !this->DebugIncludesDone &&
  3010. cm::contains(debugProperties, "INCLUDE_DIRECTORIES");
  3011. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3012. this->DebugIncludesDone = true;
  3013. }
  3014. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  3015. this, config, lang, &dagChecker, this->IncludeDirectoriesEntries);
  3016. if (lang == "Swift") {
  3017. AddSwiftImplicitIncludeDirectories(this, config, entries);
  3018. }
  3019. AddInterfaceEntries(this, config, "INTERFACE_INCLUDE_DIRECTORIES", lang,
  3020. &dagChecker, entries);
  3021. if (this->Makefile->IsOn("APPLE")) {
  3022. cmLinkImplementationLibraries const* impl =
  3023. this->GetLinkImplementationLibraries(config);
  3024. for (cmLinkImplItem const& lib : impl->Libraries) {
  3025. std::string libDir = cmSystemTools::CollapseFullPath(
  3026. lib.AsStr(), this->Makefile->GetHomeOutputDirectory());
  3027. static cmsys::RegularExpression frameworkCheck(
  3028. "(.*\\.framework)(/Versions/[^/]+)?/[^/]+$");
  3029. if (!frameworkCheck.find(libDir)) {
  3030. continue;
  3031. }
  3032. libDir = frameworkCheck.match(1);
  3033. EvaluatedTargetPropertyEntry ee(lib, cmListFileBacktrace());
  3034. ee.Values.emplace_back(std::move(libDir));
  3035. entries.Entries.emplace_back(std::move(ee));
  3036. }
  3037. }
  3038. processIncludeDirectories(this, entries, includes, uniqueIncludes,
  3039. debugIncludes);
  3040. return includes;
  3041. }
  3042. enum class OptionsParse
  3043. {
  3044. None,
  3045. Shell
  3046. };
  3047. namespace {
  3048. const auto DL_BEGIN = "<DEVICE_LINK>"_s;
  3049. const auto DL_END = "</DEVICE_LINK>"_s;
  3050. void processOptions(cmGeneratorTarget const* tgt,
  3051. EvaluatedTargetPropertyEntries const& entries,
  3052. std::vector<BT<std::string>>& options,
  3053. std::unordered_set<std::string>& uniqueOptions,
  3054. bool debugOptions, const char* logName, OptionsParse parse,
  3055. bool processDeviceOptions = false)
  3056. {
  3057. bool splitOption = !processDeviceOptions;
  3058. for (EvaluatedTargetPropertyEntry const& entry : entries.Entries) {
  3059. std::string usedOptions;
  3060. for (std::string const& opt : entry.Values) {
  3061. if (processDeviceOptions && (opt == DL_BEGIN || opt == DL_END)) {
  3062. options.emplace_back(opt, entry.Backtrace);
  3063. splitOption = opt == DL_BEGIN;
  3064. continue;
  3065. }
  3066. if (uniqueOptions.insert(opt).second) {
  3067. if (parse == OptionsParse::Shell &&
  3068. cmHasLiteralPrefix(opt, "SHELL:")) {
  3069. if (splitOption) {
  3070. std::vector<std::string> tmp;
  3071. cmSystemTools::ParseUnixCommandLine(opt.c_str() + 6, tmp);
  3072. for (std::string& o : tmp) {
  3073. options.emplace_back(std::move(o), entry.Backtrace);
  3074. }
  3075. } else {
  3076. options.emplace_back(std::string(opt.c_str() + 6),
  3077. entry.Backtrace);
  3078. }
  3079. } else {
  3080. options.emplace_back(opt, entry.Backtrace);
  3081. }
  3082. if (debugOptions) {
  3083. usedOptions += " * " + opt + "\n";
  3084. }
  3085. }
  3086. }
  3087. if (!usedOptions.empty()) {
  3088. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  3089. MessageType::LOG,
  3090. std::string("Used ") + logName + std::string(" for target ") +
  3091. tgt->GetName() + ":\n" + usedOptions,
  3092. entry.Backtrace);
  3093. }
  3094. }
  3095. }
  3096. std::vector<BT<std::string>> wrapOptions(
  3097. std::vector<std::string>& options, const cmListFileBacktrace& bt,
  3098. const std::vector<std::string>& wrapperFlag, const std::string& wrapperSep,
  3099. bool concatFlagAndArgs)
  3100. {
  3101. std::vector<BT<std::string>> result;
  3102. if (options.empty()) {
  3103. return result;
  3104. }
  3105. if (wrapperFlag.empty() || cmHasLiteralPrefix(options.front(), "LINKER:")) {
  3106. // nothing specified or LINKER wrapper, insert elements as is
  3107. result.reserve(options.size());
  3108. for (std::string& o : options) {
  3109. result.emplace_back(std::move(o), bt);
  3110. }
  3111. } else {
  3112. if (!wrapperSep.empty()) {
  3113. if (concatFlagAndArgs) {
  3114. // insert flag elements except last one
  3115. for (auto i = wrapperFlag.begin(); i != wrapperFlag.end() - 1; ++i) {
  3116. result.emplace_back(*i, bt);
  3117. }
  3118. // concatenate last flag element and all list values
  3119. // in one option
  3120. result.emplace_back(wrapperFlag.back() + cmJoin(options, wrapperSep),
  3121. bt);
  3122. } else {
  3123. for (std::string const& i : wrapperFlag) {
  3124. result.emplace_back(i, bt);
  3125. }
  3126. // concatenate all list values in one option
  3127. result.emplace_back(cmJoin(options, wrapperSep), bt);
  3128. }
  3129. } else {
  3130. // prefix each element of list with wrapper
  3131. if (concatFlagAndArgs) {
  3132. std::transform(options.begin(), options.end(), options.begin(),
  3133. [&wrapperFlag](std::string const& o) -> std::string {
  3134. return wrapperFlag.back() + o;
  3135. });
  3136. }
  3137. for (std::string& o : options) {
  3138. for (auto i = wrapperFlag.begin(),
  3139. e = concatFlagAndArgs ? wrapperFlag.end() - 1
  3140. : wrapperFlag.end();
  3141. i != e; ++i) {
  3142. result.emplace_back(*i, bt);
  3143. }
  3144. result.emplace_back(std::move(o), bt);
  3145. }
  3146. }
  3147. }
  3148. return result;
  3149. }
  3150. }
  3151. void cmGeneratorTarget::GetCompileOptions(std::vector<std::string>& result,
  3152. const std::string& config,
  3153. const std::string& language) const
  3154. {
  3155. std::vector<BT<std::string>> tmp = this->GetCompileOptions(config, language);
  3156. result.reserve(tmp.size());
  3157. for (BT<std::string>& v : tmp) {
  3158. result.emplace_back(std::move(v.Value));
  3159. }
  3160. }
  3161. std::vector<BT<std::string>> cmGeneratorTarget::GetCompileOptions(
  3162. std::string const& config, std::string const& language) const
  3163. {
  3164. std::vector<BT<std::string>> result;
  3165. std::unordered_set<std::string> uniqueOptions;
  3166. cmGeneratorExpressionDAGChecker dagChecker(this, "COMPILE_OPTIONS", nullptr,
  3167. nullptr);
  3168. std::vector<std::string> debugProperties;
  3169. this->Makefile->GetDefExpandList("CMAKE_DEBUG_TARGET_PROPERTIES",
  3170. debugProperties);
  3171. bool debugOptions = !this->DebugCompileOptionsDone &&
  3172. cm::contains(debugProperties, "COMPILE_OPTIONS");
  3173. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3174. this->DebugCompileOptionsDone = true;
  3175. }
  3176. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  3177. this, config, language, &dagChecker, this->CompileOptionsEntries);
  3178. AddInterfaceEntries(this, config, "INTERFACE_COMPILE_OPTIONS", language,
  3179. &dagChecker, entries);
  3180. processOptions(this, entries, result, uniqueOptions, debugOptions,
  3181. "compile options", OptionsParse::Shell);
  3182. return result;
  3183. }
  3184. void cmGeneratorTarget::GetCompileFeatures(std::vector<std::string>& result,
  3185. const std::string& config) const
  3186. {
  3187. std::vector<BT<std::string>> tmp = this->GetCompileFeatures(config);
  3188. result.reserve(tmp.size());
  3189. for (BT<std::string>& v : tmp) {
  3190. result.emplace_back(std::move(v.Value));
  3191. }
  3192. }
  3193. std::vector<BT<std::string>> cmGeneratorTarget::GetCompileFeatures(
  3194. std::string const& config) const
  3195. {
  3196. std::vector<BT<std::string>> result;
  3197. std::unordered_set<std::string> uniqueFeatures;
  3198. cmGeneratorExpressionDAGChecker dagChecker(this, "COMPILE_FEATURES", nullptr,
  3199. nullptr);
  3200. std::vector<std::string> debugProperties;
  3201. this->Makefile->GetDefExpandList("CMAKE_DEBUG_TARGET_PROPERTIES",
  3202. debugProperties);
  3203. bool debugFeatures = !this->DebugCompileFeaturesDone &&
  3204. cm::contains(debugProperties, "COMPILE_FEATURES");
  3205. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3206. this->DebugCompileFeaturesDone = true;
  3207. }
  3208. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  3209. this, config, std::string(), &dagChecker, this->CompileFeaturesEntries);
  3210. AddInterfaceEntries(this, config, "INTERFACE_COMPILE_FEATURES",
  3211. std::string(), &dagChecker, entries);
  3212. processOptions(this, entries, result, uniqueFeatures, debugFeatures,
  3213. "compile features", OptionsParse::None);
  3214. return result;
  3215. }
  3216. void cmGeneratorTarget::GetCompileDefinitions(
  3217. std::vector<std::string>& result, const std::string& config,
  3218. const std::string& language) const
  3219. {
  3220. std::vector<BT<std::string>> tmp =
  3221. this->GetCompileDefinitions(config, language);
  3222. result.reserve(tmp.size());
  3223. for (BT<std::string>& v : tmp) {
  3224. result.emplace_back(std::move(v.Value));
  3225. }
  3226. }
  3227. std::vector<BT<std::string>> cmGeneratorTarget::GetCompileDefinitions(
  3228. std::string const& config, std::string const& language) const
  3229. {
  3230. std::vector<BT<std::string>> list;
  3231. std::unordered_set<std::string> uniqueOptions;
  3232. cmGeneratorExpressionDAGChecker dagChecker(this, "COMPILE_DEFINITIONS",
  3233. nullptr, nullptr);
  3234. std::vector<std::string> debugProperties;
  3235. this->Makefile->GetDefExpandList("CMAKE_DEBUG_TARGET_PROPERTIES",
  3236. debugProperties);
  3237. bool debugDefines = !this->DebugCompileDefinitionsDone &&
  3238. cm::contains(debugProperties, "COMPILE_DEFINITIONS");
  3239. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3240. this->DebugCompileDefinitionsDone = true;
  3241. }
  3242. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  3243. this, config, language, &dagChecker, this->CompileDefinitionsEntries);
  3244. AddInterfaceEntries(this, config, "INTERFACE_COMPILE_DEFINITIONS", language,
  3245. &dagChecker, entries);
  3246. if (!config.empty()) {
  3247. std::string configPropName =
  3248. "COMPILE_DEFINITIONS_" + cmSystemTools::UpperCase(config);
  3249. cmProp configProp = this->GetProperty(configPropName);
  3250. if (configProp) {
  3251. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0043)) {
  3252. case cmPolicies::WARN: {
  3253. this->LocalGenerator->IssueMessage(
  3254. MessageType::AUTHOR_WARNING,
  3255. cmPolicies::GetPolicyWarning(cmPolicies::CMP0043));
  3256. CM_FALLTHROUGH;
  3257. }
  3258. case cmPolicies::OLD: {
  3259. std::unique_ptr<TargetPropertyEntry> entry =
  3260. CreateTargetPropertyEntry(*configProp);
  3261. entries.Entries.emplace_back(EvaluateTargetPropertyEntry(
  3262. this, config, language, &dagChecker, *entry));
  3263. } break;
  3264. case cmPolicies::NEW:
  3265. case cmPolicies::REQUIRED_ALWAYS:
  3266. case cmPolicies::REQUIRED_IF_USED:
  3267. break;
  3268. }
  3269. }
  3270. }
  3271. processOptions(this, entries, list, uniqueOptions, debugDefines,
  3272. "compile definitions", OptionsParse::None);
  3273. return list;
  3274. }
  3275. std::vector<BT<std::string>> cmGeneratorTarget::GetPrecompileHeaders(
  3276. const std::string& config, const std::string& language) const
  3277. {
  3278. std::unordered_set<std::string> uniqueOptions;
  3279. cmGeneratorExpressionDAGChecker dagChecker(this, "PRECOMPILE_HEADERS",
  3280. nullptr, nullptr);
  3281. std::vector<std::string> debugProperties;
  3282. this->Makefile->GetDefExpandList("CMAKE_DEBUG_TARGET_PROPERTIES",
  3283. debugProperties);
  3284. bool debugDefines = !this->DebugPrecompileHeadersDone &&
  3285. std::find(debugProperties.begin(), debugProperties.end(),
  3286. "PRECOMPILE_HEADERS") != debugProperties.end();
  3287. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3288. this->DebugPrecompileHeadersDone = true;
  3289. }
  3290. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  3291. this, config, language, &dagChecker, this->PrecompileHeadersEntries);
  3292. AddInterfaceEntries(this, config, "INTERFACE_PRECOMPILE_HEADERS", language,
  3293. &dagChecker, entries);
  3294. std::vector<BT<std::string>> list;
  3295. processOptions(this, entries, list, uniqueOptions, debugDefines,
  3296. "precompile headers", OptionsParse::None);
  3297. return list;
  3298. }
  3299. std::string cmGeneratorTarget::GetPchHeader(const std::string& config,
  3300. const std::string& language,
  3301. const std::string& arch) const
  3302. {
  3303. if (language != "C" && language != "CXX" && language != "OBJC" &&
  3304. language != "OBJCXX") {
  3305. return std::string();
  3306. }
  3307. if (this->GetPropertyAsBool("DISABLE_PRECOMPILE_HEADERS")) {
  3308. return std::string();
  3309. }
  3310. const cmGeneratorTarget* generatorTarget = this;
  3311. cmProp pchReuseFrom =
  3312. generatorTarget->GetProperty("PRECOMPILE_HEADERS_REUSE_FROM");
  3313. const auto inserted =
  3314. this->PchHeaders.insert(std::make_pair(language + config + arch, ""));
  3315. if (inserted.second) {
  3316. const std::vector<BT<std::string>> headers =
  3317. this->GetPrecompileHeaders(config, language);
  3318. if (headers.empty() && !pchReuseFrom) {
  3319. return std::string();
  3320. }
  3321. std::string& filename = inserted.first->second;
  3322. if (pchReuseFrom) {
  3323. generatorTarget =
  3324. this->GetGlobalGenerator()->FindGeneratorTarget(*pchReuseFrom);
  3325. }
  3326. filename = cmStrCat(
  3327. generatorTarget->LocalGenerator->GetCurrentBinaryDirectory(), "/");
  3328. const std::map<std::string, std::string> languageToExtension = {
  3329. { "C", ".h" },
  3330. { "CXX", ".hxx" },
  3331. { "OBJC", ".objc.h" },
  3332. { "OBJCXX", ".objcxx.hxx" }
  3333. };
  3334. filename =
  3335. cmStrCat(filename, "CMakeFiles/", generatorTarget->GetName(), ".dir");
  3336. if (this->GetGlobalGenerator()->IsMultiConfig()) {
  3337. filename = cmStrCat(filename, "/", config);
  3338. }
  3339. filename =
  3340. cmStrCat(filename, "/cmake_pch", arch.empty() ? "" : cmStrCat("_", arch),
  3341. languageToExtension.at(language));
  3342. const std::string filename_tmp = cmStrCat(filename, ".tmp");
  3343. if (!pchReuseFrom) {
  3344. auto pchPrologue = this->Makefile->GetDefinition("CMAKE_PCH_PROLOGUE");
  3345. auto pchEpilogue = this->Makefile->GetDefinition("CMAKE_PCH_EPILOGUE");
  3346. std::string firstHeaderOnDisk;
  3347. {
  3348. cmGeneratedFileStream file(
  3349. filename_tmp, false,
  3350. this->GetGlobalGenerator()->GetMakefileEncoding());
  3351. file << "/* generated by CMake */\n\n";
  3352. if (pchPrologue) {
  3353. file << pchPrologue << "\n";
  3354. }
  3355. if (this->GetGlobalGenerator()->IsXcode()) {
  3356. file << "#ifndef CMAKE_SKIP_PRECOMPILE_HEADERS\n";
  3357. }
  3358. if (language == "CXX") {
  3359. file << "#ifdef __cplusplus\n";
  3360. }
  3361. for (auto const& header_bt : headers) {
  3362. if (header_bt.Value.empty()) {
  3363. continue;
  3364. }
  3365. if (header_bt.Value[0] == '<' || header_bt.Value[0] == '\"') {
  3366. file << "#include " << header_bt.Value << "\n";
  3367. } else {
  3368. file << "#include \"" << header_bt.Value << "\"\n";
  3369. }
  3370. if (cmSystemTools::FileExists(header_bt.Value) &&
  3371. firstHeaderOnDisk.empty()) {
  3372. firstHeaderOnDisk = header_bt.Value;
  3373. }
  3374. }
  3375. if (language == "CXX") {
  3376. file << "#endif // __cplusplus\n";
  3377. }
  3378. if (this->GetGlobalGenerator()->IsXcode()) {
  3379. file << "#endif // CMAKE_SKIP_PRECOMPILE_HEADERS\n";
  3380. }
  3381. if (pchEpilogue) {
  3382. file << pchEpilogue << "\n";
  3383. }
  3384. }
  3385. if (!firstHeaderOnDisk.empty()) {
  3386. cmFileTimes::Copy(firstHeaderOnDisk, filename_tmp);
  3387. }
  3388. cmSystemTools::MoveFileIfDifferent(filename_tmp, filename);
  3389. }
  3390. }
  3391. return inserted.first->second;
  3392. }
  3393. std::string cmGeneratorTarget::GetPchSource(const std::string& config,
  3394. const std::string& language,
  3395. const std::string& arch) const
  3396. {
  3397. if (language != "C" && language != "CXX" && language != "OBJC" &&
  3398. language != "OBJCXX") {
  3399. return std::string();
  3400. }
  3401. const auto inserted =
  3402. this->PchSources.insert(std::make_pair(language + config + arch, ""));
  3403. if (inserted.second) {
  3404. const std::string pchHeader = this->GetPchHeader(config, language, arch);
  3405. if (pchHeader.empty()) {
  3406. return std::string();
  3407. }
  3408. std::string& filename = inserted.first->second;
  3409. const cmGeneratorTarget* generatorTarget = this;
  3410. cmProp pchReuseFrom =
  3411. generatorTarget->GetProperty("PRECOMPILE_HEADERS_REUSE_FROM");
  3412. if (pchReuseFrom) {
  3413. generatorTarget =
  3414. this->GetGlobalGenerator()->FindGeneratorTarget(*pchReuseFrom);
  3415. }
  3416. filename =
  3417. cmStrCat(generatorTarget->LocalGenerator->GetCurrentBinaryDirectory(),
  3418. "/CMakeFiles/", generatorTarget->GetName(), ".dir/cmake_pch");
  3419. // For GCC the source extension will be transformed into .h[xx].gch
  3420. if (!this->Makefile->IsOn("CMAKE_LINK_PCH")) {
  3421. const std::map<std::string, std::string> languageToExtension = {
  3422. { "C", ".h.c" },
  3423. { "CXX", ".hxx.cxx" },
  3424. { "OBJC", ".objc.h.m" },
  3425. { "OBJCXX", ".objcxx.hxx.mm" }
  3426. };
  3427. filename = cmStrCat(filename, arch.empty() ? "" : cmStrCat("_", arch),
  3428. languageToExtension.at(language));
  3429. } else {
  3430. const std::map<std::string, std::string> languageToExtension = {
  3431. { "C", ".c" }, { "CXX", ".cxx" }, { "OBJC", ".m" }, { "OBJCXX", ".mm" }
  3432. };
  3433. filename = cmStrCat(filename, arch.empty() ? "" : cmStrCat("_", arch),
  3434. languageToExtension.at(language));
  3435. }
  3436. const std::string filename_tmp = cmStrCat(filename, ".tmp");
  3437. if (!pchReuseFrom) {
  3438. {
  3439. cmGeneratedFileStream file(filename_tmp);
  3440. file << "/* generated by CMake */\n";
  3441. }
  3442. cmFileTimes::Copy(pchHeader, filename_tmp);
  3443. cmSystemTools::MoveFileIfDifferent(filename_tmp, filename);
  3444. }
  3445. }
  3446. return inserted.first->second;
  3447. }
  3448. std::string cmGeneratorTarget::GetPchFileObject(const std::string& config,
  3449. const std::string& language,
  3450. const std::string& arch)
  3451. {
  3452. if (language != "C" && language != "CXX" && language != "OBJC" &&
  3453. language != "OBJCXX") {
  3454. return std::string();
  3455. }
  3456. const auto inserted =
  3457. this->PchObjectFiles.insert(std::make_pair(language + config + arch, ""));
  3458. if (inserted.second) {
  3459. const std::string pchSource = this->GetPchSource(config, language, arch);
  3460. if (pchSource.empty()) {
  3461. return std::string();
  3462. }
  3463. std::string& filename = inserted.first->second;
  3464. auto pchSf = this->Makefile->GetOrCreateSource(
  3465. pchSource, false, cmSourceFileLocationKind::Known);
  3466. filename = cmStrCat(this->ObjectDirectory, this->GetObjectName(pchSf));
  3467. if (this->GetGlobalGenerator()->IsMultiConfig()) {
  3468. cmSystemTools::ReplaceString(
  3469. filename, this->GetGlobalGenerator()->GetCMakeCFGIntDir(), config);
  3470. }
  3471. }
  3472. return inserted.first->second;
  3473. }
  3474. std::string cmGeneratorTarget::GetPchFile(const std::string& config,
  3475. const std::string& language,
  3476. const std::string& arch)
  3477. {
  3478. const auto inserted =
  3479. this->PchFiles.insert(std::make_pair(language + config + arch, ""));
  3480. if (inserted.second) {
  3481. std::string& pchFile = inserted.first->second;
  3482. const std::string pchExtension =
  3483. this->Makefile->GetSafeDefinition("CMAKE_PCH_EXTENSION");
  3484. if (this->Makefile->IsOn("CMAKE_LINK_PCH")) {
  3485. auto replaceExtension = [](const std::string& str,
  3486. const std::string& ext) -> std::string {
  3487. auto dot_pos = str.rfind('.');
  3488. std::string result;
  3489. if (dot_pos != std::string::npos) {
  3490. result = str.substr(0, dot_pos);
  3491. }
  3492. result += ext;
  3493. return result;
  3494. };
  3495. cmGeneratorTarget* generatorTarget = this;
  3496. cmProp pchReuseFrom =
  3497. generatorTarget->GetProperty("PRECOMPILE_HEADERS_REUSE_FROM");
  3498. if (pchReuseFrom) {
  3499. generatorTarget =
  3500. this->GetGlobalGenerator()->FindGeneratorTarget(*pchReuseFrom);
  3501. }
  3502. const std::string pchFileObject =
  3503. generatorTarget->GetPchFileObject(config, language, arch);
  3504. if (!pchExtension.empty()) {
  3505. pchFile = replaceExtension(pchFileObject, pchExtension);
  3506. }
  3507. } else {
  3508. pchFile = this->GetPchHeader(config, language, arch);
  3509. pchFile += pchExtension;
  3510. }
  3511. }
  3512. return inserted.first->second;
  3513. }
  3514. std::string cmGeneratorTarget::GetPchCreateCompileOptions(
  3515. const std::string& config, const std::string& language,
  3516. const std::string& arch)
  3517. {
  3518. const auto inserted = this->PchCreateCompileOptions.insert(
  3519. std::make_pair(language + config + arch, ""));
  3520. if (inserted.second) {
  3521. std::string& createOptionList = inserted.first->second;
  3522. if (this->GetPropertyAsBool("PCH_WARN_INVALID")) {
  3523. createOptionList = this->Makefile->GetSafeDefinition(
  3524. cmStrCat("CMAKE_", language, "_COMPILE_OPTIONS_INVALID_PCH"));
  3525. }
  3526. const std::string createOptVar =
  3527. cmStrCat("CMAKE_", language, "_COMPILE_OPTIONS_CREATE_PCH");
  3528. createOptionList = cmStrCat(
  3529. createOptionList, ";", this->Makefile->GetSafeDefinition(createOptVar));
  3530. const std::string pchHeader = this->GetPchHeader(config, language, arch);
  3531. const std::string pchFile = this->GetPchFile(config, language, arch);
  3532. cmSystemTools::ReplaceString(createOptionList, "<PCH_HEADER>", pchHeader);
  3533. cmSystemTools::ReplaceString(createOptionList, "<PCH_FILE>", pchFile);
  3534. }
  3535. return inserted.first->second;
  3536. }
  3537. std::string cmGeneratorTarget::GetPchUseCompileOptions(
  3538. const std::string& config, const std::string& language,
  3539. const std::string& arch)
  3540. {
  3541. const auto inserted = this->PchUseCompileOptions.insert(
  3542. std::make_pair(language + config + arch, ""));
  3543. if (inserted.second) {
  3544. std::string& useOptionList = inserted.first->second;
  3545. if (this->GetPropertyAsBool("PCH_WARN_INVALID")) {
  3546. useOptionList = this->Makefile->GetSafeDefinition(
  3547. cmStrCat("CMAKE_", language, "_COMPILE_OPTIONS_INVALID_PCH"));
  3548. }
  3549. const std::string useOptVar =
  3550. cmStrCat(language, "_COMPILE_OPTIONS_USE_PCH");
  3551. std::string const& useOptionListProperty =
  3552. this->GetSafeProperty(useOptVar);
  3553. useOptionList = cmStrCat(
  3554. useOptionList, ";",
  3555. useOptionListProperty.empty()
  3556. ? this->Makefile->GetSafeDefinition(cmStrCat("CMAKE_", useOptVar))
  3557. : useOptionListProperty);
  3558. const std::string pchHeader = this->GetPchHeader(config, language, arch);
  3559. const std::string pchFile = this->GetPchFile(config, language, arch);
  3560. cmSystemTools::ReplaceString(useOptionList, "<PCH_HEADER>", pchHeader);
  3561. cmSystemTools::ReplaceString(useOptionList, "<PCH_FILE>", pchFile);
  3562. }
  3563. return inserted.first->second;
  3564. }
  3565. void cmGeneratorTarget::AddSourceFileToUnityBatch(
  3566. const std::string& sourceFilename)
  3567. {
  3568. this->UnityBatchedSourceFiles.insert(sourceFilename);
  3569. }
  3570. bool cmGeneratorTarget::IsSourceFilePartOfUnityBatch(
  3571. const std::string& sourceFilename) const
  3572. {
  3573. if (!this->GetPropertyAsBool("UNITY_BUILD")) {
  3574. return false;
  3575. }
  3576. return this->UnityBatchedSourceFiles.find(sourceFilename) !=
  3577. this->UnityBatchedSourceFiles.end();
  3578. }
  3579. void cmGeneratorTarget::GetLinkOptions(std::vector<std::string>& result,
  3580. const std::string& config,
  3581. const std::string& language) const
  3582. {
  3583. if (this->IsDeviceLink() &&
  3584. this->GetPolicyStatusCMP0105() != cmPolicies::NEW) {
  3585. // link options are not propagated to the device link step
  3586. return;
  3587. }
  3588. std::vector<BT<std::string>> tmp = this->GetLinkOptions(config, language);
  3589. result.reserve(tmp.size());
  3590. for (BT<std::string>& v : tmp) {
  3591. result.emplace_back(std::move(v.Value));
  3592. }
  3593. }
  3594. std::vector<BT<std::string>> cmGeneratorTarget::GetLinkOptions(
  3595. std::string const& config, std::string const& language) const
  3596. {
  3597. std::vector<BT<std::string>> result;
  3598. std::unordered_set<std::string> uniqueOptions;
  3599. cmGeneratorExpressionDAGChecker dagChecker(this, "LINK_OPTIONS", nullptr,
  3600. nullptr);
  3601. std::vector<std::string> debugProperties;
  3602. this->Makefile->GetDefExpandList("CMAKE_DEBUG_TARGET_PROPERTIES",
  3603. debugProperties);
  3604. bool debugOptions = !this->DebugLinkOptionsDone &&
  3605. cm::contains(debugProperties, "LINK_OPTIONS");
  3606. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3607. this->DebugLinkOptionsDone = true;
  3608. }
  3609. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  3610. this, config, language, &dagChecker, this->LinkOptionsEntries);
  3611. AddInterfaceEntries(this, config, "INTERFACE_LINK_OPTIONS", language,
  3612. &dagChecker, entries,
  3613. this->GetPolicyStatusCMP0099() != cmPolicies::NEW);
  3614. processOptions(this, entries, result, uniqueOptions, debugOptions,
  3615. "link options", OptionsParse::Shell, this->IsDeviceLink());
  3616. if (this->IsDeviceLink()) {
  3617. // wrap host link options
  3618. const std::string wrapper(this->Makefile->GetSafeDefinition(
  3619. "CMAKE_" + language + "_DEVICE_COMPILER_WRAPPER_FLAG"));
  3620. std::vector<std::string> wrapperFlag = cmExpandedList(wrapper);
  3621. const std::string wrapperSep(this->Makefile->GetSafeDefinition(
  3622. "CMAKE_" + language + "_DEVICE_COMPILER_WRAPPER_FLAG_SEP"));
  3623. bool concatFlagAndArgs = true;
  3624. if (!wrapperFlag.empty() && wrapperFlag.back() == " ") {
  3625. concatFlagAndArgs = false;
  3626. wrapperFlag.pop_back();
  3627. }
  3628. auto it = result.begin();
  3629. while (it != result.end()) {
  3630. if (it->Value == DL_BEGIN) {
  3631. // device link options, no treatment
  3632. it = result.erase(it);
  3633. it = std::find_if(it, result.end(), [](const BT<std::string>& item) {
  3634. return item.Value == DL_END;
  3635. });
  3636. if (it != result.end()) {
  3637. it = result.erase(it);
  3638. }
  3639. } else {
  3640. // host link options must be wrapped
  3641. std::vector<std::string> options;
  3642. cmSystemTools::ParseUnixCommandLine(it->Value.c_str(), options);
  3643. auto hostOptions = wrapOptions(options, it->Backtrace, wrapperFlag,
  3644. wrapperSep, concatFlagAndArgs);
  3645. it = result.erase(it);
  3646. // some compilers (like gcc 4.8 or Intel 19.0 or XLC 16) do not respect
  3647. // C++11 standard: 'std::vector::insert()' do not returns an iterator,
  3648. // so need to recompute the iterator after insertion.
  3649. if (it == result.end()) {
  3650. cm::append(result, hostOptions);
  3651. it = result.end();
  3652. } else {
  3653. auto index = it - result.begin();
  3654. result.insert(it, hostOptions.begin(), hostOptions.end());
  3655. it = result.begin() + index + hostOptions.size();
  3656. }
  3657. }
  3658. }
  3659. }
  3660. // Last step: replace "LINKER:" prefixed elements by
  3661. // actual linker wrapper
  3662. const std::string wrapper(this->Makefile->GetSafeDefinition(
  3663. "CMAKE_" + language +
  3664. (this->IsDeviceLink() ? "_DEVICE_LINKER_WRAPPER_FLAG"
  3665. : "_LINKER_WRAPPER_FLAG")));
  3666. std::vector<std::string> wrapperFlag = cmExpandedList(wrapper);
  3667. const std::string wrapperSep(this->Makefile->GetSafeDefinition(
  3668. "CMAKE_" + language +
  3669. (this->IsDeviceLink() ? "_DEVICE_LINKER_WRAPPER_FLAG_SEP"
  3670. : "_LINKER_WRAPPER_FLAG_SEP")));
  3671. bool concatFlagAndArgs = true;
  3672. if (!wrapperFlag.empty() && wrapperFlag.back() == " ") {
  3673. concatFlagAndArgs = false;
  3674. wrapperFlag.pop_back();
  3675. }
  3676. const std::string LINKER{ "LINKER:" };
  3677. const std::string SHELL{ "SHELL:" };
  3678. const std::string LINKER_SHELL = LINKER + SHELL;
  3679. std::vector<BT<std::string>>::iterator entry;
  3680. while ((entry = std::find_if(result.begin(), result.end(),
  3681. [&LINKER](BT<std::string> const& item) -> bool {
  3682. return item.Value.compare(0, LINKER.length(),
  3683. LINKER) == 0;
  3684. })) != result.end()) {
  3685. std::string value = std::move(entry->Value);
  3686. cmListFileBacktrace bt = std::move(entry->Backtrace);
  3687. entry = result.erase(entry);
  3688. std::vector<std::string> linkerOptions;
  3689. if (value.compare(0, LINKER_SHELL.length(), LINKER_SHELL) == 0) {
  3690. cmSystemTools::ParseUnixCommandLine(
  3691. value.c_str() + LINKER_SHELL.length(), linkerOptions);
  3692. } else {
  3693. linkerOptions = cmTokenize(value.substr(LINKER.length()), ",");
  3694. }
  3695. if (linkerOptions.empty() ||
  3696. (linkerOptions.size() == 1 && linkerOptions.front().empty())) {
  3697. continue;
  3698. }
  3699. // for now, raise an error if prefix SHELL: is part of arguments
  3700. if (std::find_if(linkerOptions.begin(), linkerOptions.end(),
  3701. [&SHELL](const std::string& item) -> bool {
  3702. return item.find(SHELL) != std::string::npos;
  3703. }) != linkerOptions.end()) {
  3704. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  3705. MessageType::FATAL_ERROR,
  3706. "'SHELL:' prefix is not supported as part of 'LINKER:' arguments.",
  3707. this->GetBacktrace());
  3708. return result;
  3709. }
  3710. std::vector<BT<std::string>> options = wrapOptions(
  3711. linkerOptions, bt, wrapperFlag, wrapperSep, concatFlagAndArgs);
  3712. result.insert(entry, options.begin(), options.end());
  3713. }
  3714. return result;
  3715. }
  3716. void cmGeneratorTarget::GetStaticLibraryLinkOptions(
  3717. std::vector<std::string>& result, const std::string& config,
  3718. const std::string& language) const
  3719. {
  3720. std::vector<BT<std::string>> tmp =
  3721. this->GetStaticLibraryLinkOptions(config, language);
  3722. result.reserve(tmp.size());
  3723. for (BT<std::string>& v : tmp) {
  3724. result.emplace_back(std::move(v.Value));
  3725. }
  3726. }
  3727. std::vector<BT<std::string>> cmGeneratorTarget::GetStaticLibraryLinkOptions(
  3728. std::string const& config, std::string const& language) const
  3729. {
  3730. std::vector<BT<std::string>> result;
  3731. std::unordered_set<std::string> uniqueOptions;
  3732. cmGeneratorExpressionDAGChecker dagChecker(this, "STATIC_LIBRARY_OPTIONS",
  3733. nullptr, nullptr);
  3734. EvaluatedTargetPropertyEntries entries;
  3735. if (cmProp linkOptions = this->GetProperty("STATIC_LIBRARY_OPTIONS")) {
  3736. std::vector<std::string> options = cmExpandedList(*linkOptions);
  3737. for (const auto& option : options) {
  3738. std::unique_ptr<TargetPropertyEntry> entry =
  3739. CreateTargetPropertyEntry(option);
  3740. entries.Entries.emplace_back(EvaluateTargetPropertyEntry(
  3741. this, config, language, &dagChecker, *entry));
  3742. }
  3743. }
  3744. processOptions(this, entries, result, uniqueOptions, false,
  3745. "static library link options", OptionsParse::Shell);
  3746. return result;
  3747. }
  3748. namespace {
  3749. void processLinkDirectories(cmGeneratorTarget const* tgt,
  3750. EvaluatedTargetPropertyEntries& entries,
  3751. std::vector<BT<std::string>>& directories,
  3752. std::unordered_set<std::string>& uniqueDirectories,
  3753. bool debugDirectories)
  3754. {
  3755. for (EvaluatedTargetPropertyEntry& entry : entries.Entries) {
  3756. cmLinkImplItem const& item = entry.LinkImplItem;
  3757. std::string const& targetName = item.AsStr();
  3758. std::string usedDirectories;
  3759. for (std::string& entryDirectory : entry.Values) {
  3760. if (!cmSystemTools::FileIsFullPath(entryDirectory)) {
  3761. std::ostringstream e;
  3762. bool noMessage = false;
  3763. MessageType messageType = MessageType::FATAL_ERROR;
  3764. if (!targetName.empty()) {
  3765. /* clang-format off */
  3766. e << "Target \"" << targetName << "\" contains relative "
  3767. "path in its INTERFACE_LINK_DIRECTORIES:\n"
  3768. " \"" << entryDirectory << "\"";
  3769. /* clang-format on */
  3770. } else {
  3771. switch (tgt->GetPolicyStatusCMP0081()) {
  3772. case cmPolicies::WARN: {
  3773. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0081) << "\n";
  3774. messageType = MessageType::AUTHOR_WARNING;
  3775. } break;
  3776. case cmPolicies::OLD:
  3777. noMessage = true;
  3778. break;
  3779. case cmPolicies::REQUIRED_IF_USED:
  3780. case cmPolicies::REQUIRED_ALWAYS:
  3781. case cmPolicies::NEW:
  3782. // Issue the fatal message.
  3783. break;
  3784. }
  3785. e << "Found relative path while evaluating link directories of "
  3786. "\""
  3787. << tgt->GetName() << "\":\n \"" << entryDirectory << "\"\n";
  3788. }
  3789. if (!noMessage) {
  3790. tgt->GetLocalGenerator()->IssueMessage(messageType, e.str());
  3791. if (messageType == MessageType::FATAL_ERROR) {
  3792. return;
  3793. }
  3794. }
  3795. }
  3796. // Sanitize the path the same way the link_directories command does
  3797. // in case projects set the LINK_DIRECTORIES property directly.
  3798. cmSystemTools::ConvertToUnixSlashes(entryDirectory);
  3799. if (uniqueDirectories.insert(entryDirectory).second) {
  3800. directories.emplace_back(entryDirectory, entry.Backtrace);
  3801. if (debugDirectories) {
  3802. usedDirectories += " * " + entryDirectory + "\n";
  3803. }
  3804. }
  3805. }
  3806. if (!usedDirectories.empty()) {
  3807. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  3808. MessageType::LOG,
  3809. std::string("Used link directories for target ") + tgt->GetName() +
  3810. ":\n" + usedDirectories,
  3811. entry.Backtrace);
  3812. }
  3813. }
  3814. }
  3815. }
  3816. void cmGeneratorTarget::GetLinkDirectories(std::vector<std::string>& result,
  3817. const std::string& config,
  3818. const std::string& language) const
  3819. {
  3820. std::vector<BT<std::string>> tmp =
  3821. this->GetLinkDirectories(config, language);
  3822. result.reserve(tmp.size());
  3823. for (BT<std::string>& v : tmp) {
  3824. result.emplace_back(std::move(v.Value));
  3825. }
  3826. }
  3827. std::vector<BT<std::string>> cmGeneratorTarget::GetLinkDirectories(
  3828. std::string const& config, std::string const& language) const
  3829. {
  3830. std::vector<BT<std::string>> result;
  3831. std::unordered_set<std::string> uniqueDirectories;
  3832. cmGeneratorExpressionDAGChecker dagChecker(this, "LINK_DIRECTORIES", nullptr,
  3833. nullptr);
  3834. std::vector<std::string> debugProperties;
  3835. this->Makefile->GetDefExpandList("CMAKE_DEBUG_TARGET_PROPERTIES",
  3836. debugProperties);
  3837. bool debugDirectories = !this->DebugLinkDirectoriesDone &&
  3838. cm::contains(debugProperties, "LINK_DIRECTORIES");
  3839. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  3840. this->DebugLinkDirectoriesDone = true;
  3841. }
  3842. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  3843. this, config, language, &dagChecker, this->LinkDirectoriesEntries);
  3844. AddInterfaceEntries(this, config, "INTERFACE_LINK_DIRECTORIES", language,
  3845. &dagChecker, entries,
  3846. this->GetPolicyStatusCMP0099() != cmPolicies::NEW);
  3847. processLinkDirectories(this, entries, result, uniqueDirectories,
  3848. debugDirectories);
  3849. return result;
  3850. }
  3851. void cmGeneratorTarget::GetLinkDepends(std::vector<std::string>& result,
  3852. const std::string& config,
  3853. const std::string& language) const
  3854. {
  3855. std::vector<BT<std::string>> tmp = this->GetLinkDepends(config, language);
  3856. result.reserve(tmp.size());
  3857. for (BT<std::string>& v : tmp) {
  3858. result.emplace_back(std::move(v.Value));
  3859. }
  3860. }
  3861. std::vector<BT<std::string>> cmGeneratorTarget::GetLinkDepends(
  3862. std::string const& config, std::string const& language) const
  3863. {
  3864. std::vector<BT<std::string>> result;
  3865. std::unordered_set<std::string> uniqueOptions;
  3866. cmGeneratorExpressionDAGChecker dagChecker(this, "LINK_DEPENDS", nullptr,
  3867. nullptr);
  3868. EvaluatedTargetPropertyEntries entries;
  3869. if (cmProp linkDepends = this->GetProperty("LINK_DEPENDS")) {
  3870. std::vector<std::string> depends = cmExpandedList(*linkDepends);
  3871. for (const auto& depend : depends) {
  3872. std::unique_ptr<TargetPropertyEntry> entry =
  3873. CreateTargetPropertyEntry(depend);
  3874. entries.Entries.emplace_back(EvaluateTargetPropertyEntry(
  3875. this, config, language, &dagChecker, *entry));
  3876. }
  3877. }
  3878. AddInterfaceEntries(this, config, "INTERFACE_LINK_DEPENDS", language,
  3879. &dagChecker, entries,
  3880. this->GetPolicyStatusCMP0099() != cmPolicies::NEW);
  3881. processOptions(this, entries, result, uniqueOptions, false, "link depends",
  3882. OptionsParse::None);
  3883. return result;
  3884. }
  3885. void cmGeneratorTarget::ComputeTargetManifest(const std::string& config) const
  3886. {
  3887. if (this->IsImported()) {
  3888. return;
  3889. }
  3890. cmGlobalGenerator* gg = this->LocalGenerator->GetGlobalGenerator();
  3891. // Get the names.
  3892. cmGeneratorTarget::Names targetNames;
  3893. if (this->GetType() == cmStateEnums::EXECUTABLE) {
  3894. targetNames = this->GetExecutableNames(config);
  3895. } else if (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  3896. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  3897. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  3898. targetNames = this->GetLibraryNames(config);
  3899. } else {
  3900. return;
  3901. }
  3902. // Get the directory.
  3903. std::string dir =
  3904. this->GetDirectory(config, cmStateEnums::RuntimeBinaryArtifact);
  3905. // Add each name.
  3906. std::string f;
  3907. if (!targetNames.Output.empty()) {
  3908. f = cmStrCat(dir, '/', targetNames.Output);
  3909. gg->AddToManifest(f);
  3910. }
  3911. if (!targetNames.SharedObject.empty()) {
  3912. f = cmStrCat(dir, '/', targetNames.SharedObject);
  3913. gg->AddToManifest(f);
  3914. }
  3915. if (!targetNames.Real.empty()) {
  3916. f = cmStrCat(dir, '/', targetNames.Real);
  3917. gg->AddToManifest(f);
  3918. }
  3919. if (!targetNames.PDB.empty()) {
  3920. f = cmStrCat(dir, '/', targetNames.PDB);
  3921. gg->AddToManifest(f);
  3922. }
  3923. if (!targetNames.ImportLibrary.empty()) {
  3924. f =
  3925. cmStrCat(this->GetDirectory(config, cmStateEnums::ImportLibraryArtifact),
  3926. '/', targetNames.ImportLibrary);
  3927. gg->AddToManifest(f);
  3928. }
  3929. }
  3930. bool cmGeneratorTarget::ComputeCompileFeatures(std::string const& config) const
  3931. {
  3932. // Compute the language standard based on the compile features.
  3933. cmStandardLevelResolver standardResolver(this->Makefile);
  3934. std::vector<BT<std::string>> features = this->GetCompileFeatures(config);
  3935. for (BT<std::string> const& f : features) {
  3936. std::string lang;
  3937. if (!standardResolver.CompileFeatureKnown(this->Target->GetName(), f.Value,
  3938. lang, nullptr)) {
  3939. return false;
  3940. }
  3941. std::string key = cmStrCat(cmSystemTools::UpperCase(config), '-', lang);
  3942. cmProp currentLanguageStandard = this->GetLanguageStandard(lang, config);
  3943. std::string newRequiredStandard;
  3944. if (!standardResolver.GetNewRequiredStandard(
  3945. this->Target->GetName(), f.Value, currentLanguageStandard,
  3946. newRequiredStandard)) {
  3947. return false;
  3948. }
  3949. if (!newRequiredStandard.empty()) {
  3950. BTs<std::string>& languageStandardProperty =
  3951. this->LanguageStandardMap[key];
  3952. if (languageStandardProperty.Value != newRequiredStandard) {
  3953. languageStandardProperty.Value = newRequiredStandard;
  3954. languageStandardProperty.Backtraces.clear();
  3955. }
  3956. languageStandardProperty.Backtraces.emplace_back(f.Backtrace);
  3957. }
  3958. }
  3959. return true;
  3960. }
  3961. bool cmGeneratorTarget::ComputeCompileFeatures(
  3962. std::string const& config, std::set<LanguagePair> const& languagePairs) const
  3963. {
  3964. for (const auto& language : languagePairs) {
  3965. BTs<std::string> const* generatorTargetLanguageStandard =
  3966. this->GetLanguageStandardProperty(language.first, config);
  3967. if (!generatorTargetLanguageStandard) {
  3968. // If the standard isn't explicitly set we copy it over from the
  3969. // specified paired language.
  3970. std::string key =
  3971. cmStrCat(cmSystemTools::UpperCase(config), '-', language.first);
  3972. BTs<std::string> const* standardToCopy =
  3973. this->GetLanguageStandardProperty(language.second, config);
  3974. if (standardToCopy != nullptr) {
  3975. this->LanguageStandardMap[key] = *standardToCopy;
  3976. generatorTargetLanguageStandard = &this->LanguageStandardMap[key];
  3977. } else {
  3978. cmProp defaultStandard = this->Makefile->GetDef(
  3979. cmStrCat("CMAKE_", language.second, "_STANDARD_DEFAULT"));
  3980. if (defaultStandard != nullptr) {
  3981. this->LanguageStandardMap[key] = BTs<std::string>(*defaultStandard);
  3982. generatorTargetLanguageStandard = &this->LanguageStandardMap[key];
  3983. }
  3984. }
  3985. // Custom updates for the CUDA standard.
  3986. if (generatorTargetLanguageStandard != nullptr &&
  3987. language.first == "CUDA") {
  3988. if (generatorTargetLanguageStandard->Value == "98") {
  3989. this->LanguageStandardMap[key].Value = "03";
  3990. }
  3991. }
  3992. }
  3993. }
  3994. return true;
  3995. }
  3996. std::string cmGeneratorTarget::GetImportedLibName(
  3997. std::string const& config) const
  3998. {
  3999. if (cmGeneratorTarget::ImportInfo const* info =
  4000. this->GetImportInfo(config)) {
  4001. return info->LibName;
  4002. }
  4003. return std::string();
  4004. }
  4005. std::string cmGeneratorTarget::GetFullPath(const std::string& config,
  4006. cmStateEnums::ArtifactType artifact,
  4007. bool realname) const
  4008. {
  4009. if (this->IsImported()) {
  4010. return this->Target->ImportedGetFullPath(config, artifact);
  4011. }
  4012. return this->NormalGetFullPath(config, artifact, realname);
  4013. }
  4014. std::string cmGeneratorTarget::NormalGetFullPath(
  4015. const std::string& config, cmStateEnums::ArtifactType artifact,
  4016. bool realname) const
  4017. {
  4018. std::string fpath = cmStrCat(this->GetDirectory(config, artifact), '/');
  4019. if (this->IsAppBundleOnApple()) {
  4020. fpath =
  4021. cmStrCat(this->BuildBundleDirectory(fpath, config, FullLevel), '/');
  4022. }
  4023. // Add the full name of the target.
  4024. switch (artifact) {
  4025. case cmStateEnums::RuntimeBinaryArtifact:
  4026. if (realname) {
  4027. fpath += this->NormalGetRealName(config);
  4028. } else {
  4029. fpath +=
  4030. this->GetFullName(config, cmStateEnums::RuntimeBinaryArtifact);
  4031. }
  4032. break;
  4033. case cmStateEnums::ImportLibraryArtifact:
  4034. fpath += this->GetFullName(config, cmStateEnums::ImportLibraryArtifact);
  4035. break;
  4036. }
  4037. return fpath;
  4038. }
  4039. std::string cmGeneratorTarget::NormalGetRealName(
  4040. const std::string& config) const
  4041. {
  4042. // This should not be called for imported targets.
  4043. // TODO: Split cmTarget into a class hierarchy to get compile-time
  4044. // enforcement of the limited imported target API.
  4045. if (this->IsImported()) {
  4046. std::string msg = cmStrCat("NormalGetRealName called on imported target: ",
  4047. this->GetName());
  4048. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  4049. }
  4050. if (this->GetType() == cmStateEnums::EXECUTABLE) {
  4051. // Compute the real name that will be built.
  4052. return this->GetExecutableNames(config).Real;
  4053. }
  4054. // Compute the real name that will be built.
  4055. return this->GetLibraryNames(config).Real;
  4056. }
  4057. cmGeneratorTarget::Names cmGeneratorTarget::GetLibraryNames(
  4058. const std::string& config) const
  4059. {
  4060. cmGeneratorTarget::Names targetNames;
  4061. // This should not be called for imported targets.
  4062. // TODO: Split cmTarget into a class hierarchy to get compile-time
  4063. // enforcement of the limited imported target API.
  4064. if (this->IsImported()) {
  4065. std::string msg =
  4066. cmStrCat("GetLibraryNames called on imported target: ", this->GetName());
  4067. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  4068. }
  4069. // Check for library version properties.
  4070. cmProp version = this->GetProperty("VERSION");
  4071. cmProp soversion = this->GetProperty("SOVERSION");
  4072. if (!this->HasSOName(config) ||
  4073. this->Makefile->IsOn("CMAKE_PLATFORM_NO_VERSIONED_SONAME") ||
  4074. this->IsFrameworkOnApple()) {
  4075. // Versioning is supported only for shared libraries and modules,
  4076. // and then only when the platform supports an soname flag.
  4077. version = nullptr;
  4078. soversion = nullptr;
  4079. }
  4080. if (version && !soversion) {
  4081. // The soversion must be set if the library version is set. Use
  4082. // the library version as the soversion.
  4083. soversion = version;
  4084. }
  4085. if (!version && soversion) {
  4086. // Use the soversion as the library version.
  4087. version = soversion;
  4088. }
  4089. // Get the components of the library name.
  4090. std::string prefix;
  4091. std::string suffix;
  4092. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  4093. prefix, targetNames.Base, suffix);
  4094. // The library name.
  4095. targetNames.Output = prefix + targetNames.Base + suffix;
  4096. if (this->IsFrameworkOnApple()) {
  4097. targetNames.Real = prefix;
  4098. if (!this->Makefile->PlatformIsAppleEmbedded()) {
  4099. targetNames.Real += "Versions/";
  4100. targetNames.Real += this->GetFrameworkVersion();
  4101. targetNames.Real += "/";
  4102. }
  4103. targetNames.Real += targetNames.Base + suffix;
  4104. targetNames.SharedObject = targetNames.Real + suffix;
  4105. } else {
  4106. // The library's soname.
  4107. this->ComputeVersionedName(targetNames.SharedObject, prefix,
  4108. targetNames.Base, suffix, targetNames.Output,
  4109. (soversion ? soversion->c_str() : nullptr));
  4110. // The library's real name on disk.
  4111. this->ComputeVersionedName(targetNames.Real, prefix, targetNames.Base,
  4112. suffix, targetNames.Output,
  4113. (version ? version->c_str() : nullptr));
  4114. }
  4115. // The import library name.
  4116. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4117. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  4118. targetNames.ImportLibrary =
  4119. this->GetFullNameInternal(config, cmStateEnums::ImportLibraryArtifact);
  4120. }
  4121. // The program database file name.
  4122. targetNames.PDB = this->GetPDBName(config);
  4123. return targetNames;
  4124. }
  4125. cmGeneratorTarget::Names cmGeneratorTarget::GetExecutableNames(
  4126. const std::string& config) const
  4127. {
  4128. cmGeneratorTarget::Names targetNames;
  4129. // This should not be called for imported targets.
  4130. // TODO: Split cmTarget into a class hierarchy to get compile-time
  4131. // enforcement of the limited imported target API.
  4132. if (this->IsImported()) {
  4133. std::string msg = cmStrCat(
  4134. "GetExecutableNames called on imported target: ", this->GetName());
  4135. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  4136. }
  4137. // This versioning is supported only for executables and then only
  4138. // when the platform supports symbolic links.
  4139. #if defined(_WIN32) && !defined(__CYGWIN__)
  4140. const char* version = nullptr;
  4141. #else
  4142. // Check for executable version properties.
  4143. const char* version = nullptr;
  4144. if (cmProp p = this->GetProperty("VERSION")) {
  4145. version = p->c_str();
  4146. }
  4147. if (this->GetType() != cmStateEnums::EXECUTABLE ||
  4148. this->Makefile->IsOn("XCODE")) {
  4149. version = nullptr;
  4150. }
  4151. #endif
  4152. // Get the components of the executable name.
  4153. std::string prefix;
  4154. std::string suffix;
  4155. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  4156. prefix, targetNames.Base, suffix);
  4157. // The executable name.
  4158. targetNames.Output = prefix + targetNames.Base + suffix;
  4159. // The executable's real name on disk.
  4160. #if defined(__CYGWIN__)
  4161. targetNames.Real = prefix + targetNames.Base;
  4162. #else
  4163. targetNames.Real = targetNames.Output;
  4164. #endif
  4165. if (version) {
  4166. targetNames.Real += "-";
  4167. targetNames.Real += version;
  4168. }
  4169. #if defined(__CYGWIN__)
  4170. targetNames.Real += suffix;
  4171. #endif
  4172. // The import library name.
  4173. targetNames.ImportLibrary =
  4174. this->GetFullNameInternal(config, cmStateEnums::ImportLibraryArtifact);
  4175. // The program database file name.
  4176. targetNames.PDB = this->GetPDBName(config);
  4177. return targetNames;
  4178. }
  4179. std::string cmGeneratorTarget::GetFullNameInternal(
  4180. const std::string& config, cmStateEnums::ArtifactType artifact) const
  4181. {
  4182. std::string prefix;
  4183. std::string base;
  4184. std::string suffix;
  4185. this->GetFullNameInternal(config, artifact, prefix, base, suffix);
  4186. return prefix + base + suffix;
  4187. }
  4188. std::string cmGeneratorTarget::ImportedGetLocation(
  4189. const std::string& config) const
  4190. {
  4191. assert(this->IsImported());
  4192. return this->Target->ImportedGetFullPath(
  4193. config, cmStateEnums::RuntimeBinaryArtifact);
  4194. }
  4195. std::string cmGeneratorTarget::GetFullNameImported(
  4196. const std::string& config, cmStateEnums::ArtifactType artifact) const
  4197. {
  4198. return cmSystemTools::GetFilenameName(
  4199. this->Target->ImportedGetFullPath(config, artifact));
  4200. }
  4201. void cmGeneratorTarget::GetFullNameInternal(
  4202. const std::string& config, cmStateEnums::ArtifactType artifact,
  4203. std::string& outPrefix, std::string& outBase, std::string& outSuffix) const
  4204. {
  4205. // Use just the target name for non-main target types.
  4206. if (this->GetType() != cmStateEnums::STATIC_LIBRARY &&
  4207. this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  4208. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  4209. this->GetType() != cmStateEnums::EXECUTABLE) {
  4210. outPrefix.clear();
  4211. outBase = this->GetName();
  4212. outSuffix.clear();
  4213. return;
  4214. }
  4215. const bool isImportedLibraryArtifact =
  4216. (artifact == cmStateEnums::ImportLibraryArtifact);
  4217. // Return an empty name for the import library if this platform
  4218. // does not support import libraries.
  4219. if (isImportedLibraryArtifact && !this->NeedImportLibraryName(config)) {
  4220. outPrefix.clear();
  4221. outBase.clear();
  4222. outSuffix.clear();
  4223. return;
  4224. }
  4225. // retrieve prefix and suffix
  4226. std::string ll = this->GetLinkerLanguage(config);
  4227. const char* targetPrefix = this->GetFilePrefixInternal(config, artifact, ll);
  4228. const char* targetSuffix = this->GetFileSuffixInternal(config, artifact, ll);
  4229. // The implib option is only allowed for shared libraries, module
  4230. // libraries, and executables.
  4231. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  4232. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  4233. this->GetType() != cmStateEnums::EXECUTABLE) {
  4234. artifact = cmStateEnums::RuntimeBinaryArtifact;
  4235. }
  4236. // Compute the full name for main target types.
  4237. const std::string configPostfix = this->GetFilePostfix(config);
  4238. // frameworks have directory prefix but no suffix
  4239. std::string fw_prefix;
  4240. if (this->IsFrameworkOnApple()) {
  4241. fw_prefix =
  4242. cmStrCat(this->GetFrameworkDirectory(config, ContentLevel), '/');
  4243. targetPrefix = fw_prefix.c_str();
  4244. targetSuffix = nullptr;
  4245. }
  4246. if (this->IsCFBundleOnApple()) {
  4247. fw_prefix = cmStrCat(this->GetCFBundleDirectory(config, FullLevel), '/');
  4248. targetPrefix = fw_prefix.c_str();
  4249. targetSuffix = nullptr;
  4250. }
  4251. // Begin the final name with the prefix.
  4252. outPrefix = targetPrefix ? targetPrefix : "";
  4253. // Append the target name or property-specified name.
  4254. outBase += this->GetOutputName(config, artifact);
  4255. // Append the per-configuration postfix.
  4256. // When using Xcode, the postfix should be part of the suffix rather than
  4257. // the base, because the suffix ends up being used in Xcode's
  4258. // EXECUTABLE_SUFFIX attribute.
  4259. if (this->IsFrameworkOnApple() &&
  4260. GetGlobalGenerator()->GetName() == "Xcode") {
  4261. targetSuffix = configPostfix.c_str();
  4262. } else {
  4263. outBase += configPostfix;
  4264. }
  4265. // Name shared libraries with their version number on some platforms.
  4266. if (cmProp soversion = this->GetProperty("SOVERSION")) {
  4267. if (this->GetType() == cmStateEnums::SHARED_LIBRARY &&
  4268. !isImportedLibraryArtifact &&
  4269. this->Makefile->IsOn("CMAKE_SHARED_LIBRARY_NAME_WITH_VERSION")) {
  4270. outBase += "-";
  4271. outBase += *soversion;
  4272. }
  4273. }
  4274. // Append the suffix.
  4275. outSuffix = targetSuffix ? targetSuffix : "";
  4276. }
  4277. std::string cmGeneratorTarget::GetLinkerLanguage(
  4278. const std::string& config) const
  4279. {
  4280. return this->GetLinkClosure(config)->LinkerLanguage;
  4281. }
  4282. std::string cmGeneratorTarget::GetPDBOutputName(
  4283. const std::string& config) const
  4284. {
  4285. std::string base =
  4286. this->GetOutputName(config, cmStateEnums::RuntimeBinaryArtifact);
  4287. std::vector<std::string> props;
  4288. std::string configUpper = cmSystemTools::UpperCase(config);
  4289. if (!configUpper.empty()) {
  4290. // PDB_NAME_<CONFIG>
  4291. props.push_back("PDB_NAME_" + configUpper);
  4292. }
  4293. // PDB_NAME
  4294. props.emplace_back("PDB_NAME");
  4295. for (std::string const& p : props) {
  4296. if (cmProp outName = this->GetProperty(p)) {
  4297. base = *outName;
  4298. break;
  4299. }
  4300. }
  4301. return base;
  4302. }
  4303. std::string cmGeneratorTarget::GetPDBName(const std::string& config) const
  4304. {
  4305. std::string prefix;
  4306. std::string base;
  4307. std::string suffix;
  4308. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  4309. prefix, base, suffix);
  4310. std::vector<std::string> props;
  4311. std::string configUpper = cmSystemTools::UpperCase(config);
  4312. if (!configUpper.empty()) {
  4313. // PDB_NAME_<CONFIG>
  4314. props.push_back("PDB_NAME_" + configUpper);
  4315. }
  4316. // PDB_NAME
  4317. props.emplace_back("PDB_NAME");
  4318. for (std::string const& p : props) {
  4319. if (cmProp outName = this->GetProperty(p)) {
  4320. base = *outName;
  4321. break;
  4322. }
  4323. }
  4324. return prefix + base + ".pdb";
  4325. }
  4326. std::string cmGeneratorTarget::GetObjectDirectory(
  4327. std::string const& config) const
  4328. {
  4329. std::string obj_dir =
  4330. this->GlobalGenerator->ExpandCFGIntDir(this->ObjectDirectory, config);
  4331. #if defined(__APPLE__)
  4332. // find and replace $(PROJECT_NAME) xcode placeholder
  4333. const std::string projectName = this->LocalGenerator->GetProjectName();
  4334. cmSystemTools::ReplaceString(obj_dir, "$(PROJECT_NAME)", projectName);
  4335. // Replace Xcode's placeholder for the object file directory since
  4336. // installation and export scripts need to know the real directory.
  4337. // Xcode has build-time settings (e.g. for sanitizers) that affect this,
  4338. // but we use the default here. Users that want to enable sanitizers
  4339. // will do so at the cost of object library installation and export.
  4340. cmSystemTools::ReplaceString(obj_dir, "$(OBJECT_FILE_DIR_normal:base)",
  4341. "Objects-normal");
  4342. #endif
  4343. return obj_dir;
  4344. }
  4345. void cmGeneratorTarget::GetTargetObjectNames(
  4346. std::string const& config, std::vector<std::string>& objects) const
  4347. {
  4348. std::vector<cmSourceFile const*> objectSources;
  4349. this->GetObjectSources(objectSources, config);
  4350. std::map<cmSourceFile const*, std::string> mapping;
  4351. for (cmSourceFile const* sf : objectSources) {
  4352. mapping[sf];
  4353. }
  4354. this->LocalGenerator->ComputeObjectFilenames(mapping, this);
  4355. for (cmSourceFile const* src : objectSources) {
  4356. // Find the object file name corresponding to this source file.
  4357. auto map_it = mapping.find(src);
  4358. // It must exist because we populated the mapping just above.
  4359. assert(!map_it->second.empty());
  4360. objects.push_back(map_it->second);
  4361. }
  4362. }
  4363. bool cmGeneratorTarget::StrictTargetComparison::operator()(
  4364. cmGeneratorTarget const* t1, cmGeneratorTarget const* t2) const
  4365. {
  4366. int nameResult = strcmp(t1->GetName().c_str(), t2->GetName().c_str());
  4367. if (nameResult == 0) {
  4368. return strcmp(
  4369. t1->GetLocalGenerator()->GetCurrentBinaryDirectory().c_str(),
  4370. t2->GetLocalGenerator()->GetCurrentBinaryDirectory().c_str()) < 0;
  4371. }
  4372. return nameResult < 0;
  4373. }
  4374. struct cmGeneratorTarget::SourceFileFlags
  4375. cmGeneratorTarget::GetTargetSourceFileFlags(const cmSourceFile* sf) const
  4376. {
  4377. struct SourceFileFlags flags;
  4378. this->ConstructSourceFileFlags();
  4379. auto si = this->SourceFlagsMap.find(sf);
  4380. if (si != this->SourceFlagsMap.end()) {
  4381. flags = si->second;
  4382. } else {
  4383. // Handle the MACOSX_PACKAGE_LOCATION property on source files that
  4384. // were not listed in one of the other lists.
  4385. if (cmProp location = sf->GetProperty("MACOSX_PACKAGE_LOCATION")) {
  4386. flags.MacFolder = location->c_str();
  4387. const bool stripResources =
  4388. this->GlobalGenerator->ShouldStripResourcePath(this->Makefile);
  4389. if (*location == "Resources") {
  4390. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  4391. if (stripResources) {
  4392. flags.MacFolder = "";
  4393. }
  4394. } else if (cmHasLiteralPrefix(*location, "Resources/")) {
  4395. flags.Type = cmGeneratorTarget::SourceFileTypeDeepResource;
  4396. if (stripResources) {
  4397. flags.MacFolder += strlen("Resources/");
  4398. }
  4399. } else {
  4400. flags.Type = cmGeneratorTarget::SourceFileTypeMacContent;
  4401. }
  4402. }
  4403. }
  4404. return flags;
  4405. }
  4406. void cmGeneratorTarget::ConstructSourceFileFlags() const
  4407. {
  4408. if (this->SourceFileFlagsConstructed) {
  4409. return;
  4410. }
  4411. this->SourceFileFlagsConstructed = true;
  4412. // Process public headers to mark the source files.
  4413. if (cmProp files = this->GetProperty("PUBLIC_HEADER")) {
  4414. std::vector<std::string> relFiles = cmExpandedList(*files);
  4415. for (std::string const& relFile : relFiles) {
  4416. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  4417. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  4418. flags.MacFolder = "Headers";
  4419. flags.Type = cmGeneratorTarget::SourceFileTypePublicHeader;
  4420. }
  4421. }
  4422. }
  4423. // Process private headers after public headers so that they take
  4424. // precedence if a file is listed in both.
  4425. if (cmProp files = this->GetProperty("PRIVATE_HEADER")) {
  4426. std::vector<std::string> relFiles = cmExpandedList(*files);
  4427. for (std::string const& relFile : relFiles) {
  4428. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  4429. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  4430. flags.MacFolder = "PrivateHeaders";
  4431. flags.Type = cmGeneratorTarget::SourceFileTypePrivateHeader;
  4432. }
  4433. }
  4434. }
  4435. // Mark sources listed as resources.
  4436. if (cmProp files = this->GetProperty("RESOURCE")) {
  4437. std::vector<std::string> relFiles = cmExpandedList(*files);
  4438. for (std::string const& relFile : relFiles) {
  4439. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  4440. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  4441. flags.MacFolder = "";
  4442. if (!this->GlobalGenerator->ShouldStripResourcePath(this->Makefile)) {
  4443. flags.MacFolder = "Resources";
  4444. }
  4445. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  4446. }
  4447. }
  4448. }
  4449. }
  4450. const cmGeneratorTarget::CompatibleInterfacesBase&
  4451. cmGeneratorTarget::GetCompatibleInterfaces(std::string const& config) const
  4452. {
  4453. cmGeneratorTarget::CompatibleInterfaces& compat =
  4454. this->CompatibleInterfacesMap[config];
  4455. if (!compat.Done) {
  4456. compat.Done = true;
  4457. compat.PropsBool.insert("POSITION_INDEPENDENT_CODE");
  4458. compat.PropsString.insert("AUTOUIC_OPTIONS");
  4459. std::vector<cmGeneratorTarget const*> const& deps =
  4460. this->GetLinkImplementationClosure(config);
  4461. for (cmGeneratorTarget const* li : deps) {
  4462. #define CM_READ_COMPATIBLE_INTERFACE(X, x) \
  4463. if (cmProp prop = li->GetProperty("COMPATIBLE_INTERFACE_" #X)) { \
  4464. std::vector<std::string> props; \
  4465. cmExpandList(*prop, props); \
  4466. compat.Props##x.insert(props.begin(), props.end()); \
  4467. }
  4468. CM_READ_COMPATIBLE_INTERFACE(BOOL, Bool)
  4469. CM_READ_COMPATIBLE_INTERFACE(STRING, String)
  4470. CM_READ_COMPATIBLE_INTERFACE(NUMBER_MIN, NumberMin)
  4471. CM_READ_COMPATIBLE_INTERFACE(NUMBER_MAX, NumberMax)
  4472. #undef CM_READ_COMPATIBLE_INTERFACE
  4473. }
  4474. }
  4475. return compat;
  4476. }
  4477. bool cmGeneratorTarget::IsLinkInterfaceDependentBoolProperty(
  4478. const std::string& p, const std::string& config) const
  4479. {
  4480. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  4481. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  4482. return false;
  4483. }
  4484. return this->GetCompatibleInterfaces(config).PropsBool.count(p) > 0;
  4485. }
  4486. bool cmGeneratorTarget::IsLinkInterfaceDependentStringProperty(
  4487. const std::string& p, const std::string& config) const
  4488. {
  4489. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  4490. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  4491. return false;
  4492. }
  4493. return this->GetCompatibleInterfaces(config).PropsString.count(p) > 0;
  4494. }
  4495. bool cmGeneratorTarget::IsLinkInterfaceDependentNumberMinProperty(
  4496. const std::string& p, const std::string& config) const
  4497. {
  4498. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  4499. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  4500. return false;
  4501. }
  4502. return this->GetCompatibleInterfaces(config).PropsNumberMin.count(p) > 0;
  4503. }
  4504. bool cmGeneratorTarget::IsLinkInterfaceDependentNumberMaxProperty(
  4505. const std::string& p, const std::string& config) const
  4506. {
  4507. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  4508. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  4509. return false;
  4510. }
  4511. return this->GetCompatibleInterfaces(config).PropsNumberMax.count(p) > 0;
  4512. }
  4513. enum CompatibleType
  4514. {
  4515. BoolType,
  4516. StringType,
  4517. NumberMinType,
  4518. NumberMaxType
  4519. };
  4520. template <typename PropertyType>
  4521. PropertyType getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  4522. const std::string& prop,
  4523. const std::string& config,
  4524. CompatibleType, PropertyType*);
  4525. template <>
  4526. bool getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  4527. const std::string& prop,
  4528. const std::string& config,
  4529. CompatibleType /*unused*/,
  4530. bool* /*unused*/)
  4531. {
  4532. return tgt->GetLinkInterfaceDependentBoolProperty(prop, config);
  4533. }
  4534. template <>
  4535. const char* getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  4536. const std::string& prop,
  4537. const std::string& config,
  4538. CompatibleType t,
  4539. const char** /*unused*/)
  4540. {
  4541. switch (t) {
  4542. case BoolType:
  4543. assert(false &&
  4544. "String compatibility check function called for boolean");
  4545. return nullptr;
  4546. case StringType:
  4547. return tgt->GetLinkInterfaceDependentStringProperty(prop, config);
  4548. case NumberMinType:
  4549. return tgt->GetLinkInterfaceDependentNumberMinProperty(prop, config);
  4550. case NumberMaxType:
  4551. return tgt->GetLinkInterfaceDependentNumberMaxProperty(prop, config);
  4552. }
  4553. assert(false && "Unreachable!");
  4554. return nullptr;
  4555. }
  4556. template <typename PropertyType>
  4557. void checkPropertyConsistency(cmGeneratorTarget const* depender,
  4558. cmGeneratorTarget const* dependee,
  4559. const std::string& propName,
  4560. std::set<std::string>& emitted,
  4561. const std::string& config, CompatibleType t,
  4562. PropertyType* /*unused*/)
  4563. {
  4564. cmProp prop = dependee->GetProperty(propName);
  4565. if (!prop) {
  4566. return;
  4567. }
  4568. std::vector<std::string> props = cmExpandedList(*prop);
  4569. std::string pdir =
  4570. cmStrCat(cmSystemTools::GetCMakeRoot(), "/Help/prop_tgt/");
  4571. for (std::string const& p : props) {
  4572. std::string pname = cmSystemTools::HelpFileName(p);
  4573. std::string pfile = pdir + pname + ".rst";
  4574. if (cmSystemTools::FileExists(pfile, true)) {
  4575. std::ostringstream e;
  4576. e << "Target \"" << dependee->GetName() << "\" has property \"" << p
  4577. << "\" listed in its " << propName
  4578. << " property. "
  4579. "This is not allowed. Only user-defined properties may appear "
  4580. "listed in the "
  4581. << propName << " property.";
  4582. depender->GetLocalGenerator()->IssueMessage(MessageType::FATAL_ERROR,
  4583. e.str());
  4584. return;
  4585. }
  4586. if (emitted.insert(p).second) {
  4587. getLinkInterfaceDependentProperty<PropertyType>(depender, p, config, t,
  4588. nullptr);
  4589. if (cmSystemTools::GetErrorOccuredFlag()) {
  4590. return;
  4591. }
  4592. }
  4593. }
  4594. }
  4595. namespace {
  4596. std::string intersect(const std::set<std::string>& s1,
  4597. const std::set<std::string>& s2)
  4598. {
  4599. std::set<std::string> intersect;
  4600. std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(),
  4601. std::inserter(intersect, intersect.begin()));
  4602. if (!intersect.empty()) {
  4603. return *intersect.begin();
  4604. }
  4605. return "";
  4606. }
  4607. std::string intersect(const std::set<std::string>& s1,
  4608. const std::set<std::string>& s2,
  4609. const std::set<std::string>& s3)
  4610. {
  4611. std::string result;
  4612. result = intersect(s1, s2);
  4613. if (!result.empty()) {
  4614. return result;
  4615. }
  4616. result = intersect(s1, s3);
  4617. if (!result.empty()) {
  4618. return result;
  4619. }
  4620. return intersect(s2, s3);
  4621. }
  4622. std::string intersect(const std::set<std::string>& s1,
  4623. const std::set<std::string>& s2,
  4624. const std::set<std::string>& s3,
  4625. const std::set<std::string>& s4)
  4626. {
  4627. std::string result;
  4628. result = intersect(s1, s2);
  4629. if (!result.empty()) {
  4630. return result;
  4631. }
  4632. result = intersect(s1, s3);
  4633. if (!result.empty()) {
  4634. return result;
  4635. }
  4636. result = intersect(s1, s4);
  4637. if (!result.empty()) {
  4638. return result;
  4639. }
  4640. return intersect(s2, s3, s4);
  4641. }
  4642. }
  4643. void cmGeneratorTarget::CheckPropertyCompatibility(
  4644. cmComputeLinkInformation& info, const std::string& config) const
  4645. {
  4646. const cmComputeLinkInformation::ItemVector& deps = info.GetItems();
  4647. std::set<std::string> emittedBools;
  4648. static const std::string strBool = "COMPATIBLE_INTERFACE_BOOL";
  4649. std::set<std::string> emittedStrings;
  4650. static const std::string strString = "COMPATIBLE_INTERFACE_STRING";
  4651. std::set<std::string> emittedMinNumbers;
  4652. static const std::string strNumMin = "COMPATIBLE_INTERFACE_NUMBER_MIN";
  4653. std::set<std::string> emittedMaxNumbers;
  4654. static const std::string strNumMax = "COMPATIBLE_INTERFACE_NUMBER_MAX";
  4655. for (auto const& dep : deps) {
  4656. if (!dep.Target) {
  4657. continue;
  4658. }
  4659. checkPropertyConsistency<bool>(this, dep.Target, strBool, emittedBools,
  4660. config, BoolType, nullptr);
  4661. if (cmSystemTools::GetErrorOccuredFlag()) {
  4662. return;
  4663. }
  4664. checkPropertyConsistency<const char*>(this, dep.Target, strString,
  4665. emittedStrings, config, StringType,
  4666. nullptr);
  4667. if (cmSystemTools::GetErrorOccuredFlag()) {
  4668. return;
  4669. }
  4670. checkPropertyConsistency<const char*>(this, dep.Target, strNumMin,
  4671. emittedMinNumbers, config,
  4672. NumberMinType, nullptr);
  4673. if (cmSystemTools::GetErrorOccuredFlag()) {
  4674. return;
  4675. }
  4676. checkPropertyConsistency<const char*>(this, dep.Target, strNumMax,
  4677. emittedMaxNumbers, config,
  4678. NumberMaxType, nullptr);
  4679. if (cmSystemTools::GetErrorOccuredFlag()) {
  4680. return;
  4681. }
  4682. }
  4683. std::string prop = intersect(emittedBools, emittedStrings, emittedMinNumbers,
  4684. emittedMaxNumbers);
  4685. if (!prop.empty()) {
  4686. // Use a sorted std::vector to keep the error message sorted.
  4687. std::vector<std::string> props;
  4688. auto i = emittedBools.find(prop);
  4689. if (i != emittedBools.end()) {
  4690. props.push_back(strBool);
  4691. }
  4692. i = emittedStrings.find(prop);
  4693. if (i != emittedStrings.end()) {
  4694. props.push_back(strString);
  4695. }
  4696. i = emittedMinNumbers.find(prop);
  4697. if (i != emittedMinNumbers.end()) {
  4698. props.push_back(strNumMin);
  4699. }
  4700. i = emittedMaxNumbers.find(prop);
  4701. if (i != emittedMaxNumbers.end()) {
  4702. props.push_back(strNumMax);
  4703. }
  4704. std::sort(props.begin(), props.end());
  4705. std::string propsString = cmStrCat(
  4706. cmJoin(cmMakeRange(props).retreat(1), ", "), " and the ", props.back());
  4707. std::ostringstream e;
  4708. e << "Property \"" << prop << "\" appears in both the " << propsString
  4709. << " property in the dependencies of target \"" << this->GetName()
  4710. << "\". This is not allowed. A property may only require "
  4711. "compatibility "
  4712. "in a boolean interpretation, a numeric minimum, a numeric maximum "
  4713. "or a "
  4714. "string interpretation, but not a mixture.";
  4715. this->LocalGenerator->IssueMessage(MessageType::FATAL_ERROR, e.str());
  4716. }
  4717. }
  4718. template <typename PropertyType>
  4719. std::string valueAsString(PropertyType);
  4720. template <>
  4721. std::string valueAsString<bool>(bool value)
  4722. {
  4723. return value ? "TRUE" : "FALSE";
  4724. }
  4725. template <>
  4726. std::string valueAsString<const char*>(const char* value)
  4727. {
  4728. return value ? value : "(unset)";
  4729. }
  4730. template <>
  4731. std::string valueAsString<std::string>(std::string value)
  4732. {
  4733. return value;
  4734. }
  4735. template <>
  4736. std::string valueAsString<std::nullptr_t>(std::nullptr_t /*unused*/)
  4737. {
  4738. return "(unset)";
  4739. }
  4740. std::string compatibilityType(CompatibleType t)
  4741. {
  4742. switch (t) {
  4743. case BoolType:
  4744. return "Boolean compatibility";
  4745. case StringType:
  4746. return "String compatibility";
  4747. case NumberMaxType:
  4748. return "Numeric maximum compatibility";
  4749. case NumberMinType:
  4750. return "Numeric minimum compatibility";
  4751. }
  4752. assert(false && "Unreachable!");
  4753. return "";
  4754. }
  4755. std::string compatibilityAgree(CompatibleType t, bool dominant)
  4756. {
  4757. switch (t) {
  4758. case BoolType:
  4759. case StringType:
  4760. return dominant ? "(Disagree)\n" : "(Agree)\n";
  4761. case NumberMaxType:
  4762. case NumberMinType:
  4763. return dominant ? "(Dominant)\n" : "(Ignored)\n";
  4764. }
  4765. assert(false && "Unreachable!");
  4766. return "";
  4767. }
  4768. template <typename PropertyType>
  4769. PropertyType getTypedProperty(
  4770. cmGeneratorTarget const* tgt, const std::string& prop,
  4771. cmGeneratorExpressionInterpreter* genexInterpreter = nullptr);
  4772. template <>
  4773. bool getTypedProperty<bool>(cmGeneratorTarget const* tgt,
  4774. const std::string& prop,
  4775. cmGeneratorExpressionInterpreter* genexInterpreter)
  4776. {
  4777. if (genexInterpreter == nullptr) {
  4778. return tgt->GetPropertyAsBool(prop);
  4779. }
  4780. cmProp value = tgt->GetProperty(prop);
  4781. return cmIsOn(genexInterpreter->Evaluate(value ? *value : "", prop));
  4782. }
  4783. template <>
  4784. const char* getTypedProperty<const char*>(
  4785. cmGeneratorTarget const* tgt, const std::string& prop,
  4786. cmGeneratorExpressionInterpreter* genexInterpreter)
  4787. {
  4788. cmProp value = tgt->GetProperty(prop);
  4789. if (genexInterpreter == nullptr) {
  4790. return value ? value->c_str() : nullptr;
  4791. }
  4792. return genexInterpreter->Evaluate(value ? *value : "", prop).c_str();
  4793. }
  4794. template <>
  4795. std::string getTypedProperty<std::string>(
  4796. cmGeneratorTarget const* tgt, const std::string& prop,
  4797. cmGeneratorExpressionInterpreter* genexInterpreter)
  4798. {
  4799. cmProp value = tgt->GetProperty(prop);
  4800. if (genexInterpreter == nullptr) {
  4801. return valueAsString(value ? value->c_str() : nullptr);
  4802. }
  4803. return genexInterpreter->Evaluate(value ? *value : "", prop);
  4804. }
  4805. template <typename PropertyType>
  4806. PropertyType impliedValue(PropertyType);
  4807. template <>
  4808. bool impliedValue<bool>(bool /*unused*/)
  4809. {
  4810. return false;
  4811. }
  4812. template <>
  4813. const char* impliedValue<const char*>(const char* /*unused*/)
  4814. {
  4815. return "";
  4816. }
  4817. template <>
  4818. std::string impliedValue<std::string>(std::string /*unused*/) // NOLINT(*)
  4819. {
  4820. return std::string();
  4821. }
  4822. template <typename PropertyType>
  4823. std::pair<bool, PropertyType> consistentProperty(PropertyType lhs,
  4824. PropertyType rhs,
  4825. CompatibleType t);
  4826. template <>
  4827. std::pair<bool, bool> consistentProperty(bool lhs, bool rhs,
  4828. CompatibleType /*unused*/)
  4829. {
  4830. return { lhs == rhs, lhs };
  4831. }
  4832. std::pair<bool, const char*> consistentStringProperty(const char* lhs,
  4833. const char* rhs)
  4834. {
  4835. const bool b = strcmp(lhs, rhs) == 0;
  4836. return { b, b ? lhs : nullptr };
  4837. }
  4838. std::pair<bool, std::string> consistentStringProperty(const std::string& lhs,
  4839. const std::string& rhs)
  4840. {
  4841. const bool b = lhs == rhs;
  4842. return { b, b ? lhs : valueAsString(nullptr) };
  4843. }
  4844. std::pair<bool, const char*> consistentNumberProperty(const char* lhs,
  4845. const char* rhs,
  4846. CompatibleType t)
  4847. {
  4848. char* pEnd;
  4849. long lnum = strtol(lhs, &pEnd, 0);
  4850. if (pEnd == lhs || *pEnd != '\0' || errno == ERANGE) {
  4851. return { false, nullptr };
  4852. }
  4853. long rnum = strtol(rhs, &pEnd, 0);
  4854. if (pEnd == rhs || *pEnd != '\0' || errno == ERANGE) {
  4855. return { false, nullptr };
  4856. }
  4857. if (t == NumberMaxType) {
  4858. return { true, std::max(lnum, rnum) == lnum ? lhs : rhs };
  4859. }
  4860. return { true, std::min(lnum, rnum) == lnum ? lhs : rhs };
  4861. }
  4862. template <>
  4863. std::pair<bool, const char*> consistentProperty(const char* lhs,
  4864. const char* rhs,
  4865. CompatibleType t)
  4866. {
  4867. if (!lhs && !rhs) {
  4868. return { true, lhs };
  4869. }
  4870. if (!lhs) {
  4871. return { true, rhs };
  4872. }
  4873. if (!rhs) {
  4874. return { true, lhs };
  4875. }
  4876. switch (t) {
  4877. case BoolType: {
  4878. bool same = cmIsOn(lhs) == cmIsOn(rhs);
  4879. return { same, same ? lhs : nullptr };
  4880. }
  4881. case StringType:
  4882. return consistentStringProperty(lhs, rhs);
  4883. case NumberMinType:
  4884. case NumberMaxType:
  4885. return consistentNumberProperty(lhs, rhs, t);
  4886. }
  4887. assert(false && "Unreachable!");
  4888. return { false, nullptr };
  4889. }
  4890. std::pair<bool, std::string> consistentProperty(const std::string& lhs,
  4891. const std::string& rhs,
  4892. CompatibleType t)
  4893. {
  4894. const std::string null_ptr = valueAsString(nullptr);
  4895. if (lhs == null_ptr && rhs == null_ptr) {
  4896. return { true, lhs };
  4897. }
  4898. if (lhs == null_ptr) {
  4899. return { true, rhs };
  4900. }
  4901. if (rhs == null_ptr) {
  4902. return { true, lhs };
  4903. }
  4904. switch (t) {
  4905. case BoolType: {
  4906. bool same = cmIsOn(lhs) == cmIsOn(rhs);
  4907. return { same, same ? lhs : null_ptr };
  4908. }
  4909. case StringType:
  4910. return consistentStringProperty(lhs, rhs);
  4911. case NumberMinType:
  4912. case NumberMaxType: {
  4913. auto value = consistentNumberProperty(lhs.c_str(), rhs.c_str(), t);
  4914. return { value.first,
  4915. value.first ? std::string(value.second) : null_ptr };
  4916. }
  4917. }
  4918. assert(false && "Unreachable!");
  4919. return { false, null_ptr };
  4920. }
  4921. template <typename PropertyType>
  4922. PropertyType checkInterfacePropertyCompatibility(cmGeneratorTarget const* tgt,
  4923. const std::string& p,
  4924. const std::string& config,
  4925. const char* defaultValue,
  4926. CompatibleType t,
  4927. PropertyType* /*unused*/)
  4928. {
  4929. PropertyType propContent = getTypedProperty<PropertyType>(tgt, p);
  4930. std::vector<std::string> headPropKeys = tgt->GetPropertyKeys();
  4931. const bool explicitlySet = cm::contains(headPropKeys, p);
  4932. const bool impliedByUse = tgt->IsNullImpliedByLinkLibraries(p);
  4933. assert((impliedByUse ^ explicitlySet) || (!impliedByUse && !explicitlySet));
  4934. std::vector<cmGeneratorTarget const*> const& deps =
  4935. tgt->GetLinkImplementationClosure(config);
  4936. if (deps.empty()) {
  4937. return propContent;
  4938. }
  4939. bool propInitialized = explicitlySet;
  4940. std::string report = cmStrCat(" * Target \"", tgt->GetName());
  4941. if (explicitlySet) {
  4942. report += "\" has property content \"";
  4943. report += valueAsString<PropertyType>(propContent);
  4944. report += "\"\n";
  4945. } else if (impliedByUse) {
  4946. report += "\" property is implied by use.\n";
  4947. } else {
  4948. report += "\" property not set.\n";
  4949. }
  4950. std::string interfaceProperty = "INTERFACE_" + p;
  4951. std::unique_ptr<cmGeneratorExpressionInterpreter> genexInterpreter;
  4952. if (p == "POSITION_INDEPENDENT_CODE") {
  4953. genexInterpreter = cm::make_unique<cmGeneratorExpressionInterpreter>(
  4954. tgt->GetLocalGenerator(), config, tgt);
  4955. }
  4956. for (cmGeneratorTarget const* theTarget : deps) {
  4957. // An error should be reported if one dependency
  4958. // has INTERFACE_POSITION_INDEPENDENT_CODE ON and the other
  4959. // has INTERFACE_POSITION_INDEPENDENT_CODE OFF, or if the
  4960. // target itself has a POSITION_INDEPENDENT_CODE which disagrees
  4961. // with a dependency.
  4962. std::vector<std::string> propKeys = theTarget->GetPropertyKeys();
  4963. const bool ifaceIsSet = cm::contains(propKeys, interfaceProperty);
  4964. PropertyType ifacePropContent = getTypedProperty<PropertyType>(
  4965. theTarget, interfaceProperty, genexInterpreter.get());
  4966. std::string reportEntry;
  4967. if (ifaceIsSet) {
  4968. reportEntry += " * Target \"";
  4969. reportEntry += theTarget->GetName();
  4970. reportEntry += "\" property value \"";
  4971. reportEntry += valueAsString<PropertyType>(ifacePropContent);
  4972. reportEntry += "\" ";
  4973. }
  4974. if (explicitlySet) {
  4975. if (ifaceIsSet) {
  4976. std::pair<bool, PropertyType> consistent =
  4977. consistentProperty(propContent, ifacePropContent, t);
  4978. report += reportEntry;
  4979. report += compatibilityAgree(t, propContent != consistent.second);
  4980. if (!consistent.first) {
  4981. std::ostringstream e;
  4982. e << "Property " << p << " on target \"" << tgt->GetName()
  4983. << "\" does\nnot match the "
  4984. "INTERFACE_"
  4985. << p
  4986. << " property requirement\nof "
  4987. "dependency \""
  4988. << theTarget->GetName() << "\".\n";
  4989. cmSystemTools::Error(e.str());
  4990. break;
  4991. }
  4992. propContent = consistent.second;
  4993. continue;
  4994. }
  4995. // Explicitly set on target and not set in iface. Can't disagree.
  4996. continue;
  4997. }
  4998. if (impliedByUse) {
  4999. propContent = impliedValue<PropertyType>(propContent);
  5000. if (ifaceIsSet) {
  5001. std::pair<bool, PropertyType> consistent =
  5002. consistentProperty(propContent, ifacePropContent, t);
  5003. report += reportEntry;
  5004. report += compatibilityAgree(t, propContent != consistent.second);
  5005. if (!consistent.first) {
  5006. std::ostringstream e;
  5007. e << "Property " << p << " on target \"" << tgt->GetName()
  5008. << "\" is\nimplied to be " << defaultValue
  5009. << " because it was used to determine the link libraries\n"
  5010. "already. The INTERFACE_"
  5011. << p << " property on\ndependency \"" << theTarget->GetName()
  5012. << "\" is in conflict.\n";
  5013. cmSystemTools::Error(e.str());
  5014. break;
  5015. }
  5016. propContent = consistent.second;
  5017. continue;
  5018. }
  5019. // Implicitly set on target and not set in iface. Can't disagree.
  5020. continue;
  5021. }
  5022. if (ifaceIsSet) {
  5023. if (propInitialized) {
  5024. std::pair<bool, PropertyType> consistent =
  5025. consistentProperty(propContent, ifacePropContent, t);
  5026. report += reportEntry;
  5027. report += compatibilityAgree(t, propContent != consistent.second);
  5028. if (!consistent.first) {
  5029. std::ostringstream e;
  5030. e << "The INTERFACE_" << p << " property of \""
  5031. << theTarget->GetName() << "\" does\nnot agree with the value of "
  5032. << p << " already determined\nfor \"" << tgt->GetName() << "\".\n";
  5033. cmSystemTools::Error(e.str());
  5034. break;
  5035. }
  5036. propContent = consistent.second;
  5037. continue;
  5038. }
  5039. report += reportEntry + "(Interface set)\n";
  5040. propContent = ifacePropContent;
  5041. propInitialized = true;
  5042. } else {
  5043. // Not set. Nothing to agree on.
  5044. continue;
  5045. }
  5046. }
  5047. tgt->ReportPropertyOrigin(p, valueAsString<PropertyType>(propContent),
  5048. report, compatibilityType(t));
  5049. return propContent;
  5050. }
  5051. bool cmGeneratorTarget::SetDeviceLink(bool deviceLink)
  5052. {
  5053. bool previous = this->DeviceLink;
  5054. this->DeviceLink = deviceLink;
  5055. return previous;
  5056. }
  5057. bool cmGeneratorTarget::GetLinkInterfaceDependentBoolProperty(
  5058. const std::string& p, const std::string& config) const
  5059. {
  5060. return checkInterfacePropertyCompatibility<bool>(this, p, config, "FALSE",
  5061. BoolType, nullptr);
  5062. }
  5063. std::string cmGeneratorTarget::GetLinkInterfaceDependentStringAsBoolProperty(
  5064. const std::string& p, const std::string& config) const
  5065. {
  5066. return checkInterfacePropertyCompatibility<std::string>(
  5067. this, p, config, "FALSE", BoolType, nullptr);
  5068. }
  5069. const char* cmGeneratorTarget::GetLinkInterfaceDependentStringProperty(
  5070. const std::string& p, const std::string& config) const
  5071. {
  5072. return checkInterfacePropertyCompatibility<const char*>(
  5073. this, p, config, "empty", StringType, nullptr);
  5074. }
  5075. const char* cmGeneratorTarget::GetLinkInterfaceDependentNumberMinProperty(
  5076. const std::string& p, const std::string& config) const
  5077. {
  5078. return checkInterfacePropertyCompatibility<const char*>(
  5079. this, p, config, "empty", NumberMinType, nullptr);
  5080. }
  5081. const char* cmGeneratorTarget::GetLinkInterfaceDependentNumberMaxProperty(
  5082. const std::string& p, const std::string& config) const
  5083. {
  5084. return checkInterfacePropertyCompatibility<const char*>(
  5085. this, p, config, "empty", NumberMaxType, nullptr);
  5086. }
  5087. cmComputeLinkInformation* cmGeneratorTarget::GetLinkInformation(
  5088. const std::string& config) const
  5089. {
  5090. // Lookup any existing information for this configuration.
  5091. std::string key(cmSystemTools::UpperCase(config));
  5092. auto i = this->LinkInformation.find(key);
  5093. if (i == this->LinkInformation.end()) {
  5094. // Compute information for this configuration.
  5095. auto info = cm::make_unique<cmComputeLinkInformation>(this, config);
  5096. if (info && !info->Compute()) {
  5097. info.reset();
  5098. }
  5099. // Store the information for this configuration.
  5100. i = this->LinkInformation.emplace(key, std::move(info)).first;
  5101. if (i->second) {
  5102. this->CheckPropertyCompatibility(*i->second, config);
  5103. }
  5104. }
  5105. return i->second.get();
  5106. }
  5107. void cmGeneratorTarget::GetTargetVersion(int& major, int& minor) const
  5108. {
  5109. int patch;
  5110. this->GetTargetVersion("VERSION", major, minor, patch);
  5111. }
  5112. void cmGeneratorTarget::GetTargetVersionFallback(
  5113. const std::string& property, const std::string& fallback_property,
  5114. int& major, int& minor, int& patch) const
  5115. {
  5116. if (this->GetProperty(property)) {
  5117. this->GetTargetVersion(property, major, minor, patch);
  5118. } else {
  5119. this->GetTargetVersion(fallback_property, major, minor, patch);
  5120. }
  5121. }
  5122. void cmGeneratorTarget::GetTargetVersion(const std::string& property,
  5123. int& major, int& minor,
  5124. int& patch) const
  5125. {
  5126. // Set the default values.
  5127. major = 0;
  5128. minor = 0;
  5129. patch = 0;
  5130. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  5131. if (cmProp version = this->GetProperty(property)) {
  5132. // Try to parse the version number and store the results that were
  5133. // successfully parsed.
  5134. int parsed_major;
  5135. int parsed_minor;
  5136. int parsed_patch;
  5137. switch (sscanf(version->c_str(), "%d.%d.%d", &parsed_major, &parsed_minor,
  5138. &parsed_patch)) {
  5139. case 3:
  5140. patch = parsed_patch;
  5141. CM_FALLTHROUGH;
  5142. case 2:
  5143. minor = parsed_minor;
  5144. CM_FALLTHROUGH;
  5145. case 1:
  5146. major = parsed_major;
  5147. CM_FALLTHROUGH;
  5148. default:
  5149. break;
  5150. }
  5151. }
  5152. }
  5153. std::string cmGeneratorTarget::GetRuntimeLinkLibrary(
  5154. std::string const& lang, std::string const& config) const
  5155. {
  5156. // This is activated by the presence of a default selection whether or
  5157. // not it is overridden by a property.
  5158. cmProp runtimeLibraryDefault = this->Makefile->GetDef(
  5159. cmStrCat("CMAKE_", lang, "_RUNTIME_LIBRARY_DEFAULT"));
  5160. if (!cmNonempty(runtimeLibraryDefault)) {
  5161. return std::string();
  5162. }
  5163. cmProp runtimeLibraryValue =
  5164. this->Target->GetProperty(cmStrCat(lang, "_RUNTIME_LIBRARY"));
  5165. if (!runtimeLibraryValue) {
  5166. runtimeLibraryValue = runtimeLibraryDefault;
  5167. }
  5168. return cmSystemTools::UpperCase(cmGeneratorExpression::Evaluate(
  5169. *runtimeLibraryValue, this->LocalGenerator, config, this));
  5170. }
  5171. std::string cmGeneratorTarget::GetFortranModuleDirectory(
  5172. std::string const& working_dir) const
  5173. {
  5174. if (!this->FortranModuleDirectoryCreated) {
  5175. this->FortranModuleDirectory =
  5176. this->CreateFortranModuleDirectory(working_dir);
  5177. this->FortranModuleDirectoryCreated = true;
  5178. }
  5179. return this->FortranModuleDirectory;
  5180. }
  5181. std::string cmGeneratorTarget::CreateFortranModuleDirectory(
  5182. std::string const& working_dir) const
  5183. {
  5184. std::string mod_dir;
  5185. std::string target_mod_dir;
  5186. if (cmProp prop = this->GetProperty("Fortran_MODULE_DIRECTORY")) {
  5187. target_mod_dir = *prop;
  5188. } else {
  5189. std::string const& default_mod_dir =
  5190. this->LocalGenerator->GetCurrentBinaryDirectory();
  5191. if (default_mod_dir != working_dir) {
  5192. target_mod_dir = default_mod_dir;
  5193. }
  5194. }
  5195. const char* moddir_flag =
  5196. this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_FLAG");
  5197. if (!target_mod_dir.empty() && moddir_flag) {
  5198. // Compute the full path to the module directory.
  5199. if (cmSystemTools::FileIsFullPath(target_mod_dir)) {
  5200. // Already a full path.
  5201. mod_dir = target_mod_dir;
  5202. } else {
  5203. // Interpret relative to the current output directory.
  5204. mod_dir = cmStrCat(this->LocalGenerator->GetCurrentBinaryDirectory(),
  5205. '/', target_mod_dir);
  5206. }
  5207. // Make sure the module output directory exists.
  5208. cmSystemTools::MakeDirectory(mod_dir);
  5209. }
  5210. return mod_dir;
  5211. }
  5212. std::string cmGeneratorTarget::GetFrameworkVersion() const
  5213. {
  5214. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  5215. if (cmProp fversion = this->GetProperty("FRAMEWORK_VERSION")) {
  5216. return *fversion;
  5217. }
  5218. if (cmProp tversion = this->GetProperty("VERSION")) {
  5219. return *tversion;
  5220. }
  5221. return "A";
  5222. }
  5223. void cmGeneratorTarget::ComputeVersionedName(std::string& vName,
  5224. std::string const& prefix,
  5225. std::string const& base,
  5226. std::string const& suffix,
  5227. std::string const& name,
  5228. const char* version) const
  5229. {
  5230. vName = this->Makefile->IsOn("APPLE") ? (prefix + base) : name;
  5231. if (version) {
  5232. vName += ".";
  5233. vName += version;
  5234. }
  5235. vName += this->Makefile->IsOn("APPLE") ? suffix : std::string();
  5236. }
  5237. std::vector<std::string> cmGeneratorTarget::GetPropertyKeys() const
  5238. {
  5239. return this->Target->GetProperties().GetKeys();
  5240. }
  5241. void cmGeneratorTarget::ReportPropertyOrigin(
  5242. const std::string& p, const std::string& result, const std::string& report,
  5243. const std::string& compatibilityType) const
  5244. {
  5245. std::vector<std::string> debugProperties;
  5246. this->Target->GetMakefile()->GetDefExpandList(
  5247. "CMAKE_DEBUG_TARGET_PROPERTIES", debugProperties);
  5248. bool debugOrigin = !this->DebugCompatiblePropertiesDone[p] &&
  5249. cm::contains(debugProperties, p);
  5250. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  5251. this->DebugCompatiblePropertiesDone[p] = true;
  5252. }
  5253. if (!debugOrigin) {
  5254. return;
  5255. }
  5256. std::string areport =
  5257. cmStrCat(compatibilityType, " of property \"", p, "\" for target \"",
  5258. this->GetName(), "\" (result: \"", result, "\"):\n", report);
  5259. this->LocalGenerator->GetCMakeInstance()->IssueMessage(MessageType::LOG,
  5260. areport);
  5261. }
  5262. bool cmGeneratorTarget::IsLinkLookupScope(std::string const& n,
  5263. cmLocalGenerator const*& lg) const
  5264. {
  5265. if (cmHasLiteralPrefix(n, CMAKE_DIRECTORY_ID_SEP)) {
  5266. cmDirectoryId const dirId = n.substr(sizeof(CMAKE_DIRECTORY_ID_SEP) - 1);
  5267. if (dirId.String.empty()) {
  5268. lg = this->LocalGenerator;
  5269. return true;
  5270. }
  5271. if (cmLocalGenerator const* otherLG =
  5272. this->GlobalGenerator->FindLocalGenerator(dirId)) {
  5273. lg = otherLG;
  5274. return true;
  5275. }
  5276. }
  5277. return false;
  5278. }
  5279. void cmGeneratorTarget::LookupLinkItems(std::vector<std::string> const& names,
  5280. cmListFileBacktrace const& bt,
  5281. std::vector<cmLinkItem>& items) const
  5282. {
  5283. cmLocalGenerator const* lg = this->LocalGenerator;
  5284. for (std::string const& n : names) {
  5285. if (this->IsLinkLookupScope(n, lg)) {
  5286. continue;
  5287. }
  5288. std::string name = this->CheckCMP0004(n);
  5289. if (name == this->GetName() || name.empty()) {
  5290. continue;
  5291. }
  5292. items.push_back(this->ResolveLinkItem(name, bt, lg));
  5293. }
  5294. }
  5295. void cmGeneratorTarget::ExpandLinkItems(
  5296. std::string const& prop, std::string const& value, std::string const& config,
  5297. cmGeneratorTarget const* headTarget, bool usage_requirements_only,
  5298. std::vector<cmLinkItem>& items, bool& hadHeadSensitiveCondition,
  5299. bool& hadContextSensitiveCondition,
  5300. bool& hadLinkLanguageSensitiveCondition) const
  5301. {
  5302. // Keep this logic in sync with ComputeLinkImplementationLibraries.
  5303. cmGeneratorExpression ge;
  5304. cmGeneratorExpressionDAGChecker dagChecker(this, prop, nullptr, nullptr);
  5305. // The $<LINK_ONLY> expression may be in a link interface to specify
  5306. // private link dependencies that are otherwise excluded from usage
  5307. // requirements.
  5308. if (usage_requirements_only) {
  5309. dagChecker.SetTransitivePropertiesOnly();
  5310. }
  5311. std::vector<std::string> libs;
  5312. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(value);
  5313. cmExpandList(cge->Evaluate(this->LocalGenerator, config, headTarget,
  5314. &dagChecker, this, headTarget->LinkerLanguage),
  5315. libs);
  5316. this->LookupLinkItems(libs, cge->GetBacktrace(), items);
  5317. hadHeadSensitiveCondition = cge->GetHadHeadSensitiveCondition();
  5318. hadContextSensitiveCondition = cge->GetHadContextSensitiveCondition();
  5319. hadLinkLanguageSensitiveCondition =
  5320. cge->GetHadLinkLanguageSensitiveCondition();
  5321. }
  5322. cmLinkInterface const* cmGeneratorTarget::GetLinkInterface(
  5323. const std::string& config, cmGeneratorTarget const* head) const
  5324. {
  5325. return this->GetLinkInterface(config, head, false);
  5326. }
  5327. cmLinkInterface const* cmGeneratorTarget::GetLinkInterface(
  5328. const std::string& config, cmGeneratorTarget const* head,
  5329. bool secondPass) const
  5330. {
  5331. // Imported targets have their own link interface.
  5332. if (this->IsImported()) {
  5333. return this->GetImportLinkInterface(config, head, false, secondPass);
  5334. }
  5335. // Link interfaces are not supported for executables that do not
  5336. // export symbols.
  5337. if (this->GetType() == cmStateEnums::EXECUTABLE &&
  5338. !this->IsExecutableWithExports()) {
  5339. return nullptr;
  5340. }
  5341. // Lookup any existing link interface for this configuration.
  5342. cmHeadToLinkInterfaceMap& hm = this->GetHeadToLinkInterfaceMap(config);
  5343. if (secondPass) {
  5344. hm.erase(head);
  5345. }
  5346. // If the link interface does not depend on the head target
  5347. // then return the one we computed first.
  5348. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  5349. return &hm.begin()->second;
  5350. }
  5351. cmOptionalLinkInterface& iface = hm[head];
  5352. if (!iface.LibrariesDone) {
  5353. iface.LibrariesDone = true;
  5354. this->ComputeLinkInterfaceLibraries(config, iface, head, false);
  5355. }
  5356. if (!iface.AllDone) {
  5357. iface.AllDone = true;
  5358. if (iface.Exists) {
  5359. this->ComputeLinkInterface(config, iface, head, secondPass);
  5360. }
  5361. }
  5362. return iface.Exists ? &iface : nullptr;
  5363. }
  5364. void cmGeneratorTarget::ComputeLinkInterface(
  5365. const std::string& config, cmOptionalLinkInterface& iface,
  5366. cmGeneratorTarget const* headTarget) const
  5367. {
  5368. this->ComputeLinkInterface(config, iface, headTarget, false);
  5369. }
  5370. void cmGeneratorTarget::ComputeLinkInterface(
  5371. const std::string& config, cmOptionalLinkInterface& iface,
  5372. cmGeneratorTarget const* headTarget, bool secondPass) const
  5373. {
  5374. if (iface.Explicit) {
  5375. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  5376. this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  5377. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  5378. // Shared libraries may have runtime implementation dependencies
  5379. // on other shared libraries that are not in the interface.
  5380. std::set<cmLinkItem> emitted;
  5381. for (cmLinkItem const& lib : iface.Libraries) {
  5382. emitted.insert(lib);
  5383. }
  5384. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  5385. cmLinkImplementation const* impl =
  5386. this->GetLinkImplementation(config, secondPass);
  5387. for (cmLinkImplItem const& lib : impl->Libraries) {
  5388. if (emitted.insert(lib).second) {
  5389. if (lib.Target) {
  5390. // This is a runtime dependency on another shared library.
  5391. if (lib.Target->GetType() == cmStateEnums::SHARED_LIBRARY) {
  5392. iface.SharedDeps.push_back(lib);
  5393. }
  5394. } else {
  5395. // TODO: Recognize shared library file names. Perhaps this
  5396. // should be moved to cmComputeLinkInformation, but that
  5397. // creates a chicken-and-egg problem since this list is needed
  5398. // for its construction.
  5399. }
  5400. }
  5401. }
  5402. }
  5403. }
  5404. } else if (this->GetPolicyStatusCMP0022() == cmPolicies::WARN ||
  5405. this->GetPolicyStatusCMP0022() == cmPolicies::OLD) {
  5406. // The link implementation is the default link interface.
  5407. cmLinkImplementationLibraries const* impl =
  5408. this->GetLinkImplementationLibrariesInternal(config, headTarget);
  5409. iface.ImplementationIsInterface = true;
  5410. iface.WrongConfigLibraries = impl->WrongConfigLibraries;
  5411. }
  5412. if (this->LinkLanguagePropagatesToDependents()) {
  5413. // Targets using this archive need its language runtime libraries.
  5414. if (cmLinkImplementation const* impl =
  5415. this->GetLinkImplementation(config, secondPass)) {
  5416. iface.Languages = impl->Languages;
  5417. }
  5418. }
  5419. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  5420. // Construct the property name suffix for this configuration.
  5421. std::string suffix = "_";
  5422. if (!config.empty()) {
  5423. suffix += cmSystemTools::UpperCase(config);
  5424. } else {
  5425. suffix += "NOCONFIG";
  5426. }
  5427. // How many repetitions are needed if this library has cyclic
  5428. // dependencies?
  5429. std::string propName = cmStrCat("LINK_INTERFACE_MULTIPLICITY", suffix);
  5430. if (cmProp config_reps = this->GetProperty(propName)) {
  5431. sscanf(config_reps->c_str(), "%u", &iface.Multiplicity);
  5432. } else if (cmProp reps =
  5433. this->GetProperty("LINK_INTERFACE_MULTIPLICITY")) {
  5434. sscanf(reps->c_str(), "%u", &iface.Multiplicity);
  5435. }
  5436. }
  5437. }
  5438. const cmLinkInterfaceLibraries* cmGeneratorTarget::GetLinkInterfaceLibraries(
  5439. const std::string& config, cmGeneratorTarget const* head,
  5440. bool usage_requirements_only) const
  5441. {
  5442. // Imported targets have their own link interface.
  5443. if (this->IsImported()) {
  5444. return this->GetImportLinkInterface(config, head, usage_requirements_only);
  5445. }
  5446. // Link interfaces are not supported for executables that do not
  5447. // export symbols.
  5448. if (this->GetType() == cmStateEnums::EXECUTABLE &&
  5449. !this->IsExecutableWithExports()) {
  5450. return nullptr;
  5451. }
  5452. // Lookup any existing link interface for this configuration.
  5453. cmHeadToLinkInterfaceMap& hm =
  5454. (usage_requirements_only
  5455. ? this->GetHeadToLinkInterfaceUsageRequirementsMap(config)
  5456. : this->GetHeadToLinkInterfaceMap(config));
  5457. // If the link interface does not depend on the head target
  5458. // then return the one we computed first.
  5459. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  5460. return &hm.begin()->second;
  5461. }
  5462. cmOptionalLinkInterface& iface = hm[head];
  5463. if (!iface.LibrariesDone) {
  5464. iface.LibrariesDone = true;
  5465. this->ComputeLinkInterfaceLibraries(config, iface, head,
  5466. usage_requirements_only);
  5467. }
  5468. return iface.Exists ? &iface : nullptr;
  5469. }
  5470. std::string cmGeneratorTarget::GetDirectory(
  5471. const std::string& config, cmStateEnums::ArtifactType artifact) const
  5472. {
  5473. if (this->IsImported()) {
  5474. // Return the directory from which the target is imported.
  5475. return cmSystemTools::GetFilenamePath(
  5476. this->Target->ImportedGetFullPath(config, artifact));
  5477. }
  5478. if (OutputInfo const* info = this->GetOutputInfo(config)) {
  5479. // Return the directory in which the target will be built.
  5480. switch (artifact) {
  5481. case cmStateEnums::RuntimeBinaryArtifact:
  5482. return info->OutDir;
  5483. case cmStateEnums::ImportLibraryArtifact:
  5484. return info->ImpDir;
  5485. }
  5486. }
  5487. return "";
  5488. }
  5489. bool cmGeneratorTarget::UsesDefaultOutputDir(
  5490. const std::string& config, cmStateEnums::ArtifactType artifact) const
  5491. {
  5492. std::string dir;
  5493. return this->ComputeOutputDir(config, artifact, dir);
  5494. }
  5495. cmGeneratorTarget::OutputInfo const* cmGeneratorTarget::GetOutputInfo(
  5496. const std::string& config) const
  5497. {
  5498. // There is no output information for imported targets.
  5499. if (this->IsImported()) {
  5500. return nullptr;
  5501. }
  5502. // Only libraries and executables have well-defined output files.
  5503. if (!this->HaveWellDefinedOutputFiles()) {
  5504. std::string msg = cmStrCat("cmGeneratorTarget::GetOutputInfo called for ",
  5505. this->GetName(), " which has type ",
  5506. cmState::GetTargetTypeName(this->GetType()));
  5507. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  5508. return nullptr;
  5509. }
  5510. // Lookup/compute/cache the output information for this configuration.
  5511. std::string config_upper;
  5512. if (!config.empty()) {
  5513. config_upper = cmSystemTools::UpperCase(config);
  5514. }
  5515. auto i = this->OutputInfoMap.find(config_upper);
  5516. if (i == this->OutputInfoMap.end()) {
  5517. // Add empty info in map to detect potential recursion.
  5518. OutputInfo info;
  5519. OutputInfoMapType::value_type entry(config_upper, info);
  5520. i = this->OutputInfoMap.insert(entry).first;
  5521. // Compute output directories.
  5522. this->ComputeOutputDir(config, cmStateEnums::RuntimeBinaryArtifact,
  5523. info.OutDir);
  5524. this->ComputeOutputDir(config, cmStateEnums::ImportLibraryArtifact,
  5525. info.ImpDir);
  5526. if (!this->ComputePDBOutputDir("PDB", config, info.PdbDir)) {
  5527. info.PdbDir = info.OutDir;
  5528. }
  5529. // Now update the previously-prepared map entry.
  5530. i->second = info;
  5531. } else if (i->second.empty()) {
  5532. // An empty map entry indicates we have been called recursively
  5533. // from the above block.
  5534. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  5535. MessageType::FATAL_ERROR,
  5536. "Target '" + this->GetName() + "' OUTPUT_DIRECTORY depends on itself.",
  5537. this->GetBacktrace());
  5538. return nullptr;
  5539. }
  5540. return &i->second;
  5541. }
  5542. bool cmGeneratorTarget::ComputeOutputDir(const std::string& config,
  5543. cmStateEnums::ArtifactType artifact,
  5544. std::string& out) const
  5545. {
  5546. bool usesDefaultOutputDir = false;
  5547. std::string conf = config;
  5548. // Look for a target property defining the target output directory
  5549. // based on the target type.
  5550. std::string targetTypeName = this->GetOutputTargetType(artifact);
  5551. const char* propertyName = nullptr;
  5552. std::string propertyNameStr = targetTypeName;
  5553. if (!propertyNameStr.empty()) {
  5554. propertyNameStr += "_OUTPUT_DIRECTORY";
  5555. propertyName = propertyNameStr.c_str();
  5556. }
  5557. // Check for a per-configuration output directory target property.
  5558. std::string configUpper = cmSystemTools::UpperCase(conf);
  5559. const char* configProp = nullptr;
  5560. std::string configPropStr = targetTypeName;
  5561. if (!configPropStr.empty()) {
  5562. configPropStr += "_OUTPUT_DIRECTORY_";
  5563. configPropStr += configUpper;
  5564. configProp = configPropStr.c_str();
  5565. }
  5566. // Select an output directory.
  5567. if (cmProp config_outdir = this->GetProperty(configProp)) {
  5568. // Use the user-specified per-configuration output directory.
  5569. out = cmGeneratorExpression::Evaluate(*config_outdir, this->LocalGenerator,
  5570. config);
  5571. // Skip per-configuration subdirectory.
  5572. conf.clear();
  5573. } else if (cmProp outdir = this->GetProperty(propertyName)) {
  5574. // Use the user-specified output directory.
  5575. out =
  5576. cmGeneratorExpression::Evaluate(*outdir, this->LocalGenerator, config);
  5577. // Skip per-configuration subdirectory if the value contained a
  5578. // generator expression.
  5579. if (out != *outdir) {
  5580. conf.clear();
  5581. }
  5582. } else if (this->GetType() == cmStateEnums::EXECUTABLE) {
  5583. // Lookup the output path for executables.
  5584. out = this->Makefile->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  5585. } else if (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  5586. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  5587. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  5588. // Lookup the output path for libraries.
  5589. out = this->Makefile->GetSafeDefinition("LIBRARY_OUTPUT_PATH");
  5590. }
  5591. if (out.empty()) {
  5592. // Default to the current output directory.
  5593. usesDefaultOutputDir = true;
  5594. out = ".";
  5595. }
  5596. // Convert the output path to a full path in case it is
  5597. // specified as a relative path. Treat a relative path as
  5598. // relative to the current output directory for this makefile.
  5599. out = (cmSystemTools::CollapseFullPath(
  5600. out, this->LocalGenerator->GetCurrentBinaryDirectory()));
  5601. // The generator may add the configuration's subdirectory.
  5602. if (!conf.empty()) {
  5603. bool useEPN =
  5604. this->GlobalGenerator->UseEffectivePlatformName(this->Makefile);
  5605. std::string suffix =
  5606. usesDefaultOutputDir && useEPN ? "${EFFECTIVE_PLATFORM_NAME}" : "";
  5607. this->LocalGenerator->GetGlobalGenerator()->AppendDirectoryForConfig(
  5608. "/", conf, suffix, out);
  5609. }
  5610. return usesDefaultOutputDir;
  5611. }
  5612. bool cmGeneratorTarget::ComputePDBOutputDir(const std::string& kind,
  5613. const std::string& config,
  5614. std::string& out) const
  5615. {
  5616. // Look for a target property defining the target output directory
  5617. // based on the target type.
  5618. const char* propertyName = nullptr;
  5619. std::string propertyNameStr = kind;
  5620. if (!propertyNameStr.empty()) {
  5621. propertyNameStr += "_OUTPUT_DIRECTORY";
  5622. propertyName = propertyNameStr.c_str();
  5623. }
  5624. std::string conf = config;
  5625. // Check for a per-configuration output directory target property.
  5626. std::string configUpper = cmSystemTools::UpperCase(conf);
  5627. const char* configProp = nullptr;
  5628. std::string configPropStr = kind;
  5629. if (!configPropStr.empty()) {
  5630. configPropStr += "_OUTPUT_DIRECTORY_";
  5631. configPropStr += configUpper;
  5632. configProp = configPropStr.c_str();
  5633. }
  5634. // Select an output directory.
  5635. if (cmProp config_outdir = this->GetProperty(configProp)) {
  5636. // Use the user-specified per-configuration output directory.
  5637. out = cmGeneratorExpression::Evaluate(*config_outdir, this->LocalGenerator,
  5638. config);
  5639. // Skip per-configuration subdirectory.
  5640. conf.clear();
  5641. } else if (cmProp outdir = this->GetProperty(propertyName)) {
  5642. // Use the user-specified output directory.
  5643. out =
  5644. cmGeneratorExpression::Evaluate(*outdir, this->LocalGenerator, config);
  5645. // Skip per-configuration subdirectory if the value contained a
  5646. // generator expression.
  5647. if (out != *outdir) {
  5648. conf.clear();
  5649. }
  5650. }
  5651. if (out.empty()) {
  5652. return false;
  5653. }
  5654. // Convert the output path to a full path in case it is
  5655. // specified as a relative path. Treat a relative path as
  5656. // relative to the current output directory for this makefile.
  5657. out = (cmSystemTools::CollapseFullPath(
  5658. out, this->LocalGenerator->GetCurrentBinaryDirectory()));
  5659. // The generator may add the configuration's subdirectory.
  5660. if (!conf.empty()) {
  5661. this->LocalGenerator->GetGlobalGenerator()->AppendDirectoryForConfig(
  5662. "/", conf, "", out);
  5663. }
  5664. return true;
  5665. }
  5666. bool cmGeneratorTarget::HaveInstallTreeRPATH(const std::string& config) const
  5667. {
  5668. std::string install_rpath;
  5669. this->GetInstallRPATH(config, install_rpath);
  5670. return !install_rpath.empty() &&
  5671. !this->Makefile->IsOn("CMAKE_SKIP_INSTALL_RPATH");
  5672. }
  5673. bool cmGeneratorTarget::GetBuildRPATH(const std::string& config,
  5674. std::string& rpath) const
  5675. {
  5676. return this->GetRPATH(config, "BUILD_RPATH", rpath);
  5677. }
  5678. bool cmGeneratorTarget::GetInstallRPATH(const std::string& config,
  5679. std::string& rpath) const
  5680. {
  5681. return this->GetRPATH(config, "INSTALL_RPATH", rpath);
  5682. }
  5683. bool cmGeneratorTarget::GetRPATH(const std::string& config,
  5684. const std::string& prop,
  5685. std::string& rpath) const
  5686. {
  5687. cmProp value = this->GetProperty(prop);
  5688. if (!value) {
  5689. return false;
  5690. }
  5691. rpath =
  5692. cmGeneratorExpression::Evaluate(*value, this->LocalGenerator, config);
  5693. return true;
  5694. }
  5695. void cmGeneratorTarget::ComputeLinkInterfaceLibraries(
  5696. const std::string& config, cmOptionalLinkInterface& iface,
  5697. cmGeneratorTarget const* headTarget, bool usage_requirements_only) const
  5698. {
  5699. // Construct the property name suffix for this configuration.
  5700. std::string suffix = "_";
  5701. if (!config.empty()) {
  5702. suffix += cmSystemTools::UpperCase(config);
  5703. } else {
  5704. suffix += "NOCONFIG";
  5705. }
  5706. // An explicit list of interface libraries may be set for shared
  5707. // libraries and executables that export symbols.
  5708. cmProp explicitLibraries = nullptr;
  5709. std::string linkIfaceProp;
  5710. bool const cmp0022NEW = (this->GetPolicyStatusCMP0022() != cmPolicies::OLD &&
  5711. this->GetPolicyStatusCMP0022() != cmPolicies::WARN);
  5712. if (cmp0022NEW) {
  5713. // CMP0022 NEW behavior is to use INTERFACE_LINK_LIBRARIES.
  5714. linkIfaceProp = "INTERFACE_LINK_LIBRARIES";
  5715. explicitLibraries = this->GetProperty(linkIfaceProp);
  5716. } else if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  5717. this->IsExecutableWithExports()) {
  5718. // CMP0022 OLD behavior is to use LINK_INTERFACE_LIBRARIES if set on a
  5719. // shared lib or executable.
  5720. // Lookup the per-configuration property.
  5721. linkIfaceProp = cmStrCat("LINK_INTERFACE_LIBRARIES", suffix);
  5722. explicitLibraries = this->GetProperty(linkIfaceProp);
  5723. // If not set, try the generic property.
  5724. if (!explicitLibraries) {
  5725. linkIfaceProp = "LINK_INTERFACE_LIBRARIES";
  5726. explicitLibraries = this->GetProperty(linkIfaceProp);
  5727. }
  5728. }
  5729. if (explicitLibraries &&
  5730. this->GetPolicyStatusCMP0022() == cmPolicies::WARN &&
  5731. !this->PolicyWarnedCMP0022) {
  5732. // Compare the explicitly set old link interface properties to the
  5733. // preferred new link interface property one and warn if different.
  5734. cmProp newExplicitLibraries =
  5735. this->GetProperty("INTERFACE_LINK_LIBRARIES");
  5736. if (newExplicitLibraries &&
  5737. (*newExplicitLibraries != *explicitLibraries)) {
  5738. std::ostringstream w;
  5739. /* clang-format off */
  5740. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0022) << "\n"
  5741. "Target \"" << this->GetName() << "\" has an "
  5742. "INTERFACE_LINK_LIBRARIES property which differs from its " <<
  5743. linkIfaceProp << " properties."
  5744. "\n"
  5745. "INTERFACE_LINK_LIBRARIES:\n"
  5746. " " << *newExplicitLibraries << "\n" <<
  5747. linkIfaceProp << ":\n"
  5748. " " << *explicitLibraries << "\n";
  5749. /* clang-format on */
  5750. this->LocalGenerator->IssueMessage(MessageType::AUTHOR_WARNING, w.str());
  5751. this->PolicyWarnedCMP0022 = true;
  5752. }
  5753. }
  5754. // There is no implicit link interface for executables or modules
  5755. // so if none was explicitly set then there is no link interface.
  5756. if (!explicitLibraries &&
  5757. (this->GetType() == cmStateEnums::EXECUTABLE ||
  5758. (this->GetType() == cmStateEnums::MODULE_LIBRARY))) {
  5759. return;
  5760. }
  5761. iface.Exists = true;
  5762. iface.Explicit = cmp0022NEW || explicitLibraries != nullptr;
  5763. if (explicitLibraries) {
  5764. // The interface libraries have been explicitly set.
  5765. this->ExpandLinkItems(linkIfaceProp, *explicitLibraries, config,
  5766. headTarget, usage_requirements_only, iface.Libraries,
  5767. iface.HadHeadSensitiveCondition,
  5768. iface.HadContextSensitiveCondition,
  5769. iface.HadLinkLanguageSensitiveCondition);
  5770. } else if (!cmp0022NEW)
  5771. // If CMP0022 is NEW then the plain tll signature sets the
  5772. // INTERFACE_LINK_LIBRARIES, so if we get here then the project
  5773. // cleared the property explicitly and we should not fall back
  5774. // to the link implementation.
  5775. {
  5776. // The link implementation is the default link interface.
  5777. cmLinkImplementationLibraries const* impl =
  5778. this->GetLinkImplementationLibrariesInternal(config, headTarget);
  5779. iface.Libraries.insert(iface.Libraries.end(), impl->Libraries.begin(),
  5780. impl->Libraries.end());
  5781. if (this->GetPolicyStatusCMP0022() == cmPolicies::WARN &&
  5782. !this->PolicyWarnedCMP0022 && !usage_requirements_only) {
  5783. // Compare the link implementation fallback link interface to the
  5784. // preferred new link interface property and warn if different.
  5785. std::vector<cmLinkItem> ifaceLibs;
  5786. static const std::string newProp = "INTERFACE_LINK_LIBRARIES";
  5787. if (cmProp newExplicitLibraries = this->GetProperty(newProp)) {
  5788. bool hadHeadSensitiveConditionDummy = false;
  5789. bool hadContextSensitiveConditionDummy = false;
  5790. bool hadLinkLanguageSensitiveConditionDummy = false;
  5791. this->ExpandLinkItems(newProp, *newExplicitLibraries, config,
  5792. headTarget, usage_requirements_only, ifaceLibs,
  5793. hadHeadSensitiveConditionDummy,
  5794. hadContextSensitiveConditionDummy,
  5795. hadLinkLanguageSensitiveConditionDummy);
  5796. }
  5797. if (ifaceLibs != iface.Libraries) {
  5798. std::string oldLibraries = cmJoin(impl->Libraries, ";");
  5799. std::string newLibraries = cmJoin(ifaceLibs, ";");
  5800. if (oldLibraries.empty()) {
  5801. oldLibraries = "(empty)";
  5802. }
  5803. if (newLibraries.empty()) {
  5804. newLibraries = "(empty)";
  5805. }
  5806. std::ostringstream w;
  5807. /* clang-format off */
  5808. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0022) << "\n"
  5809. "Target \"" << this->GetName() << "\" has an "
  5810. "INTERFACE_LINK_LIBRARIES property. "
  5811. "This should be preferred as the source of the link interface "
  5812. "for this library but because CMP0022 is not set CMake is "
  5813. "ignoring the property and using the link implementation "
  5814. "as the link interface instead."
  5815. "\n"
  5816. "INTERFACE_LINK_LIBRARIES:\n"
  5817. " " << newLibraries << "\n"
  5818. "Link implementation:\n"
  5819. " " << oldLibraries << "\n";
  5820. /* clang-format on */
  5821. this->LocalGenerator->IssueMessage(MessageType::AUTHOR_WARNING,
  5822. w.str());
  5823. this->PolicyWarnedCMP0022 = true;
  5824. }
  5825. }
  5826. }
  5827. }
  5828. const cmLinkInterface* cmGeneratorTarget::GetImportLinkInterface(
  5829. const std::string& config, cmGeneratorTarget const* headTarget,
  5830. bool usage_requirements_only, bool secondPass) const
  5831. {
  5832. cmGeneratorTarget::ImportInfo const* info = this->GetImportInfo(config);
  5833. if (!info) {
  5834. return nullptr;
  5835. }
  5836. cmHeadToLinkInterfaceMap& hm =
  5837. (usage_requirements_only
  5838. ? this->GetHeadToLinkInterfaceUsageRequirementsMap(config)
  5839. : this->GetHeadToLinkInterfaceMap(config));
  5840. if (secondPass) {
  5841. hm.erase(headTarget);
  5842. }
  5843. // If the link interface does not depend on the head target
  5844. // then return the one we computed first.
  5845. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  5846. return &hm.begin()->second;
  5847. }
  5848. cmOptionalLinkInterface& iface = hm[headTarget];
  5849. if (!iface.AllDone) {
  5850. iface.AllDone = true;
  5851. iface.Multiplicity = info->Multiplicity;
  5852. cmExpandList(info->Languages, iface.Languages);
  5853. this->ExpandLinkItems(info->LibrariesProp, info->Libraries, config,
  5854. headTarget, usage_requirements_only, iface.Libraries,
  5855. iface.HadHeadSensitiveCondition,
  5856. iface.HadContextSensitiveCondition,
  5857. iface.HadLinkLanguageSensitiveCondition);
  5858. std::vector<std::string> deps = cmExpandedList(info->SharedDeps);
  5859. this->LookupLinkItems(deps, cmListFileBacktrace(), iface.SharedDeps);
  5860. }
  5861. return &iface;
  5862. }
  5863. cmGeneratorTarget::ImportInfo const* cmGeneratorTarget::GetImportInfo(
  5864. const std::string& config) const
  5865. {
  5866. // There is no imported information for non-imported targets.
  5867. if (!this->IsImported()) {
  5868. return nullptr;
  5869. }
  5870. // Lookup/compute/cache the import information for this
  5871. // configuration.
  5872. std::string config_upper;
  5873. if (!config.empty()) {
  5874. config_upper = cmSystemTools::UpperCase(config);
  5875. } else {
  5876. config_upper = "NOCONFIG";
  5877. }
  5878. auto i = this->ImportInfoMap.find(config_upper);
  5879. if (i == this->ImportInfoMap.end()) {
  5880. ImportInfo info;
  5881. this->ComputeImportInfo(config_upper, info);
  5882. ImportInfoMapType::value_type entry(config_upper, info);
  5883. i = this->ImportInfoMap.insert(entry).first;
  5884. }
  5885. if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  5886. return &i->second;
  5887. }
  5888. // If the location is empty then the target is not available for
  5889. // this configuration.
  5890. if (i->second.Location.empty() && i->second.ImportLibrary.empty()) {
  5891. return nullptr;
  5892. }
  5893. // Return the import information.
  5894. return &i->second;
  5895. }
  5896. void cmGeneratorTarget::ComputeImportInfo(std::string const& desired_config,
  5897. ImportInfo& info) const
  5898. {
  5899. // This method finds information about an imported target from its
  5900. // properties. The "IMPORTED_" namespace is reserved for properties
  5901. // defined by the project exporting the target.
  5902. // Initialize members.
  5903. info.NoSOName = false;
  5904. cmProp loc = nullptr;
  5905. cmProp imp = nullptr;
  5906. std::string suffix;
  5907. if (!this->Target->GetMappedConfig(desired_config, loc, imp, suffix)) {
  5908. return;
  5909. }
  5910. // Get the link interface.
  5911. {
  5912. std::string linkProp = "INTERFACE_LINK_LIBRARIES";
  5913. cmProp propertyLibs = this->GetProperty(linkProp);
  5914. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  5915. if (!propertyLibs) {
  5916. linkProp = cmStrCat("IMPORTED_LINK_INTERFACE_LIBRARIES", suffix);
  5917. propertyLibs = this->GetProperty(linkProp);
  5918. }
  5919. if (!propertyLibs) {
  5920. linkProp = "IMPORTED_LINK_INTERFACE_LIBRARIES";
  5921. propertyLibs = this->GetProperty(linkProp);
  5922. }
  5923. }
  5924. if (propertyLibs) {
  5925. info.LibrariesProp = linkProp;
  5926. info.Libraries = *propertyLibs;
  5927. }
  5928. }
  5929. if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  5930. if (loc) {
  5931. info.LibName = *loc;
  5932. }
  5933. return;
  5934. }
  5935. // A provided configuration has been chosen. Load the
  5936. // configuration's properties.
  5937. // Get the location.
  5938. if (loc) {
  5939. info.Location = *loc;
  5940. } else {
  5941. std::string impProp = cmStrCat("IMPORTED_LOCATION", suffix);
  5942. if (cmProp config_location = this->GetProperty(impProp)) {
  5943. info.Location = *config_location;
  5944. } else if (cmProp location = this->GetProperty("IMPORTED_LOCATION")) {
  5945. info.Location = *location;
  5946. }
  5947. }
  5948. // Get the soname.
  5949. if (this->GetType() == cmStateEnums::SHARED_LIBRARY) {
  5950. std::string soProp = cmStrCat("IMPORTED_SONAME", suffix);
  5951. if (cmProp config_soname = this->GetProperty(soProp)) {
  5952. info.SOName = *config_soname;
  5953. } else if (cmProp soname = this->GetProperty("IMPORTED_SONAME")) {
  5954. info.SOName = *soname;
  5955. }
  5956. }
  5957. // Get the "no-soname" mark.
  5958. if (this->GetType() == cmStateEnums::SHARED_LIBRARY) {
  5959. std::string soProp = cmStrCat("IMPORTED_NO_SONAME", suffix);
  5960. if (cmProp config_no_soname = this->GetProperty(soProp)) {
  5961. info.NoSOName = cmIsOn(*config_no_soname);
  5962. } else if (cmProp no_soname = this->GetProperty("IMPORTED_NO_SONAME")) {
  5963. info.NoSOName = cmIsOn(*no_soname);
  5964. }
  5965. }
  5966. // Get the import library.
  5967. if (imp) {
  5968. info.ImportLibrary = *imp;
  5969. } else if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  5970. this->IsExecutableWithExports()) {
  5971. std::string impProp = cmStrCat("IMPORTED_IMPLIB", suffix);
  5972. if (cmProp config_implib = this->GetProperty(impProp)) {
  5973. info.ImportLibrary = *config_implib;
  5974. } else if (cmProp implib = this->GetProperty("IMPORTED_IMPLIB")) {
  5975. info.ImportLibrary = *implib;
  5976. }
  5977. }
  5978. // Get the link dependencies.
  5979. {
  5980. std::string linkProp =
  5981. cmStrCat("IMPORTED_LINK_DEPENDENT_LIBRARIES", suffix);
  5982. if (cmProp config_libs = this->GetProperty(linkProp)) {
  5983. info.SharedDeps = *config_libs;
  5984. } else if (cmProp libs =
  5985. this->GetProperty("IMPORTED_LINK_DEPENDENT_LIBRARIES")) {
  5986. info.SharedDeps = *libs;
  5987. }
  5988. }
  5989. // Get the link languages.
  5990. if (this->LinkLanguagePropagatesToDependents()) {
  5991. std::string linkProp =
  5992. cmStrCat("IMPORTED_LINK_INTERFACE_LANGUAGES", suffix);
  5993. if (cmProp config_libs = this->GetProperty(linkProp)) {
  5994. info.Languages = *config_libs;
  5995. } else if (cmProp libs =
  5996. this->GetProperty("IMPORTED_LINK_INTERFACE_LANGUAGES")) {
  5997. info.Languages = *libs;
  5998. }
  5999. }
  6000. // Get information if target is managed assembly.
  6001. {
  6002. std::string linkProp = "IMPORTED_COMMON_LANGUAGE_RUNTIME";
  6003. if (cmProp pc = this->GetProperty(linkProp + suffix)) {
  6004. info.Managed = this->CheckManagedType(*pc);
  6005. } else if (cmProp p = this->GetProperty(linkProp)) {
  6006. info.Managed = this->CheckManagedType(*p);
  6007. }
  6008. }
  6009. // Get the cyclic repetition count.
  6010. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  6011. std::string linkProp =
  6012. cmStrCat("IMPORTED_LINK_INTERFACE_MULTIPLICITY", suffix);
  6013. if (cmProp config_reps = this->GetProperty(linkProp)) {
  6014. sscanf(config_reps->c_str(), "%u", &info.Multiplicity);
  6015. } else if (cmProp reps =
  6016. this->GetProperty("IMPORTED_LINK_INTERFACE_MULTIPLICITY")) {
  6017. sscanf(reps->c_str(), "%u", &info.Multiplicity);
  6018. }
  6019. }
  6020. }
  6021. cmHeadToLinkInterfaceMap& cmGeneratorTarget::GetHeadToLinkInterfaceMap(
  6022. const std::string& config) const
  6023. {
  6024. return this->LinkInterfaceMap[cmSystemTools::UpperCase(config)];
  6025. }
  6026. cmHeadToLinkInterfaceMap&
  6027. cmGeneratorTarget::GetHeadToLinkInterfaceUsageRequirementsMap(
  6028. const std::string& config) const
  6029. {
  6030. return this
  6031. ->LinkInterfaceUsageRequirementsOnlyMap[cmSystemTools::UpperCase(config)];
  6032. }
  6033. const cmLinkImplementation* cmGeneratorTarget::GetLinkImplementation(
  6034. const std::string& config) const
  6035. {
  6036. return this->GetLinkImplementation(config, false);
  6037. }
  6038. const cmLinkImplementation* cmGeneratorTarget::GetLinkImplementation(
  6039. const std::string& config, bool secondPass) const
  6040. {
  6041. // There is no link implementation for imported targets.
  6042. if (this->IsImported()) {
  6043. return nullptr;
  6044. }
  6045. cmOptionalLinkImplementation& impl =
  6046. this->LinkImplMap[cmSystemTools::UpperCase(config)][this];
  6047. if (secondPass) {
  6048. impl = cmOptionalLinkImplementation();
  6049. }
  6050. if (!impl.LibrariesDone) {
  6051. impl.LibrariesDone = true;
  6052. this->ComputeLinkImplementationLibraries(config, impl, this);
  6053. }
  6054. if (!impl.LanguagesDone) {
  6055. impl.LanguagesDone = true;
  6056. this->ComputeLinkImplementationLanguages(config, impl);
  6057. }
  6058. return &impl;
  6059. }
  6060. bool cmGeneratorTarget::GetConfigCommonSourceFiles(
  6061. std::vector<cmSourceFile*>& files) const
  6062. {
  6063. std::vector<std::string> const& configs =
  6064. this->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  6065. auto it = configs.begin();
  6066. const std::string& firstConfig = *it;
  6067. this->GetSourceFilesWithoutObjectLibraries(files, firstConfig);
  6068. for (; it != configs.end(); ++it) {
  6069. std::vector<cmSourceFile*> configFiles;
  6070. this->GetSourceFilesWithoutObjectLibraries(configFiles, *it);
  6071. if (configFiles != files) {
  6072. std::string firstConfigFiles;
  6073. const char* sep = "";
  6074. for (cmSourceFile* f : files) {
  6075. firstConfigFiles += sep;
  6076. firstConfigFiles += f->ResolveFullPath();
  6077. sep = "\n ";
  6078. }
  6079. std::string thisConfigFiles;
  6080. sep = "";
  6081. for (cmSourceFile* f : configFiles) {
  6082. thisConfigFiles += sep;
  6083. thisConfigFiles += f->ResolveFullPath();
  6084. sep = "\n ";
  6085. }
  6086. std::ostringstream e;
  6087. /* clang-format off */
  6088. e << "Target \"" << this->GetName()
  6089. << "\" has source files which vary by "
  6090. "configuration. This is not supported by the \""
  6091. << this->GlobalGenerator->GetName()
  6092. << "\" generator.\n"
  6093. "Config \"" << firstConfig << "\":\n"
  6094. " " << firstConfigFiles << "\n"
  6095. "Config \"" << *it << "\":\n"
  6096. " " << thisConfigFiles << "\n";
  6097. /* clang-format on */
  6098. this->LocalGenerator->IssueMessage(MessageType::FATAL_ERROR, e.str());
  6099. return false;
  6100. }
  6101. }
  6102. return true;
  6103. }
  6104. void cmGeneratorTarget::GetObjectLibrariesCMP0026(
  6105. std::vector<cmGeneratorTarget*>& objlibs) const
  6106. {
  6107. // At configure-time, this method can be called as part of getting the
  6108. // LOCATION property or to export() a file to be include()d. However
  6109. // there is no cmGeneratorTarget at configure-time, so search the SOURCES
  6110. // for TARGET_OBJECTS instead for backwards compatibility with OLD
  6111. // behavior of CMP0024 and CMP0026 only.
  6112. cmStringRange rng = this->Target->GetSourceEntries();
  6113. for (std::string const& entry : rng) {
  6114. std::vector<std::string> files = cmExpandedList(entry);
  6115. for (std::string const& li : files) {
  6116. if (cmHasLiteralPrefix(li, "$<TARGET_OBJECTS:") && li.back() == '>') {
  6117. std::string objLibName = li.substr(17, li.size() - 18);
  6118. if (cmGeneratorExpression::Find(objLibName) != std::string::npos) {
  6119. continue;
  6120. }
  6121. cmGeneratorTarget* objLib =
  6122. this->LocalGenerator->FindGeneratorTargetToUse(objLibName);
  6123. if (objLib) {
  6124. objlibs.push_back(objLib);
  6125. }
  6126. }
  6127. }
  6128. }
  6129. }
  6130. std::string cmGeneratorTarget::CheckCMP0004(std::string const& item) const
  6131. {
  6132. // Strip whitespace off the library names because we used to do this
  6133. // in case variables were expanded at generate time. We no longer
  6134. // do the expansion but users link to libraries like " ${VAR} ".
  6135. std::string lib = item;
  6136. std::string::size_type pos = lib.find_first_not_of(" \t\r\n");
  6137. if (pos != std::string::npos) {
  6138. lib = lib.substr(pos);
  6139. }
  6140. pos = lib.find_last_not_of(" \t\r\n");
  6141. if (pos != std::string::npos) {
  6142. lib = lib.substr(0, pos + 1);
  6143. }
  6144. if (lib != item) {
  6145. cmake* cm = this->LocalGenerator->GetCMakeInstance();
  6146. switch (this->GetPolicyStatusCMP0004()) {
  6147. case cmPolicies::WARN: {
  6148. std::ostringstream w;
  6149. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0004) << "\n"
  6150. << "Target \"" << this->GetName() << "\" links to item \"" << item
  6151. << "\" which has leading or trailing whitespace.";
  6152. cm->IssueMessage(MessageType::AUTHOR_WARNING, w.str(),
  6153. this->GetBacktrace());
  6154. }
  6155. case cmPolicies::OLD:
  6156. break;
  6157. case cmPolicies::NEW: {
  6158. std::ostringstream e;
  6159. e << "Target \"" << this->GetName() << "\" links to item \"" << item
  6160. << "\" which has leading or trailing whitespace. "
  6161. << "This is now an error according to policy CMP0004.";
  6162. cm->IssueMessage(MessageType::FATAL_ERROR, e.str(),
  6163. this->GetBacktrace());
  6164. } break;
  6165. case cmPolicies::REQUIRED_IF_USED:
  6166. case cmPolicies::REQUIRED_ALWAYS: {
  6167. std::ostringstream e;
  6168. e << cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0004) << "\n"
  6169. << "Target \"" << this->GetName() << "\" links to item \"" << item
  6170. << "\" which has leading or trailing whitespace.";
  6171. cm->IssueMessage(MessageType::FATAL_ERROR, e.str(),
  6172. this->GetBacktrace());
  6173. } break;
  6174. }
  6175. }
  6176. return lib;
  6177. }
  6178. bool cmGeneratorTarget::IsDeprecated() const
  6179. {
  6180. cmProp deprecation = this->GetProperty("DEPRECATION");
  6181. return cmNonempty(deprecation);
  6182. }
  6183. std::string cmGeneratorTarget::GetDeprecation() const
  6184. {
  6185. // find DEPRECATION property
  6186. if (cmProp deprecation = this->GetProperty("DEPRECATION")) {
  6187. return *deprecation;
  6188. }
  6189. return std::string();
  6190. }
  6191. void cmGeneratorTarget::GetLanguages(std::set<std::string>& languages,
  6192. const std::string& config) const
  6193. {
  6194. std::vector<cmSourceFile*> sourceFiles;
  6195. this->GetSourceFiles(sourceFiles, config);
  6196. for (cmSourceFile* src : sourceFiles) {
  6197. const std::string& lang = src->GetOrDetermineLanguage();
  6198. if (!lang.empty()) {
  6199. languages.insert(lang);
  6200. }
  6201. }
  6202. std::vector<cmGeneratorTarget*> objectLibraries;
  6203. std::vector<cmSourceFile const*> externalObjects;
  6204. if (!this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  6205. std::vector<cmGeneratorTarget*> objectTargets;
  6206. this->GetObjectLibrariesCMP0026(objectTargets);
  6207. objectLibraries.reserve(objectTargets.size());
  6208. for (cmGeneratorTarget* gt : objectTargets) {
  6209. objectLibraries.push_back(gt);
  6210. }
  6211. } else {
  6212. this->GetExternalObjects(externalObjects, config);
  6213. for (cmSourceFile const* extObj : externalObjects) {
  6214. std::string objLib = extObj->GetObjectLibrary();
  6215. if (cmGeneratorTarget* tgt =
  6216. this->LocalGenerator->FindGeneratorTargetToUse(objLib)) {
  6217. auto const objLibIt =
  6218. std::find_if(objectLibraries.cbegin(), objectLibraries.cend(),
  6219. [tgt](cmGeneratorTarget* t) { return t == tgt; });
  6220. if (objectLibraries.cend() == objLibIt) {
  6221. objectLibraries.push_back(tgt);
  6222. }
  6223. }
  6224. }
  6225. }
  6226. for (cmGeneratorTarget* objLib : objectLibraries) {
  6227. objLib->GetLanguages(languages, config);
  6228. }
  6229. }
  6230. bool cmGeneratorTarget::IsCSharpOnly() const
  6231. {
  6232. // Only certain target types may compile CSharp.
  6233. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  6234. this->GetType() != cmStateEnums::STATIC_LIBRARY &&
  6235. this->GetType() != cmStateEnums::EXECUTABLE) {
  6236. return false;
  6237. }
  6238. std::set<std::string> languages = this->GetAllConfigCompileLanguages();
  6239. // Consider an explicit linker language property, but *not* the
  6240. // computed linker language that may depend on linked targets.
  6241. cmProp linkLang = this->GetProperty("LINKER_LANGUAGE");
  6242. if (cmNonempty(linkLang)) {
  6243. languages.insert(*linkLang);
  6244. }
  6245. return languages.size() == 1 && languages.count("CSharp") > 0;
  6246. }
  6247. void cmGeneratorTarget::ComputeLinkImplementationLanguages(
  6248. const std::string& config, cmOptionalLinkImplementation& impl) const
  6249. {
  6250. // This target needs runtime libraries for its source languages.
  6251. std::set<std::string> languages;
  6252. // Get languages used in our source files.
  6253. this->GetLanguages(languages, config);
  6254. // Copy the set of languages to the link implementation.
  6255. impl.Languages.insert(impl.Languages.begin(), languages.begin(),
  6256. languages.end());
  6257. }
  6258. bool cmGeneratorTarget::HaveBuildTreeRPATH(const std::string& config) const
  6259. {
  6260. if (this->GetPropertyAsBool("SKIP_BUILD_RPATH")) {
  6261. return false;
  6262. }
  6263. std::string build_rpath;
  6264. if (this->GetBuildRPATH(config, build_rpath)) {
  6265. return true;
  6266. }
  6267. if (cmLinkImplementationLibraries const* impl =
  6268. this->GetLinkImplementationLibraries(config)) {
  6269. return !impl->Libraries.empty();
  6270. }
  6271. return false;
  6272. }
  6273. cmLinkImplementationLibraries const*
  6274. cmGeneratorTarget::GetLinkImplementationLibraries(
  6275. const std::string& config) const
  6276. {
  6277. return this->GetLinkImplementationLibrariesInternal(config, this);
  6278. }
  6279. cmLinkImplementationLibraries const*
  6280. cmGeneratorTarget::GetLinkImplementationLibrariesInternal(
  6281. const std::string& config, cmGeneratorTarget const* head) const
  6282. {
  6283. // There is no link implementation for imported targets.
  6284. if (this->IsImported()) {
  6285. return nullptr;
  6286. }
  6287. // Populate the link implementation libraries for this configuration.
  6288. HeadToLinkImplementationMap& hm =
  6289. this->LinkImplMap[cmSystemTools::UpperCase(config)];
  6290. // If the link implementation does not depend on the head target
  6291. // then return the one we computed first.
  6292. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  6293. return &hm.begin()->second;
  6294. }
  6295. cmOptionalLinkImplementation& impl = hm[head];
  6296. if (!impl.LibrariesDone) {
  6297. impl.LibrariesDone = true;
  6298. this->ComputeLinkImplementationLibraries(config, impl, head);
  6299. }
  6300. return &impl;
  6301. }
  6302. bool cmGeneratorTarget::IsNullImpliedByLinkLibraries(
  6303. const std::string& p) const
  6304. {
  6305. return cm::contains(this->LinkImplicitNullProperties, p);
  6306. }
  6307. void cmGeneratorTarget::ComputeLinkImplementationLibraries(
  6308. const std::string& config, cmOptionalLinkImplementation& impl,
  6309. cmGeneratorTarget const* head) const
  6310. {
  6311. cmLocalGenerator const* lg = this->LocalGenerator;
  6312. cmStringRange entryRange = this->Target->GetLinkImplementationEntries();
  6313. cmBacktraceRange btRange = this->Target->GetLinkImplementationBacktraces();
  6314. cmBacktraceRange::const_iterator btIt = btRange.begin();
  6315. // Collect libraries directly linked in this configuration.
  6316. for (cmStringRange::const_iterator le = entryRange.begin(),
  6317. end = entryRange.end();
  6318. le != end; ++le, ++btIt) {
  6319. std::vector<std::string> llibs;
  6320. // Keep this logic in sync with ExpandLinkItems.
  6321. cmGeneratorExpressionDAGChecker dagChecker(this, "LINK_LIBRARIES", nullptr,
  6322. nullptr);
  6323. cmGeneratorExpression ge(*btIt);
  6324. std::unique_ptr<cmCompiledGeneratorExpression> const cge = ge.Parse(*le);
  6325. std::string const& evaluated =
  6326. cge->Evaluate(this->LocalGenerator, config, head, &dagChecker, nullptr,
  6327. this->LinkerLanguage);
  6328. cmExpandList(evaluated, llibs);
  6329. if (cge->GetHadHeadSensitiveCondition()) {
  6330. impl.HadHeadSensitiveCondition = true;
  6331. }
  6332. if (cge->GetHadContextSensitiveCondition()) {
  6333. impl.HadContextSensitiveCondition = true;
  6334. }
  6335. if (cge->GetHadLinkLanguageSensitiveCondition()) {
  6336. impl.HadLinkLanguageSensitiveCondition = true;
  6337. }
  6338. for (std::string const& lib : llibs) {
  6339. if (this->IsLinkLookupScope(lib, lg)) {
  6340. continue;
  6341. }
  6342. // Skip entries that resolve to the target itself or are empty.
  6343. std::string name = this->CheckCMP0004(lib);
  6344. if (this->GetPolicyStatusCMP0108() == cmPolicies::NEW) {
  6345. // resolve alias name
  6346. auto target = this->Makefile->FindTargetToUse(name);
  6347. if (target) {
  6348. name = target->GetName();
  6349. }
  6350. }
  6351. if (name == this->GetName() || name.empty()) {
  6352. if (name == this->GetName()) {
  6353. bool noMessage = false;
  6354. MessageType messageType = MessageType::FATAL_ERROR;
  6355. std::ostringstream e;
  6356. switch (this->GetPolicyStatusCMP0038()) {
  6357. case cmPolicies::WARN: {
  6358. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0038) << "\n";
  6359. messageType = MessageType::AUTHOR_WARNING;
  6360. } break;
  6361. case cmPolicies::OLD:
  6362. noMessage = true;
  6363. case cmPolicies::REQUIRED_IF_USED:
  6364. case cmPolicies::REQUIRED_ALWAYS:
  6365. case cmPolicies::NEW:
  6366. // Issue the fatal message.
  6367. break;
  6368. }
  6369. if (!noMessage) {
  6370. e << "Target \"" << this->GetName() << "\" links to itself.";
  6371. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  6372. messageType, e.str(), this->GetBacktrace());
  6373. if (messageType == MessageType::FATAL_ERROR) {
  6374. return;
  6375. }
  6376. }
  6377. }
  6378. continue;
  6379. }
  6380. // The entry is meant for this configuration.
  6381. impl.Libraries.emplace_back(this->ResolveLinkItem(name, *btIt, lg),
  6382. evaluated != *le);
  6383. }
  6384. std::set<std::string> const& seenProps = cge->GetSeenTargetProperties();
  6385. for (std::string const& sp : seenProps) {
  6386. if (!this->GetProperty(sp)) {
  6387. this->LinkImplicitNullProperties.insert(sp);
  6388. }
  6389. }
  6390. cge->GetMaxLanguageStandard(this, this->MaxLanguageStandards);
  6391. }
  6392. // Get the list of configurations considered to be DEBUG.
  6393. std::vector<std::string> debugConfigs =
  6394. this->Makefile->GetCMakeInstance()->GetDebugConfigs();
  6395. cmTargetLinkLibraryType linkType =
  6396. CMP0003_ComputeLinkType(config, debugConfigs);
  6397. cmTarget::LinkLibraryVectorType const& oldllibs =
  6398. this->Target->GetOriginalLinkLibraries();
  6399. for (cmTarget::LibraryID const& oldllib : oldllibs) {
  6400. if (oldllib.second != GENERAL_LibraryType && oldllib.second != linkType) {
  6401. std::string name = this->CheckCMP0004(oldllib.first);
  6402. if (name == this->GetName() || name.empty()) {
  6403. continue;
  6404. }
  6405. // Support OLD behavior for CMP0003.
  6406. impl.WrongConfigLibraries.push_back(
  6407. this->ResolveLinkItem(name, cmListFileBacktrace()));
  6408. }
  6409. }
  6410. }
  6411. cmGeneratorTarget::TargetOrString cmGeneratorTarget::ResolveTargetReference(
  6412. std::string const& name) const
  6413. {
  6414. return this->ResolveTargetReference(name, this->LocalGenerator);
  6415. }
  6416. cmGeneratorTarget::TargetOrString cmGeneratorTarget::ResolveTargetReference(
  6417. std::string const& name, cmLocalGenerator const* lg) const
  6418. {
  6419. TargetOrString resolved;
  6420. if (cmGeneratorTarget* tgt = lg->FindGeneratorTargetToUse(name)) {
  6421. resolved.Target = tgt;
  6422. } else {
  6423. resolved.String = name;
  6424. }
  6425. return resolved;
  6426. }
  6427. cmLinkItem cmGeneratorTarget::ResolveLinkItem(
  6428. std::string const& name, cmListFileBacktrace const& bt) const
  6429. {
  6430. return this->ResolveLinkItem(name, bt, this->LocalGenerator);
  6431. }
  6432. cmLinkItem cmGeneratorTarget::ResolveLinkItem(std::string const& name,
  6433. cmListFileBacktrace const& bt,
  6434. cmLocalGenerator const* lg) const
  6435. {
  6436. TargetOrString resolved = this->ResolveTargetReference(name, lg);
  6437. if (!resolved.Target) {
  6438. return cmLinkItem(resolved.String, false, bt);
  6439. }
  6440. // Check deprecation, issue message with `bt` backtrace.
  6441. if (resolved.Target->IsDeprecated()) {
  6442. std::ostringstream w;
  6443. /* clang-format off */
  6444. w <<
  6445. "The library that is being linked to, " << resolved.Target->GetName() <<
  6446. ", is marked as being deprecated by the owner. The message provided by "
  6447. "the developer is: \n" << resolved.Target->GetDeprecation() << "\n";
  6448. /* clang-format on */
  6449. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  6450. MessageType::AUTHOR_WARNING, w.str(), bt);
  6451. }
  6452. // Skip targets that will not really be linked. This is probably a
  6453. // name conflict between an external library and an executable
  6454. // within the project.
  6455. if (resolved.Target->GetType() == cmStateEnums::EXECUTABLE &&
  6456. !resolved.Target->IsExecutableWithExports()) {
  6457. return cmLinkItem(resolved.Target->GetName(), false, bt);
  6458. }
  6459. return cmLinkItem(resolved.Target, false, bt);
  6460. }
  6461. std::string cmGeneratorTarget::GetPDBDirectory(const std::string& config) const
  6462. {
  6463. if (OutputInfo const* info = this->GetOutputInfo(config)) {
  6464. // Return the directory in which the target will be built.
  6465. return info->PdbDir;
  6466. }
  6467. return "";
  6468. }
  6469. bool cmGeneratorTarget::HasImplibGNUtoMS(std::string const& config) const
  6470. {
  6471. return this->HasImportLibrary(config) && this->GetPropertyAsBool("GNUtoMS");
  6472. }
  6473. bool cmGeneratorTarget::GetImplibGNUtoMS(std::string const& config,
  6474. std::string const& gnuName,
  6475. std::string& out,
  6476. const char* newExt) const
  6477. {
  6478. if (this->HasImplibGNUtoMS(config) && gnuName.size() > 6 &&
  6479. gnuName.substr(gnuName.size() - 6) == ".dll.a") {
  6480. out = cmStrCat(cm::string_view(gnuName).substr(0, gnuName.size() - 6),
  6481. newExt ? newExt : ".lib");
  6482. return true;
  6483. }
  6484. return false;
  6485. }
  6486. bool cmGeneratorTarget::IsExecutableWithExports() const
  6487. {
  6488. return (this->GetType() == cmStateEnums::EXECUTABLE &&
  6489. this->GetPropertyAsBool("ENABLE_EXPORTS"));
  6490. }
  6491. bool cmGeneratorTarget::HasImportLibrary(std::string const& config) const
  6492. {
  6493. return (this->IsDLLPlatform() &&
  6494. (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  6495. this->IsExecutableWithExports()) &&
  6496. // Assemblies which have only managed code do not have
  6497. // import libraries.
  6498. this->GetManagedType(config) != ManagedType::Managed) ||
  6499. (this->Target->IsAIX() && this->IsExecutableWithExports());
  6500. }
  6501. bool cmGeneratorTarget::NeedImportLibraryName(std::string const& config) const
  6502. {
  6503. return this->HasImportLibrary(config) ||
  6504. // On DLL platforms we always generate the import library name
  6505. // just in case the sources have export markup.
  6506. (this->IsDLLPlatform() &&
  6507. (this->GetType() == cmStateEnums::EXECUTABLE ||
  6508. this->GetType() == cmStateEnums::MODULE_LIBRARY));
  6509. }
  6510. std::string cmGeneratorTarget::GetSupportDirectory() const
  6511. {
  6512. std::string dir = cmStrCat(this->LocalGenerator->GetCurrentBinaryDirectory(),
  6513. "/CMakeFiles/", this->GetName());
  6514. #if defined(__VMS)
  6515. dir += "_dir";
  6516. #else
  6517. dir += ".dir";
  6518. #endif
  6519. return dir;
  6520. }
  6521. bool cmGeneratorTarget::IsLinkable() const
  6522. {
  6523. return (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  6524. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  6525. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  6526. this->GetType() == cmStateEnums::UNKNOWN_LIBRARY ||
  6527. this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  6528. this->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  6529. this->IsExecutableWithExports());
  6530. }
  6531. bool cmGeneratorTarget::IsFrameworkOnApple() const
  6532. {
  6533. return ((this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  6534. this->GetType() == cmStateEnums::STATIC_LIBRARY) &&
  6535. this->Makefile->IsOn("APPLE") &&
  6536. this->GetPropertyAsBool("FRAMEWORK"));
  6537. }
  6538. bool cmGeneratorTarget::IsAppBundleOnApple() const
  6539. {
  6540. return (this->GetType() == cmStateEnums::EXECUTABLE &&
  6541. this->Makefile->IsOn("APPLE") &&
  6542. this->GetPropertyAsBool("MACOSX_BUNDLE"));
  6543. }
  6544. bool cmGeneratorTarget::IsXCTestOnApple() const
  6545. {
  6546. return (this->IsCFBundleOnApple() && this->GetPropertyAsBool("XCTEST"));
  6547. }
  6548. bool cmGeneratorTarget::IsCFBundleOnApple() const
  6549. {
  6550. return (this->GetType() == cmStateEnums::MODULE_LIBRARY &&
  6551. this->Makefile->IsOn("APPLE") && this->GetPropertyAsBool("BUNDLE"));
  6552. }
  6553. cmGeneratorTarget::ManagedType cmGeneratorTarget::CheckManagedType(
  6554. std::string const& propval) const
  6555. {
  6556. // The type of the managed assembly (mixed unmanaged C++ and C++/CLI,
  6557. // or only C++/CLI) does only depend on whether the property is an empty
  6558. // string or contains any value at all. In Visual Studio generators
  6559. // this propval is prepended with /clr[:] which results in:
  6560. //
  6561. // 1. propval does not exist: no /clr flag, unmanaged target, has import
  6562. // lib
  6563. // 2. empty propval: add /clr as flag, mixed unmanaged/managed
  6564. // target, has import lib
  6565. // 3. any value (safe,pure): add /clr:[propval] as flag, target with
  6566. // managed code only, no import lib
  6567. return propval.empty() ? ManagedType::Mixed : ManagedType::Managed;
  6568. }
  6569. cmGeneratorTarget::ManagedType cmGeneratorTarget::GetManagedType(
  6570. const std::string& config) const
  6571. {
  6572. // Only libraries and executables can be managed targets.
  6573. if (this->GetType() > cmStateEnums::SHARED_LIBRARY) {
  6574. return ManagedType::Undefined;
  6575. }
  6576. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  6577. return ManagedType::Native;
  6578. }
  6579. // Check imported target.
  6580. if (this->IsImported()) {
  6581. if (cmGeneratorTarget::ImportInfo const* info =
  6582. this->GetImportInfo(config)) {
  6583. return info->Managed;
  6584. }
  6585. return ManagedType::Undefined;
  6586. }
  6587. // Check for explicitly set clr target property.
  6588. if (cmProp clr = this->GetProperty("COMMON_LANGUAGE_RUNTIME")) {
  6589. return this->CheckManagedType(*clr);
  6590. }
  6591. // C# targets are always managed. This language specific check
  6592. // is added to avoid that the COMMON_LANGUAGE_RUNTIME target property
  6593. // has to be set manually for C# targets.
  6594. return this->IsCSharpOnly() ? ManagedType::Managed : ManagedType::Native;
  6595. }