Skip to content

Commit bf83d33

Browse files
authored
Merge pull request #4958
Improve `build-type` defaulting (see 9fb03d7 and #4958 for details)
2 parents b7255d3 + 37230a6 commit bf83d33

File tree

22 files changed

+106
-41
lines changed

22 files changed

+106
-41
lines changed

Cabal/Distribution/PackageDescription.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module Distribution.PackageDescription (
1818
PackageDescription(..),
1919
emptyPackageDescription,
2020
specVersion,
21+
buildType,
2122
descCabalVersion,
2223
BuildType(..),
2324
knownBuildTypes,

Cabal/Distribution/PackageDescription/Check.hs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -448,20 +448,20 @@ checkFields pkg =
448448
"Package names with the prefix 'z-' are reserved by Cabal and "
449449
++ "cannot be used."
450450

451-
, check (isNothing (buildType pkg)) $
451+
, check (isNothing (buildTypeRaw pkg) && specVersion pkg < mkVersion [2,1]) $
452452
PackageBuildWarning $
453453
"No 'build-type' specified. If you do not need a custom Setup.hs or "
454454
++ "./configure script then use 'build-type: Simple'."
455455

456456
, case buildType pkg of
457-
Just (UnknownBuildType unknown) -> Just $
457+
UnknownBuildType unknown -> Just $
458458
PackageBuildWarning $
459459
quote unknown ++ " is not a known 'build-type'. "
460460
++ "The known build types are: "
461461
++ commaSep (map display knownBuildTypes)
462462
_ -> Nothing
463463

464-
, check (isJust (setupBuildInfo pkg) && buildType pkg /= Just Custom) $
464+
, check (isJust (setupBuildInfo pkg) && buildType pkg /= Custom) $
465465
PackageBuildWarning $
466466
"Ignoring the 'custom-setup' section because the 'build-type' is "
467467
++ "not 'Custom'. Use 'build-type: Custom' if you need to use a "
@@ -1283,7 +1283,7 @@ checkCabalVersion pkg =
12831283

12841284
, check (specVersion pkg >= mkVersion [1,23]
12851285
&& isNothing (setupBuildInfo pkg)
1286-
&& buildType pkg == Just Custom) $
1286+
&& buildType pkg == Custom) $
12871287
PackageBuildWarning $
12881288
"Packages using 'cabal-version: >= 1.23' with 'build-type: Custom' "
12891289
++ "must use a 'custom-setup' section with a 'setup-depends' field "
@@ -1293,7 +1293,7 @@ checkCabalVersion pkg =
12931293

12941294
, check (specVersion pkg < mkVersion [1,23]
12951295
&& isNothing (setupBuildInfo pkg)
1296-
&& buildType pkg == Just Custom) $
1296+
&& buildType pkg == Custom) $
12971297
PackageDistSuspiciousWarn $
12981298
"From version 1.23 cabal supports specifiying explicit dependencies "
12991299
++ "for Custom setup scripts. Consider using cabal-version >= 1.23 and "
@@ -1903,7 +1903,7 @@ checkSetupExists :: Monad m => CheckPackageContentOps m
19031903
-> PackageDescription
19041904
-> m (Maybe PackageCheck)
19051905
checkSetupExists ops pkg = do
1906-
let simpleBuild = buildType pkg == Just Simple
1906+
let simpleBuild = buildType pkg == Simple
19071907
hsexists <- doesFileExist ops "Setup.hs"
19081908
lhsexists <- doesFileExist ops "Setup.lhs"
19091909
return $ check (not simpleBuild && not hsexists && not lhsexists) $
@@ -1913,13 +1913,14 @@ checkSetupExists ops pkg = do
19131913
checkConfigureExists :: Monad m => CheckPackageContentOps m
19141914
-> PackageDescription
19151915
-> m (Maybe PackageCheck)
1916-
checkConfigureExists ops PackageDescription { buildType = Just Configure } = do
1917-
exists <- doesFileExist ops "configure"
1918-
return $ check (not exists) $
1919-
PackageBuildWarning $
1920-
"The 'build-type' is 'Configure' but there is no 'configure' script. "
1921-
++ "You probably need to run 'autoreconf -i' to generate it."
1922-
checkConfigureExists _ _ = return Nothing
1916+
checkConfigureExists ops pd
1917+
| buildType pd == Configure = do
1918+
exists <- doesFileExist ops "configure"
1919+
return $ check (not exists) $
1920+
PackageBuildWarning $
1921+
"The 'build-type' is 'Configure' but there is no 'configure' script. "
1922+
++ "You probably need to run 'autoreconf -i' to generate it."
1923+
| otherwise = return Nothing
19231924

19241925
checkLocalPathsExist :: Monad m => CheckPackageContentOps m
19251926
-> PackageDescription

