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