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