cmGeneratorTarget.cxx 248 KB

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