How to exclude a particular file with GNUMake'

2019-09-17 16:20发布

问题:

My project's directory structure is as follows:

My Makefile looks like this:

dir1_contents := $(wildcard dir1/*) 
dir3_contents := $(wildcard dir3/*)

all: clean_dir1 clean_dir3

clean_dir1:
    echo 'dir1_contents = $(dir1_contents)'

clean_dir3:
echo 'dir3_contents = $(dir3_contents)'

When I run make, this is what I get:

$ pwd
make-test

$ make -s
dir1_contents = dir1/dir2 dir1/file2.junk dir1/file3.junk
dir3_contents = dir3/file4.junk

I want to get the contents of dir1 in dir1_contents. But I want to exclude file3.junk from that list. How can I do it?

Perhaps I should use grep -v? But I'm not sure that's the best way. Is there a built-in GNUMake function I can use here?

回答1:

Use the $(filter-out pattern, text ) text function

 $(filter-out file3.junk,${dir1_contents})

(someone suggested to use %/file3.junk instead of file3.junk, but I believe you don't need that; I leave you to experiment)