cmGeneratorTarget.cxx 244 KB

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