]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/calcdep.pl
Fix unnecessary begin blocks in Perl source files.
[user/henk/code/inspircd.git] / make / calcdep.pl
1 #!/usr/bin/env perl
2 #
3 # InspIRCd -- Internet Relay Chat Daemon
4 #
5 #   Copyright (C) 2014-2015 Attila Molnar <attilamolnar@hush.com>
6 #   Copyright (C) 2013, 2015-2019 Sadie Powell <sadie@witchery.services>
7 #   Copyright (C) 2012 Robby <robby@chatbelgie.be>
8 #   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
9 #
10 # This file is part of InspIRCd.  InspIRCd is free software: you can
11 # redistribute it and/or modify it under the terms of the GNU General Public
12 # License as published by the Free Software Foundation, version 2.
13 #
14 # This program is distributed in the hope that it will be useful, but WITHOUT
15 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17 # details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 #
22
23
24 use strict;
25 use warnings FATAL => qw(all);
26
27 use File::Basename qw(basename dirname);
28 use FindBin        qw($RealDir);
29
30 use lib dirname $RealDir;
31 use make::common;
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         create_directory(BUILDPATH, 0770) or die "Could not create build directory: $!";
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         run_dynamic();
61         close MAKE;
62 }
63
64 sub run_dynamic() {
65         print MAKE <<END;
66 # DO NOT EDIT THIS FILE
67 # It is autogenerated by make/calcdep.pl, and will be overwritten
68 # every time you rerun make in the main directory
69 VPATH = \$(SOURCEPATH)/src
70
71 bad-target:
72         \@echo "This Makefile must be run by a sub-make from the source"
73         \@echo "in order to set the correct environment variables"
74         \@exit 1
75
76 all: inspircd modules
77
78 END
79         my(@core_deps, @modlist);
80         for my $file (<*.cpp>, <socketengines/*.cpp>, "threadengines/threadengine_pthread.cpp") {
81                 my $out = find_output $file;
82                 dep_cpp $file, $out, 'gen-o';
83                 next if $file =~ m#^socketengines/# && $file ne "socketengines/socketengine_$ENV{SOCKETENGINE}.cpp";
84                 # Having a module in the src directory is a bad idea because it will be linked to the core binary
85                 if ($file =~ /^(m|core)_.*\.cpp/) {
86                         my $correctsubdir = ($file =~ /^m_/ ? "modules" : "coremods");
87                         print "Error: module $file is in the src directory, put it in src/$correctsubdir instead!\n";
88                         exit 1;
89                 }
90                 push @core_deps, $out;
91         }
92
93         foreach my $directory (qw(coremods modules)) {
94                 opendir(my $moddir, $directory);
95                 for my $file (sort readdir $moddir) {
96                         next if $file =~ /^\./;
97                         if ($directory eq 'modules' && -e "modules/extra/$file" && !-l "modules/$file") {
98                                 # Incorrect symlink?
99                                 print "Replacing symlink for $file found in modules/extra\n";
100                                 rename "modules/$file", "modules/$file~";
101                                 symlink "extra/$file", "modules/$file";
102                         }
103                         if ($file =~ /^(?:core|m)_/ && -d "$directory/$file" && dep_dir "$directory/$file", "modules/$file") {
104                                 mkdir "${\BUILDPATH}/obj/$file";
105                                 push @modlist, "modules/$file.so";
106                         }
107                         if ($file =~ /^.*\.cpp$/) {
108                                 my $out = dep_so "$directory/$file";
109                                 push @modlist, $out;
110                         }
111                 }
112         }
113
114         my $core_mk = join ' ', @core_deps;
115         my $mods = join ' ', @modlist;
116         print MAKE <<END;
117
118 bin/inspircd: $core_mk
119         @\$(SOURCEPATH)/make/unit-cc.pl core-ld \$\@ \$^ \$>
120
121 inspircd: bin/inspircd
122
123 modules: $mods
124
125 .PHONY: all bad-target inspircd modules
126
127 END
128 }
129
130 sub find_output {
131         my $file = shift;
132         my($path,$base) = $file =~ m#^((?:.*/)?)([^/]+)\.cpp# or die "Bad file $file";
133         if ($path eq 'modules/' || $path eq 'coremods/') {
134                 return "modules/$base.so";
135         } elsif ($path eq '' || $path eq 'modes/' || $path =~ /^[a-z]+engines\/$/) {
136                 return "obj/$base.o";
137         } elsif ($path =~ m#modules/(m_.*)/# || $path =~ m#coremods/(core_.*)/#) {
138                 return "obj/$1/$base.o";
139         } else {
140                 die "Can't determine output for $file";
141         }
142 }
143
144 sub gendep($) {
145         my $f = shift;
146         my $basedir = $f =~ m#(.*)/# ? $1 : '.';
147         return $f2dep{$f} if exists $f2dep{$f};
148         $f2dep{$f} = '';
149         my %dep;
150         my $link = readlink $f;
151         if (defined $link) {
152                 $link = "$basedir/$link" unless $link =~ m#^/#;
153                 $dep{$link}++;
154         }
155         open my $in, '<', $f or die "Could not read $f";
156         while (<$in>) {
157                 if (/^\s*#\s*include\s*"([^"]+)"/) {
158                         my $inc = $1;
159                         next if $inc eq 'config.h' && $f eq '../include/inspircd.h';
160                         my $found = 0;
161                         for my $loc ("$basedir/$inc", "../include/$inc") {
162                                 next unless -e $loc;
163                                 $found++;
164                                 $dep{$_}++ for split / /, gendep $loc;
165                                 $loc =~ s#^\.\./##;
166                                 $dep{$loc}++;
167                         }
168                         if ($found == 0 && $inc ne 'inspircd_win32wrapper.h') {
169                                 print STDERR "WARNING: could not find header $inc for $f\n";
170                         } elsif ($found > 1 && $basedir ne '../include') {
171                                 print STDERR "WARNING: ambiguous include $inc in $f\n";
172                         }
173                 }
174         }
175         close $in;
176         $f2dep{$f} = join ' ', sort keys %dep;
177         $f2dep{$f};
178 }
179
180 sub dep_cpp($$$) {
181         my($file, $out, $type) = @_;
182         gendep $file;
183
184         print MAKE "$out: $file $f2dep{$file}\n";
185         print MAKE "\t@\$(SOURCEPATH)/make/unit-cc.pl $type \$\@ \$(SOURCEPATH)/src/$file \$>\n";
186 }
187
188 sub dep_so($) {
189         my($file) = @_;
190         my $out = find_output $file;
191
192         my $name = basename $out, '.so';
193         print MAKE ".PHONY: $name\n";
194         print MAKE "$name: $out\n";
195
196         dep_cpp $file, $out, 'gen-so';
197         return $out;
198 }
199
200 sub dep_dir($$) {
201         my($dir, $outdir) = @_;
202         my @ofiles;
203         opendir DIR, $dir;
204         for my $file (sort readdir DIR) {
205                 next unless $file =~ /(.*)\.cpp$/;
206                 my $ofile = find_output "$dir/$file";
207                 dep_cpp "$dir/$file", $ofile, 'gen-o';
208                 push @ofiles, $ofile;
209         }
210         closedir DIR;
211         if (@ofiles) {
212                 my $ofiles = join ' ', @ofiles;
213                 my $name = basename $outdir;
214                 print MAKE ".PHONY: $name\n";
215                 print MAKE "$name: $outdir.so\n";
216                 print MAKE "$outdir.so: $ofiles\n";
217                 print MAKE "\t@\$(SOURCEPATH)/make/unit-cc.pl link-dir \$\@ ${\SOURCEPATH}/src/$dir \$^ \$>\n";
218                 return 1;
219         } else {
220                 return 0;
221         }
222 }
223