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