Skip to content

Commit

Permalink
Merge pull request #6324 from commercialhaskell/fix6323
Browse files Browse the repository at this point in the history
  • Loading branch information
mpilgrem authored Oct 28, 2023
2 parents 52bf76f + 155783d commit e7623ee
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
20 changes: 6 additions & 14 deletions src/Stack/Build/Source.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import Stack.Types.EnvConfig
)
import Stack.Types.FileDigestCache ( readFileDigest )
import Stack.Types.NamedComponent
( NamedComponent (..), isCSubLib )
( NamedComponent (..), isCSubLib, splitComponents )
import Stack.Types.Package
( FileCacheInfo (..), LocalPackage (..), Package (..)
, PackageConfig (..), PackageLibraries (..)
Expand Down Expand Up @@ -273,18 +273,6 @@ generalGhcOptions bconfig boptsCli isTarget isLocal = concat
AGOLocals -> isLocal
AGOEverything -> True

splitComponents :: [NamedComponent]
-> (Set Text, Set Text, Set Text)
splitComponents =
go id id id
where
go a b c [] = (Set.fromList $ a [], Set.fromList $ b [], Set.fromList $ c [])
go a b c (CLib:xs) = go a b c xs
go a b c (CSubLib x:xs) = go (a . (x:)) b c xs
go a b c (CExe x:xs) = go (a . (x:)) b c xs
go a b c (CTest x:xs) = go a (b . (x:)) c xs
go a b c (CBench x:xs) = go a b (c . (x:)) xs

loadCommonPackage ::
forall env. (HasBuildConfig env, HasSourceMap env)
=> CommonPackage
Expand Down Expand Up @@ -318,7 +306,11 @@ loadLocalPackage pp = do
mtarget = M.lookup name (smtTargets $ smTargets sm)
(exeCandidates, testCandidates, benchCandidates) =
case mtarget of
Just (TargetComps comps) -> splitComponents $ Set.toList comps
Just (TargetComps comps) ->
-- Currently, a named library component (a sub-library) cannot be
-- specified as a build target.
let (_s, e, t, b) = splitComponents $ Set.toList comps
in (e, t, b)
Just (TargetAll _packageType) ->
( packageExes pkg
, if boptsTests bopts
Expand Down
34 changes: 33 additions & 1 deletion src/Stack/Types/NamedComponent.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}

-- | Module exporting the 'NamedComponent' type and related functions.
module Stack.Types.NamedComponent
( NamedComponent (..)
, renderComponent
Expand All @@ -15,19 +16,25 @@ module Stack.Types.NamedComponent
, isCExe
, isCTest
, isCBench
, splitComponents
) where

import qualified Data.Set as Set
import qualified Data.Text as T
import Stack.Prelude

-- | A single, fully resolved component of a package
-- | Type representing components of a fully-resolved Cabal package.
data NamedComponent
= CLib
-- The \'main\' unnamed library component.
| CSubLib !Text
-- A named \'subsidiary\' or \'ancillary\` library component (sub-library).
| CExe !Text
-- A named executable component.
| CTest !Text
-- A named test-suite component.
| CBench !Text
-- A named benchmark component.
deriving (Eq, Ord, Show)

renderComponent :: NamedComponent -> Text
Expand Down Expand Up @@ -87,3 +94,28 @@ isCTest _ = False
isCBench :: NamedComponent -> Bool
isCBench CBench{} = True
isCBench _ = False

-- | A function to split the given list of components into sets of the names of
-- the named components by the type of component (sub-libraries, executables,
-- test-suites, benchmarks), ignoring any 'main' unnamed library component.
splitComponents ::
[NamedComponent]
-> ( Set Text
-- ^ Sub-libraries.
, Set Text
-- ^ Executables.
, Set Text
-- ^ Test-suites.
, Set Text
-- ^ Benchmarks.
)
splitComponents =
go id id id id
where
run c = Set.fromList $ c []
go s e t b [] = (run s, run e, run t, run b)
go s e t b (CLib:xs) = go s e t b xs
go s e t b (CSubLib x:xs) = go (s . (x:)) e t b xs
go s e t b (CExe x:xs) = go s (e . (x:)) t b xs
go s e t b (CTest x:xs) = go s e (t . (x:)) b xs
go s e t b (CBench x:xs) = go s e t (b . (x:)) xs

0 comments on commit e7623ee

Please sign in to comment.