summaryrefslogtreecommitdiff
path: root/bin/module-deps.pl
blob: 7263862a200ab64c77dbf3e834534a5e2d265e60 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/perl -w

use strict;

my $gnumake;
my $makefile_build;

sub read_deps()
{
    my $p;
    my $invalid_tolerance = 100;
    my $line_count = 0;
    my %deps;
#    open ($p, "ENABLE_PRINT_DEPS=1 $gnumake -n -f $makefile_build all|") || die "can't launch make: $!";
    open ($p, "/tmp/deps") || die "can't read deps: $!";
    $|=1;
    print STDERR "reading deps ";
    while (<$p>) {
	my $line = $_;
	$line_count++;
	print STDERR '.' if ($line_count % 10 == 0);
#	print STDERR $line;
	chomp ($line);
        if ($line =~ m/^LibraryDep:\s+(\S+) links against (.*)$/) {
#        if ($line =~ m/^LibraryDep:\s+(\S+)\s+links against/) {
	    $deps{$1} = ' ' if (!defined $deps{$1});
	    $deps{$1} = $deps{$1} . ' ' . $2;
        } elsif ($line =~ m/^LibraryDep:\s+links against/) {
#           these need fixing, we call gb_LinkTarget__use_$...
#           and get less than normal data back to gb_LinkTarget_use_libraries
#	    print STDERR "ignoring unhelpful external dep\n";
	} elsif ($invalid_tolerance < 0) {
#	    print "read all dependencies to: '$line'\n";
	    last;
	} else {
#	    print "no match '$line'\n";
	    $invalid_tolerance--;
	}
    }
    close ($p);
    print STDERR " done\n";

    return \%deps;
}

# graphviz etc. don't like some names
sub clean_name($)
{
    my $name = shift;
    $name =~ s/[\-\/\.]/_/g;
    return $name;
}

# first create nodes for each entry
sub clean_tree($)
{
    my $deps = shift;
    my %tree;
    for my $name (sort keys %{$deps}) {
	my $need_str = $deps->{$name};
	$need_str =~ s/^\s+//g;
	$need_str =~ s/\s+$//g;
	my @needs = split /\s+/, $need_str;
	$name =~ m/^([^_]+)_(\S+)$/ || die "invalid target name: '$name'";
	my $type = $1;
	my $target = clean_name ($2);
	$type eq 'Executable' || $type eq 'Library' ||
	    $type eq 'CppunitTest' || die "Unknown type '$type'";

	my %result;
	$result{type} = $type;
	$result{target} = $target;
	$result{generation} = 0;
	my @clean_needs;
	for my $need (@needs) {
	    push @clean_needs, clean_name($need);
	}
	$result{deps} = \@clean_needs;
	if (defined $tree{$target}) {
	    print STDERR "warning -duplicate target: '$target'\n";
	}
	$tree{$target} = \%result;

#	print "$target ($type): " . join (',', @clean_needs) . "\n";
    }
    return \%tree;
}

sub is_existing_child($$$$);
sub is_existing_child($$$$)
{
    my ($tree,$search,$name,$generation) = @_;

    my $node = $tree->{$name};

#    print STDERR "\tis $search a child of $name ?\n";

    # cyclic check ...
    if ($node->{generation} == $generation) {
#	print STDERR "probable cyclic dependency graph hunting for $search in $name\n";
    }
    $node->{generation} = $generation;

    for my $child (@{$node->{deps}}) {
	if ($child eq $search) {
	    return 1;
	}
	return 1 if (is_existing_child($tree,$search,$child, $generation));
    }
    return 0;
}

sub dump_graphviz($)
{
    my $tree = shift;
    my $generation = 1;
    print "digraph LibreOffice {\n";
    for my $name (sort keys %{$tree}) {
	my $result = $tree->{$name};
	if ($result->{type} eq 'CppunitTest' ||
	    ($result->{type} eq 'Executable' && $result->{target} ne 'soffice_bin')) {
	    next; # de-bloat the tree
	}

#	print STDERR "minimising deps for $result->{target}\n";
	my @newdeps;
	for my $dep (@{$result->{deps}}) {
	    my $print = 1;
	    # is this implied by any other child ?
#	    print STDERR "checking if '$dep' is redundant\n";
	    for my $other_dep (@{$result->{deps}}) {
		$generation++;
		next if ($other_dep eq $dep);
		if (is_existing_child($tree,$dep,$other_dep,$generation)) {
		    $print = 0;
#		    print STDERR "$dep is implied by $other_dep - ignoring\n";
		}
	    }
	    print "$name -> $dep;\n" if ($print);
	    push @newdeps, $dep;
	}
	# re-write the shrunk set to accelerate things
	$result->{deps} = \@newdeps;
    }
    print "}\n";
}

my $graphviz = 1;

while (my $arg = shift @ARGV) {
    if ($arg eq '--graph' || $arg eq '-g') {
	$graphviz = 1;
    } elsif (!defined $gnumake) {
	$gnumake = $arg;
    } elsif (!defined $makefile_build) {
	$makefile_build = $arg;
    } else {
	die "un-needed argument '$arg'";
    }
}

$gnumake = 'make' if (!defined $gnumake);
$makefile_build = 'Makefile.gbuild' if (!defined $makefile_build);

my $deps = read_deps();
my $tree = clean_tree($deps);

dump_graphviz($tree);