Pass $CFLAGS to opam?

Hello,

TL;DR: is it possible to pass CFLAGS to opam install?

(There already are several threads about $CFLAGS but I still can’t make it work - sorry for asking this again).

Long story:

I’d like to checkout Bogue, which requires ctypes-foreign.
But opam install -y ctypes-foreign fails with the following message:

ffi.h: No such file or directory

File "src/ctypes-foreign/dune", line 25, characters 33-47:
25 |   (names dl_stubs ffi_call_stubs ffi_type_stubs foreign_threaded_stubs)
                                      ^^^^^^^^^^^^^^
(cd _build/default/src/ctypes-foreign && /bin/musl-gcc -O2 -fno-strict-aliasing -fwrapv -fPIC -pthread -D_FILE_OFFSET_BITS=64 -fdiagnostics-color=always -g -I /home/void/.opam/5.3.0+static/lib/ocaml -I /home/void/.opam/5.3.0+static/lib/bigarray-compat -I /home/void/.opam/5.3.0+static/lib/ctypes -I /home/void/.opam/5.3.0+static/lib/integers -I /home/void/.opam/5.3.0+static/lib/ocaml/threads -I /home/void/.opam/5.3.0+static/lib/ocaml/unix -I /home/void/.opam/5.3.0+static/lib/stdlib-shims -o ffi_type_stubs.o -c ffi_type_stubs.c)
ffi_type_stubs.c:14:10: fatal error: ffi.h: No such file or directory
   14 | #include <ffi.h>
      |          ^~~~~~~
compilation terminated.
File "src/ctypes-foreign/dune", line 25, characters 18-32:
25 |   (names dl_stubs ffi_call_stubs ffi_type_stubs foreign_threaded_stubs)
                       ^^^^^^^^^^^^^^
(cd _build/default/src/ctypes-foreign && /bin/musl-gcc -O2 -fno-strict-aliasing -fwrapv -fPIC -pthread -D_FILE_OFFSET_BITS=64 -fdiagnostics-color=always -g -I /home/void/.opam/5.3.0+static/lib/ocaml -I /home/void/.opam/5.3.0+static/lib/bigarray-compat -I /home/void/.opam/5.3.0+static/lib/ctypes -I /home/void/.opam/5.3.0+static/lib/integers -I /home/void/.opam/5.3.0+static/lib/ocaml/threads -I /home/void/.opam/5.3.0+static/lib/ocaml/unix -I /home/void/.opam/5.3.0+static/lib/stdlib-shims -o ffi_call_stubs.o -c ffi_call_stubs.c)
ffi_call_stubs.c:22:10: fatal error: ffi.h: No such file or directory
   22 | #include <ffi.h>
      |          ^~~~~~~
compilation terminated.

However, ffi.h is available in /usr/include.
The above error messages seems to indicate the C compiler isn’t looking in /usr/include (because there is no -I /usr/include).

my attempts:

CFLAGS=-I/usr/include opam install -y ctypes-foreign
still doesn’t include /usr/include. The flag is ignored.

opam option setenv+='CLFAGS="-I/usr/include"'
doesn’t include /usr/include either. The flag is ignored too.

opam install --help
doesn’t list any environment variables I could use for including the directory.

debug info

Summary
$ opam var

<><> Global opam variables ><><><><><><><><><><><><><><><><><><><><><><><><><><>
arch              x86_64           # Inferred from system
exe                                # Suffix needed for executable filenames (Windows)
jobs              7                # The number of parallel jobs set up in opam configuration
make              make             # The 'make' command to use
opam-version      2.3.0            # The currently running opam version
os                linux            # Inferred from system
os-distribution   void             # Inferred from system
os-family         void             # Inferred from system
os-version        rolling          # Inferred from system
root              /home/void/.opam # The current opam root directory
switch            5.3.0+static     # The identifier of the current switch
sys-ocaml-arch    x86_64           # Target architecture of the OCaml compiler present on your
                                     system
sys-ocaml-cc      cc               # Host C Compiler type of the OCaml compiler present on your
                                     system
sys-ocaml-libc    libc             # Host C Runtime Library type of the OCaml compiler present on
                                     your system
sys-ocaml-system  linux            # Target system of the OCaml compiler present on your system
sys-ocaml-version 5.3.0            # OCaml version present on your system independently of opam,
                                     if any

<><> Configuration variables from the current switch ><><><><><><><><><><><><><>
prefix   /home/void/.opam/5.3.0+static
lib      /home/void/.opam/5.3.0+static/lib
bin      /home/void/.opam/5.3.0+static/bin
sbin     /home/void/.opam/5.3.0+static/sbin
share    /home/void/.opam/5.3.0+static/share
doc      /home/void/.opam/5.3.0+static/doc
etc      /home/void/.opam/5.3.0+static/etc
man      /home/void/.opam/5.3.0+static/man
toplevel /home/void/.opam/5.3.0+static/lib/toplevel
stublibs /home/void/.opam/5.3.0+static/lib/stublibs
user     void
group    void

Is there anything else I could try to successfully install ctypes-foreign?

In general that’s not the best way to extend the lookup path of your C compiler, it requires the build system to supporting it.

You should set LIBRARY_PATH and C_INCLUDE_PATH which is made for end-users to configure the C toolchain:

LIBRARY_PATH=/usr/lib C_INCLUDE_PATH=/usr/include opam install -y ctypes-foreign

See this issue in opam

1 Like

Thanks a lot! I’m impressed how short your solution is (an how long my problem lasted) :slightly_smiling_face:

1 Like

If you are on a system which installs stuff to non-standard locations (as in unknown to your host C toolchain) you may want to add that to your .profile or .shellrc. E.g. I have something like this in my .zshrc:

export LIBRARY_PATH=$HOMEBREW_PREFIX/lib:$LIBRARY_PATH
export C_INCLUDE_PATH=$HOMEBREW_PREFIX/include:$C_INCLUDE_PATH

Thx, that’s exactly what I did know.