Skip to content

Pass -optcxx for GHC >= 8.10 #7072

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

Merged
merged 1 commit into from
Sep 16, 2020
Merged
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
6 changes: 5 additions & 1 deletion Cabal/src/Distribution/Simple/Program/GHC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,11 @@ renderGhcOptions comp _platform@(Platform _arch os) opts
, concat [ [ "-optP-include", "-optP" ++ inc]
| inc <- flags ghcOptCppIncludes ]
, [ "-optc" ++ opt | opt <- ghcOptCcOptions opts]
, [ "-optc" ++ opt | opt <- ghcOptCxxOptions opts]
, -- C++ compiler options: GHC >= 8.10 requires -optcxx, older requires -optc
let cxxflag = case compilerCompatVersion GHC comp of
Just v | v >= mkVersion [8, 10] -> "-optcxx"
_ -> "-optc"
in [ cxxflag ++ opt | opt <- ghcOptCxxOptions opts]
, [ "-opta" ++ opt | opt <- ghcOptAsmOptions opts]

-----------------
Expand Down
16 changes: 16 additions & 0 deletions cabal-testsuite/PackageTests/FFI/ForeignOptsC/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{-# LANGUAGE ForeignFunctionInterface #-}

module Main where

import Foreign.C (CInt (..))

foreign import ccall "clib.h meaning_of_life_c"
meaning_of_life_c :: IO CInt

main :: IO ()
main = do
secret <- meaning_of_life_c
-- The value 11 comes from __TESTOPT_C__ - see the cabal file.
if (secret == 11)
then putStrLn ("The secret is " ++ show secret)
else error ("Expected value 11, got " ++ show secret)
5 changes: 5 additions & 0 deletions cabal-testsuite/PackageTests/FFI/ForeignOptsC/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# ForeignOptsC

This test case asserts that cabal passes `cc-options` to the C compiler (and NOT `cxx-options`).

See the additional case `ForeignOptsCxx`.
10 changes: 10 additions & 0 deletions cabal-testsuite/PackageTests/FFI/ForeignOptsC/cabal.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# cabal v2-build
Resolving dependencies...
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- foreign-opts-c-0.1 (exe:foreign-opts-c-exe) (first run)
Configuring executable 'foreign-opts-c-exe' for foreign-opts-c-0.1..
Preprocessing executable 'foreign-opts-c-exe' for foreign-opts-c-0.1..
Building executable 'foreign-opts-c-exe' for foreign-opts-c-0.1..
# foreign-opts-c foreign-opts-c-exe
The secret is 11
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
packages: .
4 changes: 4 additions & 0 deletions cabal-testsuite/PackageTests/FFI/ForeignOptsC/cabal.test.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Test.Cabal.Prelude
main = cabalTest $ do
cabal "v2-build" ["foreign-opts-c-exe"]
withPlan $ runPlanExe "foreign-opts-c" "foreign-opts-c-exe" []
13 changes: 13 additions & 0 deletions cabal-testsuite/PackageTests/FFI/ForeignOptsC/cbits/clib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "clib.h"

#ifndef __TESTOPT_C__
#error "Did not get required __TESTOPT_C__ from cc-options"
#endif

#ifdef __TESTOPT_CXX__
#error "Got unexpected __TESTOPT_CXX__ from cxx-options"
#endif

int meaning_of_life_c() {
return __TESTOPT_C__;
}
6 changes: 6 additions & 0 deletions cabal-testsuite/PackageTests/FFI/ForeignOptsC/cbits/clib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef CLIB_H
#define CLIB_H

int meaning_of_life_c();

#endif
14 changes: 14 additions & 0 deletions cabal-testsuite/PackageTests/FFI/ForeignOptsC/foreign-opts-c.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cabal-version: 2.2
name: foreign-opts-c
version: 0.1
build-type: Simple

executable foreign-opts-c-exe
main-is: Main.hs
build-depends: base
default-language: Haskell2010
include-dirs: cbits
c-sources: cbits/clib.c
-- The following options are shared in all ForeignOpts cases:
cc-options: -D__TESTOPT_C__=11
cxx-options: -D__TESTOPT_CXX__=22
16 changes: 16 additions & 0 deletions cabal-testsuite/PackageTests/FFI/ForeignOptsCxx/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{-# LANGUAGE ForeignFunctionInterface #-}

module Main where

import Foreign.C (CInt (..))

foreign import ccall "cxxlib.h meaning_of_life_cxx"
meaning_of_life_cxx :: IO CInt

main :: IO ()
main = do
secret <- meaning_of_life_cxx
-- The value 22 comes from __TESTOPT_CXX__ - see the cabal file.
if (secret == 22)
then putStrLn ("The secret is " ++ show secret)
else error ("Expected value 22, got " ++ show secret)
7 changes: 7 additions & 0 deletions cabal-testsuite/PackageTests/FFI/ForeignOptsCxx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# ForeignOptsCxx

This asserts that cabal passes `cxx-options` to the C++ compiler (and NOT `cc-options`).

Since GHC 8.10, they are passed through GHC with `-optcxx`. Before that, they were passed with `-optc`.

See the additional case `ForeignOptsC`.
10 changes: 10 additions & 0 deletions cabal-testsuite/PackageTests/FFI/ForeignOptsCxx/cabal.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# cabal v2-build
Resolving dependencies...
Build profile: -w ghc-<GHCVER> -O1
In order, the following will be built:
- foreign-opts-cxx-0.1 (exe:foreign-opts-cxx-exe) (first run)
Configuring executable 'foreign-opts-cxx-exe' for foreign-opts-cxx-0.1..
Preprocessing executable 'foreign-opts-cxx-exe' for foreign-opts-cxx-0.1..
Building executable 'foreign-opts-cxx-exe' for foreign-opts-cxx-0.1..
# foreign-opts-cxx foreign-opts-cxx-exe
The secret is 22
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
packages: .
4 changes: 4 additions & 0 deletions cabal-testsuite/PackageTests/FFI/ForeignOptsCxx/cabal.test.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Test.Cabal.Prelude
main = cabalTest $ do
cabal "v2-build" ["foreign-opts-cxx-exe"]
withPlan $ runPlanExe "foreign-opts-cxx" "foreign-opts-cxx-exe" []
13 changes: 13 additions & 0 deletions cabal-testsuite/PackageTests/FFI/ForeignOptsCxx/cxxbits/cxxlib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "cxxlib.h"

#ifdef __TESTOPT_C__
#error "Got unexpected __TESTOPT_C__ from cc-options"
#endif

#ifndef __TESTOPT_CXX__
#error "Did not get required __TESTOPT_CXX__ from cxx-options"
#endif

int meaning_of_life_cxx() {
return __TESTOPT_CXX__;
}
12 changes: 12 additions & 0 deletions cabal-testsuite/PackageTests/FFI/ForeignOptsCxx/cxxbits/cxxlib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef CXXLIB_H
#define CXXLIB_H
#ifdef __cplusplus
extern "C" {
#endif

int meaning_of_life_cxx();

#ifdef __cplusplus
}
#endif
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cabal-version: 2.2
name: foreign-opts-cxx
version: 0.1
build-type: Simple

executable foreign-opts-cxx-exe
main-is: Main.hs
build-depends: base
default-language: Haskell2010
include-dirs: cxxbits
extra-libraries: stdc++
cxx-sources: cxxbits/cxxlib.cpp
-- The following options are shared in all ForeignOpts cases:
cc-options: -D__TESTOPT_C__=11
cxx-options: -D__TESTOPT_CXX__=22