Is it possible to create a multi-line string variable in a Makefile

后端 未结 19 2073
借酒劲吻你
借酒劲吻你 2020-11-28 20:27

I want to create a makefile variable that is a multi-line string (e.g. the body of an email release announcement). something like

ANNOUNCE_BODY=\"
Version $         


        
19条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 20:45

    You should use "define/endef" Make construct:

    define ANNOUNCE_BODY
    Version $(VERSION) of $(PACKAGE_NAME) has been released.
    
    It can be downloaded from $(DOWNLOAD_URL).
    
    etc, etc.
    endef
    

    Then you should pass value of this variable to shell command. But, if you do this using Make variable substitution, it will cause command to split into multiple:

    ANNOUNCE.txt:
      echo $(ANNOUNCE_BODY) > $@               # doesn't work
    

    Qouting won't help either.

    The best way to pass value is to pass it via environment variable:

    ANNOUNCE.txt: export ANNOUNCE_BODY:=$(ANNOUNCE_BODY)
    ANNOUNCE.txt:
      echo "$${ANNOUNCE_BODY}" > $@
    

    Notice:

    1. Variable is exported for this particular target, so that you can reuse that environment will not get polluted much;
    2. Use environment variable (double qoutes and curly brackets around variable name);
    3. Use of quotes around variable. Without them newlines will be lost and all text will appear on one line.

提交回复
热议问题