Assign variable once when first used in Makefile [duplicate]











up vote
2
down vote

favorite













This question already has an answer here:




  • How to get “at most once” semantics in variable assignments?

    2 answers




How do I only invoke a make shell function once in a variable expansion, and only when the variable is first used? I don't want to use := to expand at declaration time (ie: simple expansion) because the expansion is expensive and only some of my targets need the variable.



I tried using conditional variable assignment but it invokes the shell every time, for example, the following invokes shell ls twice:



.PHONY: test

FILES ?= $(warning Invoking the shell)$(shell ls)

test:
echo $(FILES) one
echo $(FILES) two









share|improve this question













marked as duplicate by Renaud Pacalet, Community Nov 21 at 6:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    A longer writeup is here: make.mad-scientist.net/deferred-simple-variable-expansion
    – MadScientist
    Nov 18 at 14:23















up vote
2
down vote

favorite













This question already has an answer here:




  • How to get “at most once” semantics in variable assignments?

    2 answers




How do I only invoke a make shell function once in a variable expansion, and only when the variable is first used? I don't want to use := to expand at declaration time (ie: simple expansion) because the expansion is expensive and only some of my targets need the variable.



I tried using conditional variable assignment but it invokes the shell every time, for example, the following invokes shell ls twice:



.PHONY: test

FILES ?= $(warning Invoking the shell)$(shell ls)

test:
echo $(FILES) one
echo $(FILES) two









share|improve this question













marked as duplicate by Renaud Pacalet, Community Nov 21 at 6:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 1




    A longer writeup is here: make.mad-scientist.net/deferred-simple-variable-expansion
    – MadScientist
    Nov 18 at 14:23













up vote
2
down vote

favorite









up vote
2
down vote

favorite












This question already has an answer here:




  • How to get “at most once” semantics in variable assignments?

    2 answers




How do I only invoke a make shell function once in a variable expansion, and only when the variable is first used? I don't want to use := to expand at declaration time (ie: simple expansion) because the expansion is expensive and only some of my targets need the variable.



I tried using conditional variable assignment but it invokes the shell every time, for example, the following invokes shell ls twice:



.PHONY: test

FILES ?= $(warning Invoking the shell)$(shell ls)

test:
echo $(FILES) one
echo $(FILES) two









share|improve this question














This question already has an answer here:




  • How to get “at most once” semantics in variable assignments?

    2 answers




How do I only invoke a make shell function once in a variable expansion, and only when the variable is first used? I don't want to use := to expand at declaration time (ie: simple expansion) because the expansion is expensive and only some of my targets need the variable.



I tried using conditional variable assignment but it invokes the shell every time, for example, the following invokes shell ls twice:



.PHONY: test

FILES ?= $(warning Invoking the shell)$(shell ls)

test:
echo $(FILES) one
echo $(FILES) two




This question already has an answer here:




  • How to get “at most once” semantics in variable assignments?

    2 answers








makefile gnu-make






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 18 at 2:38









tekumara

5,06264058




5,06264058




marked as duplicate by Renaud Pacalet, Community Nov 21 at 6:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Renaud Pacalet, Community Nov 21 at 6:54


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    A longer writeup is here: make.mad-scientist.net/deferred-simple-variable-expansion
    – MadScientist
    Nov 18 at 14:23














  • 1




    A longer writeup is here: make.mad-scientist.net/deferred-simple-variable-expansion
    – MadScientist
    Nov 18 at 14:23








1




1




A longer writeup is here: make.mad-scientist.net/deferred-simple-variable-expansion
– MadScientist
Nov 18 at 14:23




A longer writeup is here: make.mad-scientist.net/deferred-simple-variable-expansion
– MadScientist
Nov 18 at 14:23












1 Answer
1






active

oldest

votes

















up vote
-1
down vote













Move the assignment inside the recipe



test:
FILES=$$(ls) ;
echo $$FILES one ;
echo $$FILES two





share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    -1
    down vote













    Move the assignment inside the recipe



    test:
    FILES=$$(ls) ;
    echo $$FILES one ;
    echo $$FILES two





    share|improve this answer

























      up vote
      -1
      down vote













      Move the assignment inside the recipe



      test:
      FILES=$$(ls) ;
      echo $$FILES one ;
      echo $$FILES two





      share|improve this answer























        up vote
        -1
        down vote










        up vote
        -1
        down vote









        Move the assignment inside the recipe



        test:
        FILES=$$(ls) ;
        echo $$FILES one ;
        echo $$FILES two





        share|improve this answer












        Move the assignment inside the recipe



        test:
        FILES=$$(ls) ;
        echo $$FILES one ;
        echo $$FILES two






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 18 at 7:21









        user657267

        17.2k52958




        17.2k52958















            Popular posts from this blog

            If I really need a card on my start hand, how many mulligans make sense? [duplicate]

            Alcedinidae

            Can an atomic nucleus contain both particles and antiparticles? [duplicate]