packages feed

chs-cabal (empty) → 0.1.0.0

raw patch · 6 files changed

+222/−0 lines, 6 filesdep +Cabaldep +basedep +chs-deps

Dependencies added: Cabal, base, chs-deps

Files

+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# chs-cabal++## 0.1.0.0++Initial release
+ LICENSE view
@@ -0,0 +1,11 @@+Copyright Vanessa McHale (c) 2019++Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:++1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.++2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.++3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,7 @@+# chs-cabal++[![Hackage CI](https://matrix.hackage.haskell.org/api/v2/packages/chs-cabal/badge)](https://matrix.hackage.haskell.org/package/chs-cabal)+[![Hackage](https://img.shields.io/hackage/v/chs-cabal.svg)](http://hackage.haskell.org/package/chs-cabal)+[![Dependencies of latest version on Hackage](https://img.shields.io/hackage-deps/v/chs-cabal.svg)](https://hackage.haskell.org/package/chs-cabal)++A custom `Setup.hs` that reorders `.chs` files correctly.
+ chs-cabal.cabal view
@@ -0,0 +1,40 @@+cabal-version:   1.18+name:            chs-cabal+version:         0.1.0.0+license:         BSD3+license-file:    LICENSE+copyright:       Copyright: (c) 2019 Vanessa McHale+maintainer:      vamchale@gmail.com+author:          Vanessa McHale+synopsis:        Cabal with c2hs dependencies+description:     Custom Cabal setup that orders chs dependencies correctly+category:        Distribution, Cabal, Build, CustomSetup, Setup+build-type:      Simple+extra-doc-files:+    README.md+    CHANGELOG.md++source-repository head+    type:     darcs+    location: https://hub.darcs.net/vmchale/chs-deps++library+    exposed-modules:+        Distribution.C2Hs+        Distribution.C2Hs.TopSort++    hs-source-dirs:   src+    default-language: Haskell2010+    ghc-options:      -Wall+    build-depends:+        base >=4.3 && <5,+        chs-deps -any,+        Cabal >=3.0 && <3.2++    if impl(ghc >=8.0)+        ghc-options:+            -Wincomplete-uni-patterns -Wincomplete-record-updates+            -Wredundant-constraints -Widentities++    if impl(ghc >=8.4)+        ghc-options: -Wmissing-export-lists
+ src/Distribution/C2Hs.hs view
@@ -0,0 +1,119 @@+module Distribution.C2Hs ( defaultMainC2Hs+                         , c2hsBuildHooks+                         , c2hsHaddockHooks+                         , c2hsReplHooks+                         ) where++import           Distribution.C2Hs.TopSort+import           Distribution.ModuleName               (ModuleName)+import           Distribution.Simple                   (UserHooks (buildHook, haddockHook, replHook),+                                                        defaultMainWithHooks,+                                                        simpleUserHooks)+import           Distribution.Simple.LocalBuildInfo    (LocalBuildInfo)+import           Distribution.Simple.Setup             (BuildFlags (BuildFlags, buildVerbosity),+                                                        HaddockFlags (HaddockFlags, haddockVerbosity),+                                                        ReplFlags (ReplFlags, replVerbosity),+                                                        fromFlagOrDefault)+import           Distribution.Types.Benchmark+import           Distribution.Types.BuildInfo+import           Distribution.Types.Executable+import           Distribution.Types.ForeignLib+import           Distribution.Types.Library+import           Distribution.Types.PackageDescription+import           Distribution.Types.TestSuite+import           Distribution.Verbosity                (Verbosity, normal)++defaultMainC2Hs :: IO ()+defaultMainC2Hs = defaultMainWithHooks+    simpleUserHooks { buildHook = c2hsBuildHooks+                    , haddockHook = c2hsHaddockHooks+                    , replHook = c2hsReplHooks+                    }++-- | Custom build hooks to be used with @.chs@ files which @\{#import#\}@ one+-- another.+c2hsBuildHooks :: PackageDescription -> LocalBuildInfo -> UserHooks -> BuildFlags -> IO ()+c2hsBuildHooks = \pd lbi hooks bf -> do+    let v = getVerbosity bf+    pd' <- mapPackageDescription (reorderC2Hs v) pd+    buildHook simpleUserHooks pd' lbi hooks bf++c2hsHaddockHooks :: PackageDescription -> LocalBuildInfo -> UserHooks -> HaddockFlags -> IO ()+c2hsHaddockHooks = \pd lbi hooks hf -> do+    let v = getHaddockVerbosity hf+    pd' <- mapPackageDescription (reorderC2Hs v) pd+    haddockHook simpleUserHooks pd' lbi hooks hf++c2hsReplHooks :: PackageDescription -> LocalBuildInfo -> UserHooks -> ReplFlags -> [String] -> IO ()+c2hsReplHooks = \pd lbi hooks rf ss -> do+    let v = getReplVerbosity rf+    pd' <- mapPackageDescription (reorderC2Hs v) pd+    replHook simpleUserHooks pd' lbi hooks rf ss++getHaddockVerbosity :: HaddockFlags -> Verbosity+getHaddockVerbosity HaddockFlags { haddockVerbosity = v } = fromFlagOrDefault normal v++getVerbosity :: BuildFlags -> Verbosity+getVerbosity BuildFlags { buildVerbosity = v } = fromFlagOrDefault normal v++getReplVerbosity :: ReplFlags -> Verbosity+getReplVerbosity ReplFlags { replVerbosity = v } = fromFlagOrDefault normal v++mapPackageDescription :: ([FilePath] -> [ModuleName] -> IO [ModuleName]) -> PackageDescription -> IO PackageDescription+mapPackageDescription f p@PackageDescription { library = ml+                                             , subLibraries = ls+                                             , executables = es+                                             , foreignLibs = fs+                                             , testSuites = ts+                                             , benchmarks = bs+                                             } = do+    ml' <- traverse (mapLibrary f) ml+    ls' <- traverse (mapLibrary f) ls+    es' <- traverse (mapExecutable f) es+    fs' <- traverse (mapForeignLibrary f) fs+    ts' <- traverse (mapTestSuite f) ts+    bs' <- traverse (mapBenchmark f) bs+    pure $ p { library = ml'+             , subLibraries = ls'+             , executables = es'+             , foreignLibs = fs'+             , testSuites = ts'+             , benchmarks = bs'+             }++mapLibrary :: ([FilePath] -> [ModuleName] -> IO [ModuleName]) -> Library -> IO Library+mapLibrary f lib@Library { exposedModules = es, libBuildInfo = bi } = do+    let dirs = hsSourceDirs bi+        om = otherModules bi+        isOther = (`notElem` es)+    newMods <- f dirs (es ++ om)+    let om' = filter isOther newMods+        bi' = bi { otherModules = om' }+        -- This is stupid, but I've tested it and it seems to not do anything+        -- egregious+    pure $ lib { exposedModules = newMods, libBuildInfo = bi' }++mapBenchmark :: ([FilePath] -> [ModuleName] -> IO [ModuleName]) -> Benchmark -> IO Benchmark+mapBenchmark f b@Benchmark { benchmarkBuildInfo = bi } = do+    bi' <- mapBuildInfo f bi+    pure $ b { benchmarkBuildInfo = bi' }++mapExecutable :: ([FilePath] -> [ModuleName] -> IO [ModuleName]) -> Executable -> IO Executable+mapExecutable f e@Executable { buildInfo = bi } = do+    bi' <- mapBuildInfo f bi+    pure $ e { buildInfo = bi' }++mapForeignLibrary :: ([FilePath] -> [ModuleName] -> IO [ModuleName]) -> ForeignLib -> IO ForeignLib+mapForeignLibrary f fl@ForeignLib { foreignLibBuildInfo = bi } = do+    bi' <- mapBuildInfo f bi+    pure $ fl { foreignLibBuildInfo = bi' }++mapTestSuite :: ([FilePath] -> [ModuleName] -> IO [ModuleName]) -> TestSuite -> IO TestSuite+mapTestSuite f t@TestSuite { testBuildInfo = bi } = do+    bi' <- mapBuildInfo f bi+    pure $ t { testBuildInfo = bi' }++mapBuildInfo :: ([FilePath] -> [ModuleName] -> IO [ModuleName]) -> BuildInfo -> IO BuildInfo+mapBuildInfo f bi@BuildInfo { otherModules = om, hsSourceDirs = dirs } = do+    om' <- f dirs om+    pure $ bi { otherModules = om' }
+ src/Distribution/C2Hs/TopSort.hs view
@@ -0,0 +1,40 @@+module Distribution.C2Hs.TopSort ( reorderC2Hs ) where++import           Data.Functor              (($>))+import           Distribution.Compat.Graph (Node (..), fromDistinctList,+                                            revTopSort)+import           Distribution.ModuleName   (ModuleName, toFilePath)+import           Distribution.Parsec       (simpleParsec)+import           Distribution.Simple.Utils (findFileWithExtension, warn)+import           Distribution.Verbosity    (Verbosity)+import           Language.Haskell.CHs.Deps (getFileImports)++-- | Given a list of 'ModuleName's, sort it according to @c2hs@ @{\#import\#}@+-- declarations.+reorderC2Hs :: Verbosity+            -> [FilePath] -- ^ Source directories+            -> [ModuleName] -- ^ Module names+            -> IO [ModuleName] -- ^ Sorted modules+reorderC2Hs v dirs preMods = do++    chsFiles <- traverse findCHS preMods++    modDeps <- traverse (extractDeps v) (zip preMods chsFiles)++    pure $ fmap (\(N m _ _) -> m) (revTopSort $ fromDistinctList modDeps)++        where findCHS = findFileWithExtension [".chs"] dirs . toFilePath++-- | Given a 'ModuleName' and its corresponding filepath, return a 'Node'+-- with its associated @c2hs@ dependencies+extractDeps :: Verbosity -> (ModuleName, Maybe FilePath) -> IO (Node ModuleName ModuleName)+extractDeps _ (m, Nothing) = pure (N m m [])+extractDeps v (m, Just f) = do+    res <- getFileImports f+    mods <- case res of+        Right ms -> case traverse simpleParsec ms of+            Just ms' -> pure ms'+            Nothing -> warn v ("Cannot parse module name in .chs file " ++ f) $> []+        Left err -> warn v ("Cannot parse c2hs import in " ++ f ++ ": " ++ err) $> []+    pure (N m m mods)+