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