]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/calcdep.pl
Move file inclusion logic into calcdep, and complain about ambiguous #include directi...
[user/henk/code/inspircd.git] / make / calcdep.pl
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use Getopt::Long;
5
6 my %f2dep;
7
8 sub gendep;
9 sub gendep {
10         my $f = shift;
11         my $basedir = $f =~ m#(.*)/# ? $1 : '.';
12         return $f2dep{$f} if exists $f2dep{$f};
13         $f2dep{$f} = '';
14         my %dep;
15         open my $in, '<', $f or die "Could not read $f";
16         while (<$in>) {
17                 if (/^\s*#\s*include\s*"([^"]+)"/) {
18                         my $inc = $1;
19                         my $found = 0;
20                         for my $loc ("$basedir/$inc", "../include/$inc") {
21                                 next unless -e $loc;
22                                 $found++;
23                                 $dep{$loc}++;
24                                 $dep{$_}++ for split / /, gendep $loc;
25                         }
26                         if ($found == 0 && $inc ne 'inspircd_win32wrapper.h') {
27                                 print STDERR "WARNING: could not find header $inc for $f\n";
28                         } elsif ($found > 1 && $basedir ne '../include') {
29                                 print STDERR "WARNING: ambiguous include $inc in $f\n";
30                         }
31                 }
32         }
33         close $in;
34         $f2dep{$f} = join ' ', sort keys %dep;
35         $f2dep{$f};
36 }
37
38 sub dep_cpp {
39         my $file = shift;
40         gendep $file;
41         my($path,$base) = $file =~ m#^((?:.*/)?)([^/]+)\.cpp# or die "Bad file $file";
42         my $cmd = "$path.$base.d";
43         my $ext = $path eq 'modules/' || $path eq 'commands/' ? '.so' : '.o';
44         my $out = "$path$base$ext";
45
46         open OUT, '>', $cmd;
47         print OUT "$out: $file $f2dep{$file}\n";
48         print OUT "\t@../make/unit-cc.pl \$(VERBOSE) $file $out\n";
49         print OUT "$cmd: $file $f2dep{$file}\n";
50         print OUT "\t../make/calcdep.pl -file $file\n";
51 }
52
53 sub dep_dir {
54         my $dir = shift;
55         if ($dir =~ m#^(.*?)([^/]+)/?$#) {
56                 my($path,$base) = ($1,$2);
57                 my $cmd = "$path.$base.d";
58                 my $out = "$path$base.so";
59                 opendir DIR, $dir;
60                 my $ofiles = join ' ', grep s/(.*)\.cpp$/$path$base\/$1.o/, readdir DIR;
61                 closedir DIR;
62                 open OUT, '>', $cmd;
63                 print OUT "$out: $ofiles\n\t".'$(RUNCC) $(PICLDFLAGS) -o $@ '
64                         .$ofiles."\n";
65                 print OUT "$cmd: $dir\n\t".'@../make/calcdep.pl -file '."$path$base\n";
66         } else {
67                 print STDERR "Cannot generate depencency for $dir\n";
68                 exit 1;
69         }
70 }
71
72 my($all,$quiet, $file);
73 GetOptions(
74         'all' => \$all,
75         'quiet' => \$quiet,
76         'file=s' => \$file,
77 );
78
79 if (!$all && !defined $file) {
80         print "Use: $0 {-all|-file filename} [-quiet]\n";
81         exit 1;
82 }
83
84 if (defined $file) {
85         if (-f $file) {
86                 dep_cpp $file;
87         } elsif (-d $file) {
88                 dep_dir $file;
89         } else {
90                 print STDERR "Can't generate dependencies for $file\n";
91                 exit 1;
92         }
93 } else {
94         my @files = (<*.cpp>, <commands/*.cpp>, <modes/*.cpp>, <modules/*.cpp>, <modules/m_*/*.cpp>);
95         push @files, "socketengines/$ENV{SOCKETENGINE}.cpp", "threadengines/threadengine_pthread.cpp";
96         for my $file (@files) {
97                 dep_cpp $file;
98         }
99
100         my @dirs = grep -d, <modules/m_*>;
101         for my $dir (@dirs) {
102                 dep_dir $dir;
103         }
104
105         s#([^/]+)\.cpp#.$1.d# for @files;
106         s#([^/]+)/?$#.$1.d# for @dirs;
107         print join ' ', @files, @dirs;
108 }