cmGeneratorTarget.cxx 248 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434
  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 (exportName && !exportName->empty()) {
  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 = this->Makefile->GetGeneratorConfigs();
  706. std::set<std::string> configSet(configs.begin(), configs.end());
  707. if (configSet == this->VisitedConfigsForObjects) {
  708. return;
  709. }
  710. for (std::string const& c : configs) {
  711. std::vector<cmSourceFile const*> sourceFiles;
  712. this->GetObjectSources(sourceFiles, c);
  713. }
  714. }
  715. const char* cmGeneratorTarget::GetFeature(const std::string& feature,
  716. const std::string& config) const
  717. {
  718. if (!config.empty()) {
  719. std::string featureConfig =
  720. cmStrCat(feature, '_', cmSystemTools::UpperCase(config));
  721. if (cmProp value = this->GetProperty(featureConfig)) {
  722. return value->c_str();
  723. }
  724. }
  725. if (cmProp value = this->GetProperty(feature)) {
  726. return value->c_str();
  727. }
  728. return this->LocalGenerator->GetFeature(feature, config);
  729. }
  730. const char* cmGeneratorTarget::GetLinkPIEProperty(
  731. const std::string& config) const
  732. {
  733. static std::string PICValue;
  734. PICValue = this->GetLinkInterfaceDependentStringAsBoolProperty(
  735. "POSITION_INDEPENDENT_CODE", config);
  736. if (PICValue == "(unset)") {
  737. // POSITION_INDEPENDENT_CODE is not set
  738. return nullptr;
  739. }
  740. auto status = this->GetPolicyStatusCMP0083();
  741. return (status != cmPolicies::WARN && status != cmPolicies::OLD)
  742. ? PICValue.c_str()
  743. : nullptr;
  744. }
  745. bool cmGeneratorTarget::IsIPOEnabled(std::string const& lang,
  746. std::string const& config) const
  747. {
  748. const char* feature = "INTERPROCEDURAL_OPTIMIZATION";
  749. const bool result = cmIsOn(this->GetFeature(feature, config));
  750. if (!result) {
  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. BT<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. BT<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 p && 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 = p && !p->empty();
  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 (cm::contains(dependency->GetAllConfigCompileLanguages(),
  1199. "Swift")) {
  1200. EvaluatedTargetPropertyEntry entry{ library, library.Backtrace };
  1201. if (cmProp val = dependency->GetProperty("Swift_MODULE_DIRECTORY")) {
  1202. entry.Values.emplace_back(*val);
  1203. } else {
  1204. entry.Values.emplace_back(
  1205. dependency->GetLocalGenerator()->GetCurrentBinaryDirectory());
  1206. }
  1207. cmExpandList(AddSwiftInterfaceIncludeDirectories(target, dependency,
  1208. config, &dag),
  1209. entry.Values);
  1210. entries.Entries.emplace_back(std::move(entry));
  1211. }
  1212. }
  1213. }
  1214. }
  1215. }
  1216. void AddInterfaceEntries(cmGeneratorTarget const* headTarget,
  1217. std::string const& config, std::string const& prop,
  1218. std::string const& lang,
  1219. cmGeneratorExpressionDAGChecker* dagChecker,
  1220. EvaluatedTargetPropertyEntries& entries,
  1221. bool usage_requirements_only = true)
  1222. {
  1223. if (cmLinkImplementationLibraries const* impl =
  1224. headTarget->GetLinkImplementationLibraries(config)) {
  1225. entries.HadContextSensitiveCondition = impl->HadContextSensitiveCondition;
  1226. for (cmLinkImplItem const& lib : impl->Libraries) {
  1227. if (lib.Target) {
  1228. EvaluatedTargetPropertyEntry ee(lib, lib.Backtrace);
  1229. // Pretend $<TARGET_PROPERTY:lib.Target,prop> appeared in our
  1230. // caller's property and hand-evaluate it as if it were compiled.
  1231. // Create a context as cmCompiledGeneratorExpression::Evaluate does.
  1232. cmGeneratorExpressionContext context(
  1233. headTarget->GetLocalGenerator(), config, false, headTarget,
  1234. headTarget, true, lib.Backtrace, lang);
  1235. cmExpandList(lib.Target->EvaluateInterfaceProperty(
  1236. prop, &context, dagChecker, usage_requirements_only),
  1237. ee.Values);
  1238. ee.ContextDependent = context.HadContextSensitiveCondition;
  1239. entries.Entries.emplace_back(std::move(ee));
  1240. }
  1241. }
  1242. }
  1243. }
  1244. void AddObjectEntries(cmGeneratorTarget const* headTarget,
  1245. std::string const& config,
  1246. cmGeneratorExpressionDAGChecker* dagChecker,
  1247. EvaluatedTargetPropertyEntries& entries)
  1248. {
  1249. if (cmLinkImplementationLibraries const* impl =
  1250. headTarget->GetLinkImplementationLibraries(config)) {
  1251. entries.HadContextSensitiveCondition = impl->HadContextSensitiveCondition;
  1252. for (cmLinkImplItem const& lib : impl->Libraries) {
  1253. if (lib.Target &&
  1254. lib.Target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  1255. std::string uniqueName =
  1256. headTarget->GetGlobalGenerator()->IndexGeneratorTargetUniquely(
  1257. lib.Target);
  1258. std::string genex = "$<TARGET_OBJECTS:" + std::move(uniqueName) + ">";
  1259. cmGeneratorExpression ge(lib.Backtrace);
  1260. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(genex);
  1261. cge->SetEvaluateForBuildsystem(true);
  1262. EvaluatedTargetPropertyEntry ee(lib, lib.Backtrace);
  1263. cmExpandList(cge->Evaluate(headTarget->GetLocalGenerator(), config,
  1264. headTarget, dagChecker),
  1265. ee.Values);
  1266. if (cge->GetHadContextSensitiveCondition()) {
  1267. ee.ContextDependent = true;
  1268. }
  1269. entries.Entries.emplace_back(std::move(ee));
  1270. }
  1271. }
  1272. }
  1273. }
  1274. bool processSources(cmGeneratorTarget const* tgt,
  1275. EvaluatedTargetPropertyEntries& entries,
  1276. std::vector<BT<std::string>>& srcs,
  1277. std::unordered_set<std::string>& uniqueSrcs,
  1278. bool debugSources)
  1279. {
  1280. cmMakefile* mf = tgt->Target->GetMakefile();
  1281. bool contextDependent = entries.HadContextSensitiveCondition;
  1282. for (EvaluatedTargetPropertyEntry& entry : entries.Entries) {
  1283. if (entry.ContextDependent) {
  1284. contextDependent = true;
  1285. }
  1286. cmLinkImplItem const& item = entry.LinkImplItem;
  1287. std::string const& targetName = item.AsStr();
  1288. for (std::string& src : entry.Values) {
  1289. cmSourceFile* sf = mf->GetOrCreateSource(src);
  1290. std::string e;
  1291. std::string fullPath = sf->ResolveFullPath(&e);
  1292. if (fullPath.empty()) {
  1293. if (!e.empty()) {
  1294. cmake* cm = tgt->GetLocalGenerator()->GetCMakeInstance();
  1295. cm->IssueMessage(MessageType::FATAL_ERROR, e, tgt->GetBacktrace());
  1296. }
  1297. return contextDependent;
  1298. }
  1299. if (!targetName.empty() && !cmSystemTools::FileIsFullPath(src)) {
  1300. std::ostringstream err;
  1301. if (!targetName.empty()) {
  1302. err << "Target \"" << targetName
  1303. << "\" contains relative path in its INTERFACE_SOURCES:\n \""
  1304. << src << "\"";
  1305. } else {
  1306. err << "Found relative path while evaluating sources of \""
  1307. << tgt->GetName() << "\":\n \"" << src << "\"\n";
  1308. }
  1309. tgt->GetLocalGenerator()->IssueMessage(MessageType::FATAL_ERROR,
  1310. err.str());
  1311. return contextDependent;
  1312. }
  1313. src = fullPath;
  1314. }
  1315. std::string usedSources;
  1316. for (std::string const& src : entry.Values) {
  1317. if (uniqueSrcs.insert(src).second) {
  1318. srcs.emplace_back(src, entry.Backtrace);
  1319. if (debugSources) {
  1320. usedSources += " * " + src + "\n";
  1321. }
  1322. }
  1323. }
  1324. if (!usedSources.empty()) {
  1325. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  1326. MessageType::LOG,
  1327. std::string("Used sources for target ") + tgt->GetName() + ":\n" +
  1328. usedSources,
  1329. entry.Backtrace);
  1330. }
  1331. }
  1332. return contextDependent;
  1333. }
  1334. }
  1335. std::vector<BT<std::string>> cmGeneratorTarget::GetSourceFilePaths(
  1336. std::string const& config) const
  1337. {
  1338. std::vector<BT<std::string>> files;
  1339. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  1340. if (!this->LocalGenerator->GetGlobalGenerator()->GetConfigureDoneCMP0026()) {
  1341. // At configure-time, this method can be called as part of getting the
  1342. // LOCATION property or to export() a file to be include()d. However
  1343. // there is no cmGeneratorTarget at configure-time, so search the SOURCES
  1344. // for TARGET_OBJECTS instead for backwards compatibility with OLD
  1345. // behavior of CMP0024 and CMP0026 only.
  1346. cmStringRange sourceEntries = this->Target->GetSourceEntries();
  1347. for (std::string const& entry : sourceEntries) {
  1348. std::vector<std::string> items = cmExpandedList(entry);
  1349. for (std::string const& item : items) {
  1350. if (cmHasLiteralPrefix(item, "$<TARGET_OBJECTS:") &&
  1351. item.back() == '>') {
  1352. continue;
  1353. }
  1354. files.emplace_back(item);
  1355. }
  1356. }
  1357. return files;
  1358. }
  1359. std::vector<std::string> debugProperties;
  1360. this->Makefile->GetDefExpandList("CMAKE_DEBUG_TARGET_PROPERTIES",
  1361. debugProperties);
  1362. bool debugSources =
  1363. !this->DebugSourcesDone && cm::contains(debugProperties, "SOURCES");
  1364. if (this->LocalGenerator->GetGlobalGenerator()->GetConfigureDoneCMP0026()) {
  1365. this->DebugSourcesDone = true;
  1366. }
  1367. cmGeneratorExpressionDAGChecker dagChecker(this, "SOURCES", nullptr,
  1368. nullptr);
  1369. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  1370. this, config, std::string(), &dagChecker, this->SourceEntries);
  1371. std::unordered_set<std::string> uniqueSrcs;
  1372. bool contextDependentDirectSources =
  1373. processSources(this, entries, files, uniqueSrcs, debugSources);
  1374. // Collect INTERFACE_SOURCES of all direct link-dependencies.
  1375. EvaluatedTargetPropertyEntries linkInterfaceSourcesEntries;
  1376. AddInterfaceEntries(this, config, "INTERFACE_SOURCES", std::string(),
  1377. &dagChecker, linkInterfaceSourcesEntries);
  1378. std::vector<std::string>::size_type numFilesBefore = files.size();
  1379. bool contextDependentInterfaceSources = processSources(
  1380. this, linkInterfaceSourcesEntries, files, uniqueSrcs, debugSources);
  1381. // Collect TARGET_OBJECTS of direct object link-dependencies.
  1382. bool contextDependentObjects = false;
  1383. std::vector<std::string>::size_type numFilesBefore2 = files.size();
  1384. if (this->GetType() != cmStateEnums::OBJECT_LIBRARY) {
  1385. EvaluatedTargetPropertyEntries linkObjectsEntries;
  1386. AddObjectEntries(this, config, &dagChecker, linkObjectsEntries);
  1387. contextDependentObjects = processSources(this, linkObjectsEntries, files,
  1388. uniqueSrcs, debugSources);
  1389. }
  1390. if (!contextDependentDirectSources &&
  1391. !(contextDependentInterfaceSources && numFilesBefore < files.size()) &&
  1392. !(contextDependentObjects && numFilesBefore2 < files.size())) {
  1393. this->LinkImplementationLanguageIsContextDependent = false;
  1394. }
  1395. return files;
  1396. }
  1397. void cmGeneratorTarget::GetSourceFiles(std::vector<cmSourceFile*>& files,
  1398. const std::string& config) const
  1399. {
  1400. std::vector<BT<cmSourceFile*>> tmp = this->GetSourceFiles(config);
  1401. files.reserve(tmp.size());
  1402. for (BT<cmSourceFile*>& v : tmp) {
  1403. files.push_back(v.Value);
  1404. }
  1405. }
  1406. std::vector<BT<cmSourceFile*>> cmGeneratorTarget::GetSourceFiles(
  1407. std::string const& config) const
  1408. {
  1409. std::vector<BT<cmSourceFile*>> files;
  1410. if (!this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  1411. // Since we are still configuring not all sources may exist yet,
  1412. // so we need to avoid full source classification because that
  1413. // requires the absolute paths to all sources to be determined.
  1414. // Since this is only for compatibility with old policies that
  1415. // projects should not depend on anymore, just compute the files
  1416. // without memoizing them.
  1417. std::vector<BT<std::string>> srcs = this->GetSourceFilePaths(config);
  1418. std::set<cmSourceFile*> emitted;
  1419. for (BT<std::string> const& s : srcs) {
  1420. cmSourceFile* sf = this->Makefile->GetOrCreateSource(s.Value);
  1421. if (emitted.insert(sf).second) {
  1422. files.emplace_back(sf, s.Backtrace);
  1423. }
  1424. }
  1425. return files;
  1426. }
  1427. KindedSources const& kinded = this->GetKindedSources(config);
  1428. files.reserve(kinded.Sources.size());
  1429. for (SourceAndKind const& si : kinded.Sources) {
  1430. files.push_back(si.Source);
  1431. }
  1432. return files;
  1433. }
  1434. void cmGeneratorTarget::GetSourceFilesWithoutObjectLibraries(
  1435. std::vector<cmSourceFile*>& files, const std::string& config) const
  1436. {
  1437. std::vector<BT<cmSourceFile*>> tmp =
  1438. this->GetSourceFilesWithoutObjectLibraries(config);
  1439. files.reserve(tmp.size());
  1440. for (BT<cmSourceFile*>& v : tmp) {
  1441. files.push_back(v.Value);
  1442. }
  1443. }
  1444. std::vector<BT<cmSourceFile*>>
  1445. cmGeneratorTarget::GetSourceFilesWithoutObjectLibraries(
  1446. std::string const& config) const
  1447. {
  1448. std::vector<BT<cmSourceFile*>> files;
  1449. KindedSources const& kinded = this->GetKindedSources(config);
  1450. files.reserve(kinded.Sources.size());
  1451. for (SourceAndKind const& si : kinded.Sources) {
  1452. if (si.Source.Value->GetObjectLibrary().empty()) {
  1453. files.push_back(si.Source);
  1454. }
  1455. }
  1456. return files;
  1457. }
  1458. cmGeneratorTarget::KindedSources const& cmGeneratorTarget::GetKindedSources(
  1459. std::string const& config) const
  1460. {
  1461. // If we already processed one configuration and found no dependenc
  1462. // on configuration then always use the one result.
  1463. if (!this->LinkImplementationLanguageIsContextDependent) {
  1464. return this->KindedSourcesMap.begin()->second;
  1465. }
  1466. // Lookup any existing link implementation for this configuration.
  1467. std::string const key = cmSystemTools::UpperCase(config);
  1468. auto it = this->KindedSourcesMap.find(key);
  1469. if (it != this->KindedSourcesMap.end()) {
  1470. if (!it->second.Initialized) {
  1471. std::ostringstream e;
  1472. e << "The SOURCES of \"" << this->GetName()
  1473. << "\" use a generator expression that depends on the "
  1474. "SOURCES themselves.";
  1475. this->GlobalGenerator->GetCMakeInstance()->IssueMessage(
  1476. MessageType::FATAL_ERROR, e.str(), this->GetBacktrace());
  1477. static KindedSources empty;
  1478. return empty;
  1479. }
  1480. return it->second;
  1481. }
  1482. // Add an entry to the map for this configuration.
  1483. KindedSources& files = this->KindedSourcesMap[key];
  1484. this->ComputeKindedSources(files, config);
  1485. files.Initialized = true;
  1486. return files;
  1487. }
  1488. void cmGeneratorTarget::ComputeKindedSources(KindedSources& files,
  1489. std::string const& config) const
  1490. {
  1491. // Get the source file paths by string.
  1492. std::vector<BT<std::string>> srcs = this->GetSourceFilePaths(config);
  1493. cmsys::RegularExpression header_regex(CM_HEADER_REGEX);
  1494. std::vector<cmSourceFile*> badObjLib;
  1495. std::set<cmSourceFile*> emitted;
  1496. for (BT<std::string> const& s : srcs) {
  1497. // Create each source at most once.
  1498. cmSourceFile* sf = this->Makefile->GetOrCreateSource(s.Value);
  1499. if (!emitted.insert(sf).second) {
  1500. continue;
  1501. }
  1502. // Compute the kind (classification) of this source file.
  1503. SourceKind kind;
  1504. std::string ext = cmSystemTools::LowerCase(sf->GetExtension());
  1505. if (sf->GetCustomCommand()) {
  1506. kind = SourceKindCustomCommand;
  1507. // XXX(clang-tidy): https://bugs.llvm.org/show_bug.cgi?id=44165
  1508. // NOLINTNEXTLINE(bugprone-branch-clone)
  1509. } else if (this->Target->GetType() == cmStateEnums::UTILITY) {
  1510. kind = SourceKindExtra;
  1511. } else if (this->IsSourceFilePartOfUnityBatch(sf->ResolveFullPath())) {
  1512. kind = SourceKindUnityBatched;
  1513. // XXX(clang-tidy): https://bugs.llvm.org/show_bug.cgi?id=44165
  1514. // NOLINTNEXTLINE(bugprone-branch-clone)
  1515. } else if (sf->GetPropertyAsBool("HEADER_FILE_ONLY")) {
  1516. kind = SourceKindHeader;
  1517. } else if (sf->GetPropertyAsBool("EXTERNAL_OBJECT")) {
  1518. kind = SourceKindExternalObject;
  1519. } else if (!sf->GetOrDetermineLanguage().empty()) {
  1520. kind = SourceKindObjectSource;
  1521. } else if (ext == "def") {
  1522. kind = SourceKindModuleDefinition;
  1523. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  1524. badObjLib.push_back(sf);
  1525. }
  1526. } else if (ext == "idl") {
  1527. kind = SourceKindIDL;
  1528. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  1529. badObjLib.push_back(sf);
  1530. }
  1531. } else if (ext == "resx") {
  1532. kind = SourceKindResx;
  1533. } else if (ext == "appxmanifest") {
  1534. kind = SourceKindAppManifest;
  1535. } else if (ext == "manifest") {
  1536. kind = SourceKindManifest;
  1537. } else if (ext == "pfx") {
  1538. kind = SourceKindCertificate;
  1539. } else if (ext == "xaml") {
  1540. kind = SourceKindXaml;
  1541. } else if (header_regex.find(sf->ResolveFullPath())) {
  1542. kind = SourceKindHeader;
  1543. } else {
  1544. kind = SourceKindExtra;
  1545. }
  1546. // Save this classified source file in the result vector.
  1547. files.Sources.push_back({ BT<cmSourceFile*>(sf, s.Backtrace), kind });
  1548. }
  1549. if (!badObjLib.empty()) {
  1550. std::ostringstream e;
  1551. e << "OBJECT library \"" << this->GetName() << "\" contains:\n";
  1552. for (cmSourceFile* i : badObjLib) {
  1553. e << " " << i->GetLocation().GetName() << "\n";
  1554. }
  1555. e << "but may contain only sources that compile, header files, and "
  1556. "other files that would not affect linking of a normal library.";
  1557. this->GlobalGenerator->GetCMakeInstance()->IssueMessage(
  1558. MessageType::FATAL_ERROR, e.str(), this->GetBacktrace());
  1559. }
  1560. }
  1561. std::vector<cmGeneratorTarget::AllConfigSource> const&
  1562. cmGeneratorTarget::GetAllConfigSources() const
  1563. {
  1564. if (this->AllConfigSources.empty()) {
  1565. this->ComputeAllConfigSources();
  1566. }
  1567. return this->AllConfigSources;
  1568. }
  1569. void cmGeneratorTarget::ComputeAllConfigSources() const
  1570. {
  1571. std::vector<std::string> configs;
  1572. this->Makefile->GetConfigurations(configs);
  1573. std::map<cmSourceFile const*, size_t> index;
  1574. for (size_t ci = 0; ci < configs.size(); ++ci) {
  1575. KindedSources const& sources = this->GetKindedSources(configs[ci]);
  1576. for (SourceAndKind const& src : sources.Sources) {
  1577. auto mi = index.find(src.Source.Value);
  1578. if (mi == index.end()) {
  1579. AllConfigSource acs;
  1580. acs.Source = src.Source.Value;
  1581. acs.Kind = src.Kind;
  1582. this->AllConfigSources.push_back(std::move(acs));
  1583. std::map<cmSourceFile const*, size_t>::value_type entry(
  1584. src.Source.Value, this->AllConfigSources.size() - 1);
  1585. mi = index.insert(entry).first;
  1586. }
  1587. this->AllConfigSources[mi->second].Configs.push_back(ci);
  1588. }
  1589. }
  1590. }
  1591. std::vector<cmGeneratorTarget::AllConfigSource>
  1592. cmGeneratorTarget::GetAllConfigSources(SourceKind kind) const
  1593. {
  1594. std::vector<AllConfigSource> result;
  1595. for (AllConfigSource const& source : this->GetAllConfigSources()) {
  1596. if (source.Kind == kind) {
  1597. result.push_back(source);
  1598. }
  1599. }
  1600. return result;
  1601. }
  1602. std::set<std::string> cmGeneratorTarget::GetAllConfigCompileLanguages() const
  1603. {
  1604. std::set<std::string> languages;
  1605. std::vector<AllConfigSource> const& sources = this->GetAllConfigSources();
  1606. for (AllConfigSource const& si : sources) {
  1607. std::string const& lang = si.Source->GetOrDetermineLanguage();
  1608. if (!lang.empty()) {
  1609. languages.emplace(lang);
  1610. }
  1611. }
  1612. return languages;
  1613. }
  1614. std::string cmGeneratorTarget::GetCompilePDBName(
  1615. const std::string& config) const
  1616. {
  1617. std::string prefix;
  1618. std::string base;
  1619. std::string suffix;
  1620. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  1621. prefix, base, suffix);
  1622. // Check for a per-configuration output directory target property.
  1623. std::string configUpper = cmSystemTools::UpperCase(config);
  1624. std::string configProp = cmStrCat("COMPILE_PDB_NAME_", configUpper);
  1625. cmProp config_name = this->GetProperty(configProp);
  1626. if (config_name && !config_name->empty()) {
  1627. return prefix + *config_name + ".pdb";
  1628. }
  1629. cmProp name = this->GetProperty("COMPILE_PDB_NAME");
  1630. if (name && !name->empty()) {
  1631. return prefix + *name + ".pdb";
  1632. }
  1633. return "";
  1634. }
  1635. std::string cmGeneratorTarget::GetCompilePDBPath(
  1636. const std::string& config) const
  1637. {
  1638. std::string dir = this->GetCompilePDBDirectory(config);
  1639. std::string name = this->GetCompilePDBName(config);
  1640. if (dir.empty() && !name.empty() && this->HaveWellDefinedOutputFiles()) {
  1641. dir = this->GetPDBDirectory(config);
  1642. }
  1643. if (!dir.empty()) {
  1644. dir += "/";
  1645. }
  1646. return dir + name;
  1647. }
  1648. bool cmGeneratorTarget::HasSOName(const std::string& config) const
  1649. {
  1650. // soname is supported only for shared libraries and modules,
  1651. // and then only when the platform supports an soname flag.
  1652. return ((this->GetType() == cmStateEnums::SHARED_LIBRARY) &&
  1653. !this->GetPropertyAsBool("NO_SONAME") &&
  1654. this->Makefile->GetSONameFlag(this->GetLinkerLanguage(config)));
  1655. }
  1656. bool cmGeneratorTarget::NeedRelinkBeforeInstall(
  1657. const std::string& config) const
  1658. {
  1659. // Only executables and shared libraries can have an rpath and may
  1660. // need relinking.
  1661. if (this->GetType() != cmStateEnums::EXECUTABLE &&
  1662. this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  1663. this->GetType() != cmStateEnums::MODULE_LIBRARY) {
  1664. return false;
  1665. }
  1666. // If there is no install location this target will not be installed
  1667. // and therefore does not need relinking.
  1668. if (!this->Target->GetHaveInstallRule()) {
  1669. return false;
  1670. }
  1671. // If skipping all rpaths completely then no relinking is needed.
  1672. if (this->Makefile->IsOn("CMAKE_SKIP_RPATH")) {
  1673. return false;
  1674. }
  1675. // If building with the install-tree rpath no relinking is needed.
  1676. if (this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH")) {
  1677. return false;
  1678. }
  1679. // If chrpath is going to be used no relinking is needed.
  1680. if (this->IsChrpathUsed(config)) {
  1681. return false;
  1682. }
  1683. // Check for rpath support on this platform.
  1684. std::string ll = this->GetLinkerLanguage(config);
  1685. if (!ll.empty()) {
  1686. std::string flagVar =
  1687. cmStrCat("CMAKE_SHARED_LIBRARY_RUNTIME_", ll, "_FLAG");
  1688. if (!this->Makefile->IsSet(flagVar)) {
  1689. // There is no rpath support on this platform so nothing needs
  1690. // relinking.
  1691. return false;
  1692. }
  1693. } else {
  1694. // No linker language is known. This error will be reported by
  1695. // other code.
  1696. return false;
  1697. }
  1698. // If either a build or install tree rpath is set then the rpath
  1699. // will likely change between the build tree and install tree and
  1700. // this target must be relinked.
  1701. bool have_rpath =
  1702. this->HaveBuildTreeRPATH(config) || this->HaveInstallTreeRPATH(config);
  1703. bool is_ninja =
  1704. this->LocalGenerator->GetGlobalGenerator()->GetName() == "Ninja";
  1705. if (have_rpath && is_ninja) {
  1706. std::ostringstream w;
  1707. /* clang-format off */
  1708. w <<
  1709. "The install of the " << this->GetName() << " target requires "
  1710. "changing an RPATH from the build tree, but this is not supported "
  1711. "with the Ninja generator unless on an ELF-based platform. The "
  1712. "CMAKE_BUILD_WITH_INSTALL_RPATH variable may be set to avoid this "
  1713. "relinking step."
  1714. ;
  1715. /* clang-format on */
  1716. cmake* cm = this->LocalGenerator->GetCMakeInstance();
  1717. cm->IssueMessage(MessageType::FATAL_ERROR, w.str(), this->GetBacktrace());
  1718. }
  1719. return have_rpath;
  1720. }
  1721. bool cmGeneratorTarget::IsChrpathUsed(const std::string& config) const
  1722. {
  1723. // Only certain target types have an rpath.
  1724. if (!(this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  1725. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  1726. this->GetType() == cmStateEnums::EXECUTABLE)) {
  1727. return false;
  1728. }
  1729. // If the target will not be installed we do not need to change its
  1730. // rpath.
  1731. if (!this->Target->GetHaveInstallRule()) {
  1732. return false;
  1733. }
  1734. // Skip chrpath if skipping rpath altogether.
  1735. if (this->Makefile->IsOn("CMAKE_SKIP_RPATH")) {
  1736. return false;
  1737. }
  1738. // Skip chrpath if it does not need to be changed at install time.
  1739. if (this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH")) {
  1740. return false;
  1741. }
  1742. // Allow the user to disable builtin chrpath explicitly.
  1743. if (this->Makefile->IsOn("CMAKE_NO_BUILTIN_CHRPATH")) {
  1744. return false;
  1745. }
  1746. if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  1747. return true;
  1748. }
  1749. #if defined(CMAKE_USE_ELF_PARSER)
  1750. // Enable if the rpath flag uses a separator and the target uses ELF
  1751. // binaries.
  1752. std::string ll = this->GetLinkerLanguage(config);
  1753. if (!ll.empty()) {
  1754. std::string sepVar =
  1755. cmStrCat("CMAKE_SHARED_LIBRARY_RUNTIME_", ll, "_FLAG_SEP");
  1756. const char* sep = this->Makefile->GetDefinition(sepVar);
  1757. if (sep && *sep) {
  1758. // TODO: Add ELF check to ABI detection and get rid of
  1759. // CMAKE_EXECUTABLE_FORMAT.
  1760. if (const char* fmt =
  1761. this->Makefile->GetDefinition("CMAKE_EXECUTABLE_FORMAT")) {
  1762. return strcmp(fmt, "ELF") == 0;
  1763. }
  1764. }
  1765. }
  1766. #endif
  1767. static_cast<void>(config);
  1768. return false;
  1769. }
  1770. bool cmGeneratorTarget::IsImportedSharedLibWithoutSOName(
  1771. const std::string& config) const
  1772. {
  1773. if (this->IsImported() && this->GetType() == cmStateEnums::SHARED_LIBRARY) {
  1774. if (cmGeneratorTarget::ImportInfo const* info =
  1775. this->GetImportInfo(config)) {
  1776. return info->NoSOName;
  1777. }
  1778. }
  1779. return false;
  1780. }
  1781. bool cmGeneratorTarget::HasMacOSXRpathInstallNameDir(
  1782. const std::string& config) const
  1783. {
  1784. bool install_name_is_rpath = false;
  1785. bool macosx_rpath = false;
  1786. if (!this->IsImported()) {
  1787. if (this->GetType() != cmStateEnums::SHARED_LIBRARY) {
  1788. return false;
  1789. }
  1790. cmProp install_name = this->GetProperty("INSTALL_NAME_DIR");
  1791. bool use_install_name = this->MacOSXUseInstallNameDir();
  1792. if (install_name && use_install_name && *install_name == "@rpath") {
  1793. install_name_is_rpath = true;
  1794. } else if (install_name && use_install_name) {
  1795. return false;
  1796. }
  1797. if (!install_name_is_rpath) {
  1798. macosx_rpath = this->MacOSXRpathInstallNameDirDefault();
  1799. }
  1800. } else {
  1801. // Lookup the imported soname.
  1802. if (cmGeneratorTarget::ImportInfo const* info =
  1803. this->GetImportInfo(config)) {
  1804. if (!info->NoSOName && !info->SOName.empty()) {
  1805. if (cmHasLiteralPrefix(info->SOName, "@rpath/")) {
  1806. install_name_is_rpath = true;
  1807. }
  1808. } else {
  1809. std::string install_name;
  1810. cmSystemTools::GuessLibraryInstallName(info->Location, install_name);
  1811. if (install_name.find("@rpath") != std::string::npos) {
  1812. install_name_is_rpath = true;
  1813. }
  1814. }
  1815. }
  1816. }
  1817. if (!install_name_is_rpath && !macosx_rpath) {
  1818. return false;
  1819. }
  1820. if (!this->Makefile->IsSet("CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG")) {
  1821. std::ostringstream w;
  1822. w << "Attempting to use ";
  1823. if (macosx_rpath) {
  1824. w << "MACOSX_RPATH";
  1825. } else {
  1826. w << "@rpath";
  1827. }
  1828. w << " without CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG being set.";
  1829. w << " This could be because you are using a Mac OS X version";
  1830. w << " less than 10.5 or because CMake's platform configuration is";
  1831. w << " corrupt.";
  1832. cmake* cm = this->LocalGenerator->GetCMakeInstance();
  1833. cm->IssueMessage(MessageType::FATAL_ERROR, w.str(), this->GetBacktrace());
  1834. }
  1835. return true;
  1836. }
  1837. bool cmGeneratorTarget::MacOSXRpathInstallNameDirDefault() const
  1838. {
  1839. // we can't do rpaths when unsupported
  1840. if (!this->Makefile->IsSet("CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG")) {
  1841. return false;
  1842. }
  1843. cmProp macosx_rpath_str = this->GetProperty("MACOSX_RPATH");
  1844. if (macosx_rpath_str) {
  1845. return this->GetPropertyAsBool("MACOSX_RPATH");
  1846. }
  1847. cmPolicies::PolicyStatus cmp0042 = this->GetPolicyStatusCMP0042();
  1848. if (cmp0042 == cmPolicies::WARN) {
  1849. this->LocalGenerator->GetGlobalGenerator()->AddCMP0042WarnTarget(
  1850. this->GetName());
  1851. }
  1852. return cmp0042 == cmPolicies::NEW;
  1853. }
  1854. bool cmGeneratorTarget::MacOSXUseInstallNameDir() const
  1855. {
  1856. cmProp build_with_install_name =
  1857. this->GetProperty("BUILD_WITH_INSTALL_NAME_DIR");
  1858. if (build_with_install_name) {
  1859. return cmIsOn(*build_with_install_name);
  1860. }
  1861. cmPolicies::PolicyStatus cmp0068 = this->GetPolicyStatusCMP0068();
  1862. if (cmp0068 == cmPolicies::NEW) {
  1863. return false;
  1864. }
  1865. bool use_install_name = this->GetPropertyAsBool("BUILD_WITH_INSTALL_RPATH");
  1866. if (use_install_name && cmp0068 == cmPolicies::WARN) {
  1867. this->LocalGenerator->GetGlobalGenerator()->AddCMP0068WarnTarget(
  1868. this->GetName());
  1869. }
  1870. return use_install_name;
  1871. }
  1872. bool cmGeneratorTarget::CanGenerateInstallNameDir(
  1873. InstallNameType name_type) const
  1874. {
  1875. cmPolicies::PolicyStatus cmp0068 = this->GetPolicyStatusCMP0068();
  1876. if (cmp0068 == cmPolicies::NEW) {
  1877. return true;
  1878. }
  1879. bool skip = this->Makefile->IsOn("CMAKE_SKIP_RPATH");
  1880. if (name_type == INSTALL_NAME_FOR_INSTALL) {
  1881. skip |= this->Makefile->IsOn("CMAKE_SKIP_INSTALL_RPATH");
  1882. } else {
  1883. skip |= this->GetPropertyAsBool("SKIP_BUILD_RPATH");
  1884. }
  1885. if (skip && cmp0068 == cmPolicies::WARN) {
  1886. this->LocalGenerator->GetGlobalGenerator()->AddCMP0068WarnTarget(
  1887. this->GetName());
  1888. }
  1889. return !skip;
  1890. }
  1891. std::string cmGeneratorTarget::GetSOName(const std::string& config) const
  1892. {
  1893. if (this->IsImported()) {
  1894. // Lookup the imported soname.
  1895. if (cmGeneratorTarget::ImportInfo const* info =
  1896. this->GetImportInfo(config)) {
  1897. if (info->NoSOName) {
  1898. // The imported library has no builtin soname so the name
  1899. // searched at runtime will be just the filename.
  1900. return cmSystemTools::GetFilenameName(info->Location);
  1901. }
  1902. // Use the soname given if any.
  1903. if (cmHasLiteralPrefix(info->SOName, "@rpath/")) {
  1904. return info->SOName.substr(6);
  1905. }
  1906. return info->SOName;
  1907. }
  1908. return "";
  1909. }
  1910. // Compute the soname that will be built.
  1911. return this->GetLibraryNames(config).SharedObject;
  1912. }
  1913. namespace {
  1914. bool shouldAddFullLevel(cmGeneratorTarget::BundleDirectoryLevel level)
  1915. {
  1916. return level == cmGeneratorTarget::FullLevel;
  1917. }
  1918. bool shouldAddContentLevel(cmGeneratorTarget::BundleDirectoryLevel level)
  1919. {
  1920. return level == cmGeneratorTarget::ContentLevel || shouldAddFullLevel(level);
  1921. }
  1922. }
  1923. std::string cmGeneratorTarget::GetAppBundleDirectory(
  1924. const std::string& config, BundleDirectoryLevel level) const
  1925. {
  1926. std::string fpath = cmStrCat(
  1927. this->GetFullName(config, cmStateEnums::RuntimeBinaryArtifact), '.');
  1928. cmProp ext = this->GetProperty("BUNDLE_EXTENSION");
  1929. fpath += (ext ? *ext : "app");
  1930. if (shouldAddContentLevel(level) &&
  1931. !this->Makefile->PlatformIsAppleEmbedded()) {
  1932. fpath += "/Contents";
  1933. if (shouldAddFullLevel(level)) {
  1934. fpath += "/MacOS";
  1935. }
  1936. }
  1937. return fpath;
  1938. }
  1939. bool cmGeneratorTarget::IsBundleOnApple() const
  1940. {
  1941. return this->IsFrameworkOnApple() || this->IsAppBundleOnApple() ||
  1942. this->IsCFBundleOnApple();
  1943. }
  1944. std::string cmGeneratorTarget::GetCFBundleDirectory(
  1945. const std::string& config, BundleDirectoryLevel level) const
  1946. {
  1947. std::string fpath = cmStrCat(
  1948. this->GetOutputName(config, cmStateEnums::RuntimeBinaryArtifact), '.');
  1949. std::string ext;
  1950. if (cmProp p = this->GetProperty("BUNDLE_EXTENSION")) {
  1951. ext = *p;
  1952. } else {
  1953. if (this->IsXCTestOnApple()) {
  1954. ext = "xctest";
  1955. } else {
  1956. ext = "bundle";
  1957. }
  1958. }
  1959. fpath += ext;
  1960. if (shouldAddContentLevel(level) &&
  1961. !this->Makefile->PlatformIsAppleEmbedded()) {
  1962. fpath += "/Contents";
  1963. if (shouldAddFullLevel(level)) {
  1964. fpath += "/MacOS";
  1965. }
  1966. }
  1967. return fpath;
  1968. }
  1969. std::string cmGeneratorTarget::GetFrameworkDirectory(
  1970. const std::string& config, BundleDirectoryLevel level) const
  1971. {
  1972. std::string fpath = cmStrCat(
  1973. this->GetOutputName(config, cmStateEnums::RuntimeBinaryArtifact), '.');
  1974. cmProp ext = this->GetProperty("BUNDLE_EXTENSION");
  1975. fpath += (ext ? *ext : "framework");
  1976. if (shouldAddFullLevel(level) &&
  1977. !this->Makefile->PlatformIsAppleEmbedded()) {
  1978. fpath += "/Versions/";
  1979. fpath += this->GetFrameworkVersion();
  1980. }
  1981. return fpath;
  1982. }
  1983. std::string cmGeneratorTarget::GetFullName(
  1984. const std::string& config, cmStateEnums::ArtifactType artifact) const
  1985. {
  1986. if (this->IsImported()) {
  1987. return this->GetFullNameImported(config, artifact);
  1988. }
  1989. return this->GetFullNameInternal(config, artifact);
  1990. }
  1991. std::string cmGeneratorTarget::GetInstallNameDirForBuildTree(
  1992. const std::string& config) const
  1993. {
  1994. if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  1995. // If building directly for installation then the build tree install_name
  1996. // is the same as the install tree.
  1997. if (this->MacOSXUseInstallNameDir()) {
  1998. std::string installPrefix =
  1999. this->Makefile->GetSafeDefinition("CMAKE_INSTALL_PREFIX");
  2000. return this->GetInstallNameDirForInstallTree(config, installPrefix);
  2001. }
  2002. // Use the build tree directory for the target.
  2003. if (this->CanGenerateInstallNameDir(INSTALL_NAME_FOR_BUILD)) {
  2004. std::string dir;
  2005. if (this->MacOSXRpathInstallNameDirDefault()) {
  2006. dir = "@rpath";
  2007. } else {
  2008. dir = this->GetDirectory(config);
  2009. }
  2010. dir += "/";
  2011. return dir;
  2012. }
  2013. }
  2014. return "";
  2015. }
  2016. std::string cmGeneratorTarget::GetInstallNameDirForInstallTree(
  2017. const std::string& config, const std::string& installPrefix) const
  2018. {
  2019. if (this->Makefile->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME")) {
  2020. std::string dir;
  2021. cmProp install_name_dir = this->GetProperty("INSTALL_NAME_DIR");
  2022. if (this->CanGenerateInstallNameDir(INSTALL_NAME_FOR_INSTALL)) {
  2023. if (install_name_dir && !install_name_dir->empty()) {
  2024. dir = *install_name_dir;
  2025. cmGeneratorExpression::ReplaceInstallPrefix(dir, installPrefix);
  2026. dir =
  2027. cmGeneratorExpression::Evaluate(dir, this->LocalGenerator, config);
  2028. if (!dir.empty()) {
  2029. dir = cmStrCat(dir, '/');
  2030. }
  2031. }
  2032. }
  2033. if (!install_name_dir) {
  2034. if (this->MacOSXRpathInstallNameDirDefault()) {
  2035. dir = "@rpath/";
  2036. }
  2037. }
  2038. return dir;
  2039. }
  2040. return "";
  2041. }
  2042. cmListFileBacktrace cmGeneratorTarget::GetBacktrace() const
  2043. {
  2044. return this->Target->GetBacktrace();
  2045. }
  2046. const std::set<BT<std::pair<std::string, bool>>>&
  2047. cmGeneratorTarget::GetUtilities() const
  2048. {
  2049. return this->Target->GetUtilities();
  2050. }
  2051. bool cmGeneratorTarget::HaveWellDefinedOutputFiles() const
  2052. {
  2053. return this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  2054. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2055. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  2056. this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  2057. this->GetType() == cmStateEnums::EXECUTABLE;
  2058. }
  2059. const std::string* cmGeneratorTarget::GetExportMacro() const
  2060. {
  2061. // Define the symbol for targets that export symbols.
  2062. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2063. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  2064. this->IsExecutableWithExports()) {
  2065. if (cmProp custom_export_name = this->GetProperty("DEFINE_SYMBOL")) {
  2066. this->ExportMacro = *custom_export_name;
  2067. } else {
  2068. std::string in = cmStrCat(this->GetName(), "_EXPORTS");
  2069. this->ExportMacro = cmSystemTools::MakeCidentifier(in);
  2070. }
  2071. return &this->ExportMacro;
  2072. }
  2073. return nullptr;
  2074. }
  2075. class cmTargetCollectLinkLanguages
  2076. {
  2077. public:
  2078. cmTargetCollectLinkLanguages(cmGeneratorTarget const* target,
  2079. std::string config,
  2080. std::unordered_set<std::string>& languages,
  2081. cmGeneratorTarget const* head, bool secondPass)
  2082. : Config(std::move(config))
  2083. , Languages(languages)
  2084. , HeadTarget(head)
  2085. , Target(target)
  2086. , SecondPass(secondPass)
  2087. {
  2088. this->Visited.insert(target);
  2089. }
  2090. void Visit(cmLinkItem const& item)
  2091. {
  2092. if (!item.Target) {
  2093. if (item.AsStr().find("::") != std::string::npos) {
  2094. bool noMessage = false;
  2095. MessageType messageType = MessageType::FATAL_ERROR;
  2096. std::ostringstream e;
  2097. switch (this->Target->GetLocalGenerator()->GetPolicyStatus(
  2098. cmPolicies::CMP0028)) {
  2099. case cmPolicies::WARN: {
  2100. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0028) << "\n";
  2101. messageType = MessageType::AUTHOR_WARNING;
  2102. } break;
  2103. case cmPolicies::OLD:
  2104. noMessage = true;
  2105. case cmPolicies::REQUIRED_IF_USED:
  2106. case cmPolicies::REQUIRED_ALWAYS:
  2107. case cmPolicies::NEW:
  2108. // Issue the fatal message.
  2109. break;
  2110. }
  2111. if (!noMessage) {
  2112. e << "Target \"" << this->Target->GetName()
  2113. << "\" links to target \"" << item.AsStr()
  2114. << "\" but the target was not found. Perhaps a find_package() "
  2115. "call is missing for an IMPORTED target, or an ALIAS target is "
  2116. "missing?";
  2117. this->Target->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  2118. messageType, e.str(), this->Target->GetBacktrace());
  2119. }
  2120. }
  2121. return;
  2122. }
  2123. if (!this->Visited.insert(item.Target).second) {
  2124. return;
  2125. }
  2126. cmLinkInterface const* iface = item.Target->GetLinkInterface(
  2127. this->Config, this->HeadTarget, this->SecondPass);
  2128. if (!iface) {
  2129. return;
  2130. }
  2131. if (iface->HadLinkLanguageSensitiveCondition) {
  2132. this->HadLinkLanguageSensitiveCondition = true;
  2133. }
  2134. for (std::string const& language : iface->Languages) {
  2135. this->Languages.insert(language);
  2136. }
  2137. for (cmLinkItem const& lib : iface->Libraries) {
  2138. this->Visit(lib);
  2139. }
  2140. }
  2141. bool GetHadLinkLanguageSensitiveCondition()
  2142. {
  2143. return HadLinkLanguageSensitiveCondition;
  2144. }
  2145. private:
  2146. std::string Config;
  2147. std::unordered_set<std::string>& Languages;
  2148. cmGeneratorTarget const* HeadTarget;
  2149. const cmGeneratorTarget* Target;
  2150. std::set<cmGeneratorTarget const*> Visited;
  2151. bool SecondPass;
  2152. bool HadLinkLanguageSensitiveCondition = false;
  2153. };
  2154. cmGeneratorTarget::LinkClosure const* cmGeneratorTarget::GetLinkClosure(
  2155. const std::string& config) const
  2156. {
  2157. std::string key(cmSystemTools::UpperCase(config));
  2158. auto i = this->LinkClosureMap.find(key);
  2159. if (i == this->LinkClosureMap.end()) {
  2160. LinkClosure lc;
  2161. this->ComputeLinkClosure(config, lc);
  2162. LinkClosureMapType::value_type entry(key, lc);
  2163. i = this->LinkClosureMap.insert(entry).first;
  2164. }
  2165. return &i->second;
  2166. }
  2167. class cmTargetSelectLinker
  2168. {
  2169. int Preference;
  2170. cmGeneratorTarget const* Target;
  2171. cmGlobalGenerator* GG;
  2172. std::set<std::string> Preferred;
  2173. public:
  2174. cmTargetSelectLinker(cmGeneratorTarget const* target)
  2175. : Preference(0)
  2176. , Target(target)
  2177. {
  2178. this->GG = this->Target->GetLocalGenerator()->GetGlobalGenerator();
  2179. }
  2180. void Consider(const std::string& lang)
  2181. {
  2182. int preference = this->GG->GetLinkerPreference(lang);
  2183. if (preference > this->Preference) {
  2184. this->Preference = preference;
  2185. this->Preferred.clear();
  2186. }
  2187. if (preference == this->Preference) {
  2188. this->Preferred.insert(lang);
  2189. }
  2190. }
  2191. std::string Choose()
  2192. {
  2193. if (this->Preferred.empty()) {
  2194. return "";
  2195. }
  2196. if (this->Preferred.size() > 1) {
  2197. std::ostringstream e;
  2198. e << "Target " << this->Target->GetName()
  2199. << " contains multiple languages with the highest linker preference"
  2200. << " (" << this->Preference << "):\n";
  2201. for (std::string const& li : this->Preferred) {
  2202. e << " " << li << "\n";
  2203. }
  2204. e << "Set the LINKER_LANGUAGE property for this target.";
  2205. cmake* cm = this->Target->GetLocalGenerator()->GetCMakeInstance();
  2206. cm->IssueMessage(MessageType::FATAL_ERROR, e.str(),
  2207. this->Target->GetBacktrace());
  2208. }
  2209. return *this->Preferred.begin();
  2210. }
  2211. };
  2212. bool cmGeneratorTarget::ComputeLinkClosure(const std::string& config,
  2213. LinkClosure& lc,
  2214. bool secondPass) const
  2215. {
  2216. // Get languages built in this target.
  2217. std::unordered_set<std::string> languages;
  2218. cmLinkImplementation const* impl =
  2219. this->GetLinkImplementation(config, secondPass);
  2220. assert(impl);
  2221. languages.insert(impl->Languages.cbegin(), impl->Languages.cend());
  2222. // Add interface languages from linked targets.
  2223. // cmTargetCollectLinkLanguages cll(this, config, languages, this,
  2224. // secondPass);
  2225. cmTargetCollectLinkLanguages cll(this, config, languages, this, secondPass);
  2226. for (cmLinkImplItem const& lib : impl->Libraries) {
  2227. cll.Visit(lib);
  2228. }
  2229. // Store the transitive closure of languages.
  2230. cm::append(lc.Languages, languages);
  2231. // Choose the language whose linker should be used.
  2232. if (secondPass || lc.LinkerLanguage.empty()) {
  2233. // Find the language with the highest preference value.
  2234. cmTargetSelectLinker tsl(this);
  2235. // First select from the languages compiled directly in this target.
  2236. for (std::string const& l : impl->Languages) {
  2237. tsl.Consider(l);
  2238. }
  2239. // Now consider languages that propagate from linked targets.
  2240. for (std::string const& lang : languages) {
  2241. std::string propagates =
  2242. "CMAKE_" + lang + "_LINKER_PREFERENCE_PROPAGATES";
  2243. if (this->Makefile->IsOn(propagates)) {
  2244. tsl.Consider(lang);
  2245. }
  2246. }
  2247. lc.LinkerLanguage = tsl.Choose();
  2248. }
  2249. return impl->HadLinkLanguageSensitiveCondition ||
  2250. cll.GetHadLinkLanguageSensitiveCondition();
  2251. }
  2252. void cmGeneratorTarget::ComputeLinkClosure(const std::string& config,
  2253. LinkClosure& lc) const
  2254. {
  2255. bool secondPass = false;
  2256. {
  2257. LinkClosure linkClosure;
  2258. linkClosure.LinkerLanguage = this->LinkerLanguage;
  2259. // Get languages built in this target.
  2260. secondPass = this->ComputeLinkClosure(config, linkClosure, false);
  2261. this->LinkerLanguage = linkClosure.LinkerLanguage;
  2262. if (!secondPass) {
  2263. lc = std::move(linkClosure);
  2264. }
  2265. }
  2266. if (secondPass) {
  2267. LinkClosure linkClosure;
  2268. this->ComputeLinkClosure(config, linkClosure, secondPass);
  2269. lc = std::move(linkClosure);
  2270. // linker language must not be changed between the two passes
  2271. if (this->LinkerLanguage != lc.LinkerLanguage) {
  2272. std::ostringstream e;
  2273. e << "Evaluation of $<LINK_LANGUAGE:...> or $<LINK_LAND_AND_ID:...> "
  2274. "changes\nthe linker language for target \""
  2275. << this->GetName() << "\" (from '" << this->LinkerLanguage << "' to '"
  2276. << lc.LinkerLanguage << "') which is invalid.";
  2277. cmSystemTools::Error(e.str());
  2278. }
  2279. }
  2280. }
  2281. void cmGeneratorTarget::GetFullNameComponents(
  2282. std::string& prefix, std::string& base, std::string& suffix,
  2283. const std::string& config, cmStateEnums::ArtifactType artifact) const
  2284. {
  2285. this->GetFullNameInternal(config, artifact, prefix, base, suffix);
  2286. }
  2287. std::string cmGeneratorTarget::BuildBundleDirectory(
  2288. const std::string& base, const std::string& config,
  2289. BundleDirectoryLevel level) const
  2290. {
  2291. std::string fpath = base;
  2292. if (this->IsAppBundleOnApple()) {
  2293. fpath += this->GetAppBundleDirectory(config, level);
  2294. }
  2295. if (this->IsFrameworkOnApple()) {
  2296. fpath += this->GetFrameworkDirectory(config, level);
  2297. }
  2298. if (this->IsCFBundleOnApple()) {
  2299. fpath += this->GetCFBundleDirectory(config, level);
  2300. }
  2301. return fpath;
  2302. }
  2303. std::string cmGeneratorTarget::GetMacContentDirectory(
  2304. const std::string& config, cmStateEnums::ArtifactType artifact) const
  2305. {
  2306. // Start with the output directory for the target.
  2307. std::string fpath = cmStrCat(this->GetDirectory(config, artifact), '/');
  2308. BundleDirectoryLevel level = ContentLevel;
  2309. if (this->IsFrameworkOnApple()) {
  2310. // additional files with a framework go into the version specific
  2311. // directory
  2312. level = FullLevel;
  2313. }
  2314. fpath = this->BuildBundleDirectory(fpath, config, level);
  2315. return fpath;
  2316. }
  2317. std::string cmGeneratorTarget::GetEffectiveFolderName() const
  2318. {
  2319. std::string effectiveFolder;
  2320. if (!this->GlobalGenerator->UseFolderProperty()) {
  2321. return effectiveFolder;
  2322. }
  2323. cmProp targetFolder = this->GetProperty("FOLDER");
  2324. if (targetFolder) {
  2325. effectiveFolder += *targetFolder;
  2326. }
  2327. return effectiveFolder;
  2328. }
  2329. cmGeneratorTarget::CompileInfo const* cmGeneratorTarget::GetCompileInfo(
  2330. const std::string& config) const
  2331. {
  2332. // There is no compile information for imported targets.
  2333. if (this->IsImported()) {
  2334. return nullptr;
  2335. }
  2336. if (this->GetType() > cmStateEnums::OBJECT_LIBRARY) {
  2337. std::string msg = cmStrCat("cmTarget::GetCompileInfo called for ",
  2338. this->GetName(), " which has type ",
  2339. cmState::GetTargetTypeName(this->GetType()));
  2340. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  2341. return nullptr;
  2342. }
  2343. // Lookup/compute/cache the compile information for this configuration.
  2344. std::string config_upper;
  2345. if (!config.empty()) {
  2346. config_upper = cmSystemTools::UpperCase(config);
  2347. }
  2348. auto i = this->CompileInfoMap.find(config_upper);
  2349. if (i == this->CompileInfoMap.end()) {
  2350. CompileInfo info;
  2351. this->ComputePDBOutputDir("COMPILE_PDB", config, info.CompilePdbDir);
  2352. CompileInfoMapType::value_type entry(config_upper, info);
  2353. i = this->CompileInfoMap.insert(entry).first;
  2354. }
  2355. return &i->second;
  2356. }
  2357. cmGeneratorTarget::ModuleDefinitionInfo const*
  2358. cmGeneratorTarget::GetModuleDefinitionInfo(std::string const& config) const
  2359. {
  2360. // A module definition file only makes sense on certain target types.
  2361. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  2362. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  2363. !this->IsExecutableWithExports()) {
  2364. return nullptr;
  2365. }
  2366. // Lookup/compute/cache the compile information for this configuration.
  2367. std::string config_upper;
  2368. if (!config.empty()) {
  2369. config_upper = cmSystemTools::UpperCase(config);
  2370. }
  2371. auto i = this->ModuleDefinitionInfoMap.find(config_upper);
  2372. if (i == this->ModuleDefinitionInfoMap.end()) {
  2373. ModuleDefinitionInfo info;
  2374. this->ComputeModuleDefinitionInfo(config, info);
  2375. ModuleDefinitionInfoMapType::value_type entry(config_upper, info);
  2376. i = this->ModuleDefinitionInfoMap.insert(entry).first;
  2377. }
  2378. return &i->second;
  2379. }
  2380. void cmGeneratorTarget::ComputeModuleDefinitionInfo(
  2381. std::string const& config, ModuleDefinitionInfo& info) const
  2382. {
  2383. this->GetModuleDefinitionSources(info.Sources, config);
  2384. info.WindowsExportAllSymbols =
  2385. this->Makefile->IsOn("CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS") &&
  2386. this->GetPropertyAsBool("WINDOWS_EXPORT_ALL_SYMBOLS");
  2387. #if !defined(CMAKE_BOOTSTRAP)
  2388. info.DefFileGenerated =
  2389. info.WindowsExportAllSymbols || info.Sources.size() > 1;
  2390. #else
  2391. // Our __create_def helper is not available during CMake bootstrap.
  2392. info.DefFileGenerated = false;
  2393. #endif
  2394. if (info.DefFileGenerated) {
  2395. info.DefFile =
  2396. this->GetObjectDirectory(config) /* has slash */ + "exports.def";
  2397. } else if (!info.Sources.empty()) {
  2398. info.DefFile = info.Sources.front()->GetFullPath();
  2399. }
  2400. }
  2401. bool cmGeneratorTarget::IsDLLPlatform() const
  2402. {
  2403. return this->Target->IsDLLPlatform();
  2404. }
  2405. void cmGeneratorTarget::GetAutoUicOptions(std::vector<std::string>& result,
  2406. const std::string& config) const
  2407. {
  2408. const char* prop =
  2409. this->GetLinkInterfaceDependentStringProperty("AUTOUIC_OPTIONS", config);
  2410. if (!prop) {
  2411. return;
  2412. }
  2413. cmGeneratorExpressionDAGChecker dagChecker(this, "AUTOUIC_OPTIONS", nullptr,
  2414. nullptr);
  2415. cmExpandList(cmGeneratorExpression::Evaluate(prop, this->LocalGenerator,
  2416. config, this, &dagChecker),
  2417. result);
  2418. }
  2419. void processILibs(const std::string& config,
  2420. cmGeneratorTarget const* headTarget, cmLinkItem const& item,
  2421. cmGlobalGenerator* gg,
  2422. std::vector<cmGeneratorTarget const*>& tgts,
  2423. std::set<cmGeneratorTarget const*>& emitted)
  2424. {
  2425. if (item.Target && emitted.insert(item.Target).second) {
  2426. tgts.push_back(item.Target);
  2427. if (cmLinkInterfaceLibraries const* iface =
  2428. item.Target->GetLinkInterfaceLibraries(config, headTarget, true)) {
  2429. for (cmLinkItem const& lib : iface->Libraries) {
  2430. processILibs(config, headTarget, lib, gg, tgts, emitted);
  2431. }
  2432. }
  2433. }
  2434. }
  2435. const std::vector<const cmGeneratorTarget*>&
  2436. cmGeneratorTarget::GetLinkImplementationClosure(
  2437. const std::string& config) const
  2438. {
  2439. LinkImplClosure& tgts = this->LinkImplClosureMap[config];
  2440. if (!tgts.Done) {
  2441. tgts.Done = true;
  2442. std::set<cmGeneratorTarget const*> emitted;
  2443. cmLinkImplementationLibraries const* impl =
  2444. this->GetLinkImplementationLibraries(config);
  2445. for (cmLinkImplItem const& lib : impl->Libraries) {
  2446. processILibs(config, this, lib,
  2447. this->LocalGenerator->GetGlobalGenerator(), tgts, emitted);
  2448. }
  2449. }
  2450. return tgts;
  2451. }
  2452. class cmTargetTraceDependencies
  2453. {
  2454. public:
  2455. cmTargetTraceDependencies(cmGeneratorTarget* target);
  2456. void Trace();
  2457. private:
  2458. cmGeneratorTarget* GeneratorTarget;
  2459. cmMakefile* Makefile;
  2460. cmLocalGenerator* LocalGenerator;
  2461. cmGlobalGenerator const* GlobalGenerator;
  2462. using SourceEntry = cmGeneratorTarget::SourceEntry;
  2463. SourceEntry* CurrentEntry;
  2464. std::queue<cmSourceFile*> SourceQueue;
  2465. std::set<cmSourceFile*> SourcesQueued;
  2466. using NameMapType = std::map<std::string, cmSourcesWithOutput>;
  2467. NameMapType NameMap;
  2468. std::vector<std::string> NewSources;
  2469. void QueueSource(cmSourceFile* sf);
  2470. void FollowName(std::string const& name);
  2471. void FollowNames(std::vector<std::string> const& names);
  2472. bool IsUtility(std::string const& dep);
  2473. void CheckCustomCommand(cmCustomCommand const& cc);
  2474. void CheckCustomCommands(const std::vector<cmCustomCommand>& commands);
  2475. void FollowCommandDepends(cmCustomCommand const& cc,
  2476. const std::string& config,
  2477. std::set<std::string>& emitted);
  2478. };
  2479. cmTargetTraceDependencies::cmTargetTraceDependencies(cmGeneratorTarget* target)
  2480. : GeneratorTarget(target)
  2481. {
  2482. // Convenience.
  2483. this->Makefile = target->Target->GetMakefile();
  2484. this->LocalGenerator = target->GetLocalGenerator();
  2485. this->GlobalGenerator = this->LocalGenerator->GetGlobalGenerator();
  2486. this->CurrentEntry = nullptr;
  2487. // Queue all the source files already specified for the target.
  2488. if (target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  2489. std::set<cmSourceFile*> emitted;
  2490. std::vector<std::string> const& configs =
  2491. this->Makefile->GetGeneratorConfigs();
  2492. for (std::string const& c : configs) {
  2493. std::vector<cmSourceFile*> sources;
  2494. this->GeneratorTarget->GetSourceFiles(sources, c);
  2495. for (cmSourceFile* sf : sources) {
  2496. const std::set<cmGeneratorTarget const*> tgts =
  2497. this->GlobalGenerator->GetFilenameTargetDepends(sf);
  2498. if (cm::contains(tgts, this->GeneratorTarget)) {
  2499. std::ostringstream e;
  2500. e << "Evaluation output file\n \"" << sf->ResolveFullPath()
  2501. << "\"\ndepends on the sources of a target it is used in. This "
  2502. "is a dependency loop and is not allowed.";
  2503. this->GeneratorTarget->LocalGenerator->IssueMessage(
  2504. MessageType::FATAL_ERROR, e.str());
  2505. return;
  2506. }
  2507. if (emitted.insert(sf).second &&
  2508. this->SourcesQueued.insert(sf).second) {
  2509. this->SourceQueue.push(sf);
  2510. }
  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();
  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. "Uknown 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 tranformed 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. this->LanguageStandardMap[key] =
  3951. BT<std::string>(newRequiredStandard, f.Backtrace);
  3952. }
  3953. }
  3954. return true;
  3955. }
  3956. bool cmGeneratorTarget::ComputeCompileFeatures(
  3957. std::string const& config, std::set<LanguagePair> const& languagePairs) const
  3958. {
  3959. for (const auto& language : languagePairs) {
  3960. BT<std::string> const* generatorTargetLanguageStandard =
  3961. this->GetLanguageStandardProperty(language.first, config);
  3962. if (!generatorTargetLanguageStandard) {
  3963. // If the standard isn't explicitly set we copy it over from the
  3964. // specified paired language.
  3965. std::string key =
  3966. cmStrCat(cmSystemTools::UpperCase(config), '-', language.first);
  3967. BT<std::string> const* standardToCopy =
  3968. this->GetLanguageStandardProperty(language.second, config);
  3969. if (standardToCopy != nullptr) {
  3970. this->LanguageStandardMap[key] = *standardToCopy;
  3971. generatorTargetLanguageStandard = &this->LanguageStandardMap[key];
  3972. } else {
  3973. cmProp defaultStandard = this->Makefile->GetDef(
  3974. cmStrCat("CMAKE_", language.second, "_STANDARD_DEFAULT"));
  3975. if (defaultStandard != nullptr) {
  3976. this->LanguageStandardMap[key] = BT<std::string>(*defaultStandard);
  3977. generatorTargetLanguageStandard = &this->LanguageStandardMap[key];
  3978. }
  3979. }
  3980. // Custom updates for the CUDA standard.
  3981. if (generatorTargetLanguageStandard != nullptr &&
  3982. language.first == "CUDA") {
  3983. if (generatorTargetLanguageStandard->Value == "98") {
  3984. this->LanguageStandardMap[key].Value = "03";
  3985. }
  3986. }
  3987. }
  3988. }
  3989. return true;
  3990. }
  3991. std::string cmGeneratorTarget::GetImportedLibName(
  3992. std::string const& config) const
  3993. {
  3994. if (cmGeneratorTarget::ImportInfo const* info =
  3995. this->GetImportInfo(config)) {
  3996. return info->LibName;
  3997. }
  3998. return std::string();
  3999. }
  4000. std::string cmGeneratorTarget::GetFullPath(const std::string& config,
  4001. cmStateEnums::ArtifactType artifact,
  4002. bool realname) const
  4003. {
  4004. if (this->IsImported()) {
  4005. return this->Target->ImportedGetFullPath(config, artifact);
  4006. }
  4007. return this->NormalGetFullPath(config, artifact, realname);
  4008. }
  4009. std::string cmGeneratorTarget::NormalGetFullPath(
  4010. const std::string& config, cmStateEnums::ArtifactType artifact,
  4011. bool realname) const
  4012. {
  4013. std::string fpath = cmStrCat(this->GetDirectory(config, artifact), '/');
  4014. if (this->IsAppBundleOnApple()) {
  4015. fpath =
  4016. cmStrCat(this->BuildBundleDirectory(fpath, config, FullLevel), '/');
  4017. }
  4018. // Add the full name of the target.
  4019. switch (artifact) {
  4020. case cmStateEnums::RuntimeBinaryArtifact:
  4021. if (realname) {
  4022. fpath += this->NormalGetRealName(config);
  4023. } else {
  4024. fpath +=
  4025. this->GetFullName(config, cmStateEnums::RuntimeBinaryArtifact);
  4026. }
  4027. break;
  4028. case cmStateEnums::ImportLibraryArtifact:
  4029. fpath += this->GetFullName(config, cmStateEnums::ImportLibraryArtifact);
  4030. break;
  4031. }
  4032. return fpath;
  4033. }
  4034. std::string cmGeneratorTarget::NormalGetRealName(
  4035. const std::string& config) const
  4036. {
  4037. // This should not be called for imported targets.
  4038. // TODO: Split cmTarget into a class hierarchy to get compile-time
  4039. // enforcement of the limited imported target API.
  4040. if (this->IsImported()) {
  4041. std::string msg = cmStrCat("NormalGetRealName called on imported target: ",
  4042. this->GetName());
  4043. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  4044. }
  4045. if (this->GetType() == cmStateEnums::EXECUTABLE) {
  4046. // Compute the real name that will be built.
  4047. return this->GetExecutableNames(config).Real;
  4048. }
  4049. // Compute the real name that will be built.
  4050. return this->GetLibraryNames(config).Real;
  4051. }
  4052. cmGeneratorTarget::Names cmGeneratorTarget::GetLibraryNames(
  4053. const std::string& config) const
  4054. {
  4055. cmGeneratorTarget::Names targetNames;
  4056. // This should not be called for imported targets.
  4057. // TODO: Split cmTarget into a class hierarchy to get compile-time
  4058. // enforcement of the limited imported target API.
  4059. if (this->IsImported()) {
  4060. std::string msg =
  4061. cmStrCat("GetLibraryNames called on imported target: ", this->GetName());
  4062. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  4063. }
  4064. // Check for library version properties.
  4065. cmProp version = this->GetProperty("VERSION");
  4066. cmProp soversion = this->GetProperty("SOVERSION");
  4067. if (!this->HasSOName(config) ||
  4068. this->Makefile->IsOn("CMAKE_PLATFORM_NO_VERSIONED_SONAME") ||
  4069. this->IsFrameworkOnApple()) {
  4070. // Versioning is supported only for shared libraries and modules,
  4071. // and then only when the platform supports an soname flag.
  4072. version = nullptr;
  4073. soversion = nullptr;
  4074. }
  4075. if (version && !soversion) {
  4076. // The soversion must be set if the library version is set. Use
  4077. // the library version as the soversion.
  4078. soversion = version;
  4079. }
  4080. if (!version && soversion) {
  4081. // Use the soversion as the library version.
  4082. version = soversion;
  4083. }
  4084. // Get the components of the library name.
  4085. std::string prefix;
  4086. std::string suffix;
  4087. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  4088. prefix, targetNames.Base, suffix);
  4089. // The library name.
  4090. targetNames.Output = prefix + targetNames.Base + suffix;
  4091. if (this->IsFrameworkOnApple()) {
  4092. targetNames.Real = prefix;
  4093. if (!this->Makefile->PlatformIsAppleEmbedded()) {
  4094. targetNames.Real += "Versions/";
  4095. targetNames.Real += this->GetFrameworkVersion();
  4096. targetNames.Real += "/";
  4097. }
  4098. targetNames.Real += targetNames.Base + suffix;
  4099. targetNames.SharedObject = targetNames.Real + suffix;
  4100. } else {
  4101. // The library's soname.
  4102. this->ComputeVersionedName(targetNames.SharedObject, prefix,
  4103. targetNames.Base, suffix, targetNames.Output,
  4104. (soversion ? soversion->c_str() : nullptr));
  4105. // The library's real name on disk.
  4106. this->ComputeVersionedName(targetNames.Real, prefix, targetNames.Base,
  4107. suffix, targetNames.Output,
  4108. (version ? version->c_str() : nullptr));
  4109. }
  4110. // The import library name.
  4111. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  4112. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  4113. targetNames.ImportLibrary =
  4114. this->GetFullNameInternal(config, cmStateEnums::ImportLibraryArtifact);
  4115. }
  4116. // The program database file name.
  4117. targetNames.PDB = this->GetPDBName(config);
  4118. return targetNames;
  4119. }
  4120. cmGeneratorTarget::Names cmGeneratorTarget::GetExecutableNames(
  4121. const std::string& config) const
  4122. {
  4123. cmGeneratorTarget::Names targetNames;
  4124. // This should not be called for imported targets.
  4125. // TODO: Split cmTarget into a class hierarchy to get compile-time
  4126. // enforcement of the limited imported target API.
  4127. if (this->IsImported()) {
  4128. std::string msg = cmStrCat(
  4129. "GetExecutableNames called on imported target: ", this->GetName());
  4130. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  4131. }
  4132. // This versioning is supported only for executables and then only
  4133. // when the platform supports symbolic links.
  4134. #if defined(_WIN32) && !defined(__CYGWIN__)
  4135. const char* version = nullptr;
  4136. #else
  4137. // Check for executable version properties.
  4138. const char* version = nullptr;
  4139. if (cmProp p = this->GetProperty("VERSION")) {
  4140. version = p->c_str();
  4141. }
  4142. if (this->GetType() != cmStateEnums::EXECUTABLE ||
  4143. this->Makefile->IsOn("XCODE")) {
  4144. version = nullptr;
  4145. }
  4146. #endif
  4147. // Get the components of the executable name.
  4148. std::string prefix;
  4149. std::string suffix;
  4150. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  4151. prefix, targetNames.Base, suffix);
  4152. // The executable name.
  4153. targetNames.Output = prefix + targetNames.Base + suffix;
  4154. // The executable's real name on disk.
  4155. #if defined(__CYGWIN__)
  4156. targetNames.Real = prefix + targetNames.Base;
  4157. #else
  4158. targetNames.Real = targetNames.Output;
  4159. #endif
  4160. if (version) {
  4161. targetNames.Real += "-";
  4162. targetNames.Real += version;
  4163. }
  4164. #if defined(__CYGWIN__)
  4165. targetNames.Real += suffix;
  4166. #endif
  4167. // The import library name.
  4168. targetNames.ImportLibrary =
  4169. this->GetFullNameInternal(config, cmStateEnums::ImportLibraryArtifact);
  4170. // The program database file name.
  4171. targetNames.PDB = this->GetPDBName(config);
  4172. return targetNames;
  4173. }
  4174. std::string cmGeneratorTarget::GetFullNameInternal(
  4175. const std::string& config, cmStateEnums::ArtifactType artifact) const
  4176. {
  4177. std::string prefix;
  4178. std::string base;
  4179. std::string suffix;
  4180. this->GetFullNameInternal(config, artifact, prefix, base, suffix);
  4181. return prefix + base + suffix;
  4182. }
  4183. std::string cmGeneratorTarget::ImportedGetLocation(
  4184. const std::string& config) const
  4185. {
  4186. assert(this->IsImported());
  4187. return this->Target->ImportedGetFullPath(
  4188. config, cmStateEnums::RuntimeBinaryArtifact);
  4189. }
  4190. std::string cmGeneratorTarget::GetFullNameImported(
  4191. const std::string& config, cmStateEnums::ArtifactType artifact) const
  4192. {
  4193. return cmSystemTools::GetFilenameName(
  4194. this->Target->ImportedGetFullPath(config, artifact));
  4195. }
  4196. void cmGeneratorTarget::GetFullNameInternal(
  4197. const std::string& config, cmStateEnums::ArtifactType artifact,
  4198. std::string& outPrefix, std::string& outBase, std::string& outSuffix) const
  4199. {
  4200. // Use just the target name for non-main target types.
  4201. if (this->GetType() != cmStateEnums::STATIC_LIBRARY &&
  4202. this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  4203. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  4204. this->GetType() != cmStateEnums::EXECUTABLE) {
  4205. outPrefix.clear();
  4206. outBase = this->GetName();
  4207. outSuffix.clear();
  4208. return;
  4209. }
  4210. const bool isImportedLibraryArtifact =
  4211. (artifact == cmStateEnums::ImportLibraryArtifact);
  4212. // Return an empty name for the import library if this platform
  4213. // does not support import libraries.
  4214. if (isImportedLibraryArtifact && !this->NeedImportLibraryName(config)) {
  4215. outPrefix.clear();
  4216. outBase.clear();
  4217. outSuffix.clear();
  4218. return;
  4219. }
  4220. // retrieve prefix and suffix
  4221. std::string ll = this->GetLinkerLanguage(config);
  4222. const char* targetPrefix = this->GetFilePrefixInternal(config, artifact, ll);
  4223. const char* targetSuffix = this->GetFileSuffixInternal(config, artifact, ll);
  4224. // The implib option is only allowed for shared libraries, module
  4225. // libraries, and executables.
  4226. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  4227. this->GetType() != cmStateEnums::MODULE_LIBRARY &&
  4228. this->GetType() != cmStateEnums::EXECUTABLE) {
  4229. artifact = cmStateEnums::RuntimeBinaryArtifact;
  4230. }
  4231. // Compute the full name for main target types.
  4232. const std::string configPostfix = this->GetFilePostfix(config);
  4233. // frameworks have directory prefix but no suffix
  4234. std::string fw_prefix;
  4235. if (this->IsFrameworkOnApple()) {
  4236. fw_prefix =
  4237. cmStrCat(this->GetFrameworkDirectory(config, ContentLevel), '/');
  4238. targetPrefix = fw_prefix.c_str();
  4239. targetSuffix = nullptr;
  4240. }
  4241. if (this->IsCFBundleOnApple()) {
  4242. fw_prefix = cmStrCat(this->GetCFBundleDirectory(config, FullLevel), '/');
  4243. targetPrefix = fw_prefix.c_str();
  4244. targetSuffix = nullptr;
  4245. }
  4246. // Begin the final name with the prefix.
  4247. outPrefix = targetPrefix ? targetPrefix : "";
  4248. // Append the target name or property-specified name.
  4249. outBase += this->GetOutputName(config, artifact);
  4250. // Append the per-configuration postfix.
  4251. // When using Xcode, the postfix should be part of the suffix rather than
  4252. // the base, because the suffix ends up being used in Xcode's
  4253. // EXECUTABLE_SUFFIX attribute.
  4254. if (this->IsFrameworkOnApple() &&
  4255. GetGlobalGenerator()->GetName() == "Xcode") {
  4256. targetSuffix = configPostfix.c_str();
  4257. } else {
  4258. outBase += configPostfix;
  4259. }
  4260. // Name shared libraries with their version number on some platforms.
  4261. if (cmProp soversion = this->GetProperty("SOVERSION")) {
  4262. if (this->GetType() == cmStateEnums::SHARED_LIBRARY &&
  4263. !isImportedLibraryArtifact &&
  4264. this->Makefile->IsOn("CMAKE_SHARED_LIBRARY_NAME_WITH_VERSION")) {
  4265. outBase += "-";
  4266. outBase += *soversion;
  4267. }
  4268. }
  4269. // Append the suffix.
  4270. outSuffix = targetSuffix ? targetSuffix : "";
  4271. }
  4272. std::string cmGeneratorTarget::GetLinkerLanguage(
  4273. const std::string& config) const
  4274. {
  4275. return this->GetLinkClosure(config)->LinkerLanguage;
  4276. }
  4277. std::string cmGeneratorTarget::GetPDBOutputName(
  4278. const std::string& config) const
  4279. {
  4280. std::string base =
  4281. this->GetOutputName(config, cmStateEnums::RuntimeBinaryArtifact);
  4282. std::vector<std::string> props;
  4283. std::string configUpper = cmSystemTools::UpperCase(config);
  4284. if (!configUpper.empty()) {
  4285. // PDB_NAME_<CONFIG>
  4286. props.push_back("PDB_NAME_" + configUpper);
  4287. }
  4288. // PDB_NAME
  4289. props.emplace_back("PDB_NAME");
  4290. for (std::string const& p : props) {
  4291. if (cmProp outName = this->GetProperty(p)) {
  4292. base = *outName;
  4293. break;
  4294. }
  4295. }
  4296. return base;
  4297. }
  4298. std::string cmGeneratorTarget::GetPDBName(const std::string& config) const
  4299. {
  4300. std::string prefix;
  4301. std::string base;
  4302. std::string suffix;
  4303. this->GetFullNameInternal(config, cmStateEnums::RuntimeBinaryArtifact,
  4304. prefix, base, suffix);
  4305. std::vector<std::string> props;
  4306. std::string configUpper = cmSystemTools::UpperCase(config);
  4307. if (!configUpper.empty()) {
  4308. // PDB_NAME_<CONFIG>
  4309. props.push_back("PDB_NAME_" + configUpper);
  4310. }
  4311. // PDB_NAME
  4312. props.emplace_back("PDB_NAME");
  4313. for (std::string const& p : props) {
  4314. if (cmProp outName = this->GetProperty(p)) {
  4315. base = *outName;
  4316. break;
  4317. }
  4318. }
  4319. return prefix + base + ".pdb";
  4320. }
  4321. std::string cmGeneratorTarget::GetObjectDirectory(
  4322. std::string const& config) const
  4323. {
  4324. std::string obj_dir =
  4325. this->GlobalGenerator->ExpandCFGIntDir(this->ObjectDirectory, config);
  4326. #if defined(__APPLE__)
  4327. // find and replace $(PROJECT_NAME) xcode placeholder
  4328. const std::string projectName = this->LocalGenerator->GetProjectName();
  4329. cmSystemTools::ReplaceString(obj_dir, "$(PROJECT_NAME)", projectName);
  4330. // Replace Xcode's placeholder for the object file directory since
  4331. // installation and export scripts need to know the real directory.
  4332. // Xcode has build-time settings (e.g. for sanitizers) that affect this,
  4333. // but we use the default here. Users that want to enable sanitizers
  4334. // will do so at the cost of object library installation and export.
  4335. cmSystemTools::ReplaceString(obj_dir, "$(OBJECT_FILE_DIR_normal:base)",
  4336. "Objects-normal");
  4337. #endif
  4338. return obj_dir;
  4339. }
  4340. void cmGeneratorTarget::GetTargetObjectNames(
  4341. std::string const& config, std::vector<std::string>& objects) const
  4342. {
  4343. std::vector<cmSourceFile const*> objectSources;
  4344. this->GetObjectSources(objectSources, config);
  4345. std::map<cmSourceFile const*, std::string> mapping;
  4346. for (cmSourceFile const* sf : objectSources) {
  4347. mapping[sf];
  4348. }
  4349. this->LocalGenerator->ComputeObjectFilenames(mapping, this);
  4350. for (cmSourceFile const* src : objectSources) {
  4351. // Find the object file name corresponding to this source file.
  4352. auto map_it = mapping.find(src);
  4353. // It must exist because we populated the mapping just above.
  4354. assert(!map_it->second.empty());
  4355. objects.push_back(map_it->second);
  4356. }
  4357. }
  4358. bool cmGeneratorTarget::StrictTargetComparison::operator()(
  4359. cmGeneratorTarget const* t1, cmGeneratorTarget const* t2) const
  4360. {
  4361. int nameResult = strcmp(t1->GetName().c_str(), t2->GetName().c_str());
  4362. if (nameResult == 0) {
  4363. return strcmp(
  4364. t1->GetLocalGenerator()->GetCurrentBinaryDirectory().c_str(),
  4365. t2->GetLocalGenerator()->GetCurrentBinaryDirectory().c_str()) < 0;
  4366. }
  4367. return nameResult < 0;
  4368. }
  4369. struct cmGeneratorTarget::SourceFileFlags
  4370. cmGeneratorTarget::GetTargetSourceFileFlags(const cmSourceFile* sf) const
  4371. {
  4372. struct SourceFileFlags flags;
  4373. this->ConstructSourceFileFlags();
  4374. auto si = this->SourceFlagsMap.find(sf);
  4375. if (si != this->SourceFlagsMap.end()) {
  4376. flags = si->second;
  4377. } else {
  4378. // Handle the MACOSX_PACKAGE_LOCATION property on source files that
  4379. // were not listed in one of the other lists.
  4380. if (cmProp location = sf->GetProperty("MACOSX_PACKAGE_LOCATION")) {
  4381. flags.MacFolder = location->c_str();
  4382. const bool stripResources =
  4383. this->GlobalGenerator->ShouldStripResourcePath(this->Makefile);
  4384. if (*location == "Resources") {
  4385. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  4386. if (stripResources) {
  4387. flags.MacFolder = "";
  4388. }
  4389. } else if (cmHasLiteralPrefix(*location, "Resources/")) {
  4390. flags.Type = cmGeneratorTarget::SourceFileTypeDeepResource;
  4391. if (stripResources) {
  4392. flags.MacFolder += strlen("Resources/");
  4393. }
  4394. } else {
  4395. flags.Type = cmGeneratorTarget::SourceFileTypeMacContent;
  4396. }
  4397. }
  4398. }
  4399. return flags;
  4400. }
  4401. void cmGeneratorTarget::ConstructSourceFileFlags() const
  4402. {
  4403. if (this->SourceFileFlagsConstructed) {
  4404. return;
  4405. }
  4406. this->SourceFileFlagsConstructed = true;
  4407. // Process public headers to mark the source files.
  4408. if (cmProp files = this->GetProperty("PUBLIC_HEADER")) {
  4409. std::vector<std::string> relFiles = cmExpandedList(*files);
  4410. for (std::string const& relFile : relFiles) {
  4411. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  4412. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  4413. flags.MacFolder = "Headers";
  4414. flags.Type = cmGeneratorTarget::SourceFileTypePublicHeader;
  4415. }
  4416. }
  4417. }
  4418. // Process private headers after public headers so that they take
  4419. // precedence if a file is listed in both.
  4420. if (cmProp files = this->GetProperty("PRIVATE_HEADER")) {
  4421. std::vector<std::string> relFiles = cmExpandedList(*files);
  4422. for (std::string const& relFile : relFiles) {
  4423. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  4424. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  4425. flags.MacFolder = "PrivateHeaders";
  4426. flags.Type = cmGeneratorTarget::SourceFileTypePrivateHeader;
  4427. }
  4428. }
  4429. }
  4430. // Mark sources listed as resources.
  4431. if (cmProp files = this->GetProperty("RESOURCE")) {
  4432. std::vector<std::string> relFiles = cmExpandedList(*files);
  4433. for (std::string const& relFile : relFiles) {
  4434. if (cmSourceFile* sf = this->Makefile->GetSource(relFile)) {
  4435. SourceFileFlags& flags = this->SourceFlagsMap[sf];
  4436. flags.MacFolder = "";
  4437. if (!this->GlobalGenerator->ShouldStripResourcePath(this->Makefile)) {
  4438. flags.MacFolder = "Resources";
  4439. }
  4440. flags.Type = cmGeneratorTarget::SourceFileTypeResource;
  4441. }
  4442. }
  4443. }
  4444. }
  4445. const cmGeneratorTarget::CompatibleInterfacesBase&
  4446. cmGeneratorTarget::GetCompatibleInterfaces(std::string const& config) const
  4447. {
  4448. cmGeneratorTarget::CompatibleInterfaces& compat =
  4449. this->CompatibleInterfacesMap[config];
  4450. if (!compat.Done) {
  4451. compat.Done = true;
  4452. compat.PropsBool.insert("POSITION_INDEPENDENT_CODE");
  4453. compat.PropsString.insert("AUTOUIC_OPTIONS");
  4454. std::vector<cmGeneratorTarget const*> const& deps =
  4455. this->GetLinkImplementationClosure(config);
  4456. for (cmGeneratorTarget const* li : deps) {
  4457. #define CM_READ_COMPATIBLE_INTERFACE(X, x) \
  4458. if (cmProp prop = li->GetProperty("COMPATIBLE_INTERFACE_" #X)) { \
  4459. std::vector<std::string> props; \
  4460. cmExpandList(*prop, props); \
  4461. compat.Props##x.insert(props.begin(), props.end()); \
  4462. }
  4463. CM_READ_COMPATIBLE_INTERFACE(BOOL, Bool)
  4464. CM_READ_COMPATIBLE_INTERFACE(STRING, String)
  4465. CM_READ_COMPATIBLE_INTERFACE(NUMBER_MIN, NumberMin)
  4466. CM_READ_COMPATIBLE_INTERFACE(NUMBER_MAX, NumberMax)
  4467. #undef CM_READ_COMPATIBLE_INTERFACE
  4468. }
  4469. }
  4470. return compat;
  4471. }
  4472. bool cmGeneratorTarget::IsLinkInterfaceDependentBoolProperty(
  4473. const std::string& p, const std::string& config) const
  4474. {
  4475. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  4476. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  4477. return false;
  4478. }
  4479. return this->GetCompatibleInterfaces(config).PropsBool.count(p) > 0;
  4480. }
  4481. bool cmGeneratorTarget::IsLinkInterfaceDependentStringProperty(
  4482. const std::string& p, const std::string& config) const
  4483. {
  4484. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  4485. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  4486. return false;
  4487. }
  4488. return this->GetCompatibleInterfaces(config).PropsString.count(p) > 0;
  4489. }
  4490. bool cmGeneratorTarget::IsLinkInterfaceDependentNumberMinProperty(
  4491. const std::string& p, const std::string& config) const
  4492. {
  4493. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  4494. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  4495. return false;
  4496. }
  4497. return this->GetCompatibleInterfaces(config).PropsNumberMin.count(p) > 0;
  4498. }
  4499. bool cmGeneratorTarget::IsLinkInterfaceDependentNumberMaxProperty(
  4500. const std::string& p, const std::string& config) const
  4501. {
  4502. if (this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  4503. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  4504. return false;
  4505. }
  4506. return this->GetCompatibleInterfaces(config).PropsNumberMax.count(p) > 0;
  4507. }
  4508. enum CompatibleType
  4509. {
  4510. BoolType,
  4511. StringType,
  4512. NumberMinType,
  4513. NumberMaxType
  4514. };
  4515. template <typename PropertyType>
  4516. PropertyType getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  4517. const std::string& prop,
  4518. const std::string& config,
  4519. CompatibleType, PropertyType*);
  4520. template <>
  4521. bool getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  4522. const std::string& prop,
  4523. const std::string& config,
  4524. CompatibleType /*unused*/,
  4525. bool* /*unused*/)
  4526. {
  4527. return tgt->GetLinkInterfaceDependentBoolProperty(prop, config);
  4528. }
  4529. template <>
  4530. const char* getLinkInterfaceDependentProperty(cmGeneratorTarget const* tgt,
  4531. const std::string& prop,
  4532. const std::string& config,
  4533. CompatibleType t,
  4534. const char** /*unused*/)
  4535. {
  4536. switch (t) {
  4537. case BoolType:
  4538. assert(false &&
  4539. "String compatibility check function called for boolean");
  4540. return nullptr;
  4541. case StringType:
  4542. return tgt->GetLinkInterfaceDependentStringProperty(prop, config);
  4543. case NumberMinType:
  4544. return tgt->GetLinkInterfaceDependentNumberMinProperty(prop, config);
  4545. case NumberMaxType:
  4546. return tgt->GetLinkInterfaceDependentNumberMaxProperty(prop, config);
  4547. }
  4548. assert(false && "Unreachable!");
  4549. return nullptr;
  4550. }
  4551. template <typename PropertyType>
  4552. void checkPropertyConsistency(cmGeneratorTarget const* depender,
  4553. cmGeneratorTarget const* dependee,
  4554. const std::string& propName,
  4555. std::set<std::string>& emitted,
  4556. const std::string& config, CompatibleType t,
  4557. PropertyType* /*unused*/)
  4558. {
  4559. cmProp prop = dependee->GetProperty(propName);
  4560. if (!prop) {
  4561. return;
  4562. }
  4563. std::vector<std::string> props = cmExpandedList(*prop);
  4564. std::string pdir =
  4565. cmStrCat(cmSystemTools::GetCMakeRoot(), "/Help/prop_tgt/");
  4566. for (std::string const& p : props) {
  4567. std::string pname = cmSystemTools::HelpFileName(p);
  4568. std::string pfile = pdir + pname + ".rst";
  4569. if (cmSystemTools::FileExists(pfile, true)) {
  4570. std::ostringstream e;
  4571. e << "Target \"" << dependee->GetName() << "\" has property \"" << p
  4572. << "\" listed in its " << propName
  4573. << " property. "
  4574. "This is not allowed. Only user-defined properties may appear "
  4575. "listed in the "
  4576. << propName << " property.";
  4577. depender->GetLocalGenerator()->IssueMessage(MessageType::FATAL_ERROR,
  4578. e.str());
  4579. return;
  4580. }
  4581. if (emitted.insert(p).second) {
  4582. getLinkInterfaceDependentProperty<PropertyType>(depender, p, config, t,
  4583. nullptr);
  4584. if (cmSystemTools::GetErrorOccuredFlag()) {
  4585. return;
  4586. }
  4587. }
  4588. }
  4589. }
  4590. namespace {
  4591. std::string intersect(const std::set<std::string>& s1,
  4592. const std::set<std::string>& s2)
  4593. {
  4594. std::set<std::string> intersect;
  4595. std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(),
  4596. std::inserter(intersect, intersect.begin()));
  4597. if (!intersect.empty()) {
  4598. return *intersect.begin();
  4599. }
  4600. return "";
  4601. }
  4602. std::string intersect(const std::set<std::string>& s1,
  4603. const std::set<std::string>& s2,
  4604. const std::set<std::string>& s3)
  4605. {
  4606. std::string result;
  4607. result = intersect(s1, s2);
  4608. if (!result.empty()) {
  4609. return result;
  4610. }
  4611. result = intersect(s1, s3);
  4612. if (!result.empty()) {
  4613. return result;
  4614. }
  4615. return intersect(s2, s3);
  4616. }
  4617. std::string intersect(const std::set<std::string>& s1,
  4618. const std::set<std::string>& s2,
  4619. const std::set<std::string>& s3,
  4620. const std::set<std::string>& s4)
  4621. {
  4622. std::string result;
  4623. result = intersect(s1, s2);
  4624. if (!result.empty()) {
  4625. return result;
  4626. }
  4627. result = intersect(s1, s3);
  4628. if (!result.empty()) {
  4629. return result;
  4630. }
  4631. result = intersect(s1, s4);
  4632. if (!result.empty()) {
  4633. return result;
  4634. }
  4635. return intersect(s2, s3, s4);
  4636. }
  4637. }
  4638. void cmGeneratorTarget::CheckPropertyCompatibility(
  4639. cmComputeLinkInformation& info, const std::string& config) const
  4640. {
  4641. const cmComputeLinkInformation::ItemVector& deps = info.GetItems();
  4642. std::set<std::string> emittedBools;
  4643. static const std::string strBool = "COMPATIBLE_INTERFACE_BOOL";
  4644. std::set<std::string> emittedStrings;
  4645. static const std::string strString = "COMPATIBLE_INTERFACE_STRING";
  4646. std::set<std::string> emittedMinNumbers;
  4647. static const std::string strNumMin = "COMPATIBLE_INTERFACE_NUMBER_MIN";
  4648. std::set<std::string> emittedMaxNumbers;
  4649. static const std::string strNumMax = "COMPATIBLE_INTERFACE_NUMBER_MAX";
  4650. for (auto const& dep : deps) {
  4651. if (!dep.Target) {
  4652. continue;
  4653. }
  4654. checkPropertyConsistency<bool>(this, dep.Target, strBool, emittedBools,
  4655. config, BoolType, nullptr);
  4656. if (cmSystemTools::GetErrorOccuredFlag()) {
  4657. return;
  4658. }
  4659. checkPropertyConsistency<const char*>(this, dep.Target, strString,
  4660. emittedStrings, config, StringType,
  4661. nullptr);
  4662. if (cmSystemTools::GetErrorOccuredFlag()) {
  4663. return;
  4664. }
  4665. checkPropertyConsistency<const char*>(this, dep.Target, strNumMin,
  4666. emittedMinNumbers, config,
  4667. NumberMinType, nullptr);
  4668. if (cmSystemTools::GetErrorOccuredFlag()) {
  4669. return;
  4670. }
  4671. checkPropertyConsistency<const char*>(this, dep.Target, strNumMax,
  4672. emittedMaxNumbers, config,
  4673. NumberMaxType, nullptr);
  4674. if (cmSystemTools::GetErrorOccuredFlag()) {
  4675. return;
  4676. }
  4677. }
  4678. std::string prop = intersect(emittedBools, emittedStrings, emittedMinNumbers,
  4679. emittedMaxNumbers);
  4680. if (!prop.empty()) {
  4681. // Use a sorted std::vector to keep the error message sorted.
  4682. std::vector<std::string> props;
  4683. auto i = emittedBools.find(prop);
  4684. if (i != emittedBools.end()) {
  4685. props.push_back(strBool);
  4686. }
  4687. i = emittedStrings.find(prop);
  4688. if (i != emittedStrings.end()) {
  4689. props.push_back(strString);
  4690. }
  4691. i = emittedMinNumbers.find(prop);
  4692. if (i != emittedMinNumbers.end()) {
  4693. props.push_back(strNumMin);
  4694. }
  4695. i = emittedMaxNumbers.find(prop);
  4696. if (i != emittedMaxNumbers.end()) {
  4697. props.push_back(strNumMax);
  4698. }
  4699. std::sort(props.begin(), props.end());
  4700. std::string propsString = cmStrCat(
  4701. cmJoin(cmMakeRange(props).retreat(1), ", "), " and the ", props.back());
  4702. std::ostringstream e;
  4703. e << "Property \"" << prop << "\" appears in both the " << propsString
  4704. << " property in the dependencies of target \"" << this->GetName()
  4705. << "\". This is not allowed. A property may only require "
  4706. "compatibility "
  4707. "in a boolean interpretation, a numeric minimum, a numeric maximum "
  4708. "or a "
  4709. "string interpretation, but not a mixture.";
  4710. this->LocalGenerator->IssueMessage(MessageType::FATAL_ERROR, e.str());
  4711. }
  4712. }
  4713. template <typename PropertyType>
  4714. std::string valueAsString(PropertyType);
  4715. template <>
  4716. std::string valueAsString<bool>(bool value)
  4717. {
  4718. return value ? "TRUE" : "FALSE";
  4719. }
  4720. template <>
  4721. std::string valueAsString<const char*>(const char* value)
  4722. {
  4723. return value ? value : "(unset)";
  4724. }
  4725. template <>
  4726. std::string valueAsString<std::string>(std::string value)
  4727. {
  4728. return value;
  4729. }
  4730. template <>
  4731. std::string valueAsString<std::nullptr_t>(std::nullptr_t /*unused*/)
  4732. {
  4733. return "(unset)";
  4734. }
  4735. std::string compatibilityType(CompatibleType t)
  4736. {
  4737. switch (t) {
  4738. case BoolType:
  4739. return "Boolean compatibility";
  4740. case StringType:
  4741. return "String compatibility";
  4742. case NumberMaxType:
  4743. return "Numeric maximum compatibility";
  4744. case NumberMinType:
  4745. return "Numeric minimum compatibility";
  4746. }
  4747. assert(false && "Unreachable!");
  4748. return "";
  4749. }
  4750. std::string compatibilityAgree(CompatibleType t, bool dominant)
  4751. {
  4752. switch (t) {
  4753. case BoolType:
  4754. case StringType:
  4755. return dominant ? "(Disagree)\n" : "(Agree)\n";
  4756. case NumberMaxType:
  4757. case NumberMinType:
  4758. return dominant ? "(Dominant)\n" : "(Ignored)\n";
  4759. }
  4760. assert(false && "Unreachable!");
  4761. return "";
  4762. }
  4763. template <typename PropertyType>
  4764. PropertyType getTypedProperty(
  4765. cmGeneratorTarget const* tgt, const std::string& prop,
  4766. cmGeneratorExpressionInterpreter* genexInterpreter = nullptr);
  4767. template <>
  4768. bool getTypedProperty<bool>(cmGeneratorTarget const* tgt,
  4769. const std::string& prop,
  4770. cmGeneratorExpressionInterpreter* genexInterpreter)
  4771. {
  4772. if (genexInterpreter == nullptr) {
  4773. return tgt->GetPropertyAsBool(prop);
  4774. }
  4775. cmProp value = tgt->GetProperty(prop);
  4776. return cmIsOn(
  4777. genexInterpreter->Evaluate(value ? value->c_str() : nullptr, prop));
  4778. }
  4779. template <>
  4780. const char* getTypedProperty<const char*>(
  4781. cmGeneratorTarget const* tgt, const std::string& prop,
  4782. cmGeneratorExpressionInterpreter* genexInterpreter)
  4783. {
  4784. cmProp value = tgt->GetProperty(prop);
  4785. if (genexInterpreter == nullptr) {
  4786. return value ? value->c_str() : nullptr;
  4787. }
  4788. return genexInterpreter->Evaluate(value ? value->c_str() : nullptr, prop)
  4789. .c_str();
  4790. }
  4791. template <>
  4792. std::string getTypedProperty<std::string>(
  4793. cmGeneratorTarget const* tgt, const std::string& prop,
  4794. cmGeneratorExpressionInterpreter* genexInterpreter)
  4795. {
  4796. cmProp value = tgt->GetProperty(prop);
  4797. if (genexInterpreter == nullptr) {
  4798. return valueAsString(value ? value->c_str() : nullptr);
  4799. }
  4800. return genexInterpreter->Evaluate(value ? value->c_str() : nullptr, prop);
  4801. }
  4802. template <typename PropertyType>
  4803. PropertyType impliedValue(PropertyType);
  4804. template <>
  4805. bool impliedValue<bool>(bool /*unused*/)
  4806. {
  4807. return false;
  4808. }
  4809. template <>
  4810. const char* impliedValue<const char*>(const char* /*unused*/)
  4811. {
  4812. return "";
  4813. }
  4814. template <>
  4815. std::string impliedValue<std::string>(std::string /*unused*/) // NOLINT(*)
  4816. {
  4817. return std::string();
  4818. }
  4819. template <typename PropertyType>
  4820. std::pair<bool, PropertyType> consistentProperty(PropertyType lhs,
  4821. PropertyType rhs,
  4822. CompatibleType t);
  4823. template <>
  4824. std::pair<bool, bool> consistentProperty(bool lhs, bool rhs,
  4825. CompatibleType /*unused*/)
  4826. {
  4827. return { lhs == rhs, lhs };
  4828. }
  4829. std::pair<bool, const char*> consistentStringProperty(const char* lhs,
  4830. const char* rhs)
  4831. {
  4832. const bool b = strcmp(lhs, rhs) == 0;
  4833. return { b, b ? lhs : nullptr };
  4834. }
  4835. std::pair<bool, std::string> consistentStringProperty(const std::string& lhs,
  4836. const std::string& rhs)
  4837. {
  4838. const bool b = lhs == rhs;
  4839. return { b, b ? lhs : valueAsString(nullptr) };
  4840. }
  4841. std::pair<bool, const char*> consistentNumberProperty(const char* lhs,
  4842. const char* rhs,
  4843. CompatibleType t)
  4844. {
  4845. char* pEnd;
  4846. long lnum = strtol(lhs, &pEnd, 0);
  4847. if (pEnd == lhs || *pEnd != '\0' || errno == ERANGE) {
  4848. return { false, nullptr };
  4849. }
  4850. long rnum = strtol(rhs, &pEnd, 0);
  4851. if (pEnd == rhs || *pEnd != '\0' || errno == ERANGE) {
  4852. return { false, nullptr };
  4853. }
  4854. if (t == NumberMaxType) {
  4855. return { true, std::max(lnum, rnum) == lnum ? lhs : rhs };
  4856. }
  4857. return { true, std::min(lnum, rnum) == lnum ? lhs : rhs };
  4858. }
  4859. template <>
  4860. std::pair<bool, const char*> consistentProperty(const char* lhs,
  4861. const char* rhs,
  4862. CompatibleType t)
  4863. {
  4864. if (!lhs && !rhs) {
  4865. return { true, lhs };
  4866. }
  4867. if (!lhs) {
  4868. return { true, rhs };
  4869. }
  4870. if (!rhs) {
  4871. return { true, lhs };
  4872. }
  4873. switch (t) {
  4874. case BoolType: {
  4875. bool same = cmIsOn(lhs) == cmIsOn(rhs);
  4876. return { same, same ? lhs : nullptr };
  4877. }
  4878. case StringType:
  4879. return consistentStringProperty(lhs, rhs);
  4880. case NumberMinType:
  4881. case NumberMaxType:
  4882. return consistentNumberProperty(lhs, rhs, t);
  4883. }
  4884. assert(false && "Unreachable!");
  4885. return { false, nullptr };
  4886. }
  4887. std::pair<bool, std::string> consistentProperty(const std::string& lhs,
  4888. const std::string& rhs,
  4889. CompatibleType t)
  4890. {
  4891. const std::string null_ptr = valueAsString(nullptr);
  4892. if (lhs == null_ptr && rhs == null_ptr) {
  4893. return { true, lhs };
  4894. }
  4895. if (lhs == null_ptr) {
  4896. return { true, rhs };
  4897. }
  4898. if (rhs == null_ptr) {
  4899. return { true, lhs };
  4900. }
  4901. switch (t) {
  4902. case BoolType: {
  4903. bool same = cmIsOn(lhs) == cmIsOn(rhs);
  4904. return { same, same ? lhs : null_ptr };
  4905. }
  4906. case StringType:
  4907. return consistentStringProperty(lhs, rhs);
  4908. case NumberMinType:
  4909. case NumberMaxType: {
  4910. auto value = consistentNumberProperty(lhs.c_str(), rhs.c_str(), t);
  4911. return { value.first,
  4912. value.first ? std::string(value.second) : null_ptr };
  4913. }
  4914. }
  4915. assert(false && "Unreachable!");
  4916. return { false, null_ptr };
  4917. }
  4918. template <typename PropertyType>
  4919. PropertyType checkInterfacePropertyCompatibility(cmGeneratorTarget const* tgt,
  4920. const std::string& p,
  4921. const std::string& config,
  4922. const char* defaultValue,
  4923. CompatibleType t,
  4924. PropertyType* /*unused*/)
  4925. {
  4926. PropertyType propContent = getTypedProperty<PropertyType>(tgt, p);
  4927. std::vector<std::string> headPropKeys = tgt->GetPropertyKeys();
  4928. const bool explicitlySet = cm::contains(headPropKeys, p);
  4929. const bool impliedByUse = tgt->IsNullImpliedByLinkLibraries(p);
  4930. assert((impliedByUse ^ explicitlySet) || (!impliedByUse && !explicitlySet));
  4931. std::vector<cmGeneratorTarget const*> const& deps =
  4932. tgt->GetLinkImplementationClosure(config);
  4933. if (deps.empty()) {
  4934. return propContent;
  4935. }
  4936. bool propInitialized = explicitlySet;
  4937. std::string report = cmStrCat(" * Target \"", tgt->GetName());
  4938. if (explicitlySet) {
  4939. report += "\" has property content \"";
  4940. report += valueAsString<PropertyType>(propContent);
  4941. report += "\"\n";
  4942. } else if (impliedByUse) {
  4943. report += "\" property is implied by use.\n";
  4944. } else {
  4945. report += "\" property not set.\n";
  4946. }
  4947. std::string interfaceProperty = "INTERFACE_" + p;
  4948. std::unique_ptr<cmGeneratorExpressionInterpreter> genexInterpreter;
  4949. if (p == "POSITION_INDEPENDENT_CODE") {
  4950. genexInterpreter = cm::make_unique<cmGeneratorExpressionInterpreter>(
  4951. tgt->GetLocalGenerator(), config, tgt);
  4952. }
  4953. for (cmGeneratorTarget const* theTarget : deps) {
  4954. // An error should be reported if one dependency
  4955. // has INTERFACE_POSITION_INDEPENDENT_CODE ON and the other
  4956. // has INTERFACE_POSITION_INDEPENDENT_CODE OFF, or if the
  4957. // target itself has a POSITION_INDEPENDENT_CODE which disagrees
  4958. // with a dependency.
  4959. std::vector<std::string> propKeys = theTarget->GetPropertyKeys();
  4960. const bool ifaceIsSet = cm::contains(propKeys, interfaceProperty);
  4961. PropertyType ifacePropContent = getTypedProperty<PropertyType>(
  4962. theTarget, interfaceProperty, genexInterpreter.get());
  4963. std::string reportEntry;
  4964. if (ifaceIsSet) {
  4965. reportEntry += " * Target \"";
  4966. reportEntry += theTarget->GetName();
  4967. reportEntry += "\" property value \"";
  4968. reportEntry += valueAsString<PropertyType>(ifacePropContent);
  4969. reportEntry += "\" ";
  4970. }
  4971. if (explicitlySet) {
  4972. if (ifaceIsSet) {
  4973. std::pair<bool, PropertyType> consistent =
  4974. consistentProperty(propContent, ifacePropContent, t);
  4975. report += reportEntry;
  4976. report += compatibilityAgree(t, propContent != consistent.second);
  4977. if (!consistent.first) {
  4978. std::ostringstream e;
  4979. e << "Property " << p << " on target \"" << tgt->GetName()
  4980. << "\" does\nnot match the "
  4981. "INTERFACE_"
  4982. << p
  4983. << " property requirement\nof "
  4984. "dependency \""
  4985. << theTarget->GetName() << "\".\n";
  4986. cmSystemTools::Error(e.str());
  4987. break;
  4988. }
  4989. propContent = consistent.second;
  4990. continue;
  4991. }
  4992. // Explicitly set on target and not set in iface. Can't disagree.
  4993. continue;
  4994. }
  4995. if (impliedByUse) {
  4996. propContent = impliedValue<PropertyType>(propContent);
  4997. if (ifaceIsSet) {
  4998. std::pair<bool, PropertyType> consistent =
  4999. consistentProperty(propContent, ifacePropContent, t);
  5000. report += reportEntry;
  5001. report += compatibilityAgree(t, propContent != consistent.second);
  5002. if (!consistent.first) {
  5003. std::ostringstream e;
  5004. e << "Property " << p << " on target \"" << tgt->GetName()
  5005. << "\" is\nimplied to be " << defaultValue
  5006. << " because it was used to determine the link libraries\n"
  5007. "already. The INTERFACE_"
  5008. << p << " property on\ndependency \"" << theTarget->GetName()
  5009. << "\" is in conflict.\n";
  5010. cmSystemTools::Error(e.str());
  5011. break;
  5012. }
  5013. propContent = consistent.second;
  5014. continue;
  5015. }
  5016. // Implicitly set on target and not set in iface. Can't disagree.
  5017. continue;
  5018. }
  5019. if (ifaceIsSet) {
  5020. if (propInitialized) {
  5021. std::pair<bool, PropertyType> consistent =
  5022. consistentProperty(propContent, ifacePropContent, t);
  5023. report += reportEntry;
  5024. report += compatibilityAgree(t, propContent != consistent.second);
  5025. if (!consistent.first) {
  5026. std::ostringstream e;
  5027. e << "The INTERFACE_" << p << " property of \""
  5028. << theTarget->GetName() << "\" does\nnot agree with the value of "
  5029. << p << " already determined\nfor \"" << tgt->GetName() << "\".\n";
  5030. cmSystemTools::Error(e.str());
  5031. break;
  5032. }
  5033. propContent = consistent.second;
  5034. continue;
  5035. }
  5036. report += reportEntry + "(Interface set)\n";
  5037. propContent = ifacePropContent;
  5038. propInitialized = true;
  5039. } else {
  5040. // Not set. Nothing to agree on.
  5041. continue;
  5042. }
  5043. }
  5044. tgt->ReportPropertyOrigin(p, valueAsString<PropertyType>(propContent),
  5045. report, compatibilityType(t));
  5046. return propContent;
  5047. }
  5048. bool cmGeneratorTarget::SetDeviceLink(bool deviceLink)
  5049. {
  5050. bool previous = this->DeviceLink;
  5051. this->DeviceLink = deviceLink;
  5052. return previous;
  5053. }
  5054. bool cmGeneratorTarget::GetLinkInterfaceDependentBoolProperty(
  5055. const std::string& p, const std::string& config) const
  5056. {
  5057. return checkInterfacePropertyCompatibility<bool>(this, p, config, "FALSE",
  5058. BoolType, nullptr);
  5059. }
  5060. std::string cmGeneratorTarget::GetLinkInterfaceDependentStringAsBoolProperty(
  5061. const std::string& p, const std::string& config) const
  5062. {
  5063. return checkInterfacePropertyCompatibility<std::string>(
  5064. this, p, config, "FALSE", BoolType, nullptr);
  5065. }
  5066. const char* cmGeneratorTarget::GetLinkInterfaceDependentStringProperty(
  5067. const std::string& p, const std::string& config) const
  5068. {
  5069. return checkInterfacePropertyCompatibility<const char*>(
  5070. this, p, config, "empty", StringType, nullptr);
  5071. }
  5072. const char* cmGeneratorTarget::GetLinkInterfaceDependentNumberMinProperty(
  5073. const std::string& p, const std::string& config) const
  5074. {
  5075. return checkInterfacePropertyCompatibility<const char*>(
  5076. this, p, config, "empty", NumberMinType, nullptr);
  5077. }
  5078. const char* cmGeneratorTarget::GetLinkInterfaceDependentNumberMaxProperty(
  5079. const std::string& p, const std::string& config) const
  5080. {
  5081. return checkInterfacePropertyCompatibility<const char*>(
  5082. this, p, config, "empty", NumberMaxType, nullptr);
  5083. }
  5084. cmComputeLinkInformation* cmGeneratorTarget::GetLinkInformation(
  5085. const std::string& config) const
  5086. {
  5087. // Lookup any existing information for this configuration.
  5088. std::string key(cmSystemTools::UpperCase(config));
  5089. auto i = this->LinkInformation.find(key);
  5090. if (i == this->LinkInformation.end()) {
  5091. // Compute information for this configuration.
  5092. auto info = cm::make_unique<cmComputeLinkInformation>(this, config);
  5093. if (info && !info->Compute()) {
  5094. info.reset();
  5095. }
  5096. // Store the information for this configuration.
  5097. i = this->LinkInformation.emplace(key, std::move(info)).first;
  5098. if (i->second) {
  5099. this->CheckPropertyCompatibility(*i->second, config);
  5100. }
  5101. }
  5102. return i->second.get();
  5103. }
  5104. void cmGeneratorTarget::GetTargetVersion(int& major, int& minor) const
  5105. {
  5106. int patch;
  5107. this->GetTargetVersion("VERSION", major, minor, patch);
  5108. }
  5109. void cmGeneratorTarget::GetTargetVersionFallback(
  5110. const std::string& property, const std::string& fallback_property,
  5111. int& major, int& minor, int& patch) const
  5112. {
  5113. if (this->GetProperty(property)) {
  5114. this->GetTargetVersion(property, major, minor, patch);
  5115. } else {
  5116. this->GetTargetVersion(fallback_property, major, minor, patch);
  5117. }
  5118. }
  5119. void cmGeneratorTarget::GetTargetVersion(const std::string& property,
  5120. int& major, int& minor,
  5121. int& patch) const
  5122. {
  5123. // Set the default values.
  5124. major = 0;
  5125. minor = 0;
  5126. patch = 0;
  5127. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  5128. if (cmProp version = this->GetProperty(property)) {
  5129. // Try to parse the version number and store the results that were
  5130. // successfully parsed.
  5131. int parsed_major;
  5132. int parsed_minor;
  5133. int parsed_patch;
  5134. switch (sscanf(version->c_str(), "%d.%d.%d", &parsed_major, &parsed_minor,
  5135. &parsed_patch)) {
  5136. case 3:
  5137. patch = parsed_patch;
  5138. CM_FALLTHROUGH;
  5139. case 2:
  5140. minor = parsed_minor;
  5141. CM_FALLTHROUGH;
  5142. case 1:
  5143. major = parsed_major;
  5144. CM_FALLTHROUGH;
  5145. default:
  5146. break;
  5147. }
  5148. }
  5149. }
  5150. std::string cmGeneratorTarget::GetRuntimeLinkLibrary(
  5151. std::string const& lang, std::string const& config) const
  5152. {
  5153. // This is activated by the presence of a default selection whether or
  5154. // not it is overridden by a property.
  5155. cmProp runtimeLibraryDefault = this->Makefile->GetDef(
  5156. cmStrCat("CMAKE_", lang, "_RUNTIME_LIBRARY_DEFAULT"));
  5157. if (!runtimeLibraryDefault || runtimeLibraryDefault->empty()) {
  5158. return std::string();
  5159. }
  5160. cmProp runtimeLibraryValue =
  5161. this->Target->GetProperty(cmStrCat(lang, "_RUNTIME_LIBRARY"));
  5162. if (!runtimeLibraryValue) {
  5163. runtimeLibraryValue = runtimeLibraryDefault;
  5164. }
  5165. return cmSystemTools::UpperCase(cmGeneratorExpression::Evaluate(
  5166. *runtimeLibraryValue, this->LocalGenerator, config, this));
  5167. }
  5168. std::string cmGeneratorTarget::GetFortranModuleDirectory(
  5169. std::string const& working_dir) const
  5170. {
  5171. if (!this->FortranModuleDirectoryCreated) {
  5172. this->FortranModuleDirectory =
  5173. this->CreateFortranModuleDirectory(working_dir);
  5174. this->FortranModuleDirectoryCreated = true;
  5175. }
  5176. return this->FortranModuleDirectory;
  5177. }
  5178. std::string cmGeneratorTarget::CreateFortranModuleDirectory(
  5179. std::string const& working_dir) const
  5180. {
  5181. std::string mod_dir;
  5182. std::string target_mod_dir;
  5183. if (cmProp prop = this->GetProperty("Fortran_MODULE_DIRECTORY")) {
  5184. target_mod_dir = *prop;
  5185. } else {
  5186. std::string const& default_mod_dir =
  5187. this->LocalGenerator->GetCurrentBinaryDirectory();
  5188. if (default_mod_dir != working_dir) {
  5189. target_mod_dir = default_mod_dir;
  5190. }
  5191. }
  5192. const char* moddir_flag =
  5193. this->Makefile->GetDefinition("CMAKE_Fortran_MODDIR_FLAG");
  5194. if (!target_mod_dir.empty() && moddir_flag) {
  5195. // Compute the full path to the module directory.
  5196. if (cmSystemTools::FileIsFullPath(target_mod_dir)) {
  5197. // Already a full path.
  5198. mod_dir = target_mod_dir;
  5199. } else {
  5200. // Interpret relative to the current output directory.
  5201. mod_dir = cmStrCat(this->LocalGenerator->GetCurrentBinaryDirectory(),
  5202. '/', target_mod_dir);
  5203. }
  5204. // Make sure the module output directory exists.
  5205. cmSystemTools::MakeDirectory(mod_dir);
  5206. }
  5207. return mod_dir;
  5208. }
  5209. std::string cmGeneratorTarget::GetFrameworkVersion() const
  5210. {
  5211. assert(this->GetType() != cmStateEnums::INTERFACE_LIBRARY);
  5212. if (cmProp fversion = this->GetProperty("FRAMEWORK_VERSION")) {
  5213. return *fversion;
  5214. }
  5215. if (cmProp tversion = this->GetProperty("VERSION")) {
  5216. return *tversion;
  5217. }
  5218. return "A";
  5219. }
  5220. void cmGeneratorTarget::ComputeVersionedName(std::string& vName,
  5221. std::string const& prefix,
  5222. std::string const& base,
  5223. std::string const& suffix,
  5224. std::string const& name,
  5225. const char* version) const
  5226. {
  5227. vName = this->Makefile->IsOn("APPLE") ? (prefix + base) : name;
  5228. if (version) {
  5229. vName += ".";
  5230. vName += version;
  5231. }
  5232. vName += this->Makefile->IsOn("APPLE") ? suffix : std::string();
  5233. }
  5234. std::vector<std::string> cmGeneratorTarget::GetPropertyKeys() const
  5235. {
  5236. return this->Target->GetProperties().GetKeys();
  5237. }
  5238. void cmGeneratorTarget::ReportPropertyOrigin(
  5239. const std::string& p, const std::string& result, const std::string& report,
  5240. const std::string& compatibilityType) const
  5241. {
  5242. std::vector<std::string> debugProperties;
  5243. this->Target->GetMakefile()->GetDefExpandList(
  5244. "CMAKE_DEBUG_TARGET_PROPERTIES", debugProperties);
  5245. bool debugOrigin = !this->DebugCompatiblePropertiesDone[p] &&
  5246. cm::contains(debugProperties, p);
  5247. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  5248. this->DebugCompatiblePropertiesDone[p] = true;
  5249. }
  5250. if (!debugOrigin) {
  5251. return;
  5252. }
  5253. std::string areport =
  5254. cmStrCat(compatibilityType, " of property \"", p, "\" for target \"",
  5255. this->GetName(), "\" (result: \"", result, "\"):\n", report);
  5256. this->LocalGenerator->GetCMakeInstance()->IssueMessage(MessageType::LOG,
  5257. areport);
  5258. }
  5259. bool cmGeneratorTarget::IsLinkLookupScope(std::string const& n,
  5260. cmLocalGenerator const*& lg) const
  5261. {
  5262. if (cmHasLiteralPrefix(n, CMAKE_DIRECTORY_ID_SEP)) {
  5263. cmDirectoryId const dirId = n.substr(sizeof(CMAKE_DIRECTORY_ID_SEP) - 1);
  5264. if (dirId.String.empty()) {
  5265. lg = this->LocalGenerator;
  5266. return true;
  5267. }
  5268. if (cmLocalGenerator const* otherLG =
  5269. this->GlobalGenerator->FindLocalGenerator(dirId)) {
  5270. lg = otherLG;
  5271. return true;
  5272. }
  5273. }
  5274. return false;
  5275. }
  5276. void cmGeneratorTarget::LookupLinkItems(std::vector<std::string> const& names,
  5277. cmListFileBacktrace const& bt,
  5278. std::vector<cmLinkItem>& items) const
  5279. {
  5280. cmLocalGenerator const* lg = this->LocalGenerator;
  5281. for (std::string const& n : names) {
  5282. if (this->IsLinkLookupScope(n, lg)) {
  5283. continue;
  5284. }
  5285. std::string name = this->CheckCMP0004(n);
  5286. if (name == this->GetName() || name.empty()) {
  5287. continue;
  5288. }
  5289. items.push_back(this->ResolveLinkItem(name, bt, lg));
  5290. }
  5291. }
  5292. void cmGeneratorTarget::ExpandLinkItems(
  5293. std::string const& prop, std::string const& value, std::string const& config,
  5294. cmGeneratorTarget const* headTarget, bool usage_requirements_only,
  5295. std::vector<cmLinkItem>& items, bool& hadHeadSensitiveCondition,
  5296. bool& hadContextSensitiveCondition,
  5297. bool& hadLinkLanguageSensitiveCondition) const
  5298. {
  5299. // Keep this logic in sync with ComputeLinkImplementationLibraries.
  5300. cmGeneratorExpression ge;
  5301. cmGeneratorExpressionDAGChecker dagChecker(this, prop, nullptr, nullptr);
  5302. // The $<LINK_ONLY> expression may be in a link interface to specify
  5303. // private link dependencies that are otherwise excluded from usage
  5304. // requirements.
  5305. if (usage_requirements_only) {
  5306. dagChecker.SetTransitivePropertiesOnly();
  5307. }
  5308. std::vector<std::string> libs;
  5309. std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(value);
  5310. cmExpandList(cge->Evaluate(this->LocalGenerator, config, headTarget,
  5311. &dagChecker, this, headTarget->LinkerLanguage),
  5312. libs);
  5313. this->LookupLinkItems(libs, cge->GetBacktrace(), items);
  5314. hadHeadSensitiveCondition = cge->GetHadHeadSensitiveCondition();
  5315. hadContextSensitiveCondition = cge->GetHadContextSensitiveCondition();
  5316. hadLinkLanguageSensitiveCondition =
  5317. cge->GetHadLinkLanguageSensitiveCondition();
  5318. }
  5319. cmLinkInterface const* cmGeneratorTarget::GetLinkInterface(
  5320. const std::string& config, cmGeneratorTarget const* head) const
  5321. {
  5322. return this->GetLinkInterface(config, head, false);
  5323. }
  5324. cmLinkInterface const* cmGeneratorTarget::GetLinkInterface(
  5325. const std::string& config, cmGeneratorTarget const* head,
  5326. bool secondPass) const
  5327. {
  5328. // Imported targets have their own link interface.
  5329. if (this->IsImported()) {
  5330. return this->GetImportLinkInterface(config, head, false, secondPass);
  5331. }
  5332. // Link interfaces are not supported for executables that do not
  5333. // export symbols.
  5334. if (this->GetType() == cmStateEnums::EXECUTABLE &&
  5335. !this->IsExecutableWithExports()) {
  5336. return nullptr;
  5337. }
  5338. // Lookup any existing link interface for this configuration.
  5339. cmHeadToLinkInterfaceMap& hm = this->GetHeadToLinkInterfaceMap(config);
  5340. if (secondPass) {
  5341. hm.erase(head);
  5342. }
  5343. // If the link interface does not depend on the head target
  5344. // then return the one we computed first.
  5345. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  5346. return &hm.begin()->second;
  5347. }
  5348. cmOptionalLinkInterface& iface = hm[head];
  5349. if (!iface.LibrariesDone) {
  5350. iface.LibrariesDone = true;
  5351. this->ComputeLinkInterfaceLibraries(config, iface, head, false);
  5352. }
  5353. if (!iface.AllDone) {
  5354. iface.AllDone = true;
  5355. if (iface.Exists) {
  5356. this->ComputeLinkInterface(config, iface, head, secondPass);
  5357. }
  5358. }
  5359. return iface.Exists ? &iface : nullptr;
  5360. }
  5361. void cmGeneratorTarget::ComputeLinkInterface(
  5362. const std::string& config, cmOptionalLinkInterface& iface,
  5363. cmGeneratorTarget const* headTarget) const
  5364. {
  5365. this->ComputeLinkInterface(config, iface, headTarget, false);
  5366. }
  5367. void cmGeneratorTarget::ComputeLinkInterface(
  5368. const std::string& config, cmOptionalLinkInterface& iface,
  5369. cmGeneratorTarget const* headTarget, bool secondPass) const
  5370. {
  5371. if (iface.Explicit) {
  5372. if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  5373. this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  5374. this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  5375. // Shared libraries may have runtime implementation dependencies
  5376. // on other shared libraries that are not in the interface.
  5377. std::set<cmLinkItem> emitted;
  5378. for (cmLinkItem const& lib : iface.Libraries) {
  5379. emitted.insert(lib);
  5380. }
  5381. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  5382. cmLinkImplementation const* impl =
  5383. this->GetLinkImplementation(config, secondPass);
  5384. for (cmLinkImplItem const& lib : impl->Libraries) {
  5385. if (emitted.insert(lib).second) {
  5386. if (lib.Target) {
  5387. // This is a runtime dependency on another shared library.
  5388. if (lib.Target->GetType() == cmStateEnums::SHARED_LIBRARY) {
  5389. iface.SharedDeps.push_back(lib);
  5390. }
  5391. } else {
  5392. // TODO: Recognize shared library file names. Perhaps this
  5393. // should be moved to cmComputeLinkInformation, but that
  5394. // creates a chicken-and-egg problem since this list is needed
  5395. // for its construction.
  5396. }
  5397. }
  5398. }
  5399. }
  5400. }
  5401. } else if (this->GetPolicyStatusCMP0022() == cmPolicies::WARN ||
  5402. this->GetPolicyStatusCMP0022() == cmPolicies::OLD) {
  5403. // The link implementation is the default link interface.
  5404. cmLinkImplementationLibraries const* impl =
  5405. this->GetLinkImplementationLibrariesInternal(config, headTarget);
  5406. iface.ImplementationIsInterface = true;
  5407. iface.WrongConfigLibraries = impl->WrongConfigLibraries;
  5408. }
  5409. if (this->LinkLanguagePropagatesToDependents()) {
  5410. // Targets using this archive need its language runtime libraries.
  5411. if (cmLinkImplementation const* impl =
  5412. this->GetLinkImplementation(config, secondPass)) {
  5413. iface.Languages = impl->Languages;
  5414. }
  5415. }
  5416. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  5417. // Construct the property name suffix for this configuration.
  5418. std::string suffix = "_";
  5419. if (!config.empty()) {
  5420. suffix += cmSystemTools::UpperCase(config);
  5421. } else {
  5422. suffix += "NOCONFIG";
  5423. }
  5424. // How many repetitions are needed if this library has cyclic
  5425. // dependencies?
  5426. std::string propName = cmStrCat("LINK_INTERFACE_MULTIPLICITY", suffix);
  5427. if (cmProp config_reps = this->GetProperty(propName)) {
  5428. sscanf(config_reps->c_str(), "%u", &iface.Multiplicity);
  5429. } else if (cmProp reps =
  5430. this->GetProperty("LINK_INTERFACE_MULTIPLICITY")) {
  5431. sscanf(reps->c_str(), "%u", &iface.Multiplicity);
  5432. }
  5433. }
  5434. }
  5435. const cmLinkInterfaceLibraries* cmGeneratorTarget::GetLinkInterfaceLibraries(
  5436. const std::string& config, cmGeneratorTarget const* head,
  5437. bool usage_requirements_only) const
  5438. {
  5439. // Imported targets have their own link interface.
  5440. if (this->IsImported()) {
  5441. return this->GetImportLinkInterface(config, head, usage_requirements_only);
  5442. }
  5443. // Link interfaces are not supported for executables that do not
  5444. // export symbols.
  5445. if (this->GetType() == cmStateEnums::EXECUTABLE &&
  5446. !this->IsExecutableWithExports()) {
  5447. return nullptr;
  5448. }
  5449. // Lookup any existing link interface for this configuration.
  5450. cmHeadToLinkInterfaceMap& hm =
  5451. (usage_requirements_only
  5452. ? this->GetHeadToLinkInterfaceUsageRequirementsMap(config)
  5453. : this->GetHeadToLinkInterfaceMap(config));
  5454. // If the link interface does not depend on the head target
  5455. // then return the one we computed first.
  5456. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  5457. return &hm.begin()->second;
  5458. }
  5459. cmOptionalLinkInterface& iface = hm[head];
  5460. if (!iface.LibrariesDone) {
  5461. iface.LibrariesDone = true;
  5462. this->ComputeLinkInterfaceLibraries(config, iface, head,
  5463. usage_requirements_only);
  5464. }
  5465. return iface.Exists ? &iface : nullptr;
  5466. }
  5467. std::string cmGeneratorTarget::GetDirectory(
  5468. const std::string& config, cmStateEnums::ArtifactType artifact) const
  5469. {
  5470. if (this->IsImported()) {
  5471. // Return the directory from which the target is imported.
  5472. return cmSystemTools::GetFilenamePath(
  5473. this->Target->ImportedGetFullPath(config, artifact));
  5474. }
  5475. if (OutputInfo const* info = this->GetOutputInfo(config)) {
  5476. // Return the directory in which the target will be built.
  5477. switch (artifact) {
  5478. case cmStateEnums::RuntimeBinaryArtifact:
  5479. return info->OutDir;
  5480. case cmStateEnums::ImportLibraryArtifact:
  5481. return info->ImpDir;
  5482. }
  5483. }
  5484. return "";
  5485. }
  5486. bool cmGeneratorTarget::UsesDefaultOutputDir(
  5487. const std::string& config, cmStateEnums::ArtifactType artifact) const
  5488. {
  5489. std::string dir;
  5490. return this->ComputeOutputDir(config, artifact, dir);
  5491. }
  5492. cmGeneratorTarget::OutputInfo const* cmGeneratorTarget::GetOutputInfo(
  5493. const std::string& config) const
  5494. {
  5495. // There is no output information for imported targets.
  5496. if (this->IsImported()) {
  5497. return nullptr;
  5498. }
  5499. // Only libraries and executables have well-defined output files.
  5500. if (!this->HaveWellDefinedOutputFiles()) {
  5501. std::string msg = cmStrCat("cmGeneratorTarget::GetOutputInfo called for ",
  5502. this->GetName(), " which has type ",
  5503. cmState::GetTargetTypeName(this->GetType()));
  5504. this->LocalGenerator->IssueMessage(MessageType::INTERNAL_ERROR, msg);
  5505. return nullptr;
  5506. }
  5507. // Lookup/compute/cache the output information for this configuration.
  5508. std::string config_upper;
  5509. if (!config.empty()) {
  5510. config_upper = cmSystemTools::UpperCase(config);
  5511. }
  5512. auto i = this->OutputInfoMap.find(config_upper);
  5513. if (i == this->OutputInfoMap.end()) {
  5514. // Add empty info in map to detect potential recursion.
  5515. OutputInfo info;
  5516. OutputInfoMapType::value_type entry(config_upper, info);
  5517. i = this->OutputInfoMap.insert(entry).first;
  5518. // Compute output directories.
  5519. this->ComputeOutputDir(config, cmStateEnums::RuntimeBinaryArtifact,
  5520. info.OutDir);
  5521. this->ComputeOutputDir(config, cmStateEnums::ImportLibraryArtifact,
  5522. info.ImpDir);
  5523. if (!this->ComputePDBOutputDir("PDB", config, info.PdbDir)) {
  5524. info.PdbDir = info.OutDir;
  5525. }
  5526. // Now update the previously-prepared map entry.
  5527. i->second = info;
  5528. } else if (i->second.empty()) {
  5529. // An empty map entry indicates we have been called recursively
  5530. // from the above block.
  5531. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  5532. MessageType::FATAL_ERROR,
  5533. "Target '" + this->GetName() + "' OUTPUT_DIRECTORY depends on itself.",
  5534. this->GetBacktrace());
  5535. return nullptr;
  5536. }
  5537. return &i->second;
  5538. }
  5539. bool cmGeneratorTarget::ComputeOutputDir(const std::string& config,
  5540. cmStateEnums::ArtifactType artifact,
  5541. std::string& out) const
  5542. {
  5543. bool usesDefaultOutputDir = false;
  5544. std::string conf = config;
  5545. // Look for a target property defining the target output directory
  5546. // based on the target type.
  5547. std::string targetTypeName = this->GetOutputTargetType(artifact);
  5548. const char* propertyName = nullptr;
  5549. std::string propertyNameStr = targetTypeName;
  5550. if (!propertyNameStr.empty()) {
  5551. propertyNameStr += "_OUTPUT_DIRECTORY";
  5552. propertyName = propertyNameStr.c_str();
  5553. }
  5554. // Check for a per-configuration output directory target property.
  5555. std::string configUpper = cmSystemTools::UpperCase(conf);
  5556. const char* configProp = nullptr;
  5557. std::string configPropStr = targetTypeName;
  5558. if (!configPropStr.empty()) {
  5559. configPropStr += "_OUTPUT_DIRECTORY_";
  5560. configPropStr += configUpper;
  5561. configProp = configPropStr.c_str();
  5562. }
  5563. // Select an output directory.
  5564. if (cmProp config_outdir = this->GetProperty(configProp)) {
  5565. // Use the user-specified per-configuration output directory.
  5566. out = cmGeneratorExpression::Evaluate(*config_outdir, this->LocalGenerator,
  5567. config);
  5568. // Skip per-configuration subdirectory.
  5569. conf.clear();
  5570. } else if (cmProp outdir = this->GetProperty(propertyName)) {
  5571. // Use the user-specified output directory.
  5572. out =
  5573. cmGeneratorExpression::Evaluate(*outdir, this->LocalGenerator, config);
  5574. // Skip per-configuration subdirectory if the value contained a
  5575. // generator expression.
  5576. if (out != *outdir) {
  5577. conf.clear();
  5578. }
  5579. } else if (this->GetType() == cmStateEnums::EXECUTABLE) {
  5580. // Lookup the output path for executables.
  5581. out = this->Makefile->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  5582. } else if (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  5583. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  5584. this->GetType() == cmStateEnums::MODULE_LIBRARY) {
  5585. // Lookup the output path for libraries.
  5586. out = this->Makefile->GetSafeDefinition("LIBRARY_OUTPUT_PATH");
  5587. }
  5588. if (out.empty()) {
  5589. // Default to the current output directory.
  5590. usesDefaultOutputDir = true;
  5591. out = ".";
  5592. }
  5593. // Convert the output path to a full path in case it is
  5594. // specified as a relative path. Treat a relative path as
  5595. // relative to the current output directory for this makefile.
  5596. out = (cmSystemTools::CollapseFullPath(
  5597. out, this->LocalGenerator->GetCurrentBinaryDirectory()));
  5598. // The generator may add the configuration's subdirectory.
  5599. if (!conf.empty()) {
  5600. bool useEPN =
  5601. this->GlobalGenerator->UseEffectivePlatformName(this->Makefile);
  5602. std::string suffix =
  5603. usesDefaultOutputDir && useEPN ? "${EFFECTIVE_PLATFORM_NAME}" : "";
  5604. this->LocalGenerator->GetGlobalGenerator()->AppendDirectoryForConfig(
  5605. "/", conf, suffix, out);
  5606. }
  5607. return usesDefaultOutputDir;
  5608. }
  5609. bool cmGeneratorTarget::ComputePDBOutputDir(const std::string& kind,
  5610. const std::string& config,
  5611. std::string& out) const
  5612. {
  5613. // Look for a target property defining the target output directory
  5614. // based on the target type.
  5615. const char* propertyName = nullptr;
  5616. std::string propertyNameStr = kind;
  5617. if (!propertyNameStr.empty()) {
  5618. propertyNameStr += "_OUTPUT_DIRECTORY";
  5619. propertyName = propertyNameStr.c_str();
  5620. }
  5621. std::string conf = config;
  5622. // Check for a per-configuration output directory target property.
  5623. std::string configUpper = cmSystemTools::UpperCase(conf);
  5624. const char* configProp = nullptr;
  5625. std::string configPropStr = kind;
  5626. if (!configPropStr.empty()) {
  5627. configPropStr += "_OUTPUT_DIRECTORY_";
  5628. configPropStr += configUpper;
  5629. configProp = configPropStr.c_str();
  5630. }
  5631. // Select an output directory.
  5632. if (cmProp config_outdir = this->GetProperty(configProp)) {
  5633. // Use the user-specified per-configuration output directory.
  5634. out = cmGeneratorExpression::Evaluate(*config_outdir, this->LocalGenerator,
  5635. config);
  5636. // Skip per-configuration subdirectory.
  5637. conf.clear();
  5638. } else if (cmProp outdir = this->GetProperty(propertyName)) {
  5639. // Use the user-specified output directory.
  5640. out =
  5641. cmGeneratorExpression::Evaluate(*outdir, this->LocalGenerator, config);
  5642. // Skip per-configuration subdirectory if the value contained a
  5643. // generator expression.
  5644. if (out != *outdir) {
  5645. conf.clear();
  5646. }
  5647. }
  5648. if (out.empty()) {
  5649. return false;
  5650. }
  5651. // Convert the output path to a full path in case it is
  5652. // specified as a relative path. Treat a relative path as
  5653. // relative to the current output directory for this makefile.
  5654. out = (cmSystemTools::CollapseFullPath(
  5655. out, this->LocalGenerator->GetCurrentBinaryDirectory()));
  5656. // The generator may add the configuration's subdirectory.
  5657. if (!conf.empty()) {
  5658. this->LocalGenerator->GetGlobalGenerator()->AppendDirectoryForConfig(
  5659. "/", conf, "", out);
  5660. }
  5661. return true;
  5662. }
  5663. bool cmGeneratorTarget::HaveInstallTreeRPATH(const std::string& config) const
  5664. {
  5665. std::string install_rpath;
  5666. this->GetInstallRPATH(config, install_rpath);
  5667. return !install_rpath.empty() &&
  5668. !this->Makefile->IsOn("CMAKE_SKIP_INSTALL_RPATH");
  5669. }
  5670. bool cmGeneratorTarget::GetBuildRPATH(const std::string& config,
  5671. std::string& rpath) const
  5672. {
  5673. return this->GetRPATH(config, "BUILD_RPATH", rpath);
  5674. }
  5675. bool cmGeneratorTarget::GetInstallRPATH(const std::string& config,
  5676. std::string& rpath) const
  5677. {
  5678. return this->GetRPATH(config, "INSTALL_RPATH", rpath);
  5679. }
  5680. bool cmGeneratorTarget::GetRPATH(const std::string& config,
  5681. const std::string& prop,
  5682. std::string& rpath) const
  5683. {
  5684. cmProp value = this->GetProperty(prop);
  5685. if (!value) {
  5686. return false;
  5687. }
  5688. rpath =
  5689. cmGeneratorExpression::Evaluate(*value, this->LocalGenerator, config);
  5690. return true;
  5691. }
  5692. void cmGeneratorTarget::ComputeLinkInterfaceLibraries(
  5693. const std::string& config, cmOptionalLinkInterface& iface,
  5694. cmGeneratorTarget const* headTarget, bool usage_requirements_only) const
  5695. {
  5696. // Construct the property name suffix for this configuration.
  5697. std::string suffix = "_";
  5698. if (!config.empty()) {
  5699. suffix += cmSystemTools::UpperCase(config);
  5700. } else {
  5701. suffix += "NOCONFIG";
  5702. }
  5703. // An explicit list of interface libraries may be set for shared
  5704. // libraries and executables that export symbols.
  5705. cmProp explicitLibraries = nullptr;
  5706. std::string linkIfaceProp;
  5707. bool const cmp0022NEW = (this->GetPolicyStatusCMP0022() != cmPolicies::OLD &&
  5708. this->GetPolicyStatusCMP0022() != cmPolicies::WARN);
  5709. if (cmp0022NEW) {
  5710. // CMP0022 NEW behavior is to use INTERFACE_LINK_LIBRARIES.
  5711. linkIfaceProp = "INTERFACE_LINK_LIBRARIES";
  5712. explicitLibraries = this->GetProperty(linkIfaceProp);
  5713. } else if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  5714. this->IsExecutableWithExports()) {
  5715. // CMP0022 OLD behavior is to use LINK_INTERFACE_LIBRARIES if set on a
  5716. // shared lib or executable.
  5717. // Lookup the per-configuration property.
  5718. linkIfaceProp = cmStrCat("LINK_INTERFACE_LIBRARIES", suffix);
  5719. explicitLibraries = this->GetProperty(linkIfaceProp);
  5720. // If not set, try the generic property.
  5721. if (!explicitLibraries) {
  5722. linkIfaceProp = "LINK_INTERFACE_LIBRARIES";
  5723. explicitLibraries = this->GetProperty(linkIfaceProp);
  5724. }
  5725. }
  5726. if (explicitLibraries &&
  5727. this->GetPolicyStatusCMP0022() == cmPolicies::WARN &&
  5728. !this->PolicyWarnedCMP0022) {
  5729. // Compare the explicitly set old link interface properties to the
  5730. // preferred new link interface property one and warn if different.
  5731. cmProp newExplicitLibraries =
  5732. this->GetProperty("INTERFACE_LINK_LIBRARIES");
  5733. if (newExplicitLibraries &&
  5734. (*newExplicitLibraries != *explicitLibraries)) {
  5735. std::ostringstream w;
  5736. /* clang-format off */
  5737. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0022) << "\n"
  5738. "Target \"" << this->GetName() << "\" has an "
  5739. "INTERFACE_LINK_LIBRARIES property which differs from its " <<
  5740. linkIfaceProp << " properties."
  5741. "\n"
  5742. "INTERFACE_LINK_LIBRARIES:\n"
  5743. " " << *newExplicitLibraries << "\n" <<
  5744. linkIfaceProp << ":\n"
  5745. " " << *explicitLibraries << "\n";
  5746. /* clang-format on */
  5747. this->LocalGenerator->IssueMessage(MessageType::AUTHOR_WARNING, w.str());
  5748. this->PolicyWarnedCMP0022 = true;
  5749. }
  5750. }
  5751. // There is no implicit link interface for executables or modules
  5752. // so if none was explicitly set then there is no link interface.
  5753. if (!explicitLibraries &&
  5754. (this->GetType() == cmStateEnums::EXECUTABLE ||
  5755. (this->GetType() == cmStateEnums::MODULE_LIBRARY))) {
  5756. return;
  5757. }
  5758. iface.Exists = true;
  5759. iface.Explicit = cmp0022NEW || explicitLibraries != nullptr;
  5760. if (explicitLibraries) {
  5761. // The interface libraries have been explicitly set.
  5762. this->ExpandLinkItems(linkIfaceProp, *explicitLibraries, config,
  5763. headTarget, usage_requirements_only, iface.Libraries,
  5764. iface.HadHeadSensitiveCondition,
  5765. iface.HadContextSensitiveCondition,
  5766. iface.HadLinkLanguageSensitiveCondition);
  5767. } else if (!cmp0022NEW)
  5768. // If CMP0022 is NEW then the plain tll signature sets the
  5769. // INTERFACE_LINK_LIBRARIES, so if we get here then the project
  5770. // cleared the property explicitly and we should not fall back
  5771. // to the link implementation.
  5772. {
  5773. // The link implementation is the default link interface.
  5774. cmLinkImplementationLibraries const* impl =
  5775. this->GetLinkImplementationLibrariesInternal(config, headTarget);
  5776. iface.Libraries.insert(iface.Libraries.end(), impl->Libraries.begin(),
  5777. impl->Libraries.end());
  5778. if (this->GetPolicyStatusCMP0022() == cmPolicies::WARN &&
  5779. !this->PolicyWarnedCMP0022 && !usage_requirements_only) {
  5780. // Compare the link implementation fallback link interface to the
  5781. // preferred new link interface property and warn if different.
  5782. std::vector<cmLinkItem> ifaceLibs;
  5783. static const std::string newProp = "INTERFACE_LINK_LIBRARIES";
  5784. if (cmProp newExplicitLibraries = this->GetProperty(newProp)) {
  5785. bool hadHeadSensitiveConditionDummy = false;
  5786. bool hadContextSensitiveConditionDummy = false;
  5787. bool hadLinkLanguageSensitiveConditionDummy = false;
  5788. this->ExpandLinkItems(newProp, *newExplicitLibraries, config,
  5789. headTarget, usage_requirements_only, ifaceLibs,
  5790. hadHeadSensitiveConditionDummy,
  5791. hadContextSensitiveConditionDummy,
  5792. hadLinkLanguageSensitiveConditionDummy);
  5793. }
  5794. if (ifaceLibs != iface.Libraries) {
  5795. std::string oldLibraries = cmJoin(impl->Libraries, ";");
  5796. std::string newLibraries = cmJoin(ifaceLibs, ";");
  5797. if (oldLibraries.empty()) {
  5798. oldLibraries = "(empty)";
  5799. }
  5800. if (newLibraries.empty()) {
  5801. newLibraries = "(empty)";
  5802. }
  5803. std::ostringstream w;
  5804. /* clang-format off */
  5805. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0022) << "\n"
  5806. "Target \"" << this->GetName() << "\" has an "
  5807. "INTERFACE_LINK_LIBRARIES property. "
  5808. "This should be preferred as the source of the link interface "
  5809. "for this library but because CMP0022 is not set CMake is "
  5810. "ignoring the property and using the link implementation "
  5811. "as the link interface instead."
  5812. "\n"
  5813. "INTERFACE_LINK_LIBRARIES:\n"
  5814. " " << newLibraries << "\n"
  5815. "Link implementation:\n"
  5816. " " << oldLibraries << "\n";
  5817. /* clang-format on */
  5818. this->LocalGenerator->IssueMessage(MessageType::AUTHOR_WARNING,
  5819. w.str());
  5820. this->PolicyWarnedCMP0022 = true;
  5821. }
  5822. }
  5823. }
  5824. }
  5825. const cmLinkInterface* cmGeneratorTarget::GetImportLinkInterface(
  5826. const std::string& config, cmGeneratorTarget const* headTarget,
  5827. bool usage_requirements_only, bool secondPass) const
  5828. {
  5829. cmGeneratorTarget::ImportInfo const* info = this->GetImportInfo(config);
  5830. if (!info) {
  5831. return nullptr;
  5832. }
  5833. cmHeadToLinkInterfaceMap& hm =
  5834. (usage_requirements_only
  5835. ? this->GetHeadToLinkInterfaceUsageRequirementsMap(config)
  5836. : this->GetHeadToLinkInterfaceMap(config));
  5837. if (secondPass) {
  5838. hm.erase(headTarget);
  5839. }
  5840. // If the link interface does not depend on the head target
  5841. // then return the one we computed first.
  5842. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  5843. return &hm.begin()->second;
  5844. }
  5845. cmOptionalLinkInterface& iface = hm[headTarget];
  5846. if (!iface.AllDone) {
  5847. iface.AllDone = true;
  5848. iface.Multiplicity = info->Multiplicity;
  5849. cmExpandList(info->Languages, iface.Languages);
  5850. this->ExpandLinkItems(info->LibrariesProp, info->Libraries, config,
  5851. headTarget, usage_requirements_only, iface.Libraries,
  5852. iface.HadHeadSensitiveCondition,
  5853. iface.HadContextSensitiveCondition,
  5854. iface.HadLinkLanguageSensitiveCondition);
  5855. std::vector<std::string> deps = cmExpandedList(info->SharedDeps);
  5856. this->LookupLinkItems(deps, cmListFileBacktrace(), iface.SharedDeps);
  5857. }
  5858. return &iface;
  5859. }
  5860. cmGeneratorTarget::ImportInfo const* cmGeneratorTarget::GetImportInfo(
  5861. const std::string& config) const
  5862. {
  5863. // There is no imported information for non-imported targets.
  5864. if (!this->IsImported()) {
  5865. return nullptr;
  5866. }
  5867. // Lookup/compute/cache the import information for this
  5868. // configuration.
  5869. std::string config_upper;
  5870. if (!config.empty()) {
  5871. config_upper = cmSystemTools::UpperCase(config);
  5872. } else {
  5873. config_upper = "NOCONFIG";
  5874. }
  5875. auto i = this->ImportInfoMap.find(config_upper);
  5876. if (i == this->ImportInfoMap.end()) {
  5877. ImportInfo info;
  5878. this->ComputeImportInfo(config_upper, info);
  5879. ImportInfoMapType::value_type entry(config_upper, info);
  5880. i = this->ImportInfoMap.insert(entry).first;
  5881. }
  5882. if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  5883. return &i->second;
  5884. }
  5885. // If the location is empty then the target is not available for
  5886. // this configuration.
  5887. if (i->second.Location.empty() && i->second.ImportLibrary.empty()) {
  5888. return nullptr;
  5889. }
  5890. // Return the import information.
  5891. return &i->second;
  5892. }
  5893. void cmGeneratorTarget::ComputeImportInfo(std::string const& desired_config,
  5894. ImportInfo& info) const
  5895. {
  5896. // This method finds information about an imported target from its
  5897. // properties. The "IMPORTED_" namespace is reserved for properties
  5898. // defined by the project exporting the target.
  5899. // Initialize members.
  5900. info.NoSOName = false;
  5901. cmProp loc = nullptr;
  5902. cmProp imp = nullptr;
  5903. std::string suffix;
  5904. if (!this->Target->GetMappedConfig(desired_config, loc, imp, suffix)) {
  5905. return;
  5906. }
  5907. // Get the link interface.
  5908. {
  5909. std::string linkProp = "INTERFACE_LINK_LIBRARIES";
  5910. cmProp propertyLibs = this->GetProperty(linkProp);
  5911. if (this->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  5912. if (!propertyLibs) {
  5913. linkProp = cmStrCat("IMPORTED_LINK_INTERFACE_LIBRARIES", suffix);
  5914. propertyLibs = this->GetProperty(linkProp);
  5915. }
  5916. if (!propertyLibs) {
  5917. linkProp = "IMPORTED_LINK_INTERFACE_LIBRARIES";
  5918. propertyLibs = this->GetProperty(linkProp);
  5919. }
  5920. }
  5921. if (propertyLibs) {
  5922. info.LibrariesProp = linkProp;
  5923. info.Libraries = *propertyLibs;
  5924. }
  5925. }
  5926. if (this->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  5927. if (loc) {
  5928. info.LibName = *loc;
  5929. }
  5930. return;
  5931. }
  5932. // A provided configuration has been chosen. Load the
  5933. // configuration's properties.
  5934. // Get the location.
  5935. if (loc) {
  5936. info.Location = *loc;
  5937. } else {
  5938. std::string impProp = cmStrCat("IMPORTED_LOCATION", suffix);
  5939. if (cmProp config_location = this->GetProperty(impProp)) {
  5940. info.Location = *config_location;
  5941. } else if (cmProp location = this->GetProperty("IMPORTED_LOCATION")) {
  5942. info.Location = *location;
  5943. }
  5944. }
  5945. // Get the soname.
  5946. if (this->GetType() == cmStateEnums::SHARED_LIBRARY) {
  5947. std::string soProp = cmStrCat("IMPORTED_SONAME", suffix);
  5948. if (cmProp config_soname = this->GetProperty(soProp)) {
  5949. info.SOName = *config_soname;
  5950. } else if (cmProp soname = this->GetProperty("IMPORTED_SONAME")) {
  5951. info.SOName = *soname;
  5952. }
  5953. }
  5954. // Get the "no-soname" mark.
  5955. if (this->GetType() == cmStateEnums::SHARED_LIBRARY) {
  5956. std::string soProp = cmStrCat("IMPORTED_NO_SONAME", suffix);
  5957. if (cmProp config_no_soname = this->GetProperty(soProp)) {
  5958. info.NoSOName = cmIsOn(*config_no_soname);
  5959. } else if (cmProp no_soname = this->GetProperty("IMPORTED_NO_SONAME")) {
  5960. info.NoSOName = cmIsOn(*no_soname);
  5961. }
  5962. }
  5963. // Get the import library.
  5964. if (imp) {
  5965. info.ImportLibrary = *imp;
  5966. } else if (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  5967. this->IsExecutableWithExports()) {
  5968. std::string impProp = cmStrCat("IMPORTED_IMPLIB", suffix);
  5969. if (cmProp config_implib = this->GetProperty(impProp)) {
  5970. info.ImportLibrary = *config_implib;
  5971. } else if (cmProp implib = this->GetProperty("IMPORTED_IMPLIB")) {
  5972. info.ImportLibrary = *implib;
  5973. }
  5974. }
  5975. // Get the link dependencies.
  5976. {
  5977. std::string linkProp =
  5978. cmStrCat("IMPORTED_LINK_DEPENDENT_LIBRARIES", suffix);
  5979. if (cmProp config_libs = this->GetProperty(linkProp)) {
  5980. info.SharedDeps = *config_libs;
  5981. } else if (cmProp libs =
  5982. this->GetProperty("IMPORTED_LINK_DEPENDENT_LIBRARIES")) {
  5983. info.SharedDeps = *libs;
  5984. }
  5985. }
  5986. // Get the link languages.
  5987. if (this->LinkLanguagePropagatesToDependents()) {
  5988. std::string linkProp =
  5989. cmStrCat("IMPORTED_LINK_INTERFACE_LANGUAGES", suffix);
  5990. if (cmProp config_libs = this->GetProperty(linkProp)) {
  5991. info.Languages = *config_libs;
  5992. } else if (cmProp libs =
  5993. this->GetProperty("IMPORTED_LINK_INTERFACE_LANGUAGES")) {
  5994. info.Languages = *libs;
  5995. }
  5996. }
  5997. // Get information if target is managed assembly.
  5998. {
  5999. std::string linkProp = "IMPORTED_COMMON_LANGUAGE_RUNTIME";
  6000. if (cmProp pc = this->GetProperty(linkProp + suffix)) {
  6001. info.Managed = this->CheckManagedType(*pc);
  6002. } else if (cmProp p = this->GetProperty(linkProp)) {
  6003. info.Managed = this->CheckManagedType(*p);
  6004. }
  6005. }
  6006. // Get the cyclic repetition count.
  6007. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  6008. std::string linkProp =
  6009. cmStrCat("IMPORTED_LINK_INTERFACE_MULTIPLICITY", suffix);
  6010. if (cmProp config_reps = this->GetProperty(linkProp)) {
  6011. sscanf(config_reps->c_str(), "%u", &info.Multiplicity);
  6012. } else if (cmProp reps =
  6013. this->GetProperty("IMPORTED_LINK_INTERFACE_MULTIPLICITY")) {
  6014. sscanf(reps->c_str(), "%u", &info.Multiplicity);
  6015. }
  6016. }
  6017. }
  6018. cmHeadToLinkInterfaceMap& cmGeneratorTarget::GetHeadToLinkInterfaceMap(
  6019. const std::string& config) const
  6020. {
  6021. return this->LinkInterfaceMap[cmSystemTools::UpperCase(config)];
  6022. }
  6023. cmHeadToLinkInterfaceMap&
  6024. cmGeneratorTarget::GetHeadToLinkInterfaceUsageRequirementsMap(
  6025. const std::string& config) const
  6026. {
  6027. return this
  6028. ->LinkInterfaceUsageRequirementsOnlyMap[cmSystemTools::UpperCase(config)];
  6029. }
  6030. const cmLinkImplementation* cmGeneratorTarget::GetLinkImplementation(
  6031. const std::string& config) const
  6032. {
  6033. return this->GetLinkImplementation(config, false);
  6034. }
  6035. const cmLinkImplementation* cmGeneratorTarget::GetLinkImplementation(
  6036. const std::string& config, bool secondPass) const
  6037. {
  6038. // There is no link implementation for imported targets.
  6039. if (this->IsImported()) {
  6040. return nullptr;
  6041. }
  6042. cmOptionalLinkImplementation& impl =
  6043. this->LinkImplMap[cmSystemTools::UpperCase(config)][this];
  6044. if (secondPass) {
  6045. impl = cmOptionalLinkImplementation();
  6046. }
  6047. if (!impl.LibrariesDone) {
  6048. impl.LibrariesDone = true;
  6049. this->ComputeLinkImplementationLibraries(config, impl, this);
  6050. }
  6051. if (!impl.LanguagesDone) {
  6052. impl.LanguagesDone = true;
  6053. this->ComputeLinkImplementationLanguages(config, impl);
  6054. }
  6055. return &impl;
  6056. }
  6057. bool cmGeneratorTarget::GetConfigCommonSourceFiles(
  6058. std::vector<cmSourceFile*>& files) const
  6059. {
  6060. std::vector<std::string> const& configs =
  6061. this->Makefile->GetGeneratorConfigs();
  6062. auto it = configs.begin();
  6063. const std::string& firstConfig = *it;
  6064. this->GetSourceFilesWithoutObjectLibraries(files, firstConfig);
  6065. for (; it != configs.end(); ++it) {
  6066. std::vector<cmSourceFile*> configFiles;
  6067. this->GetSourceFilesWithoutObjectLibraries(configFiles, *it);
  6068. if (configFiles != files) {
  6069. std::string firstConfigFiles;
  6070. const char* sep = "";
  6071. for (cmSourceFile* f : files) {
  6072. firstConfigFiles += sep;
  6073. firstConfigFiles += f->ResolveFullPath();
  6074. sep = "\n ";
  6075. }
  6076. std::string thisConfigFiles;
  6077. sep = "";
  6078. for (cmSourceFile* f : configFiles) {
  6079. thisConfigFiles += sep;
  6080. thisConfigFiles += f->ResolveFullPath();
  6081. sep = "\n ";
  6082. }
  6083. std::ostringstream e;
  6084. /* clang-format off */
  6085. e << "Target \"" << this->GetName()
  6086. << "\" has source files which vary by "
  6087. "configuration. This is not supported by the \""
  6088. << this->GlobalGenerator->GetName()
  6089. << "\" generator.\n"
  6090. "Config \"" << firstConfig << "\":\n"
  6091. " " << firstConfigFiles << "\n"
  6092. "Config \"" << *it << "\":\n"
  6093. " " << thisConfigFiles << "\n";
  6094. /* clang-format on */
  6095. this->LocalGenerator->IssueMessage(MessageType::FATAL_ERROR, e.str());
  6096. return false;
  6097. }
  6098. }
  6099. return true;
  6100. }
  6101. void cmGeneratorTarget::GetObjectLibrariesCMP0026(
  6102. std::vector<cmGeneratorTarget*>& objlibs) const
  6103. {
  6104. // At configure-time, this method can be called as part of getting the
  6105. // LOCATION property or to export() a file to be include()d. However
  6106. // there is no cmGeneratorTarget at configure-time, so search the SOURCES
  6107. // for TARGET_OBJECTS instead for backwards compatibility with OLD
  6108. // behavior of CMP0024 and CMP0026 only.
  6109. cmStringRange rng = this->Target->GetSourceEntries();
  6110. for (std::string const& entry : rng) {
  6111. std::vector<std::string> files = cmExpandedList(entry);
  6112. for (std::string const& li : files) {
  6113. if (cmHasLiteralPrefix(li, "$<TARGET_OBJECTS:") && li.back() == '>') {
  6114. std::string objLibName = li.substr(17, li.size() - 18);
  6115. if (cmGeneratorExpression::Find(objLibName) != std::string::npos) {
  6116. continue;
  6117. }
  6118. cmGeneratorTarget* objLib =
  6119. this->LocalGenerator->FindGeneratorTargetToUse(objLibName);
  6120. if (objLib) {
  6121. objlibs.push_back(objLib);
  6122. }
  6123. }
  6124. }
  6125. }
  6126. }
  6127. std::string cmGeneratorTarget::CheckCMP0004(std::string const& item) const
  6128. {
  6129. // Strip whitespace off the library names because we used to do this
  6130. // in case variables were expanded at generate time. We no longer
  6131. // do the expansion but users link to libraries like " ${VAR} ".
  6132. std::string lib = item;
  6133. std::string::size_type pos = lib.find_first_not_of(" \t\r\n");
  6134. if (pos != std::string::npos) {
  6135. lib = lib.substr(pos);
  6136. }
  6137. pos = lib.find_last_not_of(" \t\r\n");
  6138. if (pos != std::string::npos) {
  6139. lib = lib.substr(0, pos + 1);
  6140. }
  6141. if (lib != item) {
  6142. cmake* cm = this->LocalGenerator->GetCMakeInstance();
  6143. switch (this->GetPolicyStatusCMP0004()) {
  6144. case cmPolicies::WARN: {
  6145. std::ostringstream w;
  6146. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0004) << "\n"
  6147. << "Target \"" << this->GetName() << "\" links to item \"" << item
  6148. << "\" which has leading or trailing whitespace.";
  6149. cm->IssueMessage(MessageType::AUTHOR_WARNING, w.str(),
  6150. this->GetBacktrace());
  6151. }
  6152. case cmPolicies::OLD:
  6153. break;
  6154. case cmPolicies::NEW: {
  6155. std::ostringstream e;
  6156. e << "Target \"" << this->GetName() << "\" links to item \"" << item
  6157. << "\" which has leading or trailing whitespace. "
  6158. << "This is now an error according to policy CMP0004.";
  6159. cm->IssueMessage(MessageType::FATAL_ERROR, e.str(),
  6160. this->GetBacktrace());
  6161. } break;
  6162. case cmPolicies::REQUIRED_IF_USED:
  6163. case cmPolicies::REQUIRED_ALWAYS: {
  6164. std::ostringstream e;
  6165. e << cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0004) << "\n"
  6166. << "Target \"" << this->GetName() << "\" links to item \"" << item
  6167. << "\" which has leading or trailing whitespace.";
  6168. cm->IssueMessage(MessageType::FATAL_ERROR, e.str(),
  6169. this->GetBacktrace());
  6170. } break;
  6171. }
  6172. }
  6173. return lib;
  6174. }
  6175. bool cmGeneratorTarget::IsDeprecated() const
  6176. {
  6177. cmProp deprecation = this->GetProperty("DEPRECATION");
  6178. return deprecation && !deprecation->empty();
  6179. }
  6180. std::string cmGeneratorTarget::GetDeprecation() const
  6181. {
  6182. // find DEPRECATION property
  6183. if (cmProp deprecation = this->GetProperty("DEPRECATION")) {
  6184. return *deprecation;
  6185. }
  6186. return std::string();
  6187. }
  6188. void cmGeneratorTarget::GetLanguages(std::set<std::string>& languages,
  6189. const std::string& config) const
  6190. {
  6191. std::vector<cmSourceFile*> sourceFiles;
  6192. this->GetSourceFiles(sourceFiles, config);
  6193. for (cmSourceFile* src : sourceFiles) {
  6194. const std::string& lang = src->GetOrDetermineLanguage();
  6195. if (!lang.empty()) {
  6196. languages.insert(lang);
  6197. }
  6198. }
  6199. std::vector<cmGeneratorTarget*> objectLibraries;
  6200. std::vector<cmSourceFile const*> externalObjects;
  6201. if (!this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  6202. std::vector<cmGeneratorTarget*> objectTargets;
  6203. this->GetObjectLibrariesCMP0026(objectTargets);
  6204. objectLibraries.reserve(objectTargets.size());
  6205. for (cmGeneratorTarget* gt : objectTargets) {
  6206. objectLibraries.push_back(gt);
  6207. }
  6208. } else {
  6209. this->GetExternalObjects(externalObjects, config);
  6210. for (cmSourceFile const* extObj : externalObjects) {
  6211. std::string objLib = extObj->GetObjectLibrary();
  6212. if (cmGeneratorTarget* tgt =
  6213. this->LocalGenerator->FindGeneratorTargetToUse(objLib)) {
  6214. auto const objLibIt =
  6215. std::find_if(objectLibraries.cbegin(), objectLibraries.cend(),
  6216. [tgt](cmGeneratorTarget* t) { return t == tgt; });
  6217. if (objectLibraries.cend() == objLibIt) {
  6218. objectLibraries.push_back(tgt);
  6219. }
  6220. }
  6221. }
  6222. }
  6223. for (cmGeneratorTarget* objLib : objectLibraries) {
  6224. objLib->GetLanguages(languages, config);
  6225. }
  6226. }
  6227. bool cmGeneratorTarget::IsCSharpOnly() const
  6228. {
  6229. // Only certain target types may compile CSharp.
  6230. if (this->GetType() != cmStateEnums::SHARED_LIBRARY &&
  6231. this->GetType() != cmStateEnums::STATIC_LIBRARY &&
  6232. this->GetType() != cmStateEnums::EXECUTABLE) {
  6233. return false;
  6234. }
  6235. std::set<std::string> languages = this->GetAllConfigCompileLanguages();
  6236. // Consider an explicit linker language property, but *not* the
  6237. // computed linker language that may depend on linked targets.
  6238. cmProp linkLang = this->GetProperty("LINKER_LANGUAGE");
  6239. if (linkLang && !linkLang->empty()) {
  6240. languages.insert(*linkLang);
  6241. }
  6242. return languages.size() == 1 && languages.count("CSharp") > 0;
  6243. }
  6244. void cmGeneratorTarget::ComputeLinkImplementationLanguages(
  6245. const std::string& config, cmOptionalLinkImplementation& impl) const
  6246. {
  6247. // This target needs runtime libraries for its source languages.
  6248. std::set<std::string> languages;
  6249. // Get languages used in our source files.
  6250. this->GetLanguages(languages, config);
  6251. // Copy the set of languages to the link implementation.
  6252. impl.Languages.insert(impl.Languages.begin(), languages.begin(),
  6253. languages.end());
  6254. }
  6255. bool cmGeneratorTarget::HaveBuildTreeRPATH(const std::string& config) const
  6256. {
  6257. if (this->GetPropertyAsBool("SKIP_BUILD_RPATH")) {
  6258. return false;
  6259. }
  6260. std::string build_rpath;
  6261. if (this->GetBuildRPATH(config, build_rpath)) {
  6262. return true;
  6263. }
  6264. if (cmLinkImplementationLibraries const* impl =
  6265. this->GetLinkImplementationLibraries(config)) {
  6266. return !impl->Libraries.empty();
  6267. }
  6268. return false;
  6269. }
  6270. cmLinkImplementationLibraries const*
  6271. cmGeneratorTarget::GetLinkImplementationLibraries(
  6272. const std::string& config) const
  6273. {
  6274. return this->GetLinkImplementationLibrariesInternal(config, this);
  6275. }
  6276. cmLinkImplementationLibraries const*
  6277. cmGeneratorTarget::GetLinkImplementationLibrariesInternal(
  6278. const std::string& config, cmGeneratorTarget const* head) const
  6279. {
  6280. // There is no link implementation for imported targets.
  6281. if (this->IsImported()) {
  6282. return nullptr;
  6283. }
  6284. // Populate the link implementation libraries for this configuration.
  6285. HeadToLinkImplementationMap& hm =
  6286. this->LinkImplMap[cmSystemTools::UpperCase(config)];
  6287. // If the link implementation does not depend on the head target
  6288. // then return the one we computed first.
  6289. if (!hm.empty() && !hm.begin()->second.HadHeadSensitiveCondition) {
  6290. return &hm.begin()->second;
  6291. }
  6292. cmOptionalLinkImplementation& impl = hm[head];
  6293. if (!impl.LibrariesDone) {
  6294. impl.LibrariesDone = true;
  6295. this->ComputeLinkImplementationLibraries(config, impl, head);
  6296. }
  6297. return &impl;
  6298. }
  6299. bool cmGeneratorTarget::IsNullImpliedByLinkLibraries(
  6300. const std::string& p) const
  6301. {
  6302. return cm::contains(this->LinkImplicitNullProperties, p);
  6303. }
  6304. void cmGeneratorTarget::ComputeLinkImplementationLibraries(
  6305. const std::string& config, cmOptionalLinkImplementation& impl,
  6306. cmGeneratorTarget const* head) const
  6307. {
  6308. cmLocalGenerator const* lg = this->LocalGenerator;
  6309. cmStringRange entryRange = this->Target->GetLinkImplementationEntries();
  6310. cmBacktraceRange btRange = this->Target->GetLinkImplementationBacktraces();
  6311. cmBacktraceRange::const_iterator btIt = btRange.begin();
  6312. // Collect libraries directly linked in this configuration.
  6313. for (cmStringRange::const_iterator le = entryRange.begin(),
  6314. end = entryRange.end();
  6315. le != end; ++le, ++btIt) {
  6316. std::vector<std::string> llibs;
  6317. // Keep this logic in sync with ExpandLinkItems.
  6318. cmGeneratorExpressionDAGChecker dagChecker(this, "LINK_LIBRARIES", nullptr,
  6319. nullptr);
  6320. cmGeneratorExpression ge(*btIt);
  6321. std::unique_ptr<cmCompiledGeneratorExpression> const cge = ge.Parse(*le);
  6322. std::string const& evaluated =
  6323. cge->Evaluate(this->LocalGenerator, config, head, &dagChecker, nullptr,
  6324. this->LinkerLanguage);
  6325. cmExpandList(evaluated, llibs);
  6326. if (cge->GetHadHeadSensitiveCondition()) {
  6327. impl.HadHeadSensitiveCondition = true;
  6328. }
  6329. if (cge->GetHadContextSensitiveCondition()) {
  6330. impl.HadContextSensitiveCondition = true;
  6331. }
  6332. if (cge->GetHadLinkLanguageSensitiveCondition()) {
  6333. impl.HadLinkLanguageSensitiveCondition = true;
  6334. }
  6335. for (std::string const& lib : llibs) {
  6336. if (this->IsLinkLookupScope(lib, lg)) {
  6337. continue;
  6338. }
  6339. // Skip entries that resolve to the target itself or are empty.
  6340. std::string name = this->CheckCMP0004(lib);
  6341. if (this->GetPolicyStatusCMP0108() == cmPolicies::NEW) {
  6342. // resolve alias name
  6343. auto target = this->Makefile->FindTargetToUse(name);
  6344. if (target) {
  6345. name = target->GetName();
  6346. }
  6347. }
  6348. if (name == this->GetName() || name.empty()) {
  6349. if (name == this->GetName()) {
  6350. bool noMessage = false;
  6351. MessageType messageType = MessageType::FATAL_ERROR;
  6352. std::ostringstream e;
  6353. switch (this->GetPolicyStatusCMP0038()) {
  6354. case cmPolicies::WARN: {
  6355. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0038) << "\n";
  6356. messageType = MessageType::AUTHOR_WARNING;
  6357. } break;
  6358. case cmPolicies::OLD:
  6359. noMessage = true;
  6360. case cmPolicies::REQUIRED_IF_USED:
  6361. case cmPolicies::REQUIRED_ALWAYS:
  6362. case cmPolicies::NEW:
  6363. // Issue the fatal message.
  6364. break;
  6365. }
  6366. if (!noMessage) {
  6367. e << "Target \"" << this->GetName() << "\" links to itself.";
  6368. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  6369. messageType, e.str(), this->GetBacktrace());
  6370. if (messageType == MessageType::FATAL_ERROR) {
  6371. return;
  6372. }
  6373. }
  6374. }
  6375. continue;
  6376. }
  6377. // The entry is meant for this configuration.
  6378. impl.Libraries.emplace_back(this->ResolveLinkItem(name, *btIt, lg),
  6379. evaluated != *le);
  6380. }
  6381. std::set<std::string> const& seenProps = cge->GetSeenTargetProperties();
  6382. for (std::string const& sp : seenProps) {
  6383. if (!this->GetProperty(sp)) {
  6384. this->LinkImplicitNullProperties.insert(sp);
  6385. }
  6386. }
  6387. cge->GetMaxLanguageStandard(this, this->MaxLanguageStandards);
  6388. }
  6389. // Get the list of configurations considered to be DEBUG.
  6390. std::vector<std::string> debugConfigs =
  6391. this->Makefile->GetCMakeInstance()->GetDebugConfigs();
  6392. cmTargetLinkLibraryType linkType =
  6393. CMP0003_ComputeLinkType(config, debugConfigs);
  6394. cmTarget::LinkLibraryVectorType const& oldllibs =
  6395. this->Target->GetOriginalLinkLibraries();
  6396. for (cmTarget::LibraryID const& oldllib : oldllibs) {
  6397. if (oldllib.second != GENERAL_LibraryType && oldllib.second != linkType) {
  6398. std::string name = this->CheckCMP0004(oldllib.first);
  6399. if (name == this->GetName() || name.empty()) {
  6400. continue;
  6401. }
  6402. // Support OLD behavior for CMP0003.
  6403. impl.WrongConfigLibraries.push_back(
  6404. this->ResolveLinkItem(name, cmListFileBacktrace()));
  6405. }
  6406. }
  6407. }
  6408. cmGeneratorTarget::TargetOrString cmGeneratorTarget::ResolveTargetReference(
  6409. std::string const& name) const
  6410. {
  6411. return this->ResolveTargetReference(name, this->LocalGenerator);
  6412. }
  6413. cmGeneratorTarget::TargetOrString cmGeneratorTarget::ResolveTargetReference(
  6414. std::string const& name, cmLocalGenerator const* lg) const
  6415. {
  6416. TargetOrString resolved;
  6417. if (cmGeneratorTarget* tgt = lg->FindGeneratorTargetToUse(name)) {
  6418. resolved.Target = tgt;
  6419. } else {
  6420. resolved.String = name;
  6421. }
  6422. return resolved;
  6423. }
  6424. cmLinkItem cmGeneratorTarget::ResolveLinkItem(
  6425. std::string const& name, cmListFileBacktrace const& bt) const
  6426. {
  6427. return this->ResolveLinkItem(name, bt, this->LocalGenerator);
  6428. }
  6429. cmLinkItem cmGeneratorTarget::ResolveLinkItem(std::string const& name,
  6430. cmListFileBacktrace const& bt,
  6431. cmLocalGenerator const* lg) const
  6432. {
  6433. TargetOrString resolved = this->ResolveTargetReference(name, lg);
  6434. if (!resolved.Target) {
  6435. return cmLinkItem(resolved.String, false, bt);
  6436. }
  6437. // Check deprecation, issue message with `bt` backtrace.
  6438. if (resolved.Target->IsDeprecated()) {
  6439. std::ostringstream w;
  6440. /* clang-format off */
  6441. w <<
  6442. "The library that is being linked to, " << resolved.Target->GetName() <<
  6443. ", is marked as being deprecated by the owner. The message provided by "
  6444. "the developer is: \n" << resolved.Target->GetDeprecation() << "\n";
  6445. /* clang-format on */
  6446. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  6447. MessageType::AUTHOR_WARNING, w.str(), bt);
  6448. }
  6449. // Skip targets that will not really be linked. This is probably a
  6450. // name conflict between an external library and an executable
  6451. // within the project.
  6452. if (resolved.Target->GetType() == cmStateEnums::EXECUTABLE &&
  6453. !resolved.Target->IsExecutableWithExports()) {
  6454. return cmLinkItem(resolved.Target->GetName(), false, bt);
  6455. }
  6456. return cmLinkItem(resolved.Target, false, bt);
  6457. }
  6458. std::string cmGeneratorTarget::GetPDBDirectory(const std::string& config) const
  6459. {
  6460. if (OutputInfo const* info = this->GetOutputInfo(config)) {
  6461. // Return the directory in which the target will be built.
  6462. return info->PdbDir;
  6463. }
  6464. return "";
  6465. }
  6466. bool cmGeneratorTarget::HasImplibGNUtoMS(std::string const& config) const
  6467. {
  6468. return this->HasImportLibrary(config) && this->GetPropertyAsBool("GNUtoMS");
  6469. }
  6470. bool cmGeneratorTarget::GetImplibGNUtoMS(std::string const& config,
  6471. std::string const& gnuName,
  6472. std::string& out,
  6473. const char* newExt) const
  6474. {
  6475. if (this->HasImplibGNUtoMS(config) && gnuName.size() > 6 &&
  6476. gnuName.substr(gnuName.size() - 6) == ".dll.a") {
  6477. out = cmStrCat(cm::string_view(gnuName).substr(0, gnuName.size() - 6),
  6478. newExt ? newExt : ".lib");
  6479. return true;
  6480. }
  6481. return false;
  6482. }
  6483. bool cmGeneratorTarget::IsExecutableWithExports() const
  6484. {
  6485. return (this->GetType() == cmStateEnums::EXECUTABLE &&
  6486. this->GetPropertyAsBool("ENABLE_EXPORTS"));
  6487. }
  6488. bool cmGeneratorTarget::HasImportLibrary(std::string const& config) const
  6489. {
  6490. return (this->IsDLLPlatform() &&
  6491. (this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  6492. this->IsExecutableWithExports()) &&
  6493. // Assemblies which have only managed code do not have
  6494. // import libraries.
  6495. this->GetManagedType(config) != ManagedType::Managed) ||
  6496. (this->Target->IsAIX() && this->IsExecutableWithExports());
  6497. }
  6498. bool cmGeneratorTarget::NeedImportLibraryName(std::string const& config) const
  6499. {
  6500. return this->HasImportLibrary(config) ||
  6501. // On DLL platforms we always generate the import library name
  6502. // just in case the sources have export markup.
  6503. (this->IsDLLPlatform() &&
  6504. (this->GetType() == cmStateEnums::EXECUTABLE ||
  6505. this->GetType() == cmStateEnums::MODULE_LIBRARY));
  6506. }
  6507. std::string cmGeneratorTarget::GetSupportDirectory() const
  6508. {
  6509. std::string dir = cmStrCat(this->LocalGenerator->GetCurrentBinaryDirectory(),
  6510. "/CMakeFiles/", this->GetName());
  6511. #if defined(__VMS)
  6512. dir += "_dir";
  6513. #else
  6514. dir += ".dir";
  6515. #endif
  6516. return dir;
  6517. }
  6518. bool cmGeneratorTarget::IsLinkable() const
  6519. {
  6520. return (this->GetType() == cmStateEnums::STATIC_LIBRARY ||
  6521. this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  6522. this->GetType() == cmStateEnums::MODULE_LIBRARY ||
  6523. this->GetType() == cmStateEnums::UNKNOWN_LIBRARY ||
  6524. this->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  6525. this->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  6526. this->IsExecutableWithExports());
  6527. }
  6528. bool cmGeneratorTarget::IsFrameworkOnApple() const
  6529. {
  6530. return ((this->GetType() == cmStateEnums::SHARED_LIBRARY ||
  6531. this->GetType() == cmStateEnums::STATIC_LIBRARY) &&
  6532. this->Makefile->IsOn("APPLE") &&
  6533. this->GetPropertyAsBool("FRAMEWORK"));
  6534. }
  6535. bool cmGeneratorTarget::IsAppBundleOnApple() const
  6536. {
  6537. return (this->GetType() == cmStateEnums::EXECUTABLE &&
  6538. this->Makefile->IsOn("APPLE") &&
  6539. this->GetPropertyAsBool("MACOSX_BUNDLE"));
  6540. }
  6541. bool cmGeneratorTarget::IsXCTestOnApple() const
  6542. {
  6543. return (this->IsCFBundleOnApple() && this->GetPropertyAsBool("XCTEST"));
  6544. }
  6545. bool cmGeneratorTarget::IsCFBundleOnApple() const
  6546. {
  6547. return (this->GetType() == cmStateEnums::MODULE_LIBRARY &&
  6548. this->Makefile->IsOn("APPLE") && this->GetPropertyAsBool("BUNDLE"));
  6549. }
  6550. cmGeneratorTarget::ManagedType cmGeneratorTarget::CheckManagedType(
  6551. std::string const& propval) const
  6552. {
  6553. // The type of the managed assembly (mixed unmanaged C++ and C++/CLI,
  6554. // or only C++/CLI) does only depend on whether the property is an empty
  6555. // string or contains any value at all. In Visual Studio generators
  6556. // this propval is prepended with /clr[:] which results in:
  6557. //
  6558. // 1. propval does not exist: no /clr flag, unmanaged target, has import
  6559. // lib
  6560. // 2. empty propval: add /clr as flag, mixed unmanaged/managed
  6561. // target, has import lib
  6562. // 3. any value (safe,pure): add /clr:[propval] as flag, target with
  6563. // managed code only, no import lib
  6564. return propval.empty() ? ManagedType::Mixed : ManagedType::Managed;
  6565. }
  6566. cmGeneratorTarget::ManagedType cmGeneratorTarget::GetManagedType(
  6567. const std::string& config) const
  6568. {
  6569. // Only libraries and executables can be managed targets.
  6570. if (this->GetType() > cmStateEnums::SHARED_LIBRARY) {
  6571. return ManagedType::Undefined;
  6572. }
  6573. if (this->GetType() == cmStateEnums::STATIC_LIBRARY) {
  6574. return ManagedType::Native;
  6575. }
  6576. // Check imported target.
  6577. if (this->IsImported()) {
  6578. if (cmGeneratorTarget::ImportInfo const* info =
  6579. this->GetImportInfo(config)) {
  6580. return info->Managed;
  6581. }
  6582. return ManagedType::Undefined;
  6583. }
  6584. // Check for explicitly set clr target property.
  6585. if (cmProp clr = this->GetProperty("COMMON_LANGUAGE_RUNTIME")) {
  6586. return this->CheckManagedType(*clr);
  6587. }
  6588. // C# targets are always managed. This language specific check
  6589. // is added to avoid that the COMMON_LANGUAGE_RUNTIME target property
  6590. // has to be set manually for C# targets.
  6591. return this->IsCSharpOnly() ? ManagedType::Managed : ManagedType::Native;
  6592. }