]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/calcdep.pl
GCC 4.5 warning fixups
[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         if ($ENV{PURE_STATIC}) {
32                 run_static();
33         } else {
34                 run_dynamic();
35         }
36         close MAKE;
37 }
38
39 sub run_dynamic() {
40         my $build = $ENV{BUILDPATH};
41         print MAKE <<END;
42 # DO NOT EDIT THIS FILE
43 # It is autogenerated by make/calcdep.pl, and will be overwritten
44 # every time you rerun make in the main directory
45 VPATH = \$(SOURCEPATH)/src
46
47 bad-target:
48         \@echo "This Makefile must be run by a sub-make from the source"
49         \@echo "in order to set the correct environment variables"
50         \@exit 1
51
52 all: inspircd commands modules
53
54 END
55         my(@core_deps, @cmdlist, @modlist);
56         for my $file (<*.cpp>, <modes/*.cpp>, <socketengines/*.cpp>, "threadengines/threadengine_pthread.cpp") {
57                 my $out = find_output $file;
58                 dep_cpp $file, $out;
59                 next if $file =~ m#^socketengines/# && $file ne "socketengines/$ENV{SOCKETENGINE}.cpp";
60                 push @core_deps, $out;
61         }
62
63         for my $file (<commands/*.cpp>) {
64                 my $out = find_output $file;
65                 dep_cpp $file, $out;
66                 push @cmdlist, $out;
67         }
68
69         opendir my $moddir, 'modules';
70         for my $file (sort readdir $moddir) {
71                 next if $file =~ /^\./;
72                 if (-e "modules/extra/$file" && !-l "modules/$file") {
73                         # Incorrect symlink?
74                         print "Replacing symlink for $file found in modules/extra\n";
75                         rename "modules/$file", "modules/$file~";
76                         symlink "extra/$file", "modules/$file";
77                 }
78                 if ($file =~ /^m_/ && -d "modules/$file" && dep_dir "modules/$file") {
79                         mkdir "$build/obj/$file";
80                         push @modlist, "modules/$file.so";
81                 }
82                 if ($file =~ /^m_.*\.cpp$/) {
83                         my $out = find_output "modules/$file";
84                         dep_cpp "modules/$file", $out;
85                         push @modlist, $out;
86                 }
87         }
88         
89         my $core_mk = join ' ', @core_deps;
90         my $cmds = join ' ', @cmdlist;
91         my $mods = join ' ', @modlist;
92         print MAKE <<END;
93
94 bin/inspircd: $core_mk
95         \$(RUNCC) -o \$\@ \$(CORELDFLAGS) \$(LDLIBS) \$^ \$>
96
97 inspircd: bin/inspircd
98
99 commands: $cmds
100
101 modules: $mods
102
103 .PHONY: all bad-target inspircd commands modules
104
105 END
106 }
107
108 sub run_static() {
109         print MAKE <<END;
110 # DO NOT EDIT THIS FILE
111 # It is autogenerated by make/calcdep.pl, and will be overwritten
112 # every time you rerun make in the main directory
113 VPATH = \$(SOURCEPATH)/src
114
115 bad-target:
116         \@echo "This Makefile must be run by a sub-make from the source"
117         \@echo "in order to set the correct environment variables"
118         \@exit 1
119
120 all: inspircd
121
122 END
123         my @deps;
124         for my $file (<*.cpp>, <modes/*.cpp>, <socketengines/*.cpp>, <commands/*.cpp>,
125                         <modules/*.cpp>, <modules/m_*/*.cpp>, "threadengines/threadengine_pthread.cpp") {
126                 my $out = find_output $file, 1;
127                 dep_cpp $file, $out;
128                 next if $file =~ m#^socketengines/# && $file ne "socketengines/$ENV{SOCKETENGINE}.cpp";
129                 push @deps, $out;
130         }
131
132         my $core_mk = join ' ', @deps;
133         print MAKE <<END;
134
135 bin/inspircd: $core_mk
136         \$(RUNCC) -o \$\@ \$(CORELDFLAGS) \$(LDLIBS) \$^ \$>
137
138 inspircd: bin/inspircd
139
140 .PHONY: all bad-target inspircd
141
142 END
143 }
144
145 sub find_output {
146         my($file, $static) = @_;
147         my($path,$base) = $file =~ m#^((?:.*/)?)([^/]+)\.cpp# or die "Bad file $file";
148         if ($path eq 'modules/' || $path eq 'commands/') {
149                 return $static ? "obj/$base.o" : "modules/$base.so";
150         } elsif ($path eq '' || $path eq 'modes/' || $path =~ /^[a-z]+engines\/$/) {
151                 return "obj/$base.o";
152         } elsif ($path =~ m#modules/(m_.*)/#) {
153                 return "obj/$1/$base.o";
154         } else {
155                 die "Can't determine output for $file";
156         }
157 }
158
159 sub gendep($) {
160         my $f = shift;
161         my $basedir = $f =~ m#(.*)/# ? $1 : '.';
162         return $f2dep{$f} if exists $f2dep{$f};
163         $f2dep{$f} = '';
164         my %dep;
165         my $link = readlink $f;
166         if (defined $link) {
167                 $link = "$basedir/$link" unless $link =~ m#^/#;
168                 $dep{$link}++;
169         }
170         open my $in, '<', $f or die "Could not read $f";
171         while (<$in>) {
172                 if (/^\s*#\s*include\s*"([^"]+)"/) {
173                         my $inc = $1;
174                         next if $inc eq 'inspircd_version.h' && $f eq '../include/inspircd.h';
175                         my $found = 0;
176                         for my $loc ("$basedir/$inc", "../include/$inc") {
177                                 next unless -e $loc;
178                                 $found++;
179                                 $dep{$_}++ for split / /, gendep $loc;
180                                 $loc =~ s#^\.\./##;
181                                 $dep{$loc}++;
182                         }
183                         if ($found == 0 && $inc ne 'inspircd_win32wrapper.h') {
184                                 print STDERR "WARNING: could not find header $inc for $f\n";
185                         } elsif ($found > 1 && $basedir ne '../include') {
186                                 print STDERR "WARNING: ambiguous include $inc in $f\n";
187                         }
188                 }
189         }
190         close $in;
191         $f2dep{$f} = join ' ', sort keys %dep;
192         $f2dep{$f};
193 }
194
195 sub dep_cpp($$) {
196         my($file, $out) = @_;
197         gendep $file;
198
199         print MAKE "$out: $file $f2dep{$file}\n";
200         print MAKE "\t@\$(SOURCEPATH)/make/unit-cc.pl \$(VERBOSE) \$\@ \$< \$>\n";
201 }
202
203 sub dep_dir($) {
204         my($dir) = @_;
205         my @ofiles;
206         opendir DIR, $dir;
207         for my $file (sort readdir DIR) {
208                 next unless $file =~ /(.*)\.cpp$/;
209                 my $ofile = find_output "$dir/$file";
210                 dep_cpp "$dir/$file", $ofile;
211                 push @ofiles, $ofile;
212         }
213         closedir DIR;
214         if (@ofiles) {
215                 my $ofiles = join ' ', @ofiles;
216                 print MAKE "$dir.so: $ofiles\n\t\$(RUNCC) \$(PICLDFLAGS) -o \$\@ \$^ \$>\n";
217                 return 1;
218         } else {
219                 return 0;
220         }
221 }
222