0

I was implementing the script from the following posts. Since it´s a bit old i wanted to open this new thread as i have a problem when running the makefile.

Basically i have created a top level makefile that will compile a seet of docker images in subfolders.

makefile - build mulitple files with multiple targets

# Assuming each subdirectory containins a Dockerfile
IMAGES  := $(patsubst %/,%,$(dir $(wildcard */Dockerfile)))
BUILD_TARGS = $(patsubst %,build_%,$(IMAGES))
TEST_TARGS = $(patsubst %,test_%,$(IMAGES))
PUSH_TARGS = $(patsubst %,push_%,$(IMAGES))

VERSION := 1 # $(shell git rev-parse --short=12 --verify HEAD)
DOCKER_REPO_URL := david.io/reponame

define docker_build =
build_$(1):
    @echo "Building $(1)"
    #docker build -t $(1) --force-rm $(1)
endef

define docker_test =
test_$(1):
    @echo "Testing $(1)"
    #docker inspect $(1)
    #docker run --rm $(1) help
endef

define docker_push =
push_$(1):
    @echo "Pushing $(1)"
    #docker tag $(1) $(DOCKER_REPO_URL):$(1)-$(VERSION)
    #docker push $(DOCKER_REPO_URL):$(1)-$(VERSION)
    #docker tag $$@ $(DOCKER_REPO_URL):$(1)
    #docker push $(DOCKER_REPO_URL):$(1)
endef

.PHONY: all build test release clean $(IMAGES) $(BUILD_TARGS) $(TEST_TARGS) $(PUSH_TARGS)

all: build test release

build: $(BUILD_TARGS)
    $(foreach image,$(IMAGES),$(eval $(call docker_build,$(image))))

#test: $(TEST_TARGS)
#    $(foreach image,$(IMAGES),$(eval $(call docker_test,$(image))))

#release: $(PUSH_TARGS)
#    $(foreach image,$(IMAGES),$(eval $(call docker_push,$(image))))

I have the following error:

makefile:37: *** prerequisites cannot be defined in recipes.  Stop.
david
  • 805
  • 1
  • 9
  • 21
  • This error message means that **BUILD_TARGS = $(patsubst %,build_%,$(IMAGES))** should be replaced by explicit list of targets. Prerequisite definition - see https://www.gnu.org/software/make/manual/html_node/Prerequisite-Types.html#:~:text=There%20are%20actually%20two%20different%20types%20of%20prerequisites,target%20is%20considered%20out-of-date%20and%20must%20be%20rebuilt. – Dmitry Messerman Sep 28 '21 at 17:38

1 Answers1

0

Just as the error says, you can't eval entire rules from within a recipe.

You need to do it as make parses the makefile.

Remove the TAB chars before your foreach loops, so that they are not within a recipe.

MadScientist
  • 92,819
  • 9
  • 109
  • 136