]> git.draconx.ca Git - dxcommon.git/blob - scripts/fix-ltdl.pl
fix-ltdl: New script to fix up nonrecursive libltdl.
[dxcommon.git] / scripts / fix-ltdl.pl
1 #!/usr/bin/env perl
2 #
3 # Copyright © 2012 Nick Bowler
4 #
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.
10 #
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.
14 #
15 # Most of the specific transformations are documented below.
16 #
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.
20
21 use strict;
22 use Getopt::Long;
23
24 my $output   = undef;
25 my $input    = undef;
26
27 my $line     = 0;
28
29 Getopt::Long::Configure("gnu_getopt", "no_auto_abbrev");
30 GetOptions(
31         "o|output=s"   => \$output,
32         "i|input=s"    => \$input,
33 );
34
35 open STDOUT, ">", $output or die "$output: $!\n" if (defined $output);
36 open STDIN,  "<", $input  or die "$input: $!\n"  if (defined $input);
37
38 my $printed_header = 0;
39 my ($ltdl_dir, $am_dir_prefix);
40 my (%libtargets, %libtargetflags);
41 my @libobjfiles;
42
43 sub drop {
44         undef $_;
45         next;
46 }
47
48 sub basename {
49         my $file = shift;
50         $file =~ m|(?:.+/)?([^/]+)/?|;
51         return $1;
52 }
53
54 sub handle_libobj {
55         my $distfile = shift;
56         my $base;
57
58         return 1 if (!($distfile =~ /(.*)\.c/));
59         $base = basename($1);
60
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/${am_dir_prefix}libobj_la-$base.lo
65 \t\$(AM_V_at)(cd \$(\@D) && \$(LN_S) ${am_dir_prefix}libobj_la-\$(\@F) \$(\@F))
66 EOF
67
68         return 0;
69 }
70
71 sub fixup_libobjs {
72         my $raw = shift;
73
74         $raw =~ /([^=]+=)[[:space:]]*(.*)/s;
75         my ($left, @right) = ($1, split(/[[:space:]]+/, $2));
76
77         @right = grep(handle_libobj($_), @right);
78         return "" if (!@right);
79         return join(" ", ($left, @right)) . "\n";
80 }
81
82 while (<STDIN>) {
83         $line++;
84
85         # Combine line splices.
86         while (s/\\$//) {
87                 $line++;
88                 $_ = $_ . <STDIN>
89         }
90
91         # I cannot resist...
92         drop if (/DO NOT REMOVE THIS LINE/);
93
94         next if (/^#/);
95
96         if (!$printed_header) {
97                 print "# Postprocessed by ", basename($0), "\n";
98                 print <<'EOF';
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=|)
102 ltdl_core_headers =
103 ltdl_src_headers = $(ltdl_orderonly) $(ltdl_core_headers)
104 EOF
105                 $printed_header = 1;
106         }
107
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 =/;
111
112         # Augment references to AM_xFLAGS with the corresponding LTDL_xFLAGS.
113         s/\$\(AM_(CPPFLAGS|LDFLAGS)\)/$& \$(LTDL_\1)/;
114
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;
120                 $libtargets{$2} = 1;
121         }
122
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:]]*=/) {
126                 $am_dir_prefix=$1;
127                 m|\b([^[:space:]]*)/ltdl\.c\b|s;
128                 $ltdl_dir = $1;
129                 print <<EOF;
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
135 EOF
136         }
137
138         # Handle the relevant LIBOBJ source files, which appear in EXTRA_DIST.
139         if (/^EXTRA_DIST[[:space:]]*\+=/) {
140                 $_ = fixup_libobjs($_);
141         }
142
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/;
147
148         # Add explicit dependencies on generated header files to each library.
149         if (/^([[:alnum:]_]+)_SOURCES[[:space:]]*=/) {
150                 print "\$($1_OBJECTS): \$(ltdl_src_headers)\n";
151         }
152 } continue { s/(\n.)/\\\1/g; print; };
153
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";
159                 }
160         }
161 }