cmGeneratorTarget.cxx 216 KB

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