# relaxed TEXTREL check for all 32-bit architectures for system with installed rpm-build-ocaml
# https://github.com/ocaml/ocaml/issues/9800
%_verify_elf_method %{?_is_ilp32:textrel=relaxed}%{!?_is_ilp32:default}

%ocaml_native_arch x86_64 riscv64 aarch64

# Version of OCaml with which the rpm-build-ocaml package was built. Used to
# detect an ocaml version transition (bootstrap).
%_ocaml_sver 5.4.1

# Bootstrap auto-detect: 1 when the installed ocaml is newer than the one
# rpm-build-ocaml was built with (version transition), else 0.
# Consume BY VALUE: %if 0%{?ocaml_bootstrap} ... %endif
# The 0%{?...} guard keeps it valid even if the macro is somehow undefined
# (bare `%if %ocaml_bootstrap` is a parse error when undefined).
# NOT via %if_enabled/%if_with: those test macro *definedness*, and this macro
# is always defined (a macros.d line cannot conditionally define/undefine at
# load — only its value is computed, on expansion).
%ocaml_bootstrap %([ "$(rpmvercmp %{_ocaml_sver} %{ocaml_version})" = "-1" ] && echo 1 || echo 0)

%_ocamldir %_libdir/ocaml
%_ocaml_req_skip	""
# %add_ocaml_req_skip NAME drops generated requires matching NAME. Matches both
# cmi/cmx module requires (raw "... NAME" lines) and findlib META requires
# ("ocaml-meta(NAME)"). Affects the requires stream only; provides are kept.
%add_ocaml_req_skip()	%global _ocaml_req_skip %_ocaml_req_skip ([[:space:]]%*$|[(]%*[)]$)
%__dune %_bindir/dune
%dune_build %__dune build --verbose %_smp_mflags

%ocaml_find_files \
	__ocaml_find_files() \
	{ \
find %buildroot%_ocamldir/ -type f -regextype posix-extended \\\( \\\
                -regex '.*\\\.(cmo|cma|cmi|cmxs|so|js)$' \\\
                -o -name 'lib*.a' \\\
                -o -name 'META' \\\
                -o -name 'dune-package' \\\
                \\\) -printf '%_ocamldir/%%P\\\n' >ocaml-files.runtime; \
find %buildroot%_ocamldir/ -type d -printf '%%%%dir %_ocamldir/%%P\\\n' >>ocaml-files.runtime; \
find %buildroot%_ocamldir/ -type f -regextype posix-extended \\\( \\\
                -regex '.*\\\.(o|cmt|cmti|cmx|cmxa|ml|mli|exe)$' \\\
                -o \\\( -name '*.a' -a ! -name 'lib*.a' \\\) \\\
                -o -name 'opam' \\\
                \\\) -printf '%_ocamldir/%%P\\\n' >ocaml-files.devel; \
	} \
	__ocaml_find_files


%dune_install \
	__dune_install() \
	{ \
		%__dune install --destdir=%buildroot "$@"; \
		%ocaml_find_files \
	} \
	__dune_install

%dune_check %__dune runtest

# Generate per-package file listings for multi-package dune projects
# Usage: %ocaml_find_files_multi [pkg1 pkg2 pkg3 ...]
# Creates: ocaml-files.runtime.pkg1, ocaml-files.devel.pkg1, ...
%ocaml_find_files_multi OCAMLDIR=%_ocamldir BINDIR=%_bindir DATADIR=%_datadir BUILDROOT=%buildroot %_rpmlibdir/ocaml-find-files-multi

# Alternative dune_install for multi-package projects
# Usage: %dune_install_multi [pkg1 pkg2 pkg3 ...]
# Creates per-package file listings instead of single ocaml-files.runtime/devel
%dune_install_multi() %__dune install --destdir=%buildroot %* ; %ocaml_find_files_multi %*

