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