cmGeneratorTarget.cxx 255 KB

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