(rule
 (target c-link-paths.txt)
 (enabled_if
  (= %{system} macosx))
 (action
  (with-stdout-to
   %{target}
   (echo "(-L/usr/local/lib/gcc/current -L/opt/homebrew/lib/gcc/current)"))))

(rule
 (target c-include-paths.txt)
 (enabled_if
  (= %{system} macosx))
 (action
  (with-stdout-to
   %{target}
   (echo "(-I/usr/local/include -I/opt/homebrew/include)"))))

(rule
 (target c-link-paths.txt)
 (enabled_if
  (and
   (<> %{system} macosx)
   (<> %{system} freebsd)))
 (action
  (with-stdout-to
   %{target}
   (echo "(-L/usr/local/lib)"))))

(rule
 (target c-include-paths.txt)
 (enabled_if
  (and
   (<> %{system} macosx)
   (<> %{system} freebsd)))
 (action
  (with-stdout-to
   %{target}
   (echo "(-I/opt/local/include -I/usr/local/include)"))))

(rule
 (target c-link-paths.txt)
 (enabled_if
  (or
   (= %{system} freebsd)))
 (action
  (with-stdout-to
   %{target}
   (echo
    "(-L/usr/local/lib -L/usr/local/lib/gcc12 -Wl,-rpath=/usr/local/lib/gcc12 -L/usr/local/lib/gcc13 -Wl,-rpath=/usr/local/lib/gcc13)"))))

(rule
 (target c-include-paths.txt)
 (enabled_if
  (or
   (= %{system} freebsd)))
 (action
  (with-stdout-to
   %{target}
   (echo "(-I/usr/local/include)"))))

(library
 (public_name gccjit)
 (name gccjit)
 (libraries ctypes.foreign)
 (modes byte native)
 (flags :standard -w -9-27)
 (ctypes
  (external_library_name libgccjit)
  (build_flags_resolver
   (vendored
    (c_flags
     -fPIC
     (:include c-include-paths.txt))
    (c_library_flags
     -lgccjit
     (:include c-link-paths.txt))))
  (headers
   (include "libgccjit.h"))
  (type_description
   (instance Types)
   (functor Gccjit_bindings_types))
  (function_description
   (concurrency unlocked)
   (instance Functions)
   (functor Gccjit_bindings))
  (generated_types Types_generated)
  (generated_entry_point C)))

(documentation)

(rule
 (alias upload-doc)
 ; This doesn't work, why? So, need to dune build @doc first.
 ; (deps (alias doc))
 (action
  (progn
   (run cp -rf ../_doc/_html ../../../doc/)
   (run git add -A ../../../doc)
   (run git commit ../../../doc -m "Update generated API docs")
   (run git push origin master))))
