I did extensive searching and found many similar questions, but did not find one that actually did what I wanted, as far as I could tell.
Here is the closest question: Using sed in a makefile; how to escape variables?
A very simplified version of what I want to do inside the Makefile is
sed s~foo~${BAR}~
where $BAR
is a variable in my makefile. Obviously, I want to replace foo
with the value of $BAR
not with the text ${BAR}
as is currently happening.
Since it was requested, here is the full version:
Note, the goal is to have the Makefile be POSIX.
Makefile (relevant part only):
...
diff:
scripts/build/diff-all.sh
...
scripts/build/diff-all.sh
#!/bin/bash -e
for DIFFBRANCH in $(cat diff-branches); do
scripts/build/diff.sh $DIFFBRANCH
done
diff-branches
2016_reorg_diff
# more branches here
scripts/build/diff.sh
#/bin/bash -e
DIFFBRANCH=$1
BRANCH=$(git rev-parse --abbrev-ref HEAD)
CHAPTERDIR=src/chapters
SHORTCHAPTERDIR=${CHAPTERDIR##src/} # remove src/ from the start of CHAPTERDIR
CHAPTERS=$(ls $CHAPTERDIR | grep "^\\d\\d_.*\\.tex$")
LATEXARGS="-file-line-error -halt-on-error"
if [[ ! $DIFFBRANCH ]]; then
echo "usage: $0 DIFFBRANCH"
exit 1
fi
mkdir -p tmp
mkdir -p pdf
for CHAPTER in $CHAPTERS; do # make tex diff files for each chapter
SLUG=$(echo $CHAPTER | cut -f 1 -d '.')
latexdiff-vc --git --so --flatten --force -r $DIFFBRANCH -r $BRANCH $CHAPTERDIR/$CHAPTER
mkdir -p tmp/$CHAPTERDIR
mv -v $CHAPTERDIR/$SLUG-diff$DIFFBRANCH-$BRANCH.tex tmp/src_diff_$DIFFBRANCH/$SHORTCHAPTERDIR/$SLUG-$BRANCH-diff-$DIFFBRANCH.tex
done
rsync -avz --exclude $SHORTCHAPTERDIR src/ tmp/src_diff_$DIFFBRANCH/ # copy all source files to tmp except chapters (because they're already there)
sed -i.bak -E 's~^([[:blank:]]*\subimport{chapters/}{[[:alnum:]_]+)}~\1-$BRANCH-diff-$DIFFBRANCH}~' tmp/src_diff_$DIFFBRANCH/iuf-rulebook.tex
# this replaces \subimport{chapters/}{01_general} with \subimport{chapters/}{01_general-$BRANCH-diff-$DIFFBRANCH} with the variables expanded
# so that the iuf-rulebook.tex uses the diffed chapters
# the -i.bak is required so SED works on OSX and Linux
rm -f tmp/src_diff_$DIFFBRANCH/*.bak # because of this, we have to delete the .bak files after
mkdir -p tmp/out_diff_$DIFFBRANCH
TEXINPUTS=tmp/src_diff_$DIFFBRANCH: latexmk -pdf $LATEXARGS -quiet -output-directory=./tmp/out_diff_$DIFFBRANCH tmp/src_diff_$DIFFBRANCH/iuf-rulebook.tex
mv tmp/out_diff_$DIFFBRANCH/iuf-rulebook.pdf pdf/iuf-rulebook-$BRANCH-diff-$DIFFBRANCH.pdf
rm -rf tmp/*_$DIFFBRANCH