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