]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/calcdep.pl
Sort the module list to give a useful indicator of compile progress
[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/$ENV{SOCKETENGINE}.cpp", "threadengines/threadengine_pthread.cpp") {
41                 my $out = find_output $file;
42                 dep_cpp $file, $out;
43                 push @core_deps, $out;
44         }
45         
46         my @modlist;
47         for my $file (<commands/*.cpp>) {
48                 my $out = find_output $file;
49                 dep_cpp $file, $out;
50                 push @modlist, $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 $mods = join ' ', @modlist;
75         print MAKE <<END;
76
77 bin/inspircd: $core_mk
78         \$(RUNCC) -o \$\@ \$(CORELDFLAGS) \$(LDLIBS) \$^ \$>
79
80 inspircd: bin/inspircd
81 modules: $mods
82
83 .PHONY: inspircd modules
84
85 END
86 }
87
88 sub find_output($) {
89         my $file = shift;
90         my($path,$base) = $file =~ m#^((?:.*/)?)([^/]+)\.cpp# or die "Bad file $file";
91         if ($path eq 'modules/' || $path eq 'commands/') {
92                 return "modules/$base.so";
93         } elsif ($path eq '' || $path eq 'modes/' || $path =~ /^[a-z]+engines\/$/) {
94                 return "obj/$base.o";
95         } elsif ($path =~ m#modules/(m_.*)/#) {
96                 return "obj/$1/$base.o";
97         } else {
98                 die "Can't determine output for $file";
99         }
100 }
101
102 sub gendep($) {
103         my $f = shift;
104         my $basedir = $f =~ m#(.*)/# ? $1 : '.';
105         return $f2dep{$f} if exists $f2dep{$f};
106         $f2dep{$f} = '';
107         my %dep;
108         my $link = readlink $f;
109         if (defined $link) {
110                 $link = "$basedir/$link" unless $link =~ m#^/#;
111                 $dep{$link}++;
112         }
113         open my $in, '<', $f or die "Could not read $f";
114         while (<$in>) {
115                 if (/^\s*#\s*include\s*"([^"]+)"/) {
116                         my $inc = $1;
117                         next if $inc eq 'inspircd_version.h' && $f eq '../include/inspircd.h';
118                         my $found = 0;
119                         for my $loc ("$basedir/$inc", "../include/$inc") {
120                                 next unless -e $loc;
121                                 $found++;
122                                 $dep{$_}++ for split / /, gendep $loc;
123                                 $loc =~ s#^\.\./##;
124                                 $dep{$loc}++;
125                         }
126                         if ($found == 0 && $inc ne 'inspircd_win32wrapper.h') {
127                                 print STDERR "WARNING: could not find header $inc for $f\n";
128                         } elsif ($found > 1 && $basedir ne '../include') {
129                                 print STDERR "WARNING: ambiguous include $inc in $f\n";
130                         }
131                 }
132         }
133         close $in;
134         $f2dep{$f} = join ' ', sort keys %dep;
135         $f2dep{$f};
136 }
137
138 sub dep_cpp($$) {
139         my($file, $out) = @_;
140         gendep $file;
141
142         print MAKE "$out: $file $f2dep{$file}\n";
143         print MAKE "\t@\$(SOURCEPATH)/make/unit-cc.pl \$(VERBOSE) \$\@ \$< \$>\n";
144 }
145
146 sub dep_dir($) {
147         my($dir) = @_;
148         my @ofiles;
149         opendir DIR, $dir;
150         for my $file (readdir DIR) {
151                 next unless $file =~ /(.*)\.cpp$/;
152                 my $ofile = find_output "$dir/$file";
153                 dep_cpp "$dir/$file", $ofile;
154                 push @ofiles, $ofile;
155         }
156         closedir DIR;
157         if (@ofiles) {
158                 my $ofiles = join ' ', @ofiles;
159                 print MAKE "$dir.so: $ofiles\n\t\$(RUNCC) \$(PICLDFLAGS) -o \$\@ \$^ \$>\n";
160                 return 1;
161         } else {
162                 return 0;
163         }
164 }
165