Merge from vendor branch TNF:
[pkgsrc.git] / lang / sml-nj / scripts / get-cm
1 #!/bin/sh
2 #
3 # usage: get-cm work_dir
4 #
5 # Find all .cm Group and Library descriptions that are referenced
6 # from the top level alias files in $SML_BASE/lib/
7
8 # temp file
9 tmpfile="${TMP:-/tmp}/chomp$$"
10
11 # attempt to change directory to work dir
12 cd $1 || exit 1
13 work_dir=`pwd`  # get the canonical name for the current directory
14
15 trap "rm -f $tmpfile" 0 1 2 3 15
16
17 # hack to remove SML comments '(* ... *)' 
18
19 strip_comments()
20 {
21         # caveat: the following assumes gcc is present ...      
22         gcc -x c -E -P -ansi -DOPSYS_UNIX $1 | awk '{ 
23                 line = $0
24                 if (match(line, "\\(\\*")) {
25                         while (match($0, "\\(\\*")) {
26                                 if (RSTART > 1) {
27                                         print substr($0, 0, RSTART-1);
28                                 }
29                                 sub("^.*\\(\\*", "");
30                                 while (!match($0, "\\*\\)")) {
31                                         getline
32                                 }
33                                 $0 = substr($0, RSTART+RLENGTH);
34                         }
35                 }
36                 print
37         }'
38 }
39
40 # return canonical name for a path with embedded ..'s
41
42 canonical()
43 {
44         echo $(cd `dirname $1` && pwd)/`basename $1`
45 }
46
47 # initial list of CM description files
48 global_cm_list="$(cat ./lib/*.cm | awk '{ print $2 }')"
49
50 set -- $global_cm_list
51
52 touch $tmpfile
53
54 # process the global list
55 while [ $# -ge 1 ]; do
56
57         cm_file="$1"; shift
58         global_cm_list="$*"
59
60         echo $cm_file >> $tmpfile
61
62         cm_dir=`dirname $cm_file`
63         cm_new=$(strip_comments $cm_file | grep '\.cm' | \
64                 awk '{ print $1 }')
65
66         # append new CM files to current global list
67         for i in $cm_new; do
68                 new_cm_file=$(canonical $cm_dir/$i)
69                 if [ -r $new_cm_file ]; then 
70                         if ! grep "^$new_cm_file" $tmpfile > /dev/null; then 
71                                 global_cm_list="$new_cm_file $global_cm_list"
72                                 echo $new_cm_file >> $tmpfile
73                         fi
74                 fi
75         done 
76
77         set -- $global_cm_list
78 done
79
80 # output to stdout
81 sort -u < $tmpfile | sed -e "s,^$work_dir/,,"