handling "multi-part" source files

Robert P. J. Day rpjday at mindspring.com
Thu May 11 16:15:40 UTC 2006


On Thu, 11 May 2006, Rob Landley wrote:

> On Wednesday 10 May 2006 4:03 pm, Robert P. J. Day wrote:
> >   a sample makefile you can drop into libbb and just run with
> >
> > $ make -f <filename> allobjs
> >
> > to see how it would handle compiling multi-part source files.
> >
> > rday
> >
> > =============== cut here ===============B
> > #
> > #  Macro to extract the "subparts" of a given multi-part source file.
> > #
> >
> > get-file-subparts = $(shell grep -h "^\#ifdef L_" ${1} | sed -e "s/^\#ifdef
> > L_//")
>
> You don't need grep then sed, you should be able to just go:
>
> $(shell sed -n -e 's/^#ifdef L_//p')

um ... yeah, i knew that. :-P

> > MULTISRCS := $(shell grep -l "^\#ifdef L_" $(wildcard *.c))
> > MULTISUBPARTS := $(foreach msrc,${MULTISRCS},$(call \
> >      get-file-subparts,${msrc}))
> > MULTIOBJS := $(addsuffix .o,${MULTISUBPARTS})
>
> I'm a bit confused how you're correlating what part comes from which
> file here.  Is there some kind of nested array syntax I'm missing?
> MULTISUBPARTS is just a big flat list after the foreach, right?

that's right.  MULTISUBPARTS actually doesn't have any value for the
regular build, i was just using it to mock up a PHONY target so i
could invoke "make allobjs" to see that it worked.

technically, MULTISUBPARTS is superfluous.

> > #  For a given source file, generate the corresponding object files.
> > #
> >
> > gen-objs = $(addsuffix .o, \
> > 	$(shell grep -h "^\#ifdef L_" ${1} | sed -e "s/^\#ifdef L_//"))
>
> Why are you reproducing get-file-subparts here?  Can't you call it again?

yes, i was just being lazy, my newer version already has that fix.

> > #
> > #  Define the rule that rebuilds a set of object files from a
> > # multi-part source file.
> > #
> >
> > define objrule
> > $(call gen-objs, ${1}): ${1}
> > 	@echo CC $$< -DL_$$* -o $$@	# replace with real command
> > endef
> >
> > $(foreach src,${MULTISRCS},$(eval $(call objrule,${src})))
> >
> > .PHONY: allobjs
> > allobjs: ${MULTIOBJS}
>
> Ok, lemme see if I understand this:
>
> MULTISRCS := grep "^#ifdef L_" *.c
> MULTIOBJS := for i in $MULTISRCS: (sed -n -e 's/^#ifdef L_//p' $i) + ".o"
>
> I'm still confused about the objrule bit, since the foreach that calls it
> isn't a target of allobjs...

ignore that "allobjs" rule, it was just for testing.  let me post a
cleaned-up version and i'll check out the rest of your note while i do
that.

rday



More information about the busybox mailing list