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