#!@PERL@ # -*- perl -*- # @configure_input@ use strict; use DirHandle; use File::Basename; use File::Compare; use File::Copy; use File::stat; use FileHandle; use POSIX qw(O_RDWR O_CREAT SEEK_SET); ######################################################################## # # Command-line option handling # @ARGV == 4 or die "Usage: $0 \n"; my $generate_dir = shift; my $cache_dir = shift; my $commands = (join "\n", @ARGV) . "\n"; ######################################################################## # # Update a cache entry # # Accepts one generated file name, source or header, as an argument. # If the corresponding cache file is textually identical, gives the # generated file the same timestamp. Otherwise, copies the generated # file into the cache. # # close enough for our purposes, and avoids making many system calls my $now = time; sub update_file ($) { my $gfile = shift; my $cfile = "$cache_dir/" . basename $gfile; my $gstat = stat $gfile; my $cstat = stat $cfile; if ($cstat && $gstat->size == $cstat->size && !compare $gfile, $cfile) { utime $now, $cstat->mtime, $gfile; } else { copy $gfile, $cfile; utime $now, $gstat->mtime, $cfile; } } ######################################################################## # # Update a cache entry's compilation command # # Accepts one generated source file name as an argument. Ensures # that the corresponding cached compilation commands match # $commands. If it already matches, no file is changed. # sub update_command ($) { my $gfile = shift; my $basename = basename $gfile, '.c'; my $commandfile = "$cache_dir/$basename.cmd"; my $handle = new FileHandle $commandfile, O_RDWR | O_CREAT or die "Could not open $commandfile: $!\n"; my $content = join '', <$handle>; if ($content ne $commands) { $handle->seek(0, SEEK_SET); $handle->truncate(0); $handle->print($commands); } } ######################################################################## # # The main event # my $dir = new DirHandle $generate_dir or die "Could not scan $generate_dir: $!\n"; foreach ($dir->read) { my $file = "$generate_dir/$_"; /\.c$/ && update_command $file; /\.[ch]$/ && update_file $file; }