Update x11-drivers/xf86-input-wacom to version 0.34.0_1
[dports.git] / Tools / make_index
1 #!/usr/local/bin/perl
2 #
3 # $FreeBSD: head/Tools/make_index 340851 2014-01-23 19:55:14Z mat $
4 #
5 # INDEX builds visit each port once and write out each port's
6 # *-depends as a list of directories, using 'make describe'.  This
7 # script goes back in and maps the directories back to pkgnames,
8 # fixes up the *-depends list, and writes out the new INDEX file.
9
10 require 5.002;
11
12 # Helper function to map a directory to a pkgname.
13 sub by_path {
14     my ($name, $port) = @_;
15
16   # If a direct mapping exists, then use it.
17     return $by_path{$name} if (defined $by_path{$name});
18
19   # Make sure we have /usr/ports at the beginning.
20     $name =~ s!^$pwd!/usr/ports!o;
21     return $by_path{$name} if (defined $by_path{$name});
22
23   # Collapse all the '..' sequences.
24     my @f = split('/', $name), @p = ();
25     foreach (@f) { (/\.\./) ? pop(@p) : push(@p, $_); }
26     $name = join('/', @p);
27     return $by_path{$name} if (defined $by_path{$name});
28
29     print STDERR "make_index: $port: no entry for $name\n";
30     return undef;
31 }
32
33 # This routine replaces what used to be the time-consuming
34 # recursive 'depends-list' and 'package-depends' targets.
35 sub recurse {
36     my $pkg = shift(@_);
37     return if $pkg->{checked};
38
39   # extract-depends = extract-depends + recursive list of run-depends
40   #     for each extract-depends
41     my @deps = ();
42     foreach $name (@{$pkg->{edep}}) {
43         recurse($index{$name});
44         push(@deps, @{$index{$name}->{rdep}});
45     }
46     $pkg->{edep} = uniqify(@{$pkg->{edep}}, @deps);
47
48   # same as above except for patch-depends this time
49     @deps = ();
50     foreach $name (@{$pkg->{pdep}}) {
51         recurse($index{$name});
52         push(@deps, @{$index{$name}->{rdep}});
53     }
54     $pkg->{pdep} = uniqify(@{$pkg->{pdep}}, @deps);
55
56   # same as above except for fetch-depends this time
57     @deps = ();
58     foreach $name (@{$pkg->{fdep}}) {
59         recurse($index{$name});
60         push(@deps, @{$index{$name}->{rdep}});
61     }
62     $pkg->{fdep} = uniqify(@{$pkg->{fdep}}, @deps);
63     $pkg->{checked} = 1;
64
65   # same as above except for build-depends this time
66     @deps = ();
67     foreach $name (@{$pkg->{bdep}}) {
68         recurse($index{$name});
69         push(@deps, @{$index{$name}->{rdep}});
70     }
71     $pkg->{bdep} = uniqify(@{$pkg->{bdep}}, @deps);
72     $pkg->{checked} = 1;
73
74   # same as above except for run-depends this time
75     @deps = ();
76     foreach $name (@{$pkg->{rdep}}) {
77         recurse($index{$name});
78         push(@deps, @{$index{$name}->{rdep}});
79     }
80     $pkg->{rdep} = uniqify(@{$pkg->{rdep}}, @deps);
81     $pkg->{checked} = 1;
82
83 }
84
85 # Given one or more lists as arguments return the set
86 # of unique elements among them.
87 sub uniqify {
88     my %seen = ();
89     my @unique = grep {! $seen{$_}++} (@_);
90     return \@unique;
91 }
92
93 # Save where we are so that we can map all directories formed
94 # from ${PORTSDIR} to their canonical location '/usr/ports/...'.
95 chomp($pwd = `pwd`);
96
97 # Read each line of output generated by the 'index' target.
98 while (<>) {
99     chomp;
100     s/\015$//;
101
102     my @f = split(/\|/);
103
104   # Force to canonical form.
105     $f[1] =~ s!^$pwd!/usr/ports!o;
106     $f[4] =~ s!^$pwd!/usr/ports!o;
107
108   # Save directory -> pkgname relationship.
109   # Note: $f[0] gets clobbered by the splice below so we'll save
110   # it to a new $name first.
111     $by_path{$f[1]} = $name = $f[0];
112
113   # Create a hash table of the infomation we need about this port.
114     my $pkg = {
115         'edep'          => [split(/ /, $f[7])],
116         'pdep'          => [split(/ /, $f[8])],
117         'fdep'          => [split(/ /, $f[9])],
118         'bdep'          => [split(/ /, $f[10])],
119         'rdep'          => [split(/ /, $f[11])],
120         'rest'          => join('|', splice(@f, 12)),
121         'text'          => join('|', splice(@f, 0, 7))
122     };
123     $index{$name} = $pkg;
124
125   # This is a cheap way of preserving the order of the entries.
126     push(@names, $name);
127 }
128
129 # For each port perform the mapping between directory and pkgnames.
130 foreach $name (keys %index) {
131     my $pkg = $index{$name};
132   # first the extract dependencies
133     if (@{$pkg->{edep}}) {
134         my @edep = map { by_path($_, $name) } @{$pkg->{edep}};
135         $pkg->{edep} = \@edep;
136     }
137   # then the patch dependencies
138     if (@{$pkg->{pdep}}) {
139         my @pdep = map { by_path($_, $name) } @{$pkg->{pdep}};
140         $pkg->{pdep} = \@pdep;
141     }
142   # then the fetch dependencies
143     if (@{$pkg->{fdep}}) {
144         my @fdep = map { by_path($_, $name) } @{$pkg->{fdep}};
145         $pkg->{fdep} = \@fdep;
146     }
147   # then the build dependencies
148     if (@{$pkg->{bdep}}) {
149         my @bdep = map { by_path($_, $name) } @{$pkg->{bdep}};
150         $pkg->{bdep} = \@bdep;
151     }
152   # then the run dependencies
153     if (@{$pkg->{rdep}}) {
154         my @rdep = map { by_path($_, $name) } @{$pkg->{rdep}};
155         $pkg->{rdep} = \@rdep;
156     }
157 }
158
159 # With all that done we're finally ready to write out the new
160 # INDEX file one port at a time.
161 foreach $name (@names) {
162     my $pkg = $index{$name};
163     if (exists $pkg->{'PRINTED'}) {
164         print STDERR "Warning: Duplicate INDEX entry: $name\n";
165     } else {
166         recurse($pkg);
167         print "$pkg->{text}|";
168         print join(' ', sort(@{$pkg->{bdep}})) if @{$pkg->{bdep}};
169         print "|";
170         print join(' ', sort(@{$pkg->{rdep}})) if @{$pkg->{rdep}};
171         print "|$pkg->{rest}|";
172         print join(' ', sort(@{$pkg->{edep}})) if @{$pkg->{edep}};
173         print "|";
174         print join(' ', sort(@{$pkg->{pdep}})) if @{$pkg->{pdep}};
175         print "|";
176         print join(' ', sort(@{$pkg->{fdep}})) if @{$pkg->{fdep}};
177         print "\n";
178         ++$pkg->{'PRINTED'};
179     }
180 }