]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/calcdep.pl
calcdep: Clean up some unused/duplicate variables.
[user/henk/code/inspircd.git] / make / calcdep.pl
1 #!/usr/bin/env perl
2
3 #
4 # InspIRCd -- Internet Relay Chat Daemon
5 #
6 #   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
7 #
8 # This file is part of InspIRCd.  InspIRCd is free software: you can
9 # redistribute it and/or modify it under the terms of the GNU General Public
10 # License as published by the Free Software Foundation, version 2.
11 #
12 # This program is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15 # details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20
21
22 BEGIN {
23         require 5.10.0;
24         unless (-f 'configure') {
25                 print "Error: $0 must be run from the main source directory!\n";
26                 exit 1;
27         }
28 }
29
30 use strict;
31 use warnings FATAL => qw(all);
32
33 use constant {
34         BUILDPATH  => $ENV{BUILDPATH},
35         SOURCEPATH => $ENV{SOURCEPATH}
36 };
37
38 sub find_output;
39 sub gendep($);
40 sub dep_cpp($$$);
41 sub dep_so($);
42 sub dep_dir($$);
43 sub run();
44
45 my %f2dep;
46
47 run;
48 exit 0;
49
50 sub run() {
51         mkdir BUILDPATH;
52         chdir BUILDPATH or die "Could not open build directory: $!";
53         unlink 'include';
54         symlink "${\SOURCEPATH}/include", 'include';
55         mkdir $_ for qw/bin modules obj/;
56
57         open MAKE, '>real.mk' or die "Could not write real.mk: $!";
58         chdir "${\SOURCEPATH}/src";
59
60         if ($ENV{PURE_STATIC}) {
61                 run_static();
62         } else {
63                 run_dynamic();
64         }
65         close MAKE;
66 }
67
68 sub run_dynamic() {
69         print MAKE <<END;
70 # DO NOT EDIT THIS FILE
71 # It is autogenerated by make/calcdep.pl, and will be overwritten
72 # every time you rerun make in the main directory
73 VPATH = \$(SOURCEPATH)/src
74
75 bad-target:
76         \@echo "This Makefile must be run by a sub-make from the source"
77         \@echo "in order to set the correct environment variables"
78         \@exit 1
79
80 all: inspircd coremods modules
81
82 END
83         my(@core_deps, @cmodlist, @modlist);
84         for my $file (<*.cpp>, <modes/*.cpp>, <socketengines/*.cpp>, "threadengines/threadengine_pthread.cpp") {
85                 my $out = find_output $file;
86                 dep_cpp $file, $out, 'gen-o';
87                 next if $file =~ m#^socketengines/# && $file ne "socketengines/socketengine_$ENV{SOCKETENGINE}.cpp";
88                 push @core_deps, $out;
89         }
90
91         opendir my $coremoddir, 'coremods';
92         for my $file (sort readdir $coremoddir) {
93                 next if $file =~ /^\./;
94                 if ($file =~ /^core_/ && -d "coremods/$file" && dep_dir "coremods/$file", "modules/$file") {
95                         mkdir "${\BUILDPATH}/obj/$file";
96                         push @cmodlist, "modules/$file.so";
97                 }
98                 if ($file =~ /^core_.*\.cpp$/) {
99                         my $out = dep_so "coremods/$file";
100                         push @cmodlist, $out;
101                 }
102         }
103
104         opendir my $moddir, 'modules';
105         for my $file (sort readdir $moddir) {
106                 next if $file =~ /^\./;
107                 if (-e "modules/extra/$file" && !-l "modules/$file") {
108                         # Incorrect symlink?
109                         print "Replacing symlink for $file found in modules/extra\n";
110                         rename "modules/$file", "modules/$file~";
111                         symlink "extra/$file", "modules/$file";
112                 }
113                 if ($file =~ /^m_/ && -d "modules/$file" && dep_dir "modules/$file", "modules/$file") {
114                         mkdir "${\BUILDPATH}/obj/$file";
115                         push @modlist, "modules/$file.so";
116                 }
117                 if ($file =~ /^m_.*\.cpp$/) {
118                         my $out = dep_so "modules/$file";
119                         push @modlist, $out;
120                 }
121         }
122         
123         my $core_mk = join ' ', @core_deps;
124         my $cmods = join ' ', @cmodlist;
125         my $mods = join ' ', @modlist;
126         print MAKE <<END;
127
128 bin/inspircd: $core_mk
129         @\$(SOURCEPATH)/make/unit-cc.pl core-ld\$(VERBOSE) \$\@ \$^ \$>
130
131 inspircd: bin/inspircd
132
133 coremods: $cmods
134
135 modules: $mods
136
137 .PHONY: all bad-target inspircd coremods modules
138
139 END
140 }
141
142 sub run_static() {
143         print MAKE <<END;
144 # DO NOT EDIT THIS FILE
145 # It is autogenerated by make/calcdep.pl, and will be overwritten
146 # every time you rerun make in the main directory
147 VPATH = \$(SOURCEPATH)/src
148
149 bad-target:
150         \@echo "This Makefile must be run by a sub-make from the source"
151         \@echo "in order to set the correct environment variables"
152         \@exit 1
153
154 all: inspircd
155
156 END
157         my(@deps, @srcs);
158         for my $file (<*.cpp>, <modes/*.cpp>, <socketengines/*.cpp>, <coremods/*.cpp>, <coremods/core_*/*.cpp>,
159                         <modules/*.cpp>, <modules/m_*/*.cpp>, "threadengines/threadengine_pthread.cpp") {
160                 my $out = find_output $file, 1;
161                 if ($out =~ m#obj/([^/]+)/[^/]+.o$#) {
162                         mkdir "${\BUILDPATH}/obj/$1";
163                 }
164                 dep_cpp $file, $out, 'gen-o';
165                 next if $file =~ m#^socketengines/# && $file ne "socketengines/socketengine_$ENV{SOCKETENGINE}.cpp";
166                 push @deps, $out;
167                 push @srcs, $file;
168         }
169
170         my $core_mk = join ' ', @deps;
171         my $core_src = join ' ', @srcs;
172         print MAKE <<END;
173
174 obj/ld-extra.cmd: $core_src
175         \@\$(SOURCEPATH)/make/unit-cc.pl gen-ld\$(VERBOSE) \$\@ \$^ \$>
176
177 bin/inspircd: obj/ld-extra.cmd $core_mk
178         \@\$(SOURCEPATH)/make/unit-cc.pl static-ld\$(VERBOSE) \$\@ \$^ \$>
179
180 inspircd: bin/inspircd
181
182 .PHONY: all bad-target inspircd
183
184 END
185 }
186
187 sub find_output {
188         my($file, $static) = @_;
189         my($path,$base) = $file =~ m#^((?:.*/)?)([^/]+)\.cpp# or die "Bad file $file";
190         if ($path eq 'modules/' || $path eq 'coremods/') {
191                 return $static ? "obj/$base.o" : "modules/$base.so";
192         } elsif ($path eq '' || $path eq 'modes/' || $path =~ /^[a-z]+engines\/$/) {
193                 return "obj/$base.o";
194         } elsif ($path =~ m#modules/(m_.*)/# || $path =~ m#coremods/(core_.*)/#) {
195                 return "obj/$1/$base.o";
196         } else {
197                 die "Can't determine output for $file";
198         }
199 }
200
201 sub gendep($) {
202         my $f = shift;
203         my $basedir = $f =~ m#(.*)/# ? $1 : '.';
204         return $f2dep{$f} if exists $f2dep{$f};
205         $f2dep{$f} = '';
206         my %dep;
207         my $link = readlink $f;
208         if (defined $link) {
209                 $link = "$basedir/$link" unless $link =~ m#^/#;
210                 $dep{$link}++;
211         }
212         open my $in, '<', $f or die "Could not read $f";
213         while (<$in>) {
214                 if (/^\s*#\s*include\s*"([^"]+)"/) {
215                         my $inc = $1;
216                         next if $inc eq 'config.h' && $f eq '../include/inspircd.h';
217                         my $found = 0;
218                         for my $loc ("$basedir/$inc", "../include/$inc") {
219                                 next unless -e $loc;
220                                 $found++;
221                                 $dep{$_}++ for split / /, gendep $loc;
222                                 $loc =~ s#^\.\./##;
223                                 $dep{$loc}++;
224                         }
225                         if ($found == 0 && $inc ne 'inspircd_win32wrapper.h') {
226                                 print STDERR "WARNING: could not find header $inc for $f\n";
227                         } elsif ($found > 1 && $basedir ne '../include') {
228                                 print STDERR "WARNING: ambiguous include $inc in $f\n";
229                         }
230                 }
231         }
232         close $in;
233         $f2dep{$f} = join ' ', sort keys %dep;
234         $f2dep{$f};
235 }
236
237 sub dep_cpp($$$) {
238         my($file, $out, $type) = @_;
239         gendep $file;
240
241         print MAKE "$out: $file $f2dep{$file}\n";
242         print MAKE "\t@\$(SOURCEPATH)/make/unit-cc.pl $type\$(VERBOSE) \$\@ \$(SOURCEPATH)/src/$file \$>\n";
243 }
244
245 sub dep_so($) {
246         my($file) = @_;
247         my $out = find_output $file;
248
249         dep_cpp $file, $out, 'gen-so';
250         return $out;
251 }
252
253 sub dep_dir($$) {
254         my($dir, $outdir) = @_;
255         my @ofiles;
256         opendir DIR, $dir;
257         for my $file (sort readdir DIR) {
258                 next unless $file =~ /(.*)\.cpp$/;
259                 my $ofile = find_output "$dir/$file";
260                 dep_cpp "$dir/$file", $ofile, 'gen-o';
261                 push @ofiles, $ofile;
262         }
263         closedir DIR;
264         if (@ofiles) {
265                 my $ofiles = join ' ', @ofiles;
266                 print MAKE "$outdir.so: $ofiles\n";
267                 print MAKE "\t@\$(SOURCEPATH)/make/unit-cc.pl link-dir\$(VERBOSE) \$\@ \$^ \$>\n";
268                 return 1;
269         } else {
270                 return 0;
271         }
272 }
273