cmGeneratorTarget.cxx 228 KB

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