]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/calcdep.pl
calcdep: Remove a workaround for a very old version of BSD Make.
[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 use strict;
23 use warnings;
24 use POSIX qw(getcwd);
25
26 sub find_output;
27 sub gendep($);
28 sub dep_cpp($$$);
29 sub dep_so($);
30 sub dep_dir($$);
31 sub run();
32
33 my %f2dep;
34
35 run;
36 exit 0;
37
38 sub run() {
39         my $build = $ENV{BUILDPATH};
40         mkdir $build;
41         chdir $build or die "Could not open build directory: $!";
42         unlink 'include';
43         symlink "$ENV{SOURCEPATH}/include", 'include';
44         mkdir $_ for qw/bin modules obj/;
45
46         $build = getcwd();
47         open MAKE, '>real.mk' or die "Could not write real.mk: $!";
48         chdir "$ENV{SOURCEPATH}/src";
49
50         if ($ENV{PURE_STATIC}) {
51                 run_static();
52         } else {
53                 run_dynamic();
54         }
55         close MAKE;
56 }
57
58 sub run_dynamic() {
59         my $build = $ENV{BUILDPATH};
60         print MAKE <<END;
61 # DO NOT EDIT THIS FILE
62 # It is autogenerated by make/calcdep.pl, and will be overwritten
63 # every time you rerun make in the main directory
64 VPATH = \$(SOURCEPATH)/src
65
66 bad-target:
67         \@echo "This Makefile must be run by a sub-make from the source"
68         \@echo "in order to set the correct environment variables"
69         \@exit 1
70
71 all: inspircd coremods modules
72
73 END
74         my(@core_deps, @cmodlist, @modlist);
75         for my $file (<*.cpp>, <modes/*.cpp>, <socketengines/*.cpp>, "threadengines/threadengine_pthread.cpp") {
76                 my $out = find_output $file;
77                 dep_cpp $file, $out, 'gen-o';
78                 next if $file =~ m#^socketengines/# && $file ne "socketengines/socketengine_$ENV{SOCKETENGINE}.cpp";
79                 push @core_deps, $out;
80         }
81
82         opendir my $coremoddir, 'coremods';
83         for my $file (sort readdir $coremoddir) {
84                 next if $file =~ /^\./;
85                 if ($file =~ /^core_/ && -d "coremods/$file" && dep_dir "coremods/$file", "modules/$file") {
86                         mkdir "$build/obj/$file";
87                         push @cmodlist, "modules/$file.so";
88                 }
89                 if ($file =~ /^core_.*\.cpp$/) {
90                         my $out = dep_so "coremods/$file";
91                         push @cmodlist, $out;
92                 }
93         }
94
95         opendir my $moddir, 'modules';
96         for my $file (sort readdir $moddir) {
97                 next if $file =~ /^\./;
98                 if (-e "modules/extra/$file" && !-l "modules/$file") {
99                         # Incorrect symlink?
100                         print "Replacing symlink for $file found in modules/extra\n";
101                         rename "modules/$file", "modules/$file~";
102                         symlink "extra/$file", "modules/$file";
103                 }
104                 if ($file =~ /^m_/ && -d "modules/$file" && dep_dir "modules/$file", "modules/$file") {
105                         mkdir "$build/obj/$file";
106                         push @modlist, "modules/$file.so";
107                 }
108                 if ($file =~ /^m_.*\.cpp$/) {
109                         my $out = dep_so "modules/$file";
110                         push @modlist, $out;
111                 }
112         }
113         
114         my $core_mk = join ' ', @core_deps;
115         my $cmods = join ' ', @cmodlist;
116         my $mods = join ' ', @modlist;
117         print MAKE <<END;
118
119 bin/inspircd: $core_mk
120         @\$(SOURCEPATH)/make/unit-cc.pl core-ld\$(VERBOSE) \$\@ \$^ \$>
121
122 inspircd: bin/inspircd
123
124 coremods: $cmods
125
126 modules: $mods
127
128 .PHONY: all bad-target inspircd coremods modules
129
130 END
131 }
132
133 sub run_static() {
134         print MAKE <<END;
135 # DO NOT EDIT THIS FILE
136 # It is autogenerated by make/calcdep.pl, and will be overwritten
137 # every time you rerun make in the main directory
138 VPATH = \$(SOURCEPATH)/src
139
140 bad-target:
141         \@echo "This Makefile must be run by a sub-make from the source"
142         \@echo "in order to set the correct environment variables"
143         \@exit 1
144
145 all: inspircd
146
147 END
148         my(@deps, @srcs);
149         for my $file (<*.cpp>, <modes/*.cpp>, <socketengines/*.cpp>, <coremods/*.cpp>, <coremods/core_*/*.cpp>,
150                         <modules/*.cpp>, <modules/m_*/*.cpp>, "threadengines/threadengine_pthread.cpp") {
151                 my $out = find_output $file, 1;
152                 if ($out =~ m#obj/([^/]+)/[^/]+.o$#) {
153                         mkdir "$ENV{BUILDPATH}/obj/$1";
154                 }
155                 dep_cpp $file, $out, 'gen-o';
156                 next if $file =~ m#^socketengines/# && $file ne "socketengines/socketengine_$ENV{SOCKETENGINE}.cpp";
157                 push @deps, $out;
158                 push @srcs, $file;
159         }
160
161         my $core_mk = join ' ', @deps;
162         my $core_src = join ' ', @srcs;
163         print MAKE <<END;
164
165 obj/ld-extra.cmd: $core_src
166         \@\$(SOURCEPATH)/make/unit-cc.pl gen-ld\$(VERBOSE) \$\@ \$^ \$>
167
168 bin/inspircd: obj/ld-extra.cmd $core_mk
169         \@\$(SOURCEPATH)/make/unit-cc.pl static-ld\$(VERBOSE) \$\@ \$^ \$>
170
171 inspircd: bin/inspircd
172
173 .PHONY: all bad-target inspircd
174
175 END
176 }
177
178 sub find_output {
179         my($file, $static) = @_;
180         my($path,$base) = $file =~ m#^((?:.*/)?)([^/]+)\.cpp# or die "Bad file $file";
181         if ($path eq 'modules/' || $path eq 'coremods/') {
182                 return $static ? "obj/$base.o" : "modules/$base.so";
183         } elsif ($path eq '' || $path eq 'modes/' || $path =~ /^[a-z]+engines\/$/) {
184                 return "obj/$base.o";
185         } elsif ($path =~ m#modules/(m_.*)/# || $path =~ m#coremods/(core_.*)/#) {
186                 return "obj/$1/$base.o";
187         } else {
188                 die "Can't determine output for $file";
189         }
190 }
191
192 sub gendep($) {
193         my $f = shift;
194         my $basedir = $f =~ m#(.*)/# ? $1 : '.';
195         return $f2dep{$f} if exists $f2dep{$f};
196         $f2dep{$f} = '';
197         my %dep;
198         my $link = readlink $f;
199         if (defined $link) {
200                 $link = "$basedir/$link" unless $link =~ m#^/#;
201                 $dep{$link}++;
202         }
203         open my $in, '<', $f or die "Could not read $f";
204         while (<$in>) {
205                 if (/^\s*#\s*include\s*"([^"]+)"/) {
206                         my $inc = $1;
207                         next if $inc eq 'config.h' && $f eq '../include/inspircd.h';
208                         my $found = 0;
209                         for my $loc ("$basedir/$inc", "../include/$inc") {
210                                 next unless -e $loc;
211                                 $found++;
212                                 $dep{$_}++ for split / /, gendep $loc;
213                                 $loc =~ s#^\.\./##;
214                                 $dep{$loc}++;
215                         }
216                         if ($found == 0 && $inc ne 'inspircd_win32wrapper.h') {
217                                 print STDERR "WARNING: could not find header $inc for $f\n";
218                         } elsif ($found > 1 && $basedir ne '../include') {
219                                 print STDERR "WARNING: ambiguous include $inc in $f\n";
220                         }
221                 }
222         }
223         close $in;
224         $f2dep{$f} = join ' ', sort keys %dep;
225         $f2dep{$f};
226 }
227
228 sub dep_cpp($$$) {
229         my($file, $out, $type) = @_;
230         gendep $file;
231
232         print MAKE "$out: $file $f2dep{$file}\n";
233         print MAKE "\t@\$(SOURCEPATH)/make/unit-cc.pl $type\$(VERBOSE) \$\@ \$(SOURCEPATH)/src/$file \$>\n";
234 }
235
236 sub dep_so($) {
237         my($file) = @_;
238         my $out = find_output $file;
239
240         dep_cpp $file, $out, 'gen-so';
241         return $out;
242 }
243
244 sub dep_dir($$) {
245         my($dir, $outdir) = @_;
246         my @ofiles;
247         opendir DIR, $dir;
248         for my $file (sort readdir DIR) {
249                 next unless $file =~ /(.*)\.cpp$/;
250                 my $ofile = find_output "$dir/$file";
251                 dep_cpp "$dir/$file", $ofile, 'gen-o';
252                 push @ofiles, $ofile;
253         }
254         closedir DIR;
255         if (@ofiles) {
256                 my $ofiles = join ' ', @ofiles;
257                 print MAKE "$outdir.so: $ofiles\n";
258                 print MAKE "\t@\$(SOURCEPATH)/make/unit-cc.pl link-dir\$(VERBOSE) \$\@ \$^ \$>\n";
259                 return 1;
260         } else {
261                 return 0;
262         }
263 }
264