Skip to content

HTTPS clone URL

Subversion checkout URL

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