]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/calcdep.pl
Recreate the build/include symlink so that changing the build location works as expected
[user/henk/code/inspircd.git] / make / calcdep.pl
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use POSIX qw(getcwd);
5
6 sub find_output;
7 sub gendep($);
8 sub dep_cpp($$$);
9 sub dep_so($);
10 sub dep_dir($);
11 sub run();
12
13 my %f2dep;
14
15 run;
16 exit 0;
17
18 sub run() {
19         my $build = $ENV{BUILDPATH};
20         mkdir $build;
21         chdir $build or die "Could not open build directory: $!";
22         unlink 'include';
23         symlink "$ENV{SOURCEPATH}/include", 'include';
24         mkdir $_ for qw/bin modules obj/;
25 # BSD make has a horribly annoying bug resulting in an extra chdir of the make process
26 # Create symlinks to work around it
27         symlink "../$_", "obj/$_" for qw/bin modules obj/;
28
29         $build = getcwd();
30         open MAKE, '>real.mk' or die "Could not write real.mk: $!";
31         chdir "$ENV{SOURCEPATH}/src";
32
33         if ($ENV{PURE_STATIC}) {
34                 run_static();
35         } else {
36                 run_dynamic();
37         }
38         close MAKE;
39 }
40
41 sub run_dynamic() {
42         my $build = $ENV{BUILDPATH};
43         print MAKE <<END;
44 # DO NOT EDIT THIS FILE
45 # It is autogenerated by make/calcdep.pl, and will be overwritten
46 # every time you rerun make in the main directory
47 VPATH = \$(SOURCEPATH)/src
48
49 bad-target:
50         \@echo "This Makefile must be run by a sub-make from the source"
51         \@echo "in order to set the correct environment variables"
52         \@exit 1
53
54 all: inspircd commands modules
55
56 END
57         my(@core_deps, @cmdlist, @modlist);
58         for my $file (<*.cpp>, <modes/*.cpp>, <socketengines/*.cpp>, "threadengines/threadengine_pthread.cpp") {
59                 my $out = find_output $file;
60                 dep_cpp $file, $out, 'gen-o';
61                 next if $file =~ m#^socketengines/# && $file ne "socketengines/$ENV{SOCKETENGINE}.cpp";
62                 push @core_deps, $out;
63         }
64
65         for my $file (<commands/*.cpp>) {
66                 my $out = dep_so $file;
67                 push @cmdlist, $out;
68         }
69
70         opendir my $moddir, 'modules';
71         for my $file (sort readdir $moddir) {
72                 next if $file =~ /^\./;
73                 if (-e "modules/extra/$file" && !-l "modules/$file") {
74                         # Incorrect symlink?
75                         print "Replacing symlink for $file found in modules/extra\n";
76                         rename "modules/$file", "modules/$file~";
77                         symlink "extra/$file", "modules/$file";
78                 }
79                 if ($file =~ /^m_/ && -d "modules/$file" && dep_dir "modules/$file") {
80                         mkdir "$build/obj/$file";
81                         push @modlist, "modules/$file.so";
82                 }
83                 if ($file =~ /^m_.*\.cpp$/) {
84                         my $out = dep_so "modules/$file";
85                         push @modlist, $out;
86                 }
87         }
88         
89         my $core_mk = join ' ', @core_deps;
90         my $cmds = join ' ', @cmdlist;
91         my $mods = join ' ', @modlist;
92         print MAKE <<END;
93
94 bin/inspircd: $core_mk
95         @\$(SOURCEPATH)/make/unit-cc.pl core-ld\$(VERBOSE) \$\@ \$^ \$>
96
97 inspircd: bin/inspircd
98
99 commands: $cmds
100
101 modules: $mods
102
103 .PHONY: all bad-target inspircd commands modules
104
105 END
106 }
107
108 sub run_static() {
109         print MAKE <<END;
110 # DO NOT EDIT THIS FILE
111 # It is autogenerated by make/calcdep.pl, and will be overwritten
112 # every time you rerun make in the main directory
113 VPATH = \$(SOURCEPATH)/src
114
115 bad-target:
116         \@echo "This Makefile must be run by a sub-make from the source"
117         \@echo "in order to set the correct environment variables"
118         \@exit 1
119
120 all: inspircd
121
122 END
123         my(@deps, @srcs);
124         for my $file (<*.cpp>, <modes/*.cpp>, <socketengines/*.cpp>, <commands/*.cpp>,
125                         <modules/*.cpp>, <modules/m_*/*.cpp>, "threadengines/threadengine_pthread.cpp") {
126                 my $out = find_output $file, 1;
127                 if ($out =~ m#obj/([^/]+)/[^/]+.o$#) {
128                         mkdir "$ENV{BUILDPATH}/obj/$1";
129                 }
130                 dep_cpp $file, $out, 'gen-o';
131                 next if $file =~ m#^socketengines/# && $file ne "socketengines/$ENV{SOCKETENGINE}.cpp";
132                 push @deps, $out;
133                 push @srcs, $file;
134         }
135
136         my $core_mk = join ' ', @deps;
137         my $core_src = join ' ', @srcs;
138         print MAKE <<END;
139
140 obj/ld-extra.cmd: $core_src
141         \@\$(SOURCEPATH)/make/unit-cc.pl gen-ld\$(VERBOSE) \$\@ \$^ \$>
142
143 bin/inspircd: obj/ld-extra.cmd $core_mk
144         \@\$(SOURCEPATH)/make/unit-cc.pl static-ld\$(VERBOSE) \$\@ \$^ \$>
145
146 inspircd: bin/inspircd
147
148 .PHONY: all bad-target inspircd
149
150 END
151 }
152
153 sub find_output {
154         my($file, $static) = @_;
155         my($path,$base) = $file =~ m#^((?:.*/)?)([^/]+)\.cpp# or die "Bad file $file";
156         if ($path eq 'modules/' || $path eq 'commands/') {
157                 return $static ? "obj/$base.o" : "modules/$base.so";
158         } elsif ($path eq '' || $path eq 'modes/' || $path =~ /^[a-z]+engines\/$/) {
159                 return "obj/$base.o";
160         } elsif ($path =~ m#modules/(m_.*)/#) {
161                 return "obj/$1/$base.o";
162         } else {
163                 die "Can't determine output for $file";
164         }
165 }
166
167 sub gendep($) {
168         my $f = shift;
169         my $basedir = $f =~ m#(.*)/# ? $1 : '.';
170         return $f2dep{$f} if exists $f2dep{$f};
171         $f2dep{$f} = '';
172         my %dep;
173         my $link = readlink $f;
174         if (defined $link) {
175                 $link = "$basedir/$link" unless $link =~ m#^/#;
176                 $dep{$link}++;
177         }
178         open my $in, '<', $f or die "Could not read $f";
179         while (<$in>) {
180                 if (/^\s*#\s*include\s*"([^"]+)"/) {
181                         my $inc = $1;
182                         next if $inc eq 'inspircd_version.h' && $f eq '../include/inspircd.h';
183                         my $found = 0;
184                         for my $loc ("$basedir/$inc", "../include/$inc") {
185                                 next unless -e $loc;
186                                 $found++;
187                                 $dep{$_}++ for split / /, gendep $loc;
188                                 $loc =~ s#^\.\./##;
189                                 $dep{$loc}++;
190                         }
191                         if ($found == 0 && $inc ne 'inspircd_win32wrapper.h') {
192                                 print STDERR "WARNING: could not find header $inc for $f\n";
193                         } elsif ($found > 1 && $basedir ne '../include') {
194                                 print STDERR "WARNING: ambiguous include $inc in $f\n";
195                         }
196                 }
197         }
198         close $in;
199         $f2dep{$f} = join ' ', sort keys %dep;
200         $f2dep{$f};
201 }
202
203 sub dep_cpp($$$) {
204         my($file, $out, $type) = @_;
205         gendep $file;
206
207         print MAKE "$out: $file $f2dep{$file}\n";
208         print MAKE "\t@\$(SOURCEPATH)/make/unit-cc.pl $type\$(VERBOSE) \$\@ \$< \$>\n";
209 }
210
211 sub dep_so($) {
212         my($file) = @_;
213         my $out = find_output $file;
214         my $split = find_output $file, 1;
215
216         if ($ENV{SPLIT_CC}) {
217                 dep_cpp $file, $split, 'gen-o';
218                 print MAKE "$out: $split\n";
219                 print MAKE "\t@\$(SOURCEPATH)/make/unit-cc.pl link-so\$(VERBOSE) \$\@ \$< \$>\n";
220         } else {
221                 dep_cpp $file, $out, 'gen-so';
222         }
223         return $out;
224 }
225
226 sub dep_dir($) {
227         my($dir) = @_;
228         my @ofiles;
229         opendir DIR, $dir;
230         for my $file (sort readdir DIR) {
231                 next unless $file =~ /(.*)\.cpp$/;
232                 my $ofile = find_output "$dir/$file";
233                 dep_cpp "$dir/$file", $ofile, 'gen-o';
234                 push @ofiles, $ofile;
235         }
236         closedir DIR;
237         if (@ofiles) {
238                 my $ofiles = join ' ', @ofiles;
239                 print MAKE "$dir.so: $ofiles\n";
240                 print MAKE "\t@\$(SOURCEPATH)/make/unit-cc.pl link-dir\$(VERBOSE) \$\@ \$^ \$>\n";
241                 return 1;
242         } else {
243                 return 0;
244         }
245 }
246