What is right syntax of testing emptyness of value returned by a function and why?

StackOverflow https://stackoverflow.com/questions/22181878

  •  05-06-2023
  •  | 
  •  

Pergunta

I have the following recipe to copy files only when sub-makefiles add files to copy.

updated_example:
ifneq($(strip $^),)
       cp -rf $^ example && touch updated_example
endif

a sub-makefile may add update_example: file1 file2 etc. but it doesn't work even echo $^ shows it is not empty.

Weirdly, I changed ifneq to ifeq, the recipe get executed!?

I tried ifneq("$(strip $^)","") still doesn't work. It drives me crazy, as I know it has to work somehow. but what exactly is the right syntax?

Foi útil?

Solução

updated_example:
    $(if $^,cp -rf $^ example && touch $@)

The recipe for a make rule (i.e., the bunch of shell commands) is stored as a single recursively expanded macro. When make decides it has to re-build a target, it

  • Expands said macro
  • Passes each line in the expansion one at a time to a shell invocation

In your version, the ifneq is expanded as make is reading the Makefile, just as it's wondering what to put into the macro representing the recipe. At this point $^ does not exist. In my formulation, I use an $(if ...) which sits inside the shell recipe. It is expanded only when make starts building updated_example, at which point $^ has appropriate value.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top