]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/calcdep.pl
Remove .*.d dependency garbage, and use a dedicated build directory
[user/henk/code/inspircd.git] / make / calcdep.pl
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use Getopt::Long;
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         mkdir 'bin';
22         mkdir 'obj';
23         mkdir 'modules';
24         symlink "$ENV{SOURCEPATH}/include", 'include';
25         $build = `pwd`;
26         chomp $build;
27         open MAKE, '>real.mk' or die "Could not write real.mk: $!";
28         chdir "$ENV{SOURCEPATH}/src";
29
30         print MAKE <<END;
31 # DO NOT EDIT
32 # Autogenerated by calcdep
33 VPATH = \$(SOURCEPATH)/src
34
35 all: bin/inspircd modules
36
37 END
38         my @core_deps;
39         for my $file (<*.cpp>, <modes/*.cpp>, "socketengines/$ENV{SOCKETENGINE}.cpp", "threadengines/threadengine_pthread.cpp") {
40                 my $out = find_output $file;
41                 dep_cpp $file, $out;
42                 push @core_deps, $out;
43         }
44         
45         my @modlist;
46         for my $file (<commands/*.cpp>, <modules/*.cpp>) {
47                 my $out = find_output $file;
48                 dep_cpp $file, $out;
49                 push @modlist, $out;
50         }
51
52         opendir my $moddir, 'modules';
53         for my $dir (readdir $moddir) {
54                 next unless $dir =~ /^m_/ && -d "modules/$dir";
55                 mkdir "$build/obj/$dir";
56                 dep_dir "modules/$dir";
57                 push @modlist, "modules/$dir.so";
58         }
59         
60         my $core_mk = join ' ', @core_deps;
61         my $mods = join ' ', @modlist;
62         print MAKE <<END;
63
64 bin/inspircd: $core_mk
65         \$(RUNCC) -o \$\@ \$(CORELDFLAGS) \$(LDLIBS) \$^
66
67 inspircd: bin/inspircd
68 modules: $mods
69
70 .PHONY: inspircd modules
71
72 END
73 }
74
75 sub find_output($) {
76         my $file = shift;
77         my($path,$base) = $file =~ m#^((?:.*/)?)([^/]+)\.cpp# or die "Bad file $file";
78         if ($path eq 'modules/' || $path eq 'commands/') {
79                 return "modules/$base.so";
80         } elsif ($path eq '' || $path eq 'modes/' || $path =~ /^[a-z]+engines\/$/) {
81                 return "obj/$base.o";
82         } elsif ($path =~ m#modules/(m_.*)/#) {
83                 return "obj/$1/$base.o";
84         } else {
85                 die "Can't determine output for $file";
86         }
87 }
88
89 sub gendep($) {
90         my $f = shift;
91         my $basedir = $f =~ m#(.*)/# ? $1 : '.';
92         return $f2dep{$f} if exists $f2dep{$f};
93         $f2dep{$f} = '';
94         my %dep;
95         my $link = readlink $f;
96         if (defined $link) {
97                 $link = "$basedir/$link" unless $link =~ m#^/#;
98                 $dep{$link}++;
99         }
100         open my $in, '<', $f or die "Could not read $f";
101         while (<$in>) {
102                 if (/^\s*#\s*include\s*"([^"]+)"/) {
103                         my $inc = $1;
104                         next if $inc eq 'inspircd_version.h' && $f eq '../include/inspircd.h';
105                         my $found = 0;
106                         for my $loc ("$basedir/$inc", "../include/$inc") {
107                                 next unless -e $loc;
108                                 $found++;
109                                 $dep{$_}++ for split / /, gendep $loc;
110                                 $loc =~ s#^\.\./##;
111                                 $dep{$loc}++;
112                         }
113                         if ($found == 0 && $inc ne 'inspircd_win32wrapper.h') {
114                                 print STDERR "WARNING: could not find header $inc for $f\n";
115                         } elsif ($found > 1 && $basedir ne '../include') {
116                                 print STDERR "WARNING: ambiguous include $inc in $f\n";
117                         }
118                 }
119         }
120         close $in;
121         $f2dep{$f} = join ' ', sort keys %dep;
122         $f2dep{$f};
123 }
124
125 sub dep_cpp($$) {
126         my($file, $out) = @_;
127         gendep $file;
128
129         print MAKE "$out: $file $f2dep{$file}\n";
130         print MAKE "\t@\$(SOURCEPATH)/make/unit-cc.pl \$(VERBOSE) \$< \$\@\n";
131 }
132
133 sub dep_dir($) {
134         my($dir) = @_;
135         my @ofiles;
136         opendir DIR, $dir;
137         for my $file (readdir DIR) {
138                 next unless $file =~ /(.*)\.cpp$/;
139                 my $ofile = find_output "$dir/$file";
140                 dep_cpp "$dir/$file", $ofile;
141                 push @ofiles, $ofile;
142         }
143         closedir DIR;
144         my $ofiles = join ' ', @ofiles;
145         print MAKE "$dir.so: $ofiles\n\t\$(RUNCC) \$(PICLDFLAGS) -o \$\@ \$^\n";
146 }
147