Skip to content

Commit 1b8e5bb

Browse files
committed
Add allGhcOptExtra from all *-options
Add test for capi Add #if for GHC >=810
1 parent 824735d commit 1b8e5bb

File tree

10 files changed

+77
-1
lines changed

10 files changed

+77
-1
lines changed

Cabal/src/Distribution/Simple/GHC/Internal.hs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE CPP #-}
12
{-# LANGUAGE DataKinds #-}
23
{-# LANGUAGE FlexibleContexts #-}
34
{-# LANGUAGE PatternSynonyms #-}
@@ -377,6 +378,18 @@ includePaths lbi bi clbi odir =
377378
| dir <- mapMaybe (symbolicPathRelative_maybe . unsafeCoerceSymbolicPath) $ includeDirs bi
378379
]
379380

381+
allGhcOptExtra :: BuildInfo -> [String]
382+
allGhcOptExtra bi =
383+
["-optP" ++ opt | opt <- cppOptions bi]
384+
++ ["-opta" ++ opt | opt <- asmOptions bi]
385+
++ ["-optJSP" ++ opt | opt <- jsppOptions bi]
386+
++ ["-optl" ++ opt | opt <- ldOptions bi]
387+
++ ["-optc" ++ opt | opt <- ccOptions bi]
388+
#if __GLASGOW_HASKELL__ >= 810
389+
-- Pass -optcxx for GHC >= 8.10 https://github.com/haskell/cabal/pull/7072
390+
++ ["-optcxx" ++ opt | opt <- cxxOptions bi]
391+
#endif
392+
380393
componentCcGhcOptions
381394
:: Verbosity
382395
-> LocalBuildInfo
@@ -581,7 +594,7 @@ componentGhcOptions verbosity lbi bi clbi odir =
581594
, ghcOptOutputDir = toFlag $ coerceSymbolicPath odir
582595
, ghcOptOptimisation = toGhcOptimisation (withOptimization lbi)
583596
, ghcOptDebugInfo = toFlag (withDebugInfo lbi)
584-
, ghcOptExtra = hcOptions GHC bi
597+
, ghcOptExtra = allGhcOptExtra bi <> hcOptions GHC bi
585598
, ghcOptExtraPath = toNubListR exe_paths
586599
, ghcOptLanguage = toFlag (fromMaybe Haskell98 (defaultLanguage bi))
587600
, -- Unsupported extensions have already been checked by configure
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{-# LANGUAGE CApiFFI #-}
2+
3+
module Main where
4+
5+
import Foreign.C (CInt (..))
6+
7+
foreign import capi "clib.h myplus"
8+
myplus :: CInt -> CInt -> IO CInt
9+
10+
main :: IO ()
11+
main = do
12+
result <- myplus 5 6
13+
if (result == 11)
14+
then putStrLn ("The result is " ++ show result)
15+
else error ("Expected value 11, got " ++ show result)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# ForeignOptsCapi
2+
3+
This test case asserts that cabal passes `cc-options` to the C compiler when use `foreign import capi`.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# cabal v2-build
2+
Configuration is affected by the following files:
3+
- cabal.project
4+
Resolving dependencies...
5+
Build profile: -w ghc-<GHCVER> -O1
6+
In order, the following will be built:
7+
- foreign-opts-capi-0.1 (exe:foreign-opts-capi-exe) (first run)
8+
Configuring executable 'foreign-opts-capi-exe' for foreign-opts-capi-0.1...
9+
Preprocessing executable 'foreign-opts-capi-exe' for foreign-opts-capi-0.1...
10+
Building executable 'foreign-opts-capi-exe' for foreign-opts-capi-0.1...
11+
# foreign-opts-capi foreign-opts-capi-exe
12+
The result is 11
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
packages: .
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import Test.Cabal.Prelude
2+
main = cabalTest $ do
3+
cabal "v2-build" ["foreign-opts-capi-exe"]
4+
withPlan $ runPlanExe "foreign-opts-capi" "foreign-opts-capi-exe" []

cabal-testsuite/PackageTests/FFI/ForeignOptsCapi/cbits/clib.c

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef MYDEF
2+
#error "Did not get required MYDEF from cc-options"
3+
#endif
4+
5+
static inline int myplus(int a, int b) {
6+
return a + b;
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
cabal-version: 3.0
2+
name: foreign-opts-capi
3+
version: 0.1
4+
build-type: Simple
5+
6+
executable foreign-opts-capi-exe
7+
main-is: Main.hs
8+
build-depends: base
9+
default-language: Haskell2010
10+
include-dirs: cbits
11+
cc-options: -DMYDEF=1

changelog.d/pr-10969.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
synopsis: Pass *-options to GHC when compiling ordinary Haskell sources
3+
packages: [Cabal]
4+
prs: 10969
5+
---
6+
7+
*-options should be always passed when invoking GHC,
8+
similarly as ghc-options should be always used when
9+
invoking ghc - regardless of what is the intention of a particular GHC-call.
10+
GHC might use or not use the options, Cabal cannot know and should not guess.

0 commit comments

Comments
 (0)