Makefile variable as prerequisite

后端 未结 8 615
清歌不尽
清歌不尽 2020-12-07 09:45

In a Makefile, a deploy recipe needs a environment variable ENV to be set to properly execute itself, whereas others don\'t care, e.g.:



        
8条回答
  •  隐瞒了意图╮
    2020-12-07 10:02

    I've found with the best answer cannot be used as a requirement, except for other PHONY targets. If used as a dependency for a target that is an actual file, using check-env will force that file target to be rebuilt.

    Other answers are global (e.g. the variable is required for all targets in the Makefile) or use the shell, e.g. if ENV was missing make would terminate regardless of target.

    A solution I found to both issues is

    ndef = $(if $(value $(1)),,$(error $(1) not set))
    
    .PHONY: deploy
    deploy:
        $(call ndef,ENV)
        echo "deploying $(ENV)"
    
    .PHONY: build
    build:
        echo "building"
    

    The output looks like

    $ make build
    echo "building"
    building
    $ make deploy
    Makefile:5: *** ENV not set.  Stop.
    $ make deploy ENV="env"
    echo "deploying env"
    deploying env
    $
    

    value has some scary caveats, but for this simple use I believe it is the best choice.

提交回复
热议问题