cmGeneratorTarget.cxx 215 KB

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