I'm writing an R package that generates Makefile
s, I need to write a Makefile
that calls Rscript
before making the targets. A MWE of the problem is below. Make quits in error because the right-hand side of .INIT
does not execute. Writing a recipe for file.rds
does not suit my needs.
a=1
.INIT=`Rscript -e 'saveRDS('$(a)', "file.rds")'`
all: file2.rds
file2.rds: file.rds
cp file.rds file2.rds
clean:
rm file.rds file2.rds
What can I do to fix this Makefile
and keep it portable? From the R extensions manual, I can't use $(shell
for what I'm trying to accomplish.
EDIT
From @Spacedman's first answer, I learned that .INIT
is "expanded"/executed if and only if it is used as a variable somewhere. This is perfect! @Spacedman, I invite you to copy the following Makefile
into an answer of your own so I can give you credit.
a=1
.INIT=`Rscript -e 'saveRDS('$(a)', "file.rds")'`
all: file2.rds
file2.rds:
echo "file.rds should not have been built."
file3.rds:
echo -n $(.INIT)
cp file.rds file3.rds
clean:
rm file.rds file2.rds
The following demonstrates the results I had hoped for.
$ make file2.rds
echo "file.rds should not have been built."
file.rds should not have been built.
$ ls file.rds
ls: cannot access file.rds: No such file or directory
$ make file3.rds
echo -n `Rscript -e 'saveRDS('1', "file.rds")'`
cp file.rds file3.rds
$ ls file.rds
file.rds