Add the remake and configure infrastructure.
[tatoo.git] / tools / odeps.sh
1 #!/bin/sh
2 #Save the parameters
3
4 PROG="$0"
5 CMDLINE="$*"
6 usage() {
7     echo "$PROG [ocamldep ... ]"
8 }
9
10 OPTIONS=""
11 INCLUDES=""
12 ARGS=""
13 NATIVE=0
14 while true; do
15     case $1 in
16         -ppopt)
17             OPTIONS="$OPTIONS $1 $2"
18             shift
19             ;;
20         -I)
21             OPTIONS="$OPTIONS $1 $2"
22             INCLUDES="$INCLUDES $2"
23             shift
24             ;;
25         -native)
26             NATIVE=1
27             OPTIONS="$OPTIONS $1"
28             ;;
29         *.ml*)
30             ARGS="$ARGS $1"
31             ;;
32         *)
33             OPTIONS="$OPTIONS $1"
34             ;;
35     esac
36     if test "$#" -eq 0; then break; else shift; fi
37 done
38
39 if test "$NATIVE" -eq 1; then
40     OEXT="cmx"
41 else
42     OEXT="cmo"
43 fi
44 IEXT="cmi"
45
46 for src in $ARGS; do
47     $OPTIONS -modules "$src" | while read file deps; do
48         if test -z "$file"; then continue; fi
49         file=${file%:}
50         if test "$file" != "${file%.mli}"; then
51             ext="$IEXT"
52             base="${file%.mli}"
53             src_ext="mli"
54         else
55             ext="$OEXT"
56             base="${file%.ml}"
57             src_ext="ml"
58         fi
59         dependencies=""
60         for dep in $deps; do
61             modbase=$(echo $dep | sed 's/\(.*\)/\l\1/')
62             for dir in $INCLUDES; do
63                 dir=${dir%/} #remove trailing slash, if any
64                 #Interfaces or bytecode objects depends on other compile interfaces
65                 #if they exists, otherwise they depend on the object
66                 if test \( "$ext" = "cmi" -o "$ext" = "cmo" \) -a -f "$dir"/"$modbase".mli ; then
67                     dependencies="$dependencies $dir/$modbase".cmi
68                     break
69                 elif test -f "$dir"/"$modbase".ml -o -f "$dir"/"$modbase".mly -o -f "$dir"/"$modbase".mll -o -d "$dir"/"$modbase"; then
70                     dependencies="$dependencies $dir/$modbase"."$ext"
71                     break
72                 fi
73             done
74         done
75         #finally add the cmi as dependency of the cmo/cmx,
76         #if the .mli exists
77         if test "$ext" != "cmi" -a -f "$base".mli ; then
78             dependencies=" ${base}.cmi${dependencies}"
79         fi
80         #output a phony dependency between the cmi and the cmx/cmo if there is no .mli
81         if test "$ext" != "cmi" -a ! -f "$base".mli; then
82             echo "${base}.cmi: ${base}.${ext}"
83         fi
84         echo "${base}.${ext}:${dependencies}"
85     done
86
87 done