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