summaryrefslogtreecommitdiff
path: root/lib/Automake/Requires.pm
blob: 587a194b014a1f427e73261335203897cf5829c5 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package Automake::Requires;

use Automake::ChannelDefs;
use Automake::Channels;
use Automake::Condition qw (TRUE FALSE);
use Automake::Config;
use Automake::FileUtils;
use Automake::Global;
use Automake::Location;
use Automake::Options;
use Automake::Rule;
use Automake::Utils;
use Automake::Variable;
use File::Basename;

use Exporter;

use vars '@ISA', '@EXPORT';

@ISA = qw (Exporter);

@EXPORT = qw (require_file require_file_with_macro
    require_libsource_with_macro require_queued_file_check_or_copy
    require_conf_file require_conf_file_with_macro require_build_directory
    require_build_directory_maybe);

# push_required_file ($DIR, $FILE, $FULLFILE)
# -------------------------------------------
# Push the given file onto DIST_COMMON.
sub push_required_file
{
  my ($dir, $file, $fullfile) = @_;

  # If the file to be distributed is in the same directory of the
  # currently processed Makefile.am, then we want to distribute it
  # from this same Makefile.am.
  if ($dir eq $relative_dir)
    {
      push_dist_common ($file);
    }
  # This is needed to allow a construct in a non-top-level Makefile.am
  # to require a file in the build-aux directory (see at least the test
  # script 'test-driver-is-distributed.sh').  This is related to the
  # automake bug#9546.  Note that the use of $config_aux_dir instead
  # of $am_config_aux_dir here is deliberate and necessary.
  elsif ($dir eq $config_aux_dir)
    {
      push_dist_common ("$am_config_aux_dir/$file");
    }
  # FIXME: another spacial case, for AC_LIBOBJ/AC_LIBSOURCE support.
  # We probably need some refactoring of this function and its callers,
  # to have a more explicit and systematic handling of all the special
  # cases; but, since there are only two of them, this is low-priority
  # ATM.
  elsif ($config_libobj_dir && $dir eq $config_libobj_dir)
    {
      # Avoid unsightly '/.'s.
      my $am_config_libobj_dir =
        '$(top_srcdir)' .
        ($config_libobj_dir eq '.' ? "" : "/$config_libobj_dir");
      $am_config_libobj_dir =~ s|/*$||;
      push_dist_common ("$am_config_libobj_dir/$file");
    }
  elsif ($relative_dir eq '.' && ! is_make_dir ($dir))
    {
      # If we are doing the topmost directory, and the file is in a
      # subdir which does not have a Makefile, then we distribute it
      # here.

      # If a required file is above the source tree, it is important
      # to prefix it with '$(srcdir)' so that no VPATH search is
      # performed.  Otherwise problems occur with Make implementations
      # that rewrite and simplify rules whose dependencies are found in a
      # VPATH location.  Here is an example with OSF1/Tru64 Make.
      #
      #   % cat Makefile
      #   VPATH = sub
      #   distdir: ../a
      #	          echo ../a
      #   % ls
      #   Makefile a
      #   % make
      #   echo a
      #   a
      #
      # Dependency '../a' was found in 'sub/../a', but this make
      # implementation simplified it as 'a'.  (Note that the sub/
      # directory does not even exist.)
      #
      # This kind of VPATH rewriting seems hard to cancel.  The
      # distdir.am hack against VPATH rewriting works only when no
      # simplification is done, i.e., for dependencies which are in
      # subdirectories, not in enclosing directories.  Hence, in
      # the latter case we use a full path to make sure no VPATH
      # search occurs.
      $fullfile = '$(srcdir)/' . $fullfile
	if $dir =~ m,^\.\.(?:$|/),;

      push_dist_common ($fullfile);
    }
  else
    {
      prog_error "a Makefile in relative directory $relative_dir " .
                 "can't add files in directory $dir to DIST_COMMON";
    }
}


# If a file name appears as a key in this hash, then it has already
# been checked for.  This allows us not to report the same error more
# than once.
my %required_file_not_found = ();

# required_file_check_or_copy ($WHERE, $DIRECTORY, $FILE)
# -------------------------------------------------------
# Verify that the file must exist in $DIRECTORY, or install it.
sub required_file_check_or_copy
{
  my ($where, $dir, $file) = @_;

  my $fullfile = "$dir/$file";
  my $found_it = 0;
  my $dangling_sym = 0;

  if (-l $fullfile && ! -f $fullfile)
    {
      $dangling_sym = 1;
    }
  elsif (dir_has_case_matching_file ($dir, $file))
    {
      $found_it = 1;
    }

  # '--force-missing' only has an effect if '--add-missing' is
  # specified.
  return
    if $found_it && (! $add_missing || ! $force_missing);

  # If we've already looked for it, we're done.  You might wonder why we
  # don't do this before searching for the file.  If we do that, then
  # something like AC_OUTPUT([subdir/foo foo]) will fail to put 'foo.in'
  # into $(DIST_COMMON).
  if (! $found_it)
    {
      return if defined $required_file_not_found{$fullfile};
      $required_file_not_found{$fullfile} = 1;
    }
  if ($dangling_sym && $add_missing)
    {
      unlink ($fullfile);
    }

  my $trailer = '';
  my $trailer2 = '';
  my $suppress = 0;

  # Only install missing files according to our desired
  # strictness level.
  my $message = "required file '$fullfile' not found";
  if ($add_missing)
    {
      if (-f "$libdir/$file")
        {
          $suppress = 1;

          # Install the missing file.  Symlink if we
          # can, copy if we must.  Note: delete the file
          # first, in case it is a dangling symlink.
          $message = "installing '$fullfile'";

          # The license file should not be volatile.
          if ($file eq "COPYING")
            {
              $message .= " using GNU General Public License v3 file";
              $trailer2 = "\n    Consider adding the COPYING file"
                        . " to the version control system"
                        . "\n    for your code, to avoid questions"
                        . " about which license your project uses";
            }

          # Windows Perl will hang if we try to delete a
          # file that doesn't exist.
          unlink ($fullfile) if -f $fullfile;
          if ($symlink_exists && ! $copy_missing)
            {
              if (! symlink ("$libdir/$file", $fullfile)
                  || ! -e $fullfile)
                {
                  $suppress = 0;
                  $trailer = "; error while making link: $!";
                }
            }
          elsif (system ('cp', "$libdir/$file", $fullfile))
            {
              $suppress = 0;
              $trailer = "\n    error while copying";
            }
          set_dir_cache_file ($dir, $file);
        }
    }
  else
    {
      $trailer = "\n  'automake --add-missing' can install '$file'"
        if -f "$libdir/$file";
    }

  # If --force-missing was specified, and we have
  # actually found the file, then do nothing.
  return
    if $found_it && $force_missing;

  # If we couldn't install the file, but it is a target in
  # the Makefile, don't print anything.  This allows files
  # like README, AUTHORS, or THANKS to be generated.
  return
    if !$suppress && rule $file;

  msg ($suppress ? 'note' : 'error', $where, "$message$trailer$trailer2");
}


# require_file_internal ($WHERE, $MYSTRICT, $DIRECTORY, $QUEUE, @FILES)
# ---------------------------------------------------------------------
# Verify that the file must exist in $DIRECTORY, or install it.
# $MYSTRICT is the strictness level at which this file becomes required.
# Worker threads may queue up the action to be serialized by the master,
# if $QUEUE is true
sub require_file_internal
{
  my ($where, $mystrict, $dir, $queue, @files) = @_;

  return
    unless $strictness >= $mystrict;

  foreach my $file (@files)
    {
      push_required_file ($dir, $file, "$dir/$file");
      if ($queue)
        {
          queue_required_file_check_or_copy ($required_conf_file_queue,
                                             QUEUE_CONF_FILE, $relative_dir,
                                             $where, $mystrict, @files);
        }
      else
        {
          required_file_check_or_copy ($where, $dir, $file);
        }
    }
}

# require_file ($WHERE, $MYSTRICT, @FILES)
# ----------------------------------------
sub require_file
{
    my ($where, $mystrict, @files) = @_;
    require_file_internal ($where, $mystrict, $relative_dir, 0, @files);
}

# require_file_with_macro ($COND, $MACRO, $MYSTRICT, @FILES)
# ----------------------------------------------------------
sub require_file_with_macro
{
    my ($cond, $macro, $mystrict, @files) = @_;
    $macro = rvar ($macro) unless ref $macro;
    require_file ($macro->rdef ($cond)->location, $mystrict, @files);
}

# require_libsource_with_macro ($COND, $MACRO, $MYSTRICT, @FILES)
# ---------------------------------------------------------------
# Require an AC_LIBSOURCEd file.  If AC_CONFIG_LIBOBJ_DIR was called, it
# must be in that directory.  Otherwise expect it in the current directory.
sub require_libsource_with_macro
{
    my ($cond, $macro, $mystrict, @files) = @_;
    $macro = rvar ($macro) unless ref $macro;
    if ($config_libobj_dir)
      {
	require_file_internal ($macro->rdef ($cond)->location, $mystrict,
			       $config_libobj_dir, 0, @files);
      }
    else
      {
	require_file ($macro->rdef ($cond)->location, $mystrict, @files);
      }
}

# queue_required_file_check_or_copy ($QUEUE, $KEY, $DIR, $WHERE,
#                                    $MYSTRICT, @FILES)
# --------------------------------------------------------------
sub queue_required_file_check_or_copy
{
    my ($queue, $key, $dir, $where, $mystrict, @files) = @_;
    my @serial_loc;
    if (ref $where)
      {
        @serial_loc = (QUEUE_LOCATION, $where->serialize ());
      }
    else
      {
        @serial_loc = (QUEUE_STRING, $where);
      }
    $queue->enqueue ($key, $dir, @serial_loc, $mystrict, 0 + @files, @files);
}

# require_queued_file_check_or_copy ($QUEUE)
# ------------------------------------------
sub require_queued_file_check_or_copy
{
    my ($queue) = @_;
    my $where;
    my $dir = $queue->dequeue ();
    my $loc_key = $queue->dequeue ();
    if ($loc_key eq QUEUE_LOCATION)
      {
	$where = Automake::Location::deserialize ($queue);
      }
    elsif ($loc_key eq QUEUE_STRING)
      {
	$where = $queue->dequeue ();
      }
    else
      {
	prog_error "unexpected key $loc_key";
      }
    my $mystrict = $queue->dequeue ();
    my $nfiles = $queue->dequeue ();
    my @files;
    push @files, $queue->dequeue ()
      foreach (1 .. $nfiles);
    return
      unless $strictness >= $mystrict;
    foreach my $file (@files)
      {
        required_file_check_or_copy ($where, $config_aux_dir, $file);
      }
}

# require_conf_file ($WHERE, $MYSTRICT, @FILES)
# ---------------------------------------------
# Looks in configuration path, as specified by AC_CONFIG_AUX_DIR.
sub require_conf_file
{
    my ($where, $mystrict, @files) = @_;
    my $queue = defined $required_conf_file_queue ? 1 : 0;
    require_file_internal ($where, $mystrict, $config_aux_dir,
                           $queue, @files);
}


# require_conf_file_with_macro ($COND, $MACRO, $MYSTRICT, @FILES)
# ---------------------------------------------------------------
sub require_conf_file_with_macro
{
    my ($cond, $macro, $mystrict, @files) = @_;
    require_conf_file (rvar ($macro)->rdef ($cond)->location,
		       $mystrict, @files);
}

################################################################

# require_build_directory ($DIRECTORY)
# ------------------------------------
# Emit rules to create $DIRECTORY if needed, and return
# the file that any target requiring this directory should be made
# dependent upon.
# We don't want to emit the rule twice, and want to reuse it
# for directories with equivalent names (e.g., 'foo/bar' and './foo//bar').
sub require_build_directory
{
  my $directory = shift;

  return $directory_map{$directory} if exists $directory_map{$directory};

  my $cdir = File::Spec->canonpath ($directory);

  if (exists $directory_map{$cdir})
    {
      my $stamp = $directory_map{$cdir};
      $directory_map{$directory} = $stamp;
      return $stamp;
    }

  my $dirstamp = "$cdir/\$(am__dirstamp)";

  $directory_map{$directory} = $dirstamp;
  $directory_map{$cdir} = $dirstamp;

  # Set a variable for the dirstamp basename.
  define_pretty_variable ('am__dirstamp', TRUE, INTERNAL,
			  '$(am__leading_dot)dirstamp');

  # Directory must be removed by 'make distclean'.
  $clean_files{$dirstamp} = DIST_CLEAN;

  $output_rules .= ("$dirstamp:\n"
		    . "\t\@\$(MKDIR_P) $directory\n"
		    . "\t\@: > $dirstamp\n");

  return $dirstamp;
}

# require_build_directory_maybe ($FILE)
# -------------------------------------
# If $FILE lies in a subdirectory, emit a rule to create this
# directory and return the file that $FILE should be made
# dependent upon.  Otherwise, just return the empty string.
sub require_build_directory_maybe
{
    my $file = shift;
    my $directory = dirname ($file);

    if ($directory ne '.')
      {
	return require_build_directory ($directory);
      }
    else
      {
	return '';
      }
}

1;