3 # Copyright © 2012 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
11 # This script postprocesses Makefile.inc in the libltdl directory and attempts
12 # to fix these integration problems. The output can then included normally
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.
29 Getopt::Long::Configure("gnu_getopt", "no_auto_abbrev");
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);
50 $file =~ m|(?:.+/)?([^/]+)/?|;
58 return 1 if (!($distfile =~ /(.*)\.c/));
61 die if (!defined $am_dir_prefix);
63 ${am_dir_prefix}libobj_la_SOURCES += $distfile
64 $ltdl_dir/$base.lo: $ltdl_dir/${am_dir_prefix}libobj_la-$base.lo
65 \t\$(AM_V_at)(cd \$(\@D) && \$(LN_S) ${am_dir_prefix}libobj_la-\$(\@F) \$(\@F))
74 $raw =~ /([^=]+=)[[:space:]]*(.*)/s;
75 my ($left, @right) = ($1, split(/[[:space:]]+/, $2));
77 @right = grep(handle_libobj($_), @right);
78 return "" if (!@right);
79 return join(" ", ($left, @right)) . "\n";
85 # Combine line splices.
92 drop if (/DO NOT REMOVE THIS LINE/);
96 if (!$printed_header) {
97 print "# Postprocessed by ", basename($0), "\n";
99 # This trick should define ltdl_orderonly to | iff we're using GNU make.
100 ltdl_have_orderonly = $(findstring order-only,$(.FEATURES))
101 ltdl_orderonly = $(ltdl_have_orderonly:order-only=|)
103 ltdl_src_headers = $(ltdl_orderonly) $(ltdl_core_headers)
108 # Don't pollute the global AM_CPPFLAGS/AM_LDFLAGS environment with
109 # stuff totally specific to libltdl.
110 s/^AM_((?:CPPFLAGS|LDFLAGS)[[:space:]]*)\+=/LTDL_\1 =/;
112 # Augment references to AM_xFLAGS with the corresponding LTDL_xFLAGS.
113 s/\$\(AM_(CPPFLAGS|LDFLAGS)\)/$& \$(LTDL_\1)/;
115 # Since some of the targets rely on the automatic use of AM_xFLAGS,
116 # we need to track which libraries do not have explicit xFLAGS
117 # settings, and add references to LTDL_xFLAGS as appropriate.
118 if (/^((lib[[:alpha:]_]+_la_)[[:upper:]]+)/) {
119 $libtargetflags{$1} = 1;
123 # The use of LIBOBJ makes passing required CPPFLAGS tricky. We will
124 # handle this specially by defining a fake convenience library.
125 if (/^([[:alnum:]_]+)libltdl_la_SOURCES[[:space:]]*=/) {
127 m|\b([^[:space:]]*)/ltdl\.c\b|s;
130 EXTRA_LTLIBRARIES += $ltdl_dir/libobj.la
131 \$(${am_dir_prefix}libobj_la_OBJECTS): \$(ltdl_src_headers)
132 ${am_dir_prefix}libobj_la_SOURCES =
133 ${am_dir_prefix}libobj_la_CPPFLAGS = \$(AM_CPPFLAGS) \$(LTDL_CPPFLAGS)
134 ${am_dir_prefix}libobj_la_LINK = false
138 # Handle the relevant LIBOBJ source files, which appear in EXTRA_DIST.
139 if (/^EXTRA_DIST[[:space:]]*\+=/) {
140 $_ = fixup_libobjs($_);
143 # BUILT_SOURCES has similar problems to recursive make: inadequate
144 # dependencies lead to incorrect builds. Collect them into an
145 # ordinary variable so we can deal with them later.
146 s/BUILT_SOURCES/ltdl_core_headers/;
148 # Add explicit dependencies on generated header files to each library.
149 if (/^([[:alnum:]_]+)_SOURCES[[:space:]]*=/) {
150 print "\$($1_OBJECTS): \$(ltdl_src_headers)\n";
152 } continue { s/(\n.)/\\\1/g; print; };
154 print "\n# Automatic default flags for libraries\n";
155 foreach my $lib (keys %libtargets) {
156 foreach my $flag ("CPPFLAGS", "LDFLAGS") {
157 if (!$libtargetflags{"$lib$flag"}) {
158 print "$lib$flag = \$(AM_$flag) \$(LTDL_$flag)\n";