]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/calcdep.pl
Update m_cloaking to use free-form keys instead of weakening the hash IV
[user/henk/code/inspircd.git] / make / calcdep.pl
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use POSIX qw(getcwd);
5
6 sub find_output($);
7 sub gendep($);
8 sub dep_cpp($$);
9 sub dep_dir($);
10 sub run();
11
12 my %f2dep;
13
14 run;
15 exit 0;
16
17 sub run() {
18         my $build = $ENV{BUILDPATH};
19         mkdir $build;
20         chdir $build or die "Could not open build directory: $!";
21         symlink "$ENV{SOURCEPATH}/include", 'include';
22         mkdir $_ for qw/bin modules obj/;
23 # BSD make has a horribly annoying bug resulting in an extra chdir of the make process
24 # Create symlinks to work around it
25         symlink "../$_", "obj/$_" for qw/bin modules obj/;
26
27         $build = getcwd();
28         open MAKE, '>real.mk' or die "Could not write real.mk: $!";
29         chdir "$ENV{SOURCEPATH}/src";
30
31         print MAKE <<END;
32 # DO NOT EDIT
33 # Autogenerated by calcdep
34 VPATH = \$(SOURCEPATH)/src
35
36 all: inspircd commands modules
37
38 END
39         my(@core_deps, @cmdlist, @modlist);
40         for my $file (<*.cpp>, <modes/*.cpp>, <socketengines/*.cpp>, "threadengines/threadengine_pthread.cpp") {
41                 my $out = find_output $file;
42                 dep_cpp $file, $out;
43                 next if $file =~ m#^socketengines/# && $file ne "socketengines/$ENV{SOCKETENGINE}.cpp";
44                 push @core_deps, $out;
45         }
46
47         for my $file (<commands/*.cpp>) {
48                 my $out = find_output $file;
49                 dep_cpp $file, $out;
50                 push @cmdlist, $out;
51         }
52
53         opendir my $moddir, 'modules';
54         for my $file (sort readdir $moddir) {
55                 next if $file =~ /^\./;
56                 if (-e "modules/extra/$file" && !-l "modules/$file") {
57                         # Incorrect symlink?
58                         print "Replacing symlink for $file found in modules/extra\n";
59                         rename "modules/$file", "modules/$file~";
60                         symlink "extra/$file", "modules/$file";
61                 }
62                 if ($file =~ /^m_/ && -d "modules/$file" && dep_dir "modules/$file") {
63                         mkdir "$build/obj/$file";
64                         push @modlist, "modules/$file.so";
65                 }
66                 if ($file =~ /^m_.*\.cpp$/) {
67                         my $out = find_output "modules/$file";
68                         dep_cpp "modules/$file", $out;
69                         push @modlist, $out;
70                 }
71         }
72         
73         my $core_mk = join ' ', @core_deps;
74         my $cmds = join ' ', @cmdlist;
75         my $mods = join ' ', @modlist;
76         print MAKE <<END;
77
78 bin/inspircd: $core_mk
79         \$(RUNCC) -o \$\@ \$(CORELDFLAGS) \$(LDLIBS) \$^ \$>
80
81 inspircd: bin/inspircd
82
83 commands: $cmds
84
85 modules: $mods
86
87 .PHONY: inspircd commands modules
88
89 END
90 }
91
92 sub find_output($) {
93         my $file = shift;
94         my($path,$base) = $file =~ m#^((?:.*/)?)([^/]+)\.cpp# or die "Bad file $file";
95         if ($path eq 'modules/' || $path eq 'commands/') {
96                 return "modules/$base.so";
97         } elsif ($path eq '' || $path eq 'modes/' || $path =~ /^[a-z]+engines\/$/) {
98                 return "obj/$base.o";
99         } elsif ($path =~ m#modules/(m_.*)/#) {
100                 return "obj/$1/$base.o";
101         } else {
102                 die "Can't determine output for $file";
103         }
104 }
105
106 sub gendep($) {
107         my $f = shift;
108         my $basedir = $f =~ m#(.*)/# ? $1 : '.';
109         return $f2dep{$f} if exists $f2dep{$f};
110         $f2dep{$f} = '';
111         my %dep;
112         my $link = readlink $f;
113         if (defined $link) {
114                 $link = "$basedir/$link" unless $link =~ m#^/#;
115                 $dep{$link}++;
116         }
117         open my $in, '<', $f or die "Could not read $f";
118         while (<$in>) {
119                 if (/^\s*#\s*include\s*"([^"]+)"/) {
120                         my $inc = $1;
121                         next if $inc eq 'inspircd_version.h' && $f eq '../include/inspircd.h';
122                         my $found = 0;
123                         for my $loc ("$basedir/$inc", "../include/$inc") {
124                                 next unless -e $loc;
125                                 $found++;
126                                 $dep{$_}++ for split / /, gendep $loc;
127                                 $loc =~ s#^\.\./##;
128                                 $dep{$loc}++;
129                         }
130                         if ($found == 0 && $inc ne 'inspircd_win32wrapper.h') {
131                                 print STDERR "WARNING: could not find header $inc for $f\n";
132                         } elsif ($found > 1 && $basedir ne '../include') {
133                                 print STDERR "WARNING: ambiguous include $inc in $f\n";
134                         }
135                 }
136         }
137         close $in;
138         $f2dep{$f} = join ' ', sort keys %dep;
139         $f2dep{$f};
140 }
141
142 sub dep_cpp($$) {
143         my($file, $out) = @_;
144         gendep $file;
145
146         print MAKE "$out: $file $f2dep{$file}\n";
147         print MAKE "\t@\$(SOURCEPATH)/make/unit-cc.pl \$(VERBOSE) \$\@ \$< \$>\n";
148 }
149
150 sub dep_dir($) {
151         my($dir) = @_;
152         my @ofiles;
153         opendir DIR, $dir;
154         for my $file (sort readdir DIR) {
155                 next unless $file =~ /(.*)\.cpp$/;
156                 my $ofile = find_output "$dir/$file";
157                 dep_cpp "$dir/$file", $ofile;
158                 push @ofiles, $ofile;
159         }
160         closedir DIR;
161         if (@ofiles) {
162                 my $ofiles = join ' ', @ofiles;
163                 print MAKE "$dir.so: $ofiles\n\t\$(RUNCC) \$(PICLDFLAGS) -o \$\@ \$^ \$>\n";
164                 return 1;
165         } else {
166                 return 0;
167         }
168 }
169