(env
 (dev
  (flags
   (:standard -w -6-32-39))))

(library
 (name zarith)
 (public_name zarith)
 (modules z q big_int_Z zarith_version)
 (wrapped false)
 (foreign_stubs
  (language c)
  (names caml_z)
  (flags
   :standard
   (:include cflags.sexp)))
 (c_library_flags
  (:include libs.sexp)))

(executable
 (name configure_env)
 (modules configure_env))

(rule
 (target Makefile)
 (deps configure env)
 (action
  (bash
   "env %{read:env} ./configure --ocamllibdir %{ocaml-config:standard_library}")))

(rule
 (target env)
 (action
  (copy gmp.%{lib-available:gmp} env)))

(rule
 (target gmp.true)
 (deps
  (:exe configure_env.exe)
  %{lib:gmp:libgmp.a}
  %{lib:gmp:libgmp.so}
  %{lib:gmp:gmp.h})
 (action
  (with-stdout-to
   %{target}
   (run %{exe} --cc "%{cc}" --with-gmp=%{lib:gmp:libgmp.a}))))

(rule
 (target gmp.false)
 (deps
  (:exe configure_env.exe))
 (action
  (with-stdout-to
   %{target}
   (run %{exe} --cc "%{cc}" --with-conf-gmp))))

(rule
 (target cflags.sexp)
 (deps Makefile)
 (action
  (with-stdout-to
   %{target}
   (progn
    (bash "echo -n '('")
    (bash "cat Makefile | sed -n -e 's/CFLAGS=//p'")
    (bash "echo -n ')'")))))

; Note that the order (LDFLAGS, then LIBS) is important below since
; zarith uses pkg-config to detect gmp, and adds the output to LIBS
; but we like -L ..._build/solo5/duniverse/Zarith/../../../install/solo5/lib/gmp
; first, followed by -L/usr/local/lib -lgmp (from pkg-config)

(rule
 (target libs.sexp)
 (deps Makefile)
 (action
  (with-stdout-to
   %{target}
   (progn
    (bash "echo -n '('")
    (bash "cat Makefile | sed -n -e 's/LDFLAGS=//p'")
    (bash "cat Makefile | sed -n -e 's/LIBS=//p'")
    (bash "echo -n ')'")))))

(rule
 (deps META)
 (action
  (with-stdout-to
   zarith_version.ml
   (progn
    (run echo "let")
    (bash "grep \"version\" META | head -1")))))

(library
 (name zarith_top)
 (optional)
 (public_name zarith.top)
 (modules zarith_top)
 (libraries zarith compiler-libs.toplevel))
