]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/calcdep.pl
PURE_STATIC improvements: Allow modules to be reloaded, generate linker arguments
[user/henk/code/inspircd.git] / make / calcdep.pl
1 #!/usr/bin/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_dir($);
10 sub run();
11
12 my %f2dep;
13
14 run;
15 exit 0;
16
17 sub run() {
18         my $build = $ENV{BUILDPATH};
19         mkdir $build;
20         chdir $build or die "Could not open build directory: $!";
21         symlink "$ENV{SOURCEPATH}/include", 'include';
22         mkdir $_ for qw/bin modules obj/;
23 # BSD make has a horribly annoying bug resulting in an extra chdir of the make process
24 # Create symlinks to work around it
25         symlink "../$_", "obj/$_" for qw/bin modules obj/;
26
27         $build = getcwd();
28         open MAKE, '>real.mk' or die "Could not write real.mk: $!";
29         chdir "$ENV{SOURCEPATH}/src";
30
31         if ($ENV{PURE_STATIC}) {
32                 run_static();
33         } else {
34                 run_dynamic();
35         }
36         close MAKE;
37 }
38
39 sub run_dynamic() {
40         my $build = $ENV{BUILDPATH};
41         print MAKE <<END;
42 # DO NOT EDIT THIS FILE
43 # It is autogenerated by make/calcdep.pl, and will be overwritten
44 # every time you rerun make in the main directory
45 VPATH = \$(SOURCEPATH)/src
46
47 bad-target:
48         \@echo "This Makefile must be run by a sub-make from the source"
49         \@echo "in order to set the correct environment variables"
50         \@exit 1
51
52 all: inspircd commands modules
53
54 END
55         my(@core_deps, @cmdlist, @modlist);
56         for my $file (<*.cpp>, <modes/*.cpp>, <socketengines/*.cpp>, "threadengines/threadengine_pthread.cpp") {
57                 my $out = find_output $file;
58                 dep_cpp $file, $out;
59                 next if $file =~ m#^socketengines/# && $file ne "socketengines/$ENV{SOCKETENGINE}.cpp";
60                 push @core_deps, $out;
61         }
62
63         for my $file (<commands/*.cpp>) {
64                 my $out = find_output $file;
65                 dep_cpp $file, $out;
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 = find_output "modules/$file";
84                         dep_cpp "modules/$file", $out;
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         \$(RUNCC) -o \$\@ \$(CORELDFLAGS) \$(LDLIBS) \$^ \$>
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;
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 -f\$(VERBOSE) \$\@ \$^ \$>
142
143 bin/inspircd: obj/ld-extra.cmd $core_mk
144         \@\$(SOURCEPATH)/make/unit-cc.pl -l\$(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) = @_;
205         gendep $file;
206
207         print MAKE "$out: $file $f2dep{$file}\n";
208         print MAKE "\t@\$(SOURCEPATH)/make/unit-cc.pl \$(VERBOSE) \$\@ \$< \$>\n";
209 }
210
211 sub dep_dir($) {
212         my($dir) = @_;
213         my @ofiles;
214         opendir DIR, $dir;
215         for my $file (sort readdir DIR) {
216                 next unless $file =~ /(.*)\.cpp$/;
217                 my $ofile = find_output "$dir/$file";
218                 dep_cpp "$dir/$file", $ofile;
219                 push @ofiles, $ofile;
220         }
221         closedir DIR;
222         if (@ofiles) {
223                 my $ofiles = join ' ', @ofiles;
224                 print MAKE "$dir.so: $ofiles\n\t\$(RUNCC) \$(PICLDFLAGS) -o \$\@ \$^ \$>\n";
225                 return 1;
226         } else {
227                 return 0;
228         }
229 }
230