From ff67aa29656073443b741ffd42bf1ad305d62e1b Mon Sep 17 00:00:00 2001
From: Jerry James <loganjerry@gmail.com>
Date: Apr 06 2023 16:24:14 +0000
Subject: Update OCaml Guidelines


- Modernize example spec file and discussion of its contents
- Remove obsolete discussion of internal dependency generator
- Add example spec files that use dune and topkg
- Use modern RPM macros
- Add semantic line breaks

---

diff --git a/guidelines/modules/ROOT/examples/ocaml-dune-example.spec b/guidelines/modules/ROOT/examples/ocaml-dune-example.spec
new file mode 100644
index 0000000..79ec39e
--- /dev/null
+++ b/guidelines/modules/ROOT/examples/ocaml-dune-example.spec
@@ -0,0 +1,69 @@
+%undefine _package_note_flags
+
+%ifnarch %{ocaml_native_compiler}
+%global debug_package %{nil}
+%endif
+
+Name:           ocaml-foolib
+Version:        1.2.3
+Release:        %autorelease
+Summary:        OCaml library for fooing bars
+
+License:        LGPL-2.1-or-later
+URL:            https://www.example.com/foolib
+Source:         https://www.example.com/foolib-%{version}.tar.gz
+
+BuildRequires:  ocaml
+BuildRequires:  ocaml-dune
+
+%description
+OCaml library for fooing bars.  This library can also foo bazes.
+
+
+%package        devel
+Summary:        Development files for %{name}
+Requires:       %{name}%{?_isa} = %{version}-%{release}
+
+
+%description    devel
+The %{name}-devel package contains libraries and signature files for
+developing applications that use %{name}.
+
+
+%prep
+%autosetup -n foolib-%{version}
+
+
+%build
+# Build all installable targets
+%dune_build
+# Build a specific set of targets
+%dune_build -p bazzer,boffer
+# Build non-default targets
+%dune_build @install @doc
+
+
+%install
+# Install all installable targets
+%dune_install
+# Install a specific set of targets
+%dune_install bazzer boffer
+
+
+%check
+# Check all installable targets
+%dune_check
+# Check a specific set of targets
+%dune_check -p bazzer,boffer
+
+
+%files -f .ofiles
+%doc README
+%license LICENSE
+
+
+%files devel -f .ofiles-devel
+
+
+%changelog
+%autochangelog
diff --git a/guidelines/modules/ROOT/examples/ocaml-example.spec b/guidelines/modules/ROOT/examples/ocaml-example.spec
index ff8f69b..02591d8 100644
--- a/guidelines/modules/ROOT/examples/ocaml-example.spec
+++ b/guidelines/modules/ROOT/examples/ocaml-example.spec
@@ -1,21 +1,20 @@
-%global opt %(test -x %{_bindir}/ocamlopt && echo 1 || echo 0)
+%undefine _package_note_flags
+
+%ifnarch %{ocaml_native_compiler}
 %global debug_package %{nil}
+%endif
 
 Name:           ocaml-foolib
 Version:        1.2.3
-Release:        1%{?dist}
+Release:        %autorelease
 Summary:        OCaml library for fooing bars
 
 License:        LGPL-2.1-or-later
 URL:            https://www.example.com/foolib
-Source:         https://www.example.com/foolib-1.2.3.tar.gz
-BuildRoot:      %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
+Source:         https://www.example.com/foolib-%{version}.tar.gz
 
-BuildRequires:  ocaml, ocaml-findlib-devel
-
-%global _use_internal_dependency_generator 0
-%global __find_requires /usr/lib/rpm/ocaml-find-requires.sh
-%global __find_provides /usr/lib/rpm/ocaml-find-provides.sh
+BuildRequires:  ocaml
+BuildRequires:  ocaml-findlib
 
 %description
 OCaml library for fooing bars.  This library can also foo bazes.
@@ -23,7 +22,7 @@ OCaml library for fooing bars.  This library can also foo bazes.
 
 %package        devel
 Summary:        Development files for %{name}
-Requires:       %{name} = %{version}-%{release}
+Requires:       %{name}%{?_isa} = %{version}-%{release}
 
 
 %description    devel
@@ -32,52 +31,46 @@ developing applications that use %{name}.
 
 
 %prep
-%setup -q -n foolib-%{version}
-# You may need a ./configure step here.
+%autosetup -n foolib-%{version}
 
 
 %build
+# You may need a ./configure step here.
 make byte
-%if %opt
+%ifarch %{ocaml_native_compiler}
 make opt
 %endif
 
 
 %install
 # These rules work if the library uses 'ocamlfind install' to install itself.
