cmGeneratorTarget.cxx 215 KB

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