views:

49

answers:

1

hi, I am in the process of learning makefile creation.

Current target is to scan the $(SOURCEDIR) for .c files and create (according to that lise of .c file paths in the format of /path/file.c) a nice $(VPATH) so I do not need to use recursive makefiles (aka pain in the a..).

Atm I am stuck with this, where $(SOURCETREE) will be empty on $(ECHO)

SOURCES := $(shell find $(SOURCEDIR) -name '*.c')
SOURCETREE := $(dir $(SOURCES))
SOURCETREE := $(shell $(ECHO) $(SOURCETREE) | $(SED) -e "s/[[:space:]]/\n/g" | uniq | $(SED) -e "s/\n/[[:space:]]/g");

Maybe I just do not get the point (got late again :/ )

Thanks for any help.

Note: In a bash shell it works perfectly on my linux workbox (I replace thevariables accordingly) Note: I am no sed pro, so please explain if you do an voodoo with sed, thank you

+1  A: 

Comments:

  1. Remove the backticks. They are unnecessary; the $(shell) function already captures the command's output.

  2. The echo/sed/uniq/sed chain can be replaced by ls/uniq. ls will print one file name per line so no need for the replace-and-undo song and dance.

  3. It looks like you're losing the result from $(dir) because you use $(SOURCES) rather than $(SOURCETREE) in the third assignment.

Result:

SOURCES := $(shell find $(SOURCEDIR) -name '*.c')
SOURCETREE := $(dir $(SOURCES))
SOURCETREE := $(shell ls $(SOURCETREE) | uniq);

Actually, even this shortened shell invocation is unnecessary. The $(sort) function will sort a list of names and remove duplicates. From make's documentation: "Incidentally, since sort removes duplicate words, you can use it for this purpose even if you don't care about the sort order."

SOURCES := $(shell find $(SOURCEDIR) -name '*.c')
SOURCETREE := $(sort $(dir $(SOURCES)))
John Kugelman
not that easy, ls does not work in my case because the source dir has actually n-subdirs, therefore I use find. The result is a space seperated list of paths whereas uniq only can handly linbreak seperated lists. Thank you for noting the backticks. Though you are right dir output is lost, the utput totally should not be "" (zer0 chars)
penguinpower
Sorry, my answer was unclear. I didn't mean for `find` to be removed. I added in the previous lines to my answer for context.
John Kugelman
sort thingy works, but I am still interested in how to call sed within makefile rule, as I will probably stumble open sed in the near/far/distant futuree.g. for VPATH setup I have to replace the space/line-breaks with :
penguinpower
`VPATH := $(shell $(ECHO) $(SOURCETREE)| $(SED) -e "s/[[:space:]]/:/g" )` does its job
penguinpower