Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track included files via TH's addDependentFile #153

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions inline-c/inline-c.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ library
, transformers >= 0.1.3.0
, unordered-containers
, vector
, filepath
, directory
hs-source-dirs: src
default-language: Haskell2010

Expand Down
19 changes: 17 additions & 2 deletions inline-c/src/Language/C/Inline.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down