From 06a783dbdcf93838beff74637782d721e84863af Mon Sep 17 00:00:00 2001 From: Hai Nguyen Quang Date: Tue, 28 Dec 2021 08:19:52 +0700 Subject: [PATCH] wip: Support sub-library dependencies --- src/Stack/Build.hs | 29 ------------------- src/Stack/Package.hs | 9 ++++++ src/Stack/Types/Build.hs | 10 ++++++- src/Stack/Types/Package.hs | 2 ++ .../tests/cabal-sublibrary-dependency/Main.hs | 12 +------- .../files/stack.yaml | 5 +--- .../files/subproject/subproject.cabal | 3 +- 7 files changed, 24 insertions(+), 46 deletions(-) diff --git a/src/Stack/Build.hs b/src/Stack/Build.hs index 23b9a97199..acdbb3308e 100644 --- a/src/Stack/Build.hs +++ b/src/Stack/Build.hs @@ -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 @@ -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 @@ -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 diff --git a/src/Stack/Package.hs b/src/Stack/Package.hs index 4db50d2a4b..075ace083b 100644 --- a/src/Stack/Package.hs +++ b/src/Stack/Package.hs @@ -9,6 +9,7 @@ {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE RecordWildCards #-} +{-# LANGUAGE LambdaCase #-} -- | Dealing with Cabal. @@ -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 @@ -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 diff --git a/src/Stack/Types/Build.hs b/src/Stack/Types/Build.hs index e4a6c684ec..e06d071d72 100644 --- a/src/Stack/Types/Build.hs +++ b/src/Stack/Types/Build.hs @@ -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) @@ -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 diff --git a/src/Stack/Types/Package.hs b/src/Stack/Types/Package.hs index afae34dfec..3b884a54f1 100644 --- a/src/Stack/Types/Package.hs +++ b/src/Stack/Types/Package.hs @@ -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. diff --git a/test/integration/tests/cabal-sublibrary-dependency/Main.hs b/test/integration/tests/cabal-sublibrary-dependency/Main.hs index 1fa8ee7d89..915a702fcc 100644 --- a/test/integration/tests/cabal-sublibrary-dependency/Main.hs +++ b/test/integration/tests/cabal-sublibrary-dependency/Main.hs @@ -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"] diff --git a/test/integration/tests/cabal-sublibrary-dependency/files/stack.yaml b/test/integration/tests/cabal-sublibrary-dependency/files/stack.yaml index 8b93dddda1..3f1fad24e8 100644 --- a/test/integration/tests/cabal-sublibrary-dependency/files/stack.yaml +++ b/test/integration/tests/cabal-sublibrary-dependency/files/stack.yaml @@ -1,7 +1,4 @@ -resolver: nightly-2020-01-17 +resolver: nightly-2021-06-19 packages: - . - subproject -extra-deps: -- github: snoyberg/filelock - commit: 4f080496d8bf153fbe26e64d1f52cf73c7db25f6 diff --git a/test/integration/tests/cabal-sublibrary-dependency/files/subproject/subproject.cabal b/test/integration/tests/cabal-sublibrary-dependency/files/subproject/subproject.cabal index 4c083cac74..c3e502d26d 100644 --- a/test/integration/tests/cabal-sublibrary-dependency/files/subproject/subproject.cabal +++ b/test/integration/tests/cabal-sublibrary-dependency/files/subproject/subproject.cabal @@ -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 @@ -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