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