From 77a6782304ce338966c11b1fc10713b0ed2feb8f Mon Sep 17 00:00:00 2001 From: Rodrigo Mesquita Date: Tue, 9 Jan 2024 10:34:23 +0000 Subject: [PATCH] Add test for C source depending on Haskell C stub --- .../PackageTests/FFI/CSourceDependsStub/Lib.hs | 13 +++++++++++++ .../PackageTests/FFI/CSourceDependsStub/Main.hs | 16 ++++++++++++++++ .../FFI/CSourceDependsStub/cabal.project | 1 + .../FFI/CSourceDependsStub/cabal.test.hs | 6 ++++++ .../FFI/CSourceDependsStub/cbits/clib.c | 5 +++++ .../FFI/CSourceDependsStub/csourcedepsstub.cabal | 16 ++++++++++++++++ 6 files changed, 57 insertions(+) create mode 100644 cabal-testsuite/PackageTests/FFI/CSourceDependsStub/Lib.hs create mode 100644 cabal-testsuite/PackageTests/FFI/CSourceDependsStub/Main.hs create mode 100644 cabal-testsuite/PackageTests/FFI/CSourceDependsStub/cabal.project create mode 100644 cabal-testsuite/PackageTests/FFI/CSourceDependsStub/cabal.test.hs create mode 100644 cabal-testsuite/PackageTests/FFI/CSourceDependsStub/cbits/clib.c create mode 100644 cabal-testsuite/PackageTests/FFI/CSourceDependsStub/csourcedepsstub.cabal diff --git a/cabal-testsuite/PackageTests/FFI/CSourceDependsStub/Lib.hs b/cabal-testsuite/PackageTests/FFI/CSourceDependsStub/Lib.hs new file mode 100644 index 00000000000..9535fcd23fb --- /dev/null +++ b/cabal-testsuite/PackageTests/FFI/CSourceDependsStub/Lib.hs @@ -0,0 +1,13 @@ +{-# LANGUAGE ForeignFunctionInterface #-} + +module Lib where + +import Foreign.C (CInt (..)) + +hello :: IO CInt +hello = do + putStrLn "hello!" + return 11 + +foreign export ccall "hello" hello :: IO CInt + diff --git a/cabal-testsuite/PackageTests/FFI/CSourceDependsStub/Main.hs b/cabal-testsuite/PackageTests/FFI/CSourceDependsStub/Main.hs new file mode 100644 index 00000000000..2234f9e4ab2 --- /dev/null +++ b/cabal-testsuite/PackageTests/FFI/CSourceDependsStub/Main.hs @@ -0,0 +1,16 @@ +{-# LANGUAGE ForeignFunctionInterface #-} + +module Main where + +import Foreign.C (CInt (..)) + +foreign import ccall "meaning_of_life_c" + meaning_of_life_c :: IO CInt + +main :: IO () +main = do + secret <- meaning_of_life_c + -- The value 11 comes from the exported Lib.hello + if (secret == 11) + then putStrLn ("The secret is " ++ show secret) + else error ("Expected value 11, got " ++ show secret) diff --git a/cabal-testsuite/PackageTests/FFI/CSourceDependsStub/cabal.project b/cabal-testsuite/PackageTests/FFI/CSourceDependsStub/cabal.project new file mode 100644 index 00000000000..e6fdbadb439 --- /dev/null +++ b/cabal-testsuite/PackageTests/FFI/CSourceDependsStub/cabal.project @@ -0,0 +1 @@ +packages: . diff --git a/cabal-testsuite/PackageTests/FFI/CSourceDependsStub/cabal.test.hs b/cabal-testsuite/PackageTests/FFI/CSourceDependsStub/cabal.test.hs new file mode 100644 index 00000000000..fcfdc963f44 --- /dev/null +++ b/cabal-testsuite/PackageTests/FFI/CSourceDependsStub/cabal.test.hs @@ -0,0 +1,6 @@ +-- Tests whether an extra C source can depend on a _stub header generated by +-- GHC compiling a Haskell module with a foreign export declaration + +import Test.Cabal.Prelude +main = cabalTest $ do + cabal "v2-build" [] diff --git a/cabal-testsuite/PackageTests/FFI/CSourceDependsStub/cbits/clib.c b/cabal-testsuite/PackageTests/FFI/CSourceDependsStub/cbits/clib.c new file mode 100644 index 00000000000..3589ade00f3 --- /dev/null +++ b/cabal-testsuite/PackageTests/FFI/CSourceDependsStub/cbits/clib.c @@ -0,0 +1,5 @@ +#include "Lib_stub.h" + +int meaning_of_life_c() { + return hello(); +} diff --git a/cabal-testsuite/PackageTests/FFI/CSourceDependsStub/csourcedepsstub.cabal b/cabal-testsuite/PackageTests/FFI/CSourceDependsStub/csourcedepsstub.cabal new file mode 100644 index 00000000000..61f0266993d --- /dev/null +++ b/cabal-testsuite/PackageTests/FFI/CSourceDependsStub/csourcedepsstub.cabal @@ -0,0 +1,16 @@ +cabal-version: 2.2 +name: csourcedepsstub +version: 0.1 +build-type: Simple + +library + build-depends: base + default-language: Haskell2010 + include-dirs: cbits + c-sources: cbits/clib.c + exposed-modules: Lib + +executable csourcedeps-exe + main-is: Main.hs + build-depends: base, csourcedepsstub + default-language: Haskell2010