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