Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 281 lines (254 sloc) 10.468 kb
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
1 -- A program for generating a Gentoo ebuild from a .cabal file
2 --
3 -- Author : Duncan Coutts <dcoutts@gentoo.org>
4 --
5 -- Created: 21 July 2005
6 --
7 -- Copyright (C) 2005 Duncan Coutts
8 --
9 -- This library is free software; you can redistribute it and/or
10 -- modify it under the terms of the GNU General Public License
11 -- as published by the Free Software Foundation; either version 2
12 -- of the License, or (at your option) any later version.
13 --
14 -- This library is distributed in the hope that it will be useful,
15 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
16 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 -- General Public License for more details.
18 --
19 -- |
20 -- Maintainer : haskell@gentoo.org
21 --
22 -- cabal2ebuild - a program for generating a Gentoo ebuild from a .cabal file
23 --
24 module Cabal2Ebuild
e35e41e @kolmodin Make merging respect categories
kolmodin authored
25 (EBuild(..)
26 ,cabal2ebuild
27 ,showEBuild) where
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
28
29 import qualified Distribution.PackageDescription as Cabal
87d875b @kolmodin Wall police
kolmodin authored
30 (PackageDescription(..))
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
31 import qualified Distribution.Package as Cabal (PackageIdentifier(..))
32 import qualified Distribution.Version as Cabal (showVersion, Dependency(..),
33 VersionRange(..))
34 import qualified Distribution.License as Cabal (License(..))
35 --import qualified Distribution.Compiler as Cabal (CompilerFlavor(..))
36
8022ddc Adding support for variable substitution in generated ebuilds
der_eq@freenet.de authored
37 import Data.Char (toLower,isUpper)
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
38
39 data EBuild = EBuild {
40 name :: String,
41 version :: String,
42 description :: String,
43 homepage :: String,
44 src_uri :: String,
45 license :: String,
46 slot :: String,
47 keywords :: [String],
48 iuse :: [String],
49 depend :: [Dependency],
50 features :: [String],
51 -- comments on various fields for communicating stuff to the user
52 licenseComments :: String,
8022ddc Adding support for variable substitution in generated ebuilds
der_eq@freenet.de authored
53 my_pn :: Maybe String --If the package's name contains upper-case
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
54 }
55
56 type Package = String
57 type Version = String
58 type UseFlag = String
59 data Dependency = AnyVersionOf Package
60 | ThisVersionOf Version Package -- =package-version
61 | LaterVersionOf Version Package -- >package-version
62 | EarlierVersionOf Version Package -- <package-version
63 | OrLaterVersionOf Version Package -- >=package-version
64 | OrEarlierVersionOf Version Package -- <=package-version
65 | DependEither Dependency Dependency -- depend || depend
66 | DependIfUse UseFlag Dependency -- use? ( depend )
67
87d875b @kolmodin Wall police
kolmodin authored
68 ebuildTemplate :: EBuild
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
69 ebuildTemplate = EBuild {
70 name = "foobar",
71 version = "0.1",
72 description = "",
73 homepage = "",
74 src_uri = "",
75 license = "",
76 slot = "0",
63b57b8 Adapted HackPort to the new Hackage interface
der_eq@freenet.de authored
77 keywords = ["~amd64","~x86"],
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
78 iuse = [],
79 depend = [],
c81b055 profile and haddock only make sense for lib packages
Duncan Coutts authored
80 features = [],
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
81 licenseComments = "",
8022ddc Adding support for variable substitution in generated ebuilds
der_eq@freenet.de authored
82 my_pn = Nothing
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
83 }
84
85 cabal2ebuild :: Cabal.PackageDescription -> EBuild
86 cabal2ebuild pkg = ebuildTemplate {
8022ddc Adding support for variable substitution in generated ebuilds
der_eq@freenet.de authored
87 name = map toLower cabalPkgName,
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
88 version = Cabal.showVersion (Cabal.pkgVersion (Cabal.package pkg)),
89 description = if null (Cabal.synopsis pkg) then Cabal.description pkg
90 else Cabal.synopsis pkg,
91 homepage = Cabal.homepage pkg,
92 src_uri = Cabal.pkgUrl pkg,
93 license = convertLicense (Cabal.license pkg),
94 licenseComments = licenseComment (Cabal.license pkg),
95 depend = defaultDepGHC
c6327c1 Add possibly-versioned dependency on cabal
Duncan Coutts authored
96 : convertDependency (Cabal.Dependency "Cabal"
97 (Cabal.descCabalVersion pkg))
98 ++ convertDependencies (Cabal.buildDepends pkg),
20179c4 Set S= properly for packages with upper case names
Duncan Coutts authored
99 my_pn = if any isUpper cabalPkgName then Just cabalPkgName else Nothing,
f5d6f88 reorder the features list
Duncan Coutts authored
100 features = features ebuildTemplate
4c4b676 @kolmodin Fix subtile bug with missing features
kolmodin authored
101 ++ (if null (Cabal.executables pkg) then [] else ["bin"])
f5d6f88 reorder the features list
Duncan Coutts authored
102 ++ maybe [] (const ["lib","profile","haddock"]) (Cabal.library pkg)
8022ddc Adding support for variable substitution in generated ebuilds
der_eq@freenet.de authored
103 } where
e35e41e @kolmodin Make merging respect categories
kolmodin authored
104 cabalPkgName = Cabal.pkgName (Cabal.package pkg)
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
105
87d875b @kolmodin Wall police
kolmodin authored
106 defaultDepGHC :: Dependency
4762099 Increase default ghc dep to 6.6.1
Duncan Coutts authored
107 defaultDepGHC = OrLaterVersionOf "6.6.1" "dev-lang/ghc"
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
108
109 -- map the cabal license type to the gentoo license string format
110 convertLicense :: Cabal.License -> String
111 convertLicense Cabal.GPL = "GPL-2" -- almost certainly version 2
112 convertLicense Cabal.LGPL = "LGPL-2.1" -- probably version 2.1
113 convertLicense Cabal.BSD3 = "BSD" -- do we really not
114 convertLicense Cabal.BSD4 = "BSD" -- distinguish between these?
115 convertLicense Cabal.PublicDomain = "public-domain"
116 convertLicense Cabal.AllRightsReserved = ""
117 convertLicense _ = ""
118
87d875b @kolmodin Wall police
kolmodin authored
119 licenseComment :: Cabal.License -> String
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
120 licenseComment Cabal.AllRightsReserved =
121 "Note: packages without a license cannot be included in portage"
122 licenseComment Cabal.OtherLicense =
123 "Fixme: \"OtherLicense\", please fill in manually"
124 licenseComment _ = ""
125
126 convertDependencies :: [Cabal.Dependency] -> [Dependency]
ace40ea Cope with IntersectVersionRanges, at least partially
Duncan Coutts authored
127 convertDependencies = concatMap convertDependency
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
128
ace40ea Cope with IntersectVersionRanges, at least partially
Duncan Coutts authored
129 convertDependency :: Cabal.Dependency -> [Dependency]
87d875b @kolmodin Wall police
kolmodin authored
130 convertDependency (Cabal.Dependency pname _)
131 | pname `elem` coreLibs = [] -- no explicit dep on core libs
132 convertDependency (Cabal.Dependency pname versionRange)
ace40ea Cope with IntersectVersionRanges, at least partially
Duncan Coutts authored
133 = case versionRange of
134 (Cabal.IntersectVersionRanges v1 v2) -> [convert v1, convert v2]
135 v -> [convert v]
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
136
137 where
87d875b @kolmodin Wall police
kolmodin authored
138 ebuildName = "dev-haskell/" ++ map toLower pname
9448bb7 @kolmodin Rewritten overlayonly functionality
kolmodin authored
139
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
140 convert :: Cabal.VersionRange -> Dependency
141 convert Cabal.AnyVersion = AnyVersionOf ebuildName
142 convert (Cabal.ThisVersion v) = ThisVersionOf (Cabal.showVersion v) ebuildName
143 convert (Cabal.LaterVersion v) = LaterVersionOf (Cabal.showVersion v) ebuildName
144 convert (Cabal.EarlierVersion v) = EarlierVersionOf (Cabal.showVersion v) ebuildName
145 convert (Cabal.UnionVersionRanges (Cabal.ThisVersion v1) (Cabal.LaterVersion v2))
146 | v1 == v2 = OrLaterVersionOf (Cabal.showVersion v1) ebuildName
147 convert (Cabal.UnionVersionRanges (Cabal.ThisVersion v1) (Cabal.EarlierVersion v2))
148 | v1 == v2 = OrEarlierVersionOf (Cabal.showVersion v1) ebuildName
149 convert (Cabal.UnionVersionRanges r1 r2)
150 = DependEither (convert r1) (convert r2)
151
87d875b @kolmodin Wall police
kolmodin authored
152 coreLibs :: [String]
5bcf377 Update hackport's ebuild generation to current conventions.
Duncan Coutts authored
153 coreLibs =
61f04b0 Update list of core libs
Duncan Coutts authored
154 ["array"
8022ddc Adding support for variable substitution in generated ebuilds
der_eq@freenet.de authored
155 ,"base"
61f04b0 Update list of core libs
Duncan Coutts authored
156 --,"bytestring" --already has ebuild
157 ,"containers"
158 ,"directory"
159 --,"filepath" --already has ebuild
160 ,"ghc"
8022ddc Adding support for variable substitution in generated ebuilds
der_eq@freenet.de authored
161 ,"haskell98"
2f85f14 @kolmodin Write comments about core packages
kolmodin authored
162 ,"hpc" --has ebuild, but only in the overlay
61f04b0 Update list of core libs
Duncan Coutts authored
163 ,"old-locale"
164 ,"old-time"
165 ,"packedstring"
166 ,"pretty"
167 ,"process"
168 ,"random"
2f85f14 @kolmodin Write comments about core packages
kolmodin authored
169 ,"readline" --has ebuild, but only in the overlay
61f04b0 Update list of core libs
Duncan Coutts authored
170 ,"rts"
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
171 ,"template-haskell"
2f85f14 @kolmodin Write comments about core packages
kolmodin authored
172 ,"unix" --has ebuild, but only in the overlay
61f04b0 Update list of core libs
Duncan Coutts authored
173 ]
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
174
175 showEBuild :: EBuild -> String
176 showEBuild ebuild =
53c877c @kolmodin Change to correct ebuild header
kolmodin authored
177 ss "# Copyright 1999-2008 Gentoo Foundation". nl.
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
178 ss "# Distributed under the terms of the GNU General Public License v2". nl.
179 ss "# $Header: $". nl.
180 nl.
181 ss "CABAL_FEATURES=". quote' (sepBy " " $ features ebuild). nl.
182 ss "inherit haskell-cabal". nl.
183 nl.
20179c4 Set S= properly for packages with upper case names
Duncan Coutts authored
184 (case my_pn ebuild of
185 Nothing -> id
186 Just pn -> ss "MY_PN=". quote pn. nl.
187 ss "MY_P=". quote "${MY_PN}-${PV}". nl. nl).
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
188 ss "DESCRIPTION=". quote (description ebuild). nl.
189 ss "HOMEPAGE=". quote (homepage ebuild). nl.
8022ddc Adding support for variable substitution in generated ebuilds
der_eq@freenet.de authored
190 ss "SRC_URI=". quote (replaceVars (src_uri ebuild)).
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
191 (if null (src_uri ebuild) then ss "\t#Fixme: please fill in manually"
192 else id). nl.
5bcf377 Update hackport's ebuild generation to current conventions.
Duncan Coutts authored
193 nl.
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
194 ss "LICENSE=". quote (license ebuild).
195 (if null (licenseComments ebuild) then id
196 else ss "\t#". ss (licenseComments ebuild)). nl.
197 ss "SLOT=". quote (slot ebuild). nl.
63b57b8 Adapted HackPort to the new Hackage interface
der_eq@freenet.de authored
198 ss "KEYWORDS=". quote' (sepBy " " $ keywords ebuild).nl.
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
199 ss "IUSE=". quote' (sepBy ", " $ iuse ebuild). nl.
200 nl.
201 ss "DEPEND=". quote' (sepBy "\n\t\t" $ map showDepend $ depend ebuild). nl.
20179c4 Set S= properly for packages with upper case names
Duncan Coutts authored
202 (case my_pn ebuild of
203 Nothing -> id
87d875b @kolmodin Wall police
kolmodin authored
204 Just _ -> nl. ss "S=". quote ("${WORKDIR}/${MY_P}"). nl)
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
205 $ []
8022ddc Adding support for variable substitution in generated ebuilds
der_eq@freenet.de authored
206 where replaceVars = replaceCommonVars (name ebuild) (my_pn ebuild) (version ebuild)
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
207
87d875b @kolmodin Wall police
kolmodin authored
208 showDepend :: Dependency -> Package
209 showDepend (AnyVersionOf p) = p
210 showDepend (ThisVersionOf v p) = "~" ++ p ++ "-" ++ v
211 showDepend (LaterVersionOf v p) = ">" ++ p ++ "-" ++ v
212 showDepend (EarlierVersionOf v p) = "<" ++ p ++ "-" ++ v
213 showDepend (OrLaterVersionOf v p) = ">=" ++ p ++ "-" ++ v
214 showDepend (OrEarlierVersionOf v p) = "<=" ++ p ++ "-" ++ v
215 showDepend (DependEither dep1 dep2) = showDepend dep1
216 ++ " || " ++ showDepend dep2
217 showDepend (DependIfUse useflag dep@(DependEither _ _))
218 = useflag ++ "? " ++ showDepend dep
219 showDepend (DependIfUse useflag dep) = useflag ++ "? ( " ++ showDepend dep++ " )"
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
220
87d875b @kolmodin Wall police
kolmodin authored
221 ss :: String -> String -> String
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
222 ss = showString
87d875b @kolmodin Wall police
kolmodin authored
223
224 sc :: Char -> String -> String
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
225 sc = showChar
87d875b @kolmodin Wall police
kolmodin authored
226
227 nl :: String -> String
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
228 nl = sc '\n'
229
87d875b @kolmodin Wall police
kolmodin authored
230 quote :: String -> String -> String
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
231 quote str = sc '"'. ss str. sc '"'
87d875b @kolmodin Wall police
kolmodin authored
232
233 quote' :: (String -> String) -> String -> String
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
234 quote' str = sc '"'. str. sc '"'
235
236 sepBy :: String -> [String] -> ShowS
87d875b @kolmodin Wall police
kolmodin authored
237 sepBy _ [] = id
238 sepBy _ [x] = ss x
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
239 sepBy s (x:xs) = ss x. ss s. sepBy s xs
8022ddc Adding support for variable substitution in generated ebuilds
der_eq@freenet.de authored
240
241 getRestIfPrefix ::
242 String -> -- ^ the prefix
243 String -> -- ^ the string
244 Maybe String
245 getRestIfPrefix (p:ps) (x:xs) = if p==x then getRestIfPrefix ps xs else Nothing
246 getRestIfPrefix [] rest = Just rest
247 getRestIfPrefix _ [] = Nothing
248
249 subStr ::
250 String -> -- ^ the search string
251 String -> -- ^ the string to be searched
9448bb7 @kolmodin Rewritten overlayonly functionality
kolmodin authored
252 Maybe (String,String) -- ^ Just (pre,post) if string is found
8022ddc Adding support for variable substitution in generated ebuilds
der_eq@freenet.de authored
253 subStr sstr str = case getRestIfPrefix sstr str of
254 Nothing -> if null str then Nothing else case subStr sstr (tail str) of
255 Nothing -> Nothing
256 Just (pre,post) -> Just (head str:pre,post)
257 Just rest -> Just ([],rest)
258
259 replaceMultiVars ::
260 [(String,String)] -> -- ^ pairs of variable name and content
261 String -> -- ^ string to be searched
262 String -- ^ the result
263 replaceMultiVars [] str = str
87d875b @kolmodin Wall police
kolmodin authored
264 replaceMultiVars whole@((pname,cont):rest) str = case subStr cont str of
8022ddc Adding support for variable substitution in generated ebuilds
der_eq@freenet.de authored
265 Nothing -> replaceMultiVars rest str
87d875b @kolmodin Wall police
kolmodin authored
266 Just (pre,post) -> (replaceMultiVars rest pre)++pname++(replaceMultiVars whole post)
8022ddc Adding support for variable substitution in generated ebuilds
der_eq@freenet.de authored
267
268 replaceCommonVars ::
269 String -> -- ^ PN
270 Maybe String -> -- ^ MYPN
271 String -> -- ^ PV
272 String -> -- ^ the string to be replaced
273 String
274 replaceCommonVars pn mypn pv str
275 = replaceMultiVars
276 ([("${P}",pn++"-"++pv)]
277 ++ maybe [] (\x->[("${MY_P}",x++"-"++pv)]) mypn
278 ++[("${PN}",pn)]
279 ++ maybe [] (\x->[("${MY_PN}",x)]) mypn
280 ++[("${PV}",pv)]) str
Something went wrong with that request. Please try again.