{-# OPTIONS -cpp #-}-- OPTIONS required for ghc-6.4.x compat, and must appear first{-# LANGUAGE CPP #-}{-# OPTIONS_GHC -cpp #-}{-# OPTIONS_NHC98 -cpp #-}{-# OPTIONS_JHC -fcpp #-}------------------------------------------------------------------------------- |-- Module : Distribution.Configuration-- Copyright : Thomas Schilling, 2007---- Maintainer : cabal-devel@haskell.org-- Portability : portable---- This is about the cabal configurations feature. It exports-- 'finalizePackageDescription' and 'flattenPackageDescription' which are-- functions for converting 'GenericPackageDescription's down to-- 'PackageDescription's. It has code for working with the tree of conditions-- and resolving or flattening conditions.{- All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of Isaac Jones nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -}moduleDistribution.PackageDescription.Configuration(finalizePackageDescription,flattenPackageDescription,-- UtilsparseCondition,freeVars,)whereimportDistribution.Package(PackageName,Dependency(..))importDistribution.PackageDescription(GenericPackageDescription(..),PackageDescription(..),Library(..),Executable(..),BuildInfo(..),Flag(..),FlagName(..),FlagAssignment,CondTree(..),ConfVar(..),Condition(..))importDistribution.Version(VersionRange,anyVersion,intersectVersionRanges,withinRange)importDistribution.Compiler(CompilerId(CompilerId))importDistribution.System(Platform(..),OS,Arch)importDistribution.Simple.Utils(currentDir,lowercase)importDistribution.Text(Text(parse))importDistribution.Compat.ReadPasReadPhiding(char)importControl.Arrow(first)importqualifiedDistribution.Compat.ReadPasReadP(char)importData.Char(isAlphaNum)importData.Maybe(catMaybes,maybeToList)importData.Map(Map,fromListWith,toList)importqualifiedData.MapasMapimportData.Monoid#if defined(__GLASGOW_HASKELL__) && (__GLASGOW_HASKELL__ < 606)importqualifiedText.ReadasRimportqualifiedText.Read.LexasL#endif-------------------------------------------------------------------------------- | Simplify the condition and return its free variables.simplifyCondition::Conditionc->(c->EitherdBool)-- ^ (partial) variable assignment->(Conditiond,[d])simplifyConditioncondi=fv.walk$condwherewalkcnd=casecndofVarv->eitherVarLit(iv)Litb->LitbCNotc->casewalkcofLitTrue->LitFalseLitFalse->LitTruec'->CNotc'COrcd->case(walkc,walkd)of(LitFalse,d')->d'(LitTrue,_)->LitTrue(c',LitFalse)->c'(_,LitTrue)->LitTrue(c',d')->COrc'd'CAndcd->case(walkc,walkd)of(LitFalse,_)->LitFalse(LitTrue,d')->d'(_,LitFalse)->LitFalse(c',LitTrue)->c'(c',d')->CAndc'd'-- gather free varsfvc=(c,fv'c)fv'c=casecofVarv->[v]Lit_->[]CNotc'->fv'c'COrc1c2->fv'c1++fv'c2CAndc1c2->fv'c1++fv'c2-- | Simplify a configuration condition using the os and arch names. Returns-- the names of all the flags occurring in the condition.simplifyWithSysParams::OS->Arch->CompilerId->ConditionConfVar->(ConditionFlagName,[FlagName])simplifyWithSysParamsosarch(CompilerIdcompcompVer)cond=(cond',flags)where(cond',flags)=simplifyConditioncondinterpinterp(OSos')=Right$os'==osinterp(Archarch')=Right$arch'==archinterp(Implcomp'vr)=Right$comp'==comp&&compVer`withinRange`vrinterp(Flagf)=Leftf-- XXX: Add instances and check---- prop_sC_idempotent cond a o = cond' == cond''-- where-- cond' = simplifyCondition cond a o-- cond'' = simplifyCondition cond' a o---- prop_sC_noLits cond a o = isLit res || not (hasLits res)-- where-- res = simplifyCondition cond a o-- hasLits (Lit _) = True-- hasLits (CNot c) = hasLits c-- hasLits (COr l r) = hasLits l || hasLits r-- hasLits (CAnd l r) = hasLits l || hasLits r-- hasLits _ = False---- | Parse a configuration condition from a string.parseCondition::ReadPr(ConditionConfVar)parseCondition=condOrwherecondOr=sepBy1condAnd(oper"||")>>=return.foldl1COrcondAnd=sepBy1cond(oper"&&")>>=return.foldl1CAndcond=sp>>(boolLiteral+++inparenscondOr+++notCond+++osCond+++archCond+++flagCond+++implCond)inparens=between(ReadP.char'('>>sp)(sp>>ReadP.char')'>>sp)notCond=ReadP.char'!'>>sp>>cond>>=return.CNotosCond=string"os">>sp>>inparensosIdent>>=return.VararchCond=string"arch">>sp>>inparensarchIdent>>=return.VarflagCond=string"flag">>sp>>inparensflagIdent>>=return.VarimplCond=string"impl">>sp>>inparensimplIdent>>=return.VarboolLiteral=fmapLitparsearchIdent=fmapArchparseosIdent=fmapOSparseflagIdent=fmap(Flag.FlagName.lowercase)(munch1isIdentChar)isIdentCharc=isAlphaNumc||c=='_'||c=='-'opers=sp>>strings>>spsp=skipSpacesimplIdent=doi<-parsevr<-sp>>optionanyVersionparsereturn$Implivr------------------------------------------------------------------------------mapCondTree::(a->b)->(c->d)->(Conditionv->Conditionw)->CondTreevca->CondTreewdbmapCondTreefafcfcnd(CondNodeacifs)=CondNode(faa)(fcc)(mapgifs)whereg(cnd,t,me)=(fcndcnd,mapCondTreefafcfcndt,fmap(mapCondTreefafcfcnd)me)mapTreeConstrs::(c->d)->CondTreevca->CondTreevdamapTreeConstrsf=mapCondTreeidfidmapTreeConds::(Conditionv->Conditionw)->CondTreevca->CondTreewcamapTreeCondsf=mapCondTreeididfmapTreeData::(a->b)->CondTreevca->CondTreevcbmapTreeDataf=mapCondTreefidid-- | Result of dependency test. Isomorphic to @Maybe d@ but renamed for-- clarity.dataDepTestRsltd=DepOk|MissingDepsdinstanceMonoidd=>Monoid(DepTestRsltd)wheremempty=DepOkmappendDepOkx=xmappendxDepOk=xmappend(MissingDepsd)(MissingDepsd')=MissingDeps(d`mappend`d')dataBTa=BTNa|BTB(BTa)(BTa)-- very simple binary tree-- | Try to find a flag assignment that satisfies the constaints of all trees.---- Returns either the missing dependencies, or a tuple containing the-- resulting data, the associated dependencies, and the chosen flag-- assignments.---- In case of failure, the _smallest_ number of of missing dependencies is-- returned. [XXX: Could also be specified with a function argument.]---- XXX: The current algorithm is rather naive. A better approach would be to:---- * Rule out possible paths, by taking a look at the associated dependencies.---- * Infer the required values for the conditions of these paths, and-- calculate the required domains for the variables used in these-- conditions. Then picking a flag assignment would be linear (I guess).---- This would require some sort of SAT solving, though, thus it's not-- implemented unless we really need it.--resolveWithFlags::Monoida=>[(FlagName,[Bool])]-- ^ Domain for each flag name, will be tested in order.->OS-- ^ OS as returned by Distribution.System.buildOS->Arch-- ^ Arch as returned by Distribution.System.buildArch->CompilerId-- ^ Compiler flavour + version->[Dependency]-- ^ Additional constraints->[CondTreeConfVar[Dependency]a]->([Dependency]->DepTestRslt[Dependency])-- ^ Dependency test function.->Either[Dependency](TargetSeta,FlagAssignment)-- ^ Either the missing dependencies (error case), or a pair of-- (set of build targets with dependencies, chosen flag assignments)resolveWithFlagsdomosarchimplconstrstreescheckDeps=casetrydom[]ofRightr->RightrLeftdbt->Left$findShortestdbtwhereextraConstrs=toDepMapconstrs-- simplify trees by (partially) evaluating all conditions and converting-- dependencies to dependency maps.simplifiedTrees=map(mapTreeConstrstoDepMap-- convert to maps.mapTreeConds(fst.simplifyWithSysParamsosarchimpl))trees-- @try@ recursively tries all possible flag assignments in the domain and-- either succeeds or returns a binary tree with the missing dependencies-- encountered in each run. Since the tree is constructed lazily, we-- avoid some computation overhead in the successful case.try[]flags=lettargetSet=TargetSet$flipmapsimplifiedTrees$-- apply additional constraints to all dependenciesfirst(`constrainBy`extraConstrs).simplifyCondTree(envflags)deps=overallDependenciestargetSetincasecheckDeps(fromDepMapdeps)ofDepOk->Right(targetSet,flags)MissingDepsmds->Left(BTNmds)try((n,vals):rest)flags=tryAll$map(\v->tryrest((n,v):flags))valstryAll=foldrmpmz-- special version of `mplus' for our local purposesmp(Leftxs)(Leftys)=(Left(BTBxsys))mp(Left_)m@(Right_)=mmpm@(Right_)_=m-- `mzero'mz=Left(BTN[])envflagsflag=(maybe(Leftflag)Right.lookupflag)flags-- for the error case we inspect our lazy tree of missing dependencies and-- pick the shortest list of missing dependenciesfindShortest(BTNx)=xfindShortest(BTBltrt)=letl=findShortestltr=findShortestrtincase(l,r)of([],xs)->xs-- [] is too short(xs,[])->xs([x],_)->[x]-- single elem is optimum(_,[x])->[x](xs,ys)->iflazyLengthCmpxsysthenxselseys-- lazy variant of @\xs ys -> length xs <= length ys@lazyLengthCmp[]_=TruelazyLengthCmp_[]=FalselazyLengthCmp(_:xs)(_:ys)=lazyLengthCmpxsys-- | A map of dependencies. Newtyped since the default monoid instance is not-- appropriate. The monoid instance uses 'intersectVersionRanges'.newtypeDependencyMap=DependencyMap{unDependencyMap::MapPackageNameVersionRange}#if !defined(__GLASGOW_HASKELL__) || (__GLASGOW_HASKELL__ >= 606)deriving(Show,Read)#else-- The Show/Read instance for Data.Map in ghc-6.4 is useless-- so we have to re-implement it here:instanceShowDependencyMapwhereshowsPrecd(DependencyMapm)=showParen(d>10)(showString"DependencyMap".shows(M.toListm))instanceReadDependencyMapwherereadPrec=parens$R.prec10$doR.Ident"DependencyMap"<-R.lexPxs<-R.readPrecreturn(DependencyMap(M.fromListxs))whereparens::R.ReadPreca->R.ReadPrecaparensp=optionalwhereoptional=pR.+++mandatorymandatory=parenoptionalparen::R.ReadPreca->R.ReadPrecaparenp=doL.Punc"("<-R.lexPx<-R.resetpL.Punc")"<-R.lexPreturnxreadListPrec=R.readListPrecDefault#endifinstanceMonoidDependencyMapwheremempty=DependencyMapMap.empty(DependencyMapa)`mappend`(DependencyMapb)=DependencyMap(Map.unionWithintersectVersionRangesab)toDepMap::[Dependency]->DependencyMaptoDepMapds=DependencyMap$fromListWithintersectVersionRanges[(p,vr)|Dependencypvr<-ds]fromDepMap::DependencyMap->[Dependency]fromDepMapm=[Dependencypvr|(p,vr)<-toList(unDependencyMapm)]simplifyCondTree::(Monoida,Monoidd)=>(v->EithervBool)->CondTreevda->(d,a)simplifyCondTreeenv(CondNodeadifs)=foldrmappend(d,a)$catMaybes$mapsimplifyIfifswheresimplifyIf(cnd,t,me)=casesimplifyConditioncndenvof(LitTrue,_)->Just$simplifyCondTreeenvt(LitFalse,_)->fmap(simplifyCondTreeenv)me_->error$"Environment not defined for all free vars"-- | Flatten a CondTree. This will resolve the CondTree by taking all-- possible paths into account. Note that since branches represent exclusive-- choices this may not result in a \"sane\" result.ignoreConditions::(Monoida,Monoidc)=>CondTreevca->(a,c)ignoreConditions(CondNodeacifs)=(a,c)`mappend`mconcat(concatMapfifs)wheref(_,t,me)=ignoreConditionst:maybeToList(fmapignoreConditionsme)freeVars::CondTreeConfVarca->[FlagName]freeVarst=[f|Flagf<-freeVars't]wherefreeVars'(CondNode__ifs)=concatMapcompfvifscompfv(c,ct,mct)=condfvc++freeVars'ct++maybe[]freeVars'mctcondfvc=casecofVarv->[v]Lit_->[]CNotc'->condfvc'COrc1c2->condfvc1++condfvc2CAndc1c2->condfvc1++condfvc2-------------------------------------------------------------------------------- | A set of targets with their package dependenciesnewtypeTargetSeta=TargetSet[(DependencyMap,a)]-- | Combine the target-specific dependencies in a TargetSet to give the-- dependencies for the package as a whole.overallDependencies::Monoida=>TargetSeta->DependencyMapoverallDependencies(TargetSettargets)=mconcatdepsswhere(depss,_)=unziptargets-- Apply extra constraints to a dependency map.-- Combines dependencies where the result will only contain keys from the left-- (first) map. If a key also exists in the right map, both constraints will-- be intersected.constrainBy::DependencyMap-- ^ Input map->DependencyMap-- ^ Extra constraints->DependencyMapconstrainByleftextra=DependencyMap$Map.foldWithKeytightenConstraint(unDependencyMapleft)(unDependencyMapextra)wheretightenConstraintncl=caseMap.lookupnlofNothing->lJustvr->Map.insertn(intersectVersionRangesvrc)l-- | Collect up the targets in a TargetSet of tagged targets, storing the-- dependencies as we go.flattenTaggedTargets::TargetSetPDTagged->(MaybeLibrary,[(String,Executable)])flattenTaggedTargets(TargetSettargets)=foldruntag(Nothing,[])targetswhereuntag(_,Lib_)(Just_,_)=bug"Only one library expected"untag(deps,Libl)(Nothing,exes)=(Justl',exes)wherel'=l{libBuildInfo=(libBuildInfol){targetBuildDepends=fromDepMapdeps}}untag(deps,Exene)(mlib,exes)|any((==n).fst)exes=bug"Exe with same name found"|otherwise=(mlib,exes++[(n,e')])wheree'=e{buildInfo=(buildInfoe){targetBuildDepends=fromDepMapdeps}}untag(_,PDNull)x=x-- actually this should not happen, but let's be liberal-------------------------------------------------------------------------------- Convert GenericPackageDescription to PackageDescription--dataPDTagged=LibLibrary|ExeStringExecutable|PDNullderivingShowinstanceMonoidPDTaggedwheremempty=PDNullPDNull`mappend`x=xx`mappend`PDNull=xLibl`mappend`Libl'=Lib(l`mappend`l')Exene`mappend`Exen'e'|n==n'=Exen(e`mappend`e')_`mappend`_=bug"Cannot combine incompatible tags"-- | Create a package description with all configurations resolved.---- This function takes a `GenericPackageDescription` and several environment-- parameters and tries to generate `PackageDescription` by finding a flag-- assignment that result in satisfiable dependencies.---- It takes as inputs a not necessarily complete specifications of flags-- assignments, an optional package index as well as platform parameters. If-- some flags are not assigned explicitly, this function will try to pick an-- assignment that causes this function to succeed. The package index is-- optional since on some platforms we cannot determine which packages have-- been installed before. When no package index is supplied, every dependency-- is assumed to be satisfiable, therefore all not explicitly assigned flags-- will get their default values.---- This function will fail if it cannot find a flag assignment that leads to-- satisfiable dependencies. (It will not try alternative assignments for-- explicitly specified flags.) In case of failure it will return a /minimum/-- number of dependencies that could not be satisfied. On success, it will-- return the package description and the full flag assignment chosen.--finalizePackageDescription::FlagAssignment-- ^ Explicitly specified flag assignments->(Dependency->Bool)-- ^ Is a given depenency satisfiable from the set of available packages?-- If this is unknown then use True.->Platform-- ^ The 'Arch' and 'OS'->CompilerId-- ^ Compiler + Version->[Dependency]-- ^ Additional constraints->GenericPackageDescription->Either[Dependency](PackageDescription,FlagAssignment)-- ^ Either missing dependencies or the resolved package-- description along with the flag assignments chosen.finalizePackageDescriptionuserflagssatisfyDep(Platformarchos)implconstraints(GenericPackageDescriptionpkgflagsmlib0exes0)=caseresolveFlagsofRight((mlib,exes'),targetSet,flagVals)->Right(pkg{library=mlib,executables=exes',buildDepends=fromDepMap(overallDependenciestargetSet)--TODO: we need to find a way to avoid pulling in deps-- for non-buildable components. However cannot simply-- filter at this stage, since if the package were not-- available we would have failed already.},flagVals)Leftmissing->Leftmissingwhere-- Combine lib and exes into one list of @CondTree@s with tagged datacondTrees=maybeToList(fmap(mapTreeDataLib)mlib0)++map(\(name,tree)->mapTreeData(Exename)tree)exes0resolveFlags=caseresolveWithFlagsflagChoicesosarchimplconstraintscondTreescheckofRight(targetSet,fs)->let(mlib,exes)=flattenTaggedTargetstargetSetinRight((fmaplibFillInDefaultsmlib,map(\(n,e)->(exeFillInDefaultse){exeName=n})exes),targetSet,fs)Leftmissing->LeftmissingflagChoices=map(\(MkFlagn_dmanual)->(n,d2cmanualnd))flagsd2cmanualnb=caselookupnuserflagsofJustval->[val]Nothing|manual->[b]|otherwise->[b,notb]--flagDefaults = map (\(n,x:_) -> (n,x)) flagChoicescheckds=ifallsatisfyDepdsthenDepOkelseMissingDeps$filter(not.satisfyDep)ds{-
let tst_p = (CondNode [1::Int] [Distribution.Package.Dependency "a" AnyVersion] [])
let tst_p2 = (CondNode [1::Int] [Distribution.Package.Dependency "a" (EarlierVersion (Version [1,0] [])), Distribution.Package.Dependency "a" (LaterVersion (Version [2,0] []))] [])
let p_index = Distribution.Simple.PackageIndex.fromList [Distribution.Package.PackageIdentifier "a" (Version [0,5] []), Distribution.Package.PackageIdentifier "a" (Version [2,5] [])]
let look = not . null . Distribution.Simple.PackageIndex.lookupDependency p_index
let looks ds = mconcat $ map (\d -> if look d then DepOk else MissingDeps [d]) ds
resolveWithFlags [] Distribution.System.Linux Distribution.System.I386 (Distribution.Compiler.GHC,Version [6,8,2] []) [tst_p] looks ===> Right ...
resolveWithFlags [] Distribution.System.Linux Distribution.System.I386 (Distribution.Compiler.GHC,Version [6,8,2] []) [tst_p2] looks ===> Left ...
-}-- | Flatten a generic package description by ignoring all conditions and just-- join the field descriptors into on package description. Note, however,-- that this may lead to inconsistent field values, since all values are-- joined into one field, which may not be possible in the original package-- description, due to the use of exclusive choices (if ... else ...).---- XXX: One particularly tricky case is defaulting. In the original package-- description, e.g., the source directory might either be the default or a-- certain, explicitly set path. Since defaults are filled in only after the-- package has been resolved and when no explicit value has been set, the-- default path will be missing from the package description returned by this-- function.flattenPackageDescription::GenericPackageDescription->PackageDescriptionflattenPackageDescription(GenericPackageDescriptionpkg_mlib0exes0)=pkg{library=mlib,executables=reverseexes,buildDepends=ldeps++reverseedeps}where(mlib,ldeps)=casemlib0ofJustlib->let(l,ds)=ignoreConditionslibin(Just(libFillInDefaultsl),ds)Nothing->(Nothing,[])(exes,edeps)=foldrflattenExe([],[])exes0flattenExe(n,t)(es,ds)=let(e,ds')=ignoreConditionstin((exeFillInDefaults$e{exeName=n}):es,ds'++ds)-- This is in fact rather a hack. The original version just overrode the-- default values, however, when adding conditions we had to switch to a-- modifier-based approach. There, nothing is ever overwritten, but only-- joined together.---- This is the cleanest way i could think of, that doesn't require-- changing all field parsing functions to return modifiers instead.libFillInDefaults::Library->LibrarylibFillInDefaultslib@(Library{libBuildInfo=bi})=lib{libBuildInfo=biFillInDefaultsbi}exeFillInDefaults::Executable->ExecutableexeFillInDefaultsexe@(Executable{buildInfo=bi})=exe{buildInfo=biFillInDefaultsbi}biFillInDefaults::BuildInfo->BuildInfobiFillInDefaultsbi=ifnull(hsSourceDirsbi)thenbi{hsSourceDirs=[currentDir]}elsebibug::String->abugmsg=error$msg++". Consider this a bug."