cmGeneratorTarget.cxx 254 KB

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