pcons-2.3.1 246 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912
  1. #!/usr/bin/env perl
  2. #
  3. # Revision history:
  4. # -----------------
  5. # July 2001, Thomas Gleerup <tgl@oticon.dk>
  6. # 1) pcons-1.6 (John Erickson, August 1999) merged into cons-2.3.0.
  7. # 2) Added automatic insertion of && for multi-line commands.
  8. #
  9. # September 2001, Thomas Gleerup <tgl@oticon.dk>
  10. # 1) Updated with Kevin Nolish's multi-line improvement.
  11. # 2) Improved this so that single [perl] commands can still be used.
  12. #
  13. # NOTE: Cons intentionally does not use the "perl -w" option or
  14. # "use strict." Because Cons "configuration files" are actually
  15. # Perl scripts, enabling those restrictions here would force them
  16. # on every user's config files, wanted or not. Would users write
  17. # "better" Construct and Conscript files if we forced "use strict"
  18. # on them? Probably. But we want people to use Cons to get work
  19. # done, not force everyone to become a Perl guru to use it, so we
  20. # don't insist.
  21. #
  22. # That said, Cons' code is both "perl -w" and "use strict" clean.
  23. # Regression tests keep the code honest by checking for warnings
  24. # and "use strict" failures.
  25. use vars qw( $CVS_id $CVS_ver $ver_num $ver_rev $version );
  26. # I hate those CVS tags
  27. $CVS_id = 'pcons-2.3.1';
  28. $CVS_ver = (split (/\s+/, $CVS_id))[2];
  29. $ver_num = "__VERSION__";
  30. $ver_rev = "__REVISION__";
  31. #$version = "This is Cons $ver_num$ver_rev ($CVS_id)\n";
  32. $version = "This is parallel Cons (pcons) $ver_num$ver_rev ($CVS_id)\n";
  33. # Cons: A Software Construction Tool.
  34. # Copyright (c) 1996-2001 Free Software Foundation, Inc.
  35. #
  36. # This program is free software; you can redistribute it and/or modify
  37. # it under the terms of the GNU General Public License as published by
  38. # the Free Software Foundation; either version 2 of the License, or
  39. # (at your option) any later version.
  40. #
  41. # This program is distributed in the hope that it will be useful,
  42. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  43. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  44. # GNU General Public License for more details.
  45. #
  46. # You should have received a copy of the GNU General Public License
  47. # along with this program; see the file COPYING. If not, write to
  48. # the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  49. # Boston, MA 02111-1307, USA.
  50. require 5.003;
  51. # See the NOTE above about why Cons doesn't "use strict".
  52. use integer;
  53. use Cwd;
  54. use File::Copy;
  55. use vars qw( $_WIN32 $_a $_exe $_o $_so );
  56. #------------------------------------------------------------------
  57. # Determine if running on win32 platform - either Windows NT or 95
  58. #------------------------------------------------------------------
  59. use vars qw( $PATH_SEPARATOR $iswin32 $_WIN32 $usage $indent @targets );
  60. BEGIN
  61. {
  62. use Config;
  63. # if the version is 5.003, we can check $^O
  64. if ($] < 5.003)
  65. {
  66. eval("require Win32");
  67. $_WIN32 = (!$@);
  68. }
  69. else
  70. {
  71. $_WIN32 = ($^O eq "MSWin32") ? 1 : 0;
  72. }
  73. # Fetch the PATH separator from Config;
  74. # provide our old defaults in case it's not set.
  75. $PATH_SEPARATOR = $Config{path_sep};
  76. $PATH_SEPARATOR = $_WIN32 ? ';' : ':' if !defined $PATH_SEPARATOR;
  77. # Fetch file suffixes from Config,
  78. # accomodating differences in the Config variables
  79. # used by different Perl versions.
  80. $_exe = $Config{_exe};
  81. $_exe = $Config{exe_ext} if !defined $_exe;
  82. $_exe = $_WIN32 ? '.exe' : '' if !defined $_exe;
  83. $_o = $Config{_o};
  84. $_o = $Config{obj_ext} if !defined $_o;
  85. $_o = $_WIN32 ? '.obj' : '.o' if !defined $_o;
  86. $_a = $Config{_a};
  87. $_a = $Config{lib_ext} if !defined $_a;
  88. $_a = $_WIN32 ? '.lib' : '.a' if !defined $_a;
  89. $_so = ".$Config{so}";
  90. $_so = $_WIN32 ? '.dll' : '.so' if !defined $_so;
  91. }
  92. # Flush stdout each time.
  93. $| = 1;
  94. # Seed random number generator.
  95. srand(time . $$); # this works better than time ^ $$ in perlfunc manpage.
  96. $usage = q(
  97. Usage: cons <arguments> -- <construct-args>
  98. Arguments can be any of the following, in any order:
  99. <targets> Build the specified targets. If <target> is a directory
  100. recursively build everything within that directory.
  101. +<pattern> Limit the cons scripts considered to just those that
  102. match <pattern>. Multiple + arguments are accepted.
  103. <name>=<val> Sets <name> to value <val> in the ARG hash passed to the
  104. top-level Construct file.
  105. -cc Show command that would have been executed, when
  106. retrieving from cache. No indication that the file
  107. has been retrieved is given; this is useful for
  108. generating build logs that can be compared with
  109. real build logs.
  110. -cd Disable all caching. Do not retrieve from cache nor
  111. flush to cache.
  112. -cr Build dependencies in random order. This is useful when
  113. building multiple similar trees with caching enabled.
  114. -cs Synchronize existing build targets that are found to be
  115. up-to-date with cache. This is useful if caching has
  116. been disabled with -cc or just recently enabled with
  117. UseCache.
  118. -d Enable dependency debugging.
  119. -f <file> Use the specified file instead of "Construct" (but first
  120. change to containing directory of <file>).
  121. -h Show a help message local to the current build if
  122. one such is defined, and exit.
  123. -k Keep going as far as possible after errors.
  124. -o <file> Read override file <file>.
  125. -p Show construction products in specified trees.
  126. -pa Show construction products and associated actions.
  127. -pw Show products and where they are defined.
  128. -q Be quiet; multiple -q flags increase quietness level:
  129. 1: quiet about Installing and Removing targets
  130. 2: quiet about build commands, up-to-date targets
  131. -r Remove construction products associated with <targets>
  132. -R <repos> Search for files in <repos>. Multiple -R <repos>
  133. directories are searched in the order specified.
  134. -S <pkg> Use package sig::<pkg> to calculate file signatures.
  135. Currently supported values are "md5" for MD5
  136. signatures (the default) and "md5::debug" for MD5
  137. signature debug information.
  138. -t Traverse up the directory hierarchy looking for a
  139. Construct file, if none exists in the current directory.
  140. (Targets will be modified to be relative to the
  141. Construct file.)
  142. -v Show cons version and continue processing.
  143. -V Show cons version and exit.
  144. -wf <file> Write all filenames considered into <file>.
  145. -x Show this message and exit.
  146. Please report any suggestions through the cons-discuss@gnu.org mailing
  147. list.
  148. To subscribe, send mail to cons-discuss-request@gnu.org with body
  149. 'subscribe'.
  150. If you find a bug, please report it through the bug-cons@gnu.org
  151. mailing list.
  152. Information about CONS can be obtained from the official cons web site
  153. http://www.dsmit.com/cons/ or its mirrors (listed there).
  154. The cons maintainers can be contacted by email at cons-maintainers@gnu.org
  155. User documentation of cons is contained in cons and can be obtained
  156. by doing 'perldoc /path/to/cons'.
  157. );
  158. my $pcons = 1;
  159. # Simplify program name, if it is a path.
  160. {
  161. my ($vol, $dir, $file) = File::Spec->splitpath(File::Spec->canonpath($0));
  162. $0 = $file;
  163. }
  164. # Default parameters.
  165. $param::topfile = 'Construct'; # Top-level construction file.
  166. $param::install = 1; # Show installations
  167. $param::build = 1; # Build targets
  168. ### $param::show = 1; # Show building of targets.
  169. $param::sigpro = 'md5'; # Signature protocol.
  170. $param::depfile = ''; # Write all deps out to this file
  171. $param::salt = ''; # Salt derived file signatures with this.
  172. $param::sourcesig = ['*' => 'content']; # Source file signature calculation
  173. $param::rep_sig_times_ok = 1; # Repository .consign times are in sync
  174. # w/files.
  175. $param::conscript_chdir = 0; # Change dir to Conscript directory
  176. $param::quiet = 0; # should we show the command being executed.
  177. $param::max_jobs = 1; # pcons
  178. @param::defaults = ();
  179. #
  180. $indent = '';
  181. # Display a command while executing or otherwise. This
  182. # should be called by command builder action methods.
  183. sub showcom
  184. {
  185. print($indent . $_[0] . "\n") if ($param::quiet < 2);
  186. }
  187. # Default environment.
  188. # This contains only the completely platform-independent information
  189. # we can figure out. Platform-specific information (UNIX, Win32)
  190. # gets added below.
  191. @param::base = (
  192. 'SIGNATURE' => ['*' => 'build'],
  193. 'SUFEXE' => $_exe, # '' on UNIX systems
  194. 'SUFLIB' => $_a, # '.a' on UNIX systems
  195. 'SUFLIBS' => "$_so:$_a", # '.so:.a' on UNIX
  196. 'SUFOBJ' => $_o, # '.o' on UNIX systems
  197. 'SUFMAP' => {
  198. '.c' => 'build::command::cc',
  199. '.s' => 'build::command::cc',
  200. '.S' => 'build::command::cc',
  201. '.C' => 'build::command::cxx',
  202. '.cc' => 'build::command::cxx',
  203. '.cxx' => 'build::command::cxx',
  204. '.cpp' => 'build::command::cxx',
  205. '.c++' => 'build::command::cxx',
  206. '.C++' => 'build::command::cxx',
  207. },
  208. 'PERL' => $^X,
  209. );
  210. # pcons does not allow multi-line commands
  211. my $ar_command = ($param::max_jobs <= 1)
  212. ? # pcons
  213. ['%AR %ARFLAGS %> %<', '%RANLIB %>']
  214. : # cons
  215. '%AR %ARFLAGS %> %< && %RANLIB %>'; # pcons
  216. %param::rulesets = (
  217. # Defaults for Win32.
  218. # Defined for VC++ 6.0 by Greg Spencer <greg_spencer@acm.org>
  219. # Your mileage may vary.
  220. 'msvc' => [
  221. 'CC' => 'cl',
  222. 'CFLAGS' => '/nologo',
  223. 'CCCOM' => '%CC %CFLAGS %_IFLAGS /c %< /Fo%>',
  224. 'CXX' => '%CC',
  225. 'CXXFLAGS' => '%CFLAGS',
  226. 'CXXCOM' => '%CXX %CXXFLAGS %_IFLAGS /c %< /Fo%>',
  227. 'INCDIRPREFIX' => '/I',
  228. 'INCDIRSUFFIX' => '',
  229. 'LINK' => 'link',
  230. 'LINKCOM' => '%LINK %LDFLAGS /out:%> %< %_LDIRS %LIBS',
  231. 'LINKMODULECOM' => '%LD /r /o %> %<',
  232. 'LIBDIRPREFIX' => '/LIBPATH:',
  233. 'LIBDIRSUFFIX' => '',
  234. 'AR' => 'lib',
  235. 'ARFLAGS' => '/nologo ',
  236. 'ARCOM' => "%AR %ARFLAGS /out:%> %<",
  237. 'RANLIB' => '',
  238. 'LD' => 'link',
  239. 'LDFLAGS' => '/nologo ',
  240. 'PREFLIB' => '',
  241. ],
  242. # Defaults for a typical (?) UNIX platform.
  243. # Your mileage may vary.
  244. 'unix' => [
  245. 'CC' => 'cc',
  246. 'CFLAGS' => '',
  247. 'CCCOM' => '%CC %CFLAGS %_IFLAGS -c %< -o %>',
  248. 'CXX' => '%CC',
  249. 'CXXFLAGS' => '%CFLAGS',
  250. 'CXXCOM' => '%CXX %CXXFLAGS %_IFLAGS -c %< -o %>',
  251. 'INCDIRPREFIX' => '-I',
  252. 'INCDIRSUFFIX' => '',
  253. 'LINK' => '%CXX',
  254. 'LINKCOM' => '%LINK %LDFLAGS -o %> %< %_LDIRS %LIBS',
  255. 'LINKMODULECOM' => '%LD -r -o %> %<',
  256. 'LIBDIRPREFIX' => '-L',
  257. 'LIBDIRSUFFIX' => '',
  258. 'AR' => 'ar',
  259. 'ARFLAGS' => 'r', # rs?
  260. 'ARCOM' => $ar_command, # pcons
  261. 'RANLIB' => 'ranlib',
  262. 'AS' => 'as',
  263. 'ASFLAGS' => '',
  264. 'ASCOM' => '%AS %ASFLAGS %< -o %>',
  265. 'LD' => 'ld',
  266. 'LDFLAGS' => '',
  267. 'PREFLIB' => 'lib',
  268. 'ENV' => {'PATH' => '/bin:/usr/bin'},
  269. ],
  270. );
  271. # Set the rules based on the platform.
  272. script::DefaultRules(script::RuleSet($_WIN32 ? 'msvc' : 'unix'));
  273. # Handle command line arguments.
  274. while (@ARGV)
  275. {
  276. $_ = shift @ARGV;
  277. last if /^--$/; # Argument passing to Construct.
  278. &option, next if s/^-//;
  279. push (@param::include, $_), next if s/^\+//;
  280. &equate, next if /=/;
  281. push (@targets, $_), next;
  282. }
  283. sub option
  284. {
  285. my %opt = (
  286. 'cc' => sub { $param::cachecom = 1; },
  287. 'cd' => sub { $param::cachedisable = 1; },
  288. 'cr' => sub { $param::random = 1; },
  289. 'cs' => sub { $param::cachesync = 1; },
  290. 'd' => sub { $param::depends = 1; },
  291. 'h' => sub { $param::localhelp = 1; },
  292. 'k' => sub { $param::kflag = 1; },
  293. 'p' => sub {
  294. $param::pflag = 1;
  295. $param::build = 0;
  296. },
  297. 'pa' => sub {
  298. $param::pflag = 1;
  299. $param::aflag = 1;
  300. $indent = "... ";
  301. $param::build = 0;
  302. },
  303. 'pw' => sub {
  304. $param::pflag = 1;
  305. $param::wflag = 1;
  306. $param::build = 0;
  307. },
  308. 'q' => sub { $param::quiet++; },
  309. 'r' => sub {
  310. $param::rflag = 1;
  311. $param::build = 0;
  312. },
  313. 't' => sub { $param::traverse = 1; },
  314. 'v' => sub { print($version); },
  315. 'V' => sub { print($version), exit(0); },
  316. 'x' => sub { print($usage), exit 0; },
  317. );
  318. my %opt_arg = (
  319. 'f' => sub { $param::topfile = $_[0]; },
  320. 'o' => sub { $param::overfile = $_[0]; },
  321. 'R' => sub { script::Repository($_[0]); },
  322. 'S' => sub { $param::sigpro = $_[0]; },
  323. 'wf' => sub { $param::depfile = $_[0]; },
  324. 'j' => sub { $param::max_jobs = $_[0]; }, # pcons
  325. );
  326. if (defined $opt{$_})
  327. {
  328. &{$opt{$_}} ();
  329. return;
  330. }
  331. while ($_)
  332. {
  333. $_ =~ m/(.)(.*)/;
  334. if (defined $opt{$1})
  335. {
  336. &{$opt{$1}} ();
  337. $_ = $2;
  338. next;
  339. }
  340. if (defined $opt_arg{$1})
  341. {
  342. if (!$2)
  343. {
  344. $_ = shift @ARGV;
  345. die ("$0: -$1 option requires an argument.\n") if !$_;
  346. }
  347. &{$opt_arg{$1}} ($2 || $_);
  348. return;
  349. }
  350. $_ =~ m/(..)(.*)/;
  351. if (defined $opt_arg{$1})
  352. {
  353. if (!$2)
  354. {
  355. $_ = shift @ARGV;
  356. die ("$0: -$1 option requires an argument.\n") if !$_;
  357. }
  358. &{$opt_arg{$1}} ($2 || $_);
  359. return;
  360. }
  361. if ($_)
  362. {
  363. die
  364. qq($0: unrecognized option "-$_". Use -x for a usage message.\n);
  365. }
  366. }
  367. }
  368. # Process an equate argument (var=val).
  369. sub equate
  370. {
  371. my ($var, $val) = /([^=]*)=(.*)/;
  372. $script::ARG{$var} = $val;
  373. }
  374. # Define file signature protocol.
  375. 'sig'->select($param::sigpro);
  376. # Cleanup after an interrupt.
  377. $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = sub {
  378. $SIG{PIPE} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'IGNORE';
  379. $SIG{HUP} = $SIG{INT} if !$main::_WIN32;
  380. warn("\n$0: killed\n");
  381. # Call this first, to make sure that this processing
  382. # occurs even if a child process does not die (and we
  383. # hang on the wait).
  384. sig::hash::END();
  385. wait();
  386. exit(1);
  387. };
  388. $SIG{HUP} = $SIG{INT} if !$main::_WIN32;
  389. # Cleanup after a broken pipe (someone piped our stdout?)
  390. $SIG{PIPE} = sub {
  391. $SIG{PIPE} = $SIG{HUP} = $SIG{INT} = $SIG{QUIT} = $SIG{TERM} = 'IGNORE';
  392. warn("\n$0: broken pipe\n");
  393. sig::hash::END();
  394. wait();
  395. exit(1);
  396. };
  397. if ($param::depfile)
  398. {
  399. open(main::DEPFILE, ">" . $param::depfile)
  400. || die ("$0: couldn't open $param::depfile ($!)\n");
  401. }
  402. # If the supplied top-level Conscript file is not in the
  403. # current directory, then change to that directory.
  404. {
  405. my ($vol, $dir, $file) =
  406. File::Spec->splitpath(File::Spec->canonpath($param::topfile));
  407. if ($vol || $dir)
  408. {
  409. my ($cd) = File::Spec->catpath($vol, $dir, undef);
  410. chdir($cd) || die ("$0: couldn't change to directory $cd ($!)\n");
  411. $param::topfile = $file;
  412. }
  413. }
  414. # Walk up the directory hierarchy looking for a Conscript file (if -t set).
  415. my ($target_top);
  416. my (@targetdir) = ();
  417. if ($param::traverse && !-f $param::topfile)
  418. {
  419. my ($vol, $dirs, $file) = File::Spec->splitpath(cwd());
  420. my (@dirs) = (File::Spec->splitdir($dirs), $file);
  421. while (
  422. !-f File::Spec->catpath($vol, File::Spec->catdir(@dirs),
  423. $param::topfile))
  424. {
  425. die ("$0: unable to find $param::topfile.\n") if !@dirs;
  426. unshift (@targetdir, pop (@dirs));
  427. }
  428. my ($cwd) = File::Spec->catpath($vol, File::Spec->catdir(@dirs), '');
  429. print "$0: Entering directory `$cwd'\n";
  430. chdir($cwd);
  431. @targets = map { File::Spec->catdir(@targetdir, $_) } @targets;
  432. }
  433. # Set up $dir::top and $dir::cwd, now that we are in the right directory.
  434. dir::init();
  435. #
  436. if (@targetdir)
  437. {
  438. $target_top = $dir::top->lookupdir(File::Spec->catdir(@targetdir));
  439. }
  440. # Now handle override file.
  441. package override;
  442. if ($param::overfile)
  443. {
  444. my ($ov) = $param::overfile;
  445. die qq($0: can\'t read override file "$ov" ($!)\n) if !-f $ov; #'
  446. do $ov;
  447. if ($@)
  448. {
  449. chop($@);
  450. die qq($0: errors in override file "$ov" ($@)\n);
  451. }
  452. }
  453. # Provide this to user to setup override patterns.
  454. sub Override
  455. {
  456. my ($re, @env) = @_;
  457. return if $param::overrides{$re}; # if identical, first will win.
  458. $param::overrides = 1;
  459. $param::overrides{$re} = \@env;
  460. push (@param::overrides, $re);
  461. }
  462. package main;
  463. use vars qw( %priority $errors );
  464. # Check script inclusion regexps
  465. my $re;
  466. for $re (@param::include)
  467. {
  468. if (!defined eval { "" =~ /$re/ })
  469. {
  470. my ($err) = $@;
  471. $err =~ s/in regexp at .*$//;
  472. die ("$0: error in regexp $err");
  473. }
  474. }
  475. # Read the top-level construct file and its included scripts.
  476. doscripts($param::topfile);
  477. # Status priorities. This lets us aggregate status for directories
  478. # and print an appropriate message (at the top-level).
  479. %priority =
  480. ('none' => 1, 'handled' => 2, 'built' => 3, 'unknown' => 4, 'errors' => 5);
  481. # If no targets were specified, supply default targets (if any).
  482. @targets = @param::default_targets if !@targets;
  483. $errors = 0;
  484. # Build the supplied target patterns.
  485. my $tgt;
  486. for $tgt (map($dir::top->lookup($_), @targets))
  487. {
  488. if ($target_top && !$tgt->is_under($target_top))
  489. {
  490. # A -t option was used, and this target is not underneath
  491. # the directory where we were invoked via -t.
  492. # If the target is a directory and the -t directory
  493. # is underneath it, then build the -t directory.
  494. if (ref $tgt ne "dir" || !$target_top->is_under($tgt))
  495. {
  496. next;
  497. }
  498. $tgt = $target_top;
  499. }
  500. buildtoptarget($tgt);
  501. }
  502. exit 0 + ($errors != 0);
  503. sub buildtoptarget
  504. {
  505. my ($tgt) = @_;
  506. return if !$tgt;
  507. my ($status) = buildtarget($tgt);
  508. if ($status ne 'built')
  509. {
  510. my ($path) = $tgt->path;
  511. if ($status eq "errors")
  512. {
  513. print qq($0: "$path" not remade because of errors.\n);
  514. $errors++;
  515. }
  516. elsif ($status eq "handled")
  517. {
  518. print qq($0: "$path" is up-to-date.\n) if ($param::quiet < 2);
  519. }
  520. elsif ($status eq "unknown")
  521. {
  522. # cons error already reported.
  523. $errors++;
  524. }
  525. elsif ($status eq "none")
  526. {
  527. # search for targets that may be linked to the given path.
  528. my @linked = dir::linked_targets($tgt) if $target_top;
  529. if (@linked)
  530. {
  531. my @names = map($_->path, @linked);
  532. print "Linked targets: @names\n" if ($param::quiet < 1);
  533. map(buildtoptarget($_), @linked);
  534. }
  535. else
  536. {
  537. print qq($0: nothing to be built in "$path".\n)
  538. if $param::build && ($param::quiet < 2);
  539. }
  540. }
  541. else
  542. {
  543. print qq($0: don\'t know how to construct "$path".\n); #'
  544. $errors++;
  545. }
  546. }
  547. }
  548. # Build the supplied target directory or files. Return aggregated status.
  549. sub buildtarget
  550. {
  551. my ($tgt) = @_;
  552. if (ref($tgt) eq "dir")
  553. {
  554. my ($result) = "none";
  555. my ($priority) = $priority{$result};
  556. if (exists $tgt->{member})
  557. {
  558. my ($members) = $tgt->{member};
  559. my $entry;
  560. for $entry (sort keys %$members)
  561. {
  562. next if $entry eq $dir::CURDIR || $entry eq $dir::UPDIR;
  563. my ($tgt) = $members->{$entry};
  564. next if ref($tgt) ne "dir" && !exists($tgt->{builder});
  565. my ($stat) = buildtarget($members->{$entry});
  566. my ($pri) = $priority{$stat};
  567. if ($pri > $priority)
  568. {
  569. $priority = $pri;
  570. $result = $stat;
  571. }
  572. }
  573. }
  574. return $result;
  575. }
  576. if ($param::depends)
  577. {
  578. my ($path) = $tgt->path;
  579. if ($tgt->{builder})
  580. {
  581. my (@dep) = (@{$tgt->{dep}}, @{$tgt->{sources}});
  582. my ($dep) = join (' ', map($_->path, @dep));
  583. print("Target $path: $dep\n");
  584. }
  585. else
  586. {
  587. print("Target $path: not a derived file\n");
  588. }
  589. }
  590. if ($param::build)
  591. {
  592. return build $tgt;
  593. }
  594. elsif ($param::pflag || $param::wflag || $param::aflag)
  595. {
  596. if ($tgt->{builder})
  597. {
  598. if ($param::wflag)
  599. {
  600. print qq(${\$tgt->path}: $tgt->{script}\n);
  601. }
  602. elsif ($param::pflag)
  603. {
  604. print qq(${\$tgt->path}:\n) if $param::aflag;
  605. print qq(${\$tgt->path}\n) if !$param::aflag;
  606. }
  607. if ($param::aflag)
  608. {
  609. $tgt->{builder}->action($tgt);
  610. }
  611. }
  612. }
  613. elsif ($param::rflag && $tgt->{builder})
  614. {
  615. my ($path) = $tgt->path;
  616. if (-f $path)
  617. {
  618. if (unlink($path))
  619. {
  620. print("Removed $path\n") if ($param::quiet < 1);
  621. }
  622. else
  623. {
  624. warn("$0: couldn't remove $path\n");
  625. }
  626. }
  627. }
  628. return "none";
  629. }
  630. package NameSpace;
  631. # Return a hash that maps the name of symbols in a namespace to an
  632. # array of refs for all types for which the name has a defined value.
  633. # A list of symbols may be specified; default is all symbols in the
  634. # name space.
  635. sub save
  636. {
  637. my $package = shift;
  638. my (%namerefs, $var, $type);
  639. no strict 'refs';
  640. @_ = keys %{$package . "::"} if !@_;
  641. foreach $var (@_)
  642. {
  643. $namerefs{$var} = [];
  644. my $fqvar = $package . "::" . $var;
  645. # If the scalar for this variable name doesn't already
  646. # exist, *foo{SCALAR} will autovivify the reference
  647. # instead of returning undef, so unlike the other types,
  648. # we have to dereference to find out if it exists.
  649. push (@{$namerefs{$var}}, *{$fqvar}{SCALAR})
  650. if defined ${*{$fqvar}{SCALAR}};
  651. foreach $type (qw(ARRAY HASH CODE IO))
  652. {
  653. push (@{$namerefs{$var}}, *{$fqvar}{$type})
  654. if defined *{$fqvar}{$type};
  655. }
  656. }
  657. return \%namerefs;
  658. }
  659. # Remove the specified symbols from the namespace.
  660. # Default is to remove all.
  661. sub remove
  662. {
  663. my $package = shift;
  664. my (%namerefs, $var);
  665. no strict 'refs';
  666. @_ = keys %{$package . "::"} if !@_;
  667. foreach $var (@_)
  668. {
  669. delete ${$package . "::"}{$var};
  670. }
  671. }
  672. # Restore values to symbols specified in a hash as returned
  673. # by NameSpace::save.
  674. sub restore
  675. {
  676. my ($package, $namerefs) = @_;
  677. my ($var, $ref);
  678. no strict 'refs';
  679. foreach $var (keys %$namerefs)
  680. {
  681. my $fqvar = $package . "::" . $var;
  682. foreach $ref (@{$namerefs->{$var}})
  683. {
  684. *{$fqvar} = $ref;
  685. }
  686. }
  687. }
  688. # Support for "building" scripts, importing and exporting variables.
  689. # With the exception of the top-level routine here (invoked from the
  690. # main package by cons), these are all invoked by user scripts.
  691. package script;
  692. use vars qw( $ARG $caller_dir_path %special_var );
  693. BEGIN
  694. {
  695. # We can't Export or Import the following variables because Perl always
  696. # treats them as part of the "main::" package (see perlvar(1)).
  697. %special_var = map { $_ => 1 } qw(ENV INC ARGV ARGVOUT SIG
  698. STDIN STDOUT STDERR);
  699. }
  700. # This is called from main to interpret/run the top-level Construct
  701. # file, passed in as the single argument.
  702. sub main::doscripts
  703. {
  704. my ($script) = @_;
  705. Build($script);
  706. # Now set up the includes/excludes (after the Construct file is read).
  707. $param::include = join ('|', @param::include);
  708. # Save the original variable names from the script package.
  709. # These will stay intact, but any other "script::" variables
  710. # defined in a Conscript file will get saved, deleted,
  711. # and (when necessary) restored.
  712. my (%orig_script_var) = map { $_ => 1 } keys %script::;
  713. $caller_dir_path = undef;
  714. my $cwd = Cwd::cwd();
  715. my (@scripts) = pop (@priv::scripts);
  716. while ($priv::self = shift (@scripts))
  717. {
  718. my ($path) = $priv::self->{script}->rsrcpath;
  719. if (-f $path)
  720. {
  721. $dir::cwd = $priv::self->{script}->{dir};
  722. # Handle chdir to the Conscript file directory, if necessary.
  723. my ($vol, $dir, $file);
  724. if ($param::conscript_chdir)
  725. {
  726. ($vol, $dir, $file) =
  727. File::Spec->splitpath(File::Spec->canonpath($path));
  728. if ($vol ne '' || $dir ne '')
  729. {
  730. $caller_dir_path = File::Spec->catpath($vol, $dir, undef);
  731. chdir($caller_dir_path)
  732. || die "Could not chdir to $caller_dir_path: $!\n";
  733. }
  734. }
  735. else
  736. {
  737. $file = $path;
  738. }
  739. # Actually process the Conscript file.
  740. do $file;
  741. # Save any variables defined by the Conscript file
  742. # so we can restore them later, if needed;
  743. # then delete them from the script:: namespace.
  744. my (@del) = grep(!$orig_script_var{$_}, keys %script::);
  745. if (@del)
  746. {
  747. $priv::self->{script}->{pkgvars} =
  748. NameSpace::save('script', @del);
  749. NameSpace::remove('script', @del);
  750. }
  751. if ($caller_dir_path)
  752. {
  753. chdir($cwd);
  754. $caller_dir_path = undef;
  755. }
  756. if ($@)
  757. {
  758. chomp($@);
  759. my $err = ($@ =~ /\n/ms) ? ":\n$@" : " ($@)";
  760. print qq($0: error in file "$path"$err\n);
  761. $run::errors++;
  762. }
  763. else
  764. {
  765. # Only process subsidiary scripts if no errors in parent.
  766. unshift (@scripts, @priv::scripts);
  767. }
  768. undef @priv::scripts;
  769. }
  770. else
  771. {
  772. my $where = '';
  773. my $cref = $priv::self->{script}->creator;
  774. if (defined $cref)
  775. {
  776. my ($_foo, $script, $line, $sub) = @$cref;
  777. $where = " ($sub in $script, line $line)";
  778. }
  779. warn qq(Ignoring missing script "$path"$where);
  780. }
  781. }
  782. die ("$0: script errors encountered: construction aborted\n")
  783. if $run::errors;
  784. }
  785. # Return caller info about the method being invoked.
  786. # This is everything from the Perl "caller" builtin function,
  787. # including which Construct/Conscript file, line number,
  788. # subroutine name, etc.
  789. sub caller_info
  790. {
  791. my ($lev) = 1;
  792. my (@frame);
  793. do
  794. {
  795. @frame = caller ++$lev;
  796. if (defined($frame[3]) && $frame[3] eq '(eval)')
  797. {
  798. @frame = caller --$lev;
  799. if ($caller_dir_path)
  800. {
  801. $frame[1] = File::Spec->catfile($caller_dir_path, $frame[1]);
  802. }
  803. return @frame;
  804. }
  805. } while ($frame[3]);
  806. return;
  807. }
  808. # Link a directory to another. This simply means set up the *source*
  809. # for the directory to be the other directory.
  810. sub Link
  811. {
  812. dir::link(@_);
  813. }
  814. # Add directories to the repository search path for files.
  815. # Strip our current directory from the list so Repository
  816. # (or -R options) can be used from within the repository.
  817. sub Repository
  818. {
  819. my ($my_dir) = Cwd::cwd();
  820. my $dir;
  821. foreach $dir (@_)
  822. {
  823. # The following more direct call isn't available in
  824. # Cwd.pm until some time after 5.003...
  825. # my($d) = Cwd::abs_path($dir);
  826. chdir($dir);
  827. my ($d) = Cwd::cwd();
  828. chdir($my_dir);
  829. #
  830. next if !$d || !-d $d || $d eq $my_dir;
  831. # We know we can get away with passing undef to lookupdir
  832. # as the directory because $dir is an absolute path.
  833. push (@param::rpath, dir::lookupdir(undef, $dir));
  834. push @INC, $d;
  835. }
  836. }
  837. # Return the list of Repository directories specified.
  838. sub Repository_List
  839. {
  840. map($_->path, @param::rpath);
  841. }
  842. # Specify whether the .consign signature times in repository files are,
  843. # in fact, consistent with the times on the files themselves.
  844. sub Repository_Sig_Times_OK
  845. {
  846. $param::rep_sig_times_ok = shift;
  847. }
  848. sub SourceSignature
  849. {
  850. $param::sourcesig = [@_];
  851. }
  852. # Specify whether we should chdir to the containing directories
  853. # of Conscript files.
  854. sub Conscript_chdir
  855. {
  856. $param::conscript_chdir = shift;
  857. }
  858. # Specify files/targets that must be present and built locally,
  859. # even if they exist already-built in a Repository.
  860. sub Local
  861. {
  862. my (@files) = map($dir::cwd->lookupfile($_), @_);
  863. map($_->local(1), @files);
  864. }
  865. # Export variables to any scripts invoked from this one.
  866. sub Export
  867. {
  868. my (@illegal) = grep($special_var{$_}, @_);
  869. if (@illegal)
  870. {
  871. die qq($0: cannot Export special Perl variables: @illegal\n);
  872. }
  873. @{$priv::self->{exports}} = grep(!defined $special_var{$_}, @_);
  874. }
  875. # Import variables from the export list of the caller
  876. # of the current script.
  877. sub Import
  878. {
  879. my (@illegal) = grep($special_var{$_}, @_);
  880. if (@illegal)
  881. {
  882. die qq($0: cannot Import special Perl variables: @illegal\n);
  883. }
  884. my ($parent) = $priv::self->{parent};
  885. my ($imports) = $priv::self->{imports};
  886. @{$priv::self->{exports}} = keys %$imports;
  887. my ($var);
  888. foreach $var (grep(!defined $special_var{$_}, @_))
  889. {
  890. if (!exists $imports->{$var})
  891. {
  892. my ($path) = $parent->{script}->path;
  893. die qq($0: variable "$var" not exported by file "$path"\n);
  894. }
  895. if (!defined $imports->{$var})
  896. {
  897. my $path = $parent->{script}->path;
  898. my $err =
  899. "$0: variable \"$var\" exported but not "
  900. . "defined by file \"$path\"\n";
  901. die $err;
  902. }
  903. ${"script::$var"} = $imports->{$var};
  904. }
  905. }
  906. # Build an inferior script. That is, arrange to read and execute
  907. # the specified script, passing to it any exported variables from
  908. # the current script.
  909. sub Build
  910. {
  911. my (@files) = map($dir::cwd->lookupfile($_), @_);
  912. my (%imports) = map { $_ => ${"script::$_"} } @{$priv::self->{exports}};
  913. my $file;
  914. for $file (@files)
  915. {
  916. next if $param::include && $file->path !~ /$param::include/o;
  917. my ($self) = {
  918. 'script' => $file,
  919. 'parent' => $priv::self,
  920. 'imports' => \%imports
  921. };
  922. bless $self; # may want to bless into class of parent in future
  923. push (@priv::scripts, $self);
  924. }
  925. }
  926. # Set up regexps dependencies to ignore. Should only be called once.
  927. sub Ignore
  928. {
  929. die ("Ignore called more than once\n") if $param::ignore;
  930. $param::ignore = join ("|", map("($_)", @_)) if @_;
  931. }
  932. # Specification of default targets.
  933. sub Default
  934. {
  935. push (@param::default_targets, map($dir::cwd->lookup($_)->path, @_));
  936. }
  937. # Local Help. Should only be called once.
  938. sub Help
  939. {
  940. if ($param::localhelp)
  941. {
  942. print "@_\n";
  943. exit 2;
  944. }
  945. }
  946. # For windows platforms which use unix tool sets, the msvc defaults may
  947. # not be useful. Also, in the future, other platforms (Mac?) may have the
  948. # same problem.
  949. sub RuleSet
  950. {
  951. my $style = shift;
  952. my @rulesets = sort keys %param::rulesets;
  953. die "Unknown style for rules: $style.\n"
  954. . "Supported rules are: ("
  955. . join (" ", @rulesets) . ")"
  956. unless eval(join ("||", map("\$style eq '$_'", @rulesets)));
  957. return @param::base, @{$param::rulesets{$style}};
  958. }
  959. sub DefaultRules
  960. {
  961. @param::defaults = ();
  962. push @param::defaults, @_;
  963. }
  964. # Return the build name(s) of a file or file list.
  965. sub FilePath
  966. {
  967. wantarray
  968. ? map($dir::cwd->lookupfile($_)->path, @_)
  969. : $dir::cwd->lookupfile($_[0])->path;
  970. }
  971. # Return the build name(s) of a directory or directory list.
  972. sub DirPath
  973. {
  974. wantarray
  975. ? map($dir::cwd->lookupdir($_)->path, @_)
  976. : $dir::cwd->lookupdir($_[0])->path;
  977. }
  978. # Split the search path provided into components. Look each up
  979. # relative to the current directory.
  980. # The usual path separator problems abound; for now we'll use :
  981. sub SplitPath
  982. {
  983. my ($dirs) = @_;
  984. if (ref($dirs) ne "ARRAY")
  985. {
  986. $dirs = [split (/$main::PATH_SEPARATOR/o, $dirs)];
  987. }
  988. map { DirPath($_) } @$dirs;
  989. }
  990. # Return true if the supplied path is available as a source file
  991. # or is buildable (by rules seen to-date in the build).
  992. sub ConsPath
  993. {
  994. my ($path) = @_;
  995. my ($file) = $dir::cwd->lookup($path);
  996. return $file->accessible;
  997. }
  998. # Return the source path of the supplied path.
  999. sub SourcePath
  1000. {
  1001. wantarray
  1002. ? map($dir::cwd->lookupfile($_)->rsrcpath, @_)
  1003. : $dir::cwd->lookupfile($_[0])->rsrcpath;
  1004. }
  1005. # Search up the tree for the specified cache directory, starting with
  1006. # the current directory. Returns undef if not found, 1 otherwise.
  1007. # If the directory is found, then caching is enabled. The directory
  1008. # must be readable and writable. If the argument "mixtargets" is provided,
  1009. # then targets may be mixed in the cache (two targets may share the same
  1010. # cache file--not recommended).
  1011. sub UseCache($@)
  1012. {
  1013. my ($dir, @args) = @_;
  1014. # NOTE: it's important to process arguments here regardless of whether
  1015. # the cache is disabled temporarily, since the mixtargets option affects
  1016. # the salt for derived signatures.
  1017. for (@args)
  1018. {
  1019. if ($_ eq "mixtargets")
  1020. {
  1021. # When mixtargets is enabled, we salt the target signatures.
  1022. # This is done purely to avoid a scenario whereby if
  1023. # mixtargets is turned on or off after doing builds, and
  1024. # if cache synchronization with -cs is used, then
  1025. # cache files may be shared in the cache itself (linked
  1026. # under more than one name in the cache). This is not bad,
  1027. # per se, but simply would mean that a cache cleaning algorithm
  1028. # that looked for a link count of 1 would never find those
  1029. # particular files; they would always appear to be in use.
  1030. $param::salt = 'M' . $param::salt;
  1031. $param::mixtargets = 1;
  1032. }
  1033. else
  1034. {
  1035. die qq($0: UseCache unrecognized option "$_"\n);
  1036. }
  1037. }
  1038. if ($param::cachedisable)
  1039. {
  1040. warn("Note: caching disabled by -cd flag\n");
  1041. return 1;
  1042. }
  1043. my ($depth) = 15;
  1044. while ($depth-- && !-d $dir)
  1045. {
  1046. $dir = File::Spec->catdir($dir::UPDIR, $dir);
  1047. }
  1048. if (-d $dir)
  1049. {
  1050. $param::cache = $dir;
  1051. return 1;
  1052. }
  1053. return undef;
  1054. }
  1055. # Salt the signature generator. The salt (a number of string) is added
  1056. # into the signature of each derived file. Changing the salt will
  1057. # force recompilation of all derived files.
  1058. sub Salt($)
  1059. {
  1060. # We append the value, so that UseCache and Salt may be used
  1061. # in either order without changing the signature calculation.
  1062. $param::salt .= $_[0];
  1063. }
  1064. # Mark files (or directories) to not be removed before building.
  1065. sub Precious
  1066. {
  1067. map($_->{precious} = 1, map($dir::cwd->lookup($_), @_));
  1068. }
  1069. # These methods are callable from Conscript files, via a cons
  1070. # object. Procs beginning with _ are intended for internal use.
  1071. package cons;
  1072. use vars qw( %envcache );
  1073. # This is passed the name of the base environment to instantiate.
  1074. # Overrides to the base environment may also be passed in
  1075. # as key/value pairs.
  1076. sub new
  1077. {
  1078. my ($package) = shift;
  1079. my ($env) = {@param::defaults, @_};
  1080. @{$env->{_envcopy}} = %$env; # Note: we never change PATH
  1081. $env->{_cwd} = $dir::cwd; # Save directory of environment for
  1082. bless $env, $package; # any deferred name interpretation.
  1083. }
  1084. # Clone an environment.
  1085. # Note that the working directory will be the initial directory
  1086. # of the original environment.
  1087. sub clone
  1088. {
  1089. my ($env) = shift;
  1090. my $clone = {@{$env->{_envcopy}}, @_};
  1091. @{$clone->{_envcopy}} = %$clone; # Note: we never change PATH
  1092. $clone->{_cwd} = $env->{_cwd};
  1093. bless $clone, ref $env;
  1094. }
  1095. # Create a flattened hash representing the environment.
  1096. # It also contains a copy of the PATH, so that the path
  1097. # may be modified if it is converted back to a hash.
  1098. sub copy
  1099. {
  1100. my ($env) = shift;
  1101. (@{$env->{_envcopy}}, 'ENV' => {%{$env->{ENV}}}, @_);
  1102. }
  1103. # Resolve which environment to actually use for a given
  1104. # target. This is just used for simple overrides.
  1105. sub _resolve
  1106. {
  1107. return $_[0] if !$param::overrides;
  1108. my ($env, $tgt) = @_;
  1109. my ($path) = $tgt->path;
  1110. my $re;
  1111. for $re (@param::overrides)
  1112. {
  1113. next if $path !~ /$re/;
  1114. # Found one. Return a combination of the original environment
  1115. # and the override.
  1116. my ($ovr) = $param::overrides{$re};
  1117. return $envcache{$env, $re} if $envcache{$env, $re};
  1118. my ($newenv) = {@{$env->{_envcopy}}, @$ovr};
  1119. @{$newenv->{_envcopy}} = %$env;
  1120. $newenv->{_cwd} = $env->{_cwd};
  1121. return $envcache{$env, $re} = bless $newenv, ref $env;
  1122. }
  1123. return $env;
  1124. }
  1125. # Substitute construction environment variables into a string.
  1126. # Internal function/method.
  1127. sub _subst
  1128. {
  1129. my ($env, $str) = @_;
  1130. if (!defined $str)
  1131. {
  1132. return undef;
  1133. }
  1134. elsif (ref($str) eq "ARRAY")
  1135. {
  1136. return [map($env->_subst($_), @$str)];
  1137. }
  1138. else
  1139. {
  1140. # % expansion. %% gets converted to % later, so expand any
  1141. # %keyword construction that doesn't have a % in front of it,
  1142. # modulo multiple %% pairs in between.
  1143. # In Perl 5.005 and later, we could actually do this in one regex
  1144. # using a conditional expression as follows,
  1145. # while ($str =~ s/($pre)\%(\{)?([_a-zA-Z]\w*)(?(2)\})/"$1".
  1146. # $env->{$3}/ge) {}
  1147. # The following two-step approach is backwards-compatible
  1148. # to (at least) Perl5.003.
  1149. my $pre = '^|[^\%](?:\%\%)*';
  1150. while (($str =~ s/($pre)\%([_a-zA-Z]\w*)/$1.($env->{$2}||'')/ge)
  1151. || ($str =~ s/($pre)\%\{([_a-zA-Z]\w*)\}/$1.($env->{$2}||'')/ge))
  1152. {
  1153. }
  1154. return $str;
  1155. }
  1156. }
  1157. sub AfterBuild
  1158. {
  1159. my ($env) = shift;
  1160. my ($perl_eval_str) = pop (@_);
  1161. my $file;
  1162. for $file (map($dir::cwd->lookup($_), @_))
  1163. {
  1164. $file->{after_build_func} = $perl_eval_str;
  1165. }
  1166. }
  1167. sub Install
  1168. {
  1169. my ($env) = shift;
  1170. my ($tgtdir) = $dir::cwd->lookupdir($env->_subst(shift));
  1171. my $file;
  1172. for $file (map($dir::cwd->lookupfile($env->_subst($_)), @_))
  1173. {
  1174. my ($tgt) = $tgtdir->lookupfile($file->{entry});
  1175. $tgt->bind(find build::install($env), $file);
  1176. }
  1177. }
  1178. sub InstallAs
  1179. {
  1180. my $env = shift;
  1181. my $tgt = shift;
  1182. my $src = shift;
  1183. my @sources = ();
  1184. my @targets = ();
  1185. if (ref $tgt)
  1186. {
  1187. die "InstallAs: Source is a file and target is a list!\n"
  1188. if (!ref($src));
  1189. @sources = @$src;
  1190. @targets = @$tgt;
  1191. }
  1192. elsif (ref $src)
  1193. {
  1194. die "InstallAs: Target is a file and source is a list!\n";
  1195. }
  1196. else
  1197. {
  1198. push @sources, $src;
  1199. push @targets, $tgt;
  1200. }
  1201. if ($#sources != $#targets)
  1202. {
  1203. my $tn = $#targets + 1;
  1204. my $sn = $#sources + 1;
  1205. die "InstallAs: Source file list ($sn) and target file list ($tn) "
  1206. . "are inconsistent in length!\n";
  1207. }
  1208. else
  1209. {
  1210. foreach (0 .. $#sources)
  1211. {
  1212. my $tfile = $dir::cwd->lookupfile($env->_subst($targets[$_]));
  1213. my $sfile = $dir::cwd->lookupfile($env->_subst($sources[$_]));
  1214. $tfile->bind(find build::install($env), $sfile);
  1215. }
  1216. }
  1217. }
  1218. # Installation in a local build directory,
  1219. # copying from the repository if it's already built there.
  1220. # Functionally equivalent to:
  1221. # Install $env $dir, $file;
  1222. # Local "$dir/$file";
  1223. sub Install_Local
  1224. {
  1225. my ($env) = shift;
  1226. my ($tgtdir) = $dir::cwd->lookupdir($env->_subst(shift));
  1227. my $file;
  1228. for $file (map($dir::cwd->lookupfile($env->_subst($_)), @_))
  1229. {
  1230. my ($tgt) = $tgtdir->lookupfile($file->{entry});
  1231. $tgt->bind(find build::install($env), $file);
  1232. $tgt->local(1);
  1233. }
  1234. }
  1235. sub Objects
  1236. {
  1237. my ($env) = shift;
  1238. map($dir::cwd->relpath($_), $env->_Objects(@_));
  1239. }
  1240. # Called with multiple source file references (or object files).
  1241. # Returns corresponding object files references.
  1242. sub _Objects
  1243. {
  1244. my ($env) = shift;
  1245. my ($suffix) = $env->{SUFOBJ};
  1246. map($env->_Object($_, $_->{dir}->lookupfile($_->base_suf($suffix))),
  1247. map { ref $_ ? $_ : $dir::cwd->lookupfile($env->_subst($_)) }
  1248. grep(defined $_, @_));
  1249. }
  1250. # Called with an object and source reference. If no object reference
  1251. # is supplied, then the object file is determined implicitly from the
  1252. # source file's extension. Sets up the appropriate rules for creating
  1253. # the object from the source. Returns the object reference.
  1254. sub _Object
  1255. {
  1256. my ($env, $src, $obj) = @_;
  1257. return $obj if $src eq $obj; # don't need to build self from self.
  1258. my ($objenv) = $env->_resolve($obj);
  1259. my ($suffix) = $src->suffix;
  1260. my ($builder) = $env->{SUFMAP}{$suffix};
  1261. if ($builder)
  1262. {
  1263. $obj->bind((find $builder($objenv)), $src);
  1264. }
  1265. else
  1266. {
  1267. die ("don't know how to construct ${\$obj->path} from "
  1268. . "${\$src->path}.\n");
  1269. }
  1270. $obj;
  1271. }
  1272. sub Program
  1273. {
  1274. my ($env) = shift;
  1275. my ($tgt) =
  1276. $dir::cwd->lookupfile(
  1277. file::addsuffix($env->_subst(shift), $env->{SUFEXE}));
  1278. my ($progenv) = $env->_resolve($tgt);
  1279. $tgt->bind(find build::command::link($progenv, $progenv->{LINKCOM}),
  1280. $env->_Objects(@_));
  1281. }
  1282. sub Module
  1283. {
  1284. my ($env) = shift;
  1285. my ($tgt) = $dir::cwd->lookupfile($env->_subst(shift));
  1286. my ($modenv) = $env->_resolve($tgt);
  1287. my ($com) = pop (@_);
  1288. $tgt->bind(find build::command::link($modenv, $com), $env->_Objects(@_));
  1289. }
  1290. sub LinkedModule
  1291. {
  1292. my ($env) = shift;
  1293. my ($tgt) = $dir::cwd->lookupfile($env->_subst(shift));
  1294. my ($progenv) = $env->_resolve($tgt);
  1295. $tgt->bind(
  1296. find build::command::linkedmodule($progenv,
  1297. $progenv->{LINKMODULECOM}),
  1298. $env->_Objects(@_));
  1299. }
  1300. sub Library
  1301. {
  1302. my ($env) = shift;
  1303. my ($lib) =
  1304. $dir::cwd->lookupfile(
  1305. file::addsuffix($env->_subst(shift), $env->{SUFLIB}));
  1306. my ($libenv) = $env->_resolve($lib);
  1307. $lib->bind(find build::command::library($libenv), $env->_Objects(@_));
  1308. }
  1309. # Simple derivation: you provide target, source(s), command.
  1310. # Special variables substitute into the rule.
  1311. # Target may be a reference, in which case it is taken
  1312. # to be a multiple target (all targets built at once).
  1313. sub Command
  1314. {
  1315. my ($env) = shift;
  1316. my ($tgt) = $env->_subst(shift);
  1317. my ($builder) = find build::command::user($env, pop (@_), 'script');
  1318. my (@sources) = map($dir::cwd->lookupfile($env->_subst($_)), @_);
  1319. if (ref($tgt))
  1320. {
  1321. # A multi-target command.
  1322. my (@tgts) = map($dir::cwd->lookupfile($_), @$tgt);
  1323. die ("empty target list in multi-target command\n") if !@tgts;
  1324. $env = $env->_resolve($tgts[0]);
  1325. my ($multi) = build::multiple->new($builder, \@tgts);
  1326. for $tgt (@tgts)
  1327. {
  1328. $tgt->bind($multi, @sources);
  1329. }
  1330. }
  1331. else
  1332. {
  1333. $tgt = $dir::cwd->lookupfile($tgt);
  1334. $env = $env->_resolve($tgt);
  1335. $tgt->bind($builder, @sources);
  1336. }
  1337. }
  1338. sub Depends
  1339. {
  1340. my ($env) = shift;
  1341. my ($tgt) = $env->_subst(shift);
  1342. my (@deps) = map($dir::cwd->lookup($env->_subst($_)), @_);
  1343. if (!ref($tgt))
  1344. {
  1345. $tgt = [$tgt];
  1346. }
  1347. my ($t);
  1348. foreach $t (map($dir::cwd->lookupfile($_), @$tgt))
  1349. {
  1350. push (@{$t->{dep}}, @deps);
  1351. }
  1352. }
  1353. # Setup a quick scanner for the specified input file, for the
  1354. # associated environment. Any use of the input file will cause the
  1355. # scanner to be invoked, once only. The scanner sees just one line at
  1356. # a time of the file, and is expected to return a list of
  1357. # dependencies.
  1358. sub QuickScan
  1359. {
  1360. my ($env, $code, $file, $path) = @_;
  1361. $dir::cwd->lookup($env->_subst($file))->{'srcscan', $env} =
  1362. find scan::quickscan($code, $env, $env->_subst($path));
  1363. }
  1364. # Generic builder module. Just a few default methods. Every derivable
  1365. # file must have a builder object of some sort attached. Usually
  1366. # builder objects are shared.
  1367. package build;
  1368. use vars qw( %builder );
  1369. # Every builder must now have at least an associated environment,
  1370. # so we can find its sigarray and calculate the proper signature.
  1371. sub find
  1372. {
  1373. my ($class, $env) = @_;
  1374. $builder{$env} || do
  1375. {
  1376. my $self = {env => $env};
  1377. $builder{$env} = bless $self, $class;
  1378. }
  1379. }
  1380. # Null signature for dynamic includes.
  1381. sub includes { () }
  1382. # Null signature for build script.
  1383. sub scriptsig { () }
  1384. # Not compatible with any other builder, by default.
  1385. sub compatible { 0 }
  1386. # Builder module for the Install command.
  1387. package build::install;
  1388. use vars qw( @ISA );
  1389. BEGIN { @ISA = qw(build) }
  1390. # Caching not supported for Install: generally install is trivial anyway,
  1391. # and we don't want to clutter the cache.
  1392. sub cachin { undef }
  1393. sub cachout { }
  1394. # Do the installation.
  1395. sub action
  1396. {
  1397. my ($self, $tgt) = @_;
  1398. my ($src) = $tgt->{sources}[0];
  1399. main::showcom("Install ${\$src->rpath} as ${\$tgt->path}")
  1400. if ($param::install && $param::quiet < 1);
  1401. return unless $param::build;
  1402. futil::install($src->rpath, $tgt);
  1403. return 1;
  1404. }
  1405. # Builder module for generic UNIX commands.
  1406. package build::command;
  1407. use vars qw( @ISA %com );
  1408. BEGIN { @ISA = qw(build) }
  1409. sub find
  1410. {
  1411. my ($class, $env, $cmd, $package) = @_;
  1412. my ($act) = action::new($env, $cmd);
  1413. $package ||= '';
  1414. $com{$env, $act, $package} || do
  1415. {
  1416. my $self = {env => $env, act => $act, 'package' => $package};
  1417. $com{$env, $act, $package} = bless $self, $class;
  1418. }
  1419. }
  1420. # Default cache in function.
  1421. sub cachin
  1422. {
  1423. my ($self, $tgt, $sig) = @_;
  1424. if (cache::in($tgt, $sig))
  1425. {
  1426. if ($param::cachecom)
  1427. {
  1428. $self->{act}->show($self->{env}, $tgt);
  1429. }
  1430. else
  1431. {
  1432. printf("Retrieved %s from cache\n", $tgt->path)
  1433. if ($param::quiet < 1);
  1434. }
  1435. return 1;
  1436. }
  1437. return undef;
  1438. }
  1439. # Default cache out function.
  1440. sub cachout
  1441. {
  1442. my ($self, $tgt, $sig) = @_;
  1443. cache::out($tgt, $sig);
  1444. }
  1445. # Build the target using the previously specified commands.
  1446. sub action
  1447. {
  1448. my ($self, $tgt) = @_;
  1449. $self->{act}->execute($self->{env}, $tgt, $self->{'package'});
  1450. }
  1451. # Return script signature.
  1452. sub scriptsig
  1453. {
  1454. $_[0]->{act}->scriptsig;
  1455. }
  1456. # Create a linked module.
  1457. package build::command::link;
  1458. use vars qw( @ISA );
  1459. BEGIN { @ISA = qw(build::command) }
  1460. # Find an appropriate linker.
  1461. sub find
  1462. {
  1463. my ($class, $env, $command) = @_;
  1464. if (!exists $env->{_LDIRS})
  1465. {
  1466. my ($ldirs) = '';
  1467. my ($wd) = $env->{_cwd};
  1468. my ($pdirs) = $env->{LIBPATH};
  1469. if (!defined $pdirs)
  1470. {
  1471. $pdirs = [];
  1472. }
  1473. elsif (ref($pdirs) ne 'ARRAY')
  1474. {
  1475. $pdirs = [split (/$main::PATH_SEPARATOR/o, $pdirs)];
  1476. }
  1477. my ($dir, $dpath);
  1478. for $dir (map($wd->lookupdir($env->_subst($_)), @$pdirs))
  1479. {
  1480. $dpath = $dir->path;
  1481. # Add the (presumably local) directory to the -L flags
  1482. # if we're not using repositories, the directory exists,
  1483. # or it's Linked to a source directory (that is, it *will*
  1484. # exist by the time the link occurs).
  1485. $ldirs .= " " . $env->{LIBDIRPREFIX} . $dpath . $env->{LIBDIRSUFFIX}
  1486. if !@param::rpath || -d $dpath || $dir->is_linked;
  1487. next if File::Spec->file_name_is_absolute($dpath);
  1488. if (@param::rpath)
  1489. {
  1490. my $d;
  1491. if ($dpath eq $dir::CURDIR)
  1492. {
  1493. foreach $d (map($_->path, @param::rpath))
  1494. {
  1495. $ldirs .= " "
  1496. . $env->{LIBDIRPREFIX} . $d
  1497. . $env->{LIBDIRSUFFIX};
  1498. }
  1499. }
  1500. else
  1501. {
  1502. my ($rpath);
  1503. foreach $d (map($_->path, @param::rpath))
  1504. {
  1505. $rpath = File::Spec->catfile($d, $dpath);
  1506. $ldirs .= " "
  1507. . $env->{LIBDIRPREFIX} . $rpath
  1508. . $env->{LIBDIRSUFFIX}
  1509. if -d $rpath;
  1510. }
  1511. }
  1512. }
  1513. }
  1514. $env->{_LDIRS} = "%($ldirs%)";
  1515. }
  1516. # Introduce a new magic _LIBS symbol which allows to use the
  1517. # Unix-style -lNAME syntax for Win32 only. -lNAME will be replaced
  1518. # with %{PREFLIB}NAME%{SUFLIB}. <schwarze@isa.de> 1998-06-18
  1519. if ($main::_WIN32 && !exists $env->{_LIBS})
  1520. {
  1521. my $libs;
  1522. my $name;
  1523. for $name (split (' ', $env->_subst($env->{LIBS} || '')))
  1524. {
  1525. if ($name =~ /^-l(.*)/)
  1526. {
  1527. $name = "$env->{PREFLIB}$1$env->{SUFLIB}";
  1528. }
  1529. $libs .= ' ' . $name;
  1530. }
  1531. $env->{_LIBS} = $libs ? "%($libs%)" : '';
  1532. }
  1533. bless find build::command($env, $command);
  1534. }
  1535. # Called from file::build. Make sure any libraries needed by the
  1536. # environment are built, and return the collected signatures
  1537. # of the libraries in the path.
  1538. sub includes
  1539. {
  1540. return $_[0]->{'bsig'} if exists $_[0]->{'bsig'};
  1541. my ($self, $tgt) = @_;
  1542. my ($env) = $self->{env};
  1543. my ($ewd) = $env->{_cwd};
  1544. my $ldirs = $env->{LIBPATH};
  1545. if (!defined $ldirs)
  1546. {
  1547. $ldirs = [];
  1548. }
  1549. elsif (ref($ldirs) ne 'ARRAY')
  1550. {
  1551. $ldirs = [split (/$main::PATH_SEPARATOR/o, $ldirs)];
  1552. }
  1553. my @lpath = map($ewd->lookupdir($_), @$ldirs);
  1554. my (@sigs);
  1555. my (@names);
  1556. # Pass %LIBS symbol through %-substituition
  1557. # <schwarze@isa.de> 1998-06-18
  1558. @names = split (' ', $env->_subst($env->{LIBS} || ''));
  1559. my $name;
  1560. for $name (@names)
  1561. {
  1562. my ($lpath, @allnames);
  1563. if ($name =~ /^-l(.*)/)
  1564. {
  1565. # -l style names are looked up on LIBPATH, using all
  1566. # possible lib suffixes in the same search order the
  1567. # linker uses (according to SUFLIBS).
  1568. # Recognize new PREFLIB symbol, which should be 'lib' on
  1569. # Unix, and empty on Win32. TODO: What about shared
  1570. # library suffixes? <schwarze@isa.de> 1998-05-13
  1571. @allnames =
  1572. map("$env->{PREFLIB}$1$_", split (/:/, $env->{SUFLIBS}));
  1573. $lpath = \@lpath;
  1574. }
  1575. else
  1576. {
  1577. @allnames = ($name);
  1578. # On Win32, all library names are looked up in LIBPATH
  1579. # <schwarze@isa.de> 1998-05-13
  1580. if ($main::_WIN32)
  1581. {
  1582. $lpath = [$dir::top, @lpath];
  1583. }
  1584. else
  1585. {
  1586. $lpath = [$dir::top];
  1587. }
  1588. }
  1589. my $dir;
  1590. DIR: for $dir (@$lpath)
  1591. {
  1592. my $n;
  1593. for $n (@allnames)
  1594. {
  1595. my ($lib) = $dir->lookup_accessible($n);
  1596. if ($lib)
  1597. {
  1598. last DIR if $lib->ignore;
  1599. if ((build $lib) eq 'errors')
  1600. {
  1601. $tgt->{status} = 'errors';
  1602. return undef;
  1603. }
  1604. push (@sigs, 'sig'->signature($lib));
  1605. last DIR;
  1606. }
  1607. }
  1608. }
  1609. }
  1610. $self->{'bsig'} = 'sig'->collect(@sigs);
  1611. }
  1612. # Always compatible with other such builders, so the user
  1613. # can define a single program or module from multiple places.
  1614. sub compatible
  1615. {
  1616. my ($self, $other) = @_;
  1617. ref($other) eq "build::command::link";
  1618. }
  1619. # Link a program.
  1620. package build::command::linkedmodule;
  1621. use vars qw( @ISA );
  1622. BEGIN { @ISA = qw(build::command) }
  1623. # Always compatible with other such builders, so the user
  1624. # can define a single linked module from multiple places.
  1625. sub compatible
  1626. {
  1627. my ($self, $other) = @_;
  1628. ref($other) eq "build::command::linkedmodule";
  1629. }
  1630. # Builder for a C module
  1631. package build::command::cc;
  1632. use vars qw( @ISA );
  1633. BEGIN { @ISA = qw(build::command) }
  1634. sub find
  1635. {
  1636. $_[1]->{_cc} || do
  1637. {
  1638. my ($class, $env) = @_;
  1639. my ($cpppath) = $env->_subst($env->{CPPPATH});
  1640. my ($cscanner) = find scan::cpp($env->{_cwd}, $cpppath);
  1641. $env->{_IFLAGS} = "%(" . $cscanner->iflags($env) . "%)";
  1642. my ($self) = find build::command($env, $env->{CCCOM});
  1643. $self->{scanner} = $cscanner;
  1644. bless $env->{_cc} = $self;
  1645. }
  1646. }
  1647. # Invoke the associated C scanner to get signature of included files.
  1648. sub includes
  1649. {
  1650. my ($self, $tgt) = @_;
  1651. $self->{scanner}->includes($tgt, $tgt->{sources}[0]);
  1652. }
  1653. # Builder for a C++ module
  1654. package build::command::cxx;
  1655. use vars qw( @ISA );
  1656. BEGIN { @ISA = qw(build::command) }
  1657. sub find
  1658. {
  1659. $_[1]->{_cxx} || do
  1660. {
  1661. my ($class, $env) = @_;
  1662. my ($cpppath) = $env->_subst($env->{CPPPATH});
  1663. my ($cscanner) = find scan::cpp($env->{_cwd}, $cpppath);
  1664. $env->{_IFLAGS} = "%(" . $cscanner->iflags($env) . "%)";
  1665. my ($self) = find build::command($env, $env->{CXXCOM});
  1666. $self->{scanner} = $cscanner;
  1667. bless $env->{_cxx} = $self;
  1668. }
  1669. }
  1670. # Invoke the associated C scanner to get signature of included files.
  1671. sub includes
  1672. {
  1673. my ($self, $tgt) = @_;
  1674. $self->{scanner}->includes($tgt, $tgt->{sources}[0]);
  1675. }
  1676. # Builder for a user command (cons::Command). We assume that a user
  1677. # command might be built and implement the appropriate dependencies on
  1678. # the command itself (actually, just on the first word of the command
  1679. # line).
  1680. package build::command::user;
  1681. use vars qw( @ISA );
  1682. BEGIN { @ISA = qw(build::command) }
  1683. sub includes
  1684. {
  1685. my ($self, $tgt) = @_;
  1686. my ($sig) = '';
  1687. # Check for any quick scanners attached to source files.
  1688. my $dep;
  1689. for $dep (@{$tgt->{dep}}, @{$tgt->{sources}})
  1690. {
  1691. my ($scanner) = $dep->{'srcscan', $self->{env}};
  1692. if ($scanner)
  1693. {
  1694. $sig .= $scanner->includes($tgt, $dep);
  1695. }
  1696. }
  1697. # XXX Optimize this to not use ignored paths.
  1698. if (!exists $self->{_comsig})
  1699. {
  1700. my ($env) = $self->{env};
  1701. $self->{_comsig} = '';
  1702. my ($com, $dir);
  1703. com:
  1704. for $com ($self->{act}->commands)
  1705. {
  1706. my ($pdirs) = $env->{ENV}->{PATH};
  1707. if (!defined $pdirs)
  1708. {
  1709. $pdirs = [];
  1710. }
  1711. elsif (ref($pdirs) ne 'ARRAY')
  1712. {
  1713. $pdirs = [split (/$main::PATH_SEPARATOR/o, $pdirs)];
  1714. }
  1715. for $dir (map($dir::top->lookupdir($_), @$pdirs))
  1716. {
  1717. my ($prog) = $dir->lookup_accessible($com);
  1718. if ($prog)
  1719. { # XXX Not checking execute permission.
  1720. if ((build $prog) eq 'errors')
  1721. {
  1722. $tgt->{status} = 'errors';
  1723. return $sig;
  1724. }
  1725. next com if $prog->ignore;
  1726. $self->{_comsig} .= 'sig'->signature($prog);
  1727. next com;
  1728. }
  1729. }
  1730. }
  1731. }
  1732. return $self->{_comsig} . $sig;
  1733. }
  1734. # Builder for a library module (archive).
  1735. # We assume that a user command might be built and implement the
  1736. # appropriate dependencies on the command itself.
  1737. package build::command::library;
  1738. use vars qw( @ISA );
  1739. BEGIN { @ISA = qw(build::command) }
  1740. sub find
  1741. {
  1742. my ($class, $env) = @_;
  1743. bless find build::command($env, $env->{ARCOM});
  1744. }
  1745. # Always compatible with other library builders, so the user
  1746. # can define a single library from multiple places.
  1747. sub compatible
  1748. {
  1749. my ($self, $other) = @_;
  1750. ref($other) eq "build::command::library";
  1751. }
  1752. # A multi-target builder.
  1753. # This allows multiple targets to be associated with a single build
  1754. # script, without forcing all the code to be aware of multiple targets.
  1755. package build::multiple;
  1756. sub new
  1757. {
  1758. my ($class, $builder, $tgts) = @_;
  1759. bless {'builder' => $builder, 'env' => $builder->{env}, 'tgts' => $tgts};
  1760. }
  1761. sub scriptsig
  1762. {
  1763. my ($self, $tgt) = @_;
  1764. $self->{builder}->scriptsig($tgt);
  1765. }
  1766. sub includes
  1767. {
  1768. my ($self, $tgt) = @_;
  1769. $self->{builder}->includes($tgt);
  1770. }
  1771. sub compatible
  1772. {
  1773. my ($self, $tgt) = @_;
  1774. $self->{builder}->compatible($tgt);
  1775. }
  1776. sub cachin
  1777. {
  1778. my ($self, $tgt, $sig) = @_;
  1779. $self->{builder}->cachin($tgt, $sig);
  1780. }
  1781. sub cachout
  1782. {
  1783. my ($self, $tgt, $sig) = @_;
  1784. $self->{builder}->cachout($tgt, $sig);
  1785. }
  1786. sub action
  1787. {
  1788. my ($self, $invoked_tgt) = @_;
  1789. return $self->{built} if exists $self->{built};
  1790. # Make sure all targets in the group are unlinked before building any.
  1791. my ($tgts) = $self->{tgts};
  1792. my $tgt;
  1793. for $tgt (@$tgts)
  1794. {
  1795. futil::mkdir($tgt->{dir});
  1796. unlink($tgt->path) if !$tgt->precious;
  1797. }
  1798. # Now do the action to build all the targets. For consistency
  1799. # we always call the action on the first target, just so that
  1800. # $> is deterministic.
  1801. if ($param::max_jobs <= 1)
  1802. { # pcons
  1803. $self->{built} = $self->{builder}->action($tgts->[0]);
  1804. }
  1805. else
  1806. {
  1807. {
  1808. # action now is non-blocking, so we must kludge blocking for this
  1809. # explicit call
  1810. local ($file::child_queue) = {parent => $tgts->[0]}; # pcons
  1811. $self->{built} = $self->{builder}->action($tgts->[0]); # pcons
  1812. &file::wait_on_all_children(); # pcons
  1813. }
  1814. }
  1815. # Now "build" all the other targets (except for the one
  1816. # we were called with). This guarantees that the signature
  1817. # of each target is updated appropriately. We force the
  1818. # targets to be built even if they have been previously
  1819. # considered and found to be OK; the only effect this
  1820. # has is to make sure that signature files are updated
  1821. # correctly.
  1822. for $tgt (@$tgts)
  1823. {
  1824. if ($tgt ne $invoked_tgt)
  1825. {
  1826. delete $tgt->{status};
  1827. 'sig'->invalidate($tgt);
  1828. build $tgt;
  1829. }
  1830. }
  1831. # Status of action.
  1832. $self->{built};
  1833. }
  1834. package action;
  1835. sub new
  1836. {
  1837. my ($env, $act) = @_;
  1838. if (ref($act) eq 'CODE')
  1839. {
  1840. return action::perl->new($act);
  1841. }
  1842. else
  1843. {
  1844. return action::command->new($env, $act);
  1845. }
  1846. }
  1847. package action::command;
  1848. use vars qw( @ISA %cmd %_varopts $_varletters );
  1849. BEGIN
  1850. {
  1851. @ISA = $main::_WIN32 ? 'action::command::win32' : 'action::command::unix';
  1852. # Internal hash for processing variable options.
  1853. # f: return file part
  1854. # d: return directory part
  1855. # F: return file part, but strip any suffix
  1856. # b: return full path, but strip any suffix (a.k.a. return basename)
  1857. # s: return only the suffix (or an empty string, if no suffix is there)
  1858. # a: return the absolute path to the file
  1859. # S: return the absolute path to a Linked source file
  1860. %_varopts = (
  1861. 'f' => sub { return $_[0]->{entry}; },
  1862. 'd' => sub { return $_[0]->{dir}->path; },
  1863. 'F' => sub {
  1864. my $subst = $_[0]->{entry};
  1865. $subst =~ s/\.[^\.]+$//;
  1866. return $subst;
  1867. },
  1868. 'b' => sub {
  1869. my $subst = $_[0]->path;
  1870. $subst =~ s/\.[^\.]+$//;
  1871. return $subst;
  1872. },
  1873. 's' => sub {
  1874. my $subst = $_[0]->{entry};
  1875. $subst =~ m/(\.[^\.]+)$/;
  1876. return $1;
  1877. },
  1878. 'a' => sub {
  1879. my $path = $_[0]->path;
  1880. if (!File::Spec->file_name_is_absolute($path))
  1881. {
  1882. $path = File::Spec->catfile(Cwd::cwd(), $path);
  1883. }
  1884. return $path;
  1885. },
  1886. 'S' => sub {
  1887. my $path = $_[0]->srcpath;
  1888. if (!File::Spec->file_name_is_absolute($path))
  1889. {
  1890. my $cwd = File::Spec->canonpath(Cwd::cwd());
  1891. $path = File::Spec->catfile($cwd, $path);
  1892. }
  1893. return $path;
  1894. },
  1895. );
  1896. $_varletters = join ('', keys %_varopts);
  1897. }
  1898. # Internal routine for processing variable options.
  1899. # Options are specified in hash in the BEGIN block above.
  1900. # no option: return path to file (relative to top,
  1901. # or absolute if it's outside)
  1902. sub _variant
  1903. {
  1904. my ($opt, $file) = @_;
  1905. $opt = '' if !defined $opt;
  1906. if (defined $_varopts{$opt})
  1907. {
  1908. return &{$_varopts{$opt}} ($file);
  1909. }
  1910. return $file->path;
  1911. }
  1912. sub new
  1913. {
  1914. my ($class, $env, $cmd) = @_;
  1915. $cmd = $env->_subst($cmd);
  1916. $cmd{$env, $cmd} || do
  1917. {
  1918. # Remove unwanted bits from signature -- those bracketed by %( ... %)
  1919. my $sigs = $cmd;
  1920. my $sig = '';
  1921. if (ref($sigs) eq 'ARRAY')
  1922. {
  1923. # This is an array of commands..
  1924. my $f;
  1925. foreach $f (@$sigs)
  1926. {
  1927. $sig .= _strip($f);
  1928. }
  1929. }
  1930. else
  1931. {
  1932. $sig = _strip($sigs);
  1933. }
  1934. my $self = {cmd => $cmd, cmdsig => 'sig'->cmdsig($sig)};
  1935. $cmd{$env, $cmd} = bless $self, $class;
  1936. };
  1937. }
  1938. sub _strip
  1939. {
  1940. my $sig = shift;
  1941. $sig =~ s/^\@\s*//mg;
  1942. while ($sig =~ s/%\(([^%]|%[^\(])*?%\)//g) { }
  1943. $sig;
  1944. }
  1945. sub scriptsig
  1946. {
  1947. $_[0]->{cmdsig};
  1948. }
  1949. # Return an array of all the commands (first word on each line).
  1950. sub commands
  1951. {
  1952. my ($self) = @_;
  1953. my (@cmds) = ();
  1954. my $com;
  1955. my $cmd = $self->{'cmd'};
  1956. my @allcoms;
  1957. push @allcoms, ref $cmd ? @{$cmd} : split (/\n/, $cmd);
  1958. for $com (@allcoms)
  1959. {
  1960. $com =~ s/^\s*//;
  1961. $com =~ s/\s.*//;
  1962. next if !$com; # blank line
  1963. push @cmds, $com;
  1964. }
  1965. @cmds;
  1966. }
  1967. # For the signature of a basic command, we don't bother
  1968. # including the command itself. This is not strictly correct,
  1969. # and if we wanted to be rigorous, we might want to insist
  1970. # that the command was checked for all the basic commands
  1971. # like gcc, etc. For this reason we don't have an includes
  1972. # method.
  1973. # Call this to get the command line script: an array of
  1974. # fully substituted commands.
  1975. sub getcoms
  1976. {
  1977. my ($self, $env, $tgt) = @_;
  1978. my (@coms);
  1979. my $com;
  1980. my @allcoms = ();
  1981. my $cmd = $self->{'cmd'};
  1982. push @allcoms, ref $cmd ? @{$cmd} : split (/\n/, $cmd);
  1983. for $com (@allcoms)
  1984. {
  1985. my (@src) = (undef, @{$tgt->{sources}});
  1986. my (@src1) = @src;
  1987. next if $com =~ /^\s*$/;
  1988. # NOTE: we used to have a more elegant s//.../e solution
  1989. # for the items below, but this caused a bus error...
  1990. # Remove %( and %) -- those are only used to bracket parts
  1991. # of the command that we don't depend on.
  1992. $com =~ s/%[()]//g;
  1993. # Deal with %n, n=1,9 and variants.
  1994. while ($com =~ /%([1-9])(:([$_varletters]?))?/o)
  1995. {
  1996. my ($match) = $&;
  1997. my ($src) = $src1[$1];
  1998. my ($subst) = _variant($3, $src1[$1]->rfile);
  1999. undef $src[$1];
  2000. $com =~ s/$match/$subst/;
  2001. }
  2002. # Deal with %0 aka %> and variants.
  2003. while ($com =~ /%[0>](:([$_varletters]?))?/o)
  2004. {
  2005. my ($match) = $&;
  2006. my ($subst) = _variant($2, $tgt);
  2007. $com =~ s/$match/$subst/;
  2008. }
  2009. # Deal with %< (all sources except %n's already used)
  2010. while ($com =~ /%<(:([$_varletters]?))?/o)
  2011. {
  2012. my ($match) = $&;
  2013. my @list = ();
  2014. foreach (@src)
  2015. {
  2016. push (@list, _variant($2, $_->rfile)) if $_;
  2017. }
  2018. my ($subst) = join (' ', @list);
  2019. $com =~ s/$match/$subst/;
  2020. }
  2021. # Deal with %[ %].
  2022. $com =~ s{%\[(.*?)%\]}{
  2023. my($func, @args) = grep { $_ ne '' } split(/\s+/, $1);
  2024. die("$0: \"$func\" is not defined.\n")
  2025. unless ($env->{$func});
  2026. &{$env->{$func}}(@args);
  2027. }gex;
  2028. # Convert left-over %% into %.
  2029. $com =~ s/%%/%/g;
  2030. # White space cleanup. XXX NO WAY FOR USER TO HAVE QUOTED SPACES
  2031. $com = join (' ', split (' ', $com));
  2032. next if $com =~ /^:/ && $com !~ /^:\S/;
  2033. push (@coms, $com);
  2034. }
  2035. @coms;
  2036. }
  2037. # Build the target using the previously specified commands.
  2038. sub execute
  2039. {
  2040. my ($self, $env, $tgt, $package) = @_;
  2041. if ($param::build)
  2042. {
  2043. futil::mkdir($tgt->{dir});
  2044. unlink($tgt->path) if !$tgt->precious;
  2045. }
  2046. # Set environment.
  2047. map(delete $ENV{$_}, keys %ENV);
  2048. %ENV = %{$env->{ENV}};
  2049. # Handle multi-line commands.
  2050. my @cmds = $self->getcoms($env, $tgt); # pcons
  2051. if ($param::max_jobs > 1)
  2052. { # pcons
  2053. if ($#cmds > 0)
  2054. {
  2055. for ($i = 0 ; $i < @cmds ; $i++)
  2056. { #pcons -kn
  2057. $cmds[$i] = "( " . $cmds[$i] . " )"; #pcons -kn
  2058. } #pcons -kn
  2059. @cmds = join (" && ", @cmds); # pcons
  2060. } # pcons
  2061. } # pcons
  2062. my $com; # pcons
  2063. for $com (@cmds)
  2064. { # pcons
  2065. if ($com !~ s/^\@\s*//)
  2066. {
  2067. main::showcom($com);
  2068. }
  2069. next if !$param::build;
  2070. if ($com =~ /^\[perl\]\s*/)
  2071. {
  2072. my $perlcmd = $';
  2073. my $status;
  2074. {
  2075. # Restore the script package variables that were defined
  2076. # in the Conscript file that defined this [perl] build,
  2077. # so the code executes with the expected variables.
  2078. # Then actually execute (eval) the [perl] command to build
  2079. # the target, followed by cleaning up the name space
  2080. # by deleting the package variables we just restored.
  2081. my ($pkgvars) = $tgt->{conscript}->{pkgvars};
  2082. NameSpace::restore($package, $pkgvars) if $pkgvars;
  2083. $status = eval "package $package; $perlcmd";
  2084. NameSpace::remove($package, keys %$pkgvars) if $pkgvars;
  2085. }
  2086. if (!defined($status))
  2087. {
  2088. warn "$0: *** Error during perl command eval: $@.\n";
  2089. return undef;
  2090. }
  2091. elsif ($status == 0)
  2092. {
  2093. warn "$0: *** Perl command returned $status "
  2094. . "(this indicates an error).\n";
  2095. return undef;
  2096. }
  2097. next;
  2098. }
  2099. if (!$self->do_command($com, $tgt->path, $tgt))
  2100. {
  2101. return undef;
  2102. }
  2103. }
  2104. # success.
  2105. return 1;
  2106. }
  2107. sub show
  2108. {
  2109. my ($self, $env, $tgt) = @_;
  2110. my $com;
  2111. for $com ($self->getcoms($env, $tgt))
  2112. {
  2113. if ($com !~ /^\@\s*/)
  2114. {
  2115. main::showcom($com);
  2116. }
  2117. }
  2118. }
  2119. package action::command::unix;
  2120. sub do_command
  2121. {
  2122. my ($class, $com, $path) = @_; # cons
  2123. my ($class, $com, $path, $tgt) = @_; # pcons
  2124. if ($param::max_jobs > 1)
  2125. { # pcons
  2126. &file::wait_on_max_jobs(); # pcons
  2127. } # pcons
  2128. my ($pid) = fork();
  2129. die ("$0: unable to fork child process ($!)\n") if !defined $pid;
  2130. if (!$pid)
  2131. {
  2132. # This is the child. We eval the command to suppress -w
  2133. # warnings about not reaching the statements afterwards.
  2134. eval 'exec($com)';
  2135. $com =~ s/\s.*//;
  2136. die qq($0: failed to execute "$com" ($!). )
  2137. . qq(Is this an executable on path "$ENV{PATH}"?\n);
  2138. }
  2139. if ($param::max_jobs <= 1)
  2140. { # pcons
  2141. for (; ;)
  2142. {
  2143. do { } until wait() == $pid;
  2144. my ($b0, $b1) = ($? & 0xFF, $? >> 8);
  2145. # Don't actually see 0177 on stopped process; is this necessary?
  2146. next if $b0 == 0177; # process stopped; we can wait.
  2147. if ($b0)
  2148. {
  2149. my ($core, $sig) = ($b0 & 0200, $b0 & 0177);
  2150. my ($coremsg) = $core ? "; core dumped" : "";
  2151. $com =~ s/\s.*//;
  2152. my $err =
  2153. "$0: *** \[$path\] $com terminated by signal "
  2154. . "$sig$coremsg\n";
  2155. warn $err;
  2156. return undef;
  2157. }
  2158. if ($b1)
  2159. {
  2160. warn qq($0: *** [$path] Error $b1\n); # trying to be like make.
  2161. return undef;
  2162. }
  2163. last;
  2164. }
  2165. }
  2166. else
  2167. { # pcons
  2168. $file::child_queue->{$pid}->{com} = $com; # pcons
  2169. $file::child_queue->{$pid}->{tgt} = $tgt; # pcons
  2170. } # pcons
  2171. return 1;
  2172. }
  2173. package action::command::win32;
  2174. sub do_command
  2175. {
  2176. my ($class, $com, $path, $tgt) = @_;
  2177. system($com);
  2178. if ($?)
  2179. {
  2180. my ($b0, $b1) = ($? & 0xFF, $? >> 8);
  2181. my $err = $b1 || $?;
  2182. my $warn = qq($0: *** [$path] Error $err);
  2183. $warn .= " (executable not found in path?)" if $b1 == 0xFF;
  2184. warn "$warn\n";
  2185. return undef;
  2186. }
  2187. return 1;
  2188. }
  2189. package action::perl;
  2190. # THIS IS AN EXPERIMENTAL PACKAGE. It's entirely possible that the
  2191. # interface may change as this gets completed, so use at your own risk.
  2192. #
  2193. # There are (at least) two issues that need to be solved before blessing
  2194. # this as a real, fully-supported feature:
  2195. #
  2196. # -- We need to calculate a signature value for a Perl code ref, in
  2197. # order to rebuild the target if there's a change to the Perl code
  2198. # used to generate it.
  2199. #
  2200. # This is not straightforward. A B::Deparse package exists that
  2201. # decompiles a coderef into text. It's reportedly not completely
  2202. # reliable for closures; it misses which variables are global, and
  2203. # the values of private lexicals. Nevertheless, it'd probably
  2204. # be perfect for our purposes, except that it wasn't added until
  2205. # some time between Perl 5.00502 and 5.00554, and doesn't seem to
  2206. # really work until Perl 5.6.0, so by relying on it, we'd lose
  2207. # support for Perl versions back to 5.003*.
  2208. #
  2209. # -- Ideally, a code ref should be able to use something like
  2210. # $env->_subst to fetch values from the construction environment
  2211. # to modify its behavior without having to cut-and-paste code.
  2212. # (Actually, since we pass the environment to the executed code
  2213. # ref, there's no reason you can't do this with the code as it
  2214. # stands today.) But this REALLY complicates the signature
  2215. # calculation, because now the actual signature would depend not
  2216. # just on the code contents, but on the construction variables (or
  2217. # maybe just the environment).
  2218. #
  2219. # A potentially valid workaround would be to use the contents of the
  2220. # Conscript file in which the code reference is defined as the code
  2221. # ref's signature. This has the drawback of causing a recompilation of
  2222. # the target file even in response to unrelated changes in the Conscript
  2223. # file, but it would ensure correct builds without having to solve the
  2224. # messy issues of generating a signature directly from a code ref.
  2225. #
  2226. # Nevertheless, this seemed a useful enough skeleton of a feature that
  2227. # it made sense to release it in hopes that some practical experience
  2228. # will encourage someone to figure out how to solve the signature
  2229. # issues. Or maybe we'll discover these aren't big issues in practice
  2230. # and end up blessing it as is.
  2231. use vars qw( %code );
  2232. sub new
  2233. {
  2234. my ($class, $cref) = @_;
  2235. $code{$cref} || do
  2236. {
  2237. my $sig = '';
  2238. # Generating a code signature using B::Deparse doesn't really
  2239. # work for us until Perl 5.6.0. Here's the code in case
  2240. # someone wants to use it.
  2241. #use B::Deparse;
  2242. #my $deparse = B::Deparse->new();
  2243. #my $body = $deparse->coderef2text($cref);
  2244. #$sig = $body; # should be an MD5 sig
  2245. my ($self) = {cref => $cref, crefsig => $sig};
  2246. $code{$cref} = bless $self, $class;
  2247. }
  2248. }
  2249. sub scriptsig
  2250. {
  2251. $_[0]->{crefsig};
  2252. }
  2253. sub execute
  2254. {
  2255. my ($self, $env, $tgt) = @_;
  2256. if ($param::build)
  2257. {
  2258. futil::mkdir($tgt->{dir});
  2259. unlink($tgt->path) if !$tgt->precious;
  2260. my ($cref) = $self->{cref};
  2261. &$cref($env, $tgt->path, map($_->rpath, @{$tgt->{sources}}));
  2262. }
  2263. }
  2264. sub commands
  2265. {
  2266. return ();
  2267. }
  2268. # Generic scanning module.
  2269. package scan;
  2270. # Returns the signature of files included by the specified files on
  2271. # behalf of the associated target. Any errors in handling the included
  2272. # files are propagated to the target on whose behalf this processing
  2273. # is being done. Signatures are cached for each unique file/scanner
  2274. # pair.
  2275. sub includes
  2276. {
  2277. my ($self, $tgt, @files) = @_;
  2278. my (%files, $file);
  2279. my ($inc) = $self->{includes} || ($self->{includes} = {});
  2280. while ($file = pop @files)
  2281. {
  2282. next if exists $files{$file};
  2283. if ($inc->{$file})
  2284. {
  2285. push (@files, @{$inc->{$file}});
  2286. $files{$file} = 'sig'->signature($file->rfile);
  2287. }
  2288. else
  2289. {
  2290. if ((build $file) eq 'errors')
  2291. {
  2292. $tgt->{status} = 'errors'; # tgt inherits build status
  2293. return ();
  2294. }
  2295. $files{$file} = 'sig'->signature($file->rfile);
  2296. my (@includes) = $self->scan($file);
  2297. $inc->{$file} = \@includes;
  2298. push (@files, @includes);
  2299. }
  2300. }
  2301. 'sig'->collect(sort values %files);
  2302. }
  2303. # A simple scanner. This is used by the QuickScanfunction, to setup
  2304. # one-time target and environment-independent scanning for a source
  2305. # file. Only used for commands run by the Command method.
  2306. package scan::quickscan;
  2307. use vars qw( @ISA %scanner );
  2308. BEGIN { @ISA = qw(scan) }
  2309. sub find
  2310. {
  2311. my ($class, $code, $env, $pdirs) = @_;
  2312. if (!defined $pdirs)
  2313. {
  2314. $pdirs = [];
  2315. }
  2316. elsif (ref($pdirs) ne 'ARRAY')
  2317. {
  2318. $pdirs = [split (/$main::PATH_SEPARATOR/o, $pdirs)];
  2319. }
  2320. my (@path) = map { $dir::cwd->lookupdir($_) } @$pdirs;
  2321. my ($spath) = "@path";
  2322. $scanner{$code, $env, $spath} || do
  2323. {
  2324. my ($self) = {code => $code, env => $env, path => \@path};
  2325. $scanner{$code, $env, $spath} = bless $self;
  2326. }
  2327. }
  2328. # Scan the specified file for included file names.
  2329. sub scan
  2330. {
  2331. my ($self, $file) = @_;
  2332. my ($code) = $self->{code};
  2333. my (@includes);
  2334. # File should have been built by now. If not, we'll ignore it.
  2335. return () unless open(SCAN, $file->rpath);
  2336. while (<SCAN>)
  2337. {
  2338. push (@includes, grep($_ ne '', &$code));
  2339. }
  2340. close(SCAN);
  2341. my ($wd) = $file->{dir};
  2342. my (@files);
  2343. my $name;
  2344. for $name (@includes)
  2345. {
  2346. my $dir;
  2347. for $dir ($file->{dir}, @{$self->{path}})
  2348. {
  2349. my ($include) = $dir->lookup_accessible($name);
  2350. if ($include)
  2351. {
  2352. push (@files, $include) unless $include->ignore;
  2353. last;
  2354. }
  2355. }
  2356. }
  2357. @files;
  2358. }
  2359. # CPP (C preprocessor) scanning module
  2360. package scan::cpp;
  2361. use vars qw( @ISA %scanner );
  2362. BEGIN { @ISA = qw(scan) }
  2363. # For this constructor, provide the include path argument (colon
  2364. # separated). Each path is taken relative to the provided directory.
  2365. # Note: a particular scanning object is assumed to always return the
  2366. # same result for the same input. This is why the search path is a
  2367. # parameter to the constructor for a CPP scanning object. We go to
  2368. # some pains to make sure that we return the same scanner object
  2369. # for the same path: otherwise we will unecessarily scan files.
  2370. sub find
  2371. {
  2372. my ($class, $dir, $pdirs) = @_;
  2373. if (!defined $pdirs)
  2374. {
  2375. $pdirs = [];
  2376. }
  2377. elsif (ref($pdirs) ne 'ARRAY')
  2378. {
  2379. $pdirs = [split (/$main::PATH_SEPARATOR/o, $pdirs)];
  2380. }
  2381. my @path = map($dir->lookupdir($_), @$pdirs);
  2382. my ($spath) = "@path";
  2383. $scanner{$spath} || do
  2384. {
  2385. my ($self) = {'path' => \@path};
  2386. $scanner{$spath} = bless $self;
  2387. }
  2388. }
  2389. # Scan the specified file for include lines.
  2390. sub scan
  2391. {
  2392. my ($self, $file) = @_;
  2393. my ($angles, $quotes);
  2394. if (exists $file->{angles})
  2395. {
  2396. $angles = $file->{angles};
  2397. $quotes = $file->{quotes};
  2398. }
  2399. else
  2400. {
  2401. my (@anglenames, @quotenames);
  2402. return () unless open(SCAN, $file->rpath);
  2403. while (<SCAN>)
  2404. {
  2405. next unless /^\s*#/;
  2406. if (/^\s*#\s*include\s*([<"])(.*?)[>"]/)
  2407. {
  2408. if ($1 eq "<")
  2409. {
  2410. push (@anglenames, $2);
  2411. }
  2412. else
  2413. {
  2414. push (@quotenames, $2);
  2415. }
  2416. }
  2417. }
  2418. close(SCAN);
  2419. $angles = $file->{angles} = \@anglenames;
  2420. $quotes = $file->{quotes} = \@quotenames;
  2421. }
  2422. my (@shortpath) = @{$self->{path}}; # path for <> style includes
  2423. my (@longpath) = ($file->{dir}, @shortpath); # path for "" style includes
  2424. my (@includes);
  2425. my $name;
  2426. for $name (@$angles)
  2427. {
  2428. my $dir;
  2429. for $dir (@shortpath)
  2430. {
  2431. my ($include) = $dir->lookup_accessible($name);
  2432. if ($include)
  2433. {
  2434. push (@includes, $include) unless $include->ignore;
  2435. last;
  2436. }
  2437. }
  2438. }
  2439. for $name (@$quotes)
  2440. {
  2441. my $dir;
  2442. for $dir (@longpath)
  2443. {
  2444. my ($include) = $dir->lookup_accessible($name);
  2445. if ($include)
  2446. {
  2447. push (@includes, $include) unless $include->ignore;
  2448. last;
  2449. }
  2450. }
  2451. }
  2452. return @includes;
  2453. }
  2454. # Return the include flags that would be used for a C Compile.
  2455. sub iflags
  2456. {
  2457. my ($self, $env) = @_;
  2458. my ($iflags) = '';
  2459. my ($dir, $dpath);
  2460. for $dir (@{$self->{path}})
  2461. {
  2462. $dpath = $dir->path;
  2463. # Add the (presumably local) directory to the -I flags
  2464. # if we're not using repositories, the directory exists,
  2465. # or it's Linked to a source directory (that is, it *will*
  2466. # exist by the time the compilation occurs).
  2467. $iflags .= " " . $env->{INCDIRPREFIX} . $dpath . $env->{INCDIRSUFFIX}
  2468. if !@param::rpath || -d $dpath || $dir->is_linked;
  2469. next if File::Spec->file_name_is_absolute($dpath);
  2470. if (@param::rpath)
  2471. {
  2472. my $d;
  2473. if ($dpath eq $dir::CURDIR)
  2474. {
  2475. foreach $d (map($_->path, @param::rpath))
  2476. {
  2477. $iflags .=
  2478. " " . $env->{INCDIRPREFIX} . $d . $env->{INCDIRSUFFIX};
  2479. }
  2480. }
  2481. else
  2482. {
  2483. my ($rpath);
  2484. foreach $d (map($_->path, @param::rpath))
  2485. {
  2486. $rpath = File::Spec->catfile($d, $dpath);
  2487. $iflags .=
  2488. " " . $env->{INCDIRPREFIX} . $rpath . $env->{INCDIRSUFFIX}
  2489. if -d $rpath;
  2490. }
  2491. }
  2492. }
  2493. }
  2494. $iflags;
  2495. }
  2496. package File::Spec;
  2497. use vars qw( $_SEP $_MATCH_SEP $_MATCH_VOL );
  2498. # Cons is migrating to using File::Spec for portable path name
  2499. # manipulation. This is the right long-term direction, but there are
  2500. # some problems with making the transition:
  2501. #
  2502. # For multi-volume support, we need to use newer interfaces
  2503. # (splitpath, catpath, splitdir) that are only available in
  2504. # File::Spec 0.8.
  2505. #
  2506. # File::Spec 0.8 doesn't work with Perl 5.00[34] due to
  2507. # regular expression incompatibilities (use of \z).
  2508. #
  2509. # Forcing people to use a new version of a module is painful
  2510. # because (in the workplace) their administrators aren't
  2511. # always going to agree to install it everywhere.
  2512. #
  2513. # As a middle ground, we provide our own versions of all the File::Spec
  2514. # methods we use, supporting both UNIX and Win32. Some of these methods
  2515. # are home brew, some are cut-and-pasted from the real File::Spec methods.
  2516. # This way, we're not reinventing the whole wheel, at least.
  2517. #
  2518. # We can (and should) get rid of this class whenever 5.00[34] and
  2519. # versions of File::Spec prior to 0.9 (?) have faded sufficiently.
  2520. # We also may need to revisit whenever someone first wants to use
  2521. # Cons on some platform other than UNIX or Win32.
  2522. BEGIN
  2523. {
  2524. if ($main::_WIN32)
  2525. {
  2526. $_SEP = '\\';
  2527. $_MATCH_SEP = "[\Q/$_SEP\E]";
  2528. $_MATCH_VOL = "([a-z]:)?$_MATCH_SEP";
  2529. }
  2530. else
  2531. {
  2532. $_SEP = '/';
  2533. $_MATCH_SEP = "\Q$_SEP\E";
  2534. $_MATCH_VOL = $_MATCH_SEP;
  2535. }
  2536. }
  2537. sub canonpath
  2538. {
  2539. my ($self, $path) = @_;
  2540. if ($main::_WIN32)
  2541. {
  2542. $path =~ s/^([a-z]:)/\u$1/s;
  2543. $path =~ s|/|\\|g;
  2544. $path =~ s|([^\\])\\+|$1\\|g; # xx////xx -> xx/xx
  2545. $path =~ s|(\\\.)+\\|\\|g; # xx/././xx -> xx/xx
  2546. $path =~ s|^(\.\\)+||s unless $path eq ".\\"; # ./xx -> xx
  2547. $path =~ s|\\$|| unless $path =~ m#^([A-Z]:)?\\$#s; # xx/ -> xx
  2548. }
  2549. else
  2550. {
  2551. $path =~ s|/+|/|g unless ($^O eq 'cygwin'); # xx////xx -> xx/xx
  2552. $path =~ s|(/\.)+/|/|g; # xx/././xx -> xx/xx
  2553. $path =~ s|^(\./)+||s unless $path eq "./"; # ./xx -> xx
  2554. $path =~ s|^/(\.\./)+|/|s; # /../../xx -> xx
  2555. $path =~ s|/$|| unless $path eq "/"; # xx/ -> xx
  2556. }
  2557. return $path;
  2558. }
  2559. sub catdir
  2560. {
  2561. my $self = shift;
  2562. my @args = @_;
  2563. foreach (@args)
  2564. {
  2565. # append a slash to each argument unless it has one there
  2566. $_ .= $_SEP if $_ eq '' || substr($_, -1) ne $_SEP;
  2567. }
  2568. return $self->canonpath(join ('', @args));
  2569. }
  2570. sub catfile
  2571. {
  2572. my $self = shift;
  2573. my $file = pop @_;
  2574. return $file unless @_;
  2575. my $dir = $self->catdir(@_);
  2576. $dir .= $_SEP unless substr($dir, -1) eq $_SEP;
  2577. $file = '' if !defined($file);
  2578. return $dir . $file;
  2579. }
  2580. sub catpath
  2581. {
  2582. my $path = $_[1] . $_[0]->catfile(@_[2 .. $#_]);
  2583. $path =~ s/(.)$_MATCH_SEP*$/$1/;
  2584. $path;
  2585. }
  2586. sub curdir
  2587. {
  2588. '.';
  2589. }
  2590. sub file_name_is_absolute
  2591. {
  2592. my ($self, $file) = @_;
  2593. return scalar($file =~ m{^$_MATCH_VOL}is);
  2594. }
  2595. sub splitdir
  2596. {
  2597. my @dirs = split (/$_MATCH_SEP/, $_[1], -1);
  2598. push (@dirs, '') if $dirs[$#dirs];
  2599. @dirs;
  2600. }
  2601. sub splitpath
  2602. {
  2603. my ($self, $path) = @_;
  2604. my $vol = '';
  2605. my $sep = $_SEP;
  2606. if ($main::_WIN32)
  2607. {
  2608. if ($path =~ s#^([A-Za-z]:|(?:\\\\|//)[^\\/]+[\\/][^\\/]+)([\\/])#$2#)
  2609. {
  2610. $vol = $1;
  2611. $sep = $2;
  2612. }
  2613. }
  2614. my (@path) = split (/$_MATCH_SEP/, $path, -1);
  2615. my $file = pop @path;
  2616. my $dirs = join ($sep, @path, '');
  2617. return ($vol, $dirs, $file);
  2618. }
  2619. sub updir
  2620. {
  2621. '..';
  2622. }
  2623. sub case_tolerant
  2624. {
  2625. return $main::_WIN32;
  2626. }
  2627. # Directory and file handling. Files/dirs are represented by objects.
  2628. # Other packages are welcome to add component-specific attributes.
  2629. package dir;
  2630. use vars qw( $SEPARATOR $MATCH_SEPARATOR $CURDIR $UPDIR
  2631. $cwd_vol %root $top $cwd );
  2632. BEGIN
  2633. {
  2634. # A portable way of determing our directory separator.
  2635. $SEPARATOR = File::Spec->catdir('', '');
  2636. # A fast-path regular expression to match a directory separator
  2637. # anywhere in a path name.
  2638. if ($SEPARATOR eq '/')
  2639. {
  2640. $MATCH_SEPARATOR = "\Q$SEPARATOR\E";
  2641. }
  2642. else
  2643. {
  2644. $MATCH_SEPARATOR = "[\Q/$SEPARATOR\E]";
  2645. }
  2646. # Cache these values so we don't have to make a method call
  2647. # every time we need them.
  2648. $CURDIR = File::Spec->curdir; # '.' on UNIX
  2649. $UPDIR = File::Spec->updir; # '..' on UNIX
  2650. #
  2651. $cwd_vol = '';
  2652. }
  2653. # Annotate a node (file or directory) with info about the
  2654. # method that created it.
  2655. sub creator
  2656. {
  2657. my ($self, @frame) = @_;
  2658. $self->{'creator'} = \@frame if @frame;
  2659. $self->{'creator'};
  2660. }
  2661. # Handle a file|dir type exception. We only die if we find we were
  2662. # invoked by something in a Conscript/Construct file, because
  2663. # dependencies created directly by Cons' analysis shouldn't cause
  2664. # an error.
  2665. sub _type_exception
  2666. {
  2667. my ($e) = @_;
  2668. my ($line, $sub);
  2669. (undef, undef, $line, $sub) = script::caller_info;
  2670. if (defined $line)
  2671. {
  2672. my $err =
  2673. "\"${\$e->path}\" already in use as a "
  2674. . ref($e)
  2675. . " before $sub on line $line";
  2676. if ($e->{'creator'})
  2677. {
  2678. my $script;
  2679. (undef, $script, $line, $sub) = @{$e->{'creator'}};
  2680. $err =
  2681. "\t" . $err . ",\n\t\tdefined by $sub in $script, line $line";
  2682. }
  2683. $err .= "\n";
  2684. die $err;
  2685. }
  2686. }
  2687. # This wraps up all the common File::Spec logic that we use for parsing
  2688. # directory separators in a path and turning it into individual
  2689. # subdirectories that we must create, as well as creation of root
  2690. # nodes for any new file system volumes we find. File::Spec doesn't have
  2691. # intuitively obvious interfaces, so this is heavily commented.
  2692. #
  2693. # Note: This is NOT an object or class method;
  2694. # it's just a utility subroutine.
  2695. sub _parse_path
  2696. {
  2697. my ($dir, $path) = @_;
  2698. # Convert all slashes to the native directory separator.
  2699. # This allows Construct files to always be written with good
  2700. # old POSIX path names, regardless of what we're running on.
  2701. $path = File::Spec->canonpath($path);
  2702. # File::Spec doesn't understand the Cons convention of
  2703. # an initial '#' for top-relative files. Strip it.
  2704. my ($toprel) = $path =~ s/^#//;
  2705. # Let File::Spec do the heavy lifting of parsing the path name.
  2706. my ($vol, $directories, $entry) = File::Spec->splitpath($path);
  2707. my @dirs = File::Spec->splitdir($directories);
  2708. # If there was a file entry on the end of the path, then the
  2709. # last @dirs element is '' and we don't need it. If there
  2710. # wasn't a file entry on the end (File::Spec->splitpath() knew
  2711. # the last component was a directory), then the last @dirs
  2712. # element becomes the entry we want to look up.
  2713. my ($e) = pop @dirs;
  2714. $entry = $e if $entry eq '';
  2715. if (File::Spec->file_name_is_absolute($path))
  2716. {
  2717. # An absolute path name. If no volume was supplied,
  2718. # use the volume of our current directory.
  2719. $vol = $cwd_vol if $vol eq '';
  2720. $vol = uc($vol) if File::Spec->case_tolerant;
  2721. if (!defined $root{$vol})
  2722. {
  2723. # This is our first time looking up a path name
  2724. # on this volume, so create a root node for it.
  2725. # (On UNIX systems, $vol is always '', so '/'
  2726. # always maps to the $root{''} node.)
  2727. $root{$vol} = {
  2728. path => $vol . $SEPARATOR,
  2729. prefix => $vol . $SEPARATOR,
  2730. srcpath => $vol . $SEPARATOR,
  2731. 'exists' => 1
  2732. };
  2733. $root{$vol}->{'srcdir'} = $root{$vol};
  2734. bless $root{$vol};
  2735. }
  2736. # We're at the top, so strip the blank entry from the front of
  2737. # the @dirs array since the initial '/' it represents will now
  2738. # be supplied by the root node we return.
  2739. shift @dirs;
  2740. $dir = $root{$vol};
  2741. }
  2742. elsif ($toprel)
  2743. {
  2744. $dir = $dir::top;
  2745. }
  2746. ($dir, \@dirs, $entry);
  2747. }
  2748. # Common subroutine for creating directory nodes.
  2749. sub _create_dirs
  2750. {
  2751. my ($dir, @dirs) = @_;
  2752. my $e;
  2753. foreach $e (@dirs)
  2754. {
  2755. my $d = $dir->{member}->{$e};
  2756. if (!defined $d)
  2757. {
  2758. bless $d = {'entry' => $e, 'dir' => $dir,}, 'dir';
  2759. $d->creator(script::caller_info);
  2760. $d->{member}->{$dir::CURDIR} = $d;
  2761. $d->{member}->{$dir::UPDIR} = $dir;
  2762. $dir->{member}->{$e} = $d;
  2763. }
  2764. elsif (ref $d eq 'entry')
  2765. {
  2766. bless $d, 'dir';
  2767. $d->{member}->{$dir::CURDIR} = $d;
  2768. $d->{member}->{$dir::UPDIR} = $dir;
  2769. }
  2770. elsif (ref $d eq 'file')
  2771. {
  2772. # This clause is to supply backwards compatibility,
  2773. # with a warning, for anyone that's used FilePath
  2774. # to refer to a directory. After people have using
  2775. # 1.8 have had time to adjust (sometime in version
  2776. # 1.9 or later), we should remove this entire clause.
  2777. my ($script, $line, $sub);
  2778. (undef, $script, $line, $sub) = @{$d->{'creator'}};
  2779. if ($sub eq 'script::FilePath')
  2780. {
  2781. print STDERR
  2782. "$0: Warning: $sub used to refer to a directory\n"
  2783. . "\tat line $line of $script. Use DirPath instead.\n";
  2784. bless $d, 'dir';
  2785. }
  2786. else
  2787. {
  2788. _type_exception($d);
  2789. }
  2790. }
  2791. elsif (ref $d ne 'dir')
  2792. {
  2793. _type_exception($d);
  2794. }
  2795. $dir = $d;
  2796. }
  2797. $dir;
  2798. }
  2799. # Look up an entry in a directory. This method is for when we don't
  2800. # care whether a file or directory is returned, so if the entry already
  2801. # exists, it will simply be returned. If not, we create it as a
  2802. # generic "entry" which can be later turned into a file or directory
  2803. # by a more-specific lookup.
  2804. #
  2805. # The file entry may be specified as relative, absolute (starts with /),
  2806. # or top-relative (starts with #).
  2807. sub lookup
  2808. {
  2809. my ($dir, $entry) = @_;
  2810. if ($entry !~ m#$MATCH_SEPARATOR#o)
  2811. {
  2812. # Fast path: simple entry name in a known directory.
  2813. if ($entry =~ s/^#//)
  2814. {
  2815. # Top-relative names begin with #.
  2816. $dir = $dir::top;
  2817. }
  2818. elsif ($entry =~ s/^!//)
  2819. {
  2820. $dir = $dir::cwd->srcdir;
  2821. }
  2822. }
  2823. else
  2824. {
  2825. my $dirsref;
  2826. ($dir, $dirsref, $entry) = _parse_path($dir, $entry);
  2827. $dir = _create_dirs($dir, @$dirsref) if @$dirsref;
  2828. return if !defined $dir;
  2829. return $dir if $entry eq '';
  2830. }
  2831. my $e = $dir->{member}->{$entry};
  2832. if (!defined $e)
  2833. {
  2834. bless $e = {'entry' => $entry, 'dir' => $dir,}, 'entry';
  2835. $e->creator(script::caller_info);
  2836. $dir->{member}->{$entry} = $e;
  2837. }
  2838. $e;
  2839. }
  2840. # Look up a file entry in a directory.
  2841. #
  2842. # The file entry may be specified as relative, absolute (starts with /),
  2843. # or top-relative (starts with #).
  2844. sub lookupfile
  2845. {
  2846. my ($dir, $entry) = @_;
  2847. if ($entry !~ m#$MATCH_SEPARATOR#o)
  2848. {
  2849. # Fast path: simple entry name in a known directory.
  2850. if ($entry =~ s/^#//)
  2851. {
  2852. # Top-relative names begin with #.
  2853. $dir = $dir::top;
  2854. }
  2855. elsif ($entry =~ s/^!//)
  2856. {
  2857. $dir = $dir::cwd->srcdir;
  2858. }
  2859. }
  2860. else
  2861. {
  2862. my $dirsref;
  2863. ($dir, $dirsref, $entry) = _parse_path($dir, $entry);
  2864. $dir = _create_dirs($dir, @$dirsref) if @$dirsref;
  2865. return undef if $entry eq '';
  2866. }
  2867. my $f = $dir->{member}->{$entry};
  2868. if (!defined $f)
  2869. {
  2870. bless $f = {'entry' => $entry, 'dir' => $dir,}, 'file';
  2871. $f->creator(script::caller_info);
  2872. $dir->{member}->{$entry} = $f;
  2873. }
  2874. elsif (ref $f eq 'entry')
  2875. {
  2876. bless $f, 'file';
  2877. }
  2878. elsif (ref $f ne 'file')
  2879. {
  2880. _type_exception($f);
  2881. }
  2882. $f;
  2883. }
  2884. # Look up a (sub-)directory entry in a directory.
  2885. #
  2886. # The (sub-)directory entry may be specified as relative, absolute
  2887. # (starts with /), or top-relative (starts with #).
  2888. sub lookupdir
  2889. {
  2890. my ($dir, $entry) = @_;
  2891. my $dirsref;
  2892. if ($entry !~ m#$MATCH_SEPARATOR#o)
  2893. {
  2894. # Fast path: simple entry name in a known directory.
  2895. if ($entry =~ s/^#//)
  2896. {
  2897. # Top-relative names begin with #.
  2898. $dir = $dir::top;
  2899. }
  2900. elsif ($entry =~ s/^!//)
  2901. {
  2902. $dir = $dir::cwd->srcdir;
  2903. }
  2904. }
  2905. else
  2906. {
  2907. ($dir, $dirsref, $entry) = _parse_path($dir, $entry);
  2908. }
  2909. push (@$dirsref, $entry) if $entry ne '';
  2910. _create_dirs($dir, @$dirsref);
  2911. }
  2912. # Look up a file entry and return it if it's accessible.
  2913. sub lookup_accessible
  2914. {
  2915. my $file = $_[0]->lookupfile($_[1]);
  2916. return ($file && $file->accessible) ? $file : undef;
  2917. }
  2918. # Return the parent directory without doing a lookupdir,
  2919. # which would create a parent if it doesn't already exist.
  2920. # A return value of undef (! $dir->up) indicates a root directory.
  2921. sub up
  2922. {
  2923. $_[0]->{member}->{$dir::UPDIR};
  2924. }
  2925. # Return whether this is an entry somewhere underneath the
  2926. # specified directory.
  2927. sub is_under
  2928. {
  2929. my $dir = $_[0];
  2930. while ($dir)
  2931. {
  2932. return 1 if $_[1] == $dir;
  2933. $dir = $dir->up;
  2934. }
  2935. return undef;
  2936. }
  2937. # Return the relative path from the calling directory ($_[1])
  2938. # to the object. If the object is not under the directory, then
  2939. # we return it as a top-relative or absolute path name.
  2940. sub relpath
  2941. {
  2942. my ($dir, $obj) = @_;
  2943. my @dirs;
  2944. my $o = $obj;
  2945. while ($o)
  2946. {
  2947. if ($dir == $o)
  2948. {
  2949. if (@dirs < 2)
  2950. {
  2951. return $dirs[0] || '';
  2952. }
  2953. else
  2954. {
  2955. return File::Spec->catdir(@dirs);
  2956. }
  2957. }
  2958. unshift (@dirs, $o->{entry});
  2959. $o = $o->up;
  2960. }
  2961. # The object was not underneath the specified directory.
  2962. # Use the node's cached path, which is either top-relative
  2963. # (in which case we append '#' to the beginning) or
  2964. # absolute.
  2965. my $p = $obj->path;
  2966. $p = '#' . $p if !File::Spec->file_name_is_absolute($p);
  2967. return $p;
  2968. }
  2969. # Return the path of the directory (file paths implemented
  2970. # separately, below).
  2971. sub path
  2972. {
  2973. $_[0]->{path} || ($_[0]->{path} = $_[0]->{dir}->prefix . $_[0]->{entry});
  2974. }
  2975. # Return the pathname as a prefix to be concatenated with an entry.
  2976. sub prefix
  2977. {
  2978. return $_[0]->{prefix} if exists $_[0]->{prefix};
  2979. $_[0]->{prefix} = $_[0]->path . $SEPARATOR;
  2980. }
  2981. # Return the related source path prefix.
  2982. sub srcprefix
  2983. {
  2984. return $_[0]->{srcprefix} if exists $_[0]->{srcprefix};
  2985. my ($srcdir) = $_[0]->srcdir;
  2986. $srcdir->{srcprefix} =
  2987. $srcdir eq $_[0] ? $srcdir->prefix : $srcdir->srcprefix;
  2988. }
  2989. # Return the related source directory.
  2990. sub srcdir
  2991. {
  2992. $_[0]->{'srcdir'}
  2993. || ($_[0]->{'srcdir'} = $_[0]->{dir}->srcdir->lookupdir($_[0]->{entry}));
  2994. }
  2995. # Return if the directory is linked to a separate source directory.
  2996. sub is_linked
  2997. {
  2998. return $_[0]->{is_linked} if defined $_[0]->{is_linked};
  2999. $_[0]->{is_linked} = $_[0]->path ne $_[0]->srcdir->path;
  3000. }
  3001. sub link
  3002. {
  3003. my (@paths) = @_;
  3004. my ($srcdir) = $dir::cwd->lookupdir(pop @paths)->srcdir;
  3005. map($dir::cwd->lookupdir($_)->{'srcdir'} = $srcdir, @paths);
  3006. # make a reverse lookup for the link.
  3007. $srcdir->{links} = [] if !$srcdir->{links};
  3008. push @{$srcdir->{links}}, @paths;
  3009. }
  3010. use vars qw( @tail ); # TODO: Why global ????
  3011. sub linked_targets
  3012. {
  3013. my $tgt = shift;
  3014. my @targets = ();
  3015. my $dir;
  3016. if (ref $tgt eq 'dir')
  3017. {
  3018. $dir = $tgt;
  3019. }
  3020. else
  3021. {
  3022. push @tail, $tgt;
  3023. $dir = $tgt->{dir};
  3024. }
  3025. while ($dir)
  3026. {
  3027. if (defined $dir->{links} && @{$dir->{links}})
  3028. {
  3029. push @targets, map(File::Spec->catdir($_, @tail), @{$dir->{links}});
  3030. #print STDERR "Found Link: ${\$dir->path} -> @{\$dir->{links}}\n";
  3031. }
  3032. unshift @tail, $dir->{entry};
  3033. $dir = $dir->up;
  3034. }
  3035. return map($dir::top->lookupdir($_), @targets);
  3036. }
  3037. sub accessible
  3038. {
  3039. my $path = $_[0]->path;
  3040. my $err =
  3041. "$0: you have attempted to use path \"$path\" both as a file "
  3042. . "and as a directory!\n";
  3043. die $err;
  3044. }
  3045. sub init
  3046. {
  3047. my $path = Cwd::cwd();
  3048. # We know we can get away with passing undef to lookupdir
  3049. # as the directory because $dir is an absolute path.
  3050. $top = lookupdir(undef, $path);
  3051. $top->{'path'} = $top->{srcpath} = $dir::CURDIR;
  3052. $top->{'prefix'} = '';
  3053. $top->{'srcdir'} = $top;
  3054. $cwd = $top;
  3055. ($cwd_vol, undef, undef) = File::Spec->splitpath($path);
  3056. $cwd_vol = '' if !defined $cwd_vol;
  3057. $cwd_vol = uc($cwd_vol) if File::Spec->case_tolerant;
  3058. }
  3059. package file;
  3060. use vars qw( @ISA $level );
  3061. BEGIN { @ISA = qw(dir); $level = 0 }
  3062. # Return the pathname of the file.
  3063. # Define this separately from dir::path because we don't want to
  3064. # cache all file pathnames (just directory pathnames).
  3065. sub path
  3066. {
  3067. $_[0]->{dir}->prefix . $_[0]->{entry};
  3068. }
  3069. # Return the related source file path.
  3070. sub srcpath
  3071. {
  3072. $_[0]->{dir}->srcprefix . $_[0]->{entry};
  3073. }
  3074. # Return if the file is (should be) linked to a separate source file.
  3075. sub is_linked
  3076. {
  3077. $_[0]->{dir}->is_linked;
  3078. }
  3079. # Repository file search. If the local file exists, that wins.
  3080. # Otherwise, return the first existing same-named file under a
  3081. # Repository directory. If there isn't anything with the same name
  3082. # under a Repository directory, return the local file name anyway
  3083. # so that some higher layer can try to construct it.
  3084. sub rfile
  3085. {
  3086. return $_[0]->{rfile} if exists $_[0]->{rfile};
  3087. my ($self) = @_;
  3088. my ($rfile) = $self;
  3089. if (@param::rpath)
  3090. {
  3091. my ($path) = $self->path;
  3092. if (!File::Spec->file_name_is_absolute($path) && !-f $path)
  3093. {
  3094. my ($dir);
  3095. foreach $dir (@param::rpath)
  3096. {
  3097. my ($t) = $dir->prefix . $path;
  3098. if (-f $t)
  3099. {
  3100. $rfile = $_[0]->lookupfile($t);
  3101. $rfile->{'lfile'} = $self;
  3102. last;
  3103. }
  3104. }
  3105. }
  3106. }
  3107. $self->{rfile} = $rfile;
  3108. }
  3109. # Returns the local file for a repository file;
  3110. # returns self if it's already a local file.
  3111. sub lfile
  3112. {
  3113. $_[0]->{'lfile'} || $_[0];
  3114. }
  3115. # returns the "precious" status of this file.
  3116. sub precious
  3117. {
  3118. return $_[0]->{precious};
  3119. }
  3120. # "Erase" reference to a Repository file,
  3121. # making this a completely local file object
  3122. # by pointing it back to itself.
  3123. sub no_rfile
  3124. {
  3125. $_[0]->{'rfile'} = $_[0];
  3126. }
  3127. # Return a path to the first existing file under a Repository directory,
  3128. # implicitly returning the current file's path if there isn't a
  3129. # same-named file under a Repository directory.
  3130. sub rpath
  3131. {
  3132. $_[0]->{rpath} || ($_[0]->{rpath} = $_[0]->rfile->path);
  3133. }
  3134. # Return a path to the first linked srcpath file under a Repositoy
  3135. # directory, implicitly returning the current file's srcpath if there
  3136. # isn't a same-named file under a Repository directory.
  3137. sub rsrcpath
  3138. {
  3139. return $_[0]->{rsrcpath} if exists $_[0]->{rsrcpath};
  3140. my ($self) = @_;
  3141. my ($path) = $self->{rsrcpath} = $self->srcpath;
  3142. if (@param::rpath && !File::Spec->file_name_is_absolute($path) && !-f $path)
  3143. {
  3144. my ($dir);
  3145. foreach $dir (@param::rpath)
  3146. {
  3147. my ($t) = $dir->prefix . $path;
  3148. if (-f $t)
  3149. {
  3150. $self->{rsrcpath} = $t;
  3151. last;
  3152. }
  3153. }
  3154. }
  3155. $self->{rsrcpath};
  3156. }
  3157. # Return if a same-named file source file exists.
  3158. # This handles the interaction of Link and Repository logic.
  3159. # As a side effect, it will link a source file from its Linked
  3160. # directory (preferably local, but maybe in a repository)
  3161. # into a build directory from its proper Linked directory.
  3162. sub source_exists
  3163. {
  3164. return $_[0]->{source_exists} if defined $_[0]->{source_exists};
  3165. my ($self) = @_;
  3166. my ($path) = $self->path;
  3167. my ($mtime, $ctime) = (stat($path))[9, 10];
  3168. if ($self->is_linked)
  3169. {
  3170. # Linked directory, local logic.
  3171. my ($srcpath) = $self->srcpath;
  3172. my ($src_mtime, $src_ctime) = (stat($srcpath))[9, 10];
  3173. if ($src_mtime)
  3174. {
  3175. if (!$mtime || $src_mtime != $mtime || $src_ctime != $ctime)
  3176. {
  3177. futil::install($srcpath, $self);
  3178. }
  3179. return $self->{source_exists} = 1;
  3180. }
  3181. # Linked directory, repository logic.
  3182. if (@param::rpath)
  3183. {
  3184. if ($self != $self->rfile)
  3185. {
  3186. return $self->{source_exists} = 1;
  3187. }
  3188. my ($rsrcpath) = $self->rsrcpath;
  3189. if ($path ne $rsrcpath)
  3190. {
  3191. my ($rsrc_mtime, $rsrc_ctime) = (stat($rsrcpath))[9, 10];
  3192. if ($rsrc_mtime)
  3193. {
  3194. if (!$mtime
  3195. || $rsrc_mtime != $mtime
  3196. || $rsrc_ctime != $ctime)
  3197. {
  3198. futil::install($rsrcpath, $self);
  3199. }
  3200. return $self->{source_exists} = 1;
  3201. }
  3202. }
  3203. }
  3204. # There was no source file in any Linked directory
  3205. # under any Repository. If there's one in the local
  3206. # build directory, it no longer belongs there.
  3207. if ($mtime)
  3208. {
  3209. unlink($path) || die ("$0: couldn't unlink $path ($!)\n");
  3210. }
  3211. return $self->{source_exists} = '';
  3212. }
  3213. else
  3214. {
  3215. if ($mtime)
  3216. {
  3217. return $self->{source_exists} = 1;
  3218. }
  3219. if (@param::rpath && $self != $self->rfile)
  3220. {
  3221. return $self->{source_exists} = 1;
  3222. }
  3223. return $self->{source_exists} = '';
  3224. }
  3225. }
  3226. # Return if a same-named derived file exists under a Repository directory.
  3227. sub derived_exists
  3228. {
  3229. $_[0]->{derived_exists}
  3230. || ($_[0]->{derived_exists} = ($_[0] != $_[0]->rfile));
  3231. }
  3232. # Return if this file is somewhere under a Repository directory.
  3233. sub is_on_rpath
  3234. {
  3235. defined $_[0]->{'lfile'};
  3236. }
  3237. sub local
  3238. {
  3239. my ($self, $arg) = @_;
  3240. if (defined $arg)
  3241. {
  3242. $self->{'local'} = $arg;
  3243. }
  3244. $self->{'local'};
  3245. }
  3246. # Return the entry name of the specified file with the specified
  3247. # suffix appended. Leave it untouched if the suffix is already there.
  3248. # Differs from the addsuffix function, below, in that this strips
  3249. # the existing suffix (if any) before appending the desired one.
  3250. sub base_suf
  3251. {
  3252. my ($entry) = $_[0]->{entry};
  3253. if ($entry !~ m/$_[1]$/)
  3254. {
  3255. $entry =~ s/\.[^\.]*$//;
  3256. $entry .= $_[1];
  3257. }
  3258. $entry;
  3259. }
  3260. # Return the suffix of the file; everything including and to the
  3261. # right of the last dot.
  3262. sub suffix
  3263. {
  3264. my @pieces = split (/\./, $_[0]->{entry});
  3265. my $suffix = pop (@pieces);
  3266. return ".$suffix";
  3267. }
  3268. # Called as a simple function file::addsuffix(name, suffix)
  3269. sub addsuffix
  3270. {
  3271. my ($name, $suffix) = @_;
  3272. if ($suffix && substr($name, -length($suffix)) ne $suffix)
  3273. {
  3274. return $name .= $suffix;
  3275. }
  3276. $name;
  3277. }
  3278. # Return true if the file is (or will be) accessible.
  3279. # That is, if we can build it, or if it is already present.
  3280. sub accessible
  3281. {
  3282. (exists $_[0]->{builder}) || ($_[0]->source_exists);
  3283. }
  3284. # Return true if the file should be ignored for the purpose
  3285. # of computing dependency information (should not be considered
  3286. # as a dependency and, further, should not be scanned for
  3287. # dependencies).
  3288. sub ignore
  3289. {
  3290. return 0 if !$param::ignore;
  3291. return $_[0]->{ignore} if exists $_[0]->{ignore};
  3292. $_[0]->{ignore} = $_[0]->path =~ /$param::ignore/o;
  3293. }
  3294. # Build the file, if necessary.
  3295. sub build
  3296. {
  3297. return $_[0]->{status} if $_[0]->{status};
  3298. my ($status) = &file::_build;
  3299. if ($_[0]->{after_build_func})
  3300. {
  3301. #print STDERR "DEBUG: after_build_func=$_[0]->{after_build_func}\n";
  3302. my ($pkgvars) = $_[0]->{conscript}->{pkgvars};
  3303. NameSpace::restore('script', $pkgvars) if $pkgvars;
  3304. eval("package script; " . $_[0]->{after_build_func});
  3305. print "Error running AfterBuild for ${\$_[0]->path}: $@\n" if ($@);
  3306. NameSpace::remove('script', keys %$pkgvars) if $pkgvars;
  3307. }
  3308. return $status;
  3309. }
  3310. sub _build
  3311. {
  3312. my @args = @_;
  3313. if ($param::max_jobs <= 1)
  3314. { # pcons
  3315. my ($self) = @args;
  3316. print main::DEPFILE $self->path, "\n" if $param::depfile;
  3317. print((' ' x $level), "Checking ", $self->path, "\n")
  3318. if $param::depends;
  3319. if (!exists $self->{builder})
  3320. {
  3321. # We don't know how to build the file. This is OK, if
  3322. # the file is present as a source file, under either the
  3323. # local tree or a Repository.
  3324. if ($self->source_exists)
  3325. {
  3326. return $self->{status} = 'handled';
  3327. }
  3328. else
  3329. {
  3330. my ($name) = $self->path;
  3331. print("$0: don't know how to construct \"$name\"\n");
  3332. exit(1) unless $param::kflag;
  3333. return $self->{status} = 'errors'; # xxx used to be 'unknown'
  3334. }
  3335. }
  3336. # An associated build object exists, so we know how to build
  3337. # the file. We first compute the signature of the file, based
  3338. # on its dependendencies, then only rebuild the file if the
  3339. # signature has changed.
  3340. my ($builder) = $self->{builder};
  3341. $level += 2;
  3342. my (@deps) = (@{$self->{dep}}, @{$self->{sources}});
  3343. my ($rdeps) = \@deps;
  3344. if ($param::random)
  3345. {
  3346. # If requested, build in a random order, instead of the
  3347. # order that the dependencies were listed.
  3348. my (%rdeps);
  3349. map { $rdeps{$_, '*' x int(rand 10)} = $_ } @deps;
  3350. $rdeps = [values(%rdeps)];
  3351. }
  3352. $self->{status} = '';
  3353. my $dep;
  3354. for $dep (@$rdeps)
  3355. {
  3356. if ((build $dep) eq 'errors')
  3357. {
  3358. # Propagate dependent errors to target.
  3359. # but try to build all dependents regardless of errors.
  3360. $self->{status} = 'errors';
  3361. }
  3362. }
  3363. # If any dependents had errors, then we abort.
  3364. if ($self->{status} eq 'errors')
  3365. {
  3366. $level -= 2;
  3367. return 'errors';
  3368. }
  3369. # Compute the final signature of the file, based on
  3370. # the static dependencies (in order), dynamic dependencies,
  3371. # output path name, and (non-substituted) build script.
  3372. my ($sig) =
  3373. 'sig'->collect(map('sig'->signature($_->rfile), @deps),
  3374. $builder->includes($self), $builder->scriptsig);
  3375. # May have gotten errors during computation of dynamic
  3376. # dependency signature, above.
  3377. $level -= 2;
  3378. return 'errors' if $self->{status} eq 'errors';
  3379. if (@param::rpath && $self->derived_exists)
  3380. {
  3381. # There is no local file of this name, but there is one
  3382. # under a Repository directory.
  3383. if ('sig'->current($self->rfile, $sig))
  3384. {
  3385. # The Repository copy is current (its signature matches
  3386. # our calculated signature).
  3387. if ($self->local)
  3388. {
  3389. # ...but they want a local copy, so provide it.
  3390. main::showcom("Local copy of ${\$self->path} from "
  3391. . "${\$self->rpath}");
  3392. futil::install($self->rpath, $self);
  3393. 'sig'->bsig($self, $sig);
  3394. }
  3395. return $self->{status} = 'handled';
  3396. }
  3397. # The signatures don't match, implicitly because something
  3398. # on which we depend exists locally. Get rid of the reference
  3399. # to the Repository file; we'll build this (and anything that
  3400. # depends on it) locally.
  3401. $self->no_rfile;
  3402. }
  3403. # Then check for currency.
  3404. if (!'sig'->current($self, $sig))
  3405. {
  3406. # We have to build/derive the file.
  3407. print((' ' x $level), "Rebuilding ", $self->path,
  3408. ": out of date.\n")
  3409. if $param::depends;
  3410. # First check to see if the built file is cached.
  3411. if ($builder->cachin($self, $sig))
  3412. {
  3413. 'sig'->bsig($self, $sig);
  3414. return $self->{status} = 'built';
  3415. }
  3416. elsif ($builder->action($self))
  3417. {
  3418. $builder->cachout($self, $sig);
  3419. 'sig'->bsig($self, $sig);
  3420. return $self->{status} = 'built';
  3421. }
  3422. else
  3423. {
  3424. die ("$0: errors constructing ${\$self->path}\n")
  3425. unless $param::kflag;
  3426. return $self->{status} = 'errors';
  3427. }
  3428. }
  3429. else
  3430. {
  3431. # Push this out to the cache if we've been asked to (-C option).
  3432. # Don't normally do this because it slows us down.
  3433. # In a fully built system, no accesses to the cache directory
  3434. # are required to check any files. This is a win if cache is
  3435. # heavily shared. Enabling this option puts the directory in the
  3436. # loop. Useful only when you wish to recreate a cache from a build.
  3437. if ($param::cachesync)
  3438. {
  3439. $builder->cachout($self, $sig);
  3440. 'sig'->bsig($self, $sig);
  3441. }
  3442. return $self->{status} = 'handled';
  3443. }
  3444. }
  3445. else
  3446. { # pcons
  3447. my ($tgt) = @args; # pcons
  3448. local ($file::child_queue) = {parent => $tgt}; # pcons
  3449. _pbuild($tgt); # pcons
  3450. wait_on_all_children(); # pcons
  3451. return $tgt->{status}; # pcons
  3452. }
  3453. }
  3454. ########################################
  3455. # pcons only BEGIN
  3456. #
  3457. sub pbuild
  3458. {
  3459. $_[0]->{status} || &file::_pbuild;
  3460. }
  3461. sub _pbuild
  3462. {
  3463. my ($self) = @_;
  3464. $self->{status} = ''; # tgl
  3465. print main::DEPFILE $self->path, "\n" if param::depfile;
  3466. print((' ' x $level), $self->path, "\n") if $param::depends;
  3467. if (!exists $self->{builder})
  3468. {
  3469. # We don't know how to build the file. This is OK, if
  3470. # the file is present as a source file, under either the
  3471. # local tree or a Repository.
  3472. if ($self->source_exists)
  3473. {
  3474. return $self->{status} = 'handled';
  3475. }
  3476. else
  3477. {
  3478. my ($name) = $self->path;
  3479. print("$0: don't know how to construct \"$name\"\n");
  3480. exit(1) unless $param::kflag;
  3481. return $self->{status} = 'errors'; # xxx used to be 'unknown'
  3482. }
  3483. }
  3484. # An associated build object exists, so we know how to build
  3485. # the file. We first compute the signature of the file, based
  3486. # on its dependendencies, then only rebuild the file if the
  3487. # signature has changed.
  3488. my ($builder) = $self->{'builder'};
  3489. $level += 2;
  3490. my (@deps) = (@{$self->{dep}}, @{$self->{sources}});
  3491. my ($rdeps) = \@deps;
  3492. if ($param::random)
  3493. {
  3494. # If requested, build in a random order, instead of the
  3495. # order that the dependencies were listed.
  3496. my (%rdeps);
  3497. # FIX map { $rdeps{$_,'*' x int(rand(0,10))} = $_ } @deps;
  3498. map { $rdeps{$_, '*' x int(rand(10))} = $_ } @deps;
  3499. $rdeps = [values(%rdeps)];
  3500. }
  3501. $self->{status} = '';
  3502. my $sig;
  3503. {
  3504. # print "in ", $self->path, "\n";
  3505. local ($file::child_queue) = {parent => $self};
  3506. for my $dep (@$rdeps)
  3507. {
  3508. if ((pbuild $dep) eq 'errors')
  3509. {
  3510. # Propagate dependent errors to target.
  3511. # but try to build all dependents regardless of errors.
  3512. $self->{status} = 'errors';
  3513. }
  3514. }
  3515. wait_on_all_children();
  3516. }
  3517. # If any dependents had errors, then we abort.
  3518. if ($self->{'status'} eq 'errors')
  3519. {
  3520. $level -= 2;
  3521. return 'errors';
  3522. }
  3523. # Compute the final signature of the file, based on
  3524. # the static dependencies (in order), dynamic dependencies,
  3525. # output path name, and (non-substituted) build script.
  3526. # my($sig) = $self->{'sign'} = sig->collect(map(sig->signature($_->rfile), @deps),
  3527. # $builder->includes($self),
  3528. ## FIX $builder->script);
  3529. # $builder->scriptsig);
  3530. my ($sig) = 'sig'->collect(
  3531. map('sig'->signature($_->rfile), @deps), # from cons-2.3.0
  3532. $builder->includes($self), # from cons-2.3.0
  3533. $builder->scriptsig
  3534. ); # from cons-2.3.0
  3535. $self->{sign} = $sig;
  3536. # May have gotten errors during computation of dynamic
  3537. # dependency signature, above.
  3538. $level -= 2;
  3539. return 'errors' if $self->{status} eq 'errors';
  3540. if (@param::rpath && $self->derived_exists)
  3541. {
  3542. # There is no local file of this name, but there is one
  3543. # under a Repository directory.
  3544. if ('sig'->current($self->rfile, $sig))
  3545. {
  3546. # The Repository copy is current (its signature matches
  3547. # our calculated signature).
  3548. if ($self->local)
  3549. {
  3550. # ...but they want a local copy, so provide it.
  3551. main::showcom(
  3552. "Local copy of ${\$self->path} from ${\$self->rpath}");
  3553. futil::install($self->rpath, $self);
  3554. #'sig'->set($self, $sig);
  3555. 'sig'->bsig($self, $sig); # pcons
  3556. }
  3557. return $self->{status} = 'handled';
  3558. }
  3559. # The signatures don't match, implicitly because something
  3560. # on which we depend exists locally. Get rid of the reference
  3561. # to the Repository file; we'll build this (and anything that
  3562. # depends on it) locally.
  3563. $self->no_rfile;
  3564. }
  3565. # Then check for currency.
  3566. if (!'sig'->current($self, $sig))
  3567. {
  3568. # We have to build/derive the file.
  3569. # First check to see if the built file is cached.
  3570. if ($builder->cachin($self, $sig))
  3571. {
  3572. #'sig'->set($self, $sig);
  3573. 'sig'->bsig($self, $sig); # pcons
  3574. return $self->{status} = 'built';
  3575. # action no longer blocks (for most actions), so this returns
  3576. # immediately, before any commands are actually run. The
  3577. # signature and return status should be overridden later in
  3578. # wait_on_child if a process was forked, but they are still set
  3579. # here in case a non-spawning action was called
  3580. }
  3581. elsif ($builder->action($self))
  3582. {
  3583. $builder->cachout($self, $sig);
  3584. #'sig'->set($self, $sig);
  3585. 'sig'->bsig($self, $sig); # pcons
  3586. return $self->{status} = 'built';
  3587. }
  3588. else
  3589. {
  3590. die ("$0: errors constructing ${\$self->path}\n")
  3591. unless $param::kflag;
  3592. return $self->{status} = 'errors';
  3593. }
  3594. }
  3595. else
  3596. {
  3597. # Push this out to the cache if we've been asked to (-C option).
  3598. # Don't normally do this because it slows us down.
  3599. # In a fully built system, no accesses to the cache directory
  3600. # are required to check any files. This is a win if cache is
  3601. # heavily shared. Enabling this option puts the directory in the
  3602. # loop. Useful only when you wish to recreate a cache from a build.
  3603. if ($param::cachesync)
  3604. {
  3605. $builder->cachout($self, $sig);
  3606. #'sig'->set($self, $sig);
  3607. 'sig'->bsig($self, $sig);
  3608. }
  3609. return $self->{status} = 'handled';
  3610. }
  3611. }
  3612. my @finished; # pcons //fix -Mstrict
  3613. sub wait_on_max_jobs
  3614. {
  3615. while (grep(/\d+/, keys %{$file::child_queue}) >= $param::max_jobs)
  3616. {
  3617. wait_on_child();
  3618. }
  3619. }
  3620. sub wait_on_all_children
  3621. {
  3622. while (grep(/\d+/, keys %{$file::child_queue}) != 0)
  3623. {
  3624. wait_on_child();
  3625. }
  3626. }
  3627. sub wait_on_child
  3628. {
  3629. my ($queue) = $file::child_queue;
  3630. my ($parent) = $queue->{parent};
  3631. my $n = grep(/\d+/, keys %{$file::child_queue});
  3632. # printf "Waiting to build (%s): %s\n", $n, $parent->path if ref($parent) eq 'file';
  3633. my $pid = undef;
  3634. # first check for jobs that have already been collected from child
  3635. # targets
  3636. for my $p (@finished)
  3637. {
  3638. if ($queue->{$p})
  3639. {
  3640. $pid = $p;
  3641. last;
  3642. }
  3643. }
  3644. while (!$queue->{$pid})
  3645. {
  3646. $pid = wait();
  3647. if ($pid < 1)
  3648. {
  3649. die "wait returned invalid pid $pid";
  3650. }
  3651. else
  3652. {
  3653. # this job was meant for a parent, save it so that it can be
  3654. # found later
  3655. if (!$queue->{$pid})
  3656. {
  3657. push @finished, $pid;
  3658. }
  3659. }
  3660. }
  3661. my $child = $queue->{$pid};
  3662. my $tgt = $queue->{$pid}->{'tgt'};
  3663. my $com = $child->{'com'};
  3664. $tgt->{status} = 'built';
  3665. #print "waited: ", $tgt->path;
  3666. #print "on: ", keys %{$queue}, "\n";
  3667. my ($b0, $b1) = ($? & 0xFF, $? >> 8);
  3668. # Don't actually see 0177 on stopped process; is this necessary?
  3669. # next if $b0 == 0177; # process stopped; we can wait.
  3670. if ($b0)
  3671. {
  3672. my ($core, $sig) = ($b0 & 0200, $b0 & 0177);
  3673. my ($coremsg) = $core ? "; core dumped" : "";
  3674. $com =~ s/\s.*//;
  3675. my ($path) = $tgt->path;
  3676. warn qq($0: *** [$path] $com terminated by signal $sig$coremsg\n);
  3677. $parent->{status} = $tgt->{status} = 'errors';
  3678. }
  3679. if ($b1)
  3680. {
  3681. my ($path) = $tgt->path;
  3682. warn qq($0: *** [$path] Error $b1\n); # trying to be like make.
  3683. $parent->{status} = $tgt->{status} = 'errors';
  3684. }
  3685. if ($tgt->{status} eq 'built')
  3686. {
  3687. $tgt->{builder}->cachout($tgt, $tgt->{sign});
  3688. #'sig'->set($tgt, $tgt->{sign});
  3689. 'sig'->bsig($tgt, $tgt->{sign});
  3690. }
  3691. else
  3692. {
  3693. die ("$0: errors constructing ${\$tgt->path}\n") unless $param::kflag;
  3694. }
  3695. delete $queue->{$pid};
  3696. }
  3697. #
  3698. # pcons END
  3699. ########################################
  3700. # Bind an action to a file, with the specified sources. No return value.
  3701. sub bind
  3702. {
  3703. my ($self, $builder, @sources) = @_;
  3704. if ($self->{builder} && !$self->{builder}->compatible($builder))
  3705. {
  3706. # Even if not "compatible", we can still check to see if the
  3707. # derivation is identical. It should be identical if the builder is
  3708. # the same and the sources are the same.
  3709. if ("$self->{builder} @{$self->{sources}}" ne "$builder @sources")
  3710. {
  3711. $main::errors++;
  3712. my ($_foo1, $script1, $line1, $sub1) = @{$self->creator};
  3713. my ($_foo2, $script2, $line2, $sub2) = script::caller_info;
  3714. my $err =
  3715. "\t${\$self->path}\n"
  3716. . "\tbuilt (at least) two different ways:\n"
  3717. . "\t\t$script1, line $line1: $sub1\n"
  3718. . "\t\t$script2, line $line2: $sub2\n";
  3719. die $err;
  3720. }
  3721. return;
  3722. }
  3723. if ($param::wflag)
  3724. {
  3725. my ($script, $line, $sub);
  3726. (undef, $script, $line, $sub) = script::caller_info;
  3727. $self->{script} = '' if !defined $self->{script};
  3728. $self->{script} .= "; " if $self->{script};
  3729. $self->{script} .= qq($sub in "$script", line $line);
  3730. }
  3731. $self->{builder} = $builder;
  3732. push (@{$self->{sources}}, @sources);
  3733. @{$self->{dep}} = () if !defined $self->{dep};
  3734. $self->{conscript} = $priv::self->{script};
  3735. }
  3736. sub is_under
  3737. {
  3738. $_[0]->{dir}->is_under($_[1]);
  3739. }
  3740. sub relpath
  3741. {
  3742. my $dirpath = $_[0]->relpath($_[1]->{dir});
  3743. if (!$dirpath)
  3744. {
  3745. return $_[1]->{entry};
  3746. }
  3747. else
  3748. {
  3749. File::Spec->catfile($dirpath, $_[1]->{entry});
  3750. }
  3751. }
  3752. # Return the signature array for this file.
  3753. # This probably belongs in its own "sigarray" package,
  3754. # which would make it easier to optimize performance.
  3755. sub sigarray
  3756. {
  3757. if ($_[0]->{sigaref})
  3758. {
  3759. return @{$_[0]->{sigaref}};
  3760. }
  3761. my $self = shift;
  3762. # glob2pat based on The Perl Cookbook, p. 180.
  3763. sub glob2pat
  3764. {
  3765. my $globstr = shift;
  3766. my %patmap = (
  3767. '*' => '.*',
  3768. '?' => '.',
  3769. '[' => '[',
  3770. ']' => ']',
  3771. '/' => "\Q$dir::SEPARATOR", # Cons-specific modification
  3772. );
  3773. $globstr =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
  3774. return '^' . $globstr . '$';
  3775. }
  3776. my @sigarray;
  3777. my $default;
  3778. my $builder = $self->lfile->{builder};
  3779. if (!$builder)
  3780. {
  3781. @sigarray = @$param::sourcesig;
  3782. $default = [qw(content)];
  3783. }
  3784. else
  3785. {
  3786. if ($builder->{env} && $builder->{env}->{SIGNATURE})
  3787. {
  3788. @sigarray = @{$builder->{env}->{SIGNATURE}};
  3789. }
  3790. else
  3791. {
  3792. my $class = ref $builder;
  3793. my $path = $self->path;
  3794. warn qq($0: Warning: Builder package $class did not record\n)
  3795. . qq(\tthe calling environment for '$path'.\n)
  3796. . qq(\tUnable to use any %SIGNATURE construction variable\n)
  3797. . qq(\tfor signature configuration.\n);
  3798. }
  3799. $default = [qw(build)];
  3800. }
  3801. my $path = $self->path;
  3802. while (@sigarray)
  3803. {
  3804. my ($glob, $aref) = splice(@sigarray, 0, 2);
  3805. my $re = glob2pat($glob);
  3806. if ($path =~ /$re/)
  3807. {
  3808. $aref = [split (/\s+/, $aref)] if !ref $aref;
  3809. $self->{sigaref} = $aref;
  3810. return @$aref;
  3811. }
  3812. }
  3813. $self->{sigaref} = $default;
  3814. return @{$self->{sigaref}};
  3815. }
  3816. # Decide if this file's signature should be the content or build signature.
  3817. sub sigtype
  3818. {
  3819. if ($_[0]->{sigtype})
  3820. {
  3821. return $_[0]->{sigtype};
  3822. }
  3823. my $self = shift;
  3824. my @sigarray = $self->sigarray;
  3825. my $sigtype;
  3826. if (grep($_ eq "build", @sigarray))
  3827. {
  3828. $sigtype = 'bsig';
  3829. }
  3830. elsif (grep($_ =~ /content$/, @sigarray))
  3831. {
  3832. $sigtype = 'csig';
  3833. }
  3834. return $self->{sigtype} = $sigtype;
  3835. }
  3836. # Return whether this file is configured to use stored
  3837. # signature values from the .consign file.
  3838. sub stored
  3839. {
  3840. if (!defined $_[0]->{stored})
  3841. {
  3842. $_[0]->{stored} = grep($_ eq "stored-content", $_[0]->sigarray);
  3843. }
  3844. return $_[0]->{stored};
  3845. }
  3846. # Generic entry (file or directory) handling.
  3847. # This is an empty subclass for nodes that haven't
  3848. # quite decided whether they're files or dirs.
  3849. # Use file methods until someone blesses them one way or the other.
  3850. package entry;
  3851. use vars qw( @ISA );
  3852. BEGIN { @ISA = qw(file) }
  3853. # File utilities
  3854. package futil;
  3855. # Install one file as another.
  3856. # Links them if possible (hard link), otherwise copies.
  3857. # Don't ask why, but the source is a path, the tgt is a file obj.
  3858. sub install
  3859. {
  3860. my ($sp, $tgt) = @_;
  3861. my ($tp) = $tgt->path;
  3862. return 1 if $tp eq $sp;
  3863. return 1 if eval { link($sp, $tp) };
  3864. unlink($tp);
  3865. if (!futil::mkdir($tgt->{dir}))
  3866. {
  3867. return undef;
  3868. }
  3869. return 1 if eval { link($sp, $tp) };
  3870. futil::copy($sp, $tp);
  3871. }
  3872. # Copy one file to another. Arguments are actual file names.
  3873. # Returns undef on failure. Preserves mtime and mode.
  3874. sub copy
  3875. {
  3876. my ($sp, $tp) = @_;
  3877. my ($mode, $length, $atime, $mtime) = (stat($sp))[2, 7, 8, 9];
  3878. # Use Perl standard library module for file copying, which handles
  3879. # binary copies. <schwarze@isa.de> 1998-06-18
  3880. if (!File::Copy::copy($sp, $tp))
  3881. {
  3882. warn qq($0: can\'t install "$sp" to "$tp" ($!)\n); #'
  3883. return undef;
  3884. }
  3885. # The file has been created, so try both the chmod and utime,
  3886. # first making sure the copy is writable (because permissions
  3887. # affect the ability to modify file times on some operating
  3888. # systems), and then changing permissions back if necessary.
  3889. my $ret = 1;
  3890. my $wmode = $mode | 0700;
  3891. if (!chmod $wmode, $tp)
  3892. {
  3893. warn qq($0: can\'t set mode $wmode on file "$tp" ($!)\n); #'
  3894. $ret = undef;
  3895. }
  3896. if (!utime $atime, $mtime, $tp)
  3897. {
  3898. warn qq($0: can\'t set modification time for file "$tp" ($!)\n); #'
  3899. $ret = undef;
  3900. }
  3901. if ($mode != $wmode && !chmod $mode, $tp)
  3902. {
  3903. warn qq($0: can\'t set mode $mode on file "$tp" ($!)\n); #'
  3904. $ret = undef;
  3905. }
  3906. return $ret;
  3907. }
  3908. # Ensure that the specified directory exists.
  3909. # Aborts on failure.
  3910. sub mkdir
  3911. {
  3912. return 1 if $_[0]->{'exists'};
  3913. if (!futil::mkdir($_[0]->{dir}))
  3914. { # Recursively make parent.
  3915. return undef;
  3916. }
  3917. my ($path) = $_[0]->path;
  3918. if (!-d $path && !mkdir($path, 0777))
  3919. {
  3920. warn qq($0: can't create directory $path ($!).\n); #'
  3921. return undef;
  3922. }
  3923. $_[0]->{'exists'} = 1;
  3924. }
  3925. # Signature package.
  3926. package sig::hash;
  3927. use vars qw( $called );
  3928. sub init
  3929. {
  3930. my ($dir) = @_;
  3931. my ($consign) = $dir->prefix . ".consign";
  3932. my ($dhash) = $dir->{consign} = {};
  3933. if (-f $consign)
  3934. {
  3935. open(CONSIGN, $consign) || die ("$0: can't open $consign ($!)\n");
  3936. while (<CONSIGN>)
  3937. {
  3938. chop;
  3939. my ($file, $sig) = split (/:/, $_);
  3940. $dhash->{$file} = $sig;
  3941. }
  3942. close(CONSIGN);
  3943. }
  3944. $dhash;
  3945. }
  3946. # Read the hash entry for a particular file.
  3947. sub in
  3948. {
  3949. my ($dir) = $_[0]->{dir};
  3950. ($dir->{consign} || init($dir))->{$_[0]->{entry}};
  3951. }
  3952. # Write the hash entry for a particular file.
  3953. sub out
  3954. {
  3955. my ($file, $sig) = @_;
  3956. my ($dir) = $file->{dir};
  3957. ($dir->{consign} || init($dir))->{$file->{entry}} = $sig;
  3958. $sig::hash::dirty{$dir} = $dir;
  3959. }
  3960. # Eliminate the hash entry for a particular file.
  3961. sub clear
  3962. {
  3963. my ($file) = @_;
  3964. my ($dir) = $file->{dir};
  3965. delete $dir->{consign}->{$file->{entry}} if $dir->{consign};
  3966. $sig::hash::dirty{$dir} = $dir;
  3967. }
  3968. # Flush hash entries. Called at end or via ^C interrupt.
  3969. sub END
  3970. {
  3971. return if $called++; # May be called twice.
  3972. close(CONSIGN); # in case this came in via ^C.
  3973. my $dir;
  3974. for $dir (values %sig::hash::dirty)
  3975. {
  3976. my ($consign) = $dir->prefix . ".consign";
  3977. my ($constemp) = $consign . ".$$";
  3978. if (!open(CONSIGN, ">$constemp"))
  3979. {
  3980. die ("$0: can't create $constemp ($!)\n");
  3981. }
  3982. my ($entry, $sig);
  3983. while (($entry, $sig) = each %{$dir->{consign}})
  3984. {
  3985. if (!print CONSIGN "$entry:$sig\n")
  3986. {
  3987. die ("$0: error writing to $constemp ($!)\n");
  3988. }
  3989. }
  3990. close(CONSIGN);
  3991. if (!rename($constemp, $consign))
  3992. {
  3993. if (futil::copy($constemp, $consign))
  3994. {
  3995. unlink($constemp);
  3996. }
  3997. else
  3998. {
  3999. die ("$0: couldn't rename or copy $constemp to $consign "
  4000. . "($!)\n");
  4001. }
  4002. }
  4003. }
  4004. }
  4005. # Derived file caching.
  4006. package cache;
  4007. # Find a file in the cache. Return non-null if the file is in the cache.
  4008. sub in
  4009. {
  4010. return undef unless $param::cache;
  4011. my ($file, $sig) = @_;
  4012. # Add the path to the signature, to make it unique.
  4013. $sig = 'sig'->collect($sig, $file->path) unless $param::mixtargets;
  4014. my ($dir) = substr($sig, 0, 1);
  4015. my ($cp) = File::Spec->catfile($param::cache, $dir, $sig);
  4016. return -f $cp && futil::install($cp, $file);
  4017. }
  4018. # Try to flush a file to the cache, if not already there.
  4019. # If it doesn't make it out, due to an error, then that doesn't
  4020. # really matter.
  4021. sub out
  4022. {
  4023. return unless $param::cache;
  4024. my ($file, $sig) = @_;
  4025. # Add the path to the signature, to make it unique.
  4026. $sig = 'sig'->collect($sig, $file->path) unless $param::mixtargets;
  4027. my ($dir) = substr($sig, 0, 1);
  4028. my ($sp) = $file->path;
  4029. my ($cp) = File::Spec->catfile($param::cache, $dir, $sig);
  4030. my ($cdir) = File::Spec->catfile($param::cache, $dir);
  4031. if (!-d $cdir)
  4032. {
  4033. mkdir($cdir, 0777)
  4034. || die ("$0: can't create cache directory $cdir ($!).\n");
  4035. }
  4036. elsif (-f $cp)
  4037. {
  4038. # Already cached: try to use that instead, to save space.
  4039. # This can happen if the -cs option is used on a previously
  4040. # uncached build, or if two builds occur simultaneously.
  4041. my ($lp) = ".$sig";
  4042. unlink($lp);
  4043. return if !eval { link($cp, $lp) };
  4044. rename($lp, $sp);
  4045. # Unix98 says, "If the old argument and the new argument both
  4046. # [refer] to the same existing file, the rename() function
  4047. # returns successfully and performs no other action." So, if
  4048. # $lp and $sp are links (i.e., $cp and $sp are links), $lp is
  4049. # left, and we must unlink it ourselves. If the rename failed
  4050. # for any reason, it is also good form to unlink the temporary
  4051. # $lp. Otherwise $lp no longer exists and, barring some race,
  4052. # the unlink fails silently.
  4053. unlink($lp);
  4054. return;
  4055. }
  4056. return if eval { link($sp, $cp) };
  4057. return if !-f $sp; # if nothing to cache.
  4058. if (futil::copy($sp, "$cp.new"))
  4059. {
  4060. rename("$cp.new", $cp);
  4061. }
  4062. }
  4063. # Generic signature handling package.
  4064. # This handles the higher-layer distinction between content and build
  4065. # signatures, relying on an underlying calculation package like
  4066. # "sig::md5"" to provide the signature values themselves.
  4067. package sig;
  4068. use vars qw( @ISA );
  4069. # Select the underlying package to be used for signature calculation.
  4070. # We play a few namespace games here. Specifically, we append
  4071. # "sig::" to the beginning of the subclass we're passed. Then,
  4072. # if the package ends in "::debug", we actually subclass the
  4073. # "sig::debug" package and as a wrapper around the underlying
  4074. # (e.g.) "sig::md5" package that's doing the real calculation.
  4075. sub select
  4076. {
  4077. my ($package, $subclass) = @_;
  4078. my $p = $package . "::" . $subclass;
  4079. my $sigpkg = $p;
  4080. if ($p =~ /(.*)::debug$/)
  4081. {
  4082. $sigpkg = $1;
  4083. $p = 'sig::debug';
  4084. }
  4085. @ISA = ($p);
  4086. $p->init($sigpkg);
  4087. };
  4088. # Set or return the build signature of a file.
  4089. # This is computed elsewhere and passed in to us.
  4090. sub bsig
  4091. {
  4092. my ($self, $file, $sig) = @_;
  4093. if (defined $sig)
  4094. {
  4095. $file->{'bsig'} = $sig;
  4096. $self->set($file);
  4097. }
  4098. elsif (!defined $file->{'bsig'})
  4099. {
  4100. $file->{'bsig'} = '';
  4101. }
  4102. $file->{'bsig'};
  4103. }
  4104. # Determine the content signature of a file.
  4105. # This also sets the .consign entry unless the file is in a
  4106. # repository; we don't write into repositories, only read from them.
  4107. sub csig
  4108. {
  4109. my ($self, $file) = @_;
  4110. if (!$file->{'csig'})
  4111. {
  4112. $file->{'csig'} = $self->srcsig($file->path);
  4113. $self->set($file) if !$file->is_on_rpath;
  4114. }
  4115. $_[1]->{'csig'};
  4116. }
  4117. # Determine the current signature of an already-existing or
  4118. # non-existant file. Unless a specific signature type (bsig
  4119. # or csig) is requested, this consults the file's signature
  4120. # array to decide whether to return content or build signature,
  4121. # and whether to use a cached value from a .consign file.
  4122. sub signature
  4123. {
  4124. my ($self, $file, $sigtype) = @_;
  4125. $sigtype = $file->sigtype if !$sigtype;
  4126. #open(TTY, ">/dev/tty");
  4127. #print TTY $file->path, ": $sigtype\n";
  4128. #close(TTY);
  4129. my ($path) = $file->path;
  4130. my ($time) = (stat($path))[9];
  4131. if ($time)
  4132. {
  4133. if ($file->{$sigtype})
  4134. {
  4135. return $file->{$sigtype};
  4136. }
  4137. if ($file->is_on_rpath || $file->stored)
  4138. {
  4139. if ('sig'->fetch($file) && $file->{$sigtype})
  4140. {
  4141. if ($file->{'sigtime'} == $time
  4142. || !$param::rep_sig_times_ok && $file->is_on_rpath)
  4143. {
  4144. return $file->{$sigtype};
  4145. }
  4146. }
  4147. $file->{$sigtype} = undef;
  4148. }
  4149. if ($file->is_on_rpath || !File::Spec->file_name_is_absolute($path))
  4150. {
  4151. my $sig = '';
  4152. if ($sigtype eq 'bsig') { $sig = $self->bsig($file); }
  4153. elsif ($sigtype eq 'csig') { $sig = $self->csig($file); }
  4154. return $sig;
  4155. }
  4156. # This file is not in a repository or under the local directory
  4157. # structure. In the canonical case, it's a utility that will be
  4158. # executed by a command. Historically, Cons has returned the
  4159. # name of the command concatenated with the modification time.
  4160. # Note that this is *not* the path ("cc" not "/bin/cc"), so it
  4161. # would lose in the unlikely event that a different copy of the
  4162. # utility was used that happened to have the same modification
  4163. # time (due to living in a different directory on the PATH, for
  4164. # example). The obvious "fix" of using the path like so, however:
  4165. # return $path . $time;
  4166. # is wrong. In a multi-machine build environment, different
  4167. # systems may have the same utility in different locations (due
  4168. # to different NFS mount points, for example), which would
  4169. # cause a lot of unnecessary builds if we used the full path.
  4170. # A better solution to strengthen this signature would be to
  4171. # also concatenate the size of the file, but that would cause
  4172. # unnecessary rebuilds when coming from .consign files that used
  4173. # the old scheme. All of which is to merely explain why we're
  4174. # leaving this as it has been, but documenting it here in case
  4175. # there's reason to change it in the future.
  4176. return $file->{entry} . $time;
  4177. }
  4178. return $file->{$sigtype} = '';
  4179. }
  4180. sub bsignature
  4181. {
  4182. my ($self, $file) = @_;
  4183. my ($path) = $file->path;
  4184. my ($time) = (stat($path))[9];
  4185. if ($time)
  4186. {
  4187. if ($file->{'bsig'})
  4188. {
  4189. return $file->{'bsig'};
  4190. }
  4191. if ('sig'->fetch($file, 'bsig') && $file->{'bsig'})
  4192. {
  4193. if ($file->{'sigtime'} == $time
  4194. || !$param::rep_sig_times_ok && $file->is_on_rpath)
  4195. {
  4196. return $file->{'bsig'};
  4197. }
  4198. }
  4199. if ($file->is_on_rpath || !File::Spec->file_name_is_absolute($path))
  4200. {
  4201. return $self->bsig($file);
  4202. }
  4203. return $path . $time;
  4204. }
  4205. return $file->{'bsig'} = '';
  4206. }
  4207. # Invalidate a file's signature, also clearing its .consign entry.
  4208. sub invalidate
  4209. {
  4210. my ($self, $file) = @_;
  4211. delete $file->{'sigtime'};
  4212. delete $file->{'bsig'};
  4213. delete $file->{'csig'};
  4214. sig::hash::clear($file);
  4215. }
  4216. # Store the signature for a file.
  4217. sub set
  4218. {
  4219. my ($self, $file) = @_;
  4220. my $sig = (stat($file->path))[9];
  4221. $sig .= " " . ($file->{'bsig'} || '-');
  4222. $sig .= " " . $file->{'csig'} if $file->{'csig'};
  4223. sig::hash::out($file, $sig);
  4224. }
  4225. # Fetch the signature(s) for a file.
  4226. # Returns whether there was a signature to fetch.
  4227. sub fetch
  4228. {
  4229. my ($self, $file, @kw) = @_;
  4230. @kw = ('bsig', 'csig') if !@kw;
  4231. my $sig = sig::hash::in($file) || '';
  4232. my ($sigtime, $bsig, $csig) = split (/ /, $sig);
  4233. $file->{'sigtime'} = $sigtime;
  4234. $file->{'bsig'} = $bsig || '' if grep($_ eq 'bsig', @kw);
  4235. $file->{'csig'} = $csig || '' if grep($_ eq 'csig', @kw);
  4236. $file->{'bsig'} = '' if $file->{'bsig'} eq '-';
  4237. return $sig ne '';
  4238. }
  4239. # MD5-based signature package.
  4240. package sig::md5;
  4241. use vars qw( $md5 );
  4242. # Initialize MD5 signature calculation by finding an appropriate
  4243. # module and creating the proper object.
  4244. sub init
  4245. {
  4246. my $self = shift;
  4247. my @md5_modules = qw(Digest::MD5 MD5 Digest::Perl::MD5);
  4248. # We used to find the right module more simply, using $_ as the
  4249. # loop iterator and just doing:
  4250. #
  4251. # eval "use $_";
  4252. # $module = $_, $last if ! $@;
  4253. #
  4254. # in the loop. Empirically, though, this doesn't pass back the
  4255. # right value in $module on some ActiveState versions. (Maybe
  4256. # it's something to do with the eval in a for loop, I dunno.)
  4257. # Work around it by using $_ to pass the value out of the loop,
  4258. # which seems to work everywhere.
  4259. my $module;
  4260. for $module (@md5_modules)
  4261. {
  4262. eval "use $module";
  4263. $_ = $module, last if !$@;
  4264. }
  4265. $module = $_;
  4266. die "Cannot find any MD5 module from: @md5_modules" if $@;
  4267. $md5 = new $module;
  4268. }
  4269. # Is the provided signature equal to the signature of the current
  4270. # instantiation of the target (and does the target exist)?
  4271. sub current
  4272. {
  4273. my ($self, $file, $sig, $sigtype) = @_;
  4274. $self->bsignature($file) eq $sig;
  4275. }
  4276. # Return an aggregate signature for a list of signature values.
  4277. sub collect
  4278. {
  4279. my ($self, @sigs) = @_;
  4280. # The following sequence is faster than calling the hex interface.
  4281. $md5->reset();
  4282. $md5->add(join ('', $param::salt, @sigs));
  4283. unpack("H*", $md5->digest());
  4284. }
  4285. # Directly compute a file signature as the MD5 checksum of the
  4286. # bytes in the file.
  4287. sub srcsig
  4288. {
  4289. my ($self, $path) = @_;
  4290. $md5->reset();
  4291. open(FILE, $path) || return '';
  4292. binmode(FILE);
  4293. $md5->addfile(\*FILE);
  4294. close(FILE);
  4295. unpack("H*", $md5->digest());
  4296. }
  4297. # Compute the signature of a command string.
  4298. # For MD5, this is just the string itself, since MD5 will condense
  4299. # the string contents into the ultimate signature. Other signature
  4300. # schemes may need to figure this out differently.
  4301. sub cmdsig
  4302. {
  4303. my ($self, $sig) = @_;
  4304. return $sig;
  4305. }
  4306. # Generic debug package for signature calculation.
  4307. # Because of the way we're called by sig::select() and then use
  4308. # the specified value to set up @ISA, this package is essentially a
  4309. # factory that creates packages like sig::md5::debug, etc., on the fly.
  4310. package sig::debug;
  4311. use vars qw( @ISA $sigpkg $outfh );
  4312. local *FH;
  4313. sub init
  4314. {
  4315. my $self = shift;
  4316. $sigpkg = shift;
  4317. @ISA = ($sigpkg);
  4318. $sigpkg->init();
  4319. my $file = $ENV{CONS_SIG_DEBUG};
  4320. if ($file)
  4321. {
  4322. if (!open(FH, ">$file"))
  4323. {
  4324. die "Cannot open $file: $!";
  4325. }
  4326. $outfh = \*FH;
  4327. }
  4328. else
  4329. {
  4330. $outfh = \*STDOUT;
  4331. }
  4332. }
  4333. sub current
  4334. {
  4335. my ($self, $file, $sig, $sigtype) = @_;
  4336. my $fsig = $self->bsignature($file);
  4337. my $sub = "${sigpkg}::current";
  4338. my $sep = "\n" . ' ' x (length($sub) + 1 - 3);
  4339. print $outfh "$sub(|$fsig|${sep}eq |$sig|)\n";
  4340. return $fsig eq $sig;
  4341. }
  4342. sub collect
  4343. {
  4344. my ($self, @sigs) = @_;
  4345. my $sig = $sigpkg->collect(@sigs);
  4346. my $sub = "${sigpkg}::collect";
  4347. my $sep = ",\n" . ' ' x (length($sub) + 1);
  4348. my $buf = join ($sep, @sigs);
  4349. $buf = $param::salt . $sep . $buf if $param::salt;
  4350. print $outfh "$sub($buf)\n\t=> |$sig|\n";
  4351. return $sig;
  4352. }
  4353. sub srcsig
  4354. {
  4355. my ($self, $path) = @_;
  4356. my $sig = $sigpkg->srcsig($path);
  4357. print $outfh "${sigpkg}::srcsig($path)\n\t=> |$sig|\n";
  4358. return $sig;
  4359. }
  4360. __END__;
  4361. =head1 NAME
  4362. Cons - A Software Construction System
  4363. =head1 DESCRIPTION
  4364. A guide and reference for version __VERSION____REVISION__
  4365. Copyright (c) 1996-2001 Free Software Foundation, Inc.
  4366. This program is free software; you can redistribute it and/or modify
  4367. it under the terms of the GNU General Public License as published by
  4368. the Free Software Foundation; either version 2 of the License, or
  4369. (at your option) any later version.
  4370. This program is distributed in the hope that it will be useful,
  4371. but WITHOUT ANY WARRANTY; without even the implied warranty of
  4372. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  4373. GNU General Public License for more details.
  4374. You should have received a copy of the GNU General Public License
  4375. along with this program; see the file COPYING. If not, write to
  4376. the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  4377. Boston, MA 02111-1307, USA.
  4378. =head1 Introduction
  4379. B<Cons> is a system for constructing, primarily, software, but is quite
  4380. different from previous software construction systems. Cons was designed
  4381. from the ground up to deal easily with the construction of software spread
  4382. over multiple source directories. Cons makes it easy to create build scripts
  4383. that are simple, understandable and maintainable. Cons ensures that complex
  4384. software is easily and accurately reproducible.
  4385. Cons uses a number of techniques to accomplish all of this. Construction
  4386. scripts are just Perl scripts, making them both easy to comprehend and very
  4387. flexible. Global scoping of variables is replaced with an import/export
  4388. mechanism for sharing information between scripts, significantly improving
  4389. the readability and maintainability of each script. B<Construction
  4390. environments> are introduced: these are Perl objects that capture the
  4391. information required for controlling the build process. Multiple
  4392. environments are used when different semantics are required for generating
  4393. products in the build tree. Cons implements automatic dependency analysis
  4394. and uses this to globally sequence the entire build. Variant builds are
  4395. easily produced from a single source tree. Intelligent build subsetting is
  4396. possible, when working on localized changes. Overrides can be setup to
  4397. easily override build instructions without modifying any scripts. MD5
  4398. cryptographic B<signatures> are associated with derived files, and are used
  4399. to accurately determine whether a given file needs to be rebuilt.
  4400. While offering all of the above, and more, Cons remains simple and easy to
  4401. use. This will, hopefully, become clear as you read the remainder of this
  4402. document.
  4403. =head1 Why Cons? Why not Make?
  4404. Cons is a B<make> replacement. In the following paragraphs, we look at a few
  4405. of the undesirable characteristics of make--and typical build environments
  4406. based on make--that motivated the development of Cons.
  4407. =head2 Build complexity
  4408. Traditional make-based systems of any size tend to become quite complex. The
  4409. original make utility and its derivatives have contributed to this tendency
  4410. in a number of ways. Make is not good at dealing with systems that are
  4411. spread over multiple directories. Various work-arounds are used to overcome
  4412. this difficulty; the usual choice is for make to invoke itself recursively
  4413. for each sub-directory of a build. This leads to complicated code, in which
  4414. it is often unclear how a variable is set, or what effect the setting of a
  4415. variable will have on the build as a whole. The make scripting language has
  4416. gradually been extended to provide more possibilities, but these have
  4417. largely served to clutter an already overextended language. Often, builds
  4418. are done in multiple passes in order to provide appropriate products from
  4419. one directory to another directory. This represents a further increase in
  4420. build complexity.
  4421. =head2 Build reproducibility
  4422. The bane of all makes has always been the correct handling of
  4423. dependencies. Most often, an attempt is made to do a reasonable job of
  4424. dependencies within a single directory, but no serious attempt is made to do
  4425. the job between directories. Even when dependencies are working correctly,
  4426. make's reliance on a simple time stamp comparison to determine whether a
  4427. file is out of date with respect to its dependents is not, in general,
  4428. adequate for determining when a file should be rederived. If an external
  4429. library, for example, is rebuilt and then ``snapped'' into place, the
  4430. timestamps on its newly created files may well be earlier than the last
  4431. local build, since it was built before it became visible.
  4432. =head2 Variant builds
  4433. Make provides only limited facilities for handling variant builds. With the
  4434. proliferation of hardware platforms and the need for debuggable
  4435. vs. optimized code, the ability to easily create these variants is
  4436. essential. More importantly, if variants are created, it is important to
  4437. either be able to separate the variants or to be able to reproduce the
  4438. original or variant at will. With make it is very difficult to separate the
  4439. builds into multiple build directories, separate from the source. And if
  4440. this technique isn't used, it's also virtually impossible to guarantee at
  4441. any given time which variant is present in the tree, without resorting to a
  4442. complete rebuild.
  4443. =head2 Repositories
  4444. Make provides only limited support for building software from code that
  4445. exists in a central repository directory structure. The VPATH feature of
  4446. GNU make (and some other make implementations) is intended to provide this,
  4447. but doesn't work as expected: it changes the path of target file to the
  4448. VPATH name too early in its analysis, and therefore searches for all
  4449. dependencies in the VPATH directory. To ensure correct development builds,
  4450. it is important to be able to create a file in a local build directory and
  4451. have any files in a code repository (a VPATH directory, in make terms) that
  4452. depend on the local file get rebuilt properly. This isn't possible with
  4453. VPATH, without coding a lot of complex repository knowledge directly into
  4454. the makefiles.
  4455. =head1 Keeping it simple
  4456. A few of the difficulties with make have been cited above. In this and
  4457. subsequent sections, we shall introduce Cons and show how these issues are
  4458. addressed.
  4459. =head2 Perl scripts
  4460. Cons is Perl-based. That is, Cons scripts--F<Conscript> and F<Construct>
  4461. files, the equivalent to F<Makefile> or F<makefile>--are all written in
  4462. Perl. This provides an immediate benefit: the language for writing scripts
  4463. is a familiar one. Even if you don't happen to be a Perl programmer, it
  4464. helps to know that Perl is basically just a simple declarative language,
  4465. with a well-defined flow of control, and familiar semantics. It has
  4466. variables that behave basically the way you would expect them to,
  4467. subroutines, flow of control, and so on. There is no special syntax
  4468. introduced for Cons. The use of Perl as a scripting language simplifies
  4469. the task of expressing the appropriate solution to the often complex
  4470. requirements of a build.
  4471. =head2 Hello, World!
  4472. To ground the following discussion, here's how you could build the B<Hello,
  4473. World!> C application with Cons:
  4474. $env = new cons();
  4475. Program $env 'hello', 'hello.c';
  4476. If you install this script in a directory, naming the script F<Construct>,
  4477. and create the F<hello.c> source file in the same directory, then you can
  4478. type C<cons hello> to build the application:
  4479. % cons hello
  4480. cc -c hello.c -o hello.o
  4481. cc -o hello hello.o
  4482. =head2 Construction environments
  4483. A key simplification of Cons is the idea of a B<construction environment>. A
  4484. construction environment is an B<object> characterized by a set of key/value
  4485. pairs and a set of B<methods>. In order to tell Cons how to build something,
  4486. you invoke the appropriate method via an appropriate construction
  4487. environment. Consider the following example:
  4488. $env = new cons(
  4489. CC => 'gcc',
  4490. LIBS => 'libworld.a'
  4491. );
  4492. Program $env 'hello', 'hello.c';
  4493. In this case, rather than using the default construction environment, as is,
  4494. we have overridden the value of C<CC> so that the GNU C Compiler equivalent
  4495. is used, instead. Since this version of B<Hello, World!> requires a library,
  4496. F<libworld.a>, we have specified that any program linked in this environment
  4497. should be linked with that library. If the library exists already, well and
  4498. good, but if not, then we'll also have to include the statement:
  4499. Library $env 'libworld', 'world.c';
  4500. Now if you type C<cons hello>, the library will be built before the program
  4501. is linked, and, of course, C<gcc> will be used to compile both modules:
  4502. % cons hello
  4503. gcc -c hello.c -o hello.o
  4504. gcc -c world.c -o world.o
  4505. ar r libworld.a world.o
  4506. ar: creating libworld.a
  4507. ranlib libworld.a
  4508. gcc -o hello hello.o libworld.a
  4509. =head2 Automatic and complete dependency analysis
  4510. With Cons, dependencies are handled automatically. Continuing the previous
  4511. example, note that when we modify F<world.c>, F<world.o> is recompiled,
  4512. F<libworld.a> recreated, and F<hello> relinked:
  4513. % vi world.c
  4514. [EDIT]
  4515. % cons hello
  4516. gcc -c world.c -o world.o
  4517. ar r libworld.a world.o
  4518. ar: creating libworld.a
  4519. ranlib libworld.a
  4520. gcc -o hello hello.o libworld.a
  4521. This is a relatively simple example: Cons ``knows'' F<world.o> depends upon
  4522. F<world.c>, because the dependency is explicitly set up by the C<Library>
  4523. method. It also knows that F<libworld.a> depends upon F<world.o> and that
  4524. F<hello> depends upon F<libworld.a>, all for similar reasons.
  4525. Now it turns out that F<hello.c> also includes the interface definition
  4526. file, F<world.h>:
  4527. % emacs world.h
  4528. [EDIT]
  4529. % cons hello
  4530. gcc -c hello.c -o hello.o
  4531. gcc -o hello hello.o libworld.a
  4532. How does Cons know that F<hello.c> includes F<world.h>, and that F<hello.o>
  4533. must therefore be recompiled? For now, suffice it to say that when
  4534. considering whether or not F<hello.o> is up-to-date, Cons invokes a scanner
  4535. for its dependency, F<hello.c>. This scanner enumerates the files included
  4536. by F<hello.c> to come up with a list of further dependencies, beyond those
  4537. made explicit by the Cons script. This process is recursive: any files
  4538. included by included files will also be scanned.
  4539. Isn't this expensive? The answer is--it depends. If you do a full build of a
  4540. large system, the scanning time is insignificant. If you do a rebuild of a
  4541. large system, then Cons will spend a fair amount of time thinking about it
  4542. before it decides that nothing has to be done (although not necessarily more
  4543. time than make!). The good news is that Cons makes it very easy to
  4544. intelligently subset your build, when you are working on localized changes.
  4545. =head2 Automatic global build sequencing
  4546. Because Cons does full and accurate dependency analysis, and does this
  4547. globally, for the entire build, Cons is able to use this information to take
  4548. full control of the B<sequencing> of the build. This sequencing is evident
  4549. in the above examples, and is equivalent to what you would expect for make,
  4550. given a full set of dependencies. With Cons, this extends trivially to
  4551. larger, multi-directory builds. As a result, all of the complexity involved
  4552. in making sure that a build is organized correctly--including multi-pass
  4553. hierarchical builds--is eliminated. We'll discuss this further in the next
  4554. sections.
  4555. =head1 Building large trees--still just as simple
  4556. =head2 A hierarchy of build scripts
  4557. A larger build, in Cons, is organized by creating a hierarchy of B<build
  4558. scripts>. At the top of the tree is a script called F<Construct>. The rest
  4559. of the scripts, by convention, are each called F<Conscript>. These scripts
  4560. are connected together, very simply, by the C<Build>, C<Export>, and
  4561. C<Import> commands.
  4562. =head2 The Build command
  4563. The C<Build> command takes a list of F<Conscript> file names, and arranges
  4564. for them to be included in the build. For example:
  4565. Build qw(
  4566. drivers/display/Conscript
  4567. drivers/mouse/Conscript
  4568. parser/Conscript
  4569. utilities/Conscript
  4570. );
  4571. This is a simple two-level hierarchy of build scripts: all the subsidiary
  4572. F<Conscript> files are mentioned in the top-level F<Construct> file. Notice
  4573. that not all directories in the tree necessarily have build scripts
  4574. associated with them.
  4575. This could also be written as a multi-level script. For example, the
  4576. F<Construct> file might contain this command:
  4577. Build qw(
  4578. parser/Conscript
  4579. drivers/Conscript
  4580. utilities/Conscript
  4581. );
  4582. and the F<Conscript> file in the F<drivers> directory might contain this:
  4583. Build qw(
  4584. display/Conscript
  4585. mouse/Conscript
  4586. );
  4587. Experience has shown that the former model is a little easier to understand,
  4588. since the whole construction tree is laid out in front of you, at the
  4589. top-level. Hybrid schemes are also possible. A separately maintained
  4590. component that needs to be incorporated into a build tree, for example,
  4591. might hook into the build tree in one place, but define its own construction
  4592. hierarchy.
  4593. By default, Cons does not change its working directory to the directory
  4594. containing a subsidiary F<Conscript> file it is including. This behavior
  4595. can be enabled for a build by specifying, in the top-level F<Construct>
  4596. file:
  4597. Conscript_chdir 1;
  4598. When enabled, Cons will change to the subsidiary F<Conscript> file's
  4599. containing directory while reading in that file, and then change back
  4600. to the top-level directory once the file has been processed.
  4601. It is expected that this behavior will become the default in some future
  4602. version of Cons. To prepare for this transition, builds that expect
  4603. Cons to remain at the top of the build while it reads in a subsidiary
  4604. F<Conscript> file should explicitly disable this feature as follows:
  4605. Conscript_chdir 0;
  4606. =head2 Relative, top-relative, and absolute file names
  4607. You may have noticed that the file names specified to the Build command are
  4608. relative to the location of the script it is invoked from. This is generally
  4609. true for other filename arguments to other commands, too, although we might
  4610. as well mention here that if you begin a file name with a hash mark, ``#'',
  4611. then that file is interpreted relative to the top-level directory (where the
  4612. F<Construct> file resides). And, not surprisingly, if you begin it with ``/'',
  4613. then it is considered to be an absolute pathname. This is true even on
  4614. systems which use a back slash rather than a forward slash to name absolute
  4615. paths.
  4616. (There is another file prefix, ``!'', that is interpreted specially by
  4617. Cons. See discussion of the C<Link> command, below, for details.)
  4618. =head2 Using modules in build scripts
  4619. You may pull modules into each F<Conscript> file using the normal Perl
  4620. C<use> or C<require> statements:
  4621. use English;
  4622. require My::Module;
  4623. Each C<use> or C<require> only affects the one F<Conscript> file in which
  4624. it appears. To use a module in multiple F<Conscript> files, you must
  4625. put a C<use> or C<require> statement in each one that needs the module.
  4626. =head2 Scope of variables
  4627. The top-level F<Construct> file and all F<Conscript> files begin life in
  4628. a common, separate Perl package. B<Cons> controls the symbol table for
  4629. the package so that, the symbol table for each script is empty, except
  4630. for the F<Construct> file, which gets some of the command line arguments.
  4631. All of the variables that are set or used, therefore, are set by the
  4632. script itself--not by some external script.
  4633. Variables can be explicitly B<imported> by a script from its parent
  4634. script. To import a variable, it must have been B<exported> by the parent
  4635. and initialized (otherwise an error will occur).
  4636. =head2 The Export command
  4637. The C<Export> command is used as in the following example:
  4638. $env = new cons();
  4639. $INCLUDE = "#export/include";
  4640. $LIB = "#export/lib";
  4641. Export qw( env INCLUDE LIB );
  4642. Build qw( util/Conscript );
  4643. The values of the simple variables mentioned in the C<Export> list will be
  4644. squirreled away by any subsequent C<Build> commands. The C<Export> command
  4645. will only export Perl B<scalar> variables, that is, variables whose name
  4646. begins with C<$>. Other variables, objects, etc. can be exported by
  4647. reference--but all scripts will refer to the same object, and this object
  4648. should be considered to be read-only by the subsidiary scripts and by the
  4649. original exporting script. It's acceptable, however, to assign a new value
  4650. to the exported scalar variable--that won't change the underlying variable
  4651. referenced. This sequence, for example, is OK:
  4652. $env = new cons();
  4653. Export qw( env INCLUDE LIB );
  4654. Build qw( util/Conscript );
  4655. $env = new cons(CFLAGS => '-O');
  4656. Build qw( other/Conscript );
  4657. It doesn't matter whether the variable is set before or after the C<Export>
  4658. command. The important thing is the value of the variable at the time the
  4659. C<Build> command is executed. This is what gets squirreled away. Any
  4660. subsequent C<Export> commands, by the way, invalidate the first: you must
  4661. mention all the variables you wish to export on each C<Export> command.
  4662. =head2 The Import command
  4663. Variables exported by the C<Export> command can be imported into subsidiary
  4664. scripts by the C<Import> command. The subsidiary script always imports
  4665. variables directly from the superior script. Consider this example:
  4666. Import qw( env INCLUDE );
  4667. This is only legal if the parent script exported both C<$env> and
  4668. C<$INCLUDE>. It also must have given each of these variables values. It is
  4669. OK for the subsidiary script to only import a subset of the exported
  4670. variables (in this example, C<$LIB>, which was exported by the previous
  4671. example, is not imported).
  4672. All the imported variables are automatically re-exported, so the sequence:
  4673. Import qw ( env INCLUDE );
  4674. Build qw ( beneath-me/Conscript );
  4675. will supply both C<$env> and C<$INCLUDE> to the subsidiary file. If only
  4676. C<$env> is to be exported, then the following will suffice:
  4677. Import qw ( env INCLUDE );
  4678. Export qw ( env );
  4679. Build qw ( beneath-me/Conscript );
  4680. Needless to say, the variables may be modified locally before invoking
  4681. C<Build> on the subsidiary script.
  4682. =head2 Build script evaluation order
  4683. The only constraint on the ordering of build scripts is that superior
  4684. scripts are evaluated before their inferior scripts. The top-level
  4685. F<Construct> file, for instance, is evaluated first, followed by any
  4686. inferior scripts. This is all you really need to know about the evaluation
  4687. order, since order is generally irrelevant. Consider the following C<Build>
  4688. command:
  4689. Build qw(
  4690. drivers/display/Conscript
  4691. drivers/mouse/Conscript
  4692. parser/Conscript
  4693. utilities/Conscript
  4694. );
  4695. We've chosen to put the script names in alphabetical order, simply because
  4696. that's the most convenient for maintenance purposes. Changing the order will
  4697. make no difference to the build.
  4698. =head1 A Model for sharing files
  4699. =head2 Some simple conventions
  4700. In any complex software system, a method for sharing build products needs to
  4701. be established. We propose a simple set of conventions which are trivial to
  4702. implement with Cons, but very effective.
  4703. The basic rule is to require that all build products which need to be shared
  4704. between directories are shared via an intermediate directory. We have
  4705. typically called this F<export>, and, in a C environment, provided
  4706. conventional sub-directories of this directory, such as F<include>, F<lib>,
  4707. F<bin>, etc.
  4708. These directories are defined by the top-level F<Construct> file. A simple
  4709. F<Construct> file for a B<Hello, World!> application, organized using
  4710. multiple directories, might look like this:
  4711. # Construct file for Hello, World!
  4712. # Where to put all our shared products.
  4713. $EXPORT = '#export';
  4714. Export qw( CONS INCLUDE LIB BIN );
  4715. # Standard directories for sharing products.
  4716. $INCLUDE = "$EXPORT/include";
  4717. $LIB = "$EXPORT/lib";
  4718. $BIN = "$EXPORT/bin";
  4719. # A standard construction environment.
  4720. $CONS = new cons (
  4721. CPPPATH => $INCLUDE, # Include path for C Compilations
  4722. LIBPATH => $LIB, # Library path for linking programs
  4723. LIBS => '-lworld', # List of standard libraries
  4724. );
  4725. Build qw(
  4726. hello/Conscript
  4727. world/Conscript
  4728. );
  4729. The F<world> directory's F<Conscript> file looks like this:
  4730. # Conscript file for directory world
  4731. Import qw( CONS INCLUDE LIB );
  4732. # Install the products of this directory
  4733. Install $CONS $LIB, 'libworld.a';
  4734. Install $CONS $INCLUDE, 'world.h';
  4735. # Internal products
  4736. Library $CONS 'libworld.a', 'world.c';
  4737. and the F<hello> directory's F<Conscript> file looks like this:
  4738. # Conscript file for directory hello
  4739. Import qw( CONS BIN );
  4740. # Exported products
  4741. Install $CONS $BIN, 'hello';
  4742. # Internal products
  4743. Program $CONS 'hello', 'hello.c';
  4744. To construct a B<Hello, World!> program with this directory structure, go to
  4745. the top-level directory, and invoke C<cons> with the appropriate
  4746. arguments. In the following example, we tell Cons to build the directory
  4747. F<export>. To build a directory, Cons recursively builds all known products
  4748. within that directory (only if they need rebuilding, of course). If any of
  4749. those products depend upon other products in other directories, then those
  4750. will be built, too.
  4751. % cons export
  4752. Install world/world.h as export/include/world.h
  4753. cc -Iexport/include -c hello/hello.c -o hello/hello.o
  4754. cc -Iexport/include -c world/world.c -o world/world.o
  4755. ar r world/libworld.a world/world.o
  4756. ar: creating world/libworld.a
  4757. ranlib world/libworld.a
  4758. Install world/libworld.a as export/lib/libworld.a
  4759. cc -o hello/hello hello/hello.o -Lexport/lib -lworld
  4760. Install hello/hello as export/bin/hello
  4761. =head2 Clean, understandable, location-independent scripts
  4762. You'll note that the two F<Conscript> files are very clean and
  4763. to-the-point. They simply specify products of the directory and how to build
  4764. those products. The build instructions are minimal: they specify which
  4765. construction environment to use, the name of the product, and the name of
  4766. the inputs. Note also that the scripts are location-independent: if you wish
  4767. to reorganize your source tree, you are free to do so: you only have to
  4768. change the F<Construct> file (in this example), to specify the new locations
  4769. of the F<Conscript> files. The use of an export tree makes this goal easy.
  4770. Note, too, how Cons takes care of little details for you. All the F<export>
  4771. directories, for example, were made automatically. And the installed files
  4772. were really hard-linked into the respective export directories, to save
  4773. space and time. This attention to detail saves considerable work, and makes
  4774. it even easier to produce simple, maintainable scripts.
  4775. =head1 Separating source and build trees
  4776. It's often desirable to keep any derived files from the build completely
  4777. separate from the source files. This makes it much easier to keep track of
  4778. just what is a source file, and also makes it simpler to handle B<variant>
  4779. builds, especially if you want the variant builds to co-exist.
  4780. =head2 Separating build and source directories using the Link command
  4781. Cons provides a simple mechanism that handles all of these requirements. The
  4782. C<Link> command is invoked as in this example:
  4783. Link 'build' => 'src';
  4784. The specified directories are ``linked'' to the specified source
  4785. directory. Let's suppose that you setup a source directory, F<src>, with the
  4786. sub-directories F<world> and F<hello> below it, as in the previous
  4787. example. You could then substitute for the original build lines the
  4788. following:
  4789. Build qw(
  4790. build/world/Conscript
  4791. build/hello/Conscript
  4792. );
  4793. Notice that you treat the F<Conscript> file as if it existed in the build
  4794. directory. Now if you type the same command as before, you will get the
  4795. following results:
  4796. % cons export
  4797. Install build/world/world.h as export/include/world.h
  4798. cc -Iexport/include -c build/hello/hello.c -o build/hello/hello.o
  4799. cc -Iexport/include -c build/world/world.c -o build/world/world.o
  4800. ar r build/world/libworld.a build/world/world.o
  4801. ar: creating build/world/libworld.a
  4802. ranlib build/world/libworld.a
  4803. Install build/world/libworld.a as export/lib/libworld.a
  4804. cc -o build/hello/hello build/hello/hello.o -Lexport/lib -lworld
  4805. Install build/hello/hello as export/bin/hello
  4806. Again, Cons has taken care of the details for you. In particular, you will
  4807. notice that all the builds are done using source files and object files from
  4808. the build directory. For example, F<build/world/world.o> is compiled from
  4809. F<build/world/world.c>, and F<export/include/world.h> is installed from
  4810. F<build/world/world.h>. This is accomplished on most systems by the simple
  4811. expedient of ``hard'' linking the required files from each source directory
  4812. into the appropriate build directory.
  4813. The links are maintained correctly by Cons, no matter what you do to the
  4814. source directory. If you modify a source file, your editor may do this ``in
  4815. place'' or it may rename it first and create a new file. In the latter case,
  4816. any hard link will be lost. Cons will detect this condition the next time
  4817. the source file is needed, and will relink it appropriately.
  4818. You'll also notice, by the way, that B<no> changes were required to the
  4819. underlying F<Conscript> files. And we can go further, as we shall see in the
  4820. next section.
  4821. =head2 Explicit references to the source directory
  4822. When using the C<Link> command on some operating systems or with some
  4823. tool chains, it's sometimes useful to have a command actually use
  4824. the path name to the source directory, not the build directory. For
  4825. example, on systems that must copy, not "hard link," the F<src/> and
  4826. F<build/> copies of C<Linked> files, using the F<src/> path of a file
  4827. name might make an editor aware that a syntax error must be fixed in the
  4828. source directory, not the build directory.
  4829. You can tell Cons that you want to use the "source path" for a file by
  4830. preceding the file name with a ``!'' (exclamation point). For example,
  4831. if we add a ``!'' to the beginning of a source file:
  4832. Program $env "foo", "!foo.c"; # Notice initial ! on foo.c
  4833. Cons will compile the target as follows:
  4834. cc -c src/foo.c -o build/foo.o
  4835. cc -o build/foo build/foo.o
  4836. Notice that Cons has compiled the program from the the F<src/foo.c>
  4837. source file. Without the initial ``!'', Cons would have compiled the
  4838. program using the F<build/foo.c> path name.
  4839. =head1 Variant builds
  4840. =head2 Hello, World! for baNaNa and peAcH OS's
  4841. Variant builds require just another simple extension. Let's take as an
  4842. example a requirement to allow builds for both the baNaNa and peAcH
  4843. operating systems. In this case, we are using a distributed file system,
  4844. such as NFS to access the particular system, and only one or the other of
  4845. the systems has to be compiled for any given invocation of C<cons>. Here's
  4846. one way we could set up the F<Construct> file for our B<Hello, World!>
  4847. application:
  4848. # Construct file for Hello, World!
  4849. die qq(OS must be specified) unless $OS = $ARG{OS};
  4850. die qq(OS must be "peach" or "banana")
  4851. if $OS ne "peach" && $OS ne "banana";
  4852. # Where to put all our shared products.
  4853. $EXPORT = "#export/$OS";
  4854. Export qw( CONS INCLUDE LIB BIN );
  4855. # Standard directories for sharing products.
  4856. $INCLUDE = "$EXPORT/include";
  4857. $LIB = "$EXPORT/lib";
  4858. $BIN = "$EXPORT/bin";
  4859. # A standard construction environment.
  4860. $CONS = new cons (
  4861. CPPPATH => $INCLUDE, # Include path for C Compilations
  4862. LIBPATH => $LIB, # Library path for linking programs
  4863. LIBS => '-lworld', # List of standard libraries
  4864. );
  4865. # $BUILD is where we will derive everything.
  4866. $BUILD = "#build/$OS";
  4867. # Tell cons where the source files for $BUILD are.
  4868. Link $BUILD => 'src';
  4869. Build (
  4870. "$BUILD/hello/Conscript",
  4871. "$BUILD/world/Conscript",
  4872. );
  4873. Now if we login to a peAcH system, we can build our B<Hello, World!>
  4874. application for that platform:
  4875. % cons export OS=peach
  4876. Install build/peach/world/world.h as export/peach/include/world.h
  4877. cc -Iexport/peach/include -c build/peach/hello/hello.c -o build/peach/hello/hello.o
  4878. cc -Iexport/peach/include -c build/peach/world/world.c -o build/peach/world/world.o
  4879. ar r build/peach/world/libworld.a build/peach/world/world.o
  4880. ar: creating build/peach/world/libworld.a
  4881. ranlib build/peach/world/libworld.a
  4882. Install build/peach/world/libworld.a as export/peach/lib/libworld.a
  4883. cc -o build/peach/hello/hello build/peach/hello/hello.o -Lexport/peach/lib -lworld
  4884. Install build/peach/hello/hello as export/peach/bin/hello
  4885. =head2 Variations on a theme
  4886. Other variations of this model are possible. For example, you might decide
  4887. that you want to separate out your include files into platform dependent and
  4888. platform independent files. In this case, you'd have to define an
  4889. alternative to C<$INCLUDE> for platform-dependent files. Most F<Conscript>
  4890. files, generating purely platform-independent include files, would not have
  4891. to change.
  4892. You might also want to be able to compile your whole system with debugging
  4893. or profiling, for example, enabled. You could do this with appropriate
  4894. command line options, such as C<DEBUG=on>. This would then be translated
  4895. into the appropriate platform-specific requirements to enable debugging
  4896. (this might include turning off optimization, for example). You could
  4897. optionally vary the name space for these different types of systems, but, as
  4898. we'll see in the next section, it's not B<essential> to do this, since Cons
  4899. is pretty smart about rebuilding things when you change options.
  4900. =head1 Signatures
  4901. Cons uses file B<signatures> to decide if a derived file is out-of-date
  4902. and needs rebuilding. In essence, if the contents of a file change,
  4903. or the manner in which the file is built changes, the file's signature
  4904. changes as well. This allows Cons to decide with certainty when a file
  4905. needs rebuilding, because Cons can detect, quickly and reliably, whether
  4906. any of its dependency files have been changed.
  4907. =head2 MD5 content and build signatures
  4908. Cons uses the B<MD5> (B<Message Digest 5>) algorithm to compute file
  4909. signatures. The MD5 algorithm computes a strong cryptographic checksum
  4910. for any given input string. Cons can, based on configuration, use two
  4911. different MD5 signatures for a given file:
  4912. The B<content signature> of a file is an MD5 checksum of the file's
  4913. contents. Consequently, when the contents of a file change, its content
  4914. signature changes as well.
  4915. The B<build signature> of a file is a combined MD5 checksum of:
  4916. =over 4
  4917. the signatures of all the input files used to build the file
  4918. the signatures of all dependency files discovered by source scanners
  4919. (for example, C<.h> files)
  4920. the signatures of all dependency files specified explicitly via the
  4921. C<Depends> method)
  4922. the command-line string used to build the file
  4923. =back
  4924. The build signature is, in effect, a digest of all the dependency
  4925. information for the specified file. Consequently, a file's build
  4926. signature changes whenever any part of its dependency information
  4927. changes: a new file is added, the contents of a file on which it depends
  4928. change, there's a change to the command line used to build the file (or
  4929. any of its dependency files), etc.
  4930. For example, in the previous section, the build signature of the
  4931. F<world.o> file will include:
  4932. =over 4
  4933. the signature of the F<world.c> file
  4934. the signatures of any header files that Cons detects are included,
  4935. directly or indirectly, by F<world.c>
  4936. the text of the actual command line was used to generate F<world.o>
  4937. =back
  4938. Similarly, the build signature of the F<libworld.a> file will include
  4939. all the signatures of its constituents (and hence, transitively, the
  4940. signatures of B<their> constituents), as well as the command line that
  4941. created the file.
  4942. Note that there is no need for a derived file to depend upon any
  4943. particular F<Construct> or F<Conscript> file. If changes to these files
  4944. affect a file, then this will be automatically reflected in its build
  4945. signature, since relevant parts of the command line are included in the
  4946. signature. Unrelated F<Construct> or F<Conscript> changes will have no
  4947. effect.
  4948. =head2 Storing signatures in .consign files
  4949. Before Cons exits, it stores the calculated signatures for all of the
  4950. files it built or examined in F<.consign> files, one per directory.
  4951. Cons uses this stored information on later invocations to decide if
  4952. derived files need to be rebuilt.
  4953. After the previous example was compiled, the F<.consign> file in the
  4954. F<build/peach/world> directory looked like this:
  4955. world.h:985533370 - d181712f2fdc07c1f05d97b16bfad904
  4956. world.o:985533372 2a0f71e0766927c0532977b0d2158981
  4957. world.c:985533370 - c712f77189307907f4189b5a7ab62ff3
  4958. libworld.a:985533374 69e568fc5241d7d25be86d581e1fb6aa
  4959. After the file name and colon, the first number is a timestamp of the
  4960. file's modification time (on UNIX systems, this is typically the number
  4961. of seconds since January 1st, 1970). The second value is the build
  4962. signature of the file (or ``-'' in the case of files with no build
  4963. signature--that is, source files). The third value, if any, is the
  4964. content signature of the file.
  4965. =head2 Using build signatures to decide when to rebuild files
  4966. When Cons is deciding whether to build or rebuild a derived file, it
  4967. first computes the file's current build signature. If the file doesn't
  4968. exist, it must obviously be built.
  4969. If, however, the file already exists, Cons next compares the
  4970. modification timestamp of the file against the timestamp value in
  4971. the F<.consign> file. If the timestamps match, Cons compares the
  4972. newly-computed build signature against the build signature in the
  4973. F<.consign> file. If the timestamps do not match or the build
  4974. signatures do not match, the derived file is rebuilt.
  4975. After the file is built or rebuilt, Cons arranges to store the
  4976. newly-computed build signature in the F<.consign> file when it exits.
  4977. =head2 Signature example
  4978. The use of these signatures is an extremely simple, efficient, and
  4979. effective method of improving--dramatically--the reproducibility of a
  4980. system.
  4981. We'll demonstrate this with a simple example:
  4982. # Simple "Hello, World!" Construct file
  4983. $CFLAGS = '-g' if $ARG{DEBUG} eq 'on';
  4984. $CONS = new cons(CFLAGS => $CFLAGS);
  4985. Program $CONS 'hello', 'hello.c';
  4986. Notice how Cons recompiles at the appropriate times:
  4987. % cons hello
  4988. cc -c hello.c -o hello.o
  4989. cc -o hello hello.o
  4990. % cons hello
  4991. cons: "hello" is up-to-date.
  4992. % cons DEBUG=on hello
  4993. cc -g -c hello.c -o hello.o
  4994. cc -o hello hello.o
  4995. % cons DEBUG=on hello
  4996. cons: "hello" is up-to-date.
  4997. % cons hello
  4998. cc -c hello.c -o hello.o
  4999. cc -o hello hello.o
  5000. =head2 Source-file signature configuration
  5001. Cons provides a C<SourceSignature> method that allows you to configure
  5002. how the signature should be calculated for any source file when its
  5003. signature is being used to decide if a dependent file is up-to-date.
  5004. The arguments to the C<SourceSignature> method consist of one or more
  5005. pairs of strings:
  5006. SourceSignature 'auto/*.c' => 'content',
  5007. '*' => 'stored-content';
  5008. The first string in each pair is a pattern to match against derived file
  5009. path names. The pattern is a file-globbing pattern, not a Perl regular
  5010. expression; the pattern <*.l> will match all Lex source files. The C<*>
  5011. wildcard will match across directory separators; the pattern C<foo/*.c>
  5012. would match all C source files in any subdirectory underneath the C<foo>
  5013. subdirectory.
  5014. The second string in each pair contains one of the following keywords to
  5015. specify how signatures should be calculated for source files that match
  5016. the pattern. The available keywords are:
  5017. =over 4
  5018. =item content
  5019. Use the content signature of the source file when calculating signatures
  5020. of files that depend on it. This guarantees correct calculation of the
  5021. file's signature for all builds, by telling Cons to read the contents of
  5022. a source file to calculate its content signature each time it is run.
  5023. =item stored-content
  5024. Use the source file's content signature as stored in the F<.consign>
  5025. file, provided the file's timestamp matches the cached timestamp value
  5026. in the F<.consign> file. This optimizes performance, with the slight
  5027. risk of an incorrect build if a source file's contents have been changed
  5028. so quickly after its previous update that the timestamp still matches
  5029. the stored timestamp in the F<.consign> file even though the contents
  5030. have changed.
  5031. =back
  5032. The Cons default behavior of always calculating a source file's
  5033. signature from the file's contents is equivalent to specifying:
  5034. SourceSignature '*' => 'content';
  5035. The C<*> will match all source files. The C<content> keyword
  5036. specifies that Cons will read the contents of a source file to calculate
  5037. its signature each time it is run.
  5038. A useful global performance optimization is:
  5039. SourceSignature '*' => 'stored-content';
  5040. This specifies that Cons will use pre-computed content signatures
  5041. from F<.consign> files, when available, rather than re-calculating a
  5042. signature from the the source file's contents each time Cons is run. In
  5043. practice, this is safe for most build situations, and only a problem
  5044. when source files are changed automatically (by scripts, for example).
  5045. The Cons default, however, errs on the side of guaranteeing a correct
  5046. build in all situations.
  5047. Cons tries to match source file path names against the patterns in the
  5048. order they are specified in the C<SourceSignature> arguments:
  5049. SourceSignature '/usr/repository/objects/*' => 'stored-content',
  5050. '/usr/repository/*' => 'content',
  5051. '*.y' => 'content',
  5052. '*' => 'stored-content';
  5053. In this example, all source files under the F</usr/repository/objects>
  5054. directory will use F<.consign> file content signatures, source files
  5055. anywhere else underneath F</usr/repository> will not use F<.consign>
  5056. signature values, all Yacc source files (C<*.y>) anywhere else will not
  5057. use F<.consign> signature values, and any other source file will use
  5058. F<.consign> signature values.
  5059. =head2 Derived-file signature configuration
  5060. Cons provides a C<SIGNATURE> construction variable that allows you to
  5061. configure how signatures are calculated for any derived file when its
  5062. signature is being used to decide if a dependent file is up-to-date.
  5063. The value of the C<SIGNATURE> construction variable is a Perl array
  5064. reference that holds one or more pairs of strings, like the arguments to
  5065. the C<SourceSignature> method.
  5066. The first string in each pair is a pattern to match against derived file
  5067. path names. The pattern is a file-globbing pattern, not a Perl regular
  5068. expression; the pattern `*.obj' will match all (Win32) object files.
  5069. The C<*> wildcard will match across directory separators; the pattern
  5070. `foo/*.a' would match all (UNIX) library archives in any subdirectory
  5071. underneath the foo subdirectory.
  5072. The second string in each pair contains one of the following keywords
  5073. to specify how signatures should be calculated for derived files that
  5074. match the pattern. The available keywords are the same as for the
  5075. C<SourceSignature> method, with an additional keyword:
  5076. =over 4
  5077. =item build
  5078. Use the build signature of the derived file when calculating signatures
  5079. of files that depend on it. This guarantees correct builds by forcing
  5080. Cons to rebuild any and all files that depend on the derived file.
  5081. =item content
  5082. Use the content signature of the derived file when calculating signatures
  5083. of files that depend on it. This guarantees correct calculation of the
  5084. file's signature for all builds, by telling Cons to read the contents of
  5085. a derived file to calculate its content signature each time it is run.
  5086. =item stored-content
  5087. Use the derived file's content signature as stored in the F<.consign>
  5088. file, provided the file's timestamp matches the cached timestamp value
  5089. in the F<.consign> file. This optimizes performance, with the slight
  5090. risk of an incorrect build if a derived file's contents have been
  5091. changed so quickly after a Cons build that the file's timestamp still
  5092. matches the stored timestamp in the F<.consign> file.
  5093. =back
  5094. The Cons default behavior (as previously described) for using
  5095. derived-file signatures is equivalent to:
  5096. $env = new cons(SIGNATURE => ['*' => 'build']);
  5097. The C<*> will match all derived files. The C<build> keyword specifies
  5098. that all derived files' build signatures will be used when calculating
  5099. whether a dependent file is up-to-date.
  5100. A useful alternative default C<SIGNATURE> configuration for many sites:
  5101. $env = new cons(SIGNATURE => ['*' => 'content']);
  5102. In this configuration, derived files have their signatures calculated
  5103. from the file contents. This adds slightly to Cons' workload, but has
  5104. the useful effect of "stopping" further rebuilds if a derived file is
  5105. rebuilt to exactly the same file contents as before, which usually
  5106. outweighs the additional computation Cons must perform.
  5107. For example, changing a comment in a C file and recompiling should
  5108. generate the exact same object file (assuming the compiler doesn't
  5109. insert a timestamp in the object file's header). In that case,
  5110. specifying C<content> or C<stored-content> for the signature calculation
  5111. will cause Cons to recognize that the object file did not actually
  5112. change as a result of being rebuilt, and libraries or programs that
  5113. include the object file will not be rebuilt. When C<build> is
  5114. specified, however, Cons will only "know" that the object file was
  5115. rebuilt, and proceed to rebuild any additional files that include the
  5116. object file.
  5117. Note that Cons tries to match derived file path names against the
  5118. patterns in the order they are specified in the C<SIGNATURE> array
  5119. reference:
  5120. $env = new cons(SIGNATURE => ['foo/*.o' => 'build',
  5121. '*.o' => 'content',
  5122. '*.a' => 'stored-content',
  5123. '*' => 'content']);
  5124. In this example, all object files underneath the F<foo> subdirectory
  5125. will use build signatures, all other object files (including object
  5126. files underneath other subdirectories!) will use F<.consign> file
  5127. content signatures, libraries will use F<.consign> file build
  5128. signatures, and all other derived files will use content signatures.
  5129. =head2 Debugging signature calculation
  5130. Cons provides a C<-S> option that can be used to specify what internal
  5131. Perl package Cons should use to calculate signatures. The default Cons
  5132. behavior is equivalent to specifying C<-S md5> on the command line.
  5133. The only other package (currently) available is an C<md5::debug>
  5134. package that prints out detailed information about the MD5 signature
  5135. calculations performed by Cons:
  5136. % cons -S md5::debug hello
  5137. sig::md5::srcsig(hello.c)
  5138. => |52d891204c62fe93ecb95281e1571938|
  5139. sig::md5::collect(52d891204c62fe93ecb95281e1571938)
  5140. => |fb0660af4002c40461a2f01fbb5ffd03|
  5141. sig::md5::collect(52d891204c62fe93ecb95281e1571938,
  5142. fb0660af4002c40461a2f01fbb5ffd03,
  5143. cc -c %< -o %>)
  5144. => |f7128da6c3fe3c377dc22ade70647b39|
  5145. sig::md5::current(||
  5146. eq |f7128da6c3fe3c377dc22ade70647b39|)
  5147. cc -c hello.c -o hello.o
  5148. sig::md5::collect()
  5149. => |d41d8cd98f00b204e9800998ecf8427e|
  5150. sig::md5::collect(f7128da6c3fe3c377dc22ade70647b39,
  5151. d41d8cd98f00b204e9800998ecf8427e,
  5152. cc -o %> %< )
  5153. => |a0bdce7fd09e0350e7efbbdb043a00b0|
  5154. sig::md5::current(||
  5155. eq |a0bdce7fd09e0350e7efbbdb043a00b0|)
  5156. cc -o hello, hello.o
  5157. =head1 Code Repositories
  5158. Many software development organizations will have one or more central
  5159. repository directory trees containing the current source code for one or
  5160. more projects, as well as the derived object files, libraries, and
  5161. executables. In order to reduce unnecessary recompilation, it is useful to
  5162. use files from the repository to build development software--assuming, of
  5163. course, that no newer dependency file exists in the local build tree.
  5164. =head2 Repository
  5165. Cons provides a mechanism to specify a list of code repositories that will
  5166. be searched, in-order, for source files and derived files not found in the
  5167. local build directory tree.
  5168. The following lines in a F<Construct> file will instruct Cons to look first
  5169. under the F</usr/experiment/repository> directory and then under the
  5170. F</usr/product/repository> directory:
  5171. Repository qw (
  5172. /usr/experiment/repository
  5173. /usr/product/repository
  5174. );
  5175. The repository directories specified may contain source files, derived files
  5176. (objects, libraries and executables), or both. If there is no local file
  5177. (source or derived) under the directory in which Cons is executed, then the
  5178. first copy of a same-named file found under a repository directory will be
  5179. used to build any local derived files.
  5180. Cons maintains one global list of repositories directories. Cons will
  5181. eliminate the current directory, and any non-existent directories, from the
  5182. list.
  5183. =head2 Finding the Construct file in a Repository
  5184. Cons will also search for F<Construct> and F<Conscript> files in the
  5185. repository tree or trees. This leads to a chicken-and-egg situation,
  5186. though: how do you look in a repository tree for a F<Construct> file if the
  5187. F<Construct> file tells you where the repository is? To get around this,
  5188. repositories may be specified via C<-R> options on the command line:
  5189. % cons -R /usr/experiment/repository -R /usr/product/repository .
  5190. Any repository directories specified in the F<Construct> or F<Conscript>
  5191. files will be appended to the repository directories specified by
  5192. command-line C<-R> options.
  5193. =head2 Repository source files
  5194. If the source code (include the F<Conscript> file) for the library version
  5195. of the I<Hello, World!> C application is in a repository (with no derived
  5196. files), Cons will use the repository source files to create the local object
  5197. files and executable file:
  5198. % cons -R /usr/src_only/repository hello
  5199. gcc -c /usr/src_only/repository/hello.c -o hello.o
  5200. gcc -c /usr/src_only/repository/world.c -o world.o
  5201. ar r libworld.a world.o
  5202. ar: creating libworld.a
  5203. ranlib libworld.a
  5204. gcc -o hello hello.o libworld.a
  5205. Creating a local source file will cause Cons to rebuild the appropriate
  5206. derived file or files:
  5207. % pico world.c
  5208. [EDIT]
  5209. % cons -R /usr/src_only/repository hello
  5210. gcc -c world.c -o world.o
  5211. ar r libworld.a world.o
  5212. ar: creating libworld.a
  5213. ranlib libworld.a
  5214. gcc -o hello hello.o libworld.a
  5215. And removing the local source file will cause Cons to revert back to
  5216. building the derived files from the repository source:
  5217. % rm world.c
  5218. % cons -R /usr/src_only/repository hello
  5219. gcc -c /usr/src_only/repository/world.c -o world.o
  5220. ar r libworld.a world.o
  5221. ar: creating libworld.a
  5222. ranlib libworld.a
  5223. gcc -o hello hello.o libworld.a
  5224. =head2 Repository derived files
  5225. If a repository tree contains derived files (usually object files,
  5226. libraries, or executables), Cons will perform its normal signature
  5227. calculation to decide whether the repository file is up-to-date or a derived
  5228. file must be built locally. This means that, in order to ensure correct
  5229. signature calculation, a repository tree must also contain the F<.consign>
  5230. files that were created by Cons when generating the derived files.
  5231. This would usually be accomplished by building the software in the
  5232. repository (or, alternatively, in a build directory, and then copying the
  5233. result to the repository):
  5234. % cd /usr/all/repository
  5235. % cons hello
  5236. gcc -c hello.c -o hello.o
  5237. gcc -c world.c -o world.o
  5238. ar r libworld.a world.o
  5239. ar: creating libworld.a
  5240. ranlib libworld.a
  5241. gcc -o hello hello.o libworld.a
  5242. (This is safe even if the F<Construct> file lists the F</usr/all/repository>
  5243. directory in a C<Repository> command because Cons will remove the current
  5244. directory from the repository list.)
  5245. Now if we want to build a copy of the application with our own F<hello.c>
  5246. file, we only need to create the one necessary source file, and use the
  5247. C<-R> option to have Cons use other files from the repository:
  5248. % mkdir $HOME/build1
  5249. % cd $HOME/build1
  5250. % ed hello.c
  5251. [EDIT]
  5252. % cons -R /usr/all/repository hello
  5253. gcc -c hello.c -o hello.o
  5254. gcc -o hello hello.o /usr/all/repository/libworld.a
  5255. Notice that Cons has not bothered to recreate a local F<libworld.a> library
  5256. (or recompile the F<world.o> module), but instead uses the already-compiled
  5257. version from the repository.
  5258. Because the MD5 signatures that Cons puts in the F<.consign> file contain
  5259. timestamps for the derived files, the signature timestamps must match the
  5260. file timestamps for a signature to be considered valid.
  5261. Some software systems may alter the timestamps on repository files (by
  5262. copying them, e.g.), in which case Cons will, by default, assume the
  5263. repository signatures are invalid and rebuild files unnecessarily. This
  5264. behavior may be altered by specifying:
  5265. Repository_Sig_Times_OK 0;
  5266. This tells Cons to ignore timestamps when deciding whether a signature is
  5267. valid. (Note that avoiding this sanity check means there must be proper
  5268. control over the repository tree to ensure that the derived files cannot be
  5269. modified without updating the F<.consign> signature.)
  5270. =head2 Local copies of files
  5271. If the repository tree contains the complete results of a build, and we try
  5272. to build from the repository without any files in our local tree, something
  5273. moderately surprising happens:
  5274. % mkdir $HOME/build2
  5275. % cd $HOME/build2
  5276. % cons -R /usr/all/repository hello
  5277. cons: "hello" is up-to-date.
  5278. Why does Cons say that the F<hello> program is up-to-date when there is no
  5279. F<hello> program in the local build directory? Because the repository (not
  5280. the local directory) contains the up-to-date F<hello> program, and Cons
  5281. correctly determines that nothing needs to be done to rebuild this
  5282. up-to-date copy of the file.
  5283. There are, however, many times in which it is appropriate to ensure that a
  5284. local copy of a file always exists. A packaging or testing script, for
  5285. example, may assume that certain generated files exist locally. Instead of
  5286. making these subsidiary scripts aware of the repository directory, the
  5287. C<Local> command may be added to a F<Construct> or F<Conscript> file to
  5288. specify that a certain file or files must appear in the local build
  5289. directory:
  5290. Local qw(
  5291. hello
  5292. );
  5293. Then, if we re-run the same command, Cons will make a local copy of the
  5294. program from the repository copy (telling you that it is doing so):
  5295. % cons -R /usr/all/repository hello
  5296. Local copy of hello from /usr/all/repository/hello
  5297. cons: "hello" is up-to-date.
  5298. Notice that, because the act of making the local copy is not considered a
  5299. "build" of the F<hello> file, Cons still reports that it is up-to-date.
  5300. Creating local copies is most useful for files that are being installed into
  5301. an intermediate directory (for sharing with other directories) via the
  5302. C<Install> command. Accompanying the C<Install> command for a file with a
  5303. companion C<Local> command is so common that Cons provides a
  5304. C<Install_Local> command as a convenient way to do both:
  5305. Install_Local $env, '#export', 'hello';
  5306. is exactly equivalent to:
  5307. Install $env '#export', 'hello';
  5308. Local '#export/hello';
  5309. Both the C<Local> and C<Install_Local> commands update the local F<.consign>
  5310. file with the appropriate file signatures, so that future builds are
  5311. performed correctly.
  5312. =head2 Repository dependency analysis
  5313. Due to its built-in scanning, Cons will search the specified repository
  5314. trees for included F<.h> files. Unless the compiler also knows about the
  5315. repository trees, though, it will be unable to find F<.h> files that only
  5316. exist in a repository. If, for example, the F<hello.c> file includes the
  5317. F<hello.h> file in its current directory:
  5318. % cons -R /usr/all/repository hello
  5319. gcc -c /usr/all/repository/hello.c -o hello.o
  5320. /usr/all/repository/hello.c:1: hello.h: No such file or directory
  5321. Solving this problem forces some requirements onto the way construction
  5322. environments are defined and onto the way the C C<#include> preprocessor
  5323. directive is used to include files.
  5324. In order to inform the compiler about the repository trees, Cons will add
  5325. appropriate C<-I> flags to the compilation commands. This means that the
  5326. C<CPPPATH> variable in the construction environment must explicitly specify
  5327. all subdirectories which are to be searched for included files, including the
  5328. current directory. Consequently, we can fix the above example by changing
  5329. the environment creation in the F<Construct> file as follows:
  5330. $env = new cons(
  5331. CC => 'gcc',
  5332. CPPPATH => '.',
  5333. LIBS => 'libworld.a',
  5334. );
  5335. Due to the definition of the C<CPPPATH> variable, this yields, when we
  5336. re-execute the command:
  5337. % cons -R /usr/all/repository hello
  5338. gcc -c -I. -I/usr/all/repository /usr/all/repository/hello.c -o hello.o
  5339. gcc -o hello hello.o /usr/all/repository/libworld.a
  5340. The order of the C<-I> flags replicates, for the C preprocessor, the same
  5341. repository-directory search path that Cons uses for its own dependency
  5342. analysis. If there are multiple repositories and multiple C<CPPPATH>
  5343. directories, Cons will append the repository directories to the beginning of
  5344. each C<CPPPATH> directory, rapidly multiplying the number of C<-I> flags.
  5345. As an extreme example, a F<Construct> file containing:
  5346. Repository qw(
  5347. /u1
  5348. /u2
  5349. );
  5350. $env = new cons(
  5351. CPPPATH => 'a:b:c',
  5352. );
  5353. Would yield a compilation command of:
  5354. cc -Ia -I/u1/a -I/u2/a -Ib -I/u1/b -I/u2/b -Ic -I/u1/c -I/u2/c -c hello.c -o hello.o
  5355. In order to shorten the command lines as much as possible, Cons will
  5356. remove C<-I> flags for any directories, locally or in the repositories,
  5357. which do not actually exist. (Note that the C<-I> flags are not included
  5358. in the MD5 signature calculation for the target file, so the target will
  5359. not be recompiled if the compilation command changes due to a directory
  5360. coming into existence.)
  5361. Because Cons relies on the compiler's C<-I> flags to communicate the
  5362. order in which repository directories must be searched, Cons' handling
  5363. of repository directories is fundamentally incompatible with using
  5364. double-quotes on the C<#include> directives in any C source code that
  5365. you plan to modify:
  5366. #include "file.h" /* DON'T USE DOUBLE-QUOTES LIKE THIS */
  5367. This is because most C preprocessors, when faced with such a directive, will
  5368. always first search the directory containing the source file. This
  5369. undermines the elaborate C<-I> options that Cons constructs to make the
  5370. preprocessor conform to its preferred search path.
  5371. Consequently, when using repository trees in Cons, B<always> use
  5372. angle-brackets for included files in any C source (.c or .h) files that
  5373. you plan to modify locally:
  5374. #include <file.h> /* USE ANGLE-BRACKETS INSTEAD */
  5375. Code that will not change can still safely use double quotes on #include
  5376. lines.
  5377. =head2 Repository_List
  5378. Cons provides a C<Repository_List> command to return a list of all
  5379. repository directories in their current search order. This can be used for
  5380. debugging, or to do more complex Perl stuff:
  5381. @list = Repository_List;
  5382. print join(' ', @list), "\n";
  5383. =head2 Repository interaction with other Cons features
  5384. Cons' handling of repository trees interacts correctly with other Cons
  5385. features--which is to say, it generally does what you would expect.
  5386. Most notably, repository trees interact correctly, and rather powerfully,
  5387. with the 'Link' command. A repository tree may contain one or more
  5388. subdirectories for version builds established via C<Link> to a source
  5389. subdirectory. Cons will search for derived files in the appropriate build
  5390. subdirectories under the repository tree.
  5391. =head1 Default targets
  5392. Until now, we've demonstrated invoking Cons with an explicit target
  5393. to build:
  5394. % cons hello
  5395. Normally, Cons does not build anything unless a target is specified,
  5396. but specifying '.' (the current directory) will build everything:
  5397. % cons # does not build anything
  5398. % cons . # builds everything under the top-level directory
  5399. Adding the C<Default> method to any F<Construct> or F<Conscript> file will add
  5400. the specified targets to a list of default targets. Cons will build
  5401. these defaults if there are no targets specified on the command line.
  5402. So adding the following line to the top-level F<Construct> file will mimic
  5403. Make's typical behavior of building everything by default:
  5404. Default '.';
  5405. The following would add the F<hello> and F<goodbye> commands (in the
  5406. same directory as the F<Construct> or F<Conscript> file) to the default list:
  5407. Default qw(
  5408. hello
  5409. goodbye
  5410. );
  5411. The C<Default> method may be used more than once to add targets to the
  5412. default list.
  5413. =head1 Selective builds
  5414. Cons provides two methods for reducing the size of given build. The first is
  5415. by specifying targets on the command line, and the second is a method for
  5416. pruning the build tree. We'll consider target specification first.
  5417. =head2 Selective targeting
  5418. Like make, Cons allows the specification of ``targets'' on the command
  5419. line. Cons targets may be either files or directories. When a directory is
  5420. specified, this is simply a short-hand notation for every derivable
  5421. product--that Cons knows about--in the specified directory and below. For
  5422. example:
  5423. % cons build/hello/hello.o
  5424. means build F<hello.o> and everything that F<hello.o> might need. This is
  5425. from a previous version of the B<Hello, World!> program in which F<hello.o>
  5426. depended upon F<export/include/world.h>. If that file is not up-to-date
  5427. (because someone modified F<src/world/world.h)>, then it will be rebuilt,
  5428. even though it is in a directory remote from F<build/hello>.
  5429. In this example:
  5430. % cons build
  5431. Everything in the F<build> directory is built, if necessary. Again, this may
  5432. cause more files to be built. In particular, both F<export/include/world.h>
  5433. and F<export/lib/libworld.a> are required by the F<build/hello> directory,
  5434. and so they will be built if they are out-of-date.
  5435. If we do, instead:
  5436. % cons export
  5437. then only the files that should be installed in the export directory will be
  5438. rebuilt, if necessary, and then installed there. Note that C<cons build>
  5439. might build files that C<cons export> doesn't build, and vice-versa.
  5440. =head2 No ``special'' targets
  5441. With Cons, make-style ``special'' targets are not required. The simplest
  5442. analog with Cons is to use special F<export> directories, instead. Let's
  5443. suppose, for example, that you have a whole series of unit tests that are
  5444. associated with your code. The tests live in the source directory near the
  5445. code. Normally, however, you don't want to build these tests. One solution
  5446. is to provide all the build instructions for creating the tests, and then to
  5447. install the tests into a separate part of the tree. If we install the tests
  5448. in a top-level directory called F<tests>, then:
  5449. % cons tests
  5450. will build all the tests.
  5451. % cons export
  5452. will build the production version of the system (but not the tests), and:
  5453. % cons build
  5454. should probably be avoided (since it will compile tests unnecessarily).
  5455. If you want to build just a single test, then you could explicitly name the
  5456. test (in either the F<tests> directory or the F<build> directory). You could
  5457. also aggregate the tests into a convenient hierarchy within the tests
  5458. directory. This hierarchy need not necessarily match the source hierarchy,
  5459. in much the same manner that the include hierarchy probably doesn't match
  5460. the source hierarchy (the include hierarchy is unlikely to be more than two
  5461. levels deep, for C programs).
  5462. If you want to build absolutely everything in the tree (subject to whatever
  5463. options you select), you can use:
  5464. % cons .
  5465. This is not particularly efficient, since it will redundantly walk all the
  5466. trees, including the source tree. The source tree, of course, may have
  5467. buildable objects in it--nothing stops you from doing this, even if you
  5468. normally build in a separate build tree.
  5469. =head1 Build Pruning
  5470. In conjunction with target selection, B<build pruning> can be used to reduce
  5471. the scope of the build. In the previous peAcH and baNaNa example, we have
  5472. already seen how script-driven build pruning can be used to make only half
  5473. of the potential build available for any given invocation of C<cons>. Cons
  5474. also provides, as a convenience, a command line convention that allows you
  5475. to specify which F<Conscript> files actually get ``built''--that is,
  5476. incorporated into the build tree. For example:
  5477. % cons build +world
  5478. The C<+> argument introduces a Perl regular expression. This must, of
  5479. course, be quoted at the shell level if there are any shell meta-characters
  5480. within the expression. The expression is matched against each F<Conscript>
  5481. file which has been mentioned in a C<Build> statement, and only those
  5482. scripts with matching names are actually incorporated into the build
  5483. tree. Multiple such arguments are allowed, in which case a match against any
  5484. of them is sufficient to cause a script to be included.
  5485. In the example, above, the F<hello> program will not be built, since Cons
  5486. will have no knowledge of the script F<hello/Conscript>. The F<libworld.a>
  5487. archive will be built, however, if need be.
  5488. There are a couple of uses for build pruning via the command line. Perhaps
  5489. the most useful is the ability to make local changes, and then, with
  5490. sufficient knowledge of the consequences of those changes, restrict the size
  5491. of the build tree in order to speed up the rebuild time. A second use for
  5492. build pruning is to actively prevent the recompilation of certain files that
  5493. you know will recompile due to, for example, a modified header file. You may
  5494. know that either the changes to the header file are immaterial, or that the
  5495. changes may be safely ignored for most of the tree, for testing
  5496. purposes.With Cons, the view is that it is pragmatic to admit this type of
  5497. behavior, with the understanding that on the next full build everything that
  5498. needs to be rebuilt will be. There is no equivalent to a ``make touch''
  5499. command, to mark files as permanently up-to-date. So any risk that is
  5500. incurred by build pruning is mitigated. For release quality work, obviously,
  5501. we recommend that you do not use build pruning (it's perfectly OK to use
  5502. during integration, however, for checking compilation, etc. Just be sure to
  5503. do an unconstrained build before committing the integration).
  5504. =head1 Temporary overrides
  5505. Cons provides a very simple mechanism for overriding aspects of a build. The
  5506. essence is that you write an override file containing one or more
  5507. C<Override> commands, and you specify this on the command line, when you run
  5508. C<cons>:
  5509. % cons -o over export
  5510. will build the F<export> directory, with all derived files subject to the
  5511. overrides present in the F<over> file. If you leave out the C<-o> option,
  5512. then everything necessary to remove all overrides will be rebuilt.
  5513. =head2 Overriding environment variables
  5514. The override file can contain two types of overrides. The first is incoming
  5515. environment variables. These are normally accessible by the F<Construct>
  5516. file from the C<%ENV> hash variable. These can trivially be overridden in
  5517. the override file by setting the appropriate elements of C<%ENV> (these
  5518. could also be overridden in the user's environment, of course).
  5519. =head2 The Override command
  5520. The second type of override is accomplished with the C<Override> command,
  5521. which looks like this:
  5522. Override <regexp>, <var1> => <value1>, <var2> => <value2>, ...;
  5523. The regular expression I<regexp> is matched against every derived file that
  5524. is a candidate for the build. If the derived file matches, then the
  5525. variable/value pairs are used to override the values in the construction
  5526. environment associated with the derived file.
  5527. Let's suppose that we have a construction environment like this:
  5528. $CONS = new cons(
  5529. COPT => '',
  5530. CDBG => '-g',
  5531. CFLAGS => '%COPT %CDBG',
  5532. );
  5533. Then if we have an override file F<over> containing this command:
  5534. Override '\.o$', COPT => '-O', CDBG => '';
  5535. then any C<cons> invocation with C<-o over> that creates F<.o> files via
  5536. this environment will cause them to be compiled with C<-O >and no C<-g>. The
  5537. override could, of course, be restricted to a single directory by the
  5538. appropriate selection of a regular expression.
  5539. Here's the original version of the Hello, World! program, built with this
  5540. environment. Note that Cons rebuilds the appropriate pieces when the
  5541. override is applied or removed:
  5542. % cons hello
  5543. cc -g -c hello.c -o hello.o
  5544. cc -o hello hello.o
  5545. % cons -o over hello
  5546. cc -O -c hello.c -o hello.o
  5547. cc -o hello hello.o
  5548. % cons -o over hello
  5549. cons: "hello" is up-to-date.
  5550. % cons hello
  5551. cc -g -c hello.c -o hello.o
  5552. cc -o hello hello.o
  5553. It's important that the C<Override> command only be used for temporary,
  5554. on-the-fly overrides necessary for development because the overrides are not
  5555. platform independent and because they rely too much on intimate knowledge of
  5556. the workings of the scripts. For temporary use, however, they are exactly
  5557. what you want.
  5558. Note that it is still useful to provide, say, the ability to create a fully
  5559. optimized version of a system for production use--from the F<Construct> and
  5560. F<Conscript> files. This way you can tailor the optimized system to the
  5561. platform. Where optimizer trade-offs need to be made (particular files may
  5562. not be compiled with full optimization, for example), then these can be
  5563. recorded for posterity (and reproducibility) directly in the scripts.
  5564. =head1 More on construction environments
  5565. As previously mentioned, a B<construction environment> is an object that
  5566. has a set of keyword/value pairs and a set of methods, and which is used
  5567. to tell Cons how target files should be built. This section describes
  5568. how Cons uses and expands construction environment values to control its
  5569. build behavior.
  5570. =head2 Construction variable expansion
  5571. Construction variables from a construction environment are expanded
  5572. by preceding the keyword with a C<%> (percent sign):
  5573. Construction variables:
  5574. XYZZY => 'abracadabra',
  5575. The string: "The magic word is: %XYZZY!"
  5576. expands to: "The magic word is: abracadabra!"
  5577. A construction variable name may be surrounded by C<{> and C<}> (curly
  5578. braces), which are stripped as part of the expansion. This can
  5579. sometimes be necessary to separate a variable expansion from trailing
  5580. alphanumeric characters:
  5581. Construction variables:
  5582. OPT => 'value1',
  5583. OPTION => 'value2',
  5584. The string: "%OPT %{OPT}ION %OPTION %{OPTION}"
  5585. expands to: "value1 value1ION value2 value2"
  5586. Construction variable expansion is recursive--that is, a string
  5587. containing C<%->expansions after substitution will be re-expanded until
  5588. no further substitutions can be made:
  5589. Construction variables:
  5590. STRING => 'The result is: %FOO',
  5591. FOO => '%BAR',
  5592. BAR => 'final value',
  5593. The string: "The string says: %STRING"
  5594. expands to: "The string says: The result is: final value"
  5595. If a construction variable is not defined in an environment, then the
  5596. null string is substituted:
  5597. Construction variables:
  5598. FOO => 'value1',
  5599. BAR => 'value2',
  5600. The string: "%FOO <%NO_VARIABLE> %BAR"
  5601. expands to: "value1 <> value2"
  5602. A doubled C<%%> will be replaced by a single C<%>:
  5603. The string: "Here is a percent sign: %%"
  5604. expands to: "Here is a percent sign: %"
  5605. =head2 Default construction variables
  5606. When you specify no arguments when creating a new construction
  5607. environment:
  5608. $env = new cons();
  5609. Cons creates a reference to a new, default construction
  5610. environment. This contains a number of construction variables and some
  5611. methods. At the present writing, the default construction variables on a
  5612. UNIX system are:
  5613. CC => 'cc',
  5614. CFLAGS => '',
  5615. CCCOM => '%CC %CFLAGS %_IFLAGS -c %< -o %>',
  5616. CXX => '%CC',
  5617. CXXFLAGS => '%CFLAGS',
  5618. CXXCOM => '%CXX %CXXFLAGS %_IFLAGS -c %< -o %>',
  5619. INCDIRPREFIX => '-I',
  5620. INCDIRSUFFIX => '',
  5621. LINK => '%CXX',
  5622. LINKCOM => '%LINK %LDFLAGS -o %> %< %_LDIRS %LIBS',
  5623. LINKMODULECOM => '%LD -r -o %> %<',
  5624. LIBDIRPREFIX => '-L',
  5625. LIBDIRSUFFIX => '',
  5626. AR => 'ar',
  5627. ARFLAGS => 'r',
  5628. ARCOM => ['%AR %ARFLAGS %> %<', '%RANLIB %>'],
  5629. RANLIB => 'ranlib',
  5630. AS => 'as',
  5631. ASFLAGS => '',
  5632. ASCOM => '%AS %ASFLAGS %< -o %>',
  5633. LD => 'ld',
  5634. LDFLAGS => '',
  5635. PREFLIB => 'lib',
  5636. SUFLIB => '.a',
  5637. SUFLIBS => '.so:.a',
  5638. SUFOBJ => '.o',
  5639. SIGNATURE => [ '*' => 'build' ],
  5640. ENV => { 'PATH' => '/bin:/usr/bin' },
  5641. And on a Win32 system (Windows NT), the default construction variables
  5642. are (unless the default rule style is set using the B<DefaultRules>
  5643. method):
  5644. CC => 'cl',
  5645. CFLAGS => '/nologo',
  5646. CCCOM => '%CC %CFLAGS %_IFLAGS /c %< /Fo%>',
  5647. CXXCOM => '%CXX %CXXFLAGS %_IFLAGS /c %< /Fo%>',
  5648. INCDIRPREFIX => '/I',
  5649. INCDIRSUFFIX => '',
  5650. LINK => 'link',
  5651. LINKCOM => '%LINK %LDFLAGS /out:%> %< %_LDIRS %LIBS',
  5652. LINKMODULECOM => '%LD /r /o %> %<',
  5653. LIBDIRPREFIX => '/LIBPATH:',
  5654. LIBDIRSUFFIX => '',
  5655. AR => 'lib',
  5656. ARFLAGS => '/nologo ',
  5657. ARCOM => "%AR %ARFLAGS /out:%> %<",
  5658. RANLIB => '',
  5659. LD => 'link',
  5660. LDFLAGS => '/nologo ',
  5661. PREFLIB => '',
  5662. SUFEXE => '.exe',
  5663. SUFLIB => '.lib',
  5664. SUFLIBS => '.dll:.lib',
  5665. SUFOBJ => '.obj',
  5666. SIGNATURE => [ '*' => 'build' ],
  5667. These variables are used by the various methods associated with the
  5668. environment. In particular, any method that ultimately invokes an external
  5669. command will substitute these variables into the final command, as
  5670. appropriate. For example, the C<Objects> method takes a number of source
  5671. files and arranges to derive, if necessary, the corresponding object
  5672. files:
  5673. Objects $env 'foo.c', 'bar.c';
  5674. This will arrange to produce, if necessary, F<foo.o> and F<bar.o>. The
  5675. command invoked is simply C<%CCCOM>, which expands, through substitution,
  5676. to the appropriate external command required to build each object. The
  5677. substitution rules will be discussed in detail in the next section.
  5678. The construction variables are also used for other purposes. For example,
  5679. C<CPPPATH> is used to specify a colon-separated path of include
  5680. directories. These are intended to be passed to the C preprocessor and are
  5681. also used by the C-file scanning machinery to determine the dependencies
  5682. involved in a C Compilation.
  5683. Variables beginning with underscore are created by various methods,
  5684. and should normally be considered ``internal'' variables. For example,
  5685. when a method is called which calls for the creation of an object from
  5686. a C source, the variable C<_IFLAGS> is created: this corresponds to the
  5687. C<-I> switches required by the C compiler to represent the directories
  5688. specified by C<CPPPATH>.
  5689. Note that, for any particular environment, the value of a variable is set
  5690. once, and then never reset (to change a variable, you must create a new
  5691. environment. Methods are provided for copying existing environments for this
  5692. purpose). Some internal variables, such as C<_IFLAGS> are created on demand,
  5693. but once set, they remain fixed for the life of the environment.
  5694. The C<CFLAGS>, C<LDFLAGS>, and C<ARFLAGS> variables all supply a place
  5695. for passing options to the compiler, loader, and archiver, respectively.
  5696. The C<INCDIRPREFIX> and C<INCDIRSUFFIX> variables specify option
  5697. strings to be appended to the beginning and end, respectively, of each
  5698. include directory so that the compiler knows where to find F<.h> files.
  5699. Similarly, the C<LIBDIRPREFIX> and C<LIBDIRSUFFIX> variables specify the
  5700. option string to be appended to the beginning of and end, respectively,
  5701. of each directory that the linker should search for libraries.
  5702. Another variable, C<ENV>, is used to determine the system environment during
  5703. the execution of an external command. By default, the only environment
  5704. variable that is set is C<PATH>, which is the execution path for a UNIX
  5705. command. For the utmost reproducibility, you should really arrange to set
  5706. your own execution path, in your top-level F<Construct> file (or perhaps by
  5707. importing an appropriate construction package with the Perl C<use>
  5708. command). The default variables are intended to get you off the ground.
  5709. =head2 Expanding variables in construction commands
  5710. Within a construction command, construction variables will be expanded
  5711. according to the rules described above. In addition to normal variable
  5712. expansion from the construction environment, construction commands also
  5713. expand the following pseudo-variables to insert the specific input and
  5714. output files in the command line that will be executed:
  5715. =over 10
  5716. =item %>
  5717. The target file name. In a multi-target command, this expands to the
  5718. first target mentioned.)
  5719. =item %0
  5720. Same as C<%E<gt>>.
  5721. =item %1, %2, ..., %9
  5722. These refer to the first through ninth input file, respectively.
  5723. =item %E<lt>
  5724. The full set of input file names. If any of these have been used
  5725. anywhere else in the current command line (via C<%1>, C<%2>, etc.), then
  5726. those will be deleted from the list provided by C<%E<lt>>. Consider the
  5727. following command found in a F<Conscript> file in the F<test> directory:
  5728. Command $env 'tgt', qw(foo bar baz), qq(
  5729. echo %< -i %1 > %>
  5730. echo %< -i %2 >> %>
  5731. echo %< -i %3 >> %>
  5732. );
  5733. If F<tgt> needed to be updated, then this would result in the execution of
  5734. the following commands, assuming that no remapping has been established for
  5735. the F<test> directory:
  5736. echo test/bar test/baz -i test/foo > test/tgt
  5737. echo test/foo test/baz -i test/bar >> test/tgt
  5738. echo test/foo test/bar -i test/baz >> test/tgt
  5739. =back
  5740. Any of the above pseudo-variables may be followed immediately by one of
  5741. the following suffixes to select a portion of the expanded path name:
  5742. :a the absolute path to the file name
  5743. :b the directory plus the file name stripped of any suffix
  5744. :d the directory
  5745. :f the file name
  5746. :s the file name suffix
  5747. :F the file name stripped of any suffix
  5748. :S the absolute path path to a Linked source file
  5749. Continuing with the above example, C<%E<lt>:f> would expand to C<foo bar baz>,
  5750. and C<%E<gt>:d> would expand to C<test>.
  5751. There are additional C<%> elements which affect the command line(s):
  5752. =over 10
  5753. =item %[ %]
  5754. It is possible to programmatically rewrite part of the command by
  5755. enclosing part of it between C<%[> and C<%]>. This will call the
  5756. construction variable named as the first word enclosed in the brackets
  5757. as a Perl code reference; the results of this call will be used to
  5758. replace the contents of the brackets in the command line. For example,
  5759. given an existing input file named F<tgt.in>:
  5760. @keywords = qw(foo bar baz);
  5761. $env = new cons(X_COMMA => sub { join(",", @_) });
  5762. Command $env 'tgt', 'tgt.in', qq(
  5763. echo '# Keywords: %[X_COMMA @keywords %]' > %>
  5764. cat %< >> %>
  5765. );
  5766. This will execute:
  5767. echo '# Keywords: foo,bar,baz' > tgt
  5768. cat tgt.in >> tgt
  5769. =item %( %)
  5770. Cons includes the text of the command line in the MD5 signature for a
  5771. build, so that targets get rebuilt if you change the command line (to
  5772. add or remove an option, for example). Command-line text in between
  5773. C<%(> and C<%)>, however, will be ignored for MD5 signature calculation.
  5774. Internally, Cons uses C<%(> and C<%)> around include and library
  5775. directory options (C<-I> and C<-L> on UNIX systems, C</I> and
  5776. C</LIBPATH> on Windows NT) to avoid rebuilds just because the directory
  5777. list changes. Rebuilds occur only if the changed directory list causes
  5778. any included I<files> to change, and a changed include file is detected
  5779. by the MD5 signature calculation on the actual file contents.
  5780. =back
  5781. =head2 Expanding construction variables in file names
  5782. Cons expands construction variables in the source and target file names
  5783. passed to the various construction methods according to the expansion
  5784. rules described above:
  5785. $env = new cons(
  5786. DESTDIR => 'programs',
  5787. SRCDIR => 'src',
  5788. );
  5789. Program $env '%DESTDIR/hello', '%SRCDIR/hello.c';
  5790. This allows for flexible configuration, through the construction
  5791. environment, of directory names, suffixes, etc.
  5792. =head1 Build actions
  5793. Cons supports several types of B<build actions> that can be performed
  5794. to construct one or more target files. Usually, a build action is
  5795. a construction command--that is, a command-line string that invokes
  5796. an external command. Cons can also execute Perl code embedded in a
  5797. command-line string, and even supports an experimental ability to build
  5798. a target file by executing a Perl code reference directly.
  5799. A build action is usually specified as the value of a construction
  5800. variable:
  5801. $env = new cons(
  5802. CCCOM => '%CC %CFLAGS %_IFLAGS -c %< -o %>',
  5803. LINKCOM => '[perl] &link_executable("%>", "%<")',
  5804. ARCOM => sub { my($env, $target, @sources) = @_;
  5805. # code to create an archive
  5806. }
  5807. );
  5808. A build action may be associated directly with one or more target files
  5809. via the C<Command> method; see below.
  5810. =head2 Construction commands
  5811. A construction command goes through expansion of construction variables
  5812. and C<%-> pseudo-variables, as described above, to create the actual
  5813. command line that Cons will execute to generate the target file or
  5814. files.
  5815. After substitution occurs, strings of white space are converted into
  5816. single blanks, and leading and trailing white space is eliminated. It
  5817. is therefore currently not possible to introduce variable length white
  5818. space in strings passed into a command.
  5819. If a multi-line command string is provided, the commands are executed
  5820. sequentially. If any of the commands fails, then none of the rest are
  5821. executed, and the target is not marked as updated, i.e. a new signature is
  5822. not stored for the target.
  5823. Normally, if all the commands succeed, and return a zero status (or whatever
  5824. platform-specific indication of success is required), then a new signature
  5825. is stored for the target. If a command erroneously reports success even
  5826. after a failure, then Cons will assume that the target file created by that
  5827. command is accurate and up-to-date.
  5828. The first word of each command string, after expansion, is assumed to be an
  5829. executable command looked up on the C<PATH> environment variable (which is,
  5830. in turn, specified by the C<ENV> construction variable). If this command is
  5831. found on the path, then the target will depend upon it: the command will
  5832. therefore be automatically built, as necessary. It's possible to write
  5833. multi-part commands to some shells, separated by semi-colons. Only the first
  5834. command word will be depended upon, however, so if you write your command
  5835. strings this way, you must either explicitly set up a dependency (with the
  5836. C<Depends> method), or be sure that the command you are using is a system
  5837. command which is expected to be available. If it isn't available, you will,
  5838. of course, get an error.
  5839. Cons normally prints a command before executing it. This behavior is
  5840. suppressed if the first character of the command is C<@>. Note that
  5841. you may need to separate the C<@> from the command name or escape it to
  5842. prevent C<@cmd> from looking like an array to Perl quote operators that
  5843. perform interpolation:
  5844. # The first command line is incorrect,
  5845. # because "@cp" looks like an array
  5846. # to the Perl qq// function.
  5847. # Use the second form instead.
  5848. Command $env 'foo', 'foo.in', qq(
  5849. @cp %< tempfile
  5850. @ cp tempfile %>
  5851. );
  5852. If there are shell meta characters anywhere in the expanded command line,
  5853. such as C<E<lt>>, C<E<gt>>, quotes, or semi-colon, then the command
  5854. will actually be executed by invoking a shell. This means that a command
  5855. such as:
  5856. cd foo
  5857. alone will typically fail, since there is no command C<cd> on the path. But
  5858. the command string:
  5859. cd $<:d; tar cf $>:f $<:f
  5860. when expanded will still contain the shell meta character semi-colon, and a
  5861. shell will be invoked to interpret the command. Since C<cd> is interpreted
  5862. by this sub-shell, the command will execute as expected.
  5863. =head2 Perl expressions
  5864. If any command (even one within a multi-line command) begins with
  5865. C<[perl]>, the remainder of that command line will be evaluated by the
  5866. running Perl instead of being forked by the shell. If an error occurs
  5867. in parsing the Perl code, or if the Perl expression returns 0 or undef,
  5868. the command will be considered to have failed. For example, here is a
  5869. simple command which creates a file C<foo> directly from Perl:
  5870. $env = new cons();
  5871. Command $env 'foo',
  5872. qq([perl] open(FOO,'>foo');print FOO "hi\\n"; close(FOO); 1);
  5873. Note that when the command is executed, you are in the same package as
  5874. when the F<Construct> or F<Conscript> file was read, so you can call
  5875. Perl functions you've defined in the same F<Construct> or F<Conscript>
  5876. file in which the C<Command> appears:
  5877. $env = new cons();
  5878. sub create_file {
  5879. my $file = shift;
  5880. open(FILE, ">$file");
  5881. print FILE "hi\n";
  5882. close(FILE);
  5883. return 1;
  5884. }
  5885. Command $env 'foo', "[perl] &create_file('%>')";
  5886. The Perl string will be used to generate the signature for the derived
  5887. file, so if you change the string, the file will be rebuilt. The contents
  5888. of any subroutines you call, however, are not part of the signature,
  5889. so if you modify a called subroutine such as C<create_file> above,
  5890. the target will I<not> be rebuilt. Caveat user.
  5891. =head2 Perl code references [EXPERIMENTAL]
  5892. Cons supports the ability to create a derived file by directly executing
  5893. a Perl code reference. This feature is considered EXPERIMENTAL and
  5894. subject to change in the future.
  5895. A code reference may either be a named subroutine referenced by the
  5896. usual C<\&> syntax:
  5897. sub build_output {
  5898. my($env, $target, @sources) = @_;
  5899. print "build_output building $target\n";
  5900. open(OUT, ">$target");
  5901. foreach $src (@sources) {
  5902. if (! open(IN, "<$src")) {
  5903. print STDERR "cannot open '$src': $!\n";
  5904. return undef;
  5905. }
  5906. print OUT, <IN>;
  5907. }
  5908. close(OUT);
  5909. return 1;
  5910. }
  5911. Command $env 'output', \&build_output;
  5912. or the code reference may be an anonymous subroutine:
  5913. Command $env 'output', sub {
  5914. my($env, $target, @sources) = @_;
  5915. print "building $target\n";
  5916. open(FILE, ">$target");
  5917. print FILE "hello\n";
  5918. close(FILE);
  5919. return 1;
  5920. };
  5921. To build the target file, the referenced subroutine is passed, in order:
  5922. the construction environment used to generate the target; the path
  5923. name of the target itself; and the path names of all the source files
  5924. necessary to build the target file.
  5925. The code reference is expected to generate the target file, of course,
  5926. but may manipulate the source and target files in any way it chooses.
  5927. The code reference must return a false value (C<undef> or C<0>) if
  5928. the build of the file failed. Any true value indicates a successful
  5929. build of the target.
  5930. Building target files using code references is considered EXPERIMENTAL
  5931. due to the following current limitations:
  5932. =over 4
  5933. Cons does I<not> print anything to indicate the code reference is being
  5934. called to build the file. The only way to give the user any indication
  5935. is to have the code reference explicitly print some sort of "building"
  5936. message, as in the above examples.
  5937. Cons does not generate any signatures for code references, so if the
  5938. code in the reference changes, the target will I<not> be rebuilt.
  5939. Cons has no public method to allow a code reference to extract
  5940. construction variables. This would be good to allow generalization of
  5941. code references based on the current construction environment, but would
  5942. also complicate the problem of generating meaningful signatures for code
  5943. references.
  5944. =back
  5945. Support for building targets via code references has been released in
  5946. this version to encourage experimentation and the seeking of possible
  5947. solutions to the above limitations.
  5948. =head1 Default construction methods
  5949. The list of default construction methods includes the following:
  5950. =head2 The C<new> constructor
  5951. The C<new> method is a Perl object constructor. That is, it is not invoked
  5952. via a reference to an existing construction environment B<reference>, but,
  5953. rather statically, using the name of the Perl B<package> where the
  5954. constructor is defined. The method is invoked like this:
  5955. $env = new cons(<overrides>);
  5956. The environment you get back is blessed into the package C<cons>, which
  5957. means that it will have associated with it the default methods described
  5958. below. Individual construction variables can be overridden by providing
  5959. name/value pairs in an override list. Note that to override any command
  5960. environment variable (i.e. anything under C<ENV>), you will have to override
  5961. all of them. You can get around this difficulty by using the C<copy> method
  5962. on an existing construction environment.
  5963. =head2 The C<clone> method
  5964. The C<clone> method creates a clone of an existing construction environment,
  5965. and can be called as in the following example:
  5966. $env2 = $env1->clone(<overrides>);
  5967. You can provide overrides in the usual manner to create a different
  5968. environment from the original. If you just want a new name for the same
  5969. environment (which may be helpful when exporting environments to existing
  5970. components), you can just use simple assignment.
  5971. =head2 The C<copy> method
  5972. The C<copy> method extracts the externally defined construction variables
  5973. from an environment and returns them as a list of name/value
  5974. pairs. Overrides can also be provided, in which case, the overridden values
  5975. will be returned, as appropriate. The returned list can be assigned to a
  5976. hash, as shown in the prototype, below, but it can also be manipulated in
  5977. other ways:
  5978. %env = $env1->copy(<overrides>);
  5979. The value of C<ENV>, which is itself a hash, is also copied to a new hash,
  5980. so this may be changed without fear of affecting the original
  5981. environment. So, for example, if you really want to override just the
  5982. C<PATH> variable in the default environment, you could do the following:
  5983. %cons = new cons()->copy();
  5984. $cons{ENV}{PATH} = "<your path here>";
  5985. $cons = new cons(%cons);
  5986. This will leave anything else that might be in the default execution
  5987. environment undisturbed.
  5988. =head2 The C<Install> method
  5989. The C<Install> method arranges for the specified files to be installed in
  5990. the specified directory. The installation is optimized: the file is not
  5991. copied if it can be linked. If this is not the desired behavior, you will
  5992. need to use a different method to install the file. It is called as follows:
  5993. Install $env <directory>, <names>;
  5994. Note that, while the files to be installed may be arbitrarily named,
  5995. only the last component of each name is used for the installed target
  5996. name. So, for example, if you arrange to install F<foo/bar> in F<baz>,
  5997. this will create a F<bar> file in the F<baz> directory (not F<foo/bar>).
  5998. =head2 The C<InstallAs> method
  5999. The C<InstallAs> method arranges for the specified source file(s) to be
  6000. installed as the specified target file(s). Multiple files should be
  6001. specified as a file list. The installation is optimized: the file is not
  6002. copied if it can be linked. If this is not the desired behavior, you will
  6003. need to use a different method to install the file. It is called as follows:
  6004. C<InstallAs> works in two ways:
  6005. Single file install:
  6006. InstallAs $env TgtFile, SrcFile;
  6007. Multiple file install:
  6008. InstallAs $env ['tgt1', 'tgt2'], ['src1', 'src2'];
  6009. Or, even as:
  6010. @srcs = qw(src1 src2 src3);
  6011. @tgts = qw(tgt1 tgt2 tgt3);
  6012. InstallAs $env [@tgts], [@srcs];
  6013. Both the target and the sources lists should be of the same length.
  6014. =head2 The C<Precious> method
  6015. The C<Precious> method asks cons not to delete the specified file or
  6016. list of files before building them again. It is invoked as:
  6017. Precious <files>;
  6018. This is especially useful for allowing incremental updates to libraries
  6019. or debug information files which are updated rather than rebuilt anew each
  6020. time. Cons will still delete the files when the C<-r> flag is specified.
  6021. =head2 The C<AfterBuild> method
  6022. The C<AfterBuild> method evaluates the specified perl string after
  6023. building the given file or files (or finding that they are up to date).
  6024. The eval will happen once per specified file. C<AfterBuild> is called
  6025. as follows:
  6026. AfterBuild $env 'foo.o', qq(print "foo.o is up to date!\n");
  6027. The perl string is evaluated in the C<script> package, and has access
  6028. to all variables and subroutines defined in the F<Conscript> file in
  6029. which the C<AfterBuild> method is called.
  6030. =head2 The C<Command> method
  6031. The C<Command> method is a catchall method which can be used to arrange for
  6032. any build action to be executed to update the target. For this command, a
  6033. target file and list of inputs is provided. In addition, a build action
  6034. is specified as the last argument. The build action is typically a
  6035. command line or lines, but may also contain Perl code to be executed;
  6036. see the section above on build actions for details.
  6037. The C<Command> method is called as follows:
  6038. Command $env <target>, <inputs>, <build action>;
  6039. The target is made dependent upon the list of input files specified, and the
  6040. inputs must be built successfully or Cons will not attempt to build the
  6041. target.
  6042. To specify a command with multiple targets, you can specify a reference to a
  6043. list of targets. In Perl, a list reference can be created by enclosing a
  6044. list in square brackets. Hence the following command:
  6045. Command $env ['foo.h', 'foo.c'], 'foo.template', q(
  6046. gen %1
  6047. );
  6048. could be used in a case where the command C<gen> creates two files, both
  6049. F<foo.h> and F<foo.c>.
  6050. =head2 The C<Objects> method
  6051. The C<Objects> method arranges to create the object files that correspond to
  6052. the specified source files. It is invoked as shown below:
  6053. @files = Objects $env <source or object files>;
  6054. Under Unix, source files ending in F<.s> and F<.c> are currently
  6055. supported, and will be compiled into a name of the same file ending
  6056. in F<.o>. By default, all files are created by invoking the external
  6057. command which results from expanding the C<CCCOM> construction variable,
  6058. with C<%E<lt>> and C<%E<gt>> set to the source and object files,
  6059. respectively. (See the section above on construction variable expansion
  6060. for details). The variable C<CPPPATH> is also used when scanning source
  6061. files for dependencies. This is a colon separated list of pathnames, and
  6062. is also used to create the construction variable C<_IFLAGS,> which will
  6063. contain the appropriate list of -C<I> options for the compilation. Any
  6064. relative pathnames in C<CPPPATH> is interpreted relative to the
  6065. directory in which the associated construction environment was created
  6066. (absolute and top-relative names may also be used). This variable is
  6067. used by C<CCCOM>. The behavior of this command can be modified by
  6068. changing any of the variables which are interpolated into C<CCCOM>, such
  6069. as C<CC>, C<CFLAGS>, and, indirectly, C<CPPPATH>. It's also possible
  6070. to replace the value of C<CCCOM>, itself. As a convenience, this file
  6071. returns the list of object filenames.
  6072. =head2 The C<Program> method
  6073. The C<Program> method arranges to link the specified program with the
  6074. specified object files. It is invoked in the following manner:
  6075. Program $env <program name>, <source or object files>;
  6076. The program name will have the value of the C<SUFEXE> construction
  6077. variable appended (by default, C<.exe> on Win32 systems, nothing on Unix
  6078. systems) if the suffix is not already present.
  6079. Source files may be specified in place of objects files--the C<Objects>
  6080. method will be invoked to arrange the conversion of all the files into
  6081. object files, and hence all the observations about the C<Objects> method,
  6082. above, apply to this method also.
  6083. The actual linking of the program will be handled by an external command
  6084. which results from expanding the C<LINKCOM> construction variable, with
  6085. C<%E<lt>> set to the object files to be linked (in the order presented),
  6086. and C<%E<gt>> set to the target. (See the section above on construction
  6087. variable expansion for details.) The user may set additional variables
  6088. in the construction environment, including C<LINK>, to define which
  6089. program to use for linking, C<LIBPATH>, a colon-separated list of
  6090. library search paths, for use with library specifications of the form
  6091. I<-llib>, and C<LIBS>, specifying the list of libraries to link against
  6092. (in either I<-llib> form or just as pathnames. Relative pathnames in
  6093. both C<LIBPATH> and C<LIBS> are interpreted relative to the directory
  6094. in which the associated construction environment is created (absolute
  6095. and top-relative names may also be used). Cons automatically sets up
  6096. dependencies on any libraries mentioned in C<LIBS>: those libraries will
  6097. be built before the command is linked.
  6098. =head2 The C<Library> method
  6099. The C<Library> method arranges to create the specified library from the
  6100. specified object files. It is invoked as follows:
  6101. Library $env <library name>, <source or object files>;
  6102. The library name will have the value of the C<SUFLIB> construction
  6103. variable appended (by default, C<.lib> on Win32 systems, C<.a> on Unix
  6104. systems) if the suffix is not already present.
  6105. Source files may be specified in place of objects files--the C<Objects>
  6106. method will be invoked to arrange the conversion of all the files into
  6107. object files, and hence all the observations about the C<Objects> method,
  6108. above, apply to this method also.
  6109. The actual creation of the library will be handled by an external
  6110. command which results from expanding the C<ARCOM> construction variable,
  6111. with C<%E<lt>> set to the library members (in the order presented),
  6112. and C<%E<gt>> to the library to be created. (See the section above
  6113. on construction variable expansion for details.) The user may set
  6114. variables in the construction environment which will affect the
  6115. operation of the command. These include C<AR>, the archive program
  6116. to use, C<ARFLAGS>, which can be used to modify the flags given to
  6117. the program specified by C<AR>, and C<RANLIB>, the name of a archive
  6118. index generation program, if needed (if the particular need does not
  6119. require the latter functionality, then C<ARCOM> must be redefined to not
  6120. reference C<RANLIB>).
  6121. The C<Library> method allows the same library to be specified in multiple
  6122. method invocations. All of the contributing objects from all the invocations
  6123. (which may be from different directories) are combined and generated by a
  6124. single archive command. Note, however, that if you prune a build so that
  6125. only part of a library is specified, then only that part of the library will
  6126. be generated (the rest will disappear!).
  6127. =head2 The C<Module> method
  6128. The C<Module> method is a combination of the C<Program> and C<Command>
  6129. methods. Rather than generating an executable program directly, this command
  6130. allows you to specify your own command to actually generate a module. The
  6131. method is invoked as follows:
  6132. Module $env <module name>, <source or object files>, <construction command>;
  6133. This command is useful in instances where you wish to create, for example,
  6134. dynamically loaded modules, or statically linked code libraries.
  6135. =head2 The C<Depends> method
  6136. The C<Depends> method allows you to specify additional dependencies for a
  6137. target. It is invoked as follows:
  6138. Depends $env <target>, <dependencies>;
  6139. This may be occasionally useful, especially in cases where no scanner exists
  6140. (or is writable) for particular types of files. Normally, dependencies are
  6141. calculated automatically from a combination of the explicit dependencies set
  6142. up by the method invocation or by scanning source files.
  6143. A set of identical dependencies for multiple targets may be specified
  6144. using a reference to a list of targets. In Perl, a list reference can
  6145. be created by enclosing a list in square brackets. Hence the following
  6146. command:
  6147. Depends $env ['foo', 'bar'], 'input_file_1', 'input_file_2';
  6148. specifies that both the F<foo> and F<bar> files depend on the listed
  6149. input files.
  6150. =head2 The C<RuleSet> method
  6151. The C<RuleSet> method returns the construction variables for building
  6152. various components with one of the rule sets supported by Cons. The
  6153. currently supported rule sets are:
  6154. =over 4
  6155. =item msvc
  6156. Rules for the Microsoft Visual C++ compiler suite.
  6157. =item unix
  6158. Generic rules for most UNIX-like compiler suites.
  6159. =back
  6160. On systems with more than one available compiler suite, this allows you
  6161. to easily create side-by-side environments for building software with
  6162. multiple tools:
  6163. $msvcenv = new cons(RuleSet("msvc"));
  6164. $cygnusenv = new cons(RuleSet("unix"));
  6165. In the future, this could also be extended to other platforms that
  6166. have different default rule sets.
  6167. =head2 The C<DefaultRules> method
  6168. The C<DefaultRules> method sets the default construction variables that
  6169. will be returned by the C<new> method to the specified arguments:
  6170. DefaultRules(CC => 'gcc',
  6171. CFLAGS => '',
  6172. CCCOM => '%CC %CFLAGS %_IFLAGS -c %< -o %>');
  6173. $env = new cons();
  6174. # $env now contains *only* the CC, CFLAGS,
  6175. # and CCCOM construction variables
  6176. Combined with the C<RuleSet> method, this also provides an easy way
  6177. to set explicitly the default build environment to use some supported
  6178. toolset other than the Cons defaults:
  6179. # use a UNIX-like tool suite (like cygwin) on Win32
  6180. DefaultRules(RuleSet('unix'));
  6181. $env = new cons();
  6182. Note that the C<DefaultRules> method completely replaces the default
  6183. construction environment with the specified arguments, it does not
  6184. simply override the existing defaults. To override one or more
  6185. variables in a supported C<RuleSet>, append the variables and values:
  6186. DefaultRules(RuleSet('unix'), CFLAGS => '-O3');
  6187. $env1 = new cons();
  6188. $env2 = new cons();
  6189. # both $env1 and $env2 have 'unix' defaults
  6190. # with CFLAGS set to '-O3'
  6191. =head2 The C<Ignore> method
  6192. The C<Ignore> method allows you to ignore explicitly dependencies that
  6193. Cons infers on its own. It is invoked as follows:
  6194. Ignore <patterns>;
  6195. This can be used to avoid recompilations due to changes in system header
  6196. files or utilities that are known to not affect the generated targets.
  6197. If, for example, a program is built in an NFS-mounted directory on
  6198. multiple systems that have different copies of F<stdio.h>, the differences
  6199. will affect the signatures of all derived targets built from source files
  6200. that C<#include E<lt>stdio.hE<gt>>. This will cause all those targets to
  6201. be rebuilt when changing systems. If this is not desirable behavior, then
  6202. the following line will remove the dependencies on the F<stdio.h> file:
  6203. Ignore '^/usr/include/stdio\.h$';
  6204. Note that the arguments to the C<Ignore> method are regular expressions,
  6205. so special characters must be escaped and you may wish to anchor the
  6206. beginning or end of the expression with C<^> or C<$> characters.
  6207. =head2 The C<Salt> method
  6208. The C<Salt> method adds a constant value to the signature calculation
  6209. for every derived file. It is invoked as follows:
  6210. Salt $string;
  6211. Changing the Salt value will force a complete rebuild of every derived
  6212. file. This can be used to force rebuilds in certain desired
  6213. circumstances. For example,
  6214. Salt `uname -s`;
  6215. Would force a complete rebuild of every derived file whenever the
  6216. operating system on which the build is performed (as reported by C<uname
  6217. -s>) changes.
  6218. =head2 The C<UseCache> method
  6219. The C<UseCache> method instructs Cons to maintain a cache of derived
  6220. files, to be shared among separate build trees of the same project.
  6221. UseCache("cache/<buildname>") || warn("cache directory not found");
  6222. =head2 The C<SourcePath> method
  6223. The C<SourcePath> mathod returns the real source path name of a file,
  6224. as opposed to the path name within a build directory. It is invoked
  6225. as follows:
  6226. $path = SourcePath <buildpath>;
  6227. =head2 The C<ConsPath> method
  6228. The C<ConsPath> method returns true if the supplied path is a derivable
  6229. file, and returns undef (false) otherwise.
  6230. It is invoked as follows:
  6231. $result = ConsPath <path>;
  6232. =head2 The C<SplitPath> method
  6233. The C<SplitPath> method looks up multiple path names in a string separated
  6234. by the default path separator for the operating system (':' on UNIX
  6235. systems, ';' on Windows NT), and returns the fully-qualified names.
  6236. It is invoked as follows:
  6237. @paths = SplitPath <pathlist>;
  6238. The C<SplitPath> method will convert names prefixed '#' to the
  6239. appropriate top-level build name (without the '#') and will convert
  6240. relative names to top-level names.
  6241. =head2 The C<DirPath> method
  6242. The C<DirPath> method returns the build path name(s) of a directory or
  6243. list of directories. It is invoked as follows:
  6244. $cwd = DirPath <paths>;
  6245. The most common use for the C<DirPath> method is:
  6246. $cwd = DirPath '.';
  6247. to fetch the path to the current directory of a subsidiary F<Conscript>
  6248. file.
  6249. =head2 The C<FilePath> method
  6250. The C<FilePath> method returns the build path name(s) of a file or
  6251. list of files. It is invoked as follows:
  6252. $file = FilePath <path>;
  6253. =head2 The C<Help> method
  6254. The C<Help> method specifies help text that will be displayed when the
  6255. user invokes C<cons -h>. This can be used to provide documentation
  6256. of specific targets, values, build options, etc. for the build tree.
  6257. It is invoked as follows:
  6258. Help <helptext>;
  6259. The C<Help> method may only be called once, and should typically be
  6260. specified in the top-level F<Construct> file.
  6261. =head1 Extending Cons
  6262. =head2 Overriding construction variables
  6263. There are several ways of extending Cons, which vary in degree of
  6264. difficulty. The simplest method is to define your own construction
  6265. environment, based on the default environment, but modified to reflect your
  6266. particular needs. This will often suffice for C-based applications. You can
  6267. use the C<new> constructor, and the C<clone> and C<copy> methods to create
  6268. hybrid environments. These changes can be entirely transparent to the
  6269. underlying F<Conscript> files.
  6270. =head2 Adding new methods
  6271. For slightly more demanding changes, you may wish to add new methods to the
  6272. C<cons> package. Here's an example of a very simple extension,
  6273. C<InstallScript>, which installs a tcl script in a requested location, but
  6274. edits the script first to reflect a platform-dependent path that needs to be
  6275. installed in the script:
  6276. # cons::InstallScript - Create a platform dependent version of a shell
  6277. # script by replacing string ``#!your-path-here'' with platform specific
  6278. # path $BIN_DIR.
  6279. sub cons::InstallScript {
  6280. my ($env, $dst, $src) = @_;
  6281. Command $env $dst, $src, qq(
  6282. sed s+your-path-here+$BIN_DIR+ %< > %>
  6283. chmod oug+x %>
  6284. );
  6285. }
  6286. Notice that this method is defined directly in the C<cons> package (by
  6287. prefixing the name with C<cons::>). A change made in this manner will be
  6288. globally visible to all environments, and could be called as in the
  6289. following example:
  6290. InstallScript $env "$BIN/foo", "foo.tcl";
  6291. For a small improvement in generality, the C<BINDIR> variable could be
  6292. passed in as an argument or taken from the construction environment--as
  6293. C<%BINDIR>.
  6294. =head2 Overriding methods
  6295. Instead of adding the method to the C<cons> name space, you could define a
  6296. new package which inherits existing methods from the C<cons> package and
  6297. overrides or adds others. This can be done using Perl's inheritance
  6298. mechanisms.
  6299. The following example defines a new package C<cons::switch> which
  6300. overrides the standard C<Library> method. The overridden method builds
  6301. linked library modules, rather than library archives. A new
  6302. constructor is provided. Environments created with this constructor
  6303. will have the new library method; others won't.
  6304. package cons::switch;
  6305. BEGIN {@ISA = 'cons'}
  6306. sub new {
  6307. shift;
  6308. bless new cons(@_);
  6309. }
  6310. sub Library {
  6311. my($env) = shift;
  6312. my($lib) = shift;
  6313. my(@objs) = Objects $env @_;
  6314. Command $env $lib, @objs, q(
  6315. %LD -r %LDFLAGS %< -o %>
  6316. );
  6317. }
  6318. This functionality could be invoked as in the following example:
  6319. $env = new cons::switch(@overrides);
  6320. ...
  6321. Library $env 'lib.o', 'foo.c', 'bar.c';
  6322. =head1 Invoking Cons
  6323. The C<cons> command is usually invoked from the root of the build tree. A
  6324. F<Construct> file must exist in that directory. If the C<-f> argument is
  6325. used, then an alternate F<Construct> file may be used (and, possibly, an
  6326. alternate root, since C<cons> will cd to F<Construct> file's containing
  6327. directory).
  6328. If C<cons> is invoked from a child of the root of the build tree with
  6329. the C<-t> argument, it will walk up the directory hierarchy looking for a
  6330. F<Construct> file. (An alternate name may still be specified with C<-f>.)
  6331. The targets supplied on the command line will be modified to be relative
  6332. to the discovered F<Construct> file. For example, from a directory
  6333. containing a top-level F<Construct> file, the following invocation:
  6334. % cd libfoo/subdir
  6335. % cons -t target
  6336. is exactly equivalent to:
  6337. % cons libfoo/subdir/target
  6338. If there are any C<Default> targets specified in the directory hierarchy's
  6339. F<Construct> or F<Conscript> files, only the default targets at or below
  6340. the directory from which C<cons -t> was invoked will be built.
  6341. The command is invoked as follows:
  6342. cons <arguments> -- <construct-args>
  6343. where I<arguments> can be any of the following, in any order:
  6344. =over 10
  6345. =item I<target>
  6346. Build the specified target. If I<target> is a directory, then recursively
  6347. build everything within that directory.
  6348. =item I<+pattern>
  6349. Limit the F<Conscript> files considered to just those that match I<pattern>,
  6350. which is a Perl regular expression. Multiple C<+> arguments are accepted.
  6351. =item I<name>=<val>
  6352. Sets I<name> to value I<val> in the C<ARG> hash passed to the top-level
  6353. F<Construct> file.
  6354. =item C<-cc>
  6355. Show command that would have been executed, when retrieving from cache. No
  6356. indication that the file has been retrieved is given; this is useful for
  6357. generating build logs that can be compared with real build logs.
  6358. =item C<-cd>
  6359. Disable all caching. Do not retrieve from cache nor flush to cache.
  6360. =item C<-cr>
  6361. Build dependencies in random order. This is useful when building multiple
  6362. similar trees with caching enabled.
  6363. =item C<-cs>
  6364. Synchronize existing build targets that are found to be up-to-date with
  6365. cache. This is useful if caching has been disabled with -cc or just recently
  6366. enabled with UseCache.
  6367. =item C<-d>
  6368. Enable dependency debugging.
  6369. =item C<-f> <file>
  6370. Use the specified file instead of F<Construct> (but first change to
  6371. containing directory of I<file>).
  6372. =item C<-h>
  6373. Show a help message local to the current build if one such is defined, and
  6374. exit.
  6375. =item C<-k>
  6376. Keep going as far as possible after errors.
  6377. =item C<-o> <file>
  6378. Read override file I<file>.
  6379. =item C<-p>
  6380. Show construction products in specified trees. No build is attempted.
  6381. =item C<-pa>
  6382. Show construction products and associated actions. No build is attempted.
  6383. =item C<-pw>
  6384. Show products and where they are defined. No build is attempted.
  6385. =item C<-q>
  6386. Make the build quiet. Multiple C<-q> options may be specified.
  6387. A single C<-q> options suppress messages about Installing and Removing
  6388. targets.
  6389. Two C<-q> options suppress build command lines and target up-to-date
  6390. messages.
  6391. =item C<-r>
  6392. Remove construction products associated with <targets>. No build is
  6393. attempted.
  6394. =item C<-R> <repos>
  6395. Search for files in I<repos>. Multiple B<-R> I<repos> directories are
  6396. searched in the order specified.
  6397. =item C<-S> <pkg>
  6398. Use the sig::<pkg> package to calculate. Supported <pkg> values
  6399. include "md5" for MD5 signature calculation and "md5::debug" for debug
  6400. information about MD5 signature calculation.
  6401. If the specified package ends in <::debug>, signature debug information
  6402. will be printed to the file name specified in the C<CONS_SIG_DEBUG>
  6403. environment variable, or to standard output if the environment variable
  6404. is not set.
  6405. =item C<-t>
  6406. Traverse up the directory hierarchy looking for a F<Construct> file,
  6407. if none exists in the current directory. Targets will be modified to
  6408. be relative to the F<Construct> file.
  6409. Internally, C<cons> will change its working directory to the directory
  6410. which contains the top-level F<Construct> file and report:
  6411. cons: Entering directory `top-level-directory'
  6412. This message indicates to an invoking editor (such as emacs) or build
  6413. environment that Cons will now report all file names relative to the
  6414. top-level directory. This message can not be suppressed with the C<-q>
  6415. option.
  6416. =item C<-v>
  6417. Show C<cons> version and continue processing.
  6418. =item C<-V>
  6419. Show C<cons> version and exit.
  6420. =item C<-wf> <file>
  6421. Write all filenames considered into I<file>.
  6422. =item C<-x>
  6423. Show a help message similar to this one, and exit.
  6424. =back
  6425. And I<construct-args> can be any arguments that you wish to process in the
  6426. F<Construct> file. Note that there should be a B<--> separating the arguments
  6427. to cons and the arguments that you wish to process in the F<Construct> file.
  6428. Processing of I<construct-args> can be done by any standard package like
  6429. B<Getopt> or its variants, or any user defined package. B<cons> will pass in
  6430. the I<construct-args> as B<@ARGV> and will not attempt to interpret anything
  6431. after the B<-->.
  6432. % cons -R /usr/local/repository -d os=solaris +driver -- -c test -f DEBUG
  6433. would pass the following to cons
  6434. -R /usr/local/repository -d os=solaris +driver
  6435. and the following, to the top level F<Construct> file as B<@ARGV>
  6436. -c test -f DEBUG
  6437. Note that C<cons -r .> is equivalent to a full recursive C<make clean>,
  6438. but requires no support in the F<Construct> file or any F<Conscript>
  6439. files. This is most useful if you are compiling files into source
  6440. directories (if you separate the F<build> and F<export> directories,
  6441. then you can just remove the directories).
  6442. The options C<-p>, C<-pa>, and C<-pw> are extremely useful for use as an aid
  6443. in reading scripts or debugging them. If you want to know what script
  6444. installs F<export/include/foo.h>, for example, just type:
  6445. % cons -pw export/include/foo.h
  6446. =head1 Using and writing dependency scanners
  6447. QuickScan allows simple target-independent scanners to be set up for
  6448. source files. Only one QuickScan scanner may be associated with any given
  6449. source file and environment, although the same scanner may (and should)
  6450. be used for multiple files of a given type.
  6451. A QuickScan scanner is only ever invoked once for a given source file,
  6452. and it is only invoked if the file is used by some target in the tree
  6453. (i.e., there is a dependency on the source file).
  6454. QuickScan is invoked as follows:
  6455. QuickScan CONSENV CODEREF, FILENAME [, PATH]
  6456. The subroutine referenced by CODEREF is expected to return a list of
  6457. filenames included directly by FILE. These filenames will, in turn, be
  6458. scanned. The optional PATH argument supplies a lookup path for finding
  6459. FILENAME and/or files returned by the user-supplied subroutine. The PATH
  6460. may be a reference to an array of lookup-directory names, or a string of
  6461. names separated by the system's separator character (':' on UNIX systems,
  6462. ';' on Windows NT).
  6463. The subroutine is called once for each line in the file, with $_ set to the
  6464. current line. If the subroutine needs to look at additional lines, or, for
  6465. that matter, the entire file, then it may read them itself, from the
  6466. filehandle SCAN. It may also terminate the loop, if it knows that no further
  6467. include information is available, by closing the filehandle.
  6468. Whether or not a lookup path is provided, QuickScan first tries to lookup
  6469. the file relative to the current directory (for the top-level file
  6470. supplied directly to QuickScan), or from the directory containing the
  6471. file which referenced the file. This is not very general, but seems good
  6472. enough--especially if you have the luxury of writing your own utilities
  6473. and can control the use of the search path in a standard way.
  6474. Here's a real example, taken from a F<Construct> file here:
  6475. sub cons::SMFgen {
  6476. my($env, @tables) = @_;
  6477. foreach $t (@tables) {
  6478. $env->QuickScan(sub { /\b\S*?\.smf\b/g }, "$t.smf",
  6479. $env->{SMF_INCLUDE_PATH});
  6480. $env->Command(["$t.smdb.cc","$t.smdb.h","$t.snmp.cc",
  6481. "$t.ami.cc", "$t.http.cc"], "$t.smf",
  6482. q(smfgen %( %SMF_INCLUDE_OPT %) %<));
  6483. }
  6484. }
  6485. The subroutine above finds all names of the form <name>.smf in the
  6486. file. It will return the names even if they're found within comments,
  6487. but that's OK (the mechanism is forgiving of extra files; they're just
  6488. ignored on the assumption that the missing file will be noticed when
  6489. the program, in this example, smfgen, is actually invoked).
  6490. [NOTE that the form C<$env-E<gt>QuickScan ...> and C<$env-E<gt>Command
  6491. ...> should not be necessary, but, for some reason, is required
  6492. for this particular invocation. This appears to be a bug in Perl or
  6493. a misunderstanding on my part; this invocation style does not always
  6494. appear to be necessary.]
  6495. Here is another way to build the same scanner. This one uses an
  6496. explicit code reference, and also (unnecessarily, in this case) reads
  6497. the whole file itself:
  6498. sub myscan {
  6499. my(@includes);
  6500. do {
  6501. push(@includes, /\b\S*?\.smf\b/g);
  6502. } while <SCAN>;
  6503. @includes
  6504. }
  6505. Note that the order of the loop is reversed, with the loop test at the
  6506. end. This is because the first line is already read for you. This scanner
  6507. can be attached to a source file by:
  6508. QuickScan $env \&myscan, "$_.smf";
  6509. This final example, which scans a different type of input file, takes
  6510. over the file scanning rather than being called for each input line:
  6511. $env->QuickScan(
  6512. sub { my(@includes) = ();
  6513. do {
  6514. push(@includes, $3)
  6515. if /^(#include|import)\s+(\")(.+)(\")/ && $3
  6516. } while <SCAN>;
  6517. @includes
  6518. },
  6519. "$idlFileName",
  6520. "$env->{CPPPATH};$BUILD/ActiveContext/ACSCLientInterfaces"
  6521. );
  6522. =head1 SUPPORT AND SUGGESTIONS
  6523. Cons is maintained by the user community. To subscribe, send mail to
  6524. B<cons-discuss-request@gnu.org> with body B<subscribe>.
  6525. Please report any suggestions through the B<cons-discuss@gnu.org> mailing
  6526. list.
  6527. =head1 BUGS
  6528. Sure to be some. Please report any bugs through the B<bug-cons@gnu.org>
  6529. mailing list.
  6530. =head1 INFORMATION ABOUT CONS
  6531. Information about CONS can be obtained from the official cons web site
  6532. B<http://www.dsmit.com/cons/> or its mirrors listed there.
  6533. The cons maintainers can be contacted by email at
  6534. B<cons-maintainers@gnu.org>
  6535. =head1 AUTHORS
  6536. Originally by Bob Sidebotham. Then significantly enriched by the members
  6537. of the Cons community B<cons-discuss@gnu.org>.
  6538. The Cons community would like to thank Ulrich Pfeifer for the original pod
  6539. documentation derived from the F<cons.html> file. Cons documentation is now
  6540. a part of the program itself.
  6541. =cut