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