cdeps (empty) → 0.1.0.0
raw patch · 7 files changed
+299/−0 lines, 7 filesdep +arraydep +basedep +bytestringsetup-changed
Dependencies added: array, base, bytestring, cdeps, directory, filepath, hspec, optparse-applicative, text
Files
- LICENSE +11/−0
- README.md +4/−0
- Setup.hs +2/−0
- app/Main.hs +45/−0
- cdeps.cabal +98/−0
- src/Language/C/Dependency.x +127/−0
- test/Spec.hs +12/−0
+ LICENSE view
@@ -0,0 +1,11 @@+Copyright Vanessa McHale (c) 2018++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,4 @@+# cdeps++This package provides the ability to extract C dependencies from source code. It+can be used with [shake](https://shakebuild.com) or for other purposes.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ app/Main.hs view
@@ -0,0 +1,45 @@+module Main (main) where++import Control.Monad+import Data.Version (showVersion)+import Language.C.Dependency+import Options.Applicative+import Paths_cdeps (version)++includes' :: Parser [FilePath]+includes' = many $ strOption $ mconcat+ [ long "include"+ , short 'I'+ , help "Directories for inclusions."+ ]++data Command = Dump FilePath [FilePath]++dump :: Parser Command+dump = Dump+ <$> target+ <*> includes'++cCompletions :: Mod ArgumentFields a+cCompletions = completer . bashCompleter $ "file -X '!*.c' -o plusdirs"++target :: Parser FilePath+target = argument str+ (metavar "SOURCE"+ <> help "C source file"+ <> cCompletions)++run :: Command -> IO ()+run (Dump cSrc is) = (mapM_ putStrLn <=< getCDepends is) cSrc++versionInfo :: Parser (a -> a)+versionInfo = infoOption ("cdeps version: " ++ showVersion version) (short 'V' <> long "version" <> help "Show version")++wrapper :: ParserInfo Command+wrapper = info (helper <*> versionInfo <*> dump)+ (fullDesc+ <> progDesc "CDeps finds local dependencies for builds"+ <> header "cdeps - a tool for tracking build dependencies")++main :: IO ()+main = execParser wrapper >>= run
+ cdeps.cabal view
@@ -0,0 +1,98 @@+cabal-version: 1.18+name: cdeps+version: 0.1.0.0+license: BSD3+license-file: LICENSE+copyright: Copyright: (c) 2018 Vanessa McHale+maintainer: vamchale@gmail.com+author: Vanessa McHale+synopsis: Extract dependencies from C code.+description:+ This package provides the ability to extract dependencies from C code, for use with [shake](https://shakebuild.com) or otherwise.+category: Development, C, Language+build-type: Simple+extra-doc-files: README.md++source-repository head+ type: git+ location: https://github.com/vmchale/atspkg++flag development+ description:+ Enable `-Werror`+ default: False+ manual: True++flag no-executable+ description:+ Disable building the executable+ default: False+ manual: True++library+ exposed-modules:+ Language.C.Dependency+ build-tools: alex -any+ hs-source-dirs: src+ default-language: Haskell2010+ ghc-options: -Wall+ build-depends:+ base >=4.8 && <5,+ text -any,+ bytestring -any,+ array -any,+ directory -any,+ filepath -any+ + if flag(development)+ ghc-options: -Werror+ + if impl(ghc >=8.0)+ ghc-options: -Wincomplete-uni-patterns -Wincomplete-record-updates+ + if impl(ghc >=8.4)+ ghc-options: -Wmissing-export-lists++executable cdeps+ main-is: Main.hs+ hs-source-dirs: app+ other-modules:+ Paths_cdeps+ default-language: Haskell2010+ ghc-options: -Wall+ build-depends:+ base -any,+ cdeps -any,+ optparse-applicative -any+ + if flag(no-executable)+ buildable: False+ + if flag(development)+ ghc-options: -Werror+ + if impl(ghc >=8.0)+ ghc-options: -Wincomplete-uni-patterns -Wincomplete-record-updates+ + if impl(ghc >=8.4)+ ghc-options: -Wmissing-export-lists++test-suite cdeps-test+ type: exitcode-stdio-1.0+ main-is: Spec.hs+ hs-source-dirs: test+ default-language: Haskell2010+ ghc-options: -threaded -rtsopts -with-rtsopts=-N -Wall+ build-depends:+ base -any,+ cdeps -any,+ hspec -any+ + if flag(development)+ ghc-options: -Werror+ + if impl(ghc >=8.0)+ ghc-options: -Wincomplete-uni-patterns -Wincomplete-record-updates+ + if impl(ghc >=8.4)+ ghc-options: -Wmissing-export-lists
+ src/Language/C/Dependency.x view
@@ -0,0 +1,127 @@+{+module Language.C.Dependency ( getIncludes+ , getCDepends+ ) where++import Data.List (groupBy)+import Data.Maybe (fromMaybe)+import Control.Monad.IO.Class+import System.Directory (doesFileExist)+import Data.Bool (bool)+import qualified Data.Text.Lazy as TL+import Data.Text.Lazy.Encoding (decodeUtf8)+import qualified Data.ByteString.Lazy as BSL+import System.FilePath (takeDirectory)+import System.Environment (lookupEnv)+import Control.Monad++}++%wrapper "monad-bytestring"++$special_char = [\\ntrba\"]+$inner_char = [^\\]++@esc_char = \\ $special_char+@string = \" (@esc_char | $inner_char)* \"++@include = "#include" | "#include" $white+ \\\n++tokens :-++ $white+ ;+ "//".* ;++ "/*" { \_ _ -> nested_comment }++ @include { \_ _ -> alex Include }+ @string { tok (\_ s -> alex (StringTok (TL.unpack (decodeUtf8 s)))) }++ $printable ;++{++data Token = Include+ | StringTok String+ | End++tok f (p,_,s,_) len = f p (BSL.take len s)++alex :: a -> Alex a+alex = pure++alexEOF :: Alex Token+alexEOF = pure End++-- | Given a 'ByteString' containing C, return a list of filepaths it @#include@s.+getIncludes :: BSL.ByteString -> Either String [FilePath]+getIncludes = fmap extractDeps . lexC++nested_comment :: Alex Token+nested_comment = go 1 =<< alexGetInput++ where go :: Int -> AlexInput -> Alex Token+ go 0 input = alexSetInput input >> alexMonadScan+ go n input =+ case alexGetByte input of+ Nothing -> err input+ Just (c, input') ->+ case Data.Char.chr (fromIntegral c) of+ '*' ->+ case alexGetByte input' of+ Nothing -> err input'+ Just (47,input_) -> go (n-1) input_+ Just (_,input_) -> go n input_+ '/' ->+ case alexGetByte input' of+ Nothing -> err input'+ Just (c',input_) -> go (addLevel c' $ n) input_+ _ -> go n input'++ addLevel c' = bool id (+1) (c'==42)++ err (pos,_,_,_) =+ let (AlexPn _ line col) = pos in+ alexError ("Error in nested comment at line " ++ show line ++ ", column " ++ show col)++extractDeps :: [Token] -> [FilePath]+extractDeps [] = mempty+extractDeps (Include:StringTok s:xs) = toInclude s : extractDeps xs+extractDeps (_:xs) = extractDeps xs++toInclude :: String -> FilePath+toInclude = tail . init++lexC :: BSL.ByteString -> Either String [Token]+lexC = flip runAlex loop++loop :: Alex [Token]+loop = do+ tok' <- alexMonadScan+ case tok' of+ End -> pure mempty+ _ -> (tok' :) <$> loop++includes' :: BSL.ByteString -> [FilePath]+includes' = either error id . getIncludes++split :: String -> [String]+split = filter (/= ":") . groupBy g+ where g ':' _ = False+ g _ ':' = False+ g _ _ = True++-- | Get any filepaths that were @#include@-ed in a C source file.+getCDepends :: MonadIO m+ => [FilePath] -- ^ Directories to search in+ -> FilePath -- ^ Path to C source file+ -> m [FilePath]+getCDepends incls src = liftIO $ do+ contents <- BSL.readFile src+ envPath <- fromMaybe mempty <$> lookupEnv "C_INCLUDE_PATH"+ let incl = includes' contents+ dir = takeDirectory src+ allDirs = dir : incls <> split envPath+ filterM doesFileExist ((<>) <$> allDirs <*> incl)++}
+ test/Spec.hs view
@@ -0,0 +1,12 @@+{-# LANGUAGE OverloadedStrings #-}++import Language.C.Dependency+import Test.Hspec++main :: IO ()+main = hspec $ parallel $+ describe "byteStringIncludes" $ do+ it "should work (1/2)" $+ getIncludes "#include \"header.h\"" `shouldBe` Right ["header.h"]+ it "should work (2/2)" $+ getIncludes "#include \\\n\"gmp.cats\"" `shouldBe` Right ["gmp.cats"]