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