Force Makefile to execute script before building targets

后端 未结 4 1353
予麋鹿
予麋鹿 2020-12-08 10:26

I am using Makefiles.

However, there is a command (zsh script) I want executed before any targets is executed. How do I do this?

Thanks!

4条回答
  •  感情败类
    2020-12-08 11:03

    Solution for both preprocessing and postprocessing in makefiles using MAKECMDGOALS and double colon rules.

    MAKECMDGOALS are the targets listed on the command line.

    First step is to get the first and last targets from the command line, or if there are no targets listed, use the default target.

    ifneq ($(MAKECMDGOALS),)
    FIRST_GOAL := $(word 1, $(MAKECMDGOALS))
    LAST_GOAL := $(word $(words $(MAKECMDGOALS)), $(MAKECMDGOALS))
    else
    FIRST_GOAL := all
    LAST_GOAL := all
    endif
    

    Double colon rules allow multiple recipes for the same target executed in order. You'll have to change all command line targets to double colon rules.

    #Dummy rule to set the default
    .PHONY: all
    all ::
    
    #Preprocessing
    $(FIRST_GOAL) ::
        echo "Starting make..."
    
    all :: normal_prerequistes
        normal_recipe
    
    other_stuff
    
    #Postprocessing
    $(LAST_GOAL) ::
        echo "All done..."
    

提交回复
热议问题