13
votes

I want a different version of the clean target to run based on whether make dev or make prod are run on a makefile.

I'm not compiling anything per se, just want to conditionally call a particular target or set of targets based on a variable, for example:

ifeq ($(BUILD_ENV),"development")
clean: -clean
else
clean: -clean-info
endif

#---------------------------------
dev: BUILD_ENV = development
dev: dev-setup which-env

#---------------------------------
prod: BUILD_ENV = production
prod: prod-setup which-env

#---------------------------------


which-env: clean
    @echo -e "$(GREEN)$(BUILD_ENV)!$(CLEAR)"

-clean: -clean-info -clean-logs | silent
    @echo -e "$(GREEN)</CLEAN>$(CLEAR)"

-clean-info:
    @echo -e "$(GREEN)<CLEAN>...$(CLEAR)"

-clean-logs:
    @echo -e " $(GREY)Removing log and status files $(CLEAR)";
    @if [ -d .stat ]; then rm -rf .stat; fi
    @rm -f *.log || true

Is there a way to do this with Makefiles? I havent found anything yet that illustrates this use-case.

I'm not trying to specifically clean anything or build anything this is just an example of me trying to conditionally call a set of targets. The actual targets could be anything else.

1
I don't quite understand what you want. You want to run the appropriate clean target when you run make dev or make prod? But wouldn't that just clean up the things you just got through creating? Or, are you trying to say that when you run make clean you want to run the version of the target based on whichever you ran last, either make dev or make prod? - MadScientist
@MadScientist -- this is just an example -- dont get to caught up in the particular clean commands, I only wrote this to demonstrate what Im trying to do with targets. In this case I only want to run the clean target if the BUILD_ENV variable is set to development, or just clean_info otherwise. ( I just want a way to call a specific set of targets based on the value of a variable. ) - qodeninja

1 Answers

17
votes

It's not at all clear that what you're asking for is really what you want, but here goes:

all:

ifeq ($(BUILD_ENV),development)
all: clean-dev
else
all: clean-other
endif

clean-dev:
    @echo running $@, doing something

clean-other:
    @echo running $@, doing something else

If you run make BUILD_ENV=development, you'll get something; if you run make or make BUILD_ENV=production you'll get something else.