cmGeneratorTarget.cxx 221 KB

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