How to specify flags in dune that is conditional on c compilers and platforms?

It may be a trivial question, but I can’t figure out how to write it, after reading a dune document and trial and error for a while.

e.g. I have a dune like this (simplified from torch/src/wrapper/dune, for this issue)

(library
 (foreign_stubs
  (language cxx)
  (names torch_api)
 (name torch_core)
 (public_name torch.core)
 (c_library_flags :standard -lstdc++)
 (flags
  :standard
  (:include flags.sexp))
 (libraries ctypes.foreign ctypes torch_bindings))

I want to add in the flags saying if using GCC, add a flag -Wincompatible-pointer-types and if using clang, add a flag -Wno-error=incompatible-function-pointer-types. One example on conditioning on platform e.g. macos or linux will also be helpful.

The general approach I have used is:

  • generate flags.sexp with a helper tool, and
  • pass as arguments to the tool: %{ocaml-config:c_compiler}, %{ocaml-config:ccomp_type}, %{ocaml-config:system}, %{ocaml-config:os_type}, etc (see Variables - Dune documentation) and any other variable that you need in order to decide which flags to pass.

The tool is then free to implement any desired logic using based on these variables as input.

Cheers,
Nicolas

1 Like

Thanks!

I thought of and saw the approach to use code to generate flags.sexp, but was just curious if it’s feasible with only dune s-exp.