Skip to content

Commit 2532243

Browse files
committed
fixup register SDPX with GHC >= 8.4 [ci skip]
1 parent da08ae6 commit 2532243

File tree

7 files changed

+62
-9
lines changed

7 files changed

+62
-9
lines changed

Cabal/Distribution/License.hs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ module Distribution.License (
4646
License(..),
4747
knownLicenses,
4848
licenseToSPDX,
49+
licenseFromSPDX,
4950
) where
5051

5152
import Distribution.Compat.Prelude
@@ -57,6 +58,7 @@ import Distribution.Text
5758
import Distribution.Version
5859

5960
import qualified Distribution.Compat.CharParsing as P
61+
import qualified Distribution.Compat.Map.Strict as Map
6062
import qualified Distribution.Compat.ReadP as Parse
6163
import qualified Distribution.SPDX as SPDX
6264
import qualified Text.PrettyPrint as Disp
@@ -142,7 +144,7 @@ knownLicenses = [ GPL unversioned, GPL (version [2]), GPL (version [3])
142144
unversioned = Nothing
143145
version = Just . mkVersion
144146

145-
-- | Convert old 'License' to SPDX 'SPDX.LicenseExpression'.
147+
-- | Convert old 'License' to SPDX 'SPDX.License'.
146148
-- Non-SPDX licenses are converted to 'SPDX.LicenseRef'.
147149
--
148150
-- @since 2.2.0.0
@@ -171,6 +173,47 @@ licenseToSPDX l = case l of
171173
spdx = SPDX.License . SPDX.simpleLicenseExpression
172174
ref r = SPDX.License $ SPDX.ELicense (SPDX.ELicenseRef r) Nothing
173175

176+
-- | Convert 'SPDX.License' to 'License',
177+
--
178+
-- This is lossy conversion. We try our best.
179+
--
180+
-- >>> licenseFromSPDX . licenseToSPDX $ BSD3
181+
-- BSD3
182+
--
183+
-- >>> licenseFromSPDX . licenseToSPDX $ GPL (Just (mkVersion [3]))
184+
-- GPL (Just (mkVersion [3]))
185+
--
186+
-- >>> licenseFromSPDX . licenseToSPDX $ PublicDomain
187+
-- UnknownLicense "LicenseRefPublicDomain"
188+
--
189+
-- >>> licenseFromSPDX $ SPDX.License $ SPDX.simpleLicenseExpression SPDX.EUPL_1_1
190+
-- UnknownLicense "EUPL-1.1"
191+
--
192+
-- >>> licenseFromSPDX . licenseToSPDX $ AllRightsReserved
193+
-- AllRightsReserved
194+
--
195+
-- >>> licenseFromSPDX <$> simpleParsec "BSD-3-Clause OR GPL-3.0"
196+
-- Just (UnknownLicense "BSD3ClauseORGPL30")
197+
--
198+
-- @since 2.2.0.0
199+
licenseFromSPDX :: SPDX.License -> License
200+
licenseFromSPDX SPDX.NONE = AllRightsReserved
201+
licenseFromSPDX l =
202+
fromMaybe (mungle $ prettyShow l) $ Map.lookup l m
203+
where
204+
m :: Map.Map SPDX.License License
205+
m = Map.fromList $ filter (isSimple . fst ) $
206+
map (\x -> (licenseToSPDX x, x)) knownLicenses
207+
208+
isSimple (SPDX.License (SPDX.ELicense (SPDX.ELicenseId _) Nothing)) = True
209+
isSimple _ = False
210+
211+
mungle name = fromMaybe (UnknownLicense (mapMaybe mangle name)) (simpleParsec name)
212+
213+
mangle c
214+
| isAlphaNum c = Just c
215+
| otherwise = Nothing
216+
174217
instance Pretty License where
175218
pretty (GPL version) = Disp.text "GPL" <<>> dispOptVersion version
176219
pretty (LGPL version) = Disp.text "LGPL" <<>> dispOptVersion version

Cabal/Distribution/SPDX/License.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ data License
4141
-- ^ if the package contains no license information whatsoever; or
4242
| License LicenseExpression
4343
-- ^ A valid SPDX License Expression as defined in Appendix IV.
44-
deriving (Show, Read, Eq, Typeable, Data, Generic)
44+
deriving (Show, Read, Eq, Ord, Typeable, Data, Generic)
4545

4646
instance Binary License
4747

Cabal/Distribution/SPDX/LicenseExpression.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ data LicenseExpression
4242
= ELicense !SimpleLicenseExpression !(Maybe LicenseExceptionId)
4343
| EAnd !LicenseExpression !LicenseExpression
4444
| EOr !LicenseExpression !LicenseExpression
45-
deriving (Show, Read, Eq, Typeable, Data, Generic)
45+
deriving (Show, Read, Eq, Ord, Typeable, Data, Generic)
4646

4747
-- | Simple License Expressions.
4848
data SimpleLicenseExpression
@@ -52,7 +52,7 @@ data SimpleLicenseExpression
5252
-- ^ An SPDX License List Short Form Identifier with a unary"+" operator suffix to represent the current version of the license or any later version. For example: @GPL-2.0+@
5353
| ELicenseRef LicenseRef
5454
-- ^ A SPDX user defined license reference: For example: @LicenseRef-23@, @LicenseRef-MIT-Style-1@, or @DocumentRef-spdx-tool-1.2:LicenseRef-MIT-Style-2@
55-
deriving (Show, Read, Eq, Typeable, Data, Generic)
55+
deriving (Show, Read, Eq, Ord, Typeable, Data, Generic)
5656

5757
simpleLicenseExpression :: LicenseId -> LicenseExpression
5858
simpleLicenseExpression i = ELicense (ELicenseId i) Nothing

Cabal/Distribution/SPDX/LicenseReference.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ data LicenseRef = LicenseRef
2323
{ _lrDocument :: !(Maybe String)
2424
, _lrLicense :: !String
2525
}
26-
deriving (Show, Read, Eq, Typeable, Data, Generic)
26+
deriving (Show, Read, Eq, Ord, Typeable, Data, Generic)
2727

2828
-- | License reference.
2929
licenseRef :: LicenseRef -> String

Cabal/Distribution/Simple/Register.hs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ import qualified Distribution.Simple.Program.HcPkg as HcPkg
7474
import Distribution.Simple.Setup
7575
import Distribution.PackageDescription
7676
import Distribution.Package
77+
import Distribution.License (licenseToSPDX, licenseFromSPDX)
7778
import qualified Distribution.InstalledPackageInfo as IPI
7879
import Distribution.InstalledPackageInfo (InstalledPackageInfo)
7980
import Distribution.Simple.Utils
@@ -407,7 +408,11 @@ generalInstalledPackageInfo adjustRelIncDirs pkg abi_hash lib lbi clbi installDi
407408
IPI.instantiatedWith = componentInstantiatedWith clbi,
408409
IPI.sourceLibName = libName lib,
409410
IPI.compatPackageKey = componentCompatPackageKey clbi,
410-
IPI.license = licenseRaw pkg,
411+
-- If GHC >= 8.4 we register with SDPX, otherwise with legacy license
412+
IPI.license =
413+
if ghc84
414+
then Left $ either id licenseToSPDX $ licenseRaw pkg
415+
else Right $ either licenseFromSPDX id $ licenseRaw pkg,
411416
IPI.copyright = copyright pkg,
412417
IPI.maintainer = maintainer pkg,
413418
IPI.author = author pkg,
@@ -450,6 +455,10 @@ generalInstalledPackageInfo adjustRelIncDirs pkg abi_hash lib lbi clbi installDi
450455
IPI.pkgRoot = Nothing
451456
}
452457
where
458+
ghc84 = case compilerId $ compiler lbi of
459+
CompilerId GHC v -> v >= mkVersion [8, 4]
460+
_ -> False
461+
453462
bi = libBuildInfo lib
454463
--TODO: unclear what the root cause of the
455464
-- duplication is, but we nub it here for now:
Binary file not shown.
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Test.Cabal.Prelude
22
main = setupAndCabalTest $ withPackageDb $ do
33
setup_install []
4-
-- TODO: fix, should contain BSD-3-Clause
5-
ghcPkg' "field" ["my", "license"] >>= assertOutputContains "BSD-3"
6-
ghcPkg' "describe" ["my"] >>= assertOutputContains "BSD-3"
4+
recordMode DoNotRecord $ do
5+
ghc84 <- ghcVersionIs (>= mkVersion [8,4])
6+
let lic = if ghc84 then "BSD-3-Clause" else "BSD3"
7+
ghcPkg' "field" ["my", "license"] >>= assertOutputContains lic

0 commit comments

Comments
 (0)