Add OCaml dependency generator scripts (Richard W.M. Jones)
[platform/upstream/rpm.git] / scripts / ocaml-find-provides.sh
1 #!/bin/sh -
2 # OCaml-specific "find-provides" for RPM.
3 # By Richard W.M. Jones <rjones@redhat.com>
4 # $Id: ocaml-find-provides.sh,v 1.2 2007/09/06 11:49:59 rjones Exp $
5
6 #set -x
7
8 # Usage:
9 #   (1) Add the following to the spec file:
10 #   %define _use_internal_dependency_generator 0
11 #   %define __find_requires /usr/lib/rpm/ocaml-find-requires.sh
12 #   %define __find_provides /usr/lib/rpm/ocaml-find-provides.sh
13 #
14 #   (2) If you don't want the module to depend on the exact compiler
15 #   version then use ocaml-find-requires.sh -c, but this is not something
16 #   you should do normally.
17 #
18 #   (3) For any modules which you want to ignore, use '-i Modulename'.
19
20 OCAMLOBJINFO=ocamlobjinfo
21 TEMP=`getopt -o i:f: -n ocaml-find-provides.sh -- "$@"`
22 if [ $? != 0 ]; then echo "ocaml-find-provides.sh: failed" >&2; exit 1; fi
23 eval set -- "$TEMP"
24
25 ignore_modules=nOTREAL
26
27 while true; do
28     case "$1" in
29         -i) ignore_modules="$2 $ignore_modules"; shift 2;;
30         -f) OCAMLOBJINFO="$2"; shift 2;;
31         --) shift; break;;
32         *) echo "ocaml-find-provides.sh: option error at $1"; exit 1;;
33     esac
34 done
35
36 # Get the list of files.
37 files=`sed "s/['\"]/\\\&/g"`
38
39 # Get list of .cmi, .cmo and .cma files.
40 files=`echo $files | tr [:blank:] '\n' | grep '\.cm[ioa]$'`
41
42 if [ -z "$files" ]; then exit 0; fi
43
44 # Get the list of modules exported by the files.
45 modules=`$OCAMLOBJINFO $files |
46           grep -E '(Unit|Module) name: ' |
47           awk '{print $3}'`
48
49 # Turn list of modules into a regexp that matches the module names.
50 modules_re=`echo $modules | sed 's/ /|/g'`
51 ignore_modules_re=`echo $ignore_modules | sed 's/ /|/g'`
52
53 # Get a list of the modules these file(s) provide.
54 $OCAMLOBJINFO $files |
55 grep -Eo '[0-9a-f]{32}[[:space:]]+[A-Za-z0-9_]+' |
56 grep -E '[0-9a-f]{32}[[:space:]]+'"($modules_re)\$" |
57 while read md5sum module; do
58     echo "ocaml($module) = $md5sum"
59 done |
60 grep -Ev "$ignore_modules_re" |
61 sort -u