Cabal/Distribution/PackageDescription/FieldGrammar.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ packageDescriptionFieldGrammar = PackageDescription
8686
<*> prefixedFields "x-" L.customFieldsPD
8787
<*> pure [] -- build-depends
8888
<*> optionalFieldDefAla "cabal-version" SpecVersion L.specVersionRaw (Right anyVersion)
89-
<*> optionalField "build-type" L.buildType
89+
<*> optionalField "build-type" L.buildTypeRaw
9090
<*> pure Nothing -- custom-setup
9191
-- components
9292
<*> pure Nothing -- lib

Cabal/Distribution/PackageDescription/Parse.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pkgDescrFieldDescrs =
9797
specVersionRaw (\v pkg -> pkg{specVersionRaw=v})
9898
, simpleField "build-type"
9999
(maybe mempty disp) (fmap Just parse)
100-
buildType (\t pkg -> pkg{buildType=t})
100+
buildTypeRaw (\t pkg -> pkg{buildTypeRaw=t})
101101
, simpleField "license"
102102
disp parseLicenseQ
103103
license (\l pkg -> pkg{license=l})

Cabal/Distribution/Types/PackageDescription.hs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module Distribution.Types.PackageDescription (
3030
PackageDescription(..),
3131
specVersion,
3232
descCabalVersion,
33+
buildType,
3334
emptyPackageDescription,
3435
hasPublicLib,
3536
hasLibs,
@@ -128,7 +129,11 @@ data PackageDescription
128129
-- only ranges of the form @>= v@ make sense. We are in the process of
129130
-- transitioning to specifying just a single version, not a range.
130131
specVersionRaw :: Either Version VersionRange,
131-
buildType :: Maybe BuildType,
132+
-- | The original @build-type@ value as parsed from the
133+
-- @.cabal@ file without defaulting. See also 'buildType'.
134+
--
135+
-- @since 2.2
136+
buildTypeRaw :: Maybe BuildType,
132137
setupBuildInfo :: Maybe SetupBuildInfo,
133138
-- components
134139
library :: Maybe Library,
@@ -177,6 +182,31 @@ descCabalVersion pkg = case specVersionRaw pkg of
177182
Right versionRange -> versionRange
178183
{-# DEPRECATED descCabalVersion "Use specVersion instead" #-}
179184

185+
-- | The effective @build-type@ after applying defaulting rules.
186+
--
187+
-- The original @build-type@ value parsed is stored in the
188+
-- 'buildTypeRaw' field. However, the @build-type@ field is optional
189+
-- and can therefore be empty in which case we need to compute the
190+
-- /effective/ @build-type@. This function implements the following
191+
-- defaulting rules:
192+
--
193+
-- * For @cabal-version:2.0@ and below, default to the @Custom@
194+
-- build-type unconditionally.
195+
--
196+
-- * Otherwise, if a @custom-setup@ stanza is defined, default to
197+
-- the @Custom@ build-type; else default to @Simple@ build-type.
198+
--
199+
-- @since 2.2
200+
buildType :: PackageDescription -> BuildType
201+
buildType pkg
202+
| specVersion pkg >= mkVersion [2,1]
203+
= fromMaybe newDefault (buildTypeRaw pkg)
204+
| otherwise -- cabal-version < 2.1
205+
= fromMaybe Custom (buildTypeRaw pkg)
206+
where
207+
newDefault | isNothing (setupBuildInfo pkg) = Simple
208+
| otherwise = Custom
209+
180210
emptyPackageDescription :: PackageDescription
181211
emptyPackageDescription
182212
= PackageDescription {
@@ -185,7 +215,7 @@ emptyPackageDescription
185215
license = UnspecifiedLicense,
186216
licenseFiles = [],
187217
specVersionRaw = Right anyVersion,
188-
buildType = Nothing,
218+
buildTypeRaw = Nothing,
189219
copyright = "",
190220
maintainer = "",
191221
author = "",

Cabal/Distribution/Types/PackageDescription/Lens.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ specVersionRaw :: Lens' PackageDescription (Either Version VersionRange)
9696
specVersionRaw f s = fmap (\x -> s { T.specVersionRaw = x }) (f (T.specVersionRaw s))
9797
{-# INLINE specVersionRaw #-}
9898

99-
buildType :: Lens' PackageDescription (Maybe BuildType)
100-
buildType f s = fmap (\x -> s { T.buildType = x }) (f (T.buildType s))
101-
{-# INLINE buildType #-}
99+
buildTypeRaw :: Lens' PackageDescription (Maybe BuildType)
100+
buildTypeRaw f s = fmap (\x -> s { T.buildTypeRaw = x }) (f (T.buildTypeRaw s))
101+
{-# INLINE buildTypeRaw #-}
102102

103103
setupBuildInfo :: Lens' PackageDescription (Maybe SetupBuildInfo)
104104
setupBuildInfo f s = fmap (\x -> s { T.setupBuildInfo = x }) (f (T.setupBuildInfo s))

Cabal/changelog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
* Compilation with section splitting is now supported via the
3131
'--enable-split-sections' flag (#4819)
3232
* Support for common stanzas (#4751)
33+
* Use better defaulting for `build-type`; rename `PackageDescription`'s
34+
`buildType` field to `buildTypeRaw` and introduce new `buildType`
35+
function (#4958)
3336
* TODO
3437

3538
2.0.1.1 Mikhail Glushenkov <[email protected]> December 2017

Cabal/doc/developing-packages.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,12 +798,20 @@ describe the package as a whole:
798798

799799
.. pkg-field:: build-type: identifier
800800

801-
:default: ``Custom``
801+
:default: ``Custom`` or ``Simple``
802802

803803
The type of build used by this package. Build types are the
804804
constructors of the
805805
`BuildType <../release/cabal-latest/doc/API/Cabal/Distribution-PackageDescription.html#t:BuildType>`__
806-
type, defaulting to ``Custom``.
806+
type. This field is optional and when missing, its default value
807+
is inferred according to the following rules:
808+
809+
- When :pkg-field:`cabal-version` is set to ``2.1`` or higher,
810+
the default is ``Simple`` unless a :pkg-section:`custom-setup`
811+
exists, in which case the inferred default is ``Custom``.
812+
813+
- For lower :pkg-field:`cabal-version` values, the default is
814+
``Custom`` unconditionally.
807815

808816
If the build type is anything other than ``Custom``, then the
809817
``Setup.hs`` file *must* be exactly the standardized content

cabal-install/Distribution/Client/Dependency.hs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -525,16 +525,18 @@ addDefaultSetupDependencies defaultSetupDeps params =
525525
PD.setupBuildInfo =
526526
case PD.setupBuildInfo pkgdesc of
527527
Just sbi -> Just sbi
528-
Nothing -> case defaultSetupDeps srcpkg of
528+
Nothing -> case defaultSetupDeps srcpkg of
529529
Nothing -> Nothing
530-
Just deps -> Just PD.SetupBuildInfo {
531-
PD.defaultSetupDepends = True,
532-
PD.setupDepends = deps
533-
}
530+
Just deps | isCustom -> Just PD.SetupBuildInfo {
531+
PD.defaultSetupDepends = True,
532+
PD.setupDepends = deps
533+
}
534+
| otherwise -> Nothing
534535
}
535536
}
536537
}
537538
where
539+
isCustom = PD.buildType pkgdesc == PD.Custom
538540
gpkgdesc = packageDescription srcpkg
539541
pkgdesc = PD.packageDescription gpkgdesc
540542

@@ -619,7 +621,7 @@ standardInstallPolicy installedPkgIndex sourcePkgDb pkgSpecifiers
619621
where
620622
gpkgdesc = packageDescription srcpkg
621623
pkgdesc = PD.packageDescription gpkgdesc
622-
bt = fromMaybe PD.Custom (PD.buildType pkgdesc)
624+
bt = PD.buildType pkgdesc
623625
affected = bt == PD.Custom && hasBuildableFalse gpkgdesc
624626

625627
-- Does this package contain any components with non-empty 'build-depends'

cabal-install/Distribution/Client/ProjectBuilding.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1139,7 +1139,7 @@ buildInplaceUnpackedPackage verbosity
11391139
ifNullThen m m' = do xs <- m
11401140
if null xs then m' else return xs
11411141
monitors <- case PD.buildType (elabPkgDescription pkg) of
1142-
Just Simple -> listSimple
1142+
Simple -> listSimple
11431143
-- If a Custom setup was used, AND the Cabal is recent
11441144
-- enough to have sdist --list-sources, use that to
11451145
-- determine the files that we need to track. This can

cabal-install/Distribution/Client/ProjectPlanning.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,9 +1225,8 @@ elaborateInstallPlan verbosity platform compiler compilerprogdb pkgConfigDB
12251225
-- Once you've implemented this, swap it for the code below.
12261226
cuz_custom =
12271227
case PD.buildType (elabPkgDescription elab0) of
1228-
Nothing -> cuz "build-type is not specified"
1229-
Just PD.Custom -> cuz "build-type is Custom"
1230-
Just _ -> []
1228+
PD.Custom -> cuz "build-type is Custom"
1229+
_ -> []
12311230
-- cabal-format versions prior to 1.8 have different build-depends semantics
12321231
-- for now it's easier to just fallback to legacy-mode when specVersion < 1.8
12331232
-- see, https://github.com/haskell/cabal/issues/4121
@@ -1271,7 +1270,7 @@ elaborateInstallPlan verbosity platform compiler compilerprogdb pkgConfigDB
12711270
-- have to add dependencies on this from all other components
12721271
setupComponent :: Maybe ElaboratedConfiguredPackage
12731272
setupComponent
1274-
| fromMaybe PD.Custom (PD.buildType (elabPkgDescription elab0)) == PD.Custom
1273+
| PD.buildType (elabPkgDescription elab0) == PD.Custom
12751274
= Just elab0 {
12761275
elabModuleShape = emptyModuleShape,
12771276
elabUnitId = notImpl "elabUnitId",
@@ -2795,7 +2794,7 @@ packageSetupScriptStyle pkg
27952794
| otherwise
27962795
= SetupNonCustomInternalLib
27972796
where
2798-
buildType = fromMaybe PD.Custom (PD.buildType pkg)
2797+
buildType = PD.buildType pkg
27992798

28002799

28012800
-- | Part of our Setup.hs handling policy is implemented by getting the solver

cabal-install/Distribution/Client/SetupWrapper.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import Distribution.Package
3838
import Distribution.Types.Dependency
3939
import Distribution.PackageDescription
4040
( GenericPackageDescription(packageDescription)
41-
, PackageDescription(..), specVersion
41+
, PackageDescription(..), specVersion, buildType
4242
, BuildType(..), knownBuildTypes, defaultRenaming )
4343
import Distribution.PackageDescription.Parsec
4444
( readGenericPackageDescription )
@@ -293,7 +293,7 @@ getSetup verbosity options mpkg = do
293293
(useCabalVersion options)
294294
(orLaterVersion (specVersion pkg))
295295
}
296-
buildType' = fromMaybe Custom (buildType pkg)
296+
buildType' = buildType pkg
297297
checkBuildType buildType'
298298
(version, method, options'') <-
299299
getSetupMethod verbosity options' pkg buildType'

cabal-install/tests/UnitTests/Distribution/Solver/Modular/DSL.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,9 @@ exAvSrcPkg ex =
350350
C.package = pkgId
351351
, C.setupBuildInfo = setup
352352
, C.license = BSD3
353-
, C.buildType = if isNothing setup
354-
then Just C.Simple
355-
else Just C.Custom
353+
, C.buildTypeRaw = if isNothing setup
354+
then Just C.Simple
355+
else Just C.Custom
356356
, C.category = "category"
357357
, C.maintainer = "maintainer"
358358
, C.description = "description"

cabal-testsuite/PackageTests/CustomPlain/plain.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ version: 0.1.0.0
33
license: BSD3
44
author: Edward Z. Yang
55
maintainer: [email protected]
6-
build-type: Custom
76
cabal-version: >=1.10
87

98
library
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Setup configure
22
Resolving dependencies...
33
Configuring plain-0.1.0.0...
4+
Warning: No 'build-type' specified. If you do not need a custom Setup.hs or ./configure script then use 'build-type: Simple'.
45
# Setup build
56
Preprocessing library for plain-0.1.0.0..
67
Building library for plain-0.1.0.0..
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Setup configure
22
Configuring plain-0.1.0.0...
3+
Warning: No 'build-type' specified. If you do not need a custom Setup.hs or ./configure script then use 'build-type: Simple'.
34
# Setup build
45
Preprocessing library for plain-0.1.0.0..
56
Building library for plain-0.1.0.0..
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module M where
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
main :: IO ()
2+
main = fail "Setup called despite `build-type:Simple`"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
packages: .
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Test.Cabal.Prelude
2+
main = cabalTest $ do
3+
recordMode DoNotRecord $ do
4+
-- TODO: Hack; see also CustomDep/cabal.test.hs
5+
withEnvFilter (/= "HOME") $ do
6+
cabal "new-build" ["all"]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
cabal-version: 2.1
2+
name: my
3+
version: 0
4+
-- tests whether the default is `build-type: Simple`
5+
-- (for cabal-version >= 2.1)
6+
7+
library
8+
exposed-modules: M
9+
build-depends: base
10+
default-language: Haskell2010

cabal-testsuite/Test/Cabal/Prelude.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ setup' cmd args = do
151151
else do
152152
pdfile <- liftIO $ tryFindPackageDesc (testCurrentDir env)
153153
pdesc <- liftIO $ readGenericPackageDescription (testVerbosity env) pdfile
154-
if buildType (packageDescription pdesc) == Just Simple
154+
if buildType (packageDescription pdesc) == Simple
155155
then runM (testSetupPath env) full_args
156156
-- Run the Custom script!
157157
else do

0 commit comments

Comments
 (0)