Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 271 lines (247 sloc) 10.324 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
30 (PackageDescription(..),
31 readPackageDescription)
32 import qualified Distribution.Package as Cabal (PackageIdentifier(..))
33 import qualified Distribution.Version as Cabal (showVersion, Dependency(..),
34 VersionRange(..))
35 import qualified Distribution.License as Cabal (License(..))
36 --import qualified Distribution.Compiler as Cabal (CompilerFlavor(..))
37
8022ddc Adding support for variable substitution in generated ebuilds
der_eq@freenet.de authored
38 import Data.Char (toLower,isUpper)
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
39 import Data.Maybe (catMaybes)
8022ddc Adding support for variable substitution in generated ebuilds
der_eq@freenet.de authored
40 import Text.Regex
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
41
42 data EBuild = EBuild {
43 name :: String,
44 version :: String,
45 description :: String,
46 homepage :: String,
47 src_uri :: String,
48 license :: String,
49 slot :: String,
50 keywords :: [String],
51 iuse :: [String],
52 depend :: [Dependency],
53 features :: [String],
54 -- comments on various fields for communicating stuff to the user
55 licenseComments :: String,
8022ddc Adding support for variable substitution in generated ebuilds
der_eq@freenet.de authored
56 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
57 }
58
59 type Package = String
60 type Version = String
61 type UseFlag = String
62 data Dependency = AnyVersionOf Package
63 | ThisVersionOf Version Package -- =package-version
64 | LaterVersionOf Version Package -- >package-version
65 | EarlierVersionOf Version Package -- <package-version
66 | OrLaterVersionOf Version Package -- >=package-version
67 | OrEarlierVersionOf Version Package -- <=package-version
68 | DependEither Dependency Dependency -- depend || depend
69 | DependIfUse UseFlag Dependency -- use? ( depend )
70
71 ebuildTemplate = EBuild {
72 name = "foobar",
73 version = "0.1",
74 description = "",
75 homepage = "",
76 src_uri = "",
77 license = "",
78 slot = "0",
63b57b8 Adapted HackPort to the new Hackage interface
der_eq@freenet.de authored
79 keywords = ["~amd64","~x86"],
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
80 iuse = [],
81 depend = [],
c81b055 profile and haddock only make sense for lib packages
Duncan Coutts authored
82 features = [],
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
83 licenseComments = "",
8022ddc Adding support for variable substitution in generated ebuilds
der_eq@freenet.de authored
84 my_pn = Nothing
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
85 }
86
87 cabal2ebuild :: Cabal.PackageDescription -> EBuild
88 cabal2ebuild pkg = ebuildTemplate {
8022ddc Adding support for variable substitution in generated ebuilds
der_eq@freenet.de authored
89 name = map toLower cabalPkgName,
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
90 version = Cabal.showVersion (Cabal.pkgVersion (Cabal.package pkg)),
91 description = if null (Cabal.synopsis pkg) then Cabal.description pkg
92 else Cabal.synopsis pkg,
93 homepage = Cabal.homepage pkg,
94 src_uri = Cabal.pkgUrl pkg,
95 license = convertLicense (Cabal.license pkg),
96 licenseComments = licenseComment (Cabal.license pkg),
97 depend = defaultDepGHC
c6327c1 Add possibly-versioned dependency on cabal
Duncan Coutts authored
98 : convertDependency (Cabal.Dependency "Cabal"
99 (Cabal.descCabalVersion pkg))
100 ++ convertDependencies (Cabal.buildDepends pkg),
20179c4 Set S= properly for packages with upper case names
Duncan Coutts authored
101 my_pn = if any isUpper cabalPkgName then Just cabalPkgName else Nothing,
f5d6f88 reorder the features list
Duncan Coutts authored
102 features = features ebuildTemplate
103 ++ if null (Cabal.executables pkg) then [] else ["bin"]
104 ++ maybe [] (const ["lib","profile","haddock"]) (Cabal.library pkg)
8022ddc Adding support for variable substitution in generated ebuilds
der_eq@freenet.de authored
105 } where
e35e41e @kolmodin Make merging respect categories
kolmodin authored
106 cabalPkgName = Cabal.pkgName (Cabal.package pkg)
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
107
4762099 Increase default ghc dep to 6.6.1
Duncan Coutts authored
108 defaultDepGHC = OrLaterVersionOf "6.6.1" "dev-lang/ghc"
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
109
110 -- map the cabal license type to the gentoo license string format
111 convertLicense :: Cabal.License -> String
112 convertLicense Cabal.GPL = "GPL-2" -- almost certainly version 2
113 convertLicense Cabal.LGPL = "LGPL-2.1" -- probably version 2.1
114 convertLicense Cabal.BSD3 = "BSD" -- do we really not
115 convertLicense Cabal.BSD4 = "BSD" -- distinguish between these?
116 convertLicense Cabal.PublicDomain = "public-domain"
117 convertLicense Cabal.AllRightsReserved = ""
118 convertLicense _ = ""
119
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]
130 convertDependency (Cabal.Dependency name _)
131 | name `elem` coreLibs = [] -- no explicit dep on core libs
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
132 convertDependency (Cabal.Dependency name 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
138 ebuildName = "dev-haskell/" ++ map toLower name
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
5bcf377 Update hackport's ebuild generation to current conventions.
Duncan Coutts authored
152 coreLibs =
61f04b0 Update list of core libs
Duncan Coutts authored
153 ["array"
8022ddc Adding support for variable substitution in generated ebuilds
der_eq@freenet.de authored
154 ,"base"
61f04b0 Update list of core libs
Duncan Coutts authored
155 --,"bytestring" --already has ebuild
156 ,"containers"
157 ,"directory"
158 --,"filepath" --already has ebuild
159 ,"ghc"
8022ddc Adding support for variable substitution in generated ebuilds
der_eq@freenet.de authored
160 ,"haskell98"
61f04b0 Update list of core libs
Duncan Coutts authored
161 ,"hpc"
162 ,"old-locale"
163 ,"old-time"
164 ,"packedstring"
165 ,"pretty"
166 ,"process"
167 ,"random"
168 ,"readline"
169 ,"rts"
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
170 ,"template-haskell"
61f04b0 Update list of core libs
Duncan Coutts authored
171 ,"unix" --should have ebuild
172 ]
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
173
174 showEBuild :: EBuild -> String
175 showEBuild ebuild =
5bcf377 Update hackport's ebuild generation to current conventions.
Duncan Coutts authored
176 ss "# Copyright 1999-2007 Gentoo Foundation". nl.
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
177 ss "# Distributed under the terms of the GNU General Public License v2". nl.
178 ss "# $Header: $". nl.
179 nl.
180 ss "CABAL_FEATURES=". quote' (sepBy " " $ features ebuild). nl.
181 ss "inherit haskell-cabal". nl.
182 nl.
20179c4 Set S= properly for packages with upper case names
Duncan Coutts authored
183 (case my_pn ebuild of
184 Nothing -> id
185 Just pn -> ss "MY_PN=". quote pn. nl.
186 ss "MY_P=". quote "${MY_PN}-${PV}". nl. nl).
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
187 ss "DESCRIPTION=". quote (description ebuild). nl.
188 ss "HOMEPAGE=". quote (homepage ebuild). nl.
8022ddc Adding support for variable substitution in generated ebuilds
der_eq@freenet.de authored
189 ss "SRC_URI=". quote (replaceVars (src_uri ebuild)).
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
190 (if null (src_uri ebuild) then ss "\t#Fixme: please fill in manually"
191 else id). nl.
5bcf377 Update hackport's ebuild generation to current conventions.
Duncan Coutts authored
192 nl.
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
193 ss "LICENSE=". quote (license ebuild).
194 (if null (licenseComments ebuild) then id
195 else ss "\t#". ss (licenseComments ebuild)). nl.
196 ss "SLOT=". quote (slot ebuild). nl.
63b57b8 Adapted HackPort to the new Hackage interface
der_eq@freenet.de authored
197 ss "KEYWORDS=". quote' (sepBy " " $ keywords ebuild).nl.
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
198 ss "IUSE=". quote' (sepBy ", " $ iuse ebuild). nl.
199 nl.
200 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
201 (case my_pn ebuild of
202 Nothing -> id
2d0448c Oops, fix last nl patch.
Duncan Coutts authored
203 Just pn -> nl. ss "S=". quote ("${WORKDIR}/${MY_P}"). nl)
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
204 $ []
8022ddc Adding support for variable substitution in generated ebuilds
der_eq@freenet.de authored
205 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
206
207 showDepend (AnyVersionOf package) = package
3754021 == cabal deps correspond to ~ portage deps
Duncan Coutts authored
208 showDepend (ThisVersionOf version package) = "~" ++ package ++ "-" ++ version
aff37c2 First release of HackPort, the Hackage tool for Portage
der_eq@freenet.de authored
209 showDepend (LaterVersionOf version package) = ">" ++ package ++ "-" ++ version
210 showDepend (EarlierVersionOf version package) = "<" ++ package ++ "-" ++ version
211 showDepend (OrLaterVersionOf version package) = ">=" ++ package ++ "-" ++ version
212 showDepend (OrEarlierVersionOf version package) = "<=" ++ package ++ "-" ++ version
213 showDepend (DependEither depend1 depend2) = showDepend depend1
214 ++ " || " ++ showDepend depend2
215 showDepend (DependIfUse useflag depend@(DependEither _ _))
216 = useflag ++ "? " ++ showDepend depend
217 showDepend (DependIfUse useflag depend) = useflag ++ "? ( " ++ showDepend depend ++ " )"
218
219 ss = showString
220 sc = showChar
221 nl = sc '\n'
222
223 quote str = sc '"'. ss str. sc '"'
224 quote' str = sc '"'. str. sc '"'
225
226 sepBy :: String -> [String] -> ShowS
227 sepBy s [] = id
228 sepBy s [x] = ss x
229 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
230
231 getRestIfPrefix ::
232 String -> -- ^ the prefix
233 String -> -- ^ the string
234 Maybe String
235 getRestIfPrefix (p:ps) (x:xs) = if p==x then getRestIfPrefix ps xs else Nothing
236 getRestIfPrefix [] rest = Just rest
237 getRestIfPrefix _ [] = Nothing
238
239 subStr ::
240 String -> -- ^ the search string
241 String -> -- ^ the string to be searched
9448bb7 @kolmodin Rewritten overlayonly functionality
kolmodin authored
242 Maybe (String,String) -- ^ Just (pre,post) if string is found
8022ddc Adding support for variable substitution in generated ebuilds
der_eq@freenet.de authored
243 subStr sstr str = case getRestIfPrefix sstr str of
244 Nothing -> if null str then Nothing else case subStr sstr (tail str) of
245 Nothing -> Nothing
246 Just (pre,post) -> Just (head str:pre,post)
247 Just rest -> Just ([],rest)
248
249 replaceMultiVars ::
250 [(String,String)] -> -- ^ pairs of variable name and content
251 String -> -- ^ string to be searched
252 String -- ^ the result
253 replaceMultiVars [] str = str
254 replaceMultiVars whole@((name,cont):rest) str = case subStr cont str of
255 Nothing -> replaceMultiVars rest str
256 Just (pre,post) -> (replaceMultiVars rest pre)++name++(replaceMultiVars whole post)
257
258 replaceCommonVars ::
259 String -> -- ^ PN
260 Maybe String -> -- ^ MYPN
261 String -> -- ^ PV
262 String -> -- ^ the string to be replaced
263 String
264 replaceCommonVars pn mypn pv str
265 = replaceMultiVars
266 ([("${P}",pn++"-"++pv)]
267 ++ maybe [] (\x->[("${MY_P}",x++"-"++pv)]) mypn
268 ++[("${PN}",pn)]
269 ++ maybe [] (\x->[("${MY_PN}",x)]) mypn
270 ++[("${PV}",pv)]) str
Something went wrong with that request. Please try again.