diff --git a/hs-bindgen/app/HsBindgen/Cli/Info.hs b/hs-bindgen/app/HsBindgen/Cli/Info.hs index a306f5afd..f4a68f806 100644 --- a/hs-bindgen/app/HsBindgen/Cli/Info.hs +++ b/hs-bindgen/app/HsBindgen/Cli/Info.hs @@ -19,6 +19,7 @@ import HsBindgen.App import HsBindgen.Cli.Info.IncludeGraph qualified as IncludeGraph import HsBindgen.Cli.Info.Libclang qualified as Libclang import HsBindgen.Cli.Info.ResolveHeader qualified as ResolveHeader +import HsBindgen.Cli.Info.UseDeclGraph qualified as UseDeclGraph {------------------------------------------------------------------------------- CLI help @@ -36,6 +37,7 @@ data Cmd = CmdIncludeGraph IncludeGraph.Opts | CmdLibclang Libclang.Opts | CmdResolveHeader ResolveHeader.Opts + | CmdUseDeclGraph UseDeclGraph.Opts parseCmd :: Parser Cmd parseCmd = subparser $ mconcat [ @@ -54,6 +56,11 @@ parseCmd = subparser $ mconcat [ CmdResolveHeader ResolveHeader.parseOpts ResolveHeader.info + , cmd + "use-decl-graph" + CmdUseDeclGraph + UseDeclGraph.parseOpts + UseDeclGraph.info ] {------------------------------------------------------------------------------- @@ -65,3 +72,4 @@ exec gopts = \case CmdIncludeGraph opts -> IncludeGraph.exec gopts opts CmdLibclang opts -> Libclang.exec gopts opts CmdResolveHeader opts -> ResolveHeader.exec gopts opts + CmdUseDeclGraph opts -> UseDeclGraph.exec gopts opts diff --git a/hs-bindgen/app/HsBindgen/Cli/Info/UseDeclGraph.hs b/hs-bindgen/app/HsBindgen/Cli/Info/UseDeclGraph.hs new file mode 100644 index 000000000..74cad4edb --- /dev/null +++ b/hs-bindgen/app/HsBindgen/Cli/Info/UseDeclGraph.hs @@ -0,0 +1,67 @@ +-- | @hs-bindgen-cli info use-decl-graph@ command +-- +-- Intended for qualified import. +-- +-- > import HsBindgen.Cli.Info.UseDeclGraph qualified as UseDeclGraph +module HsBindgen.Cli.Info.UseDeclGraph ( + -- * CLI help + info + -- * Options + , Opts(..) + , parseOpts + -- * Execution + , exec + ) where + +import Options.Applicative hiding (info) + +import HsBindgen.App +import HsBindgen.Config +import HsBindgen.Frontend.RootHeader +import HsBindgen.Imports + +import HsBindgen + +{------------------------------------------------------------------------------- + CLI help +-------------------------------------------------------------------------------} + +info :: InfoMod a +info = progDesc "Output the use-decl graph" + +{------------------------------------------------------------------------------- + Options +-------------------------------------------------------------------------------} + +data Opts = Opts { + config :: Config + , configPP :: ConfigPP + , output :: Maybe FilePath + , inputs :: [UncheckedHashIncludeArg] + } + +parseOpts :: Parser Opts +parseOpts = + Opts + <$> parseConfig + <*> parseConfigPP + <*> optional parseOutput' + <*> parseInputs + +parseOutput' :: Parser FilePath +parseOutput' = strOption $ mconcat [ + short 'o' + , long "output" + , metavar "PATH" + , help "Output path for the graph" + ] + +{------------------------------------------------------------------------------- + Execution +-------------------------------------------------------------------------------} + +exec :: GlobalOpts -> Opts -> IO () +exec GlobalOpts{..} Opts{..} = do + let artefacts = writeUseDeclGraph output :* Nil + bindgenConfig = toBindgenConfigPP config configPP + void $ hsBindgen tracerConfig bindgenConfig inputs artefacts diff --git a/hs-bindgen/hs-bindgen.cabal b/hs-bindgen/hs-bindgen.cabal index 0e24f69f8..b1f7565cb 100644 --- a/hs-bindgen/hs-bindgen.cabal +++ b/hs-bindgen/hs-bindgen.cabal @@ -266,6 +266,7 @@ executable hs-bindgen-cli HsBindgen.Cli.Info.IncludeGraph HsBindgen.Cli.Info.Libclang HsBindgen.Cli.Info.ResolveHeader + HsBindgen.Cli.Info.UseDeclGraph HsBindgen.Cli.Internal HsBindgen.Cli.Internal.Frontend HsBindgen.Cli.Preprocess diff --git a/hs-bindgen/src-internal/HsBindgen.hs b/hs-bindgen/src-internal/HsBindgen.hs index 472966968..153a9a40c 100644 --- a/hs-bindgen/src-internal/HsBindgen.hs +++ b/hs-bindgen/src-internal/HsBindgen.hs @@ -100,10 +100,10 @@ writeIncludeGraph mPath = Lift (IncludeGraph :* Nil) $ write "include graph" mPath $ IncludeGraph.dumpMermaid p includeGraph -- | Write @use-decl@ graph to file. -writeUseDeclGraph :: FilePath -> Artefact () -writeUseDeclGraph path = Lift (DeclIndex :* UseDeclGraph :* Nil) $ +writeUseDeclGraph :: Maybe FilePath -> Artefact () +writeUseDeclGraph mPath = Lift (DeclIndex :* UseDeclGraph :* Nil) $ \(I index :* I useDeclGraph :* Nil) -> - write "use-decl graph" (Just path) (UseDeclGraph.dumpMermaid index useDeclGraph) + write "use-decl graph" mPath $ UseDeclGraph.dumpMermaid index useDeclGraph -- | Get bindings (single module). getBindings :: Safety -> Artefact String