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

后端 未结 19 2076
借酒劲吻你
借酒劲吻你 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:46

    Use string substitution:

    VERSION := 1.1.1
    PACKAGE_NAME := Foo Bar
    DOWNLOAD_URL := https://go.get/some/thing.tar.gz
    
    ANNOUNCE_BODY := Version $(VERSION) of $(PACKAGE_NAME) has been released. \
        | \
        | It can be downloaded from $(DOWNLOAD_URL) \
        | \
        | etc, etc
    

    Then in your recipe, put

        @echo $(subst | ,$$'\n',$(ANNOUNCE_BODY))
    

    This works because Make is substituting all occurrences of (note the space) and swapping it with a newline character ($$'\n'). You can think of the equivalent shell-script invocations as being something like this:

    Before:

    $ echo "Version 1.1.1 of Foo Bar has been released. | | It can be downloaded from https://go.get/some/thing.tar.gz | | etc, etc"
    

    After:

    $ echo "Version 1.1.1 of Foo Bar has been released.
    >
    > It can be downloaded from https://go.get/some/thing.tar.gz
    > 
    > etc, etc"
    

    I'm not sure if $'\n' is available on non-POSIX systems, but if you can gain access to a single newline character (even by reading a string from an external file), the underlying principle is the same.

    If you have many messages like this, you can reduce noise by using a macro:

    print = $(subst | ,$$'\n',$(1))
    

    Where you'd invoke it like this:

    @$(call print,$(ANNOUNCE_BODY))
    

    Hope this helps somebody. =)

提交回复
热议问题