cmGeneratorTarget.cxx 254 KB

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