cmGeneratorTarget.cxx 216 KB

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