Makefile variable as prerequisite

后端 未结 8 628
清歌不尽
清歌不尽 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:14

    Inline variant

    In my makefiles, I normally use an expression like:

    deploy:
        test -n "$(ENV)"  # $$ENV
        rsync . $(ENV).example.com:/var/www/myapp/
    

    The reasons:

    • it's a simple one-liner
    • it's compact
    • it's located close to the commands which use the variable

    Don't forget the comment which is important for debugging:

    test -n ""
    Makefile:3: recipe for target 'deploy' failed
    make: *** [deploy] Error 1
    

    ... forces you to lookup the Makefile while ...

    test -n ""  # $ENV
    Makefile:3: recipe for target 'deploy' failed
    make: *** [deploy] Error 1
    

    ... explains directly what's wrong

    Global variant (for completeness, but not asked)

    On top of your Makefile, you could also write:

    ifeq ($(ENV),)
      $(error ENV is not set)
    endif
    

    Warnings:

    • don't use tab in that block
    • use with care: even the clean target will fail if ENV is not set. Otherwise see Hudon's answer which is more complex

提交回复
热议问题