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