Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 280 lines (254 sloc) 10.364 kb
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
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
e35e41e6 » kolmodin
2007-08-19 Make merging respect categories
25 (EBuild(..)
26 ,cabal2ebuild
27 ,showEBuild) where
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
28
29 import qualified Distribution.PackageDescription as Cabal
87d875b3 » kolmodin
2008-03-15 Wall police
30 (PackageDescription(..))
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
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
8022ddcf » der_eq@freenet.de
2005-09-21 Adding support for variable substitution in generated ebuilds
37 import Data.Char (toLower,isUpper)
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
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,
8022ddcf » der_eq@freenet.de
2005-09-21 Adding support for variable substitution in generated ebuilds
53 my_pn :: Maybe String --If the package's name contains upper-case
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
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
87d875b3 » kolmodin
2008-03-15 Wall police
68 ebuildTemplate :: EBuild
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
69 ebuildTemplate = EBuild {
70 name = "foobar",
71 version = "0.1",
72 description = "",
73 homepage = "",
74 src_uri = "",
75 license = "",
76 slot = "0",
63b57b89 » der_eq@freenet.de
2007-07-28 Adapted HackPort to the new Hackage interface
77 keywords = ["~amd64","~x86"],
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
78 iuse = [],
79 depend = [],
c81b0559 » Duncan Coutts
2007-07-30 profile and haddock only make sense for lib packages
80 features = [],
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
81 licenseComments = "",
8022ddcf » der_eq@freenet.de
2005-09-21 Adding support for variable substitution in generated ebuilds
82 my_pn = Nothing
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
83 }
84
85 cabal2ebuild :: Cabal.PackageDescription -> EBuild
86 cabal2ebuild pkg = ebuildTemplate {
8022ddcf » der_eq@freenet.de
2005-09-21 Adding support for variable substitution in generated ebuilds
87 name = map toLower cabalPkgName,
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
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
c6327c15 » Duncan Coutts
2008-01-14 Add possibly-versioned dependency on cabal
96 : convertDependency (Cabal.Dependency "Cabal"
97 (Cabal.descCabalVersion pkg))
98 ++ convertDependencies (Cabal.buildDepends pkg),
20179c4c » Duncan Coutts
2007-07-30 Set S= properly for packages with upper case names
99 my_pn = if any isUpper cabalPkgName then Just cabalPkgName else Nothing,
f5d6f88d » Duncan Coutts
2008-01-16 reorder the features list
100 features = features ebuildTemplate
101 ++ if null (Cabal.executables pkg) then [] else ["bin"]
102 ++ maybe [] (const ["lib","profile","haddock"]) (Cabal.library pkg)
8022ddcf » der_eq@freenet.de
2005-09-21 Adding support for variable substitution in generated ebuilds
103 } where
e35e41e6 » kolmodin
2007-08-19 Make merging respect categories
104 cabalPkgName = Cabal.pkgName (Cabal.package pkg)
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
105
87d875b3 » kolmodin
2008-03-15 Wall police
106 defaultDepGHC :: Dependency
47620999 » Duncan Coutts
2008-01-14 Increase default ghc dep to 6.6.1
107 defaultDepGHC = OrLaterVersionOf "6.6.1" "dev-lang/ghc"
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
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
87d875b3 » kolmodin
2008-03-15 Wall police
119 licenseComment :: Cabal.License -> String
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
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]
ace40ea5 » Duncan Coutts
2007-07-31 Cope with IntersectVersionRanges, at least partially
127 convertDependencies = concatMap convertDependency
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
128
ace40ea5 » Duncan Coutts
2007-07-31 Cope with IntersectVersionRanges, at least partially
129 convertDependency :: Cabal.Dependency -> [Dependency]
87d875b3 » kolmodin
2008-03-15 Wall police
130 convertDependency (Cabal.Dependency pname _)
131 | pname `elem` coreLibs = [] -- no explicit dep on core libs
132 convertDependency (Cabal.Dependency pname versionRange)
ace40ea5 » Duncan Coutts
2007-07-31 Cope with IntersectVersionRanges, at least partially
133 = case versionRange of
134 (Cabal.IntersectVersionRanges v1 v2) -> [convert v1, convert v2]
135 v -> [convert v]
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
136
137 where
87d875b3 » kolmodin
2008-03-15 Wall police
138 ebuildName = "dev-haskell/" ++ map toLower pname
9448bb73 » kolmodin
2007-08-13 Rewritten overlayonly functionality
139
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
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
87d875b3 » kolmodin
2008-03-15 Wall police
152 coreLibs :: [String]
5bcf377a » Duncan Coutts
2007-07-28 Update hackport's ebuild generation to current conventions.
153 coreLibs =
61f04b0b » Duncan Coutts
2008-01-14 Update list of core libs
154 ["array"
8022ddcf » der_eq@freenet.de
2005-09-21 Adding support for variable substitution in generated ebuilds
155 ,"base"
61f04b0b » Duncan Coutts
2008-01-14 Update list of core libs
156 --,"bytestring" --already has ebuild
157 ,"containers"
158 ,"directory"
159 --,"filepath" --already has ebuild
160 ,"ghc"
8022ddcf » der_eq@freenet.de
2005-09-21 Adding support for variable substitution in generated ebuilds
161 ,"haskell98"
61f04b0b » Duncan Coutts
2008-01-14 Update list of core libs
162 ,"hpc"
163 ,"old-locale"
164 ,"old-time"
165 ,"packedstring"
166 ,"pretty"
167 ,"process"
168 ,"random"
169 ,"readline"
170 ,"rts"
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
171 ,"template-haskell"
61f04b0b » Duncan Coutts
2008-01-14 Update list of core libs
172 ,"unix" --should have ebuild
173 ]
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
174
175 showEBuild :: EBuild -> String
176 showEBuild ebuild =
5bcf377a » Duncan Coutts
2007-07-28 Update hackport's ebuild generation to current conventions.
177 ss "# Copyright 1999-2007 Gentoo Foundation". nl.
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
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.
20179c4c » Duncan Coutts
2007-07-30 Set S= properly for packages with upper case names
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).
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
188 ss "DESCRIPTION=". quote (description ebuild). nl.
189 ss "HOMEPAGE=". quote (homepage ebuild). nl.
8022ddcf » der_eq@freenet.de
2005-09-21 Adding support for variable substitution in generated ebuilds
190 ss "SRC_URI=". quote (replaceVars (src_uri ebuild)).
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
191 (if null (src_uri ebuild) then ss "\t#Fixme: please fill in manually"
192 else id). nl.
5bcf377a » Duncan Coutts
2007-07-28 Update hackport's ebuild generation to current conventions.
193 nl.
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
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.
63b57b89 » der_eq@freenet.de
2007-07-28 Adapted HackPort to the new Hackage interface
198 ss "KEYWORDS=". quote' (sepBy " " $ keywords ebuild).nl.
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
199 ss "IUSE=". quote' (sepBy ", " $ iuse ebuild). nl.
200 nl.
201 ss "DEPEND=". quote' (sepBy "\n\t\t" $ map showDepend $ depend ebuild). nl.
20179c4c » Duncan Coutts
2007-07-30 Set S= properly for packages with upper case names
202 (case my_pn ebuild of
203 Nothing -> id
87d875b3 » kolmodin
2008-03-15 Wall police
204 Just _ -> nl. ss "S=". quote ("${WORKDIR}/${MY_P}"). nl)
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
205 $ []
8022ddcf » der_eq@freenet.de
2005-09-21 Adding support for variable substitution in generated ebuilds
206 where replaceVars = replaceCommonVars (name ebuild) (my_pn ebuild) (version ebuild)
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
207
87d875b3 » kolmodin
2008-03-15 Wall police
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++ " )"
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
220
87d875b3 » kolmodin
2008-03-15 Wall police
221 ss :: String -> String -> String
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
222 ss = showString
87d875b3 » kolmodin
2008-03-15 Wall police
223
224 sc :: Char -> String -> String
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
225 sc = showChar
87d875b3 » kolmodin
2008-03-15 Wall police
226
227 nl :: String -> String
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
228 nl = sc '\n'
229
87d875b3 » kolmodin
2008-03-15 Wall police
230 quote :: String -> String -> String
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
231 quote str = sc '"'. ss str. sc '"'
87d875b3 » kolmodin
2008-03-15 Wall police
232
233 quote' :: (String -> String) -> String -> String
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
234 quote' str = sc '"'. str. sc '"'
235
236 sepBy :: String -> [String] -> ShowS
87d875b3 » kolmodin
2008-03-15 Wall police
237 sepBy _ [] = id
238 sepBy _ [x] = ss x
aff37c25 » der_eq@freenet.de
2005-09-05 First release of HackPort, the Hackage tool for Portage
239 sepBy s (x:xs) = ss x. ss s. sepBy s xs
8022ddcf » der_eq@freenet.de
2005-09-21 Adding support for variable substitution in generated ebuilds
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
9448bb73 » kolmodin
2007-08-13 Rewritten overlayonly functionality
252 Maybe (String,String) -- ^ Just (pre,post) if string is found
8022ddcf » der_eq@freenet.de
2005-09-21 Adding support for variable substitution in generated ebuilds
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
87d875b3 » kolmodin
2008-03-15 Wall police
264 replaceMultiVars whole@((pname,cont):rest) str = case subStr cont str of
8022ddcf » der_eq@freenet.de
2005-09-21 Adding support for variable substitution in generated ebuilds
265 Nothing -> replaceMultiVars rest str
87d875b3 » kolmodin
2008-03-15 Wall police
266 Just (pre,post) -> (replaceMultiVars rest pre)++pname++(replaceMultiVars whole post)
8022ddcf » der_eq@freenet.de
2005-09-21 Adding support for variable substitution in generated ebuilds
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.