c{c,xx}-options are ignored if source files are listed in the wrong c{,xx}-sources field
- Dominant language
- Haskell
- Stars
- 1.7k
- Forks
- 750
- Avg merge
- 4d 3h
- Merged PRs (30d)
- 28
Description
**Describe the bug**
If you accidentally list your source files under the wrong type, they still get built, but the requested `cc-options` and `cxx-options` fields are silently ignored.
**To Reproduce**
Steps to reproduce the behavior:
Put this in `t.cabal`:
```
cabal-version: 3.0
version: 0.1
name: t
build-type: Simple
executable t
main-is: Main.hs
build-depends: base ^>=4.14.1.0
cxx-sources: c.c cpp.cpp
ghc-options: -v5
cc-options: -DCC_OPTIONS_USED
cxx-options: -DCXX_OPTIONS_USED
```
Then put `main = pure ()` in `Main.hs` and create empty `c.c` and `cpp.cpp` files. Finally, run:
$ cabal v2-build all 2>&1 | grep 'gcc.* cp*.cp* '
You can observe that `cpp.cpp` gets built with `-DCXX_OPTIONS_USED`, but `c.c` does not get built with either `-DCC_OPTIONS_USED` or `-DCXX_OPTIONS_USED`. If you change `cxx-sources` to `c-sources` in the cabal file, the opposite will be true: `cpp.cpp` does not get built with either option, but `c.c` gets built with `-DCC_OPTIONS_USED`.
**Expected behavior**
I think there are three sensible options here:
1. Always pass `cc-options` when compiling files listed in `c-sources` (even if they're not actually C), and always pass `cxx-options` when compiling files listed in `cxx-sources` (even if they're not actually C++).
2. Print an error if the internal file-type detection is a mismatch with the declared file type, i.e. if there is a C++ file listed in `c-sources` or a C file listed in `cxx-sources`.
3. Always pass `cc-options` when the internal file-type detection sees a C file, and always pass `cxx-options` when the internal file-type detection sees a C++ file.
Choice (1) seems like the most flexible, but choices (1) and (3) seem like they make it way too easy to shoot yourself in the foot. I would prefer (2).
**System information**
- cabal-install 3.8.1.0, cabal 3.8.1.0
- Linux 5.14, ghc 8.10.4
Contributor guide
Assessment
This issue has not been assessed yet.