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