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