Contents in 1999-06-04-13 of release-21-2.
[chise/xemacs-chise.git.1] / src / make-src-depend
1 : #-*- Perl -*-
2 # Copyright (C) 1998 Free Software Foundation, Inc.
3
4 # This file is part of XEmacs.
5 #
6 # XEmacs is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by the
8 # Free Software Foundation; either version 2, or (at your option) any
9 # later version.
10 #
11 # XEmacs is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 # for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with XEmacs; see the file COPYING.  If not, write to
18 # the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 # Boston, MA 02111-1307, USA.
20
21 # Author: Martin Buchholz
22 eval 'exec perl -w -S $0 ${1+"$@"}'
23   if 0;
24
25 use strict;
26 my ($myName, $srcdir, %exists, %uses, %generated_header);
27
28 ($myName = $0) =~ s@.*/@@; my $usage ="
29 Usage: $myName
30
31 Generates Makefile dependencies for the XEmacs src directory.
32 The dependencies are written to stdout.
33 ";
34
35 die $usage if @ARGV;
36
37 ($srcdir = $0) =~ s@[^/]+$@@;
38 $srcdir = "." if $srcdir eq "";
39 chdir $srcdir or die "$srcdir: $!";
40
41 opendir SRCDIR, "." or die "$srcdir: $!";
42 for (grep (/\.[ch]$/, readdir (SRCDIR))) { $exists{$_} = 1; }
43 closedir SRCDIR;
44
45 for (qw (config.h sheap-adjust.h paths.h Emacs.ad.h)) {
46   $generated_header{$_} = 1;
47 }
48
49 for my $file (keys %exists) {
50   open (FILE, $file) or die "$file: $!";
51   undef $/; $_ = <FILE>;
52   RemoveComments ($_);
53   s/[ \t]+//g;
54   # Find include dependencies
55   for (/^\#include([^\n]+)/gm) {
56     if (m@^\"([A-Za-z0-9_-]+\.h)\"@) {
57       $uses{$file}{$1} = 1 if exists $exists{$1};
58     } elsif (m@<([A-Za-z0-9_-]+\.h)>@) {
59       $uses{$file}{$1} = 1 if exists $generated_header{$1};
60     } elsif (m@\"../lwlib/([A-Za-z0-9_-]+\.h)\"@) {
61       $uses{$file}{"\$(LWLIB_SRCDIR)/lwlib.h"} = 1;
62     }
63   }
64 }
65
66 # Make transitive closure of %uses
67 while (1) {
68   my $changedP = 0;
69   for my $x (keys %uses) {
70     for my $y (keys %{$uses{$x}}) {
71       for my $z (keys %{$uses{$y}}) {
72         if (! exists $uses{$x}{$z}) {
73           $uses{$x}{$z} = 1;
74           $changedP = 1;
75         }
76       }
77     }
78   }
79   last if !$changedP;
80 }
81
82 # Print file header
83 print
84 "## This file automatically generated by $myName.  Do not modify.
85
86 #ifdef USE_UNION_TYPE
87 LISP_UNION_H=lisp-union.h
88 #else
89 LISP_UNION_H=lisp-disunion.h
90 #endif
91 ";
92
93 my @LISP_H = ('lisp.h', 'config.h');
94 #@LISP_H = grep (! /lisp-(dis)?union\.h/, @LISP_H);
95 print "LISP_H = @{[grep (!/lisp-(dis)?union\.h/, @LISP_H)]} \$(LISP_UNION_H)\n";
96
97 sub PrintDeps {
98   my $file = shift;
99   my $ofile = $file; $ofile =~ s/c$/o/; print "$ofile: ";
100   if (exists $uses{$file}{'lisp.h'}) {
101     delete @{%{$uses{$file}}}{@LISP_H};
102     $uses{$file}{'$(LISP_H)'} = 1;
103   }
104   print "@{[sort keys %{$uses{$file}}]}\n";
105 }
106
107 sub PrintPatternDeps {
108   my ($pattern, $CPP_SYMBOL) = @_;
109   print "#ifdef $CPP_SYMBOL\n";
110   for my $file (sort grep (/$pattern/ && /\.c$/, keys %uses)) {
111     PrintDeps($file);
112     delete $uses{$file};
113   }
114   print "#endif\n";
115 }
116
117 PrintPatternDeps ('-msw',     "HAVE_MS_WINDOWS");
118 PrintPatternDeps ('-x',       "HAVE_X_WINDOWS");
119 PrintPatternDeps ('database', "HAVE_DATABASE");
120 PrintPatternDeps ('^mule',    "MULE");
121 PrintPatternDeps ('^(?:External|extw-)', "EXTERNAL_WIDGET");
122
123 for my $file (sort grep (/\.c$/, keys %uses)) { PrintDeps($file); }
124
125 sub RemoveComments {
126   $_[0] =~
127     s{ (
128         [^\"\'/]+ |
129         (?:\"[^\"\\]*(?:\\.[^\"\\]*)*\" [^\"\'/]*)+ |
130         (?:\'[^\'\\]*(?:\\.[^\'\\]*)*\' [^\"\'/]*)+
131        )
132        | / (?:
133             \*[^*]*\*+(?:[^/*][^*]*\*+)*/
134             |
135             /[^\n]*
136            )
137      }{defined $1 ? $1 : ""}gsxeo;
138 }