Issue
I have a folder with a lot of header (.h) files and to install our library I need to copy these files to a system folder. However I would like to copy only the files that have changed. How can I do that using just make and cp, without having to use any other tool like rsync?
Solution
You could use constructs like this:
/path/to/system/header.h: /path/to/local/header.h
cp $^ $@
which copies the local header to the system header if the local version is newer.
If you have GNU make, like on Ubuntu, you can create fancy functions, like this.
It copies updated header files from directory SRCDIR to DSTDIR. Try it by running make all
. It should only copy newer files and do nothing on a second run.
.DEFAULT_GOAL = all
SRCDIR = local
DSTDIR = system
HEADERS = $(shell cd $(SRCDIR); ls *.h)
# Define function; $(1) is the header file name.
define copyheader =
$(DSTDIR)/$(1): $(SRCDIR)/$(1)
cp $$^ $$@
endef
# Use function to create recipes for each header file.
$(foreach file,$(HEADERS),$(eval $(call copyheader,$(file))))
# Depend on all destination headers.
all: $(addprefix $(DSTDIR)/,$(HEADERS))
Answered By - Jens