kconfiglib.py 254 KB

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