@@ -58,7 +58,9 @@ module Hpack.Config (
5858, VerbatimValue (.. )
5959, verbatimValueToString
6060, CustomSetup (.. )
61+ , HasEmpty (.. )
6162, Section (.. )
63+ , maybeSectionAConditional
6264, Library (.. )
6365, Executable (.. )
6466, Conditional (.. )
@@ -527,6 +529,9 @@ instance Monoid Empty where
527529 mempty = Empty
528530 mappend = (<>)
529531
532+ instance HasEmpty Empty where
533+ empty = Empty
534+
530535instance Semigroup Empty where
531536 Empty <> Empty = Empty
532537
@@ -868,7 +873,7 @@ ensureRequiredCabalVersion inferredLicense pkg@Package{..} = pkg {
868873 executableHasGeneratedModules :: Section Executable -> Bool
869874 executableHasGeneratedModules = any (not . null . executableGeneratedModules)
870875
871- sectionCabalVersion :: (Section a -> [Module ]) -> Section a -> Maybe CabalVersion
876+ sectionCabalVersion :: (Eq a , HasEmpty a ) => ( Section a -> [Module ]) -> Section a -> Maybe CabalVersion
872877 sectionCabalVersion getMentionedModules sect = maximum $ [
873878 makeVersion [2 ,2 ] <$ guard (sectionSatisfies (not . null . sectionCxxSources) sect)
874879 , makeVersion [2 ,2 ] <$ guard (sectionSatisfies (not . null . sectionCxxOptions) sect)
@@ -880,6 +885,7 @@ ensureRequiredCabalVersion inferredLicense pkg@Package{..} = pkg {
880885 uses " RebindableSyntax"
881886 && (uses " OverloadedStrings" || uses " OverloadedLists" )
882887 && pathsModule `elem` getMentionedModules sect)
888+ , makeVersion [2 ,2 ] <$ guard (sectionSatisfies (isJust . maybeSectionAConditional) sect)
883889 ] ++ map versionFromSystemBuildTool systemBuildTools
884890 where
885891 defaultExtensions :: [String ]
@@ -1040,6 +1046,10 @@ data CustomSetup = CustomSetup {
10401046 customSetupDependencies :: Dependencies
10411047} deriving (Eq , Show )
10421048
1049+ -- | A class for types that have a value corresponding to being \'empty\'.
1050+ class HasEmpty a where
1051+ empty :: a
1052+
10431053data Library = Library {
10441054 libraryExposed :: Maybe Bool
10451055, libraryVisibility :: Maybe String
@@ -1050,12 +1060,30 @@ data Library = Library {
10501060, librarySignatures :: [String ]
10511061} deriving (Eq , Show )
10521062
1063+ instance HasEmpty Library where
1064+ empty = Library
1065+ { libraryExposed = Nothing
1066+ , libraryVisibility = Nothing
1067+ , libraryExposedModules = []
1068+ , libraryOtherModules = []
1069+ , libraryGeneratedModules = []
1070+ , libraryReexportedModules = []
1071+ , librarySignatures = []
1072+ }
1073+
10531074data Executable = Executable {
10541075 executableMain :: Maybe FilePath
10551076, executableOtherModules :: [Module ]
10561077, executableGeneratedModules :: [Module ]
10571078} deriving (Eq , Show )
10581079
1080+ instance HasEmpty Executable where
1081+ empty = Executable
1082+ { executableMain = Nothing
1083+ , executableOtherModules = []
1084+ , executableGeneratedModules = []
1085+ }
1086+
10591087data BuildTool = BuildTool String String | LocalBuildTool String
10601088 deriving (Show , Eq , Ord )
10611089
@@ -1093,6 +1121,41 @@ data Section a = Section {
10931121, sectionVerbatim :: [Verbatim ]
10941122} deriving (Eq , Show , Functor , Foldable , Traversable )
10951123
1124+ instance HasEmpty a => HasEmpty (Section a ) where
1125+ empty = Section
1126+ { sectionData = Hpack.Config. empty
1127+ , sectionSourceDirs = []
1128+ , sectionDependencies = mempty
1129+ , sectionPkgConfigDependencies = []
1130+ , sectionDefaultExtensions = []
1131+ , sectionOtherExtensions = []
1132+ , sectionLanguage = Nothing
1133+ , sectionGhcOptions = []
1134+ , sectionGhcProfOptions = []
1135+ , sectionGhcSharedOptions = []
1136+ , sectionGhcjsOptions = []
1137+ , sectionCppOptions = []
1138+ , sectionAsmOptions = []
1139+ , sectionAsmSources = []
1140+ , sectionCcOptions = []
1141+ , sectionCSources = []
1142+ , sectionCxxOptions = []
1143+ , sectionCxxSources = []
1144+ , sectionJsSources = []
1145+ , sectionExtraLibDirs = []
1146+ , sectionExtraLibraries = []
1147+ , sectionExtraFrameworksDirs = []
1148+ , sectionFrameworks = []
1149+ , sectionIncludeDirs = []
1150+ , sectionInstallIncludes = []
1151+ , sectionLdOptions = []
1152+ , sectionBuildable = Nothing
1153+ , sectionConditionals = []
1154+ , sectionBuildTools = mempty
1155+ , sectionSystemBuildTools = mempty
1156+ , sectionVerbatim = []
1157+ }
1158+
10961159data Conditional a = Conditional {
10971160 conditionalCondition :: Cond
10981161, conditionalThen :: a
@@ -1657,3 +1720,12 @@ pathsModuleFromPackageName name = Module ("Paths_" ++ map f name)
16571720 where
16581721 f ' -' = ' _'
16591722 f x = x
1723+
1724+ -- | If the given section contains only a single conditional, yields just that
1725+ -- conditional, otherwise 'Nothing'.
1726+ maybeSectionAConditional :: (Eq a , HasEmpty a ) => Section a -> Maybe (Conditional (Section a ))
1727+ maybeSectionAConditional s@ (Section { sectionConditionals = [c] }) =
1728+ if s == Hpack.Config. empty { sectionConditionals = sectionConditionals s }
1729+ then Just c
1730+ else Nothing
1731+ maybeSectionAConditional _ = Nothing
0 commit comments