Skip to content

Setup: pass include paths and CUDA macros to c2hs#87

Open
mgajda wants to merge 1 commit into
tmcdonell:masterfrom
mgajda:fix-setup-c2hs-includes
Open

Setup: pass include paths and CUDA macros to c2hs#87
mgajda wants to merge 1 commit into
tmcdonell:masterfrom
mgajda:fix-setup-c2hs-includes

Conversation

@mgajda
Copy link
Copy Markdown

@mgajda mgajda commented Apr 14, 2026

Branch: fix/setup-c2hs-includes
Commit: Setup: pass include paths and CUDA macros to c2hs


On Ubuntu 24.04 (and other distributions where the CUDA toolkit installs to a non-standard layout), two separate build failures occur during the c2hs preprocessing stage.

Failure 1cbits/stubs.h not found:

fatal error: cbits/stubs.h: No such file or directory
c2hs: Error during preprocessing custom header file

The .chs files use #include "cbits/stubs.h", which requires the package root to be on the c2hs include path. The current Setup.hs does not add it.

Failure 2CUDA_INSTALL_PATH undefined in Foreign.CUDA.Path:

c2hs: C header contains errors:
Cannot find a definition for `CUDA_INSTALL_PATH' in the header file.

Foreign.CUDA.Path uses {#const CUDA_INSTALL_PATH#}. The macro is passed to GHC via -optc-D but not forwarded to c2hs via --cppopts, so c2hs cannot resolve it.

The fix extends the x-extra-c2hs-options generated by libraryBuildInfo to include:

  • --cppopts=-I. and --cppopts=-Icbits so relative includes from .chs files resolve
  • --cppopts=-I<cuda_include_dir> from the detected CUDA installation
  • --cppopts=-DCUDA_INSTALL_PATH=... and --cppopts=-DCUDA_LIBRARY_PATH=...

Tested: c2hs 0.28.8, GHC 9.4.7, CUDA 12.0 and 13.0, Ubuntu 24.04.

On distributions where CUDA headers are not in c2hs's default search path
(e.g. Ubuntu 24.04 installs to /usr/include rather than /usr/local/cuda/include),
or where the working directory at c2hs invocation does not match the package
root, two separate failures occur:

1. cbits/stubs.h not found during preprocessing:

       fatal error: cbits/stubs.h: No such file or directory

   The .chs files use `#include "cbits/stubs.h"` which requires the package
   root to be on the c2hs include path.

2. CUDA_INSTALL_PATH undefined in Foreign.CUDA.Path:

       Cannot find a definition for `CUDA_INSTALL_PATH' in the header file.

   Foreign.CUDA.Path uses `{#const CUDA_INSTALL_PATH#}`, but the macro is
   only passed to GHC via `-optc-D`, not forwarded to c2hs `--cppopts`.

Fix: extend the `x-extra-c2hs-options` emitted by `libraryBuildInfo` to
include:

  * `--cppopts=-I.` and `--cppopts=-Icbits` for the package root and cbits/
  * `--cppopts=-I\<cuda_include_dir\>` from the detected CUDA installation
  * `--cppopts=-DCUDA_INSTALL_PATH=...` and `--cppopts=-DCUDA_LIBRARY_PATH=...`

Tested: c2hs 0.28.8, GHC 9.4.7, CUDA 12.0 and 13.0, Ubuntu 24.04.
@tomsmeding
Copy link
Copy Markdown
Collaborator

  1. Why would the working directory when invoking c2hs not be the repo path? When building with cabal, this should happen correctly automatically. How are you building the package?
  2. I have never seen this error before and your diagnosis sounds like it should prevent the cuda package from ever building. It definitely does, on Ubuntu. Again: how are you building the package?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants