gen-strtab.awk: Work around IRIX 6.2 nawk bug.
[dxcommon.git] / scripts / fix-ltdl.pl
blobbd026c87f7416acb27668dea91157af36929fd70
1 #!/usr/bin/env perl
3 # Copyright © 2012, 2014, 2022 Nick Bowler
5 # Fixup a non-recursive libltdl makefile fragment. While libltdl purports to
6 # support inclusion into non-recursive automake builds, it does a few things
7 # that are less than ideal. The most dire problem is that it modifies
8 # AM_CPPFLAGS and AM_LDFLAGS in a manner that can negatively affect the
9 # rest of the project.
11 # This script postprocesses Makefile.inc in the libltdl directory and attempts
12 # to fix these integration problems. The output can then included normally
13 # into Makefile.am.
15 # Most of the specific transformations are documented below.
17 # License WTFPL2: Do What The Fuck You Want To Public License, version 2.
18 # This is free software: you are free to do what the fuck you want to.
19 # There is NO WARRANTY, to the extent permitted by law.
21 use strict;
22 use Getopt::Long;
24 my $output = undef;
25 my $input = undef;
27 my $line = 0;
29 Getopt::Long::Configure("gnu_getopt", "no_auto_abbrev");
30 GetOptions(
31 "o|output=s" => \$output,
32 "i|input=s" => \$input,
35 open STDOUT, ">", $output or die "$output: $!\n" if (defined $output);
36 open STDIN, "<", $input or die "$input: $!\n" if (defined $input);
38 my $printed_header = 0;
39 my ($ltdl_dir, $am_dir_prefix);
40 my (%libtargets, %libtargetflags);
41 my @libobjfiles;
43 sub drop {
44 undef $_;
45 next;
48 sub basename {
49 my $file = shift;
50 $file =~ m|(?:.+/)?([^/]+)/?|;
51 return $1;
54 sub handle_libobj {
55 my $distfile = shift;
56 my $base;
58 return 1 if (!($distfile =~ /(.*)\.c/));
59 $base = basename($1);
61 die if (!defined $am_dir_prefix);
62 print <<EOF;
63 ${am_dir_prefix}libobj_la_SOURCES += $distfile
64 $ltdl_dir/$base.lo: $ltdl_dir/lo-$base.lo
65 \t\$(AM_V_at)-rm -f \$@
66 \t\$(AM_V_at)cd \$(\@D) && \$(LN_S) lo-\$(\@F) \$(\@F)
67 EOF
69 return 0;
72 sub fixup_libobjs {
73 my $raw = shift;
75 $raw =~ /([^=]+=)[[:space:]]*(.*)/s;
76 my ($left, @right) = ($1, split(/[[:space:]]+/, $2));
78 @right = grep(handle_libobj($_), @right);
79 return "" if (!@right);
80 return join(" ", ($left, @right)) . "\n";
83 while (<STDIN>) {
84 $line++;
86 # Combine line splices.
87 while (s/\\$//) {
88 $line++;
89 $_ = $_ . <STDIN>
92 # I cannot resist...
93 drop if (/DO NOT REMOVE THIS LINE/);
95 next if (/^#/);
97 if (!$printed_header) {
98 print "# Postprocessed by ", basename($0), "\n";
99 print <<'EOF';
100 # This trick should define ltdl_orderonly to | iff we're using GNU make.
101 ltdl_make_features = $(.FEATURES)
102 ltdl_have_orderonly = $(findstring order-only,${ltdl_make_features})
103 ltdl_orderonly = $(ltdl_have_orderonly:order-only=|)
104 ltdl_core_headers =
105 ltdl_src_headers = $(ltdl_orderonly) $(ltdl_core_headers)
107 $printed_header = 1;
110 # Don't pollute the global AM_CPPFLAGS/AM_LDFLAGS environment with
111 # stuff totally specific to libltdl.
112 s/^AM_((?:CPPFLAGS|LDFLAGS)[[:space:]]*)\+=/LTDL_\1 =/;
114 # Augment references to AM_xFLAGS with the corresponding LTDL_xFLAGS.
115 s/\$\(AM_(CPPFLAGS|LDFLAGS)\)/$& \$(LTDL_\1)/;
117 # Since some of the targets rely on the automatic use of AM_xFLAGS,
118 # we need to track which libraries do not have explicit xFLAGS
119 # settings, and add references to LTDL_xFLAGS as appropriate.
120 if (/^((lib[[:alpha:]_]+_la_)[[:upper:]]+)/) {
121 $libtargetflags{$1} = 1;
122 $libtargets{$2} = 1;
125 # The use of LIBOBJ makes passing required CPPFLAGS tricky. We will
126 # handle this specially by defining a fake convenience library.
127 if (/^([[:alnum:]_]+)libltdl_la_SOURCES[[:space:]]*=/) {
128 $am_dir_prefix=$1;
129 m|\b([^[:space:]]*)/ltdl\.c\b|s;
130 $ltdl_dir = $1;
131 print <<EOF;
132 EXTRA_LTLIBRARIES += $ltdl_dir/libobj.la
133 \$(${am_dir_prefix}libobj_la_OBJECTS): \$(ltdl_src_headers)
134 ${am_dir_prefix}libobj_la_SOURCES =
135 ${am_dir_prefix}libobj_la_CPPFLAGS = \$(AM_CPPFLAGS) \$(LTDL_CPPFLAGS)
136 ${am_dir_prefix}libobj_la_LINK = false
137 ${am_dir_prefix}libobj_la_SHORTNAME = lo
141 # Handle the relevant LIBOBJ source files, which appear in EXTRA_DIST.
142 if (/^EXTRA_DIST[[:space:]]*\+=/) {
143 $_ = fixup_libobjs($_);
146 # There is no reason to list $(LT_DLLOADERS) in noinst_LTLIBRARIES;
147 # these will be picked up as ordinary prerequisites. They're already
148 # all in EXTRA_LTLIBRARIES.
149 drop if (/^noinst_LTLIBRARIES.*\$\(LT_DLLOADERS\)$/);
151 # Don't list other libs in noinst_LTLIBRARIES so that they don't get
152 # built unless explicitly required by prerequisites.
153 s/noinst_LTLIBRARIES/EXTRA_LTLIBRARIES/;
155 # BUILT_SOURCES has similar problems to recursive make: inadequate
156 # dependencies lead to incorrect builds. Collect them into an
157 # ordinary variable so we can deal with them later.
158 s/BUILT_SOURCES/ltdl_core_headers/;
160 # Add explicit dependencies on generated header files to each library.
161 if (/^([[:alnum:]_]+)_SOURCES[[:space:]]*=/) {
162 print "\$($1_OBJECTS): \$(ltdl_src_headers)\n";
164 } continue { s/(\n.)/\\\1/g; print; };
166 print "\n# Automatic default flags for libraries\n";
167 foreach my $lib (keys %libtargets) {
168 foreach my $flag ("CPPFLAGS", "LDFLAGS") {
169 if (!$libtargetflags{"$lib$flag"}) {
170 print "$lib$flag = \$(AM_$flag) \$(LTDL_$flag)\n";