summaryrefslogtreecommitdiff
path: root/etc/prefix-gnulib-mk
blob: 08d22f288d9433a236fff9bf3f19dc553198ccd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#! /usr/bin/perl -w

use strict;
use IO::File;

my $prefix = "lib/";

# contents ($FILE_NAME)
# ---------------------
sub contents ($)
{
  my ($file) = @_;
  local $/;			# Turn on slurp-mode.
  my $f = new IO::File "< $file" or die "$file";
  my $contents = $f->getline or die "$file";
  $f->close;
  return $contents;
}

# prefix_word ($WORD)
# -------------------
# Do not prefix special words such as variable dereferences.  Also,
# "Makefile" is really "Makefile", since precisely there is no
# lib/Makefile.
sub prefix_word ($)
{
  local ($_) = @_;
  $_ = $prefix . $_
    unless m{^\$\(\w+\)} || $_ eq "Makefile";
  return $_;
}


# prefix_words ($TEXT)
# --------------------
sub prefix_words ($)
{
  local ($_) = @_;
  s{(\S+)}{prefix_word($1)}gem;
  return $_;
}


# prefix_assignment ($LHS-AND-ASSIGN-OP, $RHS)
# --------------------------------------------
sub prefix_assignment ($$)
{
  my ($lhs_and_assign_op, $rhs) = @_;
  my $res;

  # Some variables are initialized by gnulib.mk, and we don't want
  # that.  Change '=' to '+='.
  if ($lhs_and_assign_op =~ /^(SUBDIRS|EXTRA_DIST|BUILT_SOURCES|SUFFIXES|MOSTLYCLEANFILES|CLEANFILES|DISTCLEANFILES|MAINTAINERCLEANFILES|AM_CFLAGS|AM_CPPFLAGS|AM_GNU_GETTEXT) =/)
    {
      $lhs_and_assign_op =~ s/=/+=/;
    }
  # We don't want to inherit gnulib's AUTOMAKE_OPTIONS, comment them.
  elsif ($lhs_and_assign_op =~ /^AUTOMAKE_OPTIONS =/)
    {
      $lhs_and_assign_op =~ s/^/# /;
    }
  # Don't touch suffixes.
  elsif ($lhs_and_assign_op =~ /^SUFFIXES /)
    {
    }
  # The words are (probably) paths to files in lib/: prefix them.
  else
    {
      $rhs = prefix_words($rhs)
    }

  # Variables which name depend on the location: libbison_a_SOURCES =>
  # lib_libbison_a_SOURCES.
  $lhs_and_assign_op =~ s/(libbison)/lib_$1/g;

  # Do not use gl_LIBOBJS, but its prefixed version.
  $rhs =~ s/gl_LIBOBJS/gl_PREFIXED_LIBOBJS/g;

  return $lhs_and_assign_op . $rhs;
}

# prefix $CONTENTS
# ----------------
# $CONTENTS is a Makefile content.  Post-process it so that each file-name
# is prefixed with $prefix (e.g., "lib/").
#
# Relies heavily on the regularity of the file generated by gnulib-tool.
sub prefix ($)
{
  # Work on $_.
  local ($_) = @_;

  # Prefix all the occurrence of files in rules.  If there is nothing
  # after in the :, it's probably a phony target, or a suffix rule.
  # Don't touch it.
  s{^([\w.]+ *: *\w.*)$}
   {prefix_words($1)}gem;

  # Prefix files in variables.
  s{^([\w.]+\s*\+?=)(.*)$}
   {prefix_assignment($1, $2)}gem;

  # These three guys escape all the other regular rules.
  s{(charset\.alias|ref-add\.sed|ref-del\.sed)}{$prefix$1}g;
  # Unfortunately, as a result we sometimes have lib/lib.
  s{lib/lib/}{lib/}g;

  # $(srcdir) is actually $(top_srcdir)/lib.
  s{\$\(srcdir\)}{\$(top_srcdir)/lib}g;

  # Sometimes, t-$@ is used instead of $@-t, which, of course, does
  # not work when we have a $@ with a directory in it.
  s{t-\$\@}{\$\@-t}g;

  return $_;
}

# process ($IN)
# -------------
sub process ($)
{
  my ($file) = @_;
  my ($bak) = "$file.bak";
  rename ($file, $bak) or die;
  my $contents = contents ($bak);
  $contents = prefix ($contents);
  my $out = new IO::File(">$file") or die;
  print $out $contents;
}

process ("lib/gnulib.mk")


### Setup "GNU" style for perl-mode and cperl-mode.
## Local Variables:
## perl-indent-level: 2
## perl-continued-statement-offset: 2
## perl-continued-brace-offset: 0
## perl-brace-offset: 0
## perl-brace-imaginary-offset: 0
## perl-label-offset: -2
## cperl-indent-level: 2
## cperl-brace-offset: 0
## cperl-continued-brace-offset: 0
## cperl-label-offset: -2
## cperl-extra-newline-before-brace: t
## cperl-merge-trailing-else: nil
## cperl-continued-statement-offset: 2
## End: