]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/calcdep.pl
Remove include/inspircd_se_config.h and socketengine-specific headers
[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: bin/inspircd modules
37
38 END
39         my @core_deps;
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         my @modlist;
48         for my $file (<commands/*.cpp>) {
49                 my $out = find_output $file;
50                 dep_cpp $file, $out;
51                 push @modlist, $out;
52         }
53
54         opendir my $moddir, 'modules';
55         for my $file (sort readdir $moddir) {
56                 next if $file =~ /^\./;
57                 if (-e "modules/extra/$file" && !-l "modules/$file") {
58                         # Incorrect symlink?
59                         print "Replacing symlink for $file found in modules/extra\n";
60                         rename "modules/$file", "modules/$file~";
61                         symlink "extra/$file", "modules/$file";
62                 }
63                 if ($file =~ /^m_/ && -d "modules/$file" && dep_dir "modules/$file") {
64                         mkdir "$build/obj/$file";
65                         push @modlist, "modules/$file.so";
66                 }
67                 if ($file =~ /^m_.*\.cpp$/) {
68                         my $out = find_output "modules/$file";
69                         dep_cpp "modules/$file", $out;
70                         push @modlist, $out;
71                 }
72         }
73         
74         my $core_mk = join ' ', @core_deps;
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 modules: $mods
83
84 .PHONY: inspircd modules
85
86 END
87 }
88
89 sub find_output($) {
90         my $file = shift;
91         my($path,$base) = $file =~ m#^((?:.*/)?)([^/]+)\.cpp# or die "Bad file $file";
92         if ($path eq 'modules/' || $path eq 'commands/') {
93                 return "modules/$base.so";
94         } elsif ($path eq '' || $path eq 'modes/' || $path =~ /^[a-z]+engines\/$/) {
95                 return "obj/$base.o";
96         } elsif ($path =~ m#modules/(m_.*)/#) {
97                 return "obj/$1/$base.o";
98         } else {
99                 die "Can't determine output for $file";
100         }
101 }
102
103 sub gendep($) {
104         my $f = shift;
105         my $basedir = $f =~ m#(.*)/# ? $1 : '.';
106         return $f2dep{$f} if exists $f2dep{$f};
107         $f2dep{$f} = '';
108         my %dep;
109         my $link = readlink $f;
110         if (defined $link) {
111                 $link = "$basedir/$link" unless $link =~ m#^/#;
112                 $dep{$link}++;
113         }
114         open my $in, '<', $f or die "Could not read $f";
115         while (<$in>) {
116                 if (/^\s*#\s*include\s*"([^"]+)"/) {
117                         my $inc = $1;
118                         next if $inc eq 'inspircd_version.h' && $f eq '../include/inspircd.h';
119                         my $found = 0;
120                         for my $loc ("$basedir/$inc", "../include/$inc") {
121                                 next unless -e $loc;
122                                 $found++;
123                                 $dep{$_}++ for split / /, gendep $loc;
124                                 $loc =~ s#^\.\./##;
125                                 $dep{$loc}++;
126                         }
127                         if ($found == 0 && $inc ne 'inspircd_win32wrapper.h') {
128                                 print STDERR "WARNING: could not find header $inc for $f\n";
129                         } elsif ($found > 1 && $basedir ne '../include') {
130                                 print STDERR "WARNING: ambiguous include $inc in $f\n";
131                         }
132                 }
133         }
134         close $in;
135         $f2dep{$f} = join ' ', sort keys %dep;
136         $f2dep{$f};
137 }
138
139 sub dep_cpp($$) {
140         my($file, $out) = @_;
141         gendep $file;
142
143         print MAKE "$out: $file $f2dep{$file}\n";
144         print MAKE "\t@\$(SOURCEPATH)/make/unit-cc.pl \$(VERBOSE) \$\@ \$< \$>\n";
145 }
146
147 sub dep_dir($) {
148         my($dir) = @_;
149         my @ofiles;
150         opendir DIR, $dir;
151         for my $file (sort readdir DIR) {
152                 next unless $file =~ /(.*)\.cpp$/;
153                 my $ofile = find_output "$dir/$file";
154                 dep_cpp "$dir/$file", $ofile;
155                 push @ofiles, $ofile;
156         }
157         closedir DIR;
158         if (@ofiles) {
159                 my $ofiles = join ' ', @ofiles;
160                 print MAKE "$dir.so: $ofiles\n\t\$(RUNCC) \$(PICLDFLAGS) -o \$\@ \$^ \$>\n";
161                 return 1;
162         } else {
163                 return 0;
164         }
165 }
166