23ad02
#!/bin/sh -
23ad02
# OCaml-specific "find-requires" for RPM.
23ad02
# By Richard W.M. Jones <rjones@redhat.com>
23ad02
# $Id$
23ad02
23ad02
#set -x
23ad02
23ad02
# Usage:
23ad02
#   (1) Add the following to the spec file:
23ad02
#   %define _use_internal_dependency_generator 0
23ad02
#   %define __find_requires /usr/lib/rpm/ocaml-find-requires.sh
23ad02
#   %define __find_provides /usr/lib/rpm/ocaml-find-provides.sh
23ad02
#
23ad02
#   (2) If you don't want the module to depend on the exact compiler
23ad02
#   version then use ocaml-find-requires.sh -c, but this is not something
23ad02
#   you should do normally.
23ad02
#
23ad02
#   (3) For any modules which you want to ignore, use '-i Modulename'.
23ad02
23ad02
OCAMLOBJINFO=ocamlobjinfo
23ad02
TEMP=`getopt -o ci:f: -n ocaml-find-requires.sh -- "$@"`
23ad02
if [ $? != 0 ]; then echo "ocaml-find-requires.sh: failed" >&2; exit 1; fi
23ad02
eval set -- "$TEMP"
23ad02
23ad02
emit_compiler_version=yes
23ad02
ignore_modules=nOTREAL
23ad02
23ad02
while true; do
23ad02
    case "$1" in
23ad02
	-c) emit_compiler_version=; shift;;
23ad02
	-i) ignore_modules="$2 $ignore_modules"; shift 2;;
23ad02
	-f) OCAMLOBJINFO="$2"; shift 2;;
23ad02
	--) shift; break;;
23ad02
	*) echo "ocaml-find-requires.sh: option error at $1"; exit 1;;
23ad02
    esac
23ad02
done
23ad02
23ad02
# Get the list of files.
23ad02
files=`sed "s/['\"]/\\\&/g"`
23ad02
23ad02
# Use ordinary find-requires first.
23ad02
echo $files | tr [:blank:] '\n' | /usr/lib/rpm/find-requires
23ad02
23ad02
# Get list of .cmi, .cmo and .cma files.
23ad02
files=`echo $files | tr [:blank:] '\n' | grep '\.cm[ioa]$'`
23ad02
23ad02
if [ -z "$files" ]; then exit 0; fi
23ad02
23ad02
# Get the list of modules exported by the file(s).
23ad02
modules=`$OCAMLOBJINFO $files |
23ad02
          grep -E '(Unit|Module) name: ' |
23ad02
          awk '{print $3}'`
23ad02
23ad02
# Turn list of modules into a regexp that matches the module names.
23ad02
modules_re=`echo $modules | sed 's/ /|/g'`
23ad02
ignore_modules_re=`echo $ignore_modules | sed 's/ /|/g'`
23ad02
23ad02
# Get a list of the modules these file(s) depend on.
23ad02
$OCAMLOBJINFO $files |
23ad02
grep -Eo '[0-9a-f]{32}[[:space:]]+[A-Za-z0-9_]+' |
23ad02
grep -Ev "$modules_re" |
23ad02
while read md5sum module; do
23ad02
    echo "ocaml($module) = $md5sum"
23ad02
done |
23ad02
grep -Ev "$ignore_modules_re" |
23ad02
sort -u
23ad02
23ad02
if [ -n "$emit_compiler_version" ]; then
23ad02
    # Every OCaml program depends on the precise version of the
23ad02
    # compiler which was used to compile it.
23ad02
    # rpm -q --qf '%{NAME} = %{VERSION}-%{RELEASE}\n' ocaml
23ad02
    echo "ocaml = `cat /usr/lib*/ocaml/fedora-ocaml-release`"
23ad02
fi