Convert to using automake
[virt-top.git] / ocaml-dep.sh.in
1 #!/bin/bash -
2 # (C) Copyright 2009-2020 Red Hat Inc.
3 # @configure_input@
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 # This is a smarter wrapper around ocamldep(1) which is used to create
20 # the .depend files which are present in each subdirectory that builds
21 # OCaml code.
22 #
23 # Usage:
24 #   .depend: *.mli *.ml
25 #       $(top_builddir)/ocaml-dep.sh $^
26 #   -include .depend
27
28 set -e
29
30 # List of directories that contain common OCaml libraries.  If it
31 # contains OCaml code that can be included from another directory,
32 # then it should go here.  Note that OCaml modules in these
33 # directories must have unique names (eg. not ‘Utils’) else
34 # dependencies don't get built right.
35 include_dirs="
36 common/mlgettext
37 common/mlpcre
38 common/mlprogress
39 common/mlstdutils
40 common/mltools
41 common/mlutils
42 common/mlv2v
43 common/mlvisit
44 common/mlxml
45 lib
46 "
47
48 # Output file is always created in the current directory.
49 output=.depend
50
51 subdir=$(realpath --relative-to=@abs_top_builddir@ .)
52 srcdir=$(realpath --relative-to=. @abs_top_srcdir@/${subdir})
53 srcdir_re=$(realpath --relative-to=. @abs_top_srcdir@/${subdir} | sed 's/\./[.]/g')
54 top_builddir=$(realpath --relative-to=. @abs_top_builddir@)
55
56 includes="-I @abs_top_srcdir@/$subdir -I @abs_top_builddir@/$subdir"
57 for i in $include_dirs; do
58     includes="$includes -I @abs_top_srcdir@/$i -I @abs_top_builddir@/$i"
59 done
60
61 rm -f $output $output-t
62
63 echo "# OCaml dependencies generated by $0" > $output-t
64 echo >> $output-t
65
66 # Rewrite paths
67 # 1. Normalize absolute srcdir to relative path
68 # 2. Object files (*.cm*, *.o), in srcdir: Rewrite to builddir
69 # 3. Generated _config.ml in srcdir: Rewrite to builddir
70 # 4. Other object files below abs_top_srcdir: rewrite to corresponding builddir
71 # 5. Eliminate "./" prefix
72 @OCAMLFIND@ ocamldep @OCAMLDEP_ALL@ @OCAMLDEP_ONE_LINE@ $includes "$@" \
73     | sed \
74           -e "s,@abs_top_srcdir@/${subdir},.,g" \
75           -e "s,\B${srcdir_re}/\\([^ ]*[.]\\)\\(cm[^ ]*\\|o\\),\\1\\2,g" \
76           -e "s,\B${srcdir_re}/\\([^ /]*_config[.]ml\\),\\1,g" \
77           -e "s,@abs_top_srcdir@/\\([^ ]*[.]\\)\\(cm[^ ]*\\|o\\),${top_builddir}/\\1\\2,g" \
78           -e 's,\(^\| \)./,\1,g' \
79     >> $output-t
80
81 chmod -w $output-t
82
83 mv $output-t $output