Skip to content

Commit 9fb03d7

Browse files
committed
Implement better defaulting for build-type
This implements the following defaulting rules: * For `cabal-version:2.0` and below, default to the `Custom` build-type unconditionally (legacy defaulting) * Otherwise, if a `custom-setup` stanza is defined, default to the `Custom` build-type; else default to `Simple` build-type. This gets us better defaults for the two most popular use-cases, and which can be statically inferred by only looking at the `.cabal` file. This allows us to bring down the minimal (modern) trivial cabal package definition down to a single file with 4 lines: cabal-version: 2.1 name: mu version: 0 library NB: We don't need any `Setup.hs` file, as `cabal sdist` will magically generate one on the fly.
1 parent 895f863 commit 9fb03d7

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

Cabal/Distribution/PackageDescription/Check.hs

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

451-
, check (isNothing (buildTypeRaw 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'."

Cabal/Distribution/Types/PackageDescription.hs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,22 @@ descCabalVersion pkg = case specVersionRaw pkg of
190190
-- /effective/ @build-type@. This function implements the following
191191
-- defaulting rules:
192192
--
193-
-- * For @cabal-version:2.0@ and below, default to @Custom@ build-type.
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.
194198
--
195199
-- @since 2.2
196200
buildType :: PackageDescription -> BuildType
197-
buildType = fromMaybe Custom . buildTypeRaw
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
198209

199210
emptyPackageDescription :: PackageDescription
200211
emptyPackageDescription

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

0 commit comments

Comments
 (0)