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