Skip to content

Commit

Permalink
expandVars.pl now only writes output when it changes
Browse files Browse the repository at this point in the history
  • Loading branch information
anjohnson committed Aug 26, 2022
1 parent d7030ae commit 7607e95
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
15 changes: 5 additions & 10 deletions configure/RULES_EXPAND
Expand Up @@ -47,35 +47,30 @@ 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)
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
Expand Down
49 changes: 41 additions & 8 deletions src/tools/expandVars.pl
Expand Up @@ -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 <anj@aps.anl.gov>
# Date: 10 February 2005
Expand All @@ -22,11 +23,10 @@
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
Expand Down Expand Up @@ -59,17 +59,50 @@
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\@"}e;
$_
} <$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 <<EOF;
Usage:
expandVars.pl -h
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
EOF
exit $opt_h ? 0 : 1;
Expand Down

0 comments on commit 7607e95

Please sign in to comment.