Makefile: modifying stem in pattern rule -


i have files in directories called data , helpers, , use both of them create target files in result.

the directory structure following:

data  +  | + file1  | + file2  | + ...  + b  | + file1  ... helpers  + file1  + file2  ... 

the directory structure in result same in data. therefore, write following rule:

result/%: data/% helpers/% script     script $@ $(word 1,$^) $(word 2,$^) 

the problem file1, file2, etc. in helpers directory not in subdirectory.

i have got following options:

  • write separate rule every subdirectory (impractical, there 20)
  • do foreach including every subdirectory create rule (how can access prerequisite list then?)

however, rather write 1 of following:

result/%: data/% helpers/$(basename %) # doesn't work result/%: data/% helpers/$(basename $*) # doesn't work either 

is there way modify pattern in rule declaration modifying stem matches?

the way want enable secondary expansion in makefile.

example:

note directory prefix (d), described in implicit rule search algorithm, appended (after expansion) patterns in prerequisites list. example:

.secondexpansion:

/tmp/foo.o:

%.o: $$(addsuffix /%.c,foo bar) foo.h         @echo $^ 

the prerequisite list printed, after secondary expansion , directory prefix reconstruction, /tmp/foo/foo.c /tmp/bar/foo.c foo.h. if not interested in reconstruction, can use $$* instead of % in prerequisites list.

so should work (untested):

.secondexpansion: result/%: data/% helpers/$$(notdir %) 

make's basename not same shell's basename (that constantly trips me up).

if wanted use $(foreach) though want (untested):

define dirrule result/$1/%: data/$1/% helpers/%         @echo cmd 1         @echo cmd 2 endef  $(foreach dir,subdir1 subdir2 ... subdir20,$(eval $(call dirrule,$(dir)))) 

Comments