From 0efffc1bcb9e5dc520d0858719d06b4cf14ea6df Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Thu, 25 Aug 2022 23:28:00 -0500 Subject: [PATCH] expandVars.pl now only writes output when it changes --- configure/RULES_EXPAND | 15 +++++-------- src/tools/expandVars.pl | 49 ++++++++++++++++++++++++++++++++++------- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/configure/RULES_EXPAND b/configure/RULES_EXPAND index fcb945df3..b523cb5e1 100644 --- a/configure/RULES_EXPAND +++ b/configure/RULES_EXPAND @@ -47,7 +47,7 @@ vpath %@ $(USR_VPATH) $(ALL_SRC_DIRS) # INC += myVersion.h # Default settings -EXPAND_TOOL ?= $(PERL) $(TOOLS)/expandVars.pl +EXPAND_TOOL ?= $(PERL) $(TOOLS)/expandVars.pl $(QUIET_FLAG) EXPANDARCH = -a $(T_A) EXPANDFLAGS += -t $(INSTALL_LOCATION) @@ -55,27 +55,22 @@ EXPANDFLAGS += $(addprefix -D ,$(EXPAND_VARS) $($@_EXPAND_VARS)) EXPANDFLAGS += $(foreach var, $(EXPAND_ME) $($@_EXPAND_ME), \ -D$(var)="$(strip $($(var)))") -# The names of files to be expanded must end with '@' +# Output files EXPANDED = $(EXPAND:%@=%) -EXPANDED_COM = $(EXPAND_COMMON:%@=%) -EXPANDED_COMMON = $(EXPANDED_COM:%=$(COMMON_DIR)/%) +EXPANDED_COMMON = $(EXPAND_COMMON:%@=$(COMMON_DIR)/%) $(EXPANDED): %: %@ $(ECHO) "Expanding $< to $@" - @$(RM) $@ $(EXPAND_TOOL) $(EXPANDARCH) $(EXPANDFLAGS) $($@_EXPANDFLAGS) $< $@ -$(EXPANDED_COM): %: %@ +$(EXPANDED_COMMON): $(COMMON_DIR)/%: %@ $(ECHO) "Expanding $< to $(COMMON_DIR)/$@" - @$(RM) $@ $(EXPAND_TOOL) $(EXPANDFLAGS) $($@_EXPANDFLAGS) $< $@ -$(EXPANDED_COMMON): $(COMMON_DIR)/%: % - @$(CP) $< $@ clean: expand_clean expand_clean: - @$(RM) $(EXPANDED) $(EXPANDED_COMMON) $(EXPANDED_COM) + @$(RM) $(EXPANDED) $(EXPANDED_COMMON) .PRECIOUS: $(EXPANDED) $(EXPANDED_COMMON) .PHONY: expand_clean diff --git a/src/tools/expandVars.pl b/src/tools/expandVars.pl index ec4275476..a0941f7cb 100644 --- a/src/tools/expandVars.pl +++ b/src/tools/expandVars.pl @@ -8,7 +8,8 @@ #************************************************************************* # 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 # Date: 10 February 2005 @@ -22,11 +23,10 @@ use lib ("$Bin/../../lib/perl"); use EPICS::Getopts; use EPICS::Path; use EPICS::Release; -use EPICS::Copy; # Process command line options -our ($opt_a, $opt_d, @opt_D, $opt_h, $opt_t); -getopts('a:dD@ht:') +our ($opt_a, $opt_d, @opt_D, $opt_h, $opt_q, $opt_t); +getopts('a:dD@hqt:') or HELP_MESSAGE(); # Handle the -h command @@ -59,17 +59,50 @@ while ($_ = shift @opt_D) { print "$1 = $2\n" if $opt_d; } -# Do it! -copyFile($infile, $outfile, \%vars); +# Generate the expanded output +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 { print STDERR <