summaryrefslogtreecommitdiff
path: root/make/calcdep.pl
blob: d8b4e292b21eab1801eec4ac8ff2f1ab84f57b51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;

my $basesrc = "$ENV{SOURCEPATH}/src";
my $baseinc = "$ENV{SOURCEPATH}/include";
my $baseout = `pwd`;
chomp $baseout;
chdir $basesrc;

my %f2dep;

sub gendep;
sub gendep {
	my $f = shift;
	my $basedir = $f =~ m#(.*)/# ? $1 : '.';
	return $f2dep{$f} if exists $f2dep{$f};
	$f2dep{$f} = '';
	my %dep;
	my $link = readlink $f;
	if (defined $link) {
		$link = "$basedir/$link" unless $link =~ m#^/#;
		$dep{$link}++;
	}
	open my $in, '<', $f or die "Could not read $f";
	while (<$in>) {
		if (/^\s*#\s*include\s*"([^"]+)"/) {
			my $inc = $1;
			next if $inc eq 'inspircd_version.h' && $f eq $baseinc.'/inspircd.h';
			my $found = 0;
			for my $loc ("$basedir/$inc", "$baseinc/$inc") {
				next unless -e $loc;
				$found++;
				$dep{$loc}++;
				$dep{$_}++ for split / /, gendep $loc;
			}
			if ($found == 0 && $inc ne 'inspircd_win32wrapper.h') {
				print STDERR "WARNING: could not find header $inc for $f\n";
			} elsif ($found > 1 && $basedir ne $baseinc) {
				print STDERR "WARNING: ambiguous include $inc in $f\n";
			}
		}
	}
	close $in;
	$f2dep{$f} = join ' ', sort keys %dep;
	$f2dep{$f};
}

sub dep_cpp {
	my($file, $dfile) = @_;
	gendep $file;
	my($path,$base) = $file =~ m#^((?:.*/)?)([^/]+)\.cpp# or die "Bad file $file";
	my $ext = $path eq 'modules/' ? '.so' : '.o';
	my $out = "$path$base$ext";
	$dfile = "$baseout/$path.$base.d" unless defined $dfile;

	open OUT, '>', "$dfile" or die "Could not write $dfile: $!";
	print OUT "$out: $file $f2dep{$file}\n";
	print OUT "\t@\$(SOURCEPATH)/make/unit-cc.pl \$(VERBOSE) \$< $out\n";
}

sub dep_dir {
	my($dir, $dfile) = @_;
	if ($dir =~ m#^(.*?)([^/]+)/?$#) {
		my($path,$base) = ($1,$2);
		my $out = "$path$base.so";
		$dfile = "$baseout/$path.$base.d" unless defined $dfile;
		opendir DIR, "$basesrc/$dir";
		my $ofiles = join ' ', grep s/(.*)\.cpp$/$path$base\/$1.o/, readdir DIR;
		closedir DIR;
		open OUT, '>', "$dfile" or die "Could not write $dfile: $!";
		print OUT "$out: $ofiles\n\t\$(RUNCC) \$(PICLDFLAGS) -o \$\@ \$^\n";
		close OUT;
	} else {
		print STDERR "Cannot generate depencency for $dir\n";
		exit 1;
	}
}

my($all,$quiet, $file);
GetOptions(
	'all' => \$all,
	'quiet' => \$quiet,
	'file=s' => \$file,
);

if (!$all && !defined $file) {
	print "Use: $0 {-all|-file src dest} [-quiet]\n";
	exit 1;
}

if (defined $file) {
	my $dfile = shift or die "Syntax: -file <in> <out>";
	$dfile = "$baseout/$dfile" unless $dfile =~ m#^/#;
	if (-f $file) {
		dep_cpp $file, $dfile;
	} elsif (-d $file) {
		dep_dir $file, $dfile;
	} else {
		print STDERR "Can't generate dependencies for $file\n";
		exit 1;
	}
} else {
	my @files = (<*.cpp>, <commands/*.cpp>, <modes/*.cpp>, <modules/*.cpp>, <modules/m_*/*.cpp>);
	push @files, "socketengines/$ENV{SOCKETENGINE}.cpp", "threadengines/threadengine_pthread.cpp";
	my @dirs = grep -d, <modules/m_*>;
	mkdir "$baseout/$_" for qw(commands modes modules socketengines threadengines), @dirs;

	for my $file (@files) {
		dep_cpp $file;
	}

	for my $dir (@dirs) {
		dep_dir $dir;
	}

	s#([^/]+)\.cpp#.$1.d# for @files;
	s#([^/]+)/?$#.$1.d# for @dirs;
	print join ' ', @files, @dirs;
}