From 66c05f1553c395505e9aa95dc9fb5e466039e01d Mon Sep 17 00:00:00 2001 From: Mathias Sven Date: Sun, 14 Jan 2024 21:06:24 +0000 Subject: [PATCH] track included files via TH's addDependentFile --- inline-c/inline-c.cabal | 2 ++ inline-c/src/Language/C/Inline.hs | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/inline-c/inline-c.cabal b/inline-c/inline-c.cabal index fb0e8f6..10fe00e 100644 --- a/inline-c/inline-c.cabal +++ b/inline-c/inline-c.cabal @@ -44,6 +44,8 @@ library , transformers >= 0.1.3.0 , unordered-containers , vector + , filepath + , directory hs-source-dirs: src default-language: Haskell2010 diff --git a/inline-c/src/Language/C/Inline.hs b/inline-c/src/Language/C/Inline.hs index 7d7f1f2..2b0cbd4 100644 --- a/inline-c/src/Language/C/Inline.hs +++ b/inline-c/src/Language/C/Inline.hs @@ -74,13 +74,16 @@ import Prelude hiding (exp) import Prelude hiding (exp, pure) #endif -import Control.Monad (void) +import Control.Monad (void, when) import Foreign.C.Types import Foreign.Marshal.Alloc (alloca) import Foreign.Ptr (Ptr) import Foreign.Storable (peek, Storable) +import System.Directory (getCurrentDirectory, doesFileExist) +import System.FilePath (takeDirectory, ()) import qualified Language.Haskell.TH as TH import qualified Language.Haskell.TH.Quote as TH +import qualified Language.Haskell.TH.Syntax as TH import Language.C.Inline.Context import Language.C.Inline.Internal @@ -310,7 +313,19 @@ include :: String -> TH.DecsQ include s | null s = fail "inline-c: empty string (include)" | head s == '<' = verbatim $ "#include " ++ s - | otherwise = verbatim $ "#include \"" ++ s ++ "\"" + | otherwise = do + addDependentFileRelative s + verbatim $ "#include \"" ++ s ++ "\"" + where + addDependentFileRelative relativeFile = do + currentFilename <- TH.loc_filename <$> TH.location + pwd <- TH.runIO getCurrentDirectory + let currentDirectory = takeDirectory (pwd currentFilename) + invocationRelativePath = currentDirectory relativeFile + + fileExist <- TH.runIO $ doesFileExist invocationRelativePath + when fileExist $ + TH.addDependentFile invocationRelativePath -- | Emits an arbitrary C string to the C code associated with the -- current module. Use with care.