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