Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support sub-library dependencies #5659

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 0 additions & 29 deletions src/Stack/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ import qualified Data.Text.IO as TIO
import Data.Text.Read (decimal)
import qualified Data.Vector as V
import qualified Data.Yaml as Yaml
import qualified Distribution.PackageDescription as C
import Distribution.Types.Dependency (depLibraries)
import Distribution.Version (mkVersion)
import Path (parent)
import Stack.Build.ConstructPlan
Expand Down Expand Up @@ -68,8 +66,6 @@ build msetLocalFiles = do
depsLocals <- localDependencies
let allLocals = locals <> depsLocals

checkSubLibraryDependencies (Map.elems $ smProject sourceMap)

boptsCli <- view $ envConfigL.to envConfigBuildOptsCLI
-- Set local files, necessary for file watching
stackYaml <- view stackYamlL
Expand Down Expand Up @@ -353,28 +349,3 @@ checkComponentsBuildable lps =
| lp <- lps
, c <- Set.toList (lpUnbuildable lp)
]

-- | Find if sublibrary dependency exist in each project
checkSubLibraryDependencies :: HasLogFunc env => [ProjectPackage] -> RIO env ()
checkSubLibraryDependencies proj = do
forM_ proj $ \p -> do
C.GenericPackageDescription _ _ lib subLibs foreignLibs exes tests benches <- liftIO $ cpGPD . ppCommon $ p

let dependencies = concatMap getDeps subLibs <>
concatMap getDeps foreignLibs <>
concatMap getDeps exes <>
concatMap getDeps tests <>
concatMap getDeps benches <>
maybe [] C.condTreeConstraints lib
libraries = concatMap (toList . depLibraries) dependencies

when (subLibDepExist libraries)
(logWarn "SubLibrary dependency is not supported, this will almost certainly fail")
where
getDeps (_, C.CondNode _ dep _) = dep
subLibDepExist lib =
any (\x ->
case x of
C.LSubLibName _ -> True
C.LMainLibName -> False
) lib
9 changes: 9 additions & 0 deletions src/Stack/Package.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE LambdaCase #-}

-- | Dealing with Cabal.

Expand Down Expand Up @@ -145,6 +146,7 @@ packageFromPackageDescription packageConfig pkgFlags (PackageDescriptionPair pkg
, packageDefaultFlags = M.fromList
[(flagName flag, flagDefault flag) | flag <- pkgFlags]
, packageAllDeps = S.fromList (M.keys deps)
, packageSubLibDeps = subLibDeps
, packageLibraries =
let mlib = do
lib <- library pkg
Expand Down Expand Up @@ -259,6 +261,13 @@ packageFromPackageDescription packageConfig pkgFlags (PackageDescriptionPair pkg
(M.fromList . map (depPkgName &&& depVerRange) . setupDepends)
(setupBuildInfo pkg)

-- TODO: Should merge this calculation with deps.
subLibDeps = S.fromList $ concatMap
(\(Dependency n _ libs) -> mapMaybe (getSubLibName n) (S.toList libs))
(concatMap targetBuildDepends (allBuildInfo' pkg))
getSubLibName pn (LSubLibName cn) = Just (T.pack (unPackageName pn <> ":" <> Cabal.unUnqualComponentName cn))
getSubLibName _ _ = Nothing

asLibrary range = DepValue
{ dvVersionRange = range
, dvType = AsLibrary
Expand Down
10 changes: 9 additions & 1 deletion src/Stack/Types/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import Database.Persist.Sql (PersistField(..)
import Distribution.PackageDescription (TestSuiteInterface)
import Distribution.System (Arch)
import qualified Distribution.Text as C
import Distribution.Types.PackageName (mkPackageName, unPackageName)
import Distribution.Version (mkVersion)
import Path (parseRelDir, (</>), parent)
import Path.Extra (toFilePathNoTrailingSep)
Expand Down Expand Up @@ -690,7 +691,14 @@ configureOptsNoDir econfig bco deps isLocal package = concat
-- with --exact-configuration.
flags = packageFlags package `Map.union` packageDefaultFlags package

depOptions = map (uncurry toDepOption) $ Map.toList deps
-- FIXME: drunk code
sublibDeps = flip mapMaybe (Set.toList (packageSubLibDeps package)) $ \sublib ->
let subname = T.unpack sublib
in case find (\(PackageIdentifier pn _, _) -> unPackageName pn == takeWhile (/= ':') subname) (Map.toList deps) of
Just (PackageIdentifier _ v, gid) -> Just (PackageIdentifier (mkPackageName subname) v, gid)
_ -> Nothing

depOptions = map (uncurry toDepOption) (Map.toList deps <> sublibDeps)
where
toDepOption = if newerCabal then toDepOption1_22 else toDepOption1_18

Expand Down
2 changes: 2 additions & 0 deletions src/Stack/Types/Package.hs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ data Package =
,packageDeps :: !(Map PackageName DepValue) -- ^ Packages that the package depends on, both as libraries and build tools.
,packageUnknownTools :: !(Set ExeName) -- ^ Build tools specified in the legacy manner (build-tools:) that failed the hard-coded lookup.
,packageAllDeps :: !(Set PackageName) -- ^ Original dependencies (not sieved).
-- FIXME: Having `packageDeps`, `packageAllDeps`, and `packageSubLibDeps` seem redundant.
,packageSubLibDeps :: !(Set Text) -- ^ Original sub-library dependencies (not sieved).
,packageGhcOptions :: ![Text] -- ^ Ghc options used on package.
,packageCabalConfigOpts :: ![Text] -- ^ Additional options passed to ./Setup.hs configure
,packageFlags :: !(Map FlagName Bool) -- ^ Flags used on package.
Expand Down
12 changes: 1 addition & 11 deletions test/integration/tests/cabal-sublibrary-dependency/Main.hs
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
import Control.Monad (unless)
import Data.List (isInfixOf)
import StackTest

main :: IO ()
main = do
putStrLn "Disabled: CI doesn't have GHC 8.8.1"
{-
stackErrStderr ["build"] $ \str ->
let msg = "SubLibrary dependency is not supported, this will almost certainly fail" in

unless (msg `isInfixOf` str) $
error $ "Expected a warning: \n" ++ show msg
-}
main = stack ["build", "--install-ghc"]
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
resolver: nightly-2020-01-17
resolver: nightly-2021-06-19
packages:
- .
- subproject
extra-deps:
- github: snoyberg/filelock
commit: 4f080496d8bf153fbe26e64d1f52cf73c7db25f6
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cabal-version: 3.0
name: subproject
version: 0.1.0.0
build-type: Simple
cabal-version: >=1.10

library
hs-source-dirs: src
Expand All @@ -10,6 +10,7 @@ library
default-language: Haskell2010

library sub
visibility: public
hs-source-dirs: src
exposed-modules: SubLib
build-depends: base >= 4.7 && < 5
Expand Down