cmGeneratorTarget.cxx 191 KB

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