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