expandVars.pl now only writes output when it changes

This commit is contained in:
Andrew Johnson
2022-08-25 23:28:00 -05:00
parent d7030ae8a5
commit 0efffc1bcb
2 changed files with 46 additions and 18 deletions

View File

@ -47,7 +47,7 @@ vpath %@ $(USR_VPATH) $(ALL_SRC_DIRS)
# INC += myVersion.h # INC += myVersion.h
# Default settings # Default settings
EXPAND_TOOL ?= $(PERL) $(TOOLS)/expandVars.pl EXPAND_TOOL ?= $(PERL) $(TOOLS)/expandVars.pl $(QUIET_FLAG)
EXPANDARCH = -a $(T_A) EXPANDARCH = -a $(T_A)
EXPANDFLAGS += -t $(INSTALL_LOCATION) EXPANDFLAGS += -t $(INSTALL_LOCATION)
@ -55,27 +55,22 @@ EXPANDFLAGS += $(addprefix -D ,$(EXPAND_VARS) $($@_EXPAND_VARS))
EXPANDFLAGS += $(foreach var, $(EXPAND_ME) $($@_EXPAND_ME), \ EXPANDFLAGS += $(foreach var, $(EXPAND_ME) $($@_EXPAND_ME), \
-D$(var)="$(strip $($(var)))") -D$(var)="$(strip $($(var)))")
# The names of files to be expanded must end with '@' # Output files
EXPANDED = $(EXPAND:%@=%) EXPANDED = $(EXPAND:%@=%)
EXPANDED_COM = $(EXPAND_COMMON:%@=%) EXPANDED_COMMON = $(EXPAND_COMMON:%@=$(COMMON_DIR)/%)
EXPANDED_COMMON = $(EXPANDED_COM:%=$(COMMON_DIR)/%)
$(EXPANDED): %: %@ $(EXPANDED): %: %@
$(ECHO) "Expanding $< to $@" $(ECHO) "Expanding $< to $@"
@$(RM) $@
$(EXPAND_TOOL) $(EXPANDARCH) $(EXPANDFLAGS) $($@_EXPANDFLAGS) $< $@ $(EXPAND_TOOL) $(EXPANDARCH) $(EXPANDFLAGS) $($@_EXPANDFLAGS) $< $@
$(EXPANDED_COM): %: %@ $(EXPANDED_COMMON): $(COMMON_DIR)/%: %@
$(ECHO) "Expanding $< to $(COMMON_DIR)/$@" $(ECHO) "Expanding $< to $(COMMON_DIR)/$@"
@$(RM) $@
$(EXPAND_TOOL) $(EXPANDFLAGS) $($@_EXPANDFLAGS) $< $@ $(EXPAND_TOOL) $(EXPANDFLAGS) $($@_EXPANDFLAGS) $< $@
$(EXPANDED_COMMON): $(COMMON_DIR)/%: %
@$(CP) $< $@
clean: expand_clean clean: expand_clean
expand_clean: expand_clean:
@$(RM) $(EXPANDED) $(EXPANDED_COMMON) $(EXPANDED_COM) @$(RM) $(EXPANDED) $(EXPANDED_COMMON)
.PRECIOUS: $(EXPANDED) $(EXPANDED_COMMON) .PRECIOUS: $(EXPANDED) $(EXPANDED_COMMON)
.PHONY: expand_clean .PHONY: expand_clean

View File

@ -8,7 +8,8 @@
#************************************************************************* #*************************************************************************
# Tool to expand @VAR@ variables while copying a file. # Tool to expand @VAR@ variables while copying a file.
# The file will *not* be copied if it already exists. # The output file will *not* be written to if it already
# exists and the expansion would leave the file unchanged.
# #
# Author: Andrew Johnson <anj@aps.anl.gov> # Author: Andrew Johnson <anj@aps.anl.gov>
# Date: 10 February 2005 # Date: 10 February 2005
@ -22,11 +23,10 @@ use lib ("$Bin/../../lib/perl");
use EPICS::Getopts; use EPICS::Getopts;
use EPICS::Path; use EPICS::Path;
use EPICS::Release; use EPICS::Release;
use EPICS::Copy;
# Process command line options # Process command line options
our ($opt_a, $opt_d, @opt_D, $opt_h, $opt_t); our ($opt_a, $opt_d, @opt_D, $opt_h, $opt_q, $opt_t);
getopts('a:dD@ht:') getopts('a:dD@hqt:')
or HELP_MESSAGE(); or HELP_MESSAGE();
# Handle the -h command # Handle the -h command
@ -59,17 +59,50 @@ while ($_ = shift @opt_D) {
print "$1 = $2\n" if $opt_d; print "$1 = $2\n" if $opt_d;
} }
# Do it! # Generate the expanded output
copyFile($infile, $outfile, \%vars); open(my $SRC, '<', $infile)
or die "$! reading $infile\n";
my $output = join '', map {
# Substitute any @VARS@ in the text
s{@([A-Za-z0-9_]+)@}
{exists $vars{$1} ? $vars{$1} : "\@$1\@"}eg;
$_
} <$SRC>;
close $SRC;
print "expandVars.pl: $infile expands to:\n$output\n" if $opt_d;
##### File contains subroutines only below here # Check if the output file matches
my $DST;
if (open($DST, '+<', $outfile)) {
my $actual = join('', <$DST>);
if ($actual eq $output) {
close $DST;
print "expandVars.pl: Keeping existing output file $outfile\n"
unless $opt_q;
exit 0;
}
seek $DST, 0, 0;
truncate $DST, 0;
} else {
open($DST, '>', $outfile)
or die "Can't create $outfile: $!\n";
}
print $DST $output;
close $DST;
exit 0;
##### Subroutines only below here
sub HELP_MESSAGE { sub HELP_MESSAGE {
print STDERR <<EOF; print STDERR <<EOF;
Usage: Usage:
expandVars.pl -h expandVars.pl -h
Display this Usage message Display this Usage message
expandVars.pl -t /path/to/top [-a arch] -D var=val ... infile outfile expandVars.pl -t /path/to/top [-a arch] -D var=val ... [-q] infile outfile
Expand vars in infile to generate outfile Expand vars in infile to generate outfile
EOF EOF
exit $opt_h ? 0 : 1; exit $opt_h ? 0 : 1;