xref: /freebsd/crypto/krb5/src/util/depfix.pl (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1#!env perl -w
2#
3# Copyright 1995,2001,2002,2003,2004,2005,2009 by the Massachusetts Institute of Technology.
4# All Rights Reserved.
5#
6# Export of this software from the United States of America may
7#   require a specific license from the United States Government.
8#   It is the responsibility of any person or organization contemplating
9#   export to obtain such a license before exporting.
10#
11# WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
12# distribute this software and its documentation for any purpose and
13# without fee is hereby granted, provided that the above copyright
14# notice appear in all copies and that both that copyright notice and
15# this permission notice appear in supporting documentation, and that
16# the name of M.I.T. not be used in advertising or publicity pertaining
17# to distribution of the software without specific, written prior
18# permission.  Furthermore if you modify this software you must label
19# your software as modified software and not distribute it in such a
20# fashion that it might be confused with the original M.I.T. software.
21# M.I.T. makes no representations about the suitability of
22# this software for any purpose.  It is provided "as is" without express
23# or implied warranty.
24#
25
26eval 'exec perl -S $0 ${1+"$@"}'
27  if 0;
28$0 =~ s/^.*?(\w+)[\.\w+]*$/$1/;
29
30# Input: srctop thisdir srcdir buildtop stlibobjs
31
32# Notes: myrelativedir is something like "lib/krb5/asn.1" or ".".
33# stlibobjs will usually be empty, or include spaces.
34
35# A typical set of inputs, produced with srcdir=.. at top level:
36#
37# top_srcdir = ../../../util/et/../..
38# thisdir = util/et
39# srcdir = ../../../util/et
40# BUILDTOP = ../..
41# STLIBOBJS = error_message.o et_name.o com_err.o
42
43my($top_srcdir,$thisdir,$srcdir,$BUILDTOP,$STLIBOBJS) = @ARGV;
44
45if (0) {
46    print STDERR "top_srcdir = $top_srcdir\n";
47    print STDERR "BUILDTOP = $BUILDTOP\n";
48    print STDERR "STLIBOBJS = $STLIBOBJS\n";
49}
50
51#$srcdirpat = quotemeta($srcdir);
52
53my($extrasuffixes) = ($STLIBOBJS ne "");
54
55sub my_qm {
56    my($x) = @_;
57    $x = quotemeta($x);
58    $x =~ s,\\/,/,g;
59    return $x;
60}
61
62sub strrep {
63    my($old,$new,$s) = @_;
64    my($l) = "strrep('$old','$new','$s')";
65    my($out) = "";
66    while ($s ne "") {
67	my($i) = index($s, $old);
68	if ($i == -1) {
69	    $out .= $s;
70	    $s = "";
71	} else {
72	    $out .= substr($s, 0, $i) . $new;
73	    if (length($s) > $i + length($old)) {
74		$s = substr($s, $i + length($old));
75	    } else {
76		$s = "";
77	    }
78	}
79    }
80#    print STDERR "$l = '$out'\n";
81    return $out;
82}
83
84sub do_subs {
85    local($_) = @_;
86    s,\\$, \\,g; s, + \\$, \\,g;
87    s,//+,/,g; s, \./, ,g;
88    if ($extrasuffixes) {
89	# Only care about the additional prefixes if we're building
90	# shared libraries.
91	s,^([a-zA-Z0-9_\-]*)\.o:,$1.so $1.po \$(OUTPRE)$1.\$(OBJEXT):,;
92    } else {
93	s,^([a-zA-Z0-9_\-]*)\.o:,\$(OUTPRE)$1.\$(OBJEXT):,;
94    }
95    # Recognize $(top_srcdir) and variants.
96    my($srct) = $top_srcdir . "/";
97    $_ = strrep(" $srct", " \$(top_srcdir)/", $_);
98#    s, $pat, \$(top_srcdir)/,go;
99    while ($srct =~ m,/[a-z][a-zA-Z0-9_.\-]*/\.\./,) {
100	$srct =~ s,/[a-z][a-zA-Z0-9_.\-]*/\.\./,/,;
101	$_ = strrep(" $srct", " \$(top_srcdir)/", $_);
102    }
103    # Now try to produce pathnames relative to $(srcdir).
104    if ($thisdir eq ".") {
105	# blah
106    } else {
107	my($pat) = " \$(top_srcdir)/$thisdir/";
108	my($out) = " \$(srcdir)/";
109	$_ = strrep($pat, $out, $_);
110	while ($pat =~ m,/[a-z][a-zA-Z0-9_.\-]*/$,) {
111	    $pat =~ s,/[a-z][a-zA-Z0-9_.\-]*/$,/,;
112	    $out .= "../";
113	    if ($pat ne " \$(top_srcdir)/") {
114		$_ = strrep($pat, $out, $_);
115	    }
116	}
117    }
118    # Now substitute for BUILDTOP:
119    $_ = strrep(" $BUILDTOP/", " \$(BUILDTOP)/", $_);
120    return $_;
121}
122
123sub do_subs_2 {
124    local($_) = @_;
125    # Add a trailing space.
126    s/$/ /;
127    # Remove excess spaces.
128    s/  */ /g;
129    # Delete headers external to the source and build tree.
130    s; /[^ ]*;;g;
131    # Remove foo/../ sequences.
132    while (m/\/[a-z][a-z0-9_.\-]*\/\.\.\//) {
133	s//\//g;
134    }
135    # Use VPATH.
136    s;\$\(srcdir\)/([^ /]* );$1;g;
137
138    $_ = &uniquify($_);
139
140    # Allow override of some util dependencies in case local tools are used.
141    s;\$\(BUILDTOP\)/include/com_err.h ;\$(COM_ERR_DEPS) ;g;
142    s;\$\(BUILDTOP\)/include/ss/ss.h \$\(BUILDTOP\)/include/ss/ss_err.h ;\$(SS_DEPS) ;g;
143    s;\$\(BUILDTOP\)/include/db-config.h \$\(BUILDTOP\)/include/db.h ;\$(DB_DEPS) ;g;
144    s;\$\(BUILDTOP\)/include/verto.h ;\$(VERTO_DEPS) ;g;
145    if ($thisdir eq "util/gss-kernel-lib") {
146	# Here com_err.h is used from the current directory.
147	s;com_err.h ;\$(COM_ERR_DEPS) ;g;
148    }
149    if ($thisdir eq "lib/krb5/ccache") {
150	# These files are only used (and kcmrpc.h only generated) on macOS.
151	# There are conditional dependencies in Makefile.in.
152	s;kcmrpc.h ;;g;
153	s;kcmrpc_types.h ;;g;
154    }
155
156    $_ = &uniquify($_);
157
158    # Delete trailing whitespace.
159    s; *$;;g;
160
161    return $_;
162}
163
164sub uniquify {
165    # Apparently some versions of gcc -- like
166    # "gcc version 3.4.4 20050721 (Red Hat 3.4.4-2)"
167    # -- will sometimes emit duplicate header file names.
168    local($_) = @_;
169    my(@sides) = split ": ", $_;
170    my($lhs) = "";
171    if ($#sides == 1) {
172	$lhs = $sides[0] . ": ";
173	$_ = $sides[1];
174    }
175    my(@words) = split " ", $_;
176    my($w);
177    my($result) = "";
178    my(%seen);
179    undef %seen;
180    foreach $w (sort { $a cmp $b; } @words) {
181	next if defined($seen{$w});
182	$seen{$w} = 1;
183	if ($result ne "") { $result .= " "; }
184	$result .= $w;
185    }
186    return $lhs . $result . " ";
187}
188
189sub split_lines {
190    local($_) = @_;
191    s/(.{50}[^ ]*) /$1 \\\n  /g;
192    return $_ . "\n";
193}
194
195print <<EOH ;
196#
197# Generated makefile dependencies follow.
198#
199EOH
200my $buf = '';
201while (<STDIN>) {
202    # Strip newline.
203    chop;
204    next if /^\s*#/;
205    # Do directory-specific path substitutions on each filename read.
206    $_ = &do_subs($_);
207    if (m/\\$/) {
208	chop;
209	$buf .= $_;
210    } else {
211	$buf = &do_subs_2($buf . $_);
212	print &split_lines($buf);
213	$buf = '';
214    }
215}
216exit 0;
217