cmGeneratorTarget.cxx 238 KB

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