-export DESTDIR=$RPM_BUILD_ROOT
-export OCAMLFIND_DESTDIR=$RPM_BUILD_ROOT%{_libdir}/ocaml
-mkdir -p $OCAMLFIND_DESTDIR $OCAMLFIND_DESTDIR/stublibs
-make install
-
-
-%clean
-rm -rf $RPM_BUILD_ROOT
+export OCAMLFIND_DESTDIR=%{buildroot}%{ocamldir}
+mkdir -p $OCAMLFIND_DESTDIR/stublibs
+%make_install
 
 
 %files
 %doc README
-%{_libdir}/ocaml/foolib
-%if %opt
-%exclude %{_libdir}/ocaml/foolib/*.a
-%exclude %{_libdir}/ocaml/foolib/*.cmxa
-%exclude %{_libdir}/ocaml/foolib/*.cmx
+%license LICENSE
+%{ocamldir}/foolib
+%ifarch %{ocaml_native_compiler}
+%exclude %{ocamldir}/foolib/*.a
+%exclude %{ocamldir}/foolib/*.cmxa
+%exclude %{ocamldir}/foolib/*.cmx
 %endif
-%exclude %{_libdir}/ocaml/foolib/*.mli
-%{_libdir}/ocaml/stublibs/*.so
-%{_libdir}/ocaml/stublibs/*.so.owner
+%exclude %{ocamldir}/foolib/*.mli
+%{ocamldir}/stublibs/*.so
+%{ocamldir}/stublibs/*.so.owner
 
 
 %files devel
-%doc README
-%if %opt
-%{_libdir}/ocaml/foolib/*.a
-%{_libdir}/ocaml/foolib/*.cmxa
-%{_libdir}/ocaml/foolib/*.cmx
+%ifarch %{ocaml_native_compiler}
+%{ocamldir}/foolib/*.a
+%{ocamldir}/foolib/*.cmxa
+%{ocamldir}/foolib/*.cmx
 %endif
-%{_libdir}/ocaml/foolib/*.mli
+%{ocamldir}/foolib/*.mli
 
 
 %changelog
-* Sat May 26 2007 Your Name <you@example.com> - 1.2.3-1
-- Initial RPM release.
+%autochangelog
diff --git a/guidelines/modules/ROOT/examples/ocaml-topkg-example.spec b/guidelines/modules/ROOT/examples/ocaml-topkg-example.spec
new file mode 100644
index 0000000..7d5d969
--- /dev/null
+++ b/guidelines/modules/ROOT/examples/ocaml-topkg-example.spec
@@ -0,0 +1,74 @@
+%undefine _package_note_flags
+
+%ifnarch %{ocaml_native_compiler}
+%global debug_package %{nil}
+%endif
+
+Name:           ocaml-foolib
+Version:        1.2.3
+Release:        %autorelease
+Summary:        OCaml library for fooing bars
+
+License:        LGPL-2.1-or-later
+URL:            https://www.example.com/foolib
+Source:         https://www.example.com/foolib-%{version}.tar.gz
+
+BuildRequires:  ocaml
+BuildRequires:  ocaml-findlib
+BuildRequires:  ocaml-topkg-devel
+BuildRequires:  python3
+
+%description
+OCaml library for fooing bars.  This library can also foo bazes.
+
+
+%package        devel
+Summary:        Development files for %{name}
+Requires:       %{name}%{?_isa} = %{version}-%{release}
+
+
+%description    devel
+The %{name}-devel package contains libraries and signature files for
+developing applications that use %{name}.
+
+
+%prep
+%autosetup -n foolib-%{version}
+
+# Enable debuginfo if upstream does not
+echo true: debug >> _tags
+
+
+%build
+ocaml pkg/pkg.ml build --tests true
+
+
+%install
+mkdir -p %{buildroot}%{ocamldir}/foolib
+cp -p _build/{opam,pkg/META} %{buildroot}%{ocamldir}/foolib
+%ifarch %{ocaml_native_compiler}
+cp -a _build/src/*.{a,cma,cmi,cmt,cmti,cmx,cmxa,cmxs,mli} \
+  %{buildroot}%{ocamldir}/foolib
+%else
+cp -a _build/src/*.{cma,cmi,cmt,cmti,mli} %{buildroot}%{ocamldir}/foolib
+%endif
+
+# This macro requires python3 in the buildroot
+%ocaml_files
+
+
+%check
+ocaml pkg/pkg.ml test
+
+
+%files -f .ofiles
+%doc README
+%license LICENSE
+
+
+%files devel -f .ofiles-devel
+%ifarch %{ocaml_native_compiler}
+
+
+%changelog
+%autochangelog
diff --git a/guidelines/modules/ROOT/pages/OCaml.adoc b/guidelines/modules/ROOT/pages/OCaml.adoc
index 69ddbf0..565a9eb 100644
--- a/guidelines/modules/ROOT/pages/OCaml.adoc
+++ b/guidelines/modules/ROOT/pages/OCaml.adoc
@@ -1,27 +1,22 @@
 = OCaml Packaging Guidelines
 
-This document seeks to document the conventions and customs surrounding the proper packaging of OCaml modules in Fedora. It does not intend to cover all situations, but to codify those practices which have served the Fedora OCaml community well.
+This document seeks to document the conventions and customs surrounding the proper packaging of OCaml modules in Fedora.
+It does not intend to cover all situations, but to codify those practices which have served the Fedora OCaml community well.
 
 == Naming
 
 The base OCaml compiler is called ocaml.
 
-OCaml modules, libraries and syntax extensions should be named ocaml-foo. Examples include: ocaml-extlib, ocaml-ssl.
+OCaml modules, libraries and syntax extensions should be named ocaml-foo.
+Examples include: ocaml-extlib, ocaml-ssl.
 
-This naming does not apply to applications written in OCaml, which can be given their normal name. Examples include: mldonkey, virt-top, cduce.
+This naming does not apply to applications written in OCaml, which can be given their normal name.
+Examples include: coccinelle, frama-c, virt-top.
 
 Rationale: this is how they are named in other distros (Debian, PLD) and this is consistent with Perl / PHP / Python naming.
 
 == Packaging libraries
 
-The following is an example specfile for an imaginary OCaml library called _foolib_.
-
-.ocaml-example.spec
-[source]
-----
-include::{examplesdir}/ocaml-example.spec[]
-----
-
 === Main package
 
 In order to allow OCaml scripts and the toplevel to use a library, the main package should contain only files matching:
@@ -31,17 +26,17 @@ In order to allow OCaml scripts and the toplevel to use a library, the main pack
 * *.so (if present, contains OCaml <-> C stubs)
 * META (the findlib description)
 * *.so.owner (if present, used by findlib)
-* a license file (if present) marked %doc
+* a license file (if present) marked %license
 
-* .cmo files are not normally included. There are two exceptions where *.cmo files may be included:
-* if file is needed for link (like gtkInit.cmo in lablgtk or std_exit.cmo in OCaml itself), then it must be included to allow the library to be linked properly.
-* if the cmo file is a camlp4 preprocessor (like Camlp4OCamlPrinter.cmo in OCaml), then it must be included because otherwise the syntax extension would not be available.
+* .cmo files are not normally included. There is one exception where *.cmo files may be included: if the cmo file is needed to link, then it must be included to allow the library to be linked properly.
 
-If the package contains *.so files, then they should have rpaths removed, as per Fedora packaging guidelines.
+If the package contains *.so files, then they should not have rpaths, as per Fedora packaging guidelines.
 
-The packager should check the META file footnote:[http://projects.camlcity.org/projects/dl/findlib-1.9.1/doc/ref-html/r759.html[findlib Reference Manual - META files.]]. If there is no META file, then the packager should create one, include it in the package, and pass it to the upstream maintainer.
+The packager should check the META file footnote:[http://projects.camlcity.org/projects/dl/findlib-1.9.5/doc/ref-html/r759.html[findlib Reference Manual - META files.]].
+If there is no META file, then the packager should create one, include it in the package, and pass it to the upstream maintainer.
 
-Rationale: OCaml does not support dynamic linking of binaries, and even if it did with the current module hash system for expressing strict typing requirements almost any conceivable change to a library would require the binary to be recompiled. OCaml scripts are the closest we come to dynamic linking, in as much as they do not usually depend on a specific version of a library (albeit this only works because the scripts are recompiled each time they run).
+Rationale: OCaml does not support dynamic linking of binaries, and even if it did with the current module hash system for expressing strict typing requirements almost any conceivable change to a library would require the binary to be recompiled.
+OCaml scripts are the closest we come to dynamic linking, in as much as they do not usually depend on a specific version of a library (albeit this only works because the scripts are recompiled each time they run).
 
 === -devel subpackage
 
@@ -52,17 +47,21 @@ The -devel subpackage of a library should contain all other files required to al
 * *.cmx (if present, allows cross-module optimizations)
 * *.mli (contains the signature of the library)
 
-* .o files are not normally included. There is however one exception -- if file is needed for link (like gtkInit.cmx and gtkInit.o in lablgtk or std_exit.cmx and std_exit.o in OCaml itself), then it should be included.
+* .o files are not normally included. There is one exception -- if the file is needed to link (like std_exit.cmx and std_exit.o in OCaml itself), then it should be included.
 
 * .ml files are not normally included. The exception is if the file describes a module signature _and_ there is no corresponding .mli file, then the .ml file should be included. (Note that Debian is more permissive and they often distribute *.ml files, allowing the programmer to peek at the implementation of a module).
 
-Documentation, examples and other articles which are useful to the developer may be included in the -devel sub-package. The license file (which is in the main package) does not need to be included again in the -devel subpackage.
+Documentation, examples and other articles which are useful to the developer may be included in the -devel sub-package.
+The license file (which is in the main package) does not need to be included again in the -devel subpackage.
 
 If the -devel subpackage would only contain documentation files, then the packager may at their discretion place the documentation files in the main package and not have a -devel subpackage at all.
 
-The -devel subpackage should require the exact name-version-release of the main package (as per Fedora policy). It should also require any C libraries required for development, and sometimes this means an explicit 'Requires' is needed. For example, ocaml-pcre-devel needs an explicit 'Requires: pcre-devel' to make it usable for development.
+The -devel subpackage should require the exact name-version-release of the main package (as per Fedora policy).
+It should also require any C libraries required for development, and sometimes this means an explicit 'Requires' is needed.
+For example, ocaml-pcre-devel needs an explicit 'Requires: pcre-devel' to make it usable for development.
 
-Rationale for inclusion of all cmx files: [*.cmx files] are needed even for module included in .cmxa libraries in order to enable cross-module optimizations (inlining, constant propagation and direct function calls). The .o files are not needed. [From a private email from Alain Frisch]
+Rationale for inclusion of all cmx files: [*.cmx files] are needed even for modules included in .cmxa libraries in order to enable cross-module optimizations (inlining, constant propagation and direct function calls).
+The .o files are not needed. [From a private email from Alain Frisch]
 
 === -doc subpackage
 
@@ -83,15 +82,12 @@ Similarly for each module that library A may provide to other libraries, library
 A library must depend on the precise version of the OCaml compiler, for example:
 ocaml(runtime) = 3.10.0
 
-There are two scripts in the base ocaml package which automatically calculate the right Requires and Provides for a library. To use them, just add the following to the spec file:
+The correct Requires and Provides should be generated automatically.
 
-....
-%global _use_internal_dependency_generator 0
-%global __find_requires /usr/lib/rpm/ocaml-find-requires.sh
-%global __find_provides /usr/lib/rpm/ocaml-find-provides.sh
-....
-
-Rationale: OCaml does not offer binary compatibility between releases of the compiler (even between bugfixes). Furthermore the module system uses a hash over the interface and some internals of a module which basically means a library or program must be linked against the identical modules it was compiled with. The Requires and Provides lines express the module name and hash so that RPM enforces the same requirements as the OCaml linker itself. Please see the further reading at the end of this page for more details.
+Rationale: OCaml does not offer binary compatibility between releases of the compiler (even between bugfixes).
+Furthermore the module system uses a hash over the interface and some internals of a module which basically means a library or program must be linked against the identical modules it was compiled with.
+The Requires and Provides lines express the module name and hash so that RPM enforces the same requirements as the OCaml linker itself.
+Please see the further reading at the end of this page for more details.
 
 == Packaging binaries
 
@@ -103,7 +99,9 @@ However if the OCaml package also contains a library, then you should follow the
 
 Binaries should be stripped, as per ordinary Fedora packaging guidelines.
 
-There is one exception where a binary should not be stripped. If the package was compiled with ocamlc -custom then the package contains bytecode which strip will remove, thus rendering the binary inoperable. It is easy to test for this: If after stripping, any attempt to run the binary results in the message _No bytecode file specified_ then the binary is compiled like this and should not be stripped.
+There is one exception where a binary should not be stripped.
+If the package was compiled with `ocamlc -custom` then the package contains bytecode which strip will remove, thus rendering the binary inoperable.
+It is easy to test for this: if after stripping, any attempt to run the binary results in the message _No bytecode file specified_ then the binary is compiled like this and should not be stripped.
 
 Rationale: https://bugs.debian.org/256900
 
@@ -113,20 +111,17 @@ The packager should attempt to ship native code compiled binaries in preference 
 
 == Bytecode-only architectures
 
-The OCaml native code compiler (ocamlopt) contains code generators for popular architectures, but not for every architecture that Fedora might support. On such architectures, the spec file should still build bytecode libraries and binaries.
-
-To test for presence of the native compiler, do:
-
-....
-%global opt %(test -x %{_bindir}/ocamlopt && echo 1 || echo 0)
-....
+The OCaml native code compiler (ocamlopt) contains code generators for popular architectures, but not for every architecture that Fedora might support.
+On such architectures, the spec file should still build bytecode libraries and binaries.
 
-then define conditional sections in %build, %install and %files if necessary. For example:
+To test for presence of the native compiler, use the `%{ocaml_native_compiler}` macro.
+Define conditional sections in %build, %install and %files if necessary.
+For example:
 
 ....
 %build
 make byte
-%if %opt
+%ifarch %{ocaml_native_compiler}
 make opt
 %endif
 ....
@@ -147,7 +142,71 @@ The following files should not normally be distributed:
 
 If a security issue arises in an OCaml library, then all libraries and binaries which depend on it must be recompiled.
 
-OCaml scripts do not need to be changed (unless resolving the security issue requires changing the public interface to the library and the script is broken by the change). This is because OCaml scripts are recompiled each time they run.
+OCaml scripts do not need to be changed (unless resolving the security issue requires changing the public interface to the library and the script is broken by the change).
+This is because OCaml scripts are recompiled each time they run.
+
+== RPM Macros
+
+The following macros are available to use in spec files:
+* `%{ocaml_native_compiler}`: the architectures for which native compilation is available
+* `%{ocaml_natdynlink}`: the architectures for which native dynamic linking is available
+* `%{ocamldir}`: top-level installation directory for OCaml packages, currently equivalent to `%{_libdir}/ocaml`
+* `%{ocaml_files}`: generate a list of installed files, in files named .ofiles (for the main package) and .ofiles-devel (for the devel subpackage), unless `-s` or `-n` is given.  This macro requires that python3 be available in the buildroot.  Flags:
+    * -n: there is no devel subpackage.  All files are listed in .ofiles.
+    * -s: separate installation; each subdirectory of `%{ocamldir}` is a separate RPM package.  For each subdirectory, `.ofiles-<subdir>` and `.ofiles-<subdir>-devel` is generated (unless `-n` is also given).
+
+== Examples
+
+This section contains example spec files illustrating how to build OCaml library and binary packages with various build tools.
+
+=== Dune
+
+Dune is a popular build tool for OCaml packages.
+RPM macros are available to make building with dune simple.
+* `%dune_build`: Invoke dune to build all installable artifacts in release mode.  Flags:
+    * -j <number>: number of jobs that can be run in parallel.  This is automatically set to `%{?_smp_mflags}`, so is typically used only to eliminate parallelism with `-j 1`.
+    * -p <modules>: tell dune to build the comma-separated list of modules only, rather than every installable artifact.
+    * --: separate flags for this macro from flags to pass to dune
+* `%dune_install`: Invoke dune to install all installable artifacts.  Flags:
+    * -n: there is no devel subpackage.  All files are associated with the main package.
+    * -s: separate installation; each subdirectory of `%{ocamldir}` is a separate RPM package.  Otherwise, all files are associated with a single main package.
+    * --: separate flags for this macro from flags to pass to dune
+* `%dune_check`: Invoke dune to run tests for all installable artifacts.  Flags:
+    * -j <number>: number of jobs that can be run in parallel.  This is automatically set to `%{?_smp_mflags}`, so is typically used only to eliminate parallelism with `-j 1`.
+    * -p <modules>: tell dune to build the comma-separated list of modules only, rather than every installable artifact.
+    * --: separate flags for this macro from flags to pass to dune
+* `%odoc_package`: Declare a subpackage that olds odoc-generated documentation.  Flags:
+    * -L <license_filename>: give the name of a file to include in the subpackage as a license file.
+
+The following is an example specfile for an imaginary OCaml library called _foolib_ that is built with dune.
+
+.ocaml-dune-example.spec
+[source]
+----
+include::{examplesdir}/ocaml-dune-example.spec[]
+----
+
+=== Topkg
+
+Topkg, the "transitory OCaml software packager", generates scripts that are executed to perform various package and build tasks.
+
+The following is an example specfile for an imaginary OCaml library called _foolib_ that is built with topkg.
+
+.ocaml-topkg-example.spec
+[source]
+----
+include::{examplesdir}/ocaml-topkg-example.spec[]
+----
+
+=== Other build tools
+
+The following is an example specfile for an imaginary OCaml library called _foolib_.
+
+.ocaml-example.spec
+[source]
+----
+include::{examplesdir}/ocaml-example.spec[]
+----
 
 == Further reading
 

