I have the following line in my makefile:

var  := $(sort $(foreach m,$(libs),$($(m)-objs)))

which I changed: it now uses sort from the shell:

var  := $(shell echo $(foreach m,$(libs),$($(m)-objs)) | tr ' ' '\n' | sort | tr '\n' ' ' )

But it turns out that after my modification $(var) is empty. What did I do wrong?

EDIT: I was trying to substitute gnu-make builtin sort with bash sort, because I wanted to have more flexibility over process of sorting. It turned out that output from foreach is strange. I reproduced the problem with the following makefile:

a-obj := hhh lll
b-obj := zzz aaa
x := a b
xx := $(shell echo $(foreach m,$(x),$($(m)-obj)) | tr ' ' '\n' | sort)
yy := $(sort $(foreach m,$(x),$($(m)-obj)))

    @echo $(xx)
    @echo $(yy)

which gives the expected output:

$ make
aaa hhh lll zzz
aaa hhh lll zzz

I am sorry for bothering you.

closed as unclear what you're asking by Gilles, cuonglm, slm Jul 24 '15 at 2:25

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • I don't know anything about the makefile format but don't you need to somehow group the commands you want to echo? Something like echo $( $(foreach m,$(libs),$($(m)-objs)) | tr ' ' '\n' | sort | tr '\n' ' ')? – terdon Jul 23 '15 at 10:22
  • @terdon it does not help – user2551229 Jul 23 '15 at 10:38
  • Can you try with the execute: target for printing the result? Using $(shell) makes recipes are implicitly using the shell. The problem with this is that it moves the work into the command expansion phase, which means it can’t run in parallel. The solution for this one is not use $(shell). – Thushi Jul 23 '15 at 11:22
  • I find out the solution: second tr command eats trailing new line character which I append with: ... | sed 's/.*/&\n/' – user2551229 Jul 23 '15 at 11:25
  • Works for me. Post a complete code example that reproduces the problem. Tell us what version of make you're using. – Gilles Jul 24 '15 at 1:09

Browse other questions tagged or ask your own question.