Opened 3 years ago

Last modified 3 years ago

#13269 new bug

Changes in foreign code used in TH do not trigger recompilation

Reported by: facundo.dominguez Owned by:
Priority: normal Milestone:
Component: Template Haskell Version: 8.0.1
Keywords: inline-c addCStub addDependentFile Cc: nh2, mboes, bgamari
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: #13237 Differential Rev(s):
Wiki Page:

Description (last modified by facundo.dominguez)

Failing example:

// f.c
#include <stdio.h>

int f(int  x) {
    printf("calling f(%d)\n",x);
    return x + 1;
}
-- A.hs
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE TemplateHaskell #-}

module A where

import Language.Haskell.TH.Syntax

foreign import ccall f :: Int -> IO Int

do addCStub "#include \"f.c\""
   addDependentFile "f.c"
   return []
-- B.hs
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE TemplateHaskell #-}

module Main where

import Language.Haskell.TH.Syntax
import A

do i <- runIO $ f 0
   [d| fh = i |]

main :: IO ()
main = print fh
$ ghc --make B.hs
[1 of 2] Compiling A                ( A.hs, A.o )
[2 of 2] Compiling Main             ( B.hs, B.o )
calling f(0)
Linking B ...
$ ./B
1

Edit f.c:

 // f.c
 #include <stdio.h>

 int f(int  x) {
     printf("calling f(%d)\n",x);
-    return x + 1;
+    return x + 2;
 }

Recompiling we can see that B.hs is not rebuilt and executing the program still shows the old result.

$ ghc --make B.hs
[1 of 2] Compiling A                ( A.hs, A.o ) [f.c changed]
Linking B ...
$ ./B
1

Change History (4)

comment:1 Changed 3 years ago by facundo.dominguez

Description: modified (diff)
Summary: Changes in includes of `addCStub` do not cause recompilation of downstream modules.Changes in includes of addCStub do not cause recompilation of downstream modules.

comment:2 Changed 3 years ago by facundo.dominguez

comment:3 Changed 3 years ago by facundo.dominguez

Description: modified (diff)
Keywords: addDependentFile added

Perhaps it could be fixed by adding a hash of the files pointed with addDependendFile to the module interface file.

nh2 proposed in #13237 adding a hash of the cpp output over the string given to addCStub.

Note though, that this behavior can be experienced without addCStub if one replaces A.hs with

-- A.hs
{-# LANGUAGE ForeignFunctionInterface #-}
module A where
import Language.Haskell.TH.Syntax
foreign import ccall f :: Int -> IO Int

and builds with:

$ ghc --make B.hs f.c -fPIC

I don't have ideas to fix this case.

comment:4 Changed 3 years ago by facundo.dominguez

Summary: Changes in includes of addCStub do not cause recompilation of downstream modules.Changes in foreign code used in TH do not trigger recompilation
Note: See TracTickets for help on using tickets.