]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/calcdep.pl
Update make help, configure, and fix build of empty m_* directories
[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         mkdir 'bin';
22         mkdir 'obj';
23         mkdir 'modules';
24         symlink "$ENV{SOURCEPATH}/include", 'include';
25         $build = getcwd();
26         open MAKE, '>real.mk' or die "Could not write real.mk: $!";
27         chdir "$ENV{SOURCEPATH}/src";
28
29         print MAKE <<END;
30 # DO NOT EDIT
31 # Autogenerated by calcdep
32 VPATH = \$(SOURCEPATH)/src
33
34 all: bin/inspircd modules
35
36 END
37         my @core_deps;
38         for my $file (<*.cpp>, <modes/*.cpp>, "socketengines/$ENV{SOCKETENGINE}.cpp", "threadengines/threadengine_pthread.cpp") {
39                 my $out = find_output $file;
40                 dep_cpp $file, $out;
41                 push @core_deps, $out;
42         }
43         
44         my @modlist;
45         for my $file (<commands/*.cpp>, <modules/*.cpp>) {
46                 my $out = find_output $file;
47                 dep_cpp $file, $out;
48                 push @modlist, $out;
49         }
50
51         opendir my $moddir, 'modules';
52         for my $dir (readdir $moddir) {
53                 next unless $dir =~ /^m_/ && -d "modules/$dir";
54                 if (dep_dir "modules/$dir") {
55                         mkdir "$build/obj/$dir";
56                         push @modlist, "modules/$dir.so";
57                 }
58         }
59         
60         my $core_mk = join ' ', @core_deps;
61         my $mods = join ' ', @modlist;
62         print MAKE <<END;
63
64 bin/inspircd: $core_mk
65         \$(RUNCC) -o \$\@ \$(CORELDFLAGS) \$(LDLIBS) \$^
66
67 inspircd: bin/inspircd
68 modules: $mods
69
70 .PHONY: inspircd modules
71
72 END
73 }
74
75 sub find_output($) {
76         my $file = shift;
77         my($path,$base) = $file =~ m#^((?:.*/)?)([^/]+)\.cpp# or die "Bad file $file";
78         if ($path eq 'modules/' || $path eq 'commands/') {
79                 return "modules/$base.so";
80         } elsif ($path eq '' || $path eq 'modes/' || $path =~ /^[a-z]+engines\/$/) {
81                 return "obj/$base.o";
82         } elsif ($path =~ m#modules/(m_.*)/#) {
83                 return "obj/$1/$base.o";
84         } else {
85                 die "Can't determine output for $file";
86         }
87 }
88
89 sub gendep($) {
90         my $f = shift;
91         my $basedir = $f =~ m#(.*)/# ? $1 : '.';
92         return $f2dep{$f} if exists $f2dep{$f};
93         $f2dep{$f} = '';
94         my %dep;
95         my $link = readlink $f;
96         if (defined $link) {
97                 $link = "$basedir/$link" unless $link =~ m#^/#;
98                 $dep{$link}++;
99         }
100         open my $in, '<', $f or die "Could not read $f";
101         while (<$in>) {
102                 if (/^\s*#\s*include\s*"([^"]+)"/) {
103                         my $inc = $1;
104                         next if $inc eq 'inspircd_version.h' && $f eq '../include/inspircd.h';
105                         my $found = 0;
106                         for my $loc ("$basedir/$inc", "../include/$inc") {
107                                 next unless -e $loc;
108                                 $found++;
109                                 $dep{$_}++ for split / /, gendep $loc;
110                                 $loc =~ s#^\.\./##;
111                                 $dep{$loc}++;
112                         }
113                         if ($found == 0 && $inc ne 'inspircd_win32wrapper.h') {
114                                 print STDERR "WARNING: could not find header $inc for $f\n";
115                         } elsif ($found > 1 && $basedir ne '../include') {
116                                 print STDERR "WARNING: ambiguous include $inc in $f\n";
117                         }
118                 }
119         }
120         close $in;
121         $f2dep{$f} = join ' ', sort keys %dep;
122         $f2dep{$f};
123 }
124
125 sub dep_cpp($$) {
126         my($file, $out) = @_;
127         gendep $file;
128
129         print MAKE "$out: $file $f2dep{$file}\n";
130         print MAKE "\t@\$(SOURCEPATH)/make/unit-cc.pl \$(VERBOSE) \$< \$\@\n";
131 }
132
133 sub dep_dir($) {
134         my($dir) = @_;
135         my @ofiles;
136         opendir DIR, $dir;
137         for my $file (readdir DIR) {
138                 next unless $file =~ /(.*)\.cpp$/;
139                 my $ofile = find_output "$dir/$file";
140                 dep_cpp "$dir/$file", $ofile;
141                 push @ofiles, $ofile;
142         }
143         closedir DIR;
144         if (@ofiles) {
145                 my $ofiles = join ' ', @ofiles;
146                 print MAKE "$dir.so: $ofiles\n\t\$(RUNCC) \$(PICLDFLAGS) -o \$\@ \$^\n";
147                 return 1;
148         } else {
149                 return 0;
150         }
151 }
152