cmGeneratorTarget.cxx 238 KB

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