kconfiglib.py 249 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030
  1. # Copyright (c) 2011-2019, Ulf Magnusson
  2. # SPDX-License-Identifier: ISC
  3. """
  4. Overview
  5. ========
  6. Kconfiglib is a Python 2/3 library for scripting and extracting information
  7. from Kconfig (https://www.kernel.org/doc/Documentation/kbuild/kconfig-language.txt)
  8. configuration systems.
  9. See the homepage at https://github.com/ulfalizer/Kconfiglib for a longer
  10. overview.
  11. Since Kconfiglib 12.0.0, the library version is available in
  12. kconfiglib.VERSION, which is a (<major>, <minor>, <patch>) tuple, e.g.
  13. (12, 0, 0).
  14. Using Kconfiglib on the Linux kernel with the Makefile targets
  15. ==============================================================
  16. For the Linux kernel, a handy interface is provided by the
  17. scripts/kconfig/Makefile patch, which can be applied with either 'git am' or
  18. the 'patch' utility:
  19. $ wget -qO- https://raw.githubusercontent.com/ulfalizer/Kconfiglib/master/makefile.patch | git am
  20. $ wget -qO- https://raw.githubusercontent.com/ulfalizer/Kconfiglib/master/makefile.patch | patch -p1
  21. Warning: Not passing -p1 to patch will cause the wrong file to be patched.
  22. Please tell me if the patch does not apply. It should be trivial to apply
  23. manually, as it's just a block of text that needs to be inserted near the other
  24. *conf: targets in scripts/kconfig/Makefile.
  25. Look further down for a motivation for the Makefile patch and for instructions
  26. on how you can use Kconfiglib without it.
  27. If you do not wish to install Kconfiglib via pip, the Makefile patch is set up
  28. so that you can also just clone Kconfiglib into the kernel root:
  29. $ git clone git://github.com/ulfalizer/Kconfiglib.git
  30. $ git am Kconfiglib/makefile.patch (or 'patch -p1 < Kconfiglib/makefile.patch')
  31. Warning: The directory name Kconfiglib/ is significant in this case, because
  32. it's added to PYTHONPATH by the new targets in makefile.patch.
  33. The targets added by the Makefile patch are described in the following
  34. sections.
  35. make kmenuconfig
  36. ----------------
  37. This target runs the curses menuconfig interface with Python 3. As of
  38. Kconfiglib 12.2.0, both Python 2 and Python 3 are supported (previously, only
  39. Python 3 was supported, so this was a backport).
  40. make guiconfig
  41. --------------
  42. This target runs the Tkinter menuconfig interface. Both Python 2 and Python 3
  43. are supported. To change the Python interpreter used, pass
  44. PYTHONCMD=<executable> to 'make'. The default is 'python'.
  45. make [ARCH=<arch>] iscriptconfig
  46. --------------------------------
  47. This target gives an interactive Python prompt where a Kconfig instance has
  48. been preloaded and is available in 'kconf'. To change the Python interpreter
  49. used, pass PYTHONCMD=<executable> to 'make'. The default is 'python'.
  50. To get a feel for the API, try evaluating and printing the symbols in
  51. kconf.defined_syms, and explore the MenuNode menu tree starting at
  52. kconf.top_node by following 'next' and 'list' pointers.
  53. The item contained in a menu node is found in MenuNode.item (note that this can
  54. be one of the constants kconfiglib.MENU and kconfiglib.COMMENT), and all
  55. symbols and choices have a 'nodes' attribute containing their menu nodes
  56. (usually only one). Printing a menu node will print its item, in Kconfig
  57. format.
  58. If you want to look up a symbol by name, use the kconf.syms dictionary.
  59. make scriptconfig SCRIPT=<script> [SCRIPT_ARG=<arg>]
  60. ----------------------------------------------------
  61. This target runs the Python script given by the SCRIPT parameter on the
  62. configuration. sys.argv[1] holds the name of the top-level Kconfig file
  63. (currently always "Kconfig" in practice), and sys.argv[2] holds the SCRIPT_ARG
  64. argument, if given.
  65. See the examples/ subdirectory for example scripts.
  66. make dumpvarsconfig
  67. -------------------
  68. This target prints a list of all environment variables referenced from the
  69. Kconfig files, together with their values. See the
  70. Kconfiglib/examples/dumpvars.py script.
  71. Only environment variables that are referenced via the Kconfig preprocessor
  72. $(FOO) syntax are included. The preprocessor was added in Linux 4.18.
  73. Using Kconfiglib without the Makefile targets
  74. =============================================
  75. The make targets are only needed to pick up environment variables exported from
  76. the Kbuild makefiles and referenced inside Kconfig files, via e.g.
  77. 'source "arch/$(SRCARCH)/Kconfig" and commands run via '$(shell,...)'.
  78. These variables are referenced as of writing (Linux 4.18), together with sample
  79. values:
  80. srctree (.)
  81. ARCH (x86)
  82. SRCARCH (x86)
  83. KERNELVERSION (4.18.0)
  84. CC (gcc)
  85. HOSTCC (gcc)
  86. HOSTCXX (g++)
  87. CC_VERSION_TEXT (gcc (Ubuntu 7.3.0-16ubuntu3) 7.3.0)
  88. Older kernels only reference ARCH, SRCARCH, and KERNELVERSION.
  89. If your kernel is recent enough (4.18+), you can get a list of referenced
  90. environment variables via 'make dumpvarsconfig' (see above). Note that this
  91. command is added by the Makefile patch.
  92. To run Kconfiglib without the Makefile patch, set the environment variables
  93. manually:
  94. $ srctree=. ARCH=x86 SRCARCH=x86 KERNELVERSION=`make kernelversion` ... python(3)
  95. >>> import kconfiglib
  96. >>> kconf = kconfiglib.Kconfig() # filename defaults to "Kconfig"
  97. Search the top-level Makefile for "Additional ARCH settings" to see other
  98. possibilities for ARCH and SRCARCH.
  99. Intro to symbol values
  100. ======================
  101. Kconfiglib has the same assignment semantics as the C implementation.
  102. Any symbol can be assigned a value by the user (via Kconfig.load_config() or
  103. Symbol.set_value()), but this user value is only respected if the symbol is
  104. visible, which corresponds to it (currently) being visible in the menuconfig
  105. interface.
  106. For symbols with prompts, the visibility of the symbol is determined by the
  107. condition on the prompt. Symbols without prompts are never visible, so setting
  108. a user value on them is pointless. A warning will be printed by default if
  109. Symbol.set_value() is called on a promptless symbol. Assignments to promptless
  110. symbols are normal within a .config file, so no similar warning will be printed
  111. by load_config().
  112. Dependencies from parents and 'if'/'depends on' are propagated to properties,
  113. including prompts, so these two configurations are logically equivalent:
  114. (1)
  115. menu "menu"
  116. depends on A
  117. if B
  118. config FOO
  119. tristate "foo" if D
  120. default y
  121. depends on C
  122. endif
  123. endmenu
  124. (2)
  125. menu "menu"
  126. depends on A
  127. config FOO
  128. tristate "foo" if A && B && C && D
  129. default y if A && B && C
  130. endmenu
  131. In this example, A && B && C && D (the prompt condition) needs to be non-n for
  132. FOO to be visible (assignable). If its value is m, the symbol can only be
  133. assigned the value m: The visibility sets an upper bound on the value that can
  134. be assigned by the user, and any higher user value will be truncated down.
  135. 'default' properties are independent of the visibility, though a 'default' will
  136. often get the same condition as the prompt due to dependency propagation.
  137. 'default' properties are used if the symbol is not visible or has no user
  138. value.
  139. Symbols with no user value (or that have a user value but are not visible) and
  140. no (active) 'default' default to n for bool/tristate symbols, and to the empty
  141. string for other symbol types.
  142. 'select' works similarly to symbol visibility, but sets a lower bound on the
  143. value of the symbol. The lower bound is determined by the value of the
  144. select*ing* symbol. 'select' does not respect visibility, so non-visible
  145. symbols can be forced to a particular (minimum) value by a select as well.
  146. For non-bool/tristate symbols, it only matters whether the visibility is n or
  147. non-n: m visibility acts the same as y visibility.
  148. Conditions on 'default' and 'select' work in mostly intuitive ways. If the
  149. condition is n, the 'default' or 'select' is disabled. If it is m, the
  150. 'default' or 'select' value (the value of the selecting symbol) is truncated
  151. down to m.
  152. When writing a configuration with Kconfig.write_config(), only symbols that are
  153. visible, have an (active) default, or are selected will get written out (note
  154. that this includes all symbols that would accept user values). Kconfiglib
  155. matches the .config format produced by the C implementations down to the
  156. character. This eases testing.
  157. For a visible bool/tristate symbol FOO with value n, this line is written to
  158. .config:
  159. # CONFIG_FOO is not set
  160. The point is to remember the user n selection (which might differ from the
  161. default value the symbol would get), while at the same sticking to the rule
  162. that undefined corresponds to n (.config uses Makefile format, making the line
  163. above a comment). When the .config file is read back in, this line will be
  164. treated the same as the following assignment:
  165. CONFIG_FOO=n
  166. In Kconfiglib, the set of (currently) assignable values for a bool/tristate
  167. symbol appear in Symbol.assignable. For other symbol types, just check if
  168. sym.visibility is non-0 (non-n) to see whether the user value will have an
  169. effect.
  170. Intro to the menu tree
  171. ======================
  172. The menu structure, as seen in e.g. menuconfig, is represented by a tree of
  173. MenuNode objects. The top node of the configuration corresponds to an implicit
  174. top-level menu, the title of which is shown at the top in the standard
  175. menuconfig interface. (The title is also available in Kconfig.mainmenu_text in
  176. Kconfiglib.)
  177. The top node is found in Kconfig.top_node. From there, you can visit child menu
  178. nodes by following the 'list' pointer, and any following menu nodes by
  179. following the 'next' pointer. Usually, a non-None 'list' pointer indicates a
  180. menu or Choice, but menu nodes for symbols can sometimes have a non-None 'list'
  181. pointer too due to submenus created implicitly from dependencies.
  182. MenuNode.item is either a Symbol or a Choice object, or one of the constants
  183. MENU and COMMENT. The prompt of the menu node can be found in MenuNode.prompt,
  184. which also holds the title for menus and comments. For Symbol and Choice,
  185. MenuNode.help holds the help text (if any, otherwise None).
  186. Most symbols will only have a single menu node. A symbol defined in multiple
  187. locations will have one menu node for each location. The list of menu nodes for
  188. a Symbol or Choice can be found in the Symbol/Choice.nodes attribute.
  189. Note that prompts and help texts for symbols and choices are stored in their
  190. menu node(s) rather than in the Symbol or Choice objects themselves. This makes
  191. it possible to define a symbol in multiple locations with a different prompt or
  192. help text in each location. To get the help text or prompt for a symbol with a
  193. single menu node, do sym.nodes[0].help and sym.nodes[0].prompt, respectively.
  194. The prompt is a (text, condition) tuple, where condition determines the
  195. visibility (see 'Intro to expressions' below).
  196. This organization mirrors the C implementation. MenuNode is called
  197. 'struct menu' there, but I thought "menu" was a confusing name.
  198. It is possible to give a Choice a name and define it in multiple locations,
  199. hence why Choice.nodes is also a list.
  200. As a convenience, the properties added at a particular definition location are
  201. available on the MenuNode itself, in e.g. MenuNode.defaults. This is helpful
  202. when generating documentation, so that symbols/choices defined in multiple
  203. locations can be shown with the correct properties at each location.
  204. Intro to expressions
  205. ====================
  206. Expressions can be evaluated with the expr_value() function and printed with
  207. the expr_str() function (these are used internally as well). Evaluating an
  208. expression always yields a tristate value, where n, m, and y are represented as
  209. 0, 1, and 2, respectively.
  210. The following table should help you figure out how expressions are represented.
  211. A, B, C, ... are symbols (Symbol instances), NOT is the kconfiglib.NOT
  212. constant, etc.
  213. Expression Representation
  214. ---------- --------------
  215. A A
  216. "A" A (constant symbol)
  217. !A (NOT, A)
  218. A && B (AND, A, B)
  219. A && B && C (AND, A, (AND, B, C))
  220. A || B (OR, A, B)
  221. A || (B && C && D) (OR, A, (AND, B, (AND, C, D)))
  222. A = B (EQUAL, A, B)
  223. A != "foo" (UNEQUAL, A, foo (constant symbol))
  224. A && B = C && D (AND, A, (AND, (EQUAL, B, C), D))
  225. n Kconfig.n (constant symbol)
  226. m Kconfig.m (constant symbol)
  227. y Kconfig.y (constant symbol)
  228. "y" Kconfig.y (constant symbol)
  229. Strings like "foo" in 'default "foo"' or 'depends on SYM = "foo"' are
  230. represented as constant symbols, so the only values that appear in expressions
  231. are symbols***. This mirrors the C implementation.
  232. ***For choice symbols, the parent Choice will appear in expressions as well,
  233. but it's usually invisible as the value interfaces of Symbol and Choice are
  234. identical. This mirrors the C implementation and makes different choice modes
  235. "just work".
  236. Manual evaluation examples:
  237. - The value of A && B is min(A.tri_value, B.tri_value)
  238. - The value of A || B is max(A.tri_value, B.tri_value)
  239. - The value of !A is 2 - A.tri_value
  240. - The value of A = B is 2 (y) if A.str_value == B.str_value, and 0 (n)
  241. otherwise. Note that str_value is used here instead of tri_value.
  242. For constant (as well as undefined) symbols, str_value matches the name of
  243. the symbol. This mirrors the C implementation and explains why
  244. 'depends on SYM = "foo"' above works as expected.
  245. n/m/y are automatically converted to the corresponding constant symbols
  246. "n"/"m"/"y" (Kconfig.n/m/y) during parsing.
  247. Kconfig.const_syms is a dictionary like Kconfig.syms but for constant symbols.
  248. If a condition is missing (e.g., <cond> when the 'if <cond>' is removed from
  249. 'default A if <cond>'), it is actually Kconfig.y. The standard __str__()
  250. functions just avoid printing 'if y' conditions to give cleaner output.
  251. Kconfig extensions
  252. ==================
  253. Kconfiglib includes a couple of Kconfig extensions:
  254. 'source' with relative path
  255. ---------------------------
  256. The 'rsource' statement sources Kconfig files with a path relative to directory
  257. of the Kconfig file containing the 'rsource' statement, instead of relative to
  258. the project root.
  259. Consider following directory tree:
  260. Project
  261. +--Kconfig
  262. |
  263. +--src
  264. +--Kconfig
  265. |
  266. +--SubSystem1
  267. +--Kconfig
  268. |
  269. +--ModuleA
  270. +--Kconfig
  271. In this example, assume that src/SubSystem1/Kconfig wants to source
  272. src/SubSystem1/ModuleA/Kconfig.
  273. With 'source', this statement would be used:
  274. source "src/SubSystem1/ModuleA/Kconfig"
  275. With 'rsource', this turns into
  276. rsource "ModuleA/Kconfig"
  277. If an absolute path is given to 'rsource', it acts the same as 'source'.
  278. 'rsource' can be used to create "position-independent" Kconfig trees that can
  279. be moved around freely.
  280. Globbing 'source'
  281. -----------------
  282. 'source' and 'rsource' accept glob patterns, sourcing all matching Kconfig
  283. files. They require at least one matching file, raising a KconfigError
  284. otherwise.
  285. For example, the following statement might source sub1/foofoofoo and
  286. sub2/foobarfoo:
  287. source "sub[12]/foo*foo"
  288. The glob patterns accepted are the same as for the standard glob.glob()
  289. function.
  290. Two additional statements are provided for cases where it's acceptable for a
  291. pattern to match no files: 'osource' and 'orsource' (the o is for "optional").
  292. For example, the following statements will be no-ops if neither "foo" nor any
  293. files matching "bar*" exist:
  294. osource "foo"
  295. osource "bar*"
  296. 'orsource' does a relative optional source.
  297. 'source' and 'osource' are analogous to 'include' and '-include' in Make.
  298. Generalized def_* keywords
  299. --------------------------
  300. def_int, def_hex, and def_string are available in addition to def_bool and
  301. def_tristate, allowing int, hex, and string symbols to be given a type and a
  302. default at the same time.
  303. Extra optional warnings
  304. -----------------------
  305. Some optional warnings can be controlled via environment variables:
  306. - KCONFIG_WARN_UNDEF: If set to 'y', warnings will be generated for all
  307. references to undefined symbols within Kconfig files. The only gotcha is
  308. that all hex literals must be prefixed with "0x" or "0X", to make it
  309. possible to distinguish them from symbol references.
  310. Some projects (e.g. the Linux kernel) use multiple Kconfig trees with many
  311. shared Kconfig files, leading to some safe undefined symbol references.
  312. KCONFIG_WARN_UNDEF is useful in projects that only have a single Kconfig
  313. tree though.
  314. KCONFIG_STRICT is an older alias for this environment variable, supported
  315. for backwards compatibility.
  316. - KCONFIG_WARN_UNDEF_ASSIGN: If set to 'y', warnings will be generated for
  317. all assignments to undefined symbols within .config files. By default, no
  318. such warnings are generated.
  319. This warning can also be enabled/disabled via the Kconfig.warn_assign_undef
  320. variable.
  321. Preprocessor user functions defined in Python
  322. ---------------------------------------------
  323. Preprocessor functions can be defined in Python, which makes it simple to
  324. integrate information from existing Python tools into Kconfig (e.g. to have
  325. Kconfig symbols depend on hardware information stored in some other format).
  326. Putting a Python module named kconfigfunctions(.py) anywhere in sys.path will
  327. cause it to be imported by Kconfiglib (in Kconfig.__init__()). Note that
  328. sys.path can be customized via PYTHONPATH, and includes the directory of the
  329. module being run by default, as well as installation directories.
  330. If the KCONFIG_FUNCTIONS environment variable is set, it gives a different
  331. module name to use instead of 'kconfigfunctions'.
  332. The imported module is expected to define a global dictionary named 'functions'
  333. that maps function names to Python functions, as follows:
  334. def my_fn(kconf, name, arg_1, arg_2, ...):
  335. # kconf:
  336. # Kconfig instance
  337. #
  338. # name:
  339. # Name of the user-defined function ("my-fn"). Think argv[0].
  340. #
  341. # arg_1, arg_2, ...:
  342. # Arguments passed to the function from Kconfig (strings)
  343. #
  344. # Returns a string to be substituted as the result of calling the
  345. # function
  346. ...
  347. def my_other_fn(kconf, name, arg_1, arg_2, ...):
  348. ...
  349. functions = {
  350. "my-fn": (my_fn, <min.args>, <max.args>/None),
  351. "my-other-fn": (my_other_fn, <min.args>, <max.args>/None),
  352. ...
  353. }
  354. ...
  355. <min.args> and <max.args> are the minimum and maximum number of arguments
  356. expected by the function (excluding the implicit 'name' argument). If
  357. <max.args> is None, there is no upper limit to the number of arguments. Passing
  358. an invalid number of arguments will generate a KconfigError exception.
  359. Functions can access the current parsing location as kconf.filename/linenr.
  360. Accessing other fields of the Kconfig object is not safe. See the warning
  361. below.
  362. Keep in mind that for a variable defined like 'foo = $(fn)', 'fn' will be
  363. called only when 'foo' is expanded. If 'fn' uses the parsing location and the
  364. intent is to use the location of the assignment, you want 'foo := $(fn)'
  365. instead, which calls the function immediately.
  366. Once defined, user functions can be called from Kconfig in the same way as
  367. other preprocessor functions:
  368. config FOO
  369. ...
  370. depends on $(my-fn,arg1,arg2)
  371. If my_fn() returns "n", this will result in
  372. config FOO
  373. ...
  374. depends on n
  375. Warning
  376. *******
  377. User-defined preprocessor functions are called as they're encountered at parse
  378. time, before all Kconfig files have been processed, and before the menu tree
  379. has been finalized. There are no guarantees that accessing Kconfig symbols or
  380. the menu tree via the 'kconf' parameter will work, and it could potentially
  381. lead to a crash.
  382. Preferably, user-defined functions should be stateless.
  383. Feedback
  384. ========
  385. Send bug reports, suggestions, and questions to ulfalizer a.t Google's email
  386. service, or open a ticket on the GitHub page.
  387. """
  388. import errno
  389. import importlib
  390. import os
  391. import re
  392. import sys
  393. # Get rid of some attribute lookups. These are obvious in context.
  394. from glob import iglob
  395. from os.path import dirname, exists, expandvars, islink, join, realpath
  396. VERSION = (12, 14, 0)
  397. # File layout:
  398. #
  399. # Public classes
  400. # Public functions
  401. # Internal functions
  402. # Global constants
  403. # Line length: 79 columns
  404. #
  405. # Public classes
  406. #
  407. class Kconfig(object):
  408. """
  409. Represents a Kconfig configuration, e.g. for x86 or ARM. This is the set of
  410. symbols, choices, and menu nodes appearing in the configuration. Creating
  411. any number of Kconfig objects (including for different architectures) is
  412. safe. Kconfiglib doesn't keep any global state.
  413. The following attributes are available. They should be treated as
  414. read-only, and some are implemented through @property magic.
  415. syms:
  416. A dictionary with all symbols in the configuration, indexed by name. Also
  417. includes all symbols that are referenced in expressions but never
  418. defined, except for constant (quoted) symbols.
  419. Undefined symbols can be recognized by Symbol.nodes being empty -- see
  420. the 'Intro to the menu tree' section in the module docstring.
  421. const_syms:
  422. A dictionary like 'syms' for constant (quoted) symbols
  423. named_choices:
  424. A dictionary like 'syms' for named choices (choice FOO)
  425. defined_syms:
  426. A list with all defined symbols, in the same order as they appear in the
  427. Kconfig files. Symbols defined in multiple locations appear multiple
  428. times.
  429. Note: You probably want to use 'unique_defined_syms' instead. This
  430. attribute is mostly maintained for backwards compatibility.
  431. unique_defined_syms:
  432. A list like 'defined_syms', but with duplicates removed. Just the first
  433. instance is kept for symbols defined in multiple locations. Kconfig order
  434. is preserved otherwise.
  435. Using this attribute instead of 'defined_syms' can save work, and
  436. automatically gives reasonable behavior when writing configuration output
  437. (symbols defined in multiple locations only generate output once, while
  438. still preserving Kconfig order for readability).
  439. choices:
  440. A list with all choices, in the same order as they appear in the Kconfig
  441. files.
  442. Note: You probably want to use 'unique_choices' instead. This attribute
  443. is mostly maintained for backwards compatibility.
  444. unique_choices:
  445. Analogous to 'unique_defined_syms', for choices. Named choices can have
  446. multiple definition locations.
  447. menus:
  448. A list with all menus, in the same order as they appear in the Kconfig
  449. files
  450. comments:
  451. A list with all comments, in the same order as they appear in the Kconfig
  452. files
  453. kconfig_filenames:
  454. A list with the filenames of all Kconfig files included in the
  455. configuration, relative to $srctree (or relative to the current directory
  456. if $srctree isn't set), except absolute paths (e.g.
  457. 'source "/foo/Kconfig"') are kept as-is.
  458. The files are listed in the order they are source'd, starting with the
  459. top-level Kconfig file. If a file is source'd multiple times, it will
  460. appear multiple times. Use set() to get unique filenames.
  461. Note that Kconfig.sync_deps() already indirectly catches any file
  462. modifications that change configuration output.
  463. env_vars:
  464. A set() with the names of all environment variables referenced in the
  465. Kconfig files.
  466. Only environment variables referenced with the preprocessor $(FOO) syntax
  467. will be registered. The older $FOO syntax is only supported for backwards
  468. compatibility.
  469. Also note that $(FOO) won't be registered unless the environment variable
  470. $FOO is actually set. If it isn't, $(FOO) is an expansion of an unset
  471. preprocessor variable (which gives the empty string).
  472. Another gotcha is that environment variables referenced in the values of
  473. recursively expanded preprocessor variables (those defined with =) will
  474. only be registered if the variable is actually used (expanded) somewhere.
  475. The note from the 'kconfig_filenames' documentation applies here too.
  476. n/m/y:
  477. The predefined constant symbols n/m/y. Also available in const_syms.
  478. modules:
  479. The Symbol instance for the modules symbol. Currently hardcoded to
  480. MODULES, which is backwards compatible. Kconfiglib will warn if
  481. 'option modules' is set on some other symbol. Tell me if you need proper
  482. 'option modules' support.
  483. 'modules' is never None. If the MODULES symbol is not explicitly defined,
  484. its tri_value will be 0 (n), as expected.
  485. A simple way to enable modules is to do 'kconf.modules.set_value(2)'
  486. (provided the MODULES symbol is defined and visible). Modules are
  487. disabled by default in the kernel Kconfig files as of writing, though
  488. nearly all defconfig files enable them (with 'CONFIG_MODULES=y').
  489. defconfig_list:
  490. The Symbol instance for the 'option defconfig_list' symbol, or None if no
  491. defconfig_list symbol exists. The defconfig filename derived from this
  492. symbol can be found in Kconfig.defconfig_filename.
  493. defconfig_filename:
  494. The filename given by the defconfig_list symbol. This is taken from the
  495. first 'default' with a satisfied condition where the specified file
  496. exists (can be opened for reading). If a defconfig file foo/defconfig is
  497. not found and $srctree was set when the Kconfig was created,
  498. $srctree/foo/defconfig is looked up as well.
  499. 'defconfig_filename' is None if either no defconfig_list symbol exists,
  500. or if the defconfig_list symbol has no 'default' with a satisfied
  501. condition that specifies a file that exists.
  502. Gotcha: scripts/kconfig/Makefile might pass --defconfig=<defconfig> to
  503. scripts/kconfig/conf when running e.g. 'make defconfig'. This option
  504. overrides the defconfig_list symbol, meaning defconfig_filename might not
  505. always match what 'make defconfig' would use.
  506. top_node:
  507. The menu node (see the MenuNode class) of the implicit top-level menu.
  508. Acts as the root of the menu tree.
  509. mainmenu_text:
  510. The prompt (title) of the top menu (top_node). Defaults to "Main menu".
  511. Can be changed with the 'mainmenu' statement (see kconfig-language.txt).
  512. variables:
  513. A dictionary with all preprocessor variables, indexed by name. See the
  514. Variable class.
  515. warn:
  516. Set this variable to True/False to enable/disable warnings. See
  517. Kconfig.__init__().
  518. When 'warn' is False, the values of the other warning-related variables
  519. are ignored.
  520. This variable as well as the other warn* variables can be read to check
  521. the current warning settings.
  522. warn_to_stderr:
  523. Set this variable to True/False to enable/disable warnings on stderr. See
  524. Kconfig.__init__().
  525. warn_assign_undef:
  526. Set this variable to True to generate warnings for assignments to
  527. undefined symbols in configuration files.
  528. This variable is False by default unless the KCONFIG_WARN_UNDEF_ASSIGN
  529. environment variable was set to 'y' when the Kconfig instance was
  530. created.
  531. warn_assign_override:
  532. Set this variable to True to generate warnings for multiple assignments
  533. to the same symbol in configuration files, where the assignments set
  534. different values (e.g. CONFIG_FOO=m followed by CONFIG_FOO=y, where the
  535. last value would get used).
  536. This variable is True by default. Disabling it might be useful when
  537. merging configurations.
  538. warn_assign_redun:
  539. Like warn_assign_override, but for multiple assignments setting a symbol
  540. to the same value.
  541. This variable is True by default. Disabling it might be useful when
  542. merging configurations.
  543. warnings:
  544. A list of strings containing all warnings that have been generated, for
  545. cases where more flexibility is needed.
  546. See the 'warn_to_stderr' parameter to Kconfig.__init__() and the
  547. Kconfig.warn_to_stderr variable as well. Note that warnings still get
  548. added to Kconfig.warnings when 'warn_to_stderr' is True.
  549. Just as for warnings printed to stderr, only warnings that are enabled
  550. will get added to Kconfig.warnings. See the various Kconfig.warn*
  551. variables.
  552. missing_syms:
  553. A list with (name, value) tuples for all assignments to undefined symbols
  554. within the most recently loaded .config file(s). 'name' is the symbol
  555. name without the 'CONFIG_' prefix. 'value' is a string that gives the
  556. right-hand side of the assignment verbatim.
  557. See Kconfig.load_config() as well.
  558. srctree:
  559. The value of the $srctree environment variable when the configuration was
  560. loaded, or the empty string if $srctree wasn't set. This gives nice
  561. behavior with os.path.join(), which treats "" as the current directory,
  562. without adding "./".
  563. Kconfig files are looked up relative to $srctree (unless absolute paths
  564. are used), and .config files are looked up relative to $srctree if they
  565. are not found in the current directory. This is used to support
  566. out-of-tree builds. The C tools use this environment variable in the same
  567. way.
  568. Changing $srctree after creating the Kconfig instance has no effect. Only
  569. the value when the configuration is loaded matters. This avoids surprises
  570. if multiple configurations are loaded with different values for $srctree.
  571. config_prefix:
  572. The value of the $CONFIG_ environment variable when the configuration was
  573. loaded. This is the prefix used (and expected) on symbol names in .config
  574. files and C headers. Defaults to "CONFIG_". Used in the same way in the C
  575. tools.
  576. Like for srctree, only the value of $CONFIG_ when the configuration is
  577. loaded matters.
  578. filename/linenr:
  579. The current parsing location, for use in Python preprocessor functions.
  580. See the module docstring.
  581. """
  582. __slots__ = (
  583. "_encoding",
  584. "_functions",
  585. "_set_match",
  586. "_srctree_prefix",
  587. "_unset_match",
  588. "_warn_assign_no_prompt",
  589. "choices",
  590. "comments",
  591. "config_prefix",
  592. "const_syms",
  593. "defconfig_list",
  594. "defined_syms",
  595. "env_vars",
  596. "kconfig_filenames",
  597. "m",
  598. "menus",
  599. "missing_syms",
  600. "modules",
  601. "n",
  602. "named_choices",
  603. "srctree",
  604. "syms",
  605. "top_node",
  606. "unique_choices",
  607. "unique_defined_syms",
  608. "variables",
  609. "warn",
  610. "warn_assign_override",
  611. "warn_assign_redun",
  612. "warn_assign_undef",
  613. "warn_to_stderr",
  614. "warnings",
  615. "y",
  616. # Parsing-related
  617. "_parsing_kconfigs",
  618. "_readline",
  619. "filename",
  620. "linenr",
  621. "_include_path",
  622. "_filestack",
  623. "_line",
  624. "_tokens",
  625. "_tokens_i",
  626. "_reuse_tokens",
  627. )
  628. #
  629. # Public interface
  630. #
  631. def __init__(self, filename="Kconfig", warn=True, warn_to_stderr=True,
  632. encoding="utf-8"):
  633. """
  634. Creates a new Kconfig object by parsing Kconfig files.
  635. Note that Kconfig files are not the same as .config files (which store
  636. configuration symbol values).
  637. See the module docstring for some environment variables that influence
  638. default warning settings (KCONFIG_WARN_UNDEF and
  639. KCONFIG_WARN_UNDEF_ASSIGN).
  640. Raises KconfigError on syntax/semantic errors, and OSError or (possibly
  641. a subclass of) IOError on IO errors ('errno', 'strerror', and
  642. 'filename' are available). Note that IOError is an alias for OSError on
  643. Python 3, so it's enough to catch OSError there. If you need Python 2/3
  644. compatibility, it's easiest to catch EnvironmentError, which is a
  645. common base class of OSError/IOError on Python 2 and an alias for
  646. OSError on Python 3.
  647. filename (default: "Kconfig"):
  648. The Kconfig file to load. For the Linux kernel, you'll want "Kconfig"
  649. from the top-level directory, as environment variables will make sure
  650. the right Kconfig is included from there (arch/$SRCARCH/Kconfig as of
  651. writing).
  652. If $srctree is set, 'filename' will be looked up relative to it.
  653. $srctree is also used to look up source'd files within Kconfig files.
  654. See the class documentation.
  655. If you are using Kconfiglib via 'make scriptconfig', the filename of
  656. the base base Kconfig file will be in sys.argv[1]. It's currently
  657. always "Kconfig" in practice.
  658. warn (default: True):
  659. True if warnings related to this configuration should be generated.
  660. This can be changed later by setting Kconfig.warn to True/False. It
  661. is provided as a constructor argument since warnings might be
  662. generated during parsing.
  663. See the other Kconfig.warn_* variables as well, which enable or
  664. suppress certain warnings when warnings are enabled.
  665. All generated warnings are added to the Kconfig.warnings list. See
  666. the class documentation.
  667. warn_to_stderr (default: True):
  668. True if warnings should be printed to stderr in addition to being
  669. added to Kconfig.warnings.
  670. This can be changed later by setting Kconfig.warn_to_stderr to
  671. True/False.
  672. encoding (default: "utf-8"):
  673. The encoding to use when reading and writing files, and when decoding
  674. output from commands run via $(shell). If None, the encoding
  675. specified in the current locale will be used.
  676. The "utf-8" default avoids exceptions on systems that are configured
  677. to use the C locale, which implies an ASCII encoding.
  678. This parameter has no effect on Python 2, due to implementation
  679. issues (regular strings turning into Unicode strings, which are
  680. distinct in Python 2). Python 2 doesn't decode regular strings
  681. anyway.
  682. Related PEP: https://www.python.org/dev/peps/pep-0538/
  683. """
  684. self._encoding = encoding
  685. self.srctree = os.getenv("srctree", "")
  686. # A prefix we can reliably strip from glob() results to get a filename
  687. # relative to $srctree. relpath() can cause issues for symlinks,
  688. # because it assumes symlink/../foo is the same as foo/.
  689. self._srctree_prefix = realpath(self.srctree) + os.sep
  690. self.warn = warn
  691. self.warn_to_stderr = warn_to_stderr
  692. self.warn_assign_undef = os.getenv("KCONFIG_WARN_UNDEF_ASSIGN") == "y"
  693. self.warn_assign_override = True
  694. self.warn_assign_redun = True
  695. self._warn_assign_no_prompt = True
  696. self.warnings = []
  697. self.config_prefix = os.getenv("CONFIG_", "CONFIG_")
  698. # Regular expressions for parsing .config files
  699. self._set_match = _re_match(self.config_prefix + r"([^=]+)=(.*)")
  700. self._unset_match = _re_match(r"# {}([^ ]+) is not set".format(
  701. self.config_prefix))
  702. self.syms = {}
  703. self.const_syms = {}
  704. self.defined_syms = []
  705. self.missing_syms = []
  706. self.named_choices = {}
  707. self.choices = []
  708. self.menus = []
  709. self.comments = []
  710. for nmy in "n", "m", "y":
  711. sym = Symbol()
  712. sym.kconfig = self
  713. sym.name = nmy
  714. sym.is_constant = True
  715. sym.orig_type = TRISTATE
  716. sym._cached_tri_val = STR_TO_TRI[nmy]
  717. self.const_syms[nmy] = sym
  718. self.n = self.const_syms["n"]
  719. self.m = self.const_syms["m"]
  720. self.y = self.const_syms["y"]
  721. # Make n/m/y well-formed symbols
  722. for nmy in "n", "m", "y":
  723. sym = self.const_syms[nmy]
  724. sym.rev_dep = sym.weak_rev_dep = sym.direct_dep = self.n
  725. # Maps preprocessor variables names to Variable instances
  726. self.variables = {}
  727. # Predefined preprocessor functions, with min/max number of arguments
  728. self._functions = {
  729. "info": (_info_fn, 1, 1),
  730. "error-if": (_error_if_fn, 2, 2),
  731. "filename": (_filename_fn, 0, 0),
  732. "lineno": (_lineno_fn, 0, 0),
  733. "shell": (_shell_fn, 1, 1),
  734. "warning-if": (_warning_if_fn, 2, 2),
  735. }
  736. # Add any user-defined preprocessor functions
  737. try:
  738. self._functions.update(
  739. importlib.import_module(
  740. os.getenv("KCONFIG_FUNCTIONS", "kconfigfunctions")
  741. ).functions)
  742. except ImportError:
  743. pass
  744. # This determines whether previously unseen symbols are registered.
  745. # They shouldn't be if we parse expressions after parsing, as part of
  746. # Kconfig.eval_string().
  747. self._parsing_kconfigs = True
  748. self.modules = self._lookup_sym("MODULES")
  749. self.defconfig_list = None
  750. self.top_node = MenuNode()
  751. self.top_node.kconfig = self
  752. self.top_node.item = MENU
  753. self.top_node.is_menuconfig = True
  754. self.top_node.visibility = self.y
  755. self.top_node.prompt = ("Main menu", self.y)
  756. self.top_node.parent = None
  757. self.top_node.dep = self.y
  758. self.top_node.filename = filename
  759. self.top_node.linenr = 1
  760. self.top_node.include_path = ()
  761. # Parse the Kconfig files
  762. # Not used internally. Provided as a convenience.
  763. self.kconfig_filenames = [filename]
  764. self.env_vars = set()
  765. # Keeps track of the location in the parent Kconfig files. Kconfig
  766. # files usually source other Kconfig files. See _enter_file().
  767. self._filestack = []
  768. self._include_path = ()
  769. # The current parsing location
  770. self.filename = filename
  771. self.linenr = 0
  772. # Used to avoid retokenizing lines when we discover that they're not
  773. # part of the construct currently being parsed. This is kinda like an
  774. # unget operation.
  775. self._reuse_tokens = False
  776. # Open the top-level Kconfig file. Store the readline() method directly
  777. # as a small optimization.
  778. self._readline = self._open(join(self.srctree, filename), "r").readline
  779. try:
  780. # Parse the Kconfig files
  781. self._parse_block(None, self.top_node, self.top_node)
  782. self.top_node.list = self.top_node.next
  783. self.top_node.next = None
  784. except UnicodeDecodeError as e:
  785. _decoding_error(e, self.filename)
  786. # Close the top-level Kconfig file. __self__ fetches the 'file' object
  787. # for the method.
  788. self._readline.__self__.close()
  789. self._parsing_kconfigs = False
  790. # Do various menu tree post-processing
  791. self._finalize_node(self.top_node, self.y)
  792. self.unique_defined_syms = _ordered_unique(self.defined_syms)
  793. self.unique_choices = _ordered_unique(self.choices)
  794. # Do sanity checks. Some of these depend on everything being finalized.
  795. self._check_sym_sanity()
  796. self._check_choice_sanity()
  797. # KCONFIG_STRICT is an older alias for KCONFIG_WARN_UNDEF, supported
  798. # for backwards compatibility
  799. if os.getenv("KCONFIG_WARN_UNDEF") == "y" or \
  800. os.getenv("KCONFIG_STRICT") == "y":
  801. self._check_undef_syms()
  802. # Build Symbol._dependents for all symbols and choices
  803. self._build_dep()
  804. # Check for dependency loops
  805. check_dep_loop_sym = _check_dep_loop_sym # Micro-optimization
  806. for sym in self.unique_defined_syms:
  807. check_dep_loop_sym(sym, False)
  808. # Add extra dependencies from choices to choice symbols that get
  809. # awkward during dependency loop detection
  810. self._add_choice_deps()
  811. @property
  812. def mainmenu_text(self):
  813. """
  814. See the class documentation.
  815. """
  816. return self.top_node.prompt[0]
  817. @property
  818. def defconfig_filename(self):
  819. """
  820. See the class documentation.
  821. """
  822. if self.defconfig_list:
  823. for filename, cond in self.defconfig_list.defaults:
  824. if expr_value(cond):
  825. try:
  826. with self._open_config(filename.str_value) as f:
  827. return f.name
  828. except EnvironmentError:
  829. continue
  830. return None
  831. def load_config(self, filename=None, replace=True, verbose=None):
  832. """
  833. Loads symbol values from a file in the .config format. Equivalent to
  834. calling Symbol.set_value() to set each of the values.
  835. "# CONFIG_FOO is not set" within a .config file sets the user value of
  836. FOO to n. The C tools work the same way.
  837. For each symbol, the Symbol.user_value attribute holds the value the
  838. symbol was assigned in the .config file (if any). The user value might
  839. differ from Symbol.str/tri_value if there are unsatisfied dependencies.
  840. Calling this function also updates the Kconfig.missing_syms attribute
  841. with a list of all assignments to undefined symbols within the
  842. configuration file. Kconfig.missing_syms is cleared if 'replace' is
  843. True, and appended to otherwise. See the documentation for
  844. Kconfig.missing_syms as well.
  845. See the Kconfig.__init__() docstring for raised exceptions
  846. (OSError/IOError). KconfigError is never raised here.
  847. filename (default: None):
  848. Path to load configuration from (a string). Respects $srctree if set
  849. (see the class documentation).
  850. If 'filename' is None (the default), the configuration file to load
  851. (if any) is calculated automatically, giving the behavior you'd
  852. usually want:
  853. 1. If the KCONFIG_CONFIG environment variable is set, it gives the
  854. path to the configuration file to load. Otherwise, ".config" is
  855. used. See standard_config_filename().
  856. 2. If the path from (1.) doesn't exist, the configuration file
  857. given by kconf.defconfig_filename is loaded instead, which is
  858. derived from the 'option defconfig_list' symbol.
  859. 3. If (1.) and (2.) fail to find a configuration file to load, no
  860. configuration file is loaded, and symbols retain their current
  861. values (e.g., their default values). This is not an error.
  862. See the return value as well.
  863. replace (default: True):
  864. If True, all existing user values will be cleared before loading the
  865. .config. Pass False to merge configurations.
  866. verbose (default: None):
  867. Limited backwards compatibility to prevent crashes. A warning is
  868. printed if anything but None is passed.
  869. Prior to Kconfiglib 12.0.0, this option enabled printing of messages
  870. to stdout when 'filename' was None. A message is (always) returned
  871. now instead, which is more flexible.
  872. Will probably be removed in some future version.
  873. Returns a string with a message saying which file got loaded (or
  874. possibly that no file got loaded, when 'filename' is None). This is
  875. meant to reduce boilerplate in tools, which can do e.g.
  876. print(kconf.load_config()). The returned message distinguishes between
  877. loading (replace == True) and merging (replace == False).
  878. """
  879. if verbose is not None:
  880. _warn_verbose_deprecated("load_config")
  881. msg = None
  882. if filename is None:
  883. filename = standard_config_filename()
  884. if not exists(filename) and \
  885. not exists(join(self.srctree, filename)):
  886. defconfig = self.defconfig_filename
  887. if defconfig is None:
  888. return "Using default symbol values (no '{}')" \
  889. .format(filename)
  890. msg = " default configuration '{}' (no '{}')" \
  891. .format(defconfig, filename)
  892. filename = defconfig
  893. if not msg:
  894. msg = " configuration '{}'".format(filename)
  895. # Disable the warning about assigning to symbols without prompts. This
  896. # is normal and expected within a .config file.
  897. self._warn_assign_no_prompt = False
  898. # This stub only exists to make sure _warn_assign_no_prompt gets
  899. # reenabled
  900. try:
  901. self._load_config(filename, replace)
  902. except UnicodeDecodeError as e:
  903. _decoding_error(e, filename)
  904. finally:
  905. self._warn_assign_no_prompt = True
  906. return ("Loaded" if replace else "Merged") + msg
  907. def _load_config(self, filename, replace):
  908. with self._open_config(filename) as f:
  909. if replace:
  910. self.missing_syms = []
  911. # If we're replacing the configuration, keep track of which
  912. # symbols and choices got set so that we can unset the rest
  913. # later. This avoids invalidating everything and is faster.
  914. # Another benefit is that invalidation must be rock solid for
  915. # it to work, making it a good test.
  916. for sym in self.unique_defined_syms:
  917. sym._was_set = False
  918. for choice in self.unique_choices:
  919. choice._was_set = False
  920. # Small optimizations
  921. set_match = self._set_match
  922. unset_match = self._unset_match
  923. get_sym = self.syms.get
  924. for linenr, line in enumerate(f, 1):
  925. # The C tools ignore trailing whitespace
  926. line = line.rstrip()
  927. match = set_match(line)
  928. if match:
  929. name, val = match.groups()
  930. sym = get_sym(name)
  931. if not sym or not sym.nodes:
  932. self._undef_assign(name, val, filename, linenr)
  933. continue
  934. if sym.orig_type in _BOOL_TRISTATE:
  935. # The C implementation only checks the first character
  936. # to the right of '=', for whatever reason
  937. if not (sym.orig_type is BOOL
  938. and val.startswith(("y", "n")) or
  939. sym.orig_type is TRISTATE
  940. and val.startswith(("y", "m", "n"))):
  941. self._warn("'{}' is not a valid value for the {} "
  942. "symbol {}. Assignment ignored."
  943. .format(val, TYPE_TO_STR[sym.orig_type],
  944. _name_and_loc(sym)),
  945. filename, linenr)
  946. continue
  947. val = val[0]
  948. if sym.choice and val != "n":
  949. # During .config loading, we infer the mode of the
  950. # choice from the kind of values that are assigned
  951. # to the choice symbols
  952. prev_mode = sym.choice.user_value
  953. if prev_mode is not None and \
  954. TRI_TO_STR[prev_mode] != val:
  955. self._warn("both m and y assigned to symbols "
  956. "within the same choice",
  957. filename, linenr)
  958. # Set the choice's mode
  959. sym.choice.set_value(val)
  960. elif sym.orig_type is STRING:
  961. match = _conf_string_match(val)
  962. if not match:
  963. self._warn("malformed string literal in "
  964. "assignment to {}. Assignment ignored."
  965. .format(_name_and_loc(sym)),
  966. filename, linenr)
  967. continue
  968. val = unescape(match.group(1))
  969. else:
  970. match = unset_match(line)
  971. if not match:
  972. # Print a warning for lines that match neither
  973. # set_match() nor unset_match() and that are not blank
  974. # lines or comments. 'line' has already been
  975. # rstrip()'d, so blank lines show up as "" here.
  976. if line and not line.lstrip().startswith("#"):
  977. self._warn("ignoring malformed line '{}'"
  978. .format(line),
  979. filename, linenr)
  980. continue
  981. name = match.group(1)
  982. sym = get_sym(name)
  983. if not sym or not sym.nodes:
  984. self._undef_assign(name, "n", filename, linenr)
  985. continue
  986. if sym.orig_type not in _BOOL_TRISTATE:
  987. continue
  988. val = "n"
  989. # Done parsing the assignment. Set the value.
  990. if sym._was_set:
  991. self._assigned_twice(sym, val, filename, linenr)
  992. sym.set_value(val)
  993. if replace:
  994. # If we're replacing the configuration, unset the symbols that
  995. # didn't get set
  996. for sym in self.unique_defined_syms:
  997. if not sym._was_set:
  998. sym.unset_value()
  999. for choice in self.unique_choices:
  1000. if not choice._was_set:
  1001. choice.unset_value()
  1002. def _undef_assign(self, name, val, filename, linenr):
  1003. # Called for assignments to undefined symbols during .config loading
  1004. self.missing_syms.append((name, val))
  1005. if self.warn_assign_undef:
  1006. self._warn(
  1007. "attempt to assign the value '{}' to the undefined symbol {}"
  1008. .format(val, name), filename, linenr)
  1009. def _assigned_twice(self, sym, new_val, filename, linenr):
  1010. # Called when a symbol is assigned more than once in a .config file
  1011. # Use strings for bool/tristate user values in the warning
  1012. if sym.orig_type in _BOOL_TRISTATE:
  1013. user_val = TRI_TO_STR[sym.user_value]
  1014. else:
  1015. user_val = sym.user_value
  1016. msg = '{} set more than once. Old value "{}", new value "{}".'.format(
  1017. _name_and_loc(sym), user_val, new_val)
  1018. if user_val == new_val:
  1019. if self.warn_assign_redun:
  1020. self._warn(msg, filename, linenr)
  1021. elif self.warn_assign_override:
  1022. self._warn(msg, filename, linenr)
  1023. def write_autoconf(self, filename,
  1024. header="/* Generated by Kconfiglib (https://github.com/ulfalizer/Kconfiglib) */\n"):
  1025. r"""
  1026. Writes out symbol values as a C header file, matching the format used
  1027. by include/generated/autoconf.h in the kernel.
  1028. The ordering of the #defines matches the one generated by
  1029. write_config(). The order in the C implementation depends on the hash
  1030. table implementation as of writing, and so won't match.
  1031. If 'filename' exists and its contents is identical to what would get
  1032. written out, it is left untouched. This avoids updating file metadata
  1033. like the modification time and possibly triggering redundant work in
  1034. build tools.
  1035. filename:
  1036. Self-explanatory.
  1037. header (default: "/* Generated by Kconfiglib (https://github.com/ulfalizer/Kconfiglib) */\n"):
  1038. Text that will be inserted verbatim at the beginning of the file. You
  1039. would usually want it enclosed in '/* */' to make it a C comment,
  1040. and include a final terminating newline.
  1041. """
  1042. self._write_if_changed(filename, self._autoconf_contents(header))
  1043. def _autoconf_contents(self, header):
  1044. # write_autoconf() helper. Returns the contents to write as a string,
  1045. # with 'header' at the beginning.
  1046. # "".join()ed later
  1047. chunks = [header]
  1048. add = chunks.append
  1049. for sym in self.unique_defined_syms:
  1050. # _write_to_conf is determined when the value is calculated. This
  1051. # is a hidden function call due to property magic.
  1052. #
  1053. # Note: In client code, you can check if sym.config_string is empty
  1054. # instead, to avoid accessing the internal _write_to_conf variable
  1055. # (though it's likely to keep working).
  1056. val = sym.str_value
  1057. if not sym._write_to_conf:
  1058. continue
  1059. if sym.orig_type in _BOOL_TRISTATE:
  1060. if val == "y":
  1061. add("#define {}{} 1\n"
  1062. .format(self.config_prefix, sym.name))
  1063. elif val == "m":
  1064. add("#define {}{}_MODULE 1\n"
  1065. .format(self.config_prefix, sym.name))
  1066. elif sym.orig_type is STRING:
  1067. add('#define {}{} "{}"\n'
  1068. .format(self.config_prefix, sym.name, escape(val)))
  1069. else: # sym.orig_type in _INT_HEX:
  1070. if sym.orig_type is HEX and \
  1071. not val.startswith(("0x", "0X")):
  1072. val = "0x" + val
  1073. add("#define {}{} {}\n"
  1074. .format(self.config_prefix, sym.name, val))
  1075. return "".join(chunks)
  1076. def write_config(self, filename=None,
  1077. header="# Generated by Kconfiglib (https://github.com/ulfalizer/Kconfiglib)\n",
  1078. save_old=True, verbose=None):
  1079. r"""
  1080. Writes out symbol values in the .config format. The format matches the
  1081. C implementation, including ordering.
  1082. Symbols appear in the same order in generated .config files as they do
  1083. in the Kconfig files. For symbols defined in multiple locations, a
  1084. single assignment is written out corresponding to the first location
  1085. where the symbol is defined.
  1086. See the 'Intro to symbol values' section in the module docstring to
  1087. understand which symbols get written out.
  1088. If 'filename' exists and its contents is identical to what would get
  1089. written out, it is left untouched. This avoids updating file metadata
  1090. like the modification time and possibly triggering redundant work in
  1091. build tools.
  1092. See the Kconfig.__init__() docstring for raised exceptions
  1093. (OSError/IOError). KconfigError is never raised here.
  1094. filename (default: None):
  1095. Filename to save configuration to (a string).
  1096. If None (the default), the filename in the environment variable
  1097. KCONFIG_CONFIG is used if set, and ".config" otherwise. See
  1098. standard_config_filename().
  1099. header (default: "# Generated by Kconfiglib (https://github.com/ulfalizer/Kconfiglib)\n"):
  1100. Text that will be inserted verbatim at the beginning of the file. You
  1101. would usually want each line to start with '#' to make it a comment,
  1102. and include a final terminating newline.
  1103. save_old (default: True):
  1104. If True and <filename> already exists, a copy of it will be saved to
  1105. <filename>.old in the same directory before the new configuration is
  1106. written.
  1107. Errors are silently ignored if <filename>.old cannot be written (e.g.
  1108. due to being a directory, or <filename> being something like
  1109. /dev/null).
  1110. verbose (default: None):
  1111. Limited backwards compatibility to prevent crashes. A warning is
  1112. printed if anything but None is passed.
  1113. Prior to Kconfiglib 12.0.0, this option enabled printing of messages
  1114. to stdout when 'filename' was None. A message is (always) returned
  1115. now instead, which is more flexible.
  1116. Will probably be removed in some future version.
  1117. Returns a string with a message saying which file got saved. This is
  1118. meant to reduce boilerplate in tools, which can do e.g.
  1119. print(kconf.write_config()).
  1120. """
  1121. if verbose is not None:
  1122. _warn_verbose_deprecated("write_config")
  1123. if filename is None:
  1124. filename = standard_config_filename()
  1125. contents = self._config_contents(header)
  1126. if self._contents_eq(filename, contents):
  1127. return "No change to '{}'".format(filename)
  1128. if save_old:
  1129. _save_old(filename)
  1130. with self._open(filename, "w") as f:
  1131. f.write(contents)
  1132. return "Configuration saved to '{}'".format(filename)
  1133. def _config_contents(self, header):
  1134. # write_config() helper. Returns the contents to write as a string,
  1135. # with 'header' at the beginning.
  1136. #
  1137. # More memory friendly would be to 'yield' the strings and
  1138. # "".join(_config_contents()), but it was a bit slower on my system.
  1139. # node_iter() was used here before commit 3aea9f7 ("Add '# end of
  1140. # <menu>' after menus in .config"). Those comments get tricky to
  1141. # implement with it.
  1142. for sym in self.unique_defined_syms:
  1143. sym._visited = False
  1144. # Did we just print an '# end of ...' comment?
  1145. after_end_comment = False
  1146. # "".join()ed later
  1147. chunks = [header]
  1148. add = chunks.append
  1149. node = self.top_node
  1150. while 1:
  1151. # Jump to the next node with an iterative tree walk
  1152. if node.list:
  1153. node = node.list
  1154. elif node.next:
  1155. node = node.next
  1156. else:
  1157. while node.parent:
  1158. node = node.parent
  1159. # Add a comment when leaving visible menus
  1160. if node.item is MENU and expr_value(node.dep) and \
  1161. expr_value(node.visibility) and \
  1162. node is not self.top_node:
  1163. add("# end of {}\n".format(node.prompt[0]))
  1164. after_end_comment = True
  1165. if node.next:
  1166. node = node.next
  1167. break
  1168. else:
  1169. # No more nodes
  1170. return "".join(chunks)
  1171. # Generate configuration output for the node
  1172. item = node.item
  1173. if item.__class__ is Symbol:
  1174. if item._visited:
  1175. continue
  1176. item._visited = True
  1177. conf_string = item.config_string
  1178. if not conf_string:
  1179. continue
  1180. if after_end_comment:
  1181. # Add a blank line before the first symbol printed after an
  1182. # '# end of ...' comment
  1183. after_end_comment = False
  1184. add("\n")
  1185. add(conf_string)
  1186. elif expr_value(node.dep) and \
  1187. ((item is MENU and expr_value(node.visibility)) or
  1188. item is COMMENT):
  1189. add("\n#\n# {}\n#\n".format(node.prompt[0]))
  1190. after_end_comment = False
  1191. def write_min_config(self, filename,
  1192. header="# Generated by Kconfiglib (https://github.com/ulfalizer/Kconfiglib)\n"):
  1193. """
  1194. Writes out a "minimal" configuration file, omitting symbols whose value
  1195. matches their default value. The format matches the one produced by
  1196. 'make savedefconfig'.
  1197. The resulting configuration file is incomplete, but a complete
  1198. configuration can be derived from it by loading it. Minimal
  1199. configuration files can serve as a more manageable configuration format
  1200. compared to a "full" .config file, especially when configurations files
  1201. are merged or edited by hand.
  1202. See the Kconfig.__init__() docstring for raised exceptions
  1203. (OSError/IOError). KconfigError is never raised here.
  1204. filename:
  1205. Self-explanatory.
  1206. header (default: "# Generated by Kconfiglib (https://github.com/ulfalizer/Kconfiglib)\n"):
  1207. Text that will be inserted verbatim at the beginning of the file. You
  1208. would usually want each line to start with '#' to make it a comment,
  1209. and include a final terminating newline.
  1210. Returns a string with a message saying which file got saved. This is
  1211. meant to reduce boilerplate in tools, which can do e.g.
  1212. print(kconf.write_min_config()).
  1213. """
  1214. contents = self._min_config_contents(header)
  1215. if self._contents_eq(filename, contents):
  1216. return "No change to '{}'".format(filename)
  1217. with self._open(filename, "w") as f:
  1218. f.write(contents)
  1219. return "Minimal configuration saved to '{}'".format(filename)
  1220. def _min_config_contents(self, header):
  1221. # write_min_config() helper. Returns the contents to write as a string,
  1222. # with 'header' at the beginning.
  1223. chunks = [header]
  1224. add = chunks.append
  1225. for sym in self.unique_defined_syms:
  1226. # Skip symbols that cannot be changed. Only check
  1227. # non-choice symbols, as selects don't affect choice
  1228. # symbols.
  1229. if not sym.choice and \
  1230. sym.visibility <= expr_value(sym.rev_dep):
  1231. continue
  1232. # Skip symbols whose value matches their default
  1233. if sym.str_value == sym._str_default():
  1234. continue
  1235. # Skip symbols that would be selected by default in a
  1236. # choice, unless the choice is optional or the symbol type
  1237. # isn't bool (it might be possible to set the choice mode
  1238. # to n or the symbol to m in those cases).
  1239. if sym.choice and \
  1240. not sym.choice.is_optional and \
  1241. sym.choice._selection_from_defaults() is sym and \
  1242. sym.orig_type is BOOL and \
  1243. sym.tri_value == 2:
  1244. continue
  1245. add(sym.config_string)
  1246. return "".join(chunks)
  1247. def sync_deps(self, path):
  1248. """
  1249. Creates or updates a directory structure that can be used to avoid
  1250. doing a full rebuild whenever the configuration is changed, mirroring
  1251. include/config/ in the kernel.
  1252. This function is intended to be called during each build, before
  1253. compiling source files that depend on configuration symbols.
  1254. See the Kconfig.__init__() docstring for raised exceptions
  1255. (OSError/IOError). KconfigError is never raised here.
  1256. path:
  1257. Path to directory
  1258. sync_deps(path) does the following:
  1259. 1. If the directory <path> does not exist, it is created.
  1260. 2. If <path>/auto.conf exists, old symbol values are loaded from it,
  1261. which are then compared against the current symbol values. If a
  1262. symbol has changed value (would generate different output in
  1263. autoconf.h compared to before), the change is signaled by
  1264. touch'ing a file corresponding to the symbol.
  1265. The first time sync_deps() is run on a directory, <path>/auto.conf
  1266. won't exist, and no old symbol values will be available. This
  1267. logically has the same effect as updating the entire
  1268. configuration.
  1269. The path to a symbol's file is calculated from the symbol's name
  1270. by replacing all '_' with '/' and appending '.h'. For example, the
  1271. symbol FOO_BAR_BAZ gets the file <path>/foo/bar/baz.h, and FOO
  1272. gets the file <path>/foo.h.
  1273. This scheme matches the C tools. The point is to avoid having a
  1274. single directory with a huge number of files, which the underlying
  1275. filesystem might not handle well.
  1276. 3. A new auto.conf with the current symbol values is written, to keep
  1277. track of them for the next build.
  1278. If auto.conf exists and its contents is identical to what would
  1279. get written out, it is left untouched. This avoids updating file
  1280. metadata like the modification time and possibly triggering
  1281. redundant work in build tools.
  1282. The last piece of the puzzle is knowing what symbols each source file
  1283. depends on. Knowing that, dependencies can be added from source files
  1284. to the files corresponding to the symbols they depends on. The source
  1285. file will then get recompiled (only) when the symbol value changes
  1286. (provided sync_deps() is run first during each build).
  1287. The tool in the kernel that extracts symbol dependencies from source
  1288. files is scripts/basic/fixdep.c. Missing symbol files also correspond
  1289. to "not changed", which fixdep deals with by using the $(wildcard) Make
  1290. function when adding symbol prerequisites to source files.
  1291. In case you need a different scheme for your project, the sync_deps()
  1292. implementation can be used as a template.
  1293. """
  1294. if not exists(path):
  1295. os.mkdir(path, 0o755)
  1296. # Load old values from auto.conf, if any
  1297. self._load_old_vals(path)
  1298. for sym in self.unique_defined_syms:
  1299. # _write_to_conf is determined when the value is calculated. This
  1300. # is a hidden function call due to property magic.
  1301. #
  1302. # Note: In client code, you can check if sym.config_string is empty
  1303. # instead, to avoid accessing the internal _write_to_conf variable
  1304. # (though it's likely to keep working).
  1305. val = sym.str_value
  1306. # n tristate values do not get written to auto.conf and autoconf.h,
  1307. # making a missing symbol logically equivalent to n
  1308. if sym._write_to_conf:
  1309. if sym._old_val is None and \
  1310. sym.orig_type in _BOOL_TRISTATE and \
  1311. val == "n":
  1312. # No old value (the symbol was missing or n), new value n.
  1313. # No change.
  1314. continue
  1315. if val == sym._old_val:
  1316. # New value matches old. No change.
  1317. continue
  1318. elif sym._old_val is None:
  1319. # The symbol wouldn't appear in autoconf.h (because
  1320. # _write_to_conf is false), and it wouldn't have appeared in
  1321. # autoconf.h previously either (because it didn't appear in
  1322. # auto.conf). No change.
  1323. continue
  1324. # 'sym' has a new value. Flag it.
  1325. _touch_dep_file(path, sym.name)
  1326. # Remember the current values as the "new old" values.
  1327. #
  1328. # This call could go anywhere after the call to _load_old_vals(), but
  1329. # putting it last means _sync_deps() can be safely rerun if it fails
  1330. # before this point.
  1331. self._write_old_vals(path)
  1332. def _load_old_vals(self, path):
  1333. # Loads old symbol values from auto.conf into a dedicated
  1334. # Symbol._old_val field. Mirrors load_config().
  1335. #
  1336. # The extra field could be avoided with some trickery involving dumping
  1337. # symbol values and restoring them later, but this is simpler and
  1338. # faster. The C tools also use a dedicated field for this purpose.
  1339. for sym in self.unique_defined_syms:
  1340. sym._old_val = None
  1341. try:
  1342. auto_conf = self._open(join(path, "auto.conf"), "r")
  1343. except EnvironmentError as e:
  1344. if e.errno == errno.ENOENT:
  1345. # No old values
  1346. return
  1347. raise
  1348. with auto_conf as f:
  1349. for line in f:
  1350. match = self._set_match(line)
  1351. if not match:
  1352. # We only expect CONFIG_FOO=... (and possibly a header
  1353. # comment) in auto.conf
  1354. continue
  1355. name, val = match.groups()
  1356. if name in self.syms:
  1357. sym = self.syms[name]
  1358. if sym.orig_type is STRING:
  1359. match = _conf_string_match(val)
  1360. if not match:
  1361. continue
  1362. val = unescape(match.group(1))
  1363. self.syms[name]._old_val = val
  1364. else:
  1365. # Flag that the symbol no longer exists, in
  1366. # case something still depends on it
  1367. _touch_dep_file(path, name)
  1368. def _write_old_vals(self, path):
  1369. # Helper for writing auto.conf. Basically just a simplified
  1370. # write_config() that doesn't write any comments (including
  1371. # '# CONFIG_FOO is not set' comments). The format matches the C
  1372. # implementation, though the ordering is arbitrary there (depends on
  1373. # the hash table implementation).
  1374. #
  1375. # A separate helper function is neater than complicating write_config()
  1376. # by passing a flag to it, plus we only need to look at symbols here.
  1377. self._write_if_changed(
  1378. os.path.join(path, "auto.conf"),
  1379. self._old_vals_contents())
  1380. def _old_vals_contents(self):
  1381. # _write_old_vals() helper. Returns the contents to write as a string.
  1382. # Temporary list instead of generator makes this a bit faster
  1383. return "".join([
  1384. sym.config_string for sym in self.unique_defined_syms
  1385. if not (sym.orig_type in _BOOL_TRISTATE and not sym.tri_value)
  1386. ])
  1387. def node_iter(self, unique_syms=False):
  1388. """
  1389. Returns a generator for iterating through all MenuNode's in the Kconfig
  1390. tree. The iteration is done in Kconfig definition order (each node is
  1391. visited before its children, and the children of a node are visited
  1392. before the next node).
  1393. The Kconfig.top_node menu node is skipped. It contains an implicit menu
  1394. that holds the top-level items.
  1395. As an example, the following code will produce a list equal to
  1396. Kconfig.defined_syms:
  1397. defined_syms = [node.item for node in kconf.node_iter()
  1398. if isinstance(node.item, Symbol)]
  1399. unique_syms (default: False):
  1400. If True, only the first MenuNode will be included for symbols defined
  1401. in multiple locations.
  1402. Using kconf.node_iter(True) in the example above would give a list
  1403. equal to unique_defined_syms.
  1404. """
  1405. if unique_syms:
  1406. for sym in self.unique_defined_syms:
  1407. sym._visited = False
  1408. node = self.top_node
  1409. while 1:
  1410. # Jump to the next node with an iterative tree walk
  1411. if node.list:
  1412. node = node.list
  1413. elif node.next:
  1414. node = node.next
  1415. else:
  1416. while node.parent:
  1417. node = node.parent
  1418. if node.next:
  1419. node = node.next
  1420. break
  1421. else:
  1422. # No more nodes
  1423. return
  1424. if unique_syms and node.item.__class__ is Symbol:
  1425. if node.item._visited:
  1426. continue
  1427. node.item._visited = True
  1428. yield node
  1429. def eval_string(self, s):
  1430. """
  1431. Returns the tristate value of the expression 's', represented as 0, 1,
  1432. and 2 for n, m, and y, respectively. Raises KconfigError on syntax
  1433. errors. Warns if undefined symbols are referenced.
  1434. As an example, if FOO and BAR are tristate symbols at least one of
  1435. which has the value y, then eval_string("y && (FOO || BAR)") returns
  1436. 2 (y).
  1437. To get the string value of non-bool/tristate symbols, use
  1438. Symbol.str_value. eval_string() always returns a tristate value, and
  1439. all non-bool/tristate symbols have the tristate value 0 (n).
  1440. The expression parsing is consistent with how parsing works for
  1441. conditional ('if ...') expressions in the configuration, and matches
  1442. the C implementation. m is rewritten to 'm && MODULES', so
  1443. eval_string("m") will return 0 (n) unless modules are enabled.
  1444. """
  1445. # The parser is optimized to be fast when parsing Kconfig files (where
  1446. # an expression can never appear at the beginning of a line). We have
  1447. # to monkey-patch things a bit here to reuse it.
  1448. self.filename = None
  1449. self._tokens = self._tokenize("if " + s)
  1450. # Strip "if " to avoid giving confusing error messages
  1451. self._line = s
  1452. self._tokens_i = 1 # Skip the 'if' token
  1453. return expr_value(self._expect_expr_and_eol())
  1454. def unset_values(self):
  1455. """
  1456. Removes any user values from all symbols, as if Kconfig.load_config()
  1457. or Symbol.set_value() had never been called.
  1458. """
  1459. self._warn_assign_no_prompt = False
  1460. try:
  1461. # set_value() already rejects undefined symbols, and they don't
  1462. # need to be invalidated (because their value never changes), so we
  1463. # can just iterate over defined symbols
  1464. for sym in self.unique_defined_syms:
  1465. sym.unset_value()
  1466. for choice in self.unique_choices:
  1467. choice.unset_value()
  1468. finally:
  1469. self._warn_assign_no_prompt = True
  1470. def enable_warnings(self):
  1471. """
  1472. Do 'Kconfig.warn = True' instead. Maintained for backwards
  1473. compatibility.
  1474. """
  1475. self.warn = True
  1476. def disable_warnings(self):
  1477. """
  1478. Do 'Kconfig.warn = False' instead. Maintained for backwards
  1479. compatibility.
  1480. """
  1481. self.warn = False
  1482. def enable_stderr_warnings(self):
  1483. """
  1484. Do 'Kconfig.warn_to_stderr = True' instead. Maintained for backwards
  1485. compatibility.
  1486. """
  1487. self.warn_to_stderr = True
  1488. def disable_stderr_warnings(self):
  1489. """
  1490. Do 'Kconfig.warn_to_stderr = False' instead. Maintained for backwards
  1491. compatibility.
  1492. """
  1493. self.warn_to_stderr = False
  1494. def enable_undef_warnings(self):
  1495. """
  1496. Do 'Kconfig.warn_assign_undef = True' instead. Maintained for backwards
  1497. compatibility.
  1498. """
  1499. self.warn_assign_undef = True
  1500. def disable_undef_warnings(self):
  1501. """
  1502. Do 'Kconfig.warn_assign_undef = False' instead. Maintained for
  1503. backwards compatibility.
  1504. """
  1505. self.warn_assign_undef = False
  1506. def enable_override_warnings(self):
  1507. """
  1508. Do 'Kconfig.warn_assign_override = True' instead. Maintained for
  1509. backwards compatibility.
  1510. """
  1511. self.warn_assign_override = True
  1512. def disable_override_warnings(self):
  1513. """
  1514. Do 'Kconfig.warn_assign_override = False' instead. Maintained for
  1515. backwards compatibility.
  1516. """
  1517. self.warn_assign_override = False
  1518. def enable_redun_warnings(self):
  1519. """
  1520. Do 'Kconfig.warn_assign_redun = True' instead. Maintained for backwards
  1521. compatibility.
  1522. """
  1523. self.warn_assign_redun = True
  1524. def disable_redun_warnings(self):
  1525. """
  1526. Do 'Kconfig.warn_assign_redun = False' instead. Maintained for
  1527. backwards compatibility.
  1528. """
  1529. self.warn_assign_redun = False
  1530. def __repr__(self):
  1531. """
  1532. Returns a string with information about the Kconfig object when it is
  1533. evaluated on e.g. the interactive Python prompt.
  1534. """
  1535. def status(flag):
  1536. return "enabled" if flag else "disabled"
  1537. return "<{}>".format(", ".join((
  1538. "configuration with {} symbols".format(len(self.syms)),
  1539. 'main menu prompt "{}"'.format(self.mainmenu_text),
  1540. "srctree is current directory" if not self.srctree else
  1541. 'srctree "{}"'.format(self.srctree),
  1542. 'config symbol prefix "{}"'.format(self.config_prefix),
  1543. "warnings " + status(self.warn),
  1544. "printing of warnings to stderr " + status(self.warn_to_stderr),
  1545. "undef. symbol assignment warnings " +
  1546. status(self.warn_assign_undef),
  1547. "overriding symbol assignment warnings " +
  1548. status(self.warn_assign_override),
  1549. "redundant symbol assignment warnings " +
  1550. status(self.warn_assign_redun)
  1551. )))
  1552. #
  1553. # Private methods
  1554. #
  1555. #
  1556. # File reading
  1557. #
  1558. def _open_config(self, filename):
  1559. # Opens a .config file. First tries to open 'filename', then
  1560. # '$srctree/filename' if $srctree was set when the configuration was
  1561. # loaded.
  1562. try:
  1563. return self._open(filename, "r")
  1564. except EnvironmentError as e:
  1565. # This will try opening the same file twice if $srctree is unset,
  1566. # but it's not a big deal
  1567. try:
  1568. return self._open(join(self.srctree, filename), "r")
  1569. except EnvironmentError as e2:
  1570. # This is needed for Python 3, because e2 is deleted after
  1571. # the try block:
  1572. #
  1573. # https://docs.python.org/3/reference/compound_stmts.html#the-try-statement
  1574. e = e2
  1575. raise _KconfigIOError(
  1576. e, "Could not open '{}' ({}: {}). Check that the $srctree "
  1577. "environment variable ({}) is set correctly."
  1578. .format(filename, errno.errorcode[e.errno], e.strerror,
  1579. "set to '{}'".format(self.srctree) if self.srctree
  1580. else "unset or blank"))
  1581. def _enter_file(self, filename):
  1582. # Jumps to the beginning of a sourced Kconfig file, saving the previous
  1583. # position and file object.
  1584. #
  1585. # filename:
  1586. # Absolute path to file
  1587. # Path relative to $srctree, stored in e.g. self.filename (which makes
  1588. # it indirectly show up in MenuNode.filename). Equals 'filename' for
  1589. # absolute paths passed to 'source'.
  1590. if filename.startswith(self._srctree_prefix):
  1591. # Relative path (or a redundant absolute path to within $srctree,
  1592. # but it's probably fine to reduce those too)
  1593. rel_filename = filename[len(self._srctree_prefix):]
  1594. else:
  1595. # Absolute path
  1596. rel_filename = filename
  1597. self.kconfig_filenames.append(rel_filename)
  1598. # The parent Kconfig files are represented as a list of
  1599. # (<include path>, <Python 'file' object for Kconfig file>) tuples.
  1600. #
  1601. # <include path> is immutable and holds a *tuple* of
  1602. # (<filename>, <linenr>) tuples, giving the locations of the 'source'
  1603. # statements in the parent Kconfig files. The current include path is
  1604. # also available in Kconfig._include_path.
  1605. #
  1606. # The point of this redundant setup is to allow Kconfig._include_path
  1607. # to be assigned directly to MenuNode.include_path without having to
  1608. # copy it, sharing it wherever possible.
  1609. # Save include path and 'file' object (via its 'readline' function)
  1610. # before entering the file
  1611. self._filestack.append((self._include_path, self._readline))
  1612. # _include_path is a tuple, so this rebinds the variable instead of
  1613. # doing in-place modification
  1614. self._include_path += ((self.filename, self.linenr),)
  1615. # Check for recursive 'source'
  1616. for name, _ in self._include_path:
  1617. if name == rel_filename:
  1618. raise KconfigError(
  1619. "\n{}:{}: recursive 'source' of '{}' detected. Check that "
  1620. "environment variables are set correctly.\n"
  1621. "Include path:\n{}"
  1622. .format(self.filename, self.linenr, rel_filename,
  1623. "\n".join("{}:{}".format(name, linenr)
  1624. for name, linenr in self._include_path)))
  1625. try:
  1626. self._readline = self._open(filename, "r").readline
  1627. except EnvironmentError as e:
  1628. # We already know that the file exists
  1629. raise _KconfigIOError(
  1630. e, "{}:{}: Could not open '{}' (in '{}') ({}: {})"
  1631. .format(self.filename, self.linenr, filename,
  1632. self._line.strip(),
  1633. errno.errorcode[e.errno], e.strerror))
  1634. self.filename = rel_filename
  1635. self.linenr = 0
  1636. def _leave_file(self):
  1637. # Returns from a Kconfig file to the file that sourced it. See
  1638. # _enter_file().
  1639. # Restore location from parent Kconfig file
  1640. self.filename, self.linenr = self._include_path[-1]
  1641. # Restore include path and 'file' object
  1642. self._readline.__self__.close() # __self__ fetches the 'file' object
  1643. self._include_path, self._readline = self._filestack.pop()
  1644. def _next_line(self):
  1645. # Fetches and tokenizes the next line from the current Kconfig file.
  1646. # Returns False at EOF and True otherwise.
  1647. # We might already have tokens from parsing a line and discovering that
  1648. # it's part of a different construct
  1649. if self._reuse_tokens:
  1650. self._reuse_tokens = False
  1651. # self._tokens_i is known to be 1 here, because _parse_properties()
  1652. # leaves it like that when it can't recognize a line (or parses
  1653. # a help text)
  1654. return True
  1655. # readline() returns '' over and over at EOF, which we rely on for help
  1656. # texts at the end of files (see _line_after_help())
  1657. line = self._readline()
  1658. if not line:
  1659. return False
  1660. self.linenr += 1
  1661. # Handle line joining
  1662. while line.endswith("\\\n"):
  1663. line = line[:-2] + self._readline()
  1664. self.linenr += 1
  1665. self._tokens = self._tokenize(line)
  1666. # Initialize to 1 instead of 0 to factor out code from _parse_block()
  1667. # and _parse_properties(). They immediately fetch self._tokens[0].
  1668. self._tokens_i = 1
  1669. return True
  1670. def _line_after_help(self, line):
  1671. # Tokenizes a line after a help text. This case is special in that the
  1672. # line has already been fetched (to discover that it isn't part of the
  1673. # help text).
  1674. #
  1675. # An earlier version used a _saved_line variable instead that was
  1676. # checked in _next_line(). This special-casing gets rid of it and makes
  1677. # _reuse_tokens alone sufficient to handle unget.
  1678. # Handle line joining
  1679. while line.endswith("\\\n"):
  1680. line = line[:-2] + self._readline()
  1681. self.linenr += 1
  1682. self._tokens = self._tokenize(line)
  1683. self._reuse_tokens = True
  1684. def _write_if_changed(self, filename, contents):
  1685. # Writes 'contents' into 'filename', but only if it differs from the
  1686. # current contents of the file.
  1687. #
  1688. # Another variant would be write a temporary file on the same
  1689. # filesystem, compare the files, and rename() the temporary file if it
  1690. # differs, but it breaks stuff like write_config("/dev/null"), which is
  1691. # used out there to force evaluation-related warnings to be generated.
  1692. # This simple version is pretty failsafe and portable.
  1693. if not self._contents_eq(filename, contents):
  1694. with self._open(filename, "w") as f:
  1695. f.write(contents)
  1696. def _contents_eq(self, filename, contents):
  1697. # Returns True if the contents of 'filename' is 'contents' (a string),
  1698. # and False otherwise (including if 'filename' can't be opened/read)
  1699. try:
  1700. with self._open(filename, "r") as f:
  1701. # Robust re. things like encoding and line endings (mmap()
  1702. # trickery isn't)
  1703. return f.read(len(contents) + 1) == contents
  1704. except EnvironmentError:
  1705. # If the error here would prevent writing the file as well, we'll
  1706. # notice it later
  1707. return False
  1708. #
  1709. # Tokenization
  1710. #
  1711. def _lookup_sym(self, name):
  1712. # Fetches the symbol 'name' from the symbol table, creating and
  1713. # registering it if it does not exist. If '_parsing_kconfigs' is False,
  1714. # it means we're in eval_string(), and new symbols won't be registered.
  1715. if name in self.syms:
  1716. return self.syms[name]
  1717. sym = Symbol()
  1718. sym.kconfig = self
  1719. sym.name = name
  1720. sym.is_constant = False
  1721. sym.rev_dep = sym.weak_rev_dep = sym.direct_dep = self.n
  1722. if self._parsing_kconfigs:
  1723. self.syms[name] = sym
  1724. else:
  1725. self._warn("no symbol {} in configuration".format(name))
  1726. return sym
  1727. def _lookup_const_sym(self, name):
  1728. # Like _lookup_sym(), for constant (quoted) symbols
  1729. if name in self.const_syms:
  1730. return self.const_syms[name]
  1731. sym = Symbol()
  1732. sym.kconfig = self
  1733. sym.name = name
  1734. sym.is_constant = True
  1735. sym.rev_dep = sym.weak_rev_dep = sym.direct_dep = self.n
  1736. if self._parsing_kconfigs:
  1737. self.const_syms[name] = sym
  1738. return sym
  1739. def _tokenize(self, s):
  1740. # Parses 's', returning a None-terminated list of tokens. Registers any
  1741. # new symbols encountered with _lookup(_const)_sym().
  1742. #
  1743. # Tries to be reasonably speedy by processing chunks of text via
  1744. # regexes and string operations where possible. This is the biggest
  1745. # hotspot during parsing.
  1746. #
  1747. # It might be possible to rewrite this to 'yield' tokens instead,
  1748. # working across multiple lines. Lookback and compatibility with old
  1749. # janky versions of the C tools complicate things though.
  1750. self._line = s # Used for error reporting
  1751. # Initial token on the line
  1752. match = _command_match(s)
  1753. if not match:
  1754. if s.isspace() or s.lstrip().startswith("#"):
  1755. return (None,)
  1756. self._parse_error("unknown token at start of line")
  1757. # Tricky implementation detail: While parsing a token, 'token' refers
  1758. # to the previous token. See _STRING_LEX for why this is needed.
  1759. token = _get_keyword(match.group(1))
  1760. if not token:
  1761. # Backwards compatibility with old versions of the C tools, which
  1762. # (accidentally) accepted stuff like "--help--" and "-help---".
  1763. # This was fixed in the C tools by commit c2264564 ("kconfig: warn
  1764. # of unhandled characters in Kconfig commands"), committed in July
  1765. # 2015, but it seems people still run Kconfiglib on older kernels.
  1766. if s.strip(" \t\n-") == "help":
  1767. return (_T_HELP, None)
  1768. # If the first token is not a keyword (and not a weird help token),
  1769. # we have a preprocessor variable assignment (or a bare macro on a
  1770. # line)
  1771. self._parse_assignment(s)
  1772. return (None,)
  1773. tokens = [token]
  1774. # The current index in the string being tokenized
  1775. i = match.end()
  1776. # Main tokenization loop (for tokens past the first one)
  1777. while i < len(s):
  1778. # Test for an identifier/keyword first. This is the most common
  1779. # case.
  1780. match = _id_keyword_match(s, i)
  1781. if match:
  1782. # We have an identifier or keyword
  1783. # Check what it is. lookup_sym() will take care of allocating
  1784. # new symbols for us the first time we see them. Note that
  1785. # 'token' still refers to the previous token.
  1786. name = match.group(1)
  1787. keyword = _get_keyword(name)
  1788. if keyword:
  1789. # It's a keyword
  1790. token = keyword
  1791. # Jump past it
  1792. i = match.end()
  1793. elif token not in _STRING_LEX:
  1794. # It's a non-const symbol, except we translate n, m, and y
  1795. # into the corresponding constant symbols, like the C
  1796. # implementation
  1797. if "$" in name:
  1798. # Macro expansion within symbol name
  1799. name, s, i = self._expand_name(s, i)
  1800. else:
  1801. i = match.end()
  1802. token = self.const_syms[name] if name in STR_TO_TRI else \
  1803. self._lookup_sym(name)
  1804. else:
  1805. # It's a case of missing quotes. For example, the
  1806. # following is accepted:
  1807. #
  1808. # menu unquoted_title
  1809. #
  1810. # config A
  1811. # tristate unquoted_prompt
  1812. #
  1813. # endmenu
  1814. #
  1815. # Named choices ('choice FOO') also end up here.
  1816. if token is not _T_CHOICE:
  1817. self._warn("style: quotes recommended around '{}' in '{}'"
  1818. .format(name, self._line.strip()),
  1819. self.filename, self.linenr)
  1820. token = name
  1821. i = match.end()
  1822. else:
  1823. # Neither a keyword nor a non-const symbol
  1824. # We always strip whitespace after tokens, so it is safe to
  1825. # assume that s[i] is the start of a token here.
  1826. c = s[i]
  1827. if c in "\"'":
  1828. if "$" not in s and "\\" not in s:
  1829. # Fast path for lines without $ and \. Find the
  1830. # matching quote.
  1831. end_i = s.find(c, i + 1) + 1
  1832. if not end_i:
  1833. self._parse_error("unterminated string")
  1834. val = s[i + 1:end_i - 1]
  1835. i = end_i
  1836. else:
  1837. # Slow path
  1838. s, end_i = self._expand_str(s, i)
  1839. # os.path.expandvars() and the $UNAME_RELEASE replace()
  1840. # is a backwards compatibility hack, which should be
  1841. # reasonably safe as expandvars() leaves references to
  1842. # undefined env. vars. as is.
  1843. #
  1844. # The preprocessor functionality changed how
  1845. # environment variables are referenced, to $(FOO).
  1846. val = expandvars(s[i + 1:end_i - 1]
  1847. .replace("$UNAME_RELEASE",
  1848. _UNAME_RELEASE))
  1849. i = end_i
  1850. # This is the only place where we don't survive with a
  1851. # single token of lookback: 'option env="FOO"' does not
  1852. # refer to a constant symbol named "FOO".
  1853. token = \
  1854. val if token in _STRING_LEX or tokens[0] is _T_OPTION \
  1855. else self._lookup_const_sym(val)
  1856. elif s.startswith("&&", i):
  1857. token = _T_AND
  1858. i += 2
  1859. elif s.startswith("||", i):
  1860. token = _T_OR
  1861. i += 2
  1862. elif c == "=":
  1863. token = _T_EQUAL
  1864. i += 1
  1865. elif s.startswith("!=", i):
  1866. token = _T_UNEQUAL
  1867. i += 2
  1868. elif c == "!":
  1869. token = _T_NOT
  1870. i += 1
  1871. elif c == "(":
  1872. token = _T_OPEN_PAREN
  1873. i += 1
  1874. elif c == ")":
  1875. token = _T_CLOSE_PAREN
  1876. i += 1
  1877. elif c == "#":
  1878. break
  1879. # Very rare
  1880. elif s.startswith("<=", i):
  1881. token = _T_LESS_EQUAL
  1882. i += 2
  1883. elif c == "<":
  1884. token = _T_LESS
  1885. i += 1
  1886. elif s.startswith(">=", i):
  1887. token = _T_GREATER_EQUAL
  1888. i += 2
  1889. elif c == ">":
  1890. token = _T_GREATER
  1891. i += 1
  1892. else:
  1893. self._parse_error("unknown tokens in line")
  1894. # Skip trailing whitespace
  1895. while i < len(s) and s[i].isspace():
  1896. i += 1
  1897. # Add the token
  1898. tokens.append(token)
  1899. # None-terminating the token list makes token fetching simpler/faster
  1900. tokens.append(None)
  1901. return tokens
  1902. # Helpers for syntax checking and token fetching. See the
  1903. # 'Intro to expressions' section for what a constant symbol is.
  1904. #
  1905. # More of these could be added, but the single-use cases are inlined as an
  1906. # optimization.
  1907. def _expect_sym(self):
  1908. token = self._tokens[self._tokens_i]
  1909. self._tokens_i += 1
  1910. if token.__class__ is not Symbol:
  1911. self._parse_error("expected symbol")
  1912. return token
  1913. def _expect_nonconst_sym(self):
  1914. # Used for 'select' and 'imply' only. We know the token indices.
  1915. token = self._tokens[1]
  1916. self._tokens_i = 2
  1917. if token.__class__ is not Symbol or token.is_constant:
  1918. self._parse_error("expected nonconstant symbol")
  1919. return token
  1920. def _expect_str_and_eol(self):
  1921. token = self._tokens[self._tokens_i]
  1922. self._tokens_i += 1
  1923. if token.__class__ is not str:
  1924. self._parse_error("expected string")
  1925. if self._tokens[self._tokens_i] is not None:
  1926. self._trailing_tokens_error()
  1927. return token
  1928. def _expect_expr_and_eol(self):
  1929. expr = self._parse_expr(True)
  1930. if self._tokens[self._tokens_i] is not None:
  1931. self._trailing_tokens_error()
  1932. return expr
  1933. def _check_token(self, token):
  1934. # If the next token is 'token', removes it and returns True
  1935. if self._tokens[self._tokens_i] is token:
  1936. self._tokens_i += 1
  1937. return True
  1938. return False
  1939. #
  1940. # Preprocessor logic
  1941. #
  1942. def _parse_assignment(self, s):
  1943. # Parses a preprocessor variable assignment, registering the variable
  1944. # if it doesn't already exist. Also takes care of bare macros on lines
  1945. # (which are allowed, and can be useful for their side effects).
  1946. # Expand any macros in the left-hand side of the assignment (the
  1947. # variable name)
  1948. s = s.lstrip()
  1949. i = 0
  1950. while 1:
  1951. i = _assignment_lhs_fragment_match(s, i).end()
  1952. if s.startswith("$(", i):
  1953. s, i = self._expand_macro(s, i, ())
  1954. else:
  1955. break
  1956. if s.isspace():
  1957. # We also accept a bare macro on a line (e.g.
  1958. # $(warning-if,$(foo),ops)), provided it expands to a blank string
  1959. return
  1960. # Assigned variable
  1961. name = s[:i]
  1962. # Extract assignment operator (=, :=, or +=) and value
  1963. rhs_match = _assignment_rhs_match(s, i)
  1964. if not rhs_match:
  1965. self._parse_error("syntax error")
  1966. op, val = rhs_match.groups()
  1967. if name in self.variables:
  1968. # Already seen variable
  1969. var = self.variables[name]
  1970. else:
  1971. # New variable
  1972. var = Variable()
  1973. var.kconfig = self
  1974. var.name = name
  1975. var._n_expansions = 0
  1976. self.variables[name] = var
  1977. # += acts like = on undefined variables (defines a recursive
  1978. # variable)
  1979. if op == "+=":
  1980. op = "="
  1981. if op == "=":
  1982. var.is_recursive = True
  1983. var.value = val
  1984. elif op == ":=":
  1985. var.is_recursive = False
  1986. var.value = self._expand_whole(val, ())
  1987. else: # op == "+="
  1988. # += does immediate expansion if the variable was last set
  1989. # with :=
  1990. var.value += " " + (val if var.is_recursive else
  1991. self._expand_whole(val, ()))
  1992. def _expand_whole(self, s, args):
  1993. # Expands preprocessor macros in all of 's'. Used whenever we don't
  1994. # have to worry about delimiters. See _expand_macro() re. the 'args'
  1995. # parameter.
  1996. #
  1997. # Returns the expanded string.
  1998. i = 0
  1999. while 1:
  2000. i = s.find("$(", i)
  2001. if i == -1:
  2002. break
  2003. s, i = self._expand_macro(s, i, args)
  2004. return s
  2005. def _expand_name(self, s, i):
  2006. # Expands a symbol name starting at index 'i' in 's'.
  2007. #
  2008. # Returns the expanded name, the expanded 's' (including the part
  2009. # before the name), and the index of the first character in the next
  2010. # token after the name.
  2011. s, end_i = self._expand_name_iter(s, i)
  2012. name = s[i:end_i]
  2013. # isspace() is False for empty strings
  2014. if not name.strip():
  2015. # Avoid creating a Kconfig symbol with a blank name. It's almost
  2016. # guaranteed to be an error.
  2017. self._parse_error("macro expanded to blank string")
  2018. # Skip trailing whitespace
  2019. while end_i < len(s) and s[end_i].isspace():
  2020. end_i += 1
  2021. return name, s, end_i
  2022. def _expand_name_iter(self, s, i):
  2023. # Expands a symbol name starting at index 'i' in 's'.
  2024. #
  2025. # Returns the expanded 's' (including the part before the name) and the
  2026. # index of the first character after the expanded name in 's'.
  2027. while 1:
  2028. match = _name_special_search(s, i)
  2029. if match.group() == "$(":
  2030. s, i = self._expand_macro(s, match.start(), ())
  2031. else:
  2032. return (s, match.start())
  2033. def _expand_str(self, s, i):
  2034. # Expands a quoted string starting at index 'i' in 's'. Handles both
  2035. # backslash escapes and macro expansion.
  2036. #
  2037. # Returns the expanded 's' (including the part before the string) and
  2038. # the index of the first character after the expanded string in 's'.
  2039. quote = s[i]
  2040. i += 1 # Skip over initial "/'
  2041. while 1:
  2042. match = _string_special_search(s, i)
  2043. if not match:
  2044. self._parse_error("unterminated string")
  2045. if match.group() == quote:
  2046. # Found the end of the string
  2047. return (s, match.end())
  2048. elif match.group() == "\\":
  2049. # Replace '\x' with 'x'. 'i' ends up pointing to the character
  2050. # after 'x', which allows macros to be canceled with '\$(foo)'.
  2051. i = match.end()
  2052. s = s[:match.start()] + s[i:]
  2053. elif match.group() == "$(":
  2054. # A macro call within the string
  2055. s, i = self._expand_macro(s, match.start(), ())
  2056. else:
  2057. # A ' quote within " quotes or vice versa
  2058. i += 1
  2059. def _expand_macro(self, s, i, args):
  2060. # Expands a macro starting at index 'i' in 's'. If this macro resulted
  2061. # from the expansion of another macro, 'args' holds the arguments
  2062. # passed to that macro.
  2063. #
  2064. # Returns the expanded 's' (including the part before the macro) and
  2065. # the index of the first character after the expanded macro in 's'.
  2066. start = i
  2067. i += 2 # Skip over "$("
  2068. # Start of current macro argument
  2069. arg_start = i
  2070. # Arguments of this macro call
  2071. new_args = []
  2072. while 1:
  2073. match = _macro_special_search(s, i)
  2074. if not match:
  2075. self._parse_error("missing end parenthesis in macro expansion")
  2076. if match.group() == ")":
  2077. # Found the end of the macro
  2078. new_args.append(s[arg_start:match.start()])
  2079. prefix = s[:start]
  2080. # $(1) is replaced by the first argument to the function, etc.,
  2081. # provided at least that many arguments were passed
  2082. try:
  2083. # Does the macro look like an integer, with a corresponding
  2084. # argument? If so, expand it to the value of the argument.
  2085. prefix += args[int(new_args[0])]
  2086. except (ValueError, IndexError):
  2087. # Regular variables are just functions without arguments,
  2088. # and also go through the function value path
  2089. prefix += self._fn_val(new_args)
  2090. return (prefix + s[match.end():],
  2091. len(prefix))
  2092. elif match.group() == ",":
  2093. # Found the end of a macro argument
  2094. new_args.append(s[arg_start:match.start()])
  2095. arg_start = i = match.end()
  2096. else: # match.group() == "$("
  2097. # A nested macro call within the macro
  2098. s, i = self._expand_macro(s, match.start(), args)
  2099. def _fn_val(self, args):
  2100. # Returns the result of calling the function args[0] with the arguments
  2101. # args[1..len(args)-1]. Plain variables are treated as functions
  2102. # without arguments.
  2103. fn = args[0]
  2104. if fn in self.variables:
  2105. var = self.variables[fn]
  2106. if len(args) == 1:
  2107. # Plain variable
  2108. if var._n_expansions:
  2109. self._parse_error("Preprocessor variable {} recursively "
  2110. "references itself".format(var.name))
  2111. elif var._n_expansions > 100:
  2112. # Allow functions to call themselves, but guess that functions
  2113. # that are overly recursive are stuck
  2114. self._parse_error("Preprocessor function {} seems stuck "
  2115. "in infinite recursion".format(var.name))
  2116. var._n_expansions += 1
  2117. res = self._expand_whole(self.variables[fn].value, args)
  2118. var._n_expansions -= 1
  2119. return res
  2120. if fn in self._functions:
  2121. # Built-in or user-defined function
  2122. py_fn, min_arg, max_arg = self._functions[fn]
  2123. if len(args) - 1 < min_arg or \
  2124. (max_arg is not None and len(args) - 1 > max_arg):
  2125. if min_arg == max_arg:
  2126. expected_args = min_arg
  2127. elif max_arg is None:
  2128. expected_args = "{} or more".format(min_arg)
  2129. else:
  2130. expected_args = "{}-{}".format(min_arg, max_arg)
  2131. raise KconfigError("{}:{}: bad number of arguments in call "
  2132. "to {}, expected {}, got {}"
  2133. .format(self.filename, self.linenr, fn,
  2134. expected_args, len(args) - 1))
  2135. return py_fn(self, *args)
  2136. # Environment variables are tried last
  2137. if fn in os.environ:
  2138. self.env_vars.add(fn)
  2139. return os.environ[fn]
  2140. return ""
  2141. #
  2142. # Parsing
  2143. #
  2144. def _make_and(self, e1, e2):
  2145. # Constructs an AND (&&) expression. Performs trivial simplification.
  2146. if e1 is self.y:
  2147. return e2
  2148. if e2 is self.y:
  2149. return e1
  2150. if e1 is self.n or e2 is self.n:
  2151. return self.n
  2152. return (AND, e1, e2)
  2153. def _make_or(self, e1, e2):
  2154. # Constructs an OR (||) expression. Performs trivial simplification.
  2155. if e1 is self.n:
  2156. return e2
  2157. if e2 is self.n:
  2158. return e1
  2159. if e1 is self.y or e2 is self.y:
  2160. return self.y
  2161. return (OR, e1, e2)
  2162. def _parse_block(self, end_token, parent, prev):
  2163. # Parses a block, which is the contents of either a file or an if,
  2164. # menu, or choice statement.
  2165. #
  2166. # end_token:
  2167. # The token that ends the block, e.g. _T_ENDIF ("endif") for ifs.
  2168. # None for files.
  2169. #
  2170. # parent:
  2171. # The parent menu node, corresponding to a menu, Choice, or 'if'.
  2172. # 'if's are flattened after parsing.
  2173. #
  2174. # prev:
  2175. # The previous menu node. New nodes will be added after this one (by
  2176. # modifying their 'next' pointer).
  2177. #
  2178. # 'prev' is reused to parse a list of child menu nodes (for a menu or
  2179. # Choice): After parsing the children, the 'next' pointer is assigned
  2180. # to the 'list' pointer to "tilt up" the children above the node.
  2181. #
  2182. # Returns the final menu node in the block (or 'prev' if the block is
  2183. # empty). This allows chaining.
  2184. while self._next_line():
  2185. t0 = self._tokens[0]
  2186. if t0 is _T_CONFIG or t0 is _T_MENUCONFIG:
  2187. # The tokenizer allocates Symbol objects for us
  2188. sym = self._tokens[1]
  2189. if sym.__class__ is not Symbol or sym.is_constant:
  2190. self._parse_error("missing or bad symbol name")
  2191. if self._tokens[2] is not None:
  2192. self._trailing_tokens_error()
  2193. self.defined_syms.append(sym)
  2194. node = MenuNode()
  2195. node.kconfig = self
  2196. node.item = sym
  2197. node.is_menuconfig = (t0 is _T_MENUCONFIG)
  2198. node.prompt = node.help = node.list = None
  2199. node.parent = parent
  2200. node.filename = self.filename
  2201. node.linenr = self.linenr
  2202. node.include_path = self._include_path
  2203. sym.nodes.append(node)
  2204. self._parse_properties(node)
  2205. if node.is_menuconfig and not node.prompt:
  2206. self._warn("the menuconfig symbol {} has no prompt"
  2207. .format(_name_and_loc(sym)))
  2208. # Equivalent to
  2209. #
  2210. # prev.next = node
  2211. # prev = node
  2212. #
  2213. # due to tricky Python semantics. The order matters.
  2214. prev.next = prev = node
  2215. elif t0 is None:
  2216. # Blank line
  2217. continue
  2218. elif t0 in _SOURCE_TOKENS:
  2219. pattern = self._expect_str_and_eol()
  2220. if t0 in _REL_SOURCE_TOKENS:
  2221. # Relative source
  2222. pattern = join(dirname(self.filename), pattern)
  2223. # - glob() doesn't support globbing relative to a directory, so
  2224. # we need to prepend $srctree to 'pattern'. Use join()
  2225. # instead of '+' so that an absolute path in 'pattern' is
  2226. # preserved.
  2227. #
  2228. # - Sort the glob results to ensure a consistent ordering of
  2229. # Kconfig symbols, which indirectly ensures a consistent
  2230. # ordering in e.g. .config files
  2231. filenames = sorted(iglob(join(self._srctree_prefix, pattern)))
  2232. if not filenames and t0 in _OBL_SOURCE_TOKENS:
  2233. raise KconfigError(
  2234. "{}:{}: '{}' not found (in '{}'). Check that "
  2235. "environment variables are set correctly (e.g. "
  2236. "$srctree, which is {}). Also note that unset "
  2237. "environment variables expand to the empty string."
  2238. .format(self.filename, self.linenr, pattern,
  2239. self._line.strip(),
  2240. "set to '{}'".format(self.srctree)
  2241. if self.srctree else "unset or blank"))
  2242. for filename in filenames:
  2243. self._enter_file(filename)
  2244. prev = self._parse_block(None, parent, prev)
  2245. self._leave_file()
  2246. elif t0 is end_token:
  2247. # Reached the end of the block. Terminate the final node and
  2248. # return it.
  2249. if self._tokens[1] is not None:
  2250. self._trailing_tokens_error()
  2251. prev.next = None
  2252. return prev
  2253. elif t0 is _T_IF:
  2254. node = MenuNode()
  2255. node.item = node.prompt = None
  2256. node.parent = parent
  2257. node.dep = self._expect_expr_and_eol()
  2258. self._parse_block(_T_ENDIF, node, node)
  2259. node.list = node.next
  2260. prev.next = prev = node
  2261. elif t0 is _T_MENU:
  2262. node = MenuNode()
  2263. node.kconfig = self
  2264. node.item = t0 # _T_MENU == MENU
  2265. node.is_menuconfig = True
  2266. node.prompt = (self._expect_str_and_eol(), self.y)
  2267. node.visibility = self.y
  2268. node.parent = parent
  2269. node.filename = self.filename
  2270. node.linenr = self.linenr
  2271. node.include_path = self._include_path
  2272. self.menus.append(node)
  2273. self._parse_properties(node)
  2274. self._parse_block(_T_ENDMENU, node, node)
  2275. node.list = node.next
  2276. prev.next = prev = node
  2277. elif t0 is _T_COMMENT:
  2278. node = MenuNode()
  2279. node.kconfig = self
  2280. node.item = t0 # _T_COMMENT == COMMENT
  2281. node.is_menuconfig = False
  2282. node.prompt = (self._expect_str_and_eol(), self.y)
  2283. node.list = None
  2284. node.parent = parent
  2285. node.filename = self.filename
  2286. node.linenr = self.linenr
  2287. node.include_path = self._include_path
  2288. self.comments.append(node)
  2289. self._parse_properties(node)
  2290. prev.next = prev = node
  2291. elif t0 is _T_CHOICE:
  2292. if self._tokens[1] is None:
  2293. choice = Choice()
  2294. choice.direct_dep = self.n
  2295. else:
  2296. # Named choice
  2297. name = self._expect_str_and_eol()
  2298. choice = self.named_choices.get(name)
  2299. if not choice:
  2300. choice = Choice()
  2301. choice.name = name
  2302. choice.direct_dep = self.n
  2303. self.named_choices[name] = choice
  2304. self.choices.append(choice)
  2305. node = MenuNode()
  2306. node.kconfig = choice.kconfig = self
  2307. node.item = choice
  2308. node.is_menuconfig = True
  2309. node.prompt = node.help = None
  2310. node.parent = parent
  2311. node.filename = self.filename
  2312. node.linenr = self.linenr
  2313. node.include_path = self._include_path
  2314. choice.nodes.append(node)
  2315. self._parse_properties(node)
  2316. self._parse_block(_T_ENDCHOICE, node, node)
  2317. node.list = node.next
  2318. prev.next = prev = node
  2319. elif t0 is _T_MAINMENU:
  2320. self.top_node.prompt = (self._expect_str_and_eol(), self.y)
  2321. else:
  2322. # A valid endchoice/endif/endmenu is caught by the 'end_token'
  2323. # check above
  2324. self._parse_error(
  2325. "no corresponding 'choice'" if t0 is _T_ENDCHOICE else
  2326. "no corresponding 'if'" if t0 is _T_ENDIF else
  2327. "no corresponding 'menu'" if t0 is _T_ENDMENU else
  2328. "unrecognized construct")
  2329. # End of file reached. Terminate the final node and return it.
  2330. if end_token:
  2331. raise KconfigError(
  2332. "expected '{}' at end of '{}'"
  2333. .format("endchoice" if end_token is _T_ENDCHOICE else
  2334. "endif" if end_token is _T_ENDIF else
  2335. "endmenu",
  2336. self.filename))
  2337. prev.next = None
  2338. return prev
  2339. def _parse_cond(self):
  2340. # Parses an optional 'if <expr>' construct and returns the parsed
  2341. # <expr>, or self.y if the next token is not _T_IF
  2342. expr = self._parse_expr(True) if self._check_token(_T_IF) else self.y
  2343. if self._tokens[self._tokens_i] is not None:
  2344. self._trailing_tokens_error()
  2345. return expr
  2346. def _parse_properties(self, node):
  2347. # Parses and adds properties to the MenuNode 'node' (type, 'prompt',
  2348. # 'default's, etc.) Properties are later copied up to symbols and
  2349. # choices in a separate pass after parsing, in e.g.
  2350. # _add_props_to_sym().
  2351. #
  2352. # An older version of this code added properties directly to symbols
  2353. # and choices instead of to their menu nodes (and handled dependency
  2354. # propagation simultaneously), but that loses information on where a
  2355. # property is added when a symbol or choice is defined in multiple
  2356. # locations. Some Kconfig configuration systems rely heavily on such
  2357. # symbols, and better docs can be generated by keeping track of where
  2358. # properties are added.
  2359. #
  2360. # node:
  2361. # The menu node we're parsing properties on
  2362. # Dependencies from 'depends on'. Will get propagated to the properties
  2363. # below.
  2364. node.dep = self.y
  2365. while self._next_line():
  2366. t0 = self._tokens[0]
  2367. if t0 in _TYPE_TOKENS:
  2368. # Relies on '_T_BOOL is BOOL', etc., to save a conversion
  2369. self._set_type(node, t0)
  2370. if self._tokens[1] is not None:
  2371. self._parse_prompt(node)
  2372. elif t0 is _T_DEPENDS:
  2373. if not self._check_token(_T_ON):
  2374. self._parse_error("expected 'on' after 'depends'")
  2375. node.dep = self._make_and(node.dep,
  2376. self._expect_expr_and_eol())
  2377. elif t0 is _T_HELP:
  2378. self._parse_help(node)
  2379. elif t0 is _T_SELECT:
  2380. if node.item.__class__ is not Symbol:
  2381. self._parse_error("only symbols can select")
  2382. node.selects.append((self._expect_nonconst_sym(),
  2383. self._parse_cond()))
  2384. elif t0 is None:
  2385. # Blank line
  2386. continue
  2387. elif t0 is _T_DEFAULT:
  2388. node.defaults.append((self._parse_expr(False),
  2389. self._parse_cond()))
  2390. elif t0 in _DEF_TOKEN_TO_TYPE:
  2391. self._set_type(node, _DEF_TOKEN_TO_TYPE[t0])
  2392. node.defaults.append((self._parse_expr(False),
  2393. self._parse_cond()))
  2394. elif t0 is _T_PROMPT:
  2395. self._parse_prompt(node)
  2396. elif t0 is _T_RANGE:
  2397. node.ranges.append((self._expect_sym(), self._expect_sym(),
  2398. self._parse_cond()))
  2399. elif t0 is _T_IMPLY:
  2400. if node.item.__class__ is not Symbol:
  2401. self._parse_error("only symbols can imply")
  2402. node.implies.append((self._expect_nonconst_sym(),
  2403. self._parse_cond()))
  2404. elif t0 is _T_VISIBLE:
  2405. if not self._check_token(_T_IF):
  2406. self._parse_error("expected 'if' after 'visible'")
  2407. node.visibility = self._make_and(node.visibility,
  2408. self._expect_expr_and_eol())
  2409. elif t0 is _T_OPTION:
  2410. if self._check_token(_T_ENV):
  2411. if not self._check_token(_T_EQUAL):
  2412. self._parse_error("expected '=' after 'env'")
  2413. env_var = self._expect_str_and_eol()
  2414. node.item.env_var = env_var
  2415. if env_var in os.environ:
  2416. node.defaults.append(
  2417. (self._lookup_const_sym(os.environ[env_var]),
  2418. self.y))
  2419. else:
  2420. self._warn("{1} has 'option env=\"{0}\"', "
  2421. "but the environment variable {0} is not "
  2422. "set".format(node.item.name, env_var),
  2423. self.filename, self.linenr)
  2424. if env_var != node.item.name:
  2425. self._warn("Kconfiglib expands environment variables "
  2426. "in strings directly, meaning you do not "
  2427. "need 'option env=...' \"bounce\" symbols. "
  2428. "For compatibility with the C tools, "
  2429. "rename {} to {} (so that the symbol name "
  2430. "matches the environment variable name)."
  2431. .format(node.item.name, env_var),
  2432. self.filename, self.linenr)
  2433. elif self._check_token(_T_DEFCONFIG_LIST):
  2434. if not self.defconfig_list:
  2435. self.defconfig_list = node.item
  2436. else:
  2437. self._warn("'option defconfig_list' set on multiple "
  2438. "symbols ({0} and {1}). Only {0} will be "
  2439. "used.".format(self.defconfig_list.name,
  2440. node.item.name),
  2441. self.filename, self.linenr)
  2442. elif self._check_token(_T_MODULES):
  2443. # To reduce warning spam, only warn if 'option modules' is
  2444. # set on some symbol that isn't MODULES, which should be
  2445. # safe. I haven't run into any projects that make use
  2446. # modules besides the kernel yet, and there it's likely to
  2447. # keep being called "MODULES".
  2448. if node.item is not self.modules:
  2449. self._warn("the 'modules' option is not supported. "
  2450. "Let me know if this is a problem for you, "
  2451. "as it wouldn't be that hard to implement. "
  2452. "Note that modules are supported -- "
  2453. "Kconfiglib just assumes the symbol name "
  2454. "MODULES, like older versions of the C "
  2455. "implementation did when 'option modules' "
  2456. "wasn't used.",
  2457. self.filename, self.linenr)
  2458. elif self._check_token(_T_ALLNOCONFIG_Y):
  2459. if node.item.__class__ is not Symbol:
  2460. self._parse_error("the 'allnoconfig_y' option is only "
  2461. "valid for symbols")
  2462. node.item.is_allnoconfig_y = True
  2463. else:
  2464. self._parse_error("unrecognized option")
  2465. elif t0 is _T_OPTIONAL:
  2466. if node.item.__class__ is not Choice:
  2467. self._parse_error('"optional" is only valid for choices')
  2468. node.item.is_optional = True
  2469. else:
  2470. # Reuse the tokens for the non-property line later
  2471. self._reuse_tokens = True
  2472. return
  2473. def _set_type(self, node, new_type):
  2474. # UNKNOWN is falsy
  2475. if node.item.orig_type and node.item.orig_type is not new_type:
  2476. self._warn("{} defined with multiple types, {} will be used"
  2477. .format(_name_and_loc(node.item),
  2478. TYPE_TO_STR[new_type]))
  2479. node.item.orig_type = new_type
  2480. def _parse_prompt(self, node):
  2481. # 'prompt' properties override each other within a single definition of
  2482. # a symbol, but additional prompts can be added by defining the symbol
  2483. # multiple times
  2484. if node.prompt:
  2485. self._warn(_name_and_loc(node.item) +
  2486. " defined with multiple prompts in single location")
  2487. prompt = self._tokens[1]
  2488. self._tokens_i = 2
  2489. if prompt.__class__ is not str:
  2490. self._parse_error("expected prompt string")
  2491. if prompt != prompt.strip():
  2492. self._warn(_name_and_loc(node.item) +
  2493. " has leading or trailing whitespace in its prompt")
  2494. # This avoid issues for e.g. reStructuredText documentation, where
  2495. # '*prompt *' is invalid
  2496. prompt = prompt.strip()
  2497. node.prompt = (prompt, self._parse_cond())
  2498. def _parse_help(self, node):
  2499. if node.help is not None:
  2500. self._warn(_name_and_loc(node.item) + " defined with more than "
  2501. "one help text -- only the last one will be used")
  2502. # Micro-optimization. This code is pretty hot.
  2503. readline = self._readline
  2504. # Find first non-blank (not all-space) line and get its
  2505. # indentation
  2506. while 1:
  2507. line = readline()
  2508. self.linenr += 1
  2509. if not line:
  2510. self._empty_help(node, line)
  2511. return
  2512. if not line.isspace():
  2513. break
  2514. len_ = len # Micro-optimization
  2515. # Use a separate 'expline' variable here and below to avoid stomping on
  2516. # any tabs people might've put deliberately into the first line after
  2517. # the help text
  2518. expline = line.expandtabs()
  2519. indent = len_(expline) - len_(expline.lstrip())
  2520. if not indent:
  2521. self._empty_help(node, line)
  2522. return
  2523. # The help text goes on till the first non-blank line with less indent
  2524. # than the first line
  2525. # Add the first line
  2526. lines = [expline[indent:]]
  2527. add_line = lines.append # Micro-optimization
  2528. while 1:
  2529. line = readline()
  2530. if line.isspace():
  2531. # No need to preserve the exact whitespace in these
  2532. add_line("\n")
  2533. elif not line:
  2534. # End of file
  2535. break
  2536. else:
  2537. expline = line.expandtabs()
  2538. if len_(expline) - len_(expline.lstrip()) < indent:
  2539. break
  2540. add_line(expline[indent:])
  2541. self.linenr += len_(lines)
  2542. node.help = "".join(lines).rstrip()
  2543. if line:
  2544. self._line_after_help(line)
  2545. def _empty_help(self, node, line):
  2546. self._warn(_name_and_loc(node.item) +
  2547. " has 'help' but empty help text")
  2548. node.help = ""
  2549. if line:
  2550. self._line_after_help(line)
  2551. def _parse_expr(self, transform_m):
  2552. # Parses an expression from the tokens in Kconfig._tokens using a
  2553. # simple top-down approach. See the module docstring for the expression
  2554. # format.
  2555. #
  2556. # transform_m:
  2557. # True if m should be rewritten to m && MODULES. See the
  2558. # Kconfig.eval_string() documentation.
  2559. # Grammar:
  2560. #
  2561. # expr: and_expr ['||' expr]
  2562. # and_expr: factor ['&&' and_expr]
  2563. # factor: <symbol> ['='/'!='/'<'/... <symbol>]
  2564. # '!' factor
  2565. # '(' expr ')'
  2566. #
  2567. # It helps to think of the 'expr: and_expr' case as a single-operand OR
  2568. # (no ||), and of the 'and_expr: factor' case as a single-operand AND
  2569. # (no &&). Parsing code is always a bit tricky.
  2570. # Mind dump: parse_factor() and two nested loops for OR and AND would
  2571. # work as well. The straightforward implementation there gives a
  2572. # (op, (op, (op, A, B), C), D) parse for A op B op C op D. Representing
  2573. # expressions as (op, [list of operands]) instead goes nicely with that
  2574. # version, but is wasteful for short expressions and complicates
  2575. # expression evaluation and other code that works on expressions (more
  2576. # complicated code likely offsets any performance gain from less
  2577. # recursion too). If we also try to optimize the list representation by
  2578. # merging lists when possible (e.g. when ANDing two AND expressions),
  2579. # we end up allocating a ton of lists instead of reusing expressions,
  2580. # which is bad.
  2581. and_expr = self._parse_and_expr(transform_m)
  2582. # Return 'and_expr' directly if we have a "single-operand" OR.
  2583. # Otherwise, parse the expression on the right and make an OR node.
  2584. # This turns A || B || C || D into (OR, A, (OR, B, (OR, C, D))).
  2585. return and_expr if not self._check_token(_T_OR) else \
  2586. (OR, and_expr, self._parse_expr(transform_m))
  2587. def _parse_and_expr(self, transform_m):
  2588. factor = self._parse_factor(transform_m)
  2589. # Return 'factor' directly if we have a "single-operand" AND.
  2590. # Otherwise, parse the right operand and make an AND node. This turns
  2591. # A && B && C && D into (AND, A, (AND, B, (AND, C, D))).
  2592. return factor if not self._check_token(_T_AND) else \
  2593. (AND, factor, self._parse_and_expr(transform_m))
  2594. def _parse_factor(self, transform_m):
  2595. token = self._tokens[self._tokens_i]
  2596. self._tokens_i += 1
  2597. if token.__class__ is Symbol:
  2598. # Plain symbol or relation
  2599. if self._tokens[self._tokens_i] not in _RELATIONS:
  2600. # Plain symbol
  2601. # For conditional expressions ('depends on <expr>',
  2602. # '... if <expr>', etc.), m is rewritten to m && MODULES.
  2603. if transform_m and token is self.m:
  2604. return (AND, self.m, self.modules)
  2605. return token
  2606. # Relation
  2607. #
  2608. # _T_EQUAL, _T_UNEQUAL, etc., deliberately have the same values as
  2609. # EQUAL, UNEQUAL, etc., so we can just use the token directly
  2610. self._tokens_i += 1
  2611. return (self._tokens[self._tokens_i - 1], token,
  2612. self._expect_sym())
  2613. if token is _T_NOT:
  2614. # token == _T_NOT == NOT
  2615. return (token, self._parse_factor(transform_m))
  2616. if token is _T_OPEN_PAREN:
  2617. expr_parse = self._parse_expr(transform_m)
  2618. if self._check_token(_T_CLOSE_PAREN):
  2619. return expr_parse
  2620. self._parse_error("malformed expression")
  2621. #
  2622. # Caching and invalidation
  2623. #
  2624. def _build_dep(self):
  2625. # Populates the Symbol/Choice._dependents sets, which contain all other
  2626. # items (symbols and choices) that immediately depend on the item in
  2627. # the sense that changing the value of the item might affect the value
  2628. # of the dependent items. This is used for caching/invalidation.
  2629. #
  2630. # The calculated sets might be larger than necessary as we don't do any
  2631. # complex analysis of the expressions.
  2632. make_depend_on = _make_depend_on # Micro-optimization
  2633. # Only calculate _dependents for defined symbols. Constant and
  2634. # undefined symbols could theoretically be selected/implied, but it
  2635. # wouldn't change their value, so it's not a true dependency.
  2636. for sym in self.unique_defined_syms:
  2637. # Symbols depend on the following:
  2638. # The prompt conditions
  2639. for node in sym.nodes:
  2640. if node.prompt:
  2641. make_depend_on(sym, node.prompt[1])
  2642. # The default values and their conditions
  2643. for value, cond in sym.defaults:
  2644. make_depend_on(sym, value)
  2645. make_depend_on(sym, cond)
  2646. # The reverse and weak reverse dependencies
  2647. make_depend_on(sym, sym.rev_dep)
  2648. make_depend_on(sym, sym.weak_rev_dep)
  2649. # The ranges along with their conditions
  2650. for low, high, cond in sym.ranges:
  2651. make_depend_on(sym, low)
  2652. make_depend_on(sym, high)
  2653. make_depend_on(sym, cond)
  2654. # The direct dependencies. This is usually redundant, as the direct
  2655. # dependencies get propagated to properties, but it's needed to get
  2656. # invalidation solid for 'imply', which only checks the direct
  2657. # dependencies (even if there are no properties to propagate it
  2658. # to).
  2659. make_depend_on(sym, sym.direct_dep)
  2660. # In addition to the above, choice symbols depend on the choice
  2661. # they're in, but that's handled automatically since the Choice is
  2662. # propagated to the conditions of the properties before
  2663. # _build_dep() runs.
  2664. for choice in self.unique_choices:
  2665. # Choices depend on the following:
  2666. # The prompt conditions
  2667. for node in choice.nodes:
  2668. if node.prompt:
  2669. make_depend_on(choice, node.prompt[1])
  2670. # The default symbol conditions
  2671. for _, cond in choice.defaults:
  2672. make_depend_on(choice, cond)
  2673. def _add_choice_deps(self):
  2674. # Choices also depend on the choice symbols themselves, because the
  2675. # y-mode selection of the choice might change if a choice symbol's
  2676. # visibility changes.
  2677. #
  2678. # We add these dependencies separately after dependency loop detection.
  2679. # The invalidation algorithm can handle the resulting
  2680. # <choice symbol> <-> <choice> dependency loops, but they make loop
  2681. # detection awkward.
  2682. for choice in self.unique_choices:
  2683. for sym in choice.syms:
  2684. sym._dependents.add(choice)
  2685. def _invalidate_all(self):
  2686. # Undefined symbols never change value and don't need to be
  2687. # invalidated, so we can just iterate over defined symbols.
  2688. # Invalidating constant symbols would break things horribly.
  2689. for sym in self.unique_defined_syms:
  2690. sym._invalidate()
  2691. for choice in self.unique_choices:
  2692. choice._invalidate()
  2693. #
  2694. # Post-parsing menu tree processing, including dependency propagation and
  2695. # implicit submenu creation
  2696. #
  2697. def _finalize_node(self, node, visible_if):
  2698. # Finalizes a menu node and its children:
  2699. #
  2700. # - Copies properties from menu nodes up to their contained
  2701. # symbols/choices
  2702. #
  2703. # - Propagates dependencies from parent to child nodes
  2704. #
  2705. # - Creates implicit menus (see kconfig-language.txt)
  2706. #
  2707. # - Removes 'if' nodes
  2708. #
  2709. # - Sets 'choice' types and registers choice symbols
  2710. #
  2711. # menu_finalize() in the C implementation is similar.
  2712. #
  2713. # node:
  2714. # The menu node to finalize. This node and its children will have
  2715. # been finalized when the function returns, and any implicit menus
  2716. # will have been created.
  2717. #
  2718. # visible_if:
  2719. # Dependencies from 'visible if' on parent menus. These are added to
  2720. # the prompts of symbols and choices.
  2721. if node.item.__class__ is Symbol:
  2722. # Copy defaults, ranges, selects, and implies to the Symbol
  2723. self._add_props_to_sym(node)
  2724. # Find any items that should go in an implicit menu rooted at the
  2725. # symbol
  2726. cur = node
  2727. while cur.next and _auto_menu_dep(node, cur.next):
  2728. # This makes implicit submenu creation work recursively, with
  2729. # implicit menus inside implicit menus
  2730. self._finalize_node(cur.next, visible_if)
  2731. cur = cur.next
  2732. cur.parent = node
  2733. if cur is not node:
  2734. # Found symbols that should go in an implicit submenu. Tilt
  2735. # them up above us.
  2736. node.list = node.next
  2737. node.next = cur.next
  2738. cur.next = None
  2739. elif node.list:
  2740. # The menu node is a choice, menu, or if. Finalize each child node.
  2741. if node.item is MENU:
  2742. visible_if = self._make_and(visible_if, node.visibility)
  2743. # Propagate the menu node's dependencies to each child menu node.
  2744. #
  2745. # This needs to go before the recursive _finalize_node() call so
  2746. # that implicit submenu creation can look ahead at dependencies.
  2747. self._propagate_deps(node, visible_if)
  2748. # Finalize the children
  2749. cur = node.list
  2750. while cur:
  2751. self._finalize_node(cur, visible_if)
  2752. cur = cur.next
  2753. if node.list:
  2754. # node's children have been individually finalized. Do final steps
  2755. # to finalize this "level" in the menu tree.
  2756. _flatten(node.list)
  2757. _remove_ifs(node)
  2758. # Empty choices (node.list None) are possible, so this needs to go
  2759. # outside
  2760. if node.item.__class__ is Choice:
  2761. # Add the node's non-node-specific properties to the choice, like
  2762. # _add_props_to_sym() does
  2763. choice = node.item
  2764. choice.direct_dep = self._make_or(choice.direct_dep, node.dep)
  2765. choice.defaults += node.defaults
  2766. _finalize_choice(node)
  2767. def _propagate_deps(self, node, visible_if):
  2768. # Propagates 'node's dependencies to its child menu nodes
  2769. # If the parent node holds a Choice, we use the Choice itself as the
  2770. # parent dependency. This makes sense as the value (mode) of the choice
  2771. # limits the visibility of the contained choice symbols. The C
  2772. # implementation works the same way.
  2773. #
  2774. # Due to the similar interface, Choice works as a drop-in replacement
  2775. # for Symbol here.
  2776. basedep = node.item if node.item.__class__ is Choice else node.dep
  2777. cur = node.list
  2778. while cur:
  2779. dep = cur.dep = self._make_and(cur.dep, basedep)
  2780. if cur.item.__class__ in _SYMBOL_CHOICE:
  2781. # Propagate 'visible if' and dependencies to the prompt
  2782. if cur.prompt:
  2783. cur.prompt = (cur.prompt[0],
  2784. self._make_and(
  2785. cur.prompt[1],
  2786. self._make_and(visible_if, dep)))
  2787. # Propagate dependencies to defaults
  2788. if cur.defaults:
  2789. cur.defaults = [(default, self._make_and(cond, dep))
  2790. for default, cond in cur.defaults]
  2791. # Propagate dependencies to ranges
  2792. if cur.ranges:
  2793. cur.ranges = [(low, high, self._make_and(cond, dep))
  2794. for low, high, cond in cur.ranges]
  2795. # Propagate dependencies to selects
  2796. if cur.selects:
  2797. cur.selects = [(target, self._make_and(cond, dep))
  2798. for target, cond in cur.selects]
  2799. # Propagate dependencies to implies
  2800. if cur.implies:
  2801. cur.implies = [(target, self._make_and(cond, dep))
  2802. for target, cond in cur.implies]
  2803. elif cur.prompt: # Not a symbol/choice
  2804. # Propagate dependencies to the prompt. 'visible if' is only
  2805. # propagated to symbols/choices.
  2806. cur.prompt = (cur.prompt[0],
  2807. self._make_and(cur.prompt[1], dep))
  2808. cur = cur.next
  2809. def _add_props_to_sym(self, node):
  2810. # Copies properties from the menu node 'node' up to its contained
  2811. # symbol, and adds (weak) reverse dependencies to selected/implied
  2812. # symbols.
  2813. #
  2814. # This can't be rolled into _propagate_deps(), because that function
  2815. # traverses the menu tree roughly breadth-first, meaning properties on
  2816. # symbols defined in multiple locations could end up in the wrong
  2817. # order.
  2818. sym = node.item
  2819. # See the Symbol class docstring
  2820. sym.direct_dep = self._make_or(sym.direct_dep, node.dep)
  2821. sym.defaults += node.defaults
  2822. sym.ranges += node.ranges
  2823. sym.selects += node.selects
  2824. sym.implies += node.implies
  2825. # Modify the reverse dependencies of the selected symbol
  2826. for target, cond in node.selects:
  2827. target.rev_dep = self._make_or(
  2828. target.rev_dep,
  2829. self._make_and(sym, cond))
  2830. # Modify the weak reverse dependencies of the implied
  2831. # symbol
  2832. for target, cond in node.implies:
  2833. target.weak_rev_dep = self._make_or(
  2834. target.weak_rev_dep,
  2835. self._make_and(sym, cond))
  2836. #
  2837. # Misc.
  2838. #
  2839. def _check_sym_sanity(self):
  2840. # Checks various symbol properties that are handiest to check after
  2841. # parsing. Only generates errors and warnings.
  2842. def num_ok(sym, type_):
  2843. # Returns True if the (possibly constant) symbol 'sym' is valid as a value
  2844. # for a symbol of type type_ (INT or HEX)
  2845. # 'not sym.nodes' implies a constant or undefined symbol, e.g. a plain
  2846. # "123"
  2847. if not sym.nodes:
  2848. return _is_base_n(sym.name, _TYPE_TO_BASE[type_])
  2849. return sym.orig_type is type_
  2850. for sym in self.unique_defined_syms:
  2851. if sym.orig_type in _BOOL_TRISTATE:
  2852. # A helper function could be factored out here, but keep it
  2853. # speedy/straightforward
  2854. for target_sym, _ in sym.selects:
  2855. if target_sym.orig_type not in _BOOL_TRISTATE_UNKNOWN:
  2856. self._warn("{} selects the {} symbol {}, which is not "
  2857. "bool or tristate"
  2858. .format(_name_and_loc(sym),
  2859. TYPE_TO_STR[target_sym.orig_type],
  2860. _name_and_loc(target_sym)))
  2861. for target_sym, _ in sym.implies:
  2862. if target_sym.orig_type not in _BOOL_TRISTATE_UNKNOWN:
  2863. self._warn("{} implies the {} symbol {}, which is not "
  2864. "bool or tristate"
  2865. .format(_name_and_loc(sym),
  2866. TYPE_TO_STR[target_sym.orig_type],
  2867. _name_and_loc(target_sym)))
  2868. elif sym.orig_type: # STRING/INT/HEX
  2869. for default, _ in sym.defaults:
  2870. if default.__class__ is not Symbol:
  2871. raise KconfigError(
  2872. "the {} symbol {} has a malformed default {} -- expected "
  2873. "a single symbol"
  2874. .format(TYPE_TO_STR[sym.orig_type], _name_and_loc(sym),
  2875. expr_str(default)))
  2876. if sym.orig_type is STRING:
  2877. if not default.is_constant and not default.nodes and \
  2878. not default.name.isupper():
  2879. # 'default foo' on a string symbol could be either a symbol
  2880. # reference or someone leaving out the quotes. Guess that
  2881. # the quotes were left out if 'foo' isn't all-uppercase
  2882. # (and no symbol named 'foo' exists).
  2883. self._warn("style: quotes recommended around "
  2884. "default value for string symbol "
  2885. + _name_and_loc(sym))
  2886. elif not num_ok(default, sym.orig_type): # INT/HEX
  2887. self._warn("the {0} symbol {1} has a non-{0} default {2}"
  2888. .format(TYPE_TO_STR[sym.orig_type],
  2889. _name_and_loc(sym),
  2890. _name_and_loc(default)))
  2891. if sym.selects or sym.implies:
  2892. self._warn("the {} symbol {} has selects or implies"
  2893. .format(TYPE_TO_STR[sym.orig_type],
  2894. _name_and_loc(sym)))
  2895. else: # UNKNOWN
  2896. self._warn("{} defined without a type"
  2897. .format(_name_and_loc(sym)))
  2898. if sym.ranges:
  2899. if sym.orig_type not in _INT_HEX:
  2900. self._warn(
  2901. "the {} symbol {} has ranges, but is not int or hex"
  2902. .format(TYPE_TO_STR[sym.orig_type],
  2903. _name_and_loc(sym)))
  2904. else:
  2905. for low, high, _ in sym.ranges:
  2906. if not num_ok(low, sym.orig_type) or \
  2907. not num_ok(high, sym.orig_type):
  2908. self._warn("the {0} symbol {1} has a non-{0} "
  2909. "range [{2}, {3}]"
  2910. .format(TYPE_TO_STR[sym.orig_type],
  2911. _name_and_loc(sym),
  2912. _name_and_loc(low),
  2913. _name_and_loc(high)))
  2914. def _check_choice_sanity(self):
  2915. # Checks various choice properties that are handiest to check after
  2916. # parsing. Only generates errors and warnings.
  2917. def warn_select_imply(sym, expr, expr_type):
  2918. msg = "the choice symbol {} is {} by the following symbols, but " \
  2919. "select/imply has no effect on choice symbols" \
  2920. .format(_name_and_loc(sym), expr_type)
  2921. # si = select/imply
  2922. for si in split_expr(expr, OR):
  2923. msg += "\n - " + _name_and_loc(split_expr(si, AND)[0])
  2924. self._warn(msg)
  2925. for choice in self.unique_choices:
  2926. if choice.orig_type not in _BOOL_TRISTATE:
  2927. self._warn("{} defined with type {}"
  2928. .format(_name_and_loc(choice),
  2929. TYPE_TO_STR[choice.orig_type]))
  2930. for node in choice.nodes:
  2931. if node.prompt:
  2932. break
  2933. else:
  2934. self._warn(_name_and_loc(choice) + " defined without a prompt")
  2935. for default, _ in choice.defaults:
  2936. if default.__class__ is not Symbol:
  2937. raise KconfigError(
  2938. "{} has a malformed default {}"
  2939. .format(_name_and_loc(choice), expr_str(default)))
  2940. if default.choice is not choice:
  2941. self._warn("the default selection {} of {} is not "
  2942. "contained in the choice"
  2943. .format(_name_and_loc(default),
  2944. _name_and_loc(choice)))
  2945. for sym in choice.syms:
  2946. if sym.defaults:
  2947. self._warn("default on the choice symbol {} will have "
  2948. "no effect, as defaults do not affect choice "
  2949. "symbols".format(_name_and_loc(sym)))
  2950. if sym.rev_dep is not sym.kconfig.n:
  2951. warn_select_imply(sym, sym.rev_dep, "selected")
  2952. if sym.weak_rev_dep is not sym.kconfig.n:
  2953. warn_select_imply(sym, sym.weak_rev_dep, "implied")
  2954. for node in sym.nodes:
  2955. if node.parent.item is choice:
  2956. if not node.prompt:
  2957. self._warn("the choice symbol {} has no prompt"
  2958. .format(_name_and_loc(sym)))
  2959. elif node.prompt:
  2960. self._warn("the choice symbol {} is defined with a "
  2961. "prompt outside the choice"
  2962. .format(_name_and_loc(sym)))
  2963. def _parse_error(self, msg):
  2964. raise KconfigError("{}couldn't parse '{}': {}".format(
  2965. "" if self.filename is None else
  2966. "{}:{}: ".format(self.filename, self.linenr),
  2967. self._line.strip(), msg))
  2968. def _trailing_tokens_error(self):
  2969. self._parse_error("extra tokens at end of line")
  2970. def _open(self, filename, mode):
  2971. # open() wrapper:
  2972. #
  2973. # - Enable universal newlines mode on Python 2 to ease
  2974. # interoperability between Linux and Windows. It's already the
  2975. # default on Python 3.
  2976. #
  2977. # The "U" flag would currently work for both Python 2 and 3, but it's
  2978. # deprecated on Python 3, so play it future-safe.
  2979. #
  2980. # io.open() defaults to universal newlines on Python 2 (and is an
  2981. # alias for open() on Python 3), but it returns 'unicode' strings and
  2982. # slows things down:
  2983. #
  2984. # Parsing x86 Kconfigs on Python 2
  2985. #
  2986. # with open(..., "rU"):
  2987. #
  2988. # real 0m0.930s
  2989. # user 0m0.905s
  2990. # sys 0m0.025s
  2991. #
  2992. # with io.open():
  2993. #
  2994. # real 0m1.069s
  2995. # user 0m1.040s
  2996. # sys 0m0.029s
  2997. #
  2998. # There's no appreciable performance difference between "r" and
  2999. # "rU" for parsing performance on Python 2.
  3000. #
  3001. # - For Python 3, force the encoding. Forcing the encoding on Python 2
  3002. # turns strings into Unicode strings, which gets messy. Python 2
  3003. # doesn't decode regular strings anyway.
  3004. return open(filename, "rU" if mode == "r" else mode) if _IS_PY2 else \
  3005. open(filename, mode, encoding=self._encoding)
  3006. def _check_undef_syms(self):
  3007. # Prints warnings for all references to undefined symbols within the
  3008. # Kconfig files
  3009. def is_num(s):
  3010. # Returns True if the string 's' looks like a number.
  3011. #
  3012. # Internally, all operands in Kconfig are symbols, only undefined symbols
  3013. # (which numbers usually are) get their name as their value.
  3014. #
  3015. # Only hex numbers that start with 0x/0X are classified as numbers.
  3016. # Otherwise, symbols whose names happen to contain only the letters A-F
  3017. # would trigger false positives.
  3018. try:
  3019. int(s)
  3020. except ValueError:
  3021. if not s.startswith(("0x", "0X")):
  3022. return False
  3023. try:
  3024. int(s, 16)
  3025. except ValueError:
  3026. return False
  3027. return True
  3028. for sym in (self.syms.viewvalues if _IS_PY2 else self.syms.values)():
  3029. # - sym.nodes empty means the symbol is undefined (has no
  3030. # definition locations)
  3031. #
  3032. # - Due to Kconfig internals, numbers show up as undefined Kconfig
  3033. # symbols, but shouldn't be flagged
  3034. #
  3035. # - The MODULES symbol always exists
  3036. if not sym.nodes and not is_num(sym.name) and \
  3037. sym.name != "MODULES":
  3038. msg = "undefined symbol {}:".format(sym.name)
  3039. for node in self.node_iter():
  3040. if sym in node.referenced:
  3041. msg += "\n\n- Referenced at {}:{}:\n\n{}" \
  3042. .format(node.filename, node.linenr, node)
  3043. self._warn(msg)
  3044. def _warn(self, msg, filename=None, linenr=None):
  3045. # For printing general warnings
  3046. if not self.warn:
  3047. return
  3048. msg = "warning: " + msg
  3049. if filename is not None:
  3050. msg = "{}:{}: {}".format(filename, linenr, msg)
  3051. self.warnings.append(msg)
  3052. if self.warn_to_stderr:
  3053. sys.stderr.write(msg + "\n")
  3054. class Symbol(object):
  3055. """
  3056. Represents a configuration symbol:
  3057. (menu)config FOO
  3058. ...
  3059. The following attributes are available. They should be viewed as read-only,
  3060. and some are implemented through @property magic (but are still efficient
  3061. to access due to internal caching).
  3062. Note: Prompts, help texts, and locations are stored in the Symbol's
  3063. MenuNode(s) rather than in the Symbol itself. Check the MenuNode class and
  3064. the Symbol.nodes attribute. This organization matches the C tools.
  3065. name:
  3066. The name of the symbol, e.g. "FOO" for 'config FOO'.
  3067. type:
  3068. The type of the symbol. One of BOOL, TRISTATE, STRING, INT, HEX, UNKNOWN.
  3069. UNKNOWN is for undefined symbols, (non-special) constant symbols, and
  3070. symbols defined without a type.
  3071. When running without modules (MODULES having the value n), TRISTATE
  3072. symbols magically change type to BOOL. This also happens for symbols
  3073. within choices in "y" mode. This matches the C tools, and makes sense for
  3074. menuconfig-like functionality.
  3075. orig_type:
  3076. The type as given in the Kconfig file, without any magic applied. Used
  3077. when printing the symbol.
  3078. str_value:
  3079. The value of the symbol as a string. Gives the value for string/int/hex
  3080. symbols. For bool/tristate symbols, gives "n", "m", or "y".
  3081. This is the symbol value that's used in relational expressions
  3082. (A = B, A != B, etc.)
  3083. Gotcha: For int/hex symbols, the exact format of the value must often be
  3084. preserved (e.g., when writing a .config file), hence why you can't get it
  3085. directly as an int. Do int(int_sym.str_value) or
  3086. int(hex_sym.str_value, 16) to get the integer value.
  3087. tri_value:
  3088. The tristate value of the symbol as an integer. One of 0, 1, 2,
  3089. representing n, m, y. Always 0 (n) for non-bool/tristate symbols.
  3090. This is the symbol value that's used outside of relation expressions
  3091. (A, !A, A && B, A || B).
  3092. assignable:
  3093. A tuple containing the tristate user values that can currently be
  3094. assigned to the symbol (that would be respected), ordered from lowest (0,
  3095. representing n) to highest (2, representing y). This corresponds to the
  3096. selections available in the menuconfig interface. The set of assignable
  3097. values is calculated from the symbol's visibility and selects/implies.
  3098. Returns the empty set for non-bool/tristate symbols and for symbols with
  3099. visibility n. The other possible values are (0, 2), (0, 1, 2), (1, 2),
  3100. (1,), and (2,). A (1,) or (2,) result means the symbol is visible but
  3101. "locked" to m or y through a select, perhaps in combination with the
  3102. visibility. menuconfig represents this as -M- and -*-, respectively.
  3103. For string/hex/int symbols, check if Symbol.visibility is non-0 (non-n)
  3104. instead to determine if the value can be changed.
  3105. Some handy 'assignable' idioms:
  3106. # Is 'sym' an assignable (visible) bool/tristate symbol?
  3107. if sym.assignable:
  3108. # What's the highest value it can be assigned? [-1] in Python
  3109. # gives the last element.
  3110. sym_high = sym.assignable[-1]
  3111. # The lowest?
  3112. sym_low = sym.assignable[0]
  3113. # Can the symbol be set to at least m?
  3114. if sym.assignable[-1] >= 1:
  3115. ...
  3116. # Can the symbol be set to m?
  3117. if 1 in sym.assignable:
  3118. ...
  3119. visibility:
  3120. The visibility of the symbol. One of 0, 1, 2, representing n, m, y. See
  3121. the module documentation for an overview of symbol values and visibility.
  3122. user_value:
  3123. The user value of the symbol. None if no user value has been assigned
  3124. (via Kconfig.load_config() or Symbol.set_value()).
  3125. Holds 0, 1, or 2 for bool/tristate symbols, and a string for the other
  3126. symbol types.
  3127. WARNING: Do not assign directly to this. It will break things. Use
  3128. Symbol.set_value().
  3129. config_string:
  3130. The .config assignment string that would get written out for the symbol
  3131. by Kconfig.write_config(). Returns the empty string if no .config
  3132. assignment would get written out.
  3133. In general, visible symbols, symbols with (active) defaults, and selected
  3134. symbols get written out. This includes all non-n-valued bool/tristate
  3135. symbols, and all visible string/int/hex symbols.
  3136. Symbols with the (no longer needed) 'option env=...' option generate no
  3137. configuration output, and neither does the special
  3138. 'option defconfig_list' symbol.
  3139. Tip: This field is useful when generating custom configuration output,
  3140. even for non-.config-like formats. To write just the symbols that would
  3141. get written out to .config files, do this:
  3142. if sym.config_string:
  3143. *Write symbol, e.g. by looking sym.str_value*
  3144. This is a superset of the symbols written out by write_autoconf().
  3145. That function skips all n-valued symbols.
  3146. There usually won't be any great harm in just writing all symbols either,
  3147. though you might get some special symbols and possibly some "redundant"
  3148. n-valued symbol entries in there.
  3149. nodes:
  3150. A list of MenuNodes for this symbol. Will contain a single MenuNode for
  3151. most symbols. Undefined and constant symbols have an empty nodes list.
  3152. Symbols defined in multiple locations get one node for each location.
  3153. choice:
  3154. Holds the parent Choice for choice symbols, and None for non-choice
  3155. symbols. Doubles as a flag for whether a symbol is a choice symbol.
  3156. defaults:
  3157. List of (default, cond) tuples for the symbol's 'default' properties. For
  3158. example, 'default A && B if C || D' is represented as
  3159. ((AND, A, B), (OR, C, D)). If no condition was given, 'cond' is
  3160. self.kconfig.y.
  3161. Note that 'depends on' and parent dependencies are propagated to
  3162. 'default' conditions.
  3163. selects:
  3164. List of (symbol, cond) tuples for the symbol's 'select' properties. For
  3165. example, 'select A if B && C' is represented as (A, (AND, B, C)). If no
  3166. condition was given, 'cond' is self.kconfig.y.
  3167. Note that 'depends on' and parent dependencies are propagated to 'select'
  3168. conditions.
  3169. implies:
  3170. Like 'selects', for imply.
  3171. ranges:
  3172. List of (low, high, cond) tuples for the symbol's 'range' properties. For
  3173. example, 'range 1 2 if A' is represented as (1, 2, A). If there is no
  3174. condition, 'cond' is self.kconfig.y.
  3175. Note that 'depends on' and parent dependencies are propagated to 'range'
  3176. conditions.
  3177. Gotcha: 1 and 2 above will be represented as (undefined) Symbols rather
  3178. than plain integers. Undefined symbols get their name as their string
  3179. value, so this works out. The C tools work the same way.
  3180. orig_defaults:
  3181. orig_selects:
  3182. orig_implies:
  3183. orig_ranges:
  3184. See the corresponding attributes on the MenuNode class.
  3185. rev_dep:
  3186. Reverse dependency expression from other symbols selecting this symbol.
  3187. Multiple selections get ORed together. A condition on a select is ANDed
  3188. with the selecting symbol.
  3189. For example, if A has 'select FOO' and B has 'select FOO if C', then
  3190. FOO's rev_dep will be (OR, A, (AND, B, C)).
  3191. weak_rev_dep:
  3192. Like rev_dep, for imply.
  3193. direct_dep:
  3194. The direct ('depends on') dependencies for the symbol, or self.kconfig.y
  3195. if there are no direct dependencies.
  3196. This attribute includes any dependencies from surrounding menus and ifs.
  3197. Those get propagated to the direct dependencies, and the resulting direct
  3198. dependencies in turn get propagated to the conditions of all properties.
  3199. If the symbol is defined in multiple locations, the dependencies from the
  3200. different locations get ORed together.
  3201. referenced:
  3202. A set() with all symbols and choices referenced in the properties and
  3203. property conditions of the symbol.
  3204. Also includes dependencies from surrounding menus and ifs, because those
  3205. get propagated to the symbol (see the 'Intro to symbol values' section in
  3206. the module docstring).
  3207. Choices appear in the dependencies of choice symbols.
  3208. For the following definitions, only B and not C appears in A's
  3209. 'referenced'. To get transitive references, you'll have to recursively
  3210. expand 'references' until no new items appear.
  3211. config A
  3212. bool
  3213. depends on B
  3214. config B
  3215. bool
  3216. depends on C
  3217. config C
  3218. bool
  3219. See the Symbol.direct_dep attribute if you're only interested in the
  3220. direct dependencies of the symbol (its 'depends on'). You can extract the
  3221. symbols in it with the global expr_items() function.
  3222. env_var:
  3223. If the Symbol has an 'option env="FOO"' option, this contains the name
  3224. ("FOO") of the environment variable. None for symbols without no
  3225. 'option env'.
  3226. 'option env="FOO"' acts like a 'default' property whose value is the
  3227. value of $FOO.
  3228. Symbols with 'option env' are never written out to .config files, even if
  3229. they are visible. env_var corresponds to a flag called SYMBOL_AUTO in the
  3230. C implementation.
  3231. is_allnoconfig_y:
  3232. True if the symbol has 'option allnoconfig_y' set on it. This has no
  3233. effect internally (except when printing symbols), but can be checked by
  3234. scripts.
  3235. is_constant:
  3236. True if the symbol is a constant (quoted) symbol.
  3237. kconfig:
  3238. The Kconfig instance this symbol is from.
  3239. """
  3240. __slots__ = (
  3241. "_cached_assignable",
  3242. "_cached_str_val",
  3243. "_cached_tri_val",
  3244. "_cached_vis",
  3245. "_dependents",
  3246. "_old_val",
  3247. "_visited",
  3248. "_was_set",
  3249. "_write_to_conf",
  3250. "choice",
  3251. "defaults",
  3252. "direct_dep",
  3253. "env_var",
  3254. "implies",
  3255. "is_allnoconfig_y",
  3256. "is_constant",
  3257. "kconfig",
  3258. "name",
  3259. "nodes",
  3260. "orig_type",
  3261. "ranges",
  3262. "rev_dep",
  3263. "selects",
  3264. "user_value",
  3265. "weak_rev_dep",
  3266. )
  3267. #
  3268. # Public interface
  3269. #
  3270. @property
  3271. def type(self):
  3272. """
  3273. See the class documentation.
  3274. """
  3275. if self.orig_type is TRISTATE and \
  3276. (self.choice and self.choice.tri_value == 2 or
  3277. not self.kconfig.modules.tri_value):
  3278. return BOOL
  3279. return self.orig_type
  3280. @property
  3281. def str_value(self):
  3282. """
  3283. See the class documentation.
  3284. """
  3285. if self._cached_str_val is not None:
  3286. return self._cached_str_val
  3287. if self.orig_type in _BOOL_TRISTATE:
  3288. # Also calculates the visibility, so invalidation safe
  3289. self._cached_str_val = TRI_TO_STR[self.tri_value]
  3290. return self._cached_str_val
  3291. # As a quirk of Kconfig, undefined symbols get their name as their
  3292. # string value. This is why things like "FOO = bar" work for seeing if
  3293. # FOO has the value "bar".
  3294. if not self.orig_type: # UNKNOWN
  3295. self._cached_str_val = self.name
  3296. return self.name
  3297. val = ""
  3298. # Warning: See Symbol._rec_invalidate(), and note that this is a hidden
  3299. # function call (property magic)
  3300. vis = self.visibility
  3301. self._write_to_conf = (vis != 0)
  3302. if self.orig_type in _INT_HEX:
  3303. # The C implementation checks the user value against the range in a
  3304. # separate code path (post-processing after loading a .config).
  3305. # Checking all values here instead makes more sense for us. It
  3306. # requires that we check for a range first.
  3307. base = _TYPE_TO_BASE[self.orig_type]
  3308. # Check if a range is in effect
  3309. for low_expr, high_expr, cond in self.ranges:
  3310. if expr_value(cond):
  3311. has_active_range = True
  3312. # The zeros are from the C implementation running strtoll()
  3313. # on empty strings
  3314. low = int(low_expr.str_value, base) if \
  3315. _is_base_n(low_expr.str_value, base) else 0
  3316. high = int(high_expr.str_value, base) if \
  3317. _is_base_n(high_expr.str_value, base) else 0
  3318. break
  3319. else:
  3320. has_active_range = False
  3321. # Defaults are used if the symbol is invisible, lacks a user value,
  3322. # or has an out-of-range user value
  3323. use_defaults = True
  3324. if vis and self.user_value:
  3325. user_val = int(self.user_value, base)
  3326. if has_active_range and not low <= user_val <= high:
  3327. num2str = str if base == 10 else hex
  3328. self.kconfig._warn(
  3329. "user value {} on the {} symbol {} ignored due to "
  3330. "being outside the active range ([{}, {}]) -- falling "
  3331. "back on defaults"
  3332. .format(num2str(user_val), TYPE_TO_STR[self.orig_type],
  3333. _name_and_loc(self),
  3334. num2str(low), num2str(high)))
  3335. else:
  3336. # If the user value is well-formed and satisfies range
  3337. # contraints, it is stored in exactly the same form as
  3338. # specified in the assignment (with or without "0x", etc.)
  3339. val = self.user_value
  3340. use_defaults = False
  3341. if use_defaults:
  3342. # No user value or invalid user value. Look at defaults.
  3343. # Used to implement the warning below
  3344. has_default = False
  3345. for sym, cond in self.defaults:
  3346. if expr_value(cond):
  3347. has_default = self._write_to_conf = True
  3348. val = sym.str_value
  3349. if _is_base_n(val, base):
  3350. val_num = int(val, base)
  3351. else:
  3352. val_num = 0 # strtoll() on empty string
  3353. break
  3354. else:
  3355. val_num = 0 # strtoll() on empty string
  3356. # This clamping procedure runs even if there's no default
  3357. if has_active_range:
  3358. clamp = None
  3359. if val_num < low:
  3360. clamp = low
  3361. elif val_num > high:
  3362. clamp = high
  3363. if clamp is not None:
  3364. # The value is rewritten to a standard form if it is
  3365. # clamped
  3366. val = str(clamp) \
  3367. if self.orig_type is INT else \
  3368. hex(clamp)
  3369. if has_default:
  3370. num2str = str if base == 10 else hex
  3371. self.kconfig._warn(
  3372. "default value {} on {} clamped to {} due to "
  3373. "being outside the active range ([{}, {}])"
  3374. .format(val_num, _name_and_loc(self),
  3375. num2str(clamp), num2str(low),
  3376. num2str(high)))
  3377. elif self.orig_type is STRING:
  3378. if vis and self.user_value is not None:
  3379. # If the symbol is visible and has a user value, use that
  3380. val = self.user_value
  3381. else:
  3382. # Otherwise, look at defaults
  3383. for sym, cond in self.defaults:
  3384. if expr_value(cond):
  3385. val = sym.str_value
  3386. self._write_to_conf = True
  3387. break
  3388. # env_var corresponds to SYMBOL_AUTO in the C implementation, and is
  3389. # also set on the defconfig_list symbol there. Test for the
  3390. # defconfig_list symbol explicitly instead here, to avoid a nonsensical
  3391. # env_var setting and the defconfig_list symbol being printed
  3392. # incorrectly. This code is pretty cold anyway.
  3393. if self.env_var is not None or self is self.kconfig.defconfig_list:
  3394. self._write_to_conf = False
  3395. self._cached_str_val = val
  3396. return val
  3397. @property
  3398. def tri_value(self):
  3399. """
  3400. See the class documentation.
  3401. """
  3402. if self._cached_tri_val is not None:
  3403. return self._cached_tri_val
  3404. if self.orig_type not in _BOOL_TRISTATE:
  3405. if self.orig_type: # != UNKNOWN
  3406. # Would take some work to give the location here
  3407. self.kconfig._warn(
  3408. "The {} symbol {} is being evaluated in a logical context "
  3409. "somewhere. It will always evaluate to n."
  3410. .format(TYPE_TO_STR[self.orig_type], _name_and_loc(self)))
  3411. self._cached_tri_val = 0
  3412. return 0
  3413. # Warning: See Symbol._rec_invalidate(), and note that this is a hidden
  3414. # function call (property magic)
  3415. vis = self.visibility
  3416. self._write_to_conf = (vis != 0)
  3417. val = 0
  3418. if not self.choice:
  3419. # Non-choice symbol
  3420. if vis and self.user_value is not None:
  3421. # If the symbol is visible and has a user value, use that
  3422. val = min(self.user_value, vis)
  3423. else:
  3424. # Otherwise, look at defaults and weak reverse dependencies
  3425. # (implies)
  3426. for default, cond in self.defaults:
  3427. dep_val = expr_value(cond)
  3428. if dep_val:
  3429. val = min(expr_value(default), dep_val)
  3430. if val:
  3431. self._write_to_conf = True
  3432. break
  3433. # Weak reverse dependencies are only considered if our
  3434. # direct dependencies are met
  3435. dep_val = expr_value(self.weak_rev_dep)
  3436. if dep_val and expr_value(self.direct_dep):
  3437. val = max(dep_val, val)
  3438. self._write_to_conf = True
  3439. # Reverse (select-related) dependencies take precedence
  3440. dep_val = expr_value(self.rev_dep)
  3441. if dep_val:
  3442. if expr_value(self.direct_dep) < dep_val:
  3443. self._warn_select_unsatisfied_deps()
  3444. val = max(dep_val, val)
  3445. self._write_to_conf = True
  3446. # m is promoted to y for (1) bool symbols and (2) symbols with a
  3447. # weak_rev_dep (from imply) of y
  3448. if val == 1 and \
  3449. (self.type is BOOL or expr_value(self.weak_rev_dep) == 2):
  3450. val = 2
  3451. elif vis == 2:
  3452. # Visible choice symbol in y-mode choice. The choice mode limits
  3453. # the visibility of choice symbols, so it's sufficient to just
  3454. # check the visibility of the choice symbols themselves.
  3455. val = 2 if self.choice.selection is self else 0
  3456. elif vis and self.user_value:
  3457. # Visible choice symbol in m-mode choice, with set non-0 user value
  3458. val = 1
  3459. self._cached_tri_val = val
  3460. return val
  3461. @property
  3462. def assignable(self):
  3463. """
  3464. See the class documentation.
  3465. """
  3466. if self._cached_assignable is None:
  3467. self._cached_assignable = self._assignable()
  3468. return self._cached_assignable
  3469. @property
  3470. def visibility(self):
  3471. """
  3472. See the class documentation.
  3473. """
  3474. if self._cached_vis is None:
  3475. self._cached_vis = _visibility(self)
  3476. return self._cached_vis
  3477. @property
  3478. def config_string(self):
  3479. """
  3480. See the class documentation.
  3481. """
  3482. # _write_to_conf is determined when the value is calculated. This is a
  3483. # hidden function call due to property magic.
  3484. val = self.str_value
  3485. if not self._write_to_conf:
  3486. return ""
  3487. if self.orig_type in _BOOL_TRISTATE:
  3488. return "{}{}={}\n" \
  3489. .format(self.kconfig.config_prefix, self.name, val) \
  3490. if val != "n" else \
  3491. "# {}{} is not set\n" \
  3492. .format(self.kconfig.config_prefix, self.name)
  3493. if self.orig_type in _INT_HEX:
  3494. return "{}{}={}\n" \
  3495. .format(self.kconfig.config_prefix, self.name, val)
  3496. # sym.orig_type is STRING
  3497. return '{}{}="{}"\n' \
  3498. .format(self.kconfig.config_prefix, self.name, escape(val))
  3499. def set_value(self, value):
  3500. """
  3501. Sets the user value of the symbol.
  3502. Equal in effect to assigning the value to the symbol within a .config
  3503. file. For bool and tristate symbols, use the 'assignable' attribute to
  3504. check which values can currently be assigned. Setting values outside
  3505. 'assignable' will cause Symbol.user_value to differ from
  3506. Symbol.str/tri_value (be truncated down or up).
  3507. Setting a choice symbol to 2 (y) sets Choice.user_selection to the
  3508. choice symbol in addition to setting Symbol.user_value.
  3509. Choice.user_selection is considered when the choice is in y mode (the
  3510. "normal" mode).
  3511. Other symbols that depend (possibly indirectly) on this symbol are
  3512. automatically recalculated to reflect the assigned value.
  3513. value:
  3514. The user value to give to the symbol. For bool and tristate symbols,
  3515. n/m/y can be specified either as 0/1/2 (the usual format for tristate
  3516. values in Kconfiglib) or as one of the strings "n"/"m"/"y". For other
  3517. symbol types, pass a string.
  3518. Note that the value for an int/hex symbol is passed as a string, e.g.
  3519. "123" or "0x0123". The format of this string is preserved in the
  3520. output.
  3521. Values that are invalid for the type (such as "foo" or 1 (m) for a
  3522. BOOL or "0x123" for an INT) are ignored and won't be stored in
  3523. Symbol.user_value. Kconfiglib will print a warning by default for
  3524. invalid assignments, and set_value() will return False.
  3525. Returns True if the value is valid for the type of the symbol, and
  3526. False otherwise. This only looks at the form of the value. For BOOL and
  3527. TRISTATE symbols, check the Symbol.assignable attribute to see what
  3528. values are currently in range and would actually be reflected in the
  3529. value of the symbol. For other symbol types, check whether the
  3530. visibility is non-n.
  3531. """
  3532. if self.orig_type in _BOOL_TRISTATE and value in STR_TO_TRI:
  3533. value = STR_TO_TRI[value]
  3534. # If the new user value matches the old, nothing changes, and we can
  3535. # avoid invalidating cached values.
  3536. #
  3537. # This optimization is skipped for choice symbols: Setting a choice
  3538. # symbol's user value to y might change the state of the choice, so it
  3539. # wouldn't be safe (symbol user values always match the values set in a
  3540. # .config file or via set_value(), and are never implicitly updated).
  3541. if value == self.user_value and not self.choice:
  3542. self._was_set = True
  3543. return True
  3544. # Check if the value is valid for our type
  3545. if not (self.orig_type is BOOL and value in (2, 0) or
  3546. self.orig_type is TRISTATE and value in TRI_TO_STR or
  3547. value.__class__ is str and
  3548. (self.orig_type is STRING or
  3549. self.orig_type is INT and _is_base_n(value, 10) or
  3550. self.orig_type is HEX and _is_base_n(value, 16)
  3551. and int(value, 16) >= 0)):
  3552. # Display tristate values as n, m, y in the warning
  3553. self.kconfig._warn(
  3554. "the value {} is invalid for {}, which has type {} -- "
  3555. "assignment ignored"
  3556. .format(TRI_TO_STR[value] if value in TRI_TO_STR else
  3557. "'{}'".format(value),
  3558. _name_and_loc(self), TYPE_TO_STR[self.orig_type]))
  3559. return False
  3560. self.user_value = value
  3561. self._was_set = True
  3562. if self.choice and value == 2:
  3563. # Setting a choice symbol to y makes it the user selection of the
  3564. # choice. Like for symbol user values, the user selection is not
  3565. # guaranteed to match the actual selection of the choice, as
  3566. # dependencies come into play.
  3567. self.choice.user_selection = self
  3568. self.choice._was_set = True
  3569. self.choice._rec_invalidate()
  3570. else:
  3571. self._rec_invalidate_if_has_prompt()
  3572. return True
  3573. def unset_value(self):
  3574. """
  3575. Removes any user value from the symbol, as if the symbol had never
  3576. gotten a user value via Kconfig.load_config() or Symbol.set_value().
  3577. """
  3578. if self.user_value is not None:
  3579. self.user_value = None
  3580. self._rec_invalidate_if_has_prompt()
  3581. @property
  3582. def referenced(self):
  3583. """
  3584. See the class documentation.
  3585. """
  3586. return {item for node in self.nodes for item in node.referenced}
  3587. @property
  3588. def orig_defaults(self):
  3589. """
  3590. See the class documentation.
  3591. """
  3592. return [d for node in self.nodes for d in node.orig_defaults]
  3593. @property
  3594. def orig_selects(self):
  3595. """
  3596. See the class documentation.
  3597. """
  3598. return [s for node in self.nodes for s in node.orig_selects]
  3599. @property
  3600. def orig_implies(self):
  3601. """
  3602. See the class documentation.
  3603. """
  3604. return [i for node in self.nodes for i in node.orig_implies]
  3605. @property
  3606. def orig_ranges(self):
  3607. """
  3608. See the class documentation.
  3609. """
  3610. return [r for node in self.nodes for r in node.orig_ranges]
  3611. def __repr__(self):
  3612. """
  3613. Returns a string with information about the symbol (including its name,
  3614. value, visibility, and location(s)) when it is evaluated on e.g. the
  3615. interactive Python prompt.
  3616. """
  3617. fields = ["symbol " + self.name, TYPE_TO_STR[self.type]]
  3618. add = fields.append
  3619. for node in self.nodes:
  3620. if node.prompt:
  3621. add('"{}"'.format(node.prompt[0]))
  3622. # Only add quotes for non-bool/tristate symbols
  3623. add("value " + (self.str_value if self.orig_type in _BOOL_TRISTATE
  3624. else '"{}"'.format(self.str_value)))
  3625. if not self.is_constant:
  3626. # These aren't helpful to show for constant symbols
  3627. if self.user_value is not None:
  3628. # Only add quotes for non-bool/tristate symbols
  3629. add("user value " + (TRI_TO_STR[self.user_value]
  3630. if self.orig_type in _BOOL_TRISTATE
  3631. else '"{}"'.format(self.user_value)))
  3632. add("visibility " + TRI_TO_STR[self.visibility])
  3633. if self.choice:
  3634. add("choice symbol")
  3635. if self.is_allnoconfig_y:
  3636. add("allnoconfig_y")
  3637. if self is self.kconfig.defconfig_list:
  3638. add("is the defconfig_list symbol")
  3639. if self.env_var is not None:
  3640. add("from environment variable " + self.env_var)
  3641. if self is self.kconfig.modules:
  3642. add("is the modules symbol")
  3643. add("direct deps " + TRI_TO_STR[expr_value(self.direct_dep)])
  3644. if self.nodes:
  3645. for node in self.nodes:
  3646. add("{}:{}".format(node.filename, node.linenr))
  3647. else:
  3648. add("constant" if self.is_constant else "undefined")
  3649. return "<{}>".format(", ".join(fields))
  3650. def __str__(self):
  3651. """
  3652. Returns a string representation of the symbol when it is printed.
  3653. Matches the Kconfig format, with any parent dependencies propagated to
  3654. the 'depends on' condition.
  3655. The string is constructed by joining the strings returned by
  3656. MenuNode.__str__() for each of the symbol's menu nodes, so symbols
  3657. defined in multiple locations will return a string with all
  3658. definitions.
  3659. The returned string does not end in a newline. An empty string is
  3660. returned for undefined and constant symbols.
  3661. """
  3662. return self.custom_str(standard_sc_expr_str)
  3663. def custom_str(self, sc_expr_str_fn):
  3664. """
  3665. Works like Symbol.__str__(), but allows a custom format to be used for
  3666. all symbol/choice references. See expr_str().
  3667. """
  3668. return "\n\n".join(node.custom_str(sc_expr_str_fn)
  3669. for node in self.nodes)
  3670. #
  3671. # Private methods
  3672. #
  3673. def __init__(self):
  3674. """
  3675. Symbol constructor -- not intended to be called directly by Kconfiglib
  3676. clients.
  3677. """
  3678. # These attributes are always set on the instance from outside and
  3679. # don't need defaults:
  3680. # kconfig
  3681. # direct_dep
  3682. # is_constant
  3683. # name
  3684. # rev_dep
  3685. # weak_rev_dep
  3686. # - UNKNOWN == 0
  3687. # - _visited is used during tree iteration and dep. loop detection
  3688. self.orig_type = self._visited = 0
  3689. self.nodes = []
  3690. self.defaults = []
  3691. self.selects = []
  3692. self.implies = []
  3693. self.ranges = []
  3694. self.user_value = \
  3695. self.choice = \
  3696. self.env_var = \
  3697. self._cached_str_val = self._cached_tri_val = self._cached_vis = \
  3698. self._cached_assignable = None
  3699. # _write_to_conf is calculated along with the value. If True, the
  3700. # Symbol gets a .config entry.
  3701. self.is_allnoconfig_y = \
  3702. self._was_set = \
  3703. self._write_to_conf = False
  3704. # See Kconfig._build_dep()
  3705. self._dependents = set()
  3706. def _assignable(self):
  3707. # Worker function for the 'assignable' attribute
  3708. if self.orig_type not in _BOOL_TRISTATE:
  3709. return ()
  3710. # Warning: See Symbol._rec_invalidate(), and note that this is a hidden
  3711. # function call (property magic)
  3712. vis = self.visibility
  3713. if not vis:
  3714. return ()
  3715. rev_dep_val = expr_value(self.rev_dep)
  3716. if vis == 2:
  3717. if self.choice:
  3718. return (2,)
  3719. if not rev_dep_val:
  3720. if self.type is BOOL or expr_value(self.weak_rev_dep) == 2:
  3721. return (0, 2)
  3722. return (0, 1, 2)
  3723. if rev_dep_val == 2:
  3724. return (2,)
  3725. # rev_dep_val == 1
  3726. if self.type is BOOL or expr_value(self.weak_rev_dep) == 2:
  3727. return (2,)
  3728. return (1, 2)
  3729. # vis == 1
  3730. # Must be a tristate here, because bool m visibility gets promoted to y
  3731. if not rev_dep_val:
  3732. return (0, 1) if expr_value(self.weak_rev_dep) != 2 else (0, 2)
  3733. if rev_dep_val == 2:
  3734. return (2,)
  3735. # vis == rev_dep_val == 1
  3736. return (1,)
  3737. def _invalidate(self):
  3738. # Marks the symbol as needing to be recalculated
  3739. self._cached_str_val = self._cached_tri_val = self._cached_vis = \
  3740. self._cached_assignable = None
  3741. def _rec_invalidate(self):
  3742. # Invalidates the symbol and all items that (possibly) depend on it
  3743. if self is self.kconfig.modules:
  3744. # Invalidating MODULES has wide-ranging effects
  3745. self.kconfig._invalidate_all()
  3746. else:
  3747. self._invalidate()
  3748. for item in self._dependents:
  3749. # _cached_vis doubles as a flag that tells us whether 'item'
  3750. # has cached values, because it's calculated as a side effect
  3751. # of calculating all other (non-constant) cached values.
  3752. #
  3753. # If item._cached_vis is None, it means there can't be cached
  3754. # values on other items that depend on 'item', because if there
  3755. # were, some value on 'item' would have been calculated and
  3756. # item._cached_vis set as a side effect. It's therefore safe to
  3757. # stop the invalidation at symbols with _cached_vis None.
  3758. #
  3759. # This approach massively speeds up scripts that set a lot of
  3760. # values, vs simply invalidating all possibly dependent symbols
  3761. # (even when you already have a list of all the dependent
  3762. # symbols, because some symbols get huge dependency trees).
  3763. #
  3764. # This gracefully handles dependency loops too, which is nice
  3765. # for choices, where the choice depends on the choice symbols
  3766. # and vice versa.
  3767. if item._cached_vis is not None:
  3768. item._rec_invalidate()
  3769. def _rec_invalidate_if_has_prompt(self):
  3770. # Invalidates the symbol and its dependent symbols, but only if the
  3771. # symbol has a prompt. User values never have an effect on promptless
  3772. # symbols, so we skip invalidation for them as an optimization.
  3773. #
  3774. # This also prevents constant (quoted) symbols from being invalidated
  3775. # if set_value() is called on them, which would make them lose their
  3776. # value and break things.
  3777. #
  3778. # Prints a warning if the symbol has no prompt. In some contexts (e.g.
  3779. # when loading a .config files) assignments to promptless symbols are
  3780. # normal and expected, so the warning can be disabled.
  3781. for node in self.nodes:
  3782. if node.prompt:
  3783. self._rec_invalidate()
  3784. return
  3785. if self.kconfig._warn_assign_no_prompt:
  3786. self.kconfig._warn(_name_and_loc(self) + " has no prompt, meaning "
  3787. "user values have no effect on it")
  3788. def _str_default(self):
  3789. # write_min_config() helper function. Returns the value the symbol
  3790. # would get from defaults if it didn't have a user value. Uses exactly
  3791. # the same algorithm as the C implementation (though a bit cleaned up),
  3792. # for compatibility.
  3793. if self.orig_type in _BOOL_TRISTATE:
  3794. val = 0
  3795. # Defaults, selects, and implies do not affect choice symbols
  3796. if not self.choice:
  3797. for default, cond in self.defaults:
  3798. cond_val = expr_value(cond)
  3799. if cond_val:
  3800. val = min(expr_value(default), cond_val)
  3801. break
  3802. val = max(expr_value(self.rev_dep),
  3803. expr_value(self.weak_rev_dep),
  3804. val)
  3805. # Transpose mod to yes if type is bool (possibly due to modules
  3806. # being disabled)
  3807. if val == 1 and self.type is BOOL:
  3808. val = 2
  3809. return TRI_TO_STR[val]
  3810. if self.orig_type: # STRING/INT/HEX
  3811. for default, cond in self.defaults:
  3812. if expr_value(cond):
  3813. return default.str_value
  3814. return ""
  3815. def _warn_select_unsatisfied_deps(self):
  3816. # Helper for printing an informative warning when a symbol with
  3817. # unsatisfied direct dependencies (dependencies from 'depends on', ifs,
  3818. # and menus) is selected by some other symbol. Also warn if a symbol
  3819. # whose direct dependencies evaluate to m is selected to y.
  3820. msg = "{} has direct dependencies {} with value {}, but is " \
  3821. "currently being {}-selected by the following symbols:" \
  3822. .format(_name_and_loc(self), expr_str(self.direct_dep),
  3823. TRI_TO_STR[expr_value(self.direct_dep)],
  3824. TRI_TO_STR[expr_value(self.rev_dep)])
  3825. # The reverse dependencies from each select are ORed together
  3826. for select in split_expr(self.rev_dep, OR):
  3827. if expr_value(select) <= expr_value(self.direct_dep):
  3828. # Only include selects that exceed the direct dependencies
  3829. continue
  3830. # - 'select A if B' turns into A && B
  3831. # - 'select A' just turns into A
  3832. #
  3833. # In both cases, we can split on AND and pick the first operand
  3834. selecting_sym = split_expr(select, AND)[0]
  3835. msg += "\n - {}, with value {}, direct dependencies {} " \
  3836. "(value: {})" \
  3837. .format(_name_and_loc(selecting_sym),
  3838. selecting_sym.str_value,
  3839. expr_str(selecting_sym.direct_dep),
  3840. TRI_TO_STR[expr_value(selecting_sym.direct_dep)])
  3841. if select.__class__ is tuple:
  3842. msg += ", and select condition {} (value: {})" \
  3843. .format(expr_str(select[2]),
  3844. TRI_TO_STR[expr_value(select[2])])
  3845. self.kconfig._warn(msg)
  3846. class Choice(object):
  3847. """
  3848. Represents a choice statement:
  3849. choice
  3850. ...
  3851. endchoice
  3852. The following attributes are available on Choice instances. They should be
  3853. treated as read-only, and some are implemented through @property magic (but
  3854. are still efficient to access due to internal caching).
  3855. Note: Prompts, help texts, and locations are stored in the Choice's
  3856. MenuNode(s) rather than in the Choice itself. Check the MenuNode class and
  3857. the Choice.nodes attribute. This organization matches the C tools.
  3858. name:
  3859. The name of the choice, e.g. "FOO" for 'choice FOO', or None if the
  3860. Choice has no name.
  3861. type:
  3862. The type of the choice. One of BOOL, TRISTATE, UNKNOWN. UNKNOWN is for
  3863. choices defined without a type where none of the contained symbols have a
  3864. type either (otherwise the choice inherits the type of the first symbol
  3865. defined with a type).
  3866. When running without modules (CONFIG_MODULES=n), TRISTATE choices
  3867. magically change type to BOOL. This matches the C tools, and makes sense
  3868. for menuconfig-like functionality.
  3869. orig_type:
  3870. The type as given in the Kconfig file, without any magic applied. Used
  3871. when printing the choice.
  3872. tri_value:
  3873. The tristate value (mode) of the choice. A choice can be in one of three
  3874. modes:
  3875. 0 (n) - The choice is disabled and no symbols can be selected. For
  3876. visible choices, this mode is only possible for choices with
  3877. the 'optional' flag set (see kconfig-language.txt).
  3878. 1 (m) - Any number of choice symbols can be set to m, the rest will
  3879. be n.
  3880. 2 (y) - One symbol will be y, the rest n.
  3881. Only tristate choices can be in m mode. The visibility of the choice is
  3882. an upper bound on the mode, and the mode in turn is an upper bound on the
  3883. visibility of the choice symbols.
  3884. To change the mode, use Choice.set_value().
  3885. Implementation note:
  3886. The C tools internally represent choices as a type of symbol, with
  3887. special-casing in many code paths. This is why there is a lot of
  3888. similarity to Symbol. The value (mode) of a choice is really just a
  3889. normal symbol value, and an implicit reverse dependency forces its
  3890. lower bound to m for visible non-optional choices (the reverse
  3891. dependency is 'm && <visibility>').
  3892. Symbols within choices get the choice propagated as a dependency to
  3893. their properties. This turns the mode of the choice into an upper bound
  3894. on e.g. the visibility of choice symbols, and explains the gotcha
  3895. related to printing choice symbols mentioned in the module docstring.
  3896. Kconfiglib uses a separate Choice class only because it makes the code
  3897. and interface less confusing (especially in a user-facing interface).
  3898. Corresponding attributes have the same name in the Symbol and Choice
  3899. classes, for consistency and compatibility.
  3900. assignable:
  3901. See the symbol class documentation. Gives the assignable values (modes).
  3902. visibility:
  3903. See the Symbol class documentation. Acts on the value (mode).
  3904. selection:
  3905. The Symbol instance of the currently selected symbol. None if the Choice
  3906. is not in y mode or has no selected symbol (due to unsatisfied
  3907. dependencies on choice symbols).
  3908. WARNING: Do not assign directly to this. It will break things. Call
  3909. sym.set_value(2) on the choice symbol you want to select instead.
  3910. user_value:
  3911. The value (mode) selected by the user through Choice.set_value(). Either
  3912. 0, 1, or 2, or None if the user hasn't selected a mode. See
  3913. Symbol.user_value.
  3914. WARNING: Do not assign directly to this. It will break things. Use
  3915. Choice.set_value() instead.
  3916. user_selection:
  3917. The symbol selected by the user (by setting it to y). Ignored if the
  3918. choice is not in y mode, but still remembered so that the choice "snaps
  3919. back" to the user selection if the mode is changed back to y. This might
  3920. differ from 'selection' due to unsatisfied dependencies.
  3921. WARNING: Do not assign directly to this. It will break things. Call
  3922. sym.set_value(2) on the choice symbol to be selected instead.
  3923. syms:
  3924. List of symbols contained in the choice.
  3925. Obscure gotcha: If a symbol depends on the previous symbol within a
  3926. choice so that an implicit menu is created, it won't be a choice symbol,
  3927. and won't be included in 'syms'.
  3928. nodes:
  3929. A list of MenuNodes for this choice. In practice, the list will probably
  3930. always contain a single MenuNode, but it is possible to give a choice a
  3931. name and define it in multiple locations.
  3932. defaults:
  3933. List of (symbol, cond) tuples for the choice's 'defaults' properties. For
  3934. example, 'default A if B && C' is represented as (A, (AND, B, C)). If
  3935. there is no condition, 'cond' is self.kconfig.y.
  3936. Note that 'depends on' and parent dependencies are propagated to
  3937. 'default' conditions.
  3938. orig_defaults:
  3939. See the corresponding attribute on the MenuNode class.
  3940. direct_dep:
  3941. See Symbol.direct_dep.
  3942. referenced:
  3943. A set() with all symbols referenced in the properties and property
  3944. conditions of the choice.
  3945. Also includes dependencies from surrounding menus and ifs, because those
  3946. get propagated to the choice (see the 'Intro to symbol values' section in
  3947. the module docstring).
  3948. is_optional:
  3949. True if the choice has the 'optional' flag set on it and can be in
  3950. n mode.
  3951. kconfig:
  3952. The Kconfig instance this choice is from.
  3953. """
  3954. __slots__ = (
  3955. "_cached_assignable",
  3956. "_cached_selection",
  3957. "_cached_vis",
  3958. "_dependents",
  3959. "_visited",
  3960. "_was_set",
  3961. "defaults",
  3962. "direct_dep",
  3963. "is_constant",
  3964. "is_optional",
  3965. "kconfig",
  3966. "name",
  3967. "nodes",
  3968. "orig_type",
  3969. "syms",
  3970. "user_selection",
  3971. "user_value",
  3972. )
  3973. #
  3974. # Public interface
  3975. #
  3976. @property
  3977. def type(self):
  3978. """
  3979. Returns the type of the choice. See Symbol.type.
  3980. """
  3981. if self.orig_type is TRISTATE and not self.kconfig.modules.tri_value:
  3982. return BOOL
  3983. return self.orig_type
  3984. @property
  3985. def str_value(self):
  3986. """
  3987. See the class documentation.
  3988. """
  3989. return TRI_TO_STR[self.tri_value]
  3990. @property
  3991. def tri_value(self):
  3992. """
  3993. See the class documentation.
  3994. """
  3995. # This emulates a reverse dependency of 'm && visibility' for
  3996. # non-optional choices, which is how the C implementation does it
  3997. val = 0 if self.is_optional else 1
  3998. if self.user_value is not None:
  3999. val = max(val, self.user_value)
  4000. # Warning: See Symbol._rec_invalidate(), and note that this is a hidden
  4001. # function call (property magic)
  4002. val = min(val, self.visibility)
  4003. # Promote m to y for boolean choices
  4004. return 2 if val == 1 and self.type is BOOL else val
  4005. @property
  4006. def assignable(self):
  4007. """
  4008. See the class documentation.
  4009. """
  4010. if self._cached_assignable is None:
  4011. self._cached_assignable = self._assignable()
  4012. return self._cached_assignable
  4013. @property
  4014. def visibility(self):
  4015. """
  4016. See the class documentation.
  4017. """
  4018. if self._cached_vis is None:
  4019. self._cached_vis = _visibility(self)
  4020. return self._cached_vis
  4021. @property
  4022. def selection(self):
  4023. """
  4024. See the class documentation.
  4025. """
  4026. if self._cached_selection is _NO_CACHED_SELECTION:
  4027. self._cached_selection = self._selection()
  4028. return self._cached_selection
  4029. def set_value(self, value):
  4030. """
  4031. Sets the user value (mode) of the choice. Like for Symbol.set_value(),
  4032. the visibility might truncate the value. Choices without the 'optional'
  4033. attribute (is_optional) can never be in n mode, but 0/"n" is still
  4034. accepted since it's not a malformed value (though it will have no
  4035. effect).
  4036. Returns True if the value is valid for the type of the choice, and
  4037. False otherwise. This only looks at the form of the value. Check the
  4038. Choice.assignable attribute to see what values are currently in range
  4039. and would actually be reflected in the mode of the choice.
  4040. """
  4041. if value in STR_TO_TRI:
  4042. value = STR_TO_TRI[value]
  4043. if value == self.user_value:
  4044. # We know the value must be valid if it was successfully set
  4045. # previously
  4046. self._was_set = True
  4047. return True
  4048. if not (self.orig_type is BOOL and value in (2, 0) or
  4049. self.orig_type is TRISTATE and value in TRI_TO_STR):
  4050. # Display tristate values as n, m, y in the warning
  4051. self.kconfig._warn(
  4052. "the value {} is invalid for {}, which has type {} -- "
  4053. "assignment ignored"
  4054. .format(TRI_TO_STR[value] if value in TRI_TO_STR else
  4055. "'{}'".format(value),
  4056. _name_and_loc(self), TYPE_TO_STR[self.orig_type]))
  4057. return False
  4058. self.user_value = value
  4059. self._was_set = True
  4060. self._rec_invalidate()
  4061. return True
  4062. def unset_value(self):
  4063. """
  4064. Resets the user value (mode) and user selection of the Choice, as if
  4065. the user had never touched the mode or any of the choice symbols.
  4066. """
  4067. if self.user_value is not None or self.user_selection:
  4068. self.user_value = self.user_selection = None
  4069. self._rec_invalidate()
  4070. @property
  4071. def referenced(self):
  4072. """
  4073. See the class documentation.
  4074. """
  4075. return {item for node in self.nodes for item in node.referenced}
  4076. @property
  4077. def orig_defaults(self):
  4078. """
  4079. See the class documentation.
  4080. """
  4081. return [d for node in self.nodes for d in node.orig_defaults]
  4082. def __repr__(self):
  4083. """
  4084. Returns a string with information about the choice when it is evaluated
  4085. on e.g. the interactive Python prompt.
  4086. """
  4087. fields = ["choice " + self.name if self.name else "choice",
  4088. TYPE_TO_STR[self.type]]
  4089. add = fields.append
  4090. for node in self.nodes:
  4091. if node.prompt:
  4092. add('"{}"'.format(node.prompt[0]))
  4093. add("mode " + self.str_value)
  4094. if self.user_value is not None:
  4095. add('user mode {}'.format(TRI_TO_STR[self.user_value]))
  4096. if self.selection:
  4097. add("{} selected".format(self.selection.name))
  4098. if self.user_selection:
  4099. user_sel_str = "{} selected by user" \
  4100. .format(self.user_selection.name)
  4101. if self.selection is not self.user_selection:
  4102. user_sel_str += " (overridden)"
  4103. add(user_sel_str)
  4104. add("visibility " + TRI_TO_STR[self.visibility])
  4105. if self.is_optional:
  4106. add("optional")
  4107. for node in self.nodes:
  4108. add("{}:{}".format(node.filename, node.linenr))
  4109. return "<{}>".format(", ".join(fields))
  4110. def __str__(self):
  4111. """
  4112. Returns a string representation of the choice when it is printed.
  4113. Matches the Kconfig format (though without the contained choice
  4114. symbols), with any parent dependencies propagated to the 'depends on'
  4115. condition.
  4116. The returned string does not end in a newline.
  4117. See Symbol.__str__() as well.
  4118. """
  4119. return self.custom_str(standard_sc_expr_str)
  4120. def custom_str(self, sc_expr_str_fn):
  4121. """
  4122. Works like Choice.__str__(), but allows a custom format to be used for
  4123. all symbol/choice references. See expr_str().
  4124. """
  4125. return "\n\n".join(node.custom_str(sc_expr_str_fn)
  4126. for node in self.nodes)
  4127. #
  4128. # Private methods
  4129. #
  4130. def __init__(self):
  4131. """
  4132. Choice constructor -- not intended to be called directly by Kconfiglib
  4133. clients.
  4134. """
  4135. # These attributes are always set on the instance from outside and
  4136. # don't need defaults:
  4137. # direct_dep
  4138. # kconfig
  4139. # - UNKNOWN == 0
  4140. # - _visited is used during dep. loop detection
  4141. self.orig_type = self._visited = 0
  4142. self.nodes = []
  4143. self.syms = []
  4144. self.defaults = []
  4145. self.name = \
  4146. self.user_value = self.user_selection = \
  4147. self._cached_vis = self._cached_assignable = None
  4148. self._cached_selection = _NO_CACHED_SELECTION
  4149. # is_constant is checked by _make_depend_on(). Just set it to avoid
  4150. # having to special-case choices.
  4151. self.is_constant = self.is_optional = False
  4152. # See Kconfig._build_dep()
  4153. self._dependents = set()
  4154. def _assignable(self):
  4155. # Worker function for the 'assignable' attribute
  4156. # Warning: See Symbol._rec_invalidate(), and note that this is a hidden
  4157. # function call (property magic)
  4158. vis = self.visibility
  4159. if not vis:
  4160. return ()
  4161. if vis == 2:
  4162. if not self.is_optional:
  4163. return (2,) if self.type is BOOL else (1, 2)
  4164. return (0, 2) if self.type is BOOL else (0, 1, 2)
  4165. # vis == 1
  4166. return (0, 1) if self.is_optional else (1,)
  4167. def _selection(self):
  4168. # Worker function for the 'selection' attribute
  4169. # Warning: See Symbol._rec_invalidate(), and note that this is a hidden
  4170. # function call (property magic)
  4171. if self.tri_value != 2:
  4172. # Not in y mode, so no selection
  4173. return None
  4174. # Use the user selection if it's visible
  4175. if self.user_selection and self.user_selection.visibility:
  4176. return self.user_selection
  4177. # Otherwise, check if we have a default
  4178. return self._selection_from_defaults()
  4179. def _selection_from_defaults(self):
  4180. # Check if we have a default
  4181. for sym, cond in self.defaults:
  4182. # The default symbol must be visible too
  4183. if expr_value(cond) and sym.visibility:
  4184. return sym
  4185. # Otherwise, pick the first visible symbol, if any
  4186. for sym in self.syms:
  4187. if sym.visibility:
  4188. return sym
  4189. # Couldn't find a selection
  4190. return None
  4191. def _invalidate(self):
  4192. self._cached_vis = self._cached_assignable = None
  4193. self._cached_selection = _NO_CACHED_SELECTION
  4194. def _rec_invalidate(self):
  4195. # See Symbol._rec_invalidate()
  4196. self._invalidate()
  4197. for item in self._dependents:
  4198. if item._cached_vis is not None:
  4199. item._rec_invalidate()
  4200. class MenuNode(object):
  4201. """
  4202. Represents a menu node in the configuration. This corresponds to an entry
  4203. in e.g. the 'make menuconfig' interface, though non-visible choices, menus,
  4204. and comments also get menu nodes. If a symbol or choice is defined in
  4205. multiple locations, it gets one menu node for each location.
  4206. The top-level menu node, corresponding to the implicit top-level menu, is
  4207. available in Kconfig.top_node.
  4208. The menu nodes for a Symbol or Choice can be found in the
  4209. Symbol/Choice.nodes attribute. Menus and comments are represented as plain
  4210. menu nodes, with their text stored in the prompt attribute (prompt[0]).
  4211. This mirrors the C implementation.
  4212. The following attributes are available on MenuNode instances. They should
  4213. be viewed as read-only.
  4214. item:
  4215. Either a Symbol, a Choice, or one of the constants MENU and COMMENT.
  4216. Menus and comments are represented as plain menu nodes. Ifs are collapsed
  4217. (matching the C implementation) and do not appear in the final menu tree.
  4218. next:
  4219. The following menu node. None if there is no following node.
  4220. list:
  4221. The first child menu node. None if there are no children.
  4222. Choices and menus naturally have children, but Symbols can also have
  4223. children because of menus created automatically from dependencies (see
  4224. kconfig-language.txt).
  4225. parent:
  4226. The parent menu node. None if there is no parent.
  4227. prompt:
  4228. A (string, cond) tuple with the prompt for the menu node and its
  4229. conditional expression (which is self.kconfig.y if there is no
  4230. condition). None if there is no prompt.
  4231. For symbols and choices, the prompt is stored in the MenuNode rather than
  4232. the Symbol or Choice instance. For menus and comments, the prompt holds
  4233. the text.
  4234. defaults:
  4235. The 'default' properties for this particular menu node. See
  4236. symbol.defaults.
  4237. When evaluating defaults, you should use Symbol/Choice.defaults instead,
  4238. as it include properties from all menu nodes (a symbol/choice can have
  4239. multiple definition locations/menu nodes). MenuNode.defaults is meant for
  4240. documentation generation.
  4241. selects:
  4242. Like MenuNode.defaults, for selects.
  4243. implies:
  4244. Like MenuNode.defaults, for implies.
  4245. ranges:
  4246. Like MenuNode.defaults, for ranges.
  4247. orig_prompt:
  4248. orig_defaults:
  4249. orig_selects:
  4250. orig_implies:
  4251. orig_ranges:
  4252. These work the like the corresponding attributes without orig_*, but omit
  4253. any dependencies propagated from 'depends on' and surrounding 'if's (the
  4254. direct dependencies, stored in MenuNode.dep).
  4255. One use for this is generating less cluttered documentation, by only
  4256. showing the direct dependencies in one place.
  4257. help:
  4258. The help text for the menu node for Symbols and Choices. None if there is
  4259. no help text. Always stored in the node rather than the Symbol or Choice.
  4260. It is possible to have a separate help text at each location if a symbol
  4261. is defined in multiple locations.
  4262. Trailing whitespace (including a final newline) is stripped from the help
  4263. text. This was not the case before Kconfiglib 10.21.0, where the format
  4264. was undocumented.
  4265. dep:
  4266. The direct ('depends on') dependencies for the menu node, or
  4267. self.kconfig.y if there are no direct dependencies.
  4268. This attribute includes any dependencies from surrounding menus and ifs.
  4269. Those get propagated to the direct dependencies, and the resulting direct
  4270. dependencies in turn get propagated to the conditions of all properties.
  4271. If a symbol or choice is defined in multiple locations, only the
  4272. properties defined at a particular location get the corresponding
  4273. MenuNode.dep dependencies propagated to them.
  4274. visibility:
  4275. The 'visible if' dependencies for the menu node (which must represent a
  4276. menu), or self.kconfig.y if there are no 'visible if' dependencies.
  4277. 'visible if' dependencies are recursively propagated to the prompts of
  4278. symbols and choices within the menu.
  4279. referenced:
  4280. A set() with all symbols and choices referenced in the properties and
  4281. property conditions of the menu node.
  4282. Also includes dependencies inherited from surrounding menus and ifs.
  4283. Choices appear in the dependencies of choice symbols.
  4284. is_menuconfig:
  4285. Set to True if the children of the menu node should be displayed in a
  4286. separate menu. This is the case for the following items:
  4287. - Menus (node.item == MENU)
  4288. - Choices
  4289. - Symbols defined with the 'menuconfig' keyword. The children come from
  4290. implicitly created submenus, and should be displayed in a separate
  4291. menu rather than being indented.
  4292. 'is_menuconfig' is just a hint on how to display the menu node. It's
  4293. ignored internally by Kconfiglib, except when printing symbols.
  4294. filename/linenr:
  4295. The location where the menu node appears. The filename is relative to
  4296. $srctree (or to the current directory if $srctree isn't set), except
  4297. absolute paths are used for paths outside $srctree.
  4298. include_path:
  4299. A tuple of (filename, linenr) tuples, giving the locations of the
  4300. 'source' statements via which the Kconfig file containing this menu node
  4301. was included. The first element is the location of the 'source' statement
  4302. in the top-level Kconfig file passed to Kconfig.__init__(), etc.
  4303. Note that the Kconfig file of the menu node itself isn't included. Check
  4304. 'filename' and 'linenr' for that.
  4305. kconfig:
  4306. The Kconfig instance the menu node is from.
  4307. """
  4308. __slots__ = (
  4309. "dep",
  4310. "filename",
  4311. "help",
  4312. "include_path",
  4313. "is_menuconfig",
  4314. "item",
  4315. "kconfig",
  4316. "linenr",
  4317. "list",
  4318. "next",
  4319. "parent",
  4320. "prompt",
  4321. "visibility",
  4322. # Properties
  4323. "defaults",
  4324. "selects",
  4325. "implies",
  4326. "ranges",
  4327. )
  4328. def __init__(self):
  4329. # Properties defined on this particular menu node. A local 'depends on'
  4330. # only applies to these, in case a symbol is defined in multiple
  4331. # locations.
  4332. self.defaults = []
  4333. self.selects = []
  4334. self.implies = []
  4335. self.ranges = []
  4336. @property
  4337. def orig_prompt(self):
  4338. """
  4339. See the class documentation.
  4340. """
  4341. if not self.prompt:
  4342. return None
  4343. return (self.prompt[0], self._strip_dep(self.prompt[1]))
  4344. @property
  4345. def orig_defaults(self):
  4346. """
  4347. See the class documentation.
  4348. """
  4349. return [(default, self._strip_dep(cond))
  4350. for default, cond in self.defaults]
  4351. @property
  4352. def orig_selects(self):
  4353. """
  4354. See the class documentation.
  4355. """
  4356. return [(select, self._strip_dep(cond))
  4357. for select, cond in self.selects]
  4358. @property
  4359. def orig_implies(self):
  4360. """
  4361. See the class documentation.
  4362. """
  4363. return [(imply, self._strip_dep(cond))
  4364. for imply, cond in self.implies]
  4365. @property
  4366. def orig_ranges(self):
  4367. """
  4368. See the class documentation.
  4369. """
  4370. return [(low, high, self._strip_dep(cond))
  4371. for low, high, cond in self.ranges]
  4372. @property
  4373. def referenced(self):
  4374. """
  4375. See the class documentation.
  4376. """
  4377. # self.dep is included to catch dependencies from a lone 'depends on'
  4378. # when there are no properties to propagate it to
  4379. res = expr_items(self.dep)
  4380. if self.prompt:
  4381. res |= expr_items(self.prompt[1])
  4382. if self.item is MENU:
  4383. res |= expr_items(self.visibility)
  4384. for value, cond in self.defaults:
  4385. res |= expr_items(value)
  4386. res |= expr_items(cond)
  4387. for value, cond in self.selects:
  4388. res.add(value)
  4389. res |= expr_items(cond)
  4390. for value, cond in self.implies:
  4391. res.add(value)
  4392. res |= expr_items(cond)
  4393. for low, high, cond in self.ranges:
  4394. res.add(low)
  4395. res.add(high)
  4396. res |= expr_items(cond)
  4397. return res
  4398. def __repr__(self):
  4399. """
  4400. Returns a string with information about the menu node when it is
  4401. evaluated on e.g. the interactive Python prompt.
  4402. """
  4403. fields = []
  4404. add = fields.append
  4405. if self.item.__class__ is Symbol:
  4406. add("menu node for symbol " + self.item.name)
  4407. elif self.item.__class__ is Choice:
  4408. s = "menu node for choice"
  4409. if self.item.name is not None:
  4410. s += " " + self.item.name
  4411. add(s)
  4412. elif self.item is MENU:
  4413. add("menu node for menu")
  4414. else: # self.item is COMMENT
  4415. add("menu node for comment")
  4416. if self.prompt:
  4417. add('prompt "{}" (visibility {})'.format(
  4418. self.prompt[0], TRI_TO_STR[expr_value(self.prompt[1])]))
  4419. if self.item.__class__ is Symbol and self.is_menuconfig:
  4420. add("is menuconfig")
  4421. add("deps " + TRI_TO_STR[expr_value(self.dep)])
  4422. if self.item is MENU:
  4423. add("'visible if' deps " + TRI_TO_STR[expr_value(self.visibility)])
  4424. if self.item.__class__ in _SYMBOL_CHOICE and self.help is not None:
  4425. add("has help")
  4426. if self.list:
  4427. add("has child")
  4428. if self.next:
  4429. add("has next")
  4430. add("{}:{}".format(self.filename, self.linenr))
  4431. return "<{}>".format(", ".join(fields))
  4432. def __str__(self):
  4433. """
  4434. Returns a string representation of the menu node. Matches the Kconfig
  4435. format, with any parent dependencies propagated to the 'depends on'
  4436. condition.
  4437. The output could (almost) be fed back into a Kconfig parser to redefine
  4438. the object associated with the menu node. See the module documentation
  4439. for a gotcha related to choice symbols.
  4440. For symbols and choices with multiple menu nodes (multiple definition
  4441. locations), properties that aren't associated with a particular menu
  4442. node are shown on all menu nodes ('option env=...', 'optional' for
  4443. choices, etc.).
  4444. The returned string does not end in a newline.
  4445. """
  4446. return self.custom_str(standard_sc_expr_str)
  4447. def custom_str(self, sc_expr_str_fn):
  4448. """
  4449. Works like MenuNode.__str__(), but allows a custom format to be used
  4450. for all symbol/choice references. See expr_str().
  4451. """
  4452. return self._menu_comment_node_str(sc_expr_str_fn) \
  4453. if self.item in _MENU_COMMENT else \
  4454. self._sym_choice_node_str(sc_expr_str_fn)
  4455. def _menu_comment_node_str(self, sc_expr_str_fn):
  4456. s = '{} "{}"'.format("menu" if self.item is MENU else "comment",
  4457. self.prompt[0])
  4458. if self.dep is not self.kconfig.y:
  4459. s += "\n\tdepends on {}".format(expr_str(self.dep, sc_expr_str_fn))
  4460. if self.item is MENU and self.visibility is not self.kconfig.y:
  4461. s += "\n\tvisible if {}".format(expr_str(self.visibility,
  4462. sc_expr_str_fn))
  4463. return s
  4464. def _sym_choice_node_str(self, sc_expr_str_fn):
  4465. def indent_add(s):
  4466. lines.append("\t" + s)
  4467. def indent_add_cond(s, cond):
  4468. if cond is not self.kconfig.y:
  4469. s += " if " + expr_str(cond, sc_expr_str_fn)
  4470. indent_add(s)
  4471. sc = self.item
  4472. if sc.__class__ is Symbol:
  4473. lines = [("menuconfig " if self.is_menuconfig else "config ")
  4474. + sc.name]
  4475. else:
  4476. lines = ["choice " + sc.name if sc.name else "choice"]
  4477. if sc.orig_type and not self.prompt: # sc.orig_type != UNKNOWN
  4478. # If there's a prompt, we'll use the '<type> "prompt"' shorthand
  4479. # instead
  4480. indent_add(TYPE_TO_STR[sc.orig_type])
  4481. if self.prompt:
  4482. if sc.orig_type:
  4483. prefix = TYPE_TO_STR[sc.orig_type]
  4484. else:
  4485. # Symbol defined without a type (which generates a warning)
  4486. prefix = "prompt"
  4487. indent_add_cond(prefix + ' "{}"'.format(escape(self.prompt[0])),
  4488. self.orig_prompt[1])
  4489. if sc.__class__ is Symbol:
  4490. if sc.is_allnoconfig_y:
  4491. indent_add("option allnoconfig_y")
  4492. if sc is sc.kconfig.defconfig_list:
  4493. indent_add("option defconfig_list")
  4494. if sc.env_var is not None:
  4495. indent_add('option env="{}"'.format(sc.env_var))
  4496. if sc is sc.kconfig.modules:
  4497. indent_add("option modules")
  4498. for low, high, cond in self.orig_ranges:
  4499. indent_add_cond(
  4500. "range {} {}".format(sc_expr_str_fn(low),
  4501. sc_expr_str_fn(high)),
  4502. cond)
  4503. for default, cond in self.orig_defaults:
  4504. indent_add_cond("default " + expr_str(default, sc_expr_str_fn),
  4505. cond)
  4506. if sc.__class__ is Choice and sc.is_optional:
  4507. indent_add("optional")
  4508. if sc.__class__ is Symbol:
  4509. for select, cond in self.orig_selects:
  4510. indent_add_cond("select " + sc_expr_str_fn(select), cond)
  4511. for imply, cond in self.orig_implies:
  4512. indent_add_cond("imply " + sc_expr_str_fn(imply), cond)
  4513. if self.dep is not sc.kconfig.y:
  4514. indent_add("depends on " + expr_str(self.dep, sc_expr_str_fn))
  4515. if self.help is not None:
  4516. indent_add("help")
  4517. for line in self.help.splitlines():
  4518. indent_add(" " + line)
  4519. return "\n".join(lines)
  4520. def _strip_dep(self, expr):
  4521. # Helper function for removing MenuNode.dep from 'expr'. Uses two
  4522. # pieces of internal knowledge: (1) Expressions are reused rather than
  4523. # copied, and (2) the direct dependencies always appear at the end.
  4524. # ... if dep -> ... if y
  4525. if self.dep is expr:
  4526. return self.kconfig.y
  4527. # (AND, X, dep) -> X
  4528. if expr.__class__ is tuple and expr[0] is AND and expr[2] is self.dep:
  4529. return expr[1]
  4530. return expr
  4531. class Variable(object):
  4532. """
  4533. Represents a preprocessor variable/function.
  4534. The following attributes are available:
  4535. name:
  4536. The name of the variable.
  4537. value:
  4538. The unexpanded value of the variable.
  4539. expanded_value:
  4540. The expanded value of the variable. For simple variables (those defined
  4541. with :=), this will equal 'value'. Accessing this property will raise a
  4542. KconfigError if the expansion seems to be stuck in a loop.
  4543. Accessing this field is the same as calling expanded_value_w_args() with
  4544. no arguments. I hadn't considered function arguments when adding it. It
  4545. is retained for backwards compatibility though.
  4546. is_recursive:
  4547. True if the variable is recursive (defined with =).
  4548. """
  4549. __slots__ = (
  4550. "_n_expansions",
  4551. "is_recursive",
  4552. "kconfig",
  4553. "name",
  4554. "value",
  4555. )
  4556. @property
  4557. def expanded_value(self):
  4558. """
  4559. See the class documentation.
  4560. """
  4561. return self.expanded_value_w_args()
  4562. def expanded_value_w_args(self, *args):
  4563. """
  4564. Returns the expanded value of the variable/function. Any arguments
  4565. passed will be substituted for $(1), $(2), etc.
  4566. Raises a KconfigError if the expansion seems to be stuck in a loop.
  4567. """
  4568. return self.kconfig._fn_val((self.name,) + args)
  4569. def __repr__(self):
  4570. return "<variable {}, {}, value '{}'>" \
  4571. .format(self.name,
  4572. "recursive" if self.is_recursive else "immediate",
  4573. self.value)
  4574. class KconfigError(Exception):
  4575. """
  4576. Exception raised for Kconfig-related errors.
  4577. KconfigError and KconfigSyntaxError are the same class. The
  4578. KconfigSyntaxError alias is only maintained for backwards compatibility.
  4579. """
  4580. KconfigSyntaxError = KconfigError # Backwards compatibility
  4581. class InternalError(Exception):
  4582. "Never raised. Kept around for backwards compatibility."
  4583. # Workaround:
  4584. #
  4585. # If 'errno' and 'strerror' are set on IOError, then __str__() always returns
  4586. # "[Errno <errno>] <strerror>", ignoring any custom message passed to the
  4587. # constructor. By defining our own subclass, we can use a custom message while
  4588. # also providing 'errno', 'strerror', and 'filename' to scripts.
  4589. class _KconfigIOError(IOError):
  4590. def __init__(self, ioerror, msg):
  4591. self.msg = msg
  4592. super(_KconfigIOError, self).__init__(
  4593. ioerror.errno, ioerror.strerror, ioerror.filename)
  4594. def __str__(self):
  4595. return self.msg
  4596. #
  4597. # Public functions
  4598. #
  4599. def expr_value(expr):
  4600. """
  4601. Evaluates the expression 'expr' to a tristate value. Returns 0 (n), 1 (m),
  4602. or 2 (y).
  4603. 'expr' must be an already-parsed expression from a Symbol, Choice, or
  4604. MenuNode property. To evaluate an expression represented as a string, use
  4605. Kconfig.eval_string().
  4606. Passing subexpressions of expressions to this function works as expected.
  4607. """
  4608. if expr.__class__ is not tuple:
  4609. return expr.tri_value
  4610. if expr[0] is AND:
  4611. v1 = expr_value(expr[1])
  4612. # Short-circuit the n case as an optimization (~5% faster
  4613. # allnoconfig.py and allyesconfig.py, as of writing)
  4614. return 0 if not v1 else min(v1, expr_value(expr[2]))
  4615. if expr[0] is OR:
  4616. v1 = expr_value(expr[1])
  4617. # Short-circuit the y case as an optimization
  4618. return 2 if v1 == 2 else max(v1, expr_value(expr[2]))
  4619. if expr[0] is NOT:
  4620. return 2 - expr_value(expr[1])
  4621. # Relation
  4622. #
  4623. # Implements <, <=, >, >= comparisons as well. These were added to
  4624. # kconfig in 31847b67 (kconfig: allow use of relations other than
  4625. # (in)equality).
  4626. rel, v1, v2 = expr
  4627. # If both operands are strings...
  4628. if v1.orig_type is STRING and v2.orig_type is STRING:
  4629. # ...then compare them lexicographically
  4630. comp = _strcmp(v1.str_value, v2.str_value)
  4631. else:
  4632. # Otherwise, try to compare them as numbers
  4633. try:
  4634. comp = _sym_to_num(v1) - _sym_to_num(v2)
  4635. except ValueError:
  4636. # Fall back on a lexicographic comparison if the operands don't
  4637. # parse as numbers
  4638. comp = _strcmp(v1.str_value, v2.str_value)
  4639. return 2*(comp == 0 if rel is EQUAL else
  4640. comp != 0 if rel is UNEQUAL else
  4641. comp < 0 if rel is LESS else
  4642. comp <= 0 if rel is LESS_EQUAL else
  4643. comp > 0 if rel is GREATER else
  4644. comp >= 0)
  4645. def standard_sc_expr_str(sc):
  4646. """
  4647. Standard symbol/choice printing function. Uses plain Kconfig syntax, and
  4648. displays choices as <choice> (or <choice NAME>, for named choices).
  4649. See expr_str().
  4650. """
  4651. if sc.__class__ is Symbol:
  4652. if sc.is_constant and sc.name not in STR_TO_TRI:
  4653. return '"{}"'.format(escape(sc.name))
  4654. return sc.name
  4655. return "<choice {}>".format(sc.name) if sc.name else "<choice>"
  4656. def expr_str(expr, sc_expr_str_fn=standard_sc_expr_str):
  4657. """
  4658. Returns the string representation of the expression 'expr', as in a Kconfig
  4659. file.
  4660. Passing subexpressions of expressions to this function works as expected.
  4661. sc_expr_str_fn (default: standard_sc_expr_str):
  4662. This function is called for every symbol/choice (hence "sc") appearing in
  4663. the expression, with the symbol/choice as the argument. It is expected to
  4664. return a string to be used for the symbol/choice.
  4665. This can be used e.g. to turn symbols/choices into links when generating
  4666. documentation, or for printing the value of each symbol/choice after it.
  4667. Note that quoted values are represented as constants symbols
  4668. (Symbol.is_constant == True).
  4669. """
  4670. if expr.__class__ is not tuple:
  4671. return sc_expr_str_fn(expr)
  4672. if expr[0] is AND:
  4673. return "{} && {}".format(_parenthesize(expr[1], OR, sc_expr_str_fn),
  4674. _parenthesize(expr[2], OR, sc_expr_str_fn))
  4675. if expr[0] is OR:
  4676. # This turns A && B || C && D into "(A && B) || (C && D)", which is
  4677. # redundant, but more readable
  4678. return "{} || {}".format(_parenthesize(expr[1], AND, sc_expr_str_fn),
  4679. _parenthesize(expr[2], AND, sc_expr_str_fn))
  4680. if expr[0] is NOT:
  4681. if expr[1].__class__ is tuple:
  4682. return "!({})".format(expr_str(expr[1], sc_expr_str_fn))
  4683. return "!" + sc_expr_str_fn(expr[1]) # Symbol
  4684. # Relation
  4685. #
  4686. # Relation operands are always symbols (quoted strings are constant
  4687. # symbols)
  4688. return "{} {} {}".format(sc_expr_str_fn(expr[1]), REL_TO_STR[expr[0]],
  4689. sc_expr_str_fn(expr[2]))
  4690. def expr_items(expr):
  4691. """
  4692. Returns a set() of all items (symbols and choices) that appear in the
  4693. expression 'expr'.
  4694. Passing subexpressions of expressions to this function works as expected.
  4695. """
  4696. res = set()
  4697. def rec(subexpr):
  4698. if subexpr.__class__ is tuple:
  4699. # AND, OR, NOT, or relation
  4700. rec(subexpr[1])
  4701. # NOTs only have a single operand
  4702. if subexpr[0] is not NOT:
  4703. rec(subexpr[2])
  4704. else:
  4705. # Symbol or choice
  4706. res.add(subexpr)
  4707. rec(expr)
  4708. return res
  4709. def split_expr(expr, op):
  4710. """
  4711. Returns a list containing the top-level AND or OR operands in the
  4712. expression 'expr', in the same (left-to-right) order as they appear in
  4713. the expression.
  4714. This can be handy e.g. for splitting (weak) reverse dependencies
  4715. from 'select' and 'imply' into individual selects/implies.
  4716. op:
  4717. Either AND to get AND operands, or OR to get OR operands.
  4718. (Having this as an operand might be more future-safe than having two
  4719. hardcoded functions.)
  4720. Pseudo-code examples:
  4721. split_expr( A , OR ) -> [A]
  4722. split_expr( A && B , OR ) -> [A && B]
  4723. split_expr( A || B , OR ) -> [A, B]
  4724. split_expr( A || B , AND ) -> [A || B]
  4725. split_expr( A || B || (C && D) , OR ) -> [A, B, C && D]
  4726. # Second || is not at the top level
  4727. split_expr( A || (B && (C || D)) , OR ) -> [A, B && (C || D)]
  4728. # Parentheses don't matter as long as we stay at the top level (don't
  4729. # encounter any non-'op' nodes)
  4730. split_expr( (A || B) || C , OR ) -> [A, B, C]
  4731. split_expr( A || (B || C) , OR ) -> [A, B, C]
  4732. """
  4733. res = []
  4734. def rec(subexpr):
  4735. if subexpr.__class__ is tuple and subexpr[0] is op:
  4736. rec(subexpr[1])
  4737. rec(subexpr[2])
  4738. else:
  4739. res.append(subexpr)
  4740. rec(expr)
  4741. return res
  4742. def escape(s):
  4743. r"""
  4744. Escapes the string 's' in the same fashion as is done for display in
  4745. Kconfig format and when writing strings to a .config file. " and \ are
  4746. replaced by \" and \\, respectively.
  4747. """
  4748. # \ must be escaped before " to avoid double escaping
  4749. return s.replace("\\", r"\\").replace('"', r'\"')
  4750. def unescape(s):
  4751. r"""
  4752. Unescapes the string 's'. \ followed by any character is replaced with just
  4753. that character. Used internally when reading .config files.
  4754. """
  4755. return _unescape_sub(r"\1", s)
  4756. # unescape() helper
  4757. _unescape_sub = re.compile(r"\\(.)").sub
  4758. def standard_kconfig():
  4759. """
  4760. Helper for tools. Loads the top-level Kconfig specified as the first
  4761. command-line argument, or "Kconfig" if there are no command-line arguments.
  4762. Returns the Kconfig instance.
  4763. Exits with sys.exit() (which raises a SystemExit exception) and prints a
  4764. usage note to stderr if more than one command-line argument is passed.
  4765. """
  4766. if len(sys.argv) > 2:
  4767. sys.exit("usage: {} [Kconfig]".format(sys.argv[0]))
  4768. # Only show backtraces for unexpected exceptions
  4769. try:
  4770. return Kconfig("Kconfig" if len(sys.argv) < 2 else sys.argv[1])
  4771. except (EnvironmentError, KconfigError) as e:
  4772. # Some long exception messages have extra newlines for better
  4773. # formatting when reported as an unhandled exception. Strip them here.
  4774. sys.exit(str(e).strip())
  4775. def standard_config_filename():
  4776. """
  4777. Helper for tools. Returns the value of KCONFIG_CONFIG (which specifies the
  4778. .config file to load/save) if it is set, and ".config" otherwise.
  4779. Calling load_config() with filename=None might give the behavior you want,
  4780. without having to use this function.
  4781. """
  4782. return os.getenv("KCONFIG_CONFIG", ".config")
  4783. def load_allconfig(kconf, filename):
  4784. """
  4785. Helper for all*config. Loads (merges) the configuration file specified by
  4786. KCONFIG_ALLCONFIG, if any. See Documentation/kbuild/kconfig.txt in the
  4787. Linux kernel.
  4788. Disables warnings for duplicated assignments within configuration files for
  4789. the duration of the call (kconf.warn_assign_override/warn_assign_redun = False),
  4790. and restores the previous warning settings at the end. The
  4791. KCONFIG_ALLCONFIG configuration file is expected to override symbols.
  4792. Exits with sys.exit() (which raises a SystemExit exception) and prints an
  4793. error to stderr if KCONFIG_ALLCONFIG is set but the configuration file
  4794. can't be opened.
  4795. kconf:
  4796. Kconfig instance to load the configuration in.
  4797. filename:
  4798. Command-specific configuration filename - "allyes.config",
  4799. "allno.config", etc.
  4800. """
  4801. allconfig = os.getenv("KCONFIG_ALLCONFIG")
  4802. if allconfig is None:
  4803. return
  4804. def std_msg(e):
  4805. # "Upcasts" a _KconfigIOError to an IOError, removing the custom
  4806. # __str__() message. The standard message is better here.
  4807. #
  4808. # This might also convert an OSError to an IOError in obscure cases,
  4809. # but it's probably not a big deal. The distinction is shaky (see
  4810. # PEP-3151).
  4811. return IOError(e.errno, e.strerror, e.filename)
  4812. old_warn_assign_override = kconf.warn_assign_override
  4813. old_warn_assign_redun = kconf.warn_assign_redun
  4814. kconf.warn_assign_override = kconf.warn_assign_redun = False
  4815. if allconfig in ("", "1"):
  4816. try:
  4817. print(kconf.load_config(filename, False))
  4818. except EnvironmentError as e1:
  4819. try:
  4820. print(kconf.load_config("all.config", False))
  4821. except EnvironmentError as e2:
  4822. sys.exit("error: KCONFIG_ALLCONFIG is set, but neither {} "
  4823. "nor all.config could be opened: {}, {}"
  4824. .format(filename, std_msg(e1), std_msg(e2)))
  4825. else:
  4826. try:
  4827. print(kconf.load_config(allconfig, False))
  4828. except EnvironmentError as e:
  4829. sys.exit("error: KCONFIG_ALLCONFIG is set to '{}', which "
  4830. "could not be opened: {}"
  4831. .format(allconfig, std_msg(e)))
  4832. kconf.warn_assign_override = old_warn_assign_override
  4833. kconf.warn_assign_redun = old_warn_assign_redun
  4834. #
  4835. # Internal functions
  4836. #
  4837. def _visibility(sc):
  4838. # Symbols and Choices have a "visibility" that acts as an upper bound on
  4839. # the values a user can set for them, corresponding to the visibility in
  4840. # e.g. 'make menuconfig'. This function calculates the visibility for the
  4841. # Symbol or Choice 'sc' -- the logic is nearly identical.
  4842. vis = 0
  4843. for node in sc.nodes:
  4844. if node.prompt:
  4845. vis = max(vis, expr_value(node.prompt[1]))
  4846. if sc.__class__ is Symbol and sc.choice:
  4847. if sc.choice.orig_type is TRISTATE and \
  4848. sc.orig_type is not TRISTATE and sc.choice.tri_value != 2:
  4849. # Non-tristate choice symbols are only visible in y mode
  4850. return 0
  4851. if sc.orig_type is TRISTATE and vis == 1 and sc.choice.tri_value == 2:
  4852. # Choice symbols with m visibility are not visible in y mode
  4853. return 0
  4854. # Promote m to y if we're dealing with a non-tristate (possibly due to
  4855. # modules being disabled)
  4856. if vis == 1 and sc.type is not TRISTATE:
  4857. return 2
  4858. return vis
  4859. def _make_depend_on(sc, expr):
  4860. # Adds 'sc' (symbol or choice) as a "dependee" to all symbols in 'expr'.
  4861. # Constant symbols in 'expr' are skipped as they can never change value
  4862. # anyway.
  4863. if expr.__class__ is tuple:
  4864. # AND, OR, NOT, or relation
  4865. _make_depend_on(sc, expr[1])
  4866. # NOTs only have a single operand
  4867. if expr[0] is not NOT:
  4868. _make_depend_on(sc, expr[2])
  4869. elif not expr.is_constant:
  4870. # Non-constant symbol, or choice
  4871. expr._dependents.add(sc)
  4872. def _parenthesize(expr, type_, sc_expr_str_fn):
  4873. # expr_str() helper. Adds parentheses around expressions of type 'type_'.
  4874. if expr.__class__ is tuple and expr[0] is type_:
  4875. return "({})".format(expr_str(expr, sc_expr_str_fn))
  4876. return expr_str(expr, sc_expr_str_fn)
  4877. def _ordered_unique(lst):
  4878. # Returns 'lst' with any duplicates removed, preserving order. This hacky
  4879. # version seems to be a common idiom. It relies on short-circuit evaluation
  4880. # and set.add() returning None, which is falsy.
  4881. seen = set()
  4882. seen_add = seen.add
  4883. return [x for x in lst if x not in seen and not seen_add(x)]
  4884. def _is_base_n(s, n):
  4885. try:
  4886. int(s, n)
  4887. return True
  4888. except ValueError:
  4889. return False
  4890. def _strcmp(s1, s2):
  4891. # strcmp()-alike that returns -1, 0, or 1
  4892. return (s1 > s2) - (s1 < s2)
  4893. def _sym_to_num(sym):
  4894. # expr_value() helper for converting a symbol to a number. Raises
  4895. # ValueError for symbols that can't be converted.
  4896. # For BOOL and TRISTATE, n/m/y count as 0/1/2. This mirrors 9059a3493ef
  4897. # ("kconfig: fix relational operators for bool and tristate symbols") in
  4898. # the C implementation.
  4899. return sym.tri_value if sym.orig_type in _BOOL_TRISTATE else \
  4900. int(sym.str_value, _TYPE_TO_BASE[sym.orig_type])
  4901. def _touch_dep_file(path, sym_name):
  4902. # If sym_name is MY_SYM_NAME, touches my/sym/name.h. See the sync_deps()
  4903. # docstring.
  4904. sym_path = path + os.sep + sym_name.lower().replace("_", os.sep) + ".h"
  4905. sym_path_dir = dirname(sym_path)
  4906. if not exists(sym_path_dir):
  4907. os.makedirs(sym_path_dir, 0o755)
  4908. # A kind of truncating touch, mirroring the C tools
  4909. os.close(os.open(
  4910. sym_path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o644))
  4911. def _save_old(path):
  4912. # See write_config()
  4913. def copy(src, dst):
  4914. # Import as needed, to save some startup time
  4915. import shutil
  4916. shutil.copyfile(src, dst)
  4917. if islink(path):
  4918. # Preserve symlinks
  4919. copy_fn = copy
  4920. elif hasattr(os, "replace"):
  4921. # Python 3 (3.3+) only. Best choice when available, because it
  4922. # removes <filename>.old on both *nix and Windows.
  4923. copy_fn = os.replace
  4924. elif os.name == "posix":
  4925. # Removes <filename>.old on POSIX systems
  4926. copy_fn = os.rename
  4927. else:
  4928. # Fall back on copying
  4929. copy_fn = copy
  4930. try:
  4931. copy_fn(path, path + ".old")
  4932. except Exception:
  4933. # Ignore errors from 'path' missing as well as other errors.
  4934. # <filename>.old file is usually more of a nice-to-have, and not worth
  4935. # erroring out over e.g. if <filename>.old happens to be a directory or
  4936. # <filename> is something like /dev/null.
  4937. pass
  4938. def _name_and_loc(sc):
  4939. # Helper for giving the symbol/choice name and location(s) in e.g. warnings
  4940. # Reuse the expression format. That way choices show up as
  4941. # '<choice (name, if any)>'
  4942. name = standard_sc_expr_str(sc)
  4943. if not sc.nodes:
  4944. return name + " (undefined)"
  4945. return "{} (defined at {})".format(
  4946. name,
  4947. ", ".join("{}:{}".format(node.filename, node.linenr)
  4948. for node in sc.nodes))
  4949. # Menu manipulation
  4950. def _expr_depends_on(expr, sym):
  4951. # Reimplementation of expr_depends_symbol() from mconf.c. Used to determine
  4952. # if a submenu should be implicitly created. This also influences which
  4953. # items inside choice statements are considered choice items.
  4954. if expr.__class__ is not tuple:
  4955. return expr is sym
  4956. if expr[0] in _EQUAL_UNEQUAL:
  4957. # Check for one of the following:
  4958. # sym = m/y, m/y = sym, sym != n, n != sym
  4959. left, right = expr[1:]
  4960. if right is sym:
  4961. left, right = right, left
  4962. elif left is not sym:
  4963. return False
  4964. return (expr[0] is EQUAL and right is sym.kconfig.m or
  4965. right is sym.kconfig.y) or \
  4966. (expr[0] is UNEQUAL and right is sym.kconfig.n)
  4967. return expr[0] is AND and \
  4968. (_expr_depends_on(expr[1], sym) or
  4969. _expr_depends_on(expr[2], sym))
  4970. def _auto_menu_dep(node1, node2):
  4971. # Returns True if node2 has an "automatic menu dependency" on node1. If
  4972. # node2 has a prompt, we check its condition. Otherwise, we look directly
  4973. # at node2.dep.
  4974. return _expr_depends_on(node2.prompt[1] if node2.prompt else node2.dep,
  4975. node1.item)
  4976. def _flatten(node):
  4977. # "Flattens" menu nodes without prompts (e.g. 'if' nodes and non-visible
  4978. # symbols with children from automatic menu creation) so that their
  4979. # children appear after them instead. This gives a clean menu structure
  4980. # with no unexpected "jumps" in the indentation.
  4981. #
  4982. # Do not flatten promptless choices (which can appear "legitimately" if a
  4983. # named choice is defined in multiple locations to add on symbols). It
  4984. # looks confusing, and the menuconfig already shows all choice symbols if
  4985. # you enter the choice at some location with a prompt.
  4986. while node:
  4987. if node.list and not node.prompt and \
  4988. node.item.__class__ is not Choice:
  4989. last_node = node.list
  4990. while 1:
  4991. last_node.parent = node.parent
  4992. if not last_node.next:
  4993. break
  4994. last_node = last_node.next
  4995. last_node.next = node.next
  4996. node.next = node.list
  4997. node.list = None
  4998. node = node.next
  4999. def _remove_ifs(node):
  5000. # Removes 'if' nodes (which can be recognized by MenuNode.item being None),
  5001. # which are assumed to already have been flattened. The C implementation
  5002. # doesn't bother to do this, but we expose the menu tree directly, and it
  5003. # makes it nicer to work with.
  5004. cur = node.list
  5005. while cur and not cur.item:
  5006. cur = cur.next
  5007. node.list = cur
  5008. while cur:
  5009. next = cur.next
  5010. while next and not next.item:
  5011. next = next.next
  5012. # Equivalent to
  5013. #
  5014. # cur.next = next
  5015. # cur = next
  5016. #
  5017. # due to tricky Python semantics. The order matters.
  5018. cur.next = cur = next
  5019. def _finalize_choice(node):
  5020. # Finalizes a choice, marking each symbol whose menu node has the choice as
  5021. # the parent as a choice symbol, and automatically determining types if not
  5022. # specified.
  5023. choice = node.item
  5024. cur = node.list
  5025. while cur:
  5026. if cur.item.__class__ is Symbol:
  5027. cur.item.choice = choice
  5028. choice.syms.append(cur.item)
  5029. cur = cur.next
  5030. # If no type is specified for the choice, its type is that of
  5031. # the first choice item with a specified type
  5032. if not choice.orig_type:
  5033. for item in choice.syms:
  5034. if item.orig_type:
  5035. choice.orig_type = item.orig_type
  5036. break
  5037. # Each choice item of UNKNOWN type gets the type of the choice
  5038. for sym in choice.syms:
  5039. if not sym.orig_type:
  5040. sym.orig_type = choice.orig_type
  5041. def _check_dep_loop_sym(sym, ignore_choice):
  5042. # Detects dependency loops using depth-first search on the dependency graph
  5043. # (which is calculated earlier in Kconfig._build_dep()).
  5044. #
  5045. # Algorithm:
  5046. #
  5047. # 1. Symbols/choices start out with _visited = 0, meaning unvisited.
  5048. #
  5049. # 2. When a symbol/choice is first visited, _visited is set to 1, meaning
  5050. # "visited, potentially part of a dependency loop". The recursive
  5051. # search then continues from the symbol/choice.
  5052. #
  5053. # 3. If we run into a symbol/choice X with _visited already set to 1,
  5054. # there's a dependency loop. The loop is found on the call stack by
  5055. # recording symbols while returning ("on the way back") until X is seen
  5056. # again.
  5057. #
  5058. # 4. Once a symbol/choice and all its dependencies (or dependents in this
  5059. # case) have been checked recursively without detecting any loops, its
  5060. # _visited is set to 2, meaning "visited, not part of a dependency
  5061. # loop".
  5062. #
  5063. # This saves work if we run into the symbol/choice again in later calls
  5064. # to _check_dep_loop_sym(). We just return immediately.
  5065. #
  5066. # Choices complicate things, as every choice symbol depends on every other
  5067. # choice symbol in a sense. When a choice is "entered" via a choice symbol
  5068. # X, we visit all choice symbols from the choice except X, and prevent
  5069. # immediately revisiting the choice with a flag (ignore_choice).
  5070. #
  5071. # Maybe there's a better way to handle this (different flags or the
  5072. # like...)
  5073. if not sym._visited:
  5074. # sym._visited == 0, unvisited
  5075. sym._visited = 1
  5076. for dep in sym._dependents:
  5077. # Choices show up in Symbol._dependents when the choice has the
  5078. # symbol in a 'prompt' or 'default' condition (e.g.
  5079. # 'default ... if SYM').
  5080. #
  5081. # Since we aren't entering the choice via a choice symbol, all
  5082. # choice symbols need to be checked, hence the None.
  5083. loop = _check_dep_loop_choice(dep, None) \
  5084. if dep.__class__ is Choice \
  5085. else _check_dep_loop_sym(dep, False)
  5086. if loop:
  5087. # Dependency loop found
  5088. return _found_dep_loop(loop, sym)
  5089. if sym.choice and not ignore_choice:
  5090. loop = _check_dep_loop_choice(sym.choice, sym)
  5091. if loop:
  5092. # Dependency loop found
  5093. return _found_dep_loop(loop, sym)
  5094. # The symbol is not part of a dependency loop
  5095. sym._visited = 2
  5096. # No dependency loop found
  5097. return None
  5098. if sym._visited == 2:
  5099. # The symbol was checked earlier and is already known to not be part of
  5100. # a dependency loop
  5101. return None
  5102. # sym._visited == 1, found a dependency loop. Return the symbol as the
  5103. # first element in it.
  5104. return (sym,)
  5105. def _check_dep_loop_choice(choice, skip):
  5106. if not choice._visited:
  5107. # choice._visited == 0, unvisited
  5108. choice._visited = 1
  5109. # Check for loops involving choice symbols. If we came here via a
  5110. # choice symbol, skip that one, as we'd get a false positive
  5111. # '<sym FOO> -> <choice> -> <sym FOO>' loop otherwise.
  5112. for sym in choice.syms:
  5113. if sym is not skip:
  5114. # Prevent the choice from being immediately re-entered via the
  5115. # "is a choice symbol" path by passing True
  5116. loop = _check_dep_loop_sym(sym, True)
  5117. if loop:
  5118. # Dependency loop found
  5119. return _found_dep_loop(loop, choice)
  5120. # The choice is not part of a dependency loop
  5121. choice._visited = 2
  5122. # No dependency loop found
  5123. return None
  5124. if choice._visited == 2:
  5125. # The choice was checked earlier and is already known to not be part of
  5126. # a dependency loop
  5127. return None
  5128. # choice._visited == 1, found a dependency loop. Return the choice as the
  5129. # first element in it.
  5130. return (choice,)
  5131. def _found_dep_loop(loop, cur):
  5132. # Called "on the way back" when we know we have a loop
  5133. # Is the symbol/choice 'cur' where the loop started?
  5134. if cur is not loop[0]:
  5135. # Nope, it's just a part of the loop
  5136. return loop + (cur,)
  5137. # Yep, we have the entire loop. Throw an exception that shows it.
  5138. msg = "\nDependency loop\n" \
  5139. "===============\n\n"
  5140. for item in loop:
  5141. if item is not loop[0]:
  5142. msg += "...depends on "
  5143. if item.__class__ is Symbol and item.choice:
  5144. msg += "the choice symbol "
  5145. msg += "{}, with definition...\n\n{}\n\n" \
  5146. .format(_name_and_loc(item), item)
  5147. # Small wart: Since we reuse the already calculated
  5148. # Symbol/Choice._dependents sets for recursive dependency detection, we
  5149. # lose information on whether a dependency came from a 'select'/'imply'
  5150. # condition or e.g. a 'depends on'.
  5151. #
  5152. # This might cause selecting symbols to "disappear". For example,
  5153. # a symbol B having 'select A if C' gives a direct dependency from A to
  5154. # C, since it corresponds to a reverse dependency of B && C.
  5155. #
  5156. # Always print reverse dependencies for symbols that have them to make
  5157. # sure information isn't lost. I wonder if there's some neat way to
  5158. # improve this.
  5159. if item.__class__ is Symbol:
  5160. if item.rev_dep is not item.kconfig.n:
  5161. msg += "(select-related dependencies: {})\n\n" \
  5162. .format(expr_str(item.rev_dep))
  5163. if item.weak_rev_dep is not item.kconfig.n:
  5164. msg += "(imply-related dependencies: {})\n\n" \
  5165. .format(expr_str(item.rev_dep))
  5166. msg += "...depends again on {}".format(_name_and_loc(loop[0]))
  5167. raise KconfigError(msg)
  5168. def _decoding_error(e, filename, macro_linenr=None):
  5169. # Gives the filename and context for UnicodeDecodeError's, which are a pain
  5170. # to debug otherwise. 'e' is the UnicodeDecodeError object.
  5171. #
  5172. # If the decoding error is for the output of a $(shell,...) command,
  5173. # macro_linenr holds the line number where it was run (the exact line
  5174. # number isn't available for decoding errors in files).
  5175. raise KconfigError(
  5176. "\n"
  5177. "Malformed {} in {}\n"
  5178. "Context: {}\n"
  5179. "Problematic data: {}\n"
  5180. "Reason: {}".format(
  5181. e.encoding,
  5182. "'{}'".format(filename) if macro_linenr is None else
  5183. "output from macro at {}:{}".format(filename, macro_linenr),
  5184. e.object[max(e.start - 40, 0):e.end + 40],
  5185. e.object[e.start:e.end],
  5186. e.reason))
  5187. def _warn_verbose_deprecated(fn_name):
  5188. sys.stderr.write(
  5189. "Deprecation warning: {0}()'s 'verbose' argument has no effect. Since "
  5190. "Kconfiglib 12.0.0, the message is returned from {0}() instead, "
  5191. "and is always generated. Do e.g. print(kconf.{0}()) if you want to "
  5192. "want to show a message like \"Loaded configuration '.config'\" on "
  5193. "stdout. The old API required ugly hacks to reuse messages in "
  5194. "configuration interfaces.\n".format(fn_name))
  5195. # Predefined preprocessor functions
  5196. def _filename_fn(kconf, _):
  5197. return kconf.filename
  5198. def _lineno_fn(kconf, _):
  5199. return str(kconf.linenr)
  5200. def _info_fn(kconf, _, msg):
  5201. print("{}:{}: {}".format(kconf.filename, kconf.linenr, msg))
  5202. return ""
  5203. def _warning_if_fn(kconf, _, cond, msg):
  5204. if cond == "y":
  5205. kconf._warn(msg, kconf.filename, kconf.linenr)
  5206. return ""
  5207. def _error_if_fn(kconf, _, cond, msg):
  5208. if cond == "y":
  5209. raise KconfigError("{}:{}: {}".format(
  5210. kconf.filename, kconf.linenr, msg))
  5211. return ""
  5212. def _shell_fn(kconf, _, command):
  5213. # Only import as needed, to save some startup time
  5214. import subprocess
  5215. stdout, stderr = subprocess.Popen(
  5216. command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
  5217. ).communicate()
  5218. if not _IS_PY2:
  5219. try:
  5220. stdout = stdout.decode(kconf._encoding)
  5221. stderr = stderr.decode(kconf._encoding)
  5222. except UnicodeDecodeError as e:
  5223. _decoding_error(e, kconf.filename, kconf.linenr)
  5224. if stderr:
  5225. kconf._warn("'{}' wrote to stderr: {}".format(
  5226. command, "\n".join(stderr.splitlines())),
  5227. kconf.filename, kconf.linenr)
  5228. # Universal newlines with splitlines() (to prevent e.g. stray \r's in
  5229. # command output on Windows), trailing newline removal, and
  5230. # newline-to-space conversion.
  5231. #
  5232. # On Python 3 versions before 3.6, it's not possible to specify the
  5233. # encoding when passing universal_newlines=True to Popen() (the 'encoding'
  5234. # parameter was added in 3.6), so we do this manual version instead.
  5235. return "\n".join(stdout.splitlines()).rstrip("\n").replace("\n", " ")
  5236. #
  5237. # Global constants
  5238. #
  5239. TRI_TO_STR = {
  5240. 0: "n",
  5241. 1: "m",
  5242. 2: "y",
  5243. }
  5244. STR_TO_TRI = {
  5245. "n": 0,
  5246. "m": 1,
  5247. "y": 2,
  5248. }
  5249. # Constant representing that there's no cached choice selection. This is
  5250. # distinct from a cached None (no selection). Any object that's not None or a
  5251. # Symbol will do. We test this with 'is'.
  5252. _NO_CACHED_SELECTION = 0
  5253. # Are we running on Python 2?
  5254. _IS_PY2 = sys.version_info[0] < 3
  5255. try:
  5256. _UNAME_RELEASE = os.uname()[2]
  5257. except AttributeError:
  5258. # Only import as needed, to save some startup time
  5259. import platform
  5260. _UNAME_RELEASE = platform.uname()[2]
  5261. # The token and type constants below are safe to test with 'is', which is a bit
  5262. # faster (~30% faster on my machine, and a few % faster for total parsing
  5263. # time), even without assuming Python's small integer optimization (which
  5264. # caches small integer objects). The constants end up pointing to unique
  5265. # integer objects, and since we consistently refer to them via the names below,
  5266. # we always get the same object.
  5267. #
  5268. # Client code should use == though.
  5269. # Tokens, with values 1, 2, ... . Avoiding 0 simplifies some checks by making
  5270. # all tokens except empty strings truthy.
  5271. (
  5272. _T_ALLNOCONFIG_Y,
  5273. _T_AND,
  5274. _T_BOOL,
  5275. _T_CHOICE,
  5276. _T_CLOSE_PAREN,
  5277. _T_COMMENT,
  5278. _T_CONFIG,
  5279. _T_DEFAULT,
  5280. _T_DEFCONFIG_LIST,
  5281. _T_DEF_BOOL,
  5282. _T_DEF_HEX,
  5283. _T_DEF_INT,
  5284. _T_DEF_STRING,
  5285. _T_DEF_TRISTATE,
  5286. _T_DEPENDS,
  5287. _T_ENDCHOICE,
  5288. _T_ENDIF,
  5289. _T_ENDMENU,
  5290. _T_ENV,
  5291. _T_EQUAL,
  5292. _T_GREATER,
  5293. _T_GREATER_EQUAL,
  5294. _T_HELP,
  5295. _T_HEX,
  5296. _T_IF,
  5297. _T_IMPLY,
  5298. _T_INT,
  5299. _T_LESS,
  5300. _T_LESS_EQUAL,
  5301. _T_MAINMENU,
  5302. _T_MENU,
  5303. _T_MENUCONFIG,
  5304. _T_MODULES,
  5305. _T_NOT,
  5306. _T_ON,
  5307. _T_OPEN_PAREN,
  5308. _T_OPTION,
  5309. _T_OPTIONAL,
  5310. _T_OR,
  5311. _T_ORSOURCE,
  5312. _T_OSOURCE,
  5313. _T_PROMPT,
  5314. _T_RANGE,
  5315. _T_RSOURCE,
  5316. _T_SELECT,
  5317. _T_SOURCE,
  5318. _T_STRING,
  5319. _T_TRISTATE,
  5320. _T_UNEQUAL,
  5321. _T_VISIBLE,
  5322. ) = range(1, 51)
  5323. # Keyword to token map, with the get() method assigned directly as a small
  5324. # optimization
  5325. _get_keyword = {
  5326. "---help---": _T_HELP,
  5327. "allnoconfig_y": _T_ALLNOCONFIG_Y,
  5328. "bool": _T_BOOL,
  5329. "boolean": _T_BOOL,
  5330. "choice": _T_CHOICE,
  5331. "comment": _T_COMMENT,
  5332. "config": _T_CONFIG,
  5333. "def_bool": _T_DEF_BOOL,
  5334. "def_hex": _T_DEF_HEX,
  5335. "def_int": _T_DEF_INT,
  5336. "def_string": _T_DEF_STRING,
  5337. "def_tristate": _T_DEF_TRISTATE,
  5338. "default": _T_DEFAULT,
  5339. "defconfig_list": _T_DEFCONFIG_LIST,
  5340. "depends": _T_DEPENDS,
  5341. "endchoice": _T_ENDCHOICE,
  5342. "endif": _T_ENDIF,
  5343. "endmenu": _T_ENDMENU,
  5344. "env": _T_ENV,
  5345. "grsource": _T_ORSOURCE, # Backwards compatibility
  5346. "gsource": _T_OSOURCE, # Backwards compatibility
  5347. "help": _T_HELP,
  5348. "hex": _T_HEX,
  5349. "if": _T_IF,
  5350. "imply": _T_IMPLY,
  5351. "int": _T_INT,
  5352. "mainmenu": _T_MAINMENU,
  5353. "menu": _T_MENU,
  5354. "menuconfig": _T_MENUCONFIG,
  5355. "modules": _T_MODULES,
  5356. "on": _T_ON,
  5357. "option": _T_OPTION,
  5358. "optional": _T_OPTIONAL,
  5359. "orsource": _T_ORSOURCE,
  5360. "osource": _T_OSOURCE,
  5361. "prompt": _T_PROMPT,
  5362. "range": _T_RANGE,
  5363. "rsource": _T_RSOURCE,
  5364. "select": _T_SELECT,
  5365. "source": _T_SOURCE,
  5366. "string": _T_STRING,
  5367. "tristate": _T_TRISTATE,
  5368. "visible": _T_VISIBLE,
  5369. }.get
  5370. # The constants below match the value of the corresponding tokens to remove the
  5371. # need for conversion
  5372. # Node types
  5373. MENU = _T_MENU
  5374. COMMENT = _T_COMMENT
  5375. # Expression types
  5376. AND = _T_AND
  5377. OR = _T_OR
  5378. NOT = _T_NOT
  5379. EQUAL = _T_EQUAL
  5380. UNEQUAL = _T_UNEQUAL
  5381. LESS = _T_LESS
  5382. LESS_EQUAL = _T_LESS_EQUAL
  5383. GREATER = _T_GREATER
  5384. GREATER_EQUAL = _T_GREATER_EQUAL
  5385. REL_TO_STR = {
  5386. EQUAL: "=",
  5387. UNEQUAL: "!=",
  5388. LESS: "<",
  5389. LESS_EQUAL: "<=",
  5390. GREATER: ">",
  5391. GREATER_EQUAL: ">=",
  5392. }
  5393. # Symbol/choice types. UNKNOWN is 0 (falsy) to simplify some checks.
  5394. # Client code shouldn't rely on it though, as it was non-zero in
  5395. # older versions.
  5396. UNKNOWN = 0
  5397. BOOL = _T_BOOL
  5398. TRISTATE = _T_TRISTATE
  5399. STRING = _T_STRING
  5400. INT = _T_INT
  5401. HEX = _T_HEX
  5402. TYPE_TO_STR = {
  5403. UNKNOWN: "unknown",
  5404. BOOL: "bool",
  5405. TRISTATE: "tristate",
  5406. STRING: "string",
  5407. INT: "int",
  5408. HEX: "hex",
  5409. }
  5410. # Used in comparisons. 0 means the base is inferred from the format of the
  5411. # string.
  5412. _TYPE_TO_BASE = {
  5413. HEX: 16,
  5414. INT: 10,
  5415. STRING: 0,
  5416. UNKNOWN: 0,
  5417. }
  5418. # def_bool -> BOOL, etc.
  5419. _DEF_TOKEN_TO_TYPE = {
  5420. _T_DEF_BOOL: BOOL,
  5421. _T_DEF_HEX: HEX,
  5422. _T_DEF_INT: INT,
  5423. _T_DEF_STRING: STRING,
  5424. _T_DEF_TRISTATE: TRISTATE,
  5425. }
  5426. # Tokens after which strings are expected. This is used to tell strings from
  5427. # constant symbol references during tokenization, both of which are enclosed in
  5428. # quotes.
  5429. #
  5430. # Identifier-like lexemes ("missing quotes") are also treated as strings after
  5431. # these tokens. _T_CHOICE is included to avoid symbols being registered for
  5432. # named choices.
  5433. _STRING_LEX = frozenset({
  5434. _T_BOOL,
  5435. _T_CHOICE,
  5436. _T_COMMENT,
  5437. _T_HEX,
  5438. _T_INT,
  5439. _T_MAINMENU,
  5440. _T_MENU,
  5441. _T_ORSOURCE,
  5442. _T_OSOURCE,
  5443. _T_PROMPT,
  5444. _T_RSOURCE,
  5445. _T_SOURCE,
  5446. _T_STRING,
  5447. _T_TRISTATE,
  5448. })
  5449. # Various sets for quick membership tests. Gives a single global lookup and
  5450. # avoids creating temporary dicts/tuples.
  5451. _TYPE_TOKENS = frozenset({
  5452. _T_BOOL,
  5453. _T_TRISTATE,
  5454. _T_INT,
  5455. _T_HEX,
  5456. _T_STRING,
  5457. })
  5458. _SOURCE_TOKENS = frozenset({
  5459. _T_SOURCE,
  5460. _T_RSOURCE,
  5461. _T_OSOURCE,
  5462. _T_ORSOURCE,
  5463. })
  5464. _REL_SOURCE_TOKENS = frozenset({
  5465. _T_RSOURCE,
  5466. _T_ORSOURCE,
  5467. })
  5468. # Obligatory (non-optional) sources
  5469. _OBL_SOURCE_TOKENS = frozenset({
  5470. _T_SOURCE,
  5471. _T_RSOURCE,
  5472. })
  5473. _BOOL_TRISTATE = frozenset({
  5474. BOOL,
  5475. TRISTATE,
  5476. })
  5477. _BOOL_TRISTATE_UNKNOWN = frozenset({
  5478. BOOL,
  5479. TRISTATE,
  5480. UNKNOWN,
  5481. })
  5482. _INT_HEX = frozenset({
  5483. INT,
  5484. HEX,
  5485. })
  5486. _SYMBOL_CHOICE = frozenset({
  5487. Symbol,
  5488. Choice,
  5489. })
  5490. _MENU_COMMENT = frozenset({
  5491. MENU,
  5492. COMMENT,
  5493. })
  5494. _EQUAL_UNEQUAL = frozenset({
  5495. EQUAL,
  5496. UNEQUAL,
  5497. })
  5498. _RELATIONS = frozenset({
  5499. EQUAL,
  5500. UNEQUAL,
  5501. LESS,
  5502. LESS_EQUAL,
  5503. GREATER,
  5504. GREATER_EQUAL,
  5505. })
  5506. # Helper functions for getting compiled regular expressions, with the needed
  5507. # matching function returned directly as a small optimization.
  5508. #
  5509. # Use ASCII regex matching on Python 3. It's already the default on Python 2.
  5510. def _re_match(regex):
  5511. return re.compile(regex, 0 if _IS_PY2 else re.ASCII).match
  5512. def _re_search(regex):
  5513. return re.compile(regex, 0 if _IS_PY2 else re.ASCII).search
  5514. # Various regular expressions used during parsing
  5515. # The initial token on a line. Also eats leading and trailing whitespace, so
  5516. # that we can jump straight to the next token (or to the end of the line if
  5517. # there is only one token).
  5518. #
  5519. # This regex will also fail to match for empty lines and comment lines.
  5520. #
  5521. # '$' is included to detect preprocessor variable assignments with macro
  5522. # expansions in the left-hand side.
  5523. _command_match = _re_match(r"\s*([A-Za-z0-9_$-]+)\s*")
  5524. # An identifier/keyword after the first token. Also eats trailing whitespace.
  5525. # '$' is included to detect identifiers containing macro expansions.
  5526. _id_keyword_match = _re_match(r"([A-Za-z0-9_$/.-]+)\s*")
  5527. # A fragment in the left-hand side of a preprocessor variable assignment. These
  5528. # are the portions between macro expansions ($(foo)). Macros are supported in
  5529. # the LHS (variable name).
  5530. _assignment_lhs_fragment_match = _re_match("[A-Za-z0-9_-]*")
  5531. # The assignment operator and value (right-hand side) in a preprocessor
  5532. # variable assignment
  5533. _assignment_rhs_match = _re_match(r"\s*(=|:=|\+=)\s*(.*)")
  5534. # Special characters/strings while expanding a macro (')', ',', and '$(')
  5535. _macro_special_search = _re_search(r"\)|,|\$\(")
  5536. # Special characters/strings while expanding a string (quotes, '\', and '$(')
  5537. _string_special_search = _re_search(r'"|\'|\\|\$\(')
  5538. # Special characters/strings while expanding a symbol name. Also includes
  5539. # end-of-line, in case the macro is the last thing on the line.
  5540. _name_special_search = _re_search(r'[^A-Za-z0-9_$/.-]|\$\(|$')
  5541. # A valid right-hand side for an assignment to a string symbol in a .config
  5542. # file, including escaped characters. Extracts the contents.
  5543. _conf_string_match = _re_match(r'"((?:[^\\"]|\\.)*)"')