Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to use directory names in GNU make patterns?

Tags:

gnu-make

I have a number of device drivers: the source code for each device is in a sub-directory with the same name as the device:

devices/foo/foo.c
devices/bar/bar.c

If I set up an individual GNU make rule for each device like this, it works:

obj/foo.o: devices/foo/foo.c
    $(CC) $(CFLAGS) devices/foo/foo.c -o obj/foo.o

If I set up a make pattern like this, it is unable to work out how to make the file. I guess that the problem is to do with using a % as the name of the sub-directory.

obj/%.o: devices/%/%.c
    $(CC) $(CFLAGS) $< -o $@ 

Is there some way that I can set up a pattern to do this?

like image 409
JavaLatte Avatar asked Oct 15 '18 06:10

JavaLatte


People also ask

Does GNU Make support pattern-specific variable values?

In addition to target-specific variable values (see section Target-specific Variable Values ), GNU make supports pattern-specific variable values. In this form, a variable is defined for any target that matches the pattern specified.

How to name installation directories in GNU/Linux?

Installation directories should always be named by variables, so it is easy to install in a nonstandard place. The standard names for these variables and the values they should have in GNU packages are described below. They are based on a standard file system layout; variants of it are used in GNU/Linux and other modern operating systems.

What variables should I use to specify directories for makefiles?

Here are the variables Makefiles should use to specify directories to put these various kinds of files in: The root of the directory tree for read-only architecture-independent data files. This should normally be /usr/local/share, but write it as $ (prefix)/share.

What is a%a pattern in Linux?

A pattern rule looks like an ordinary rule, except that its target contains the character ‘%’ (exactly one of them). The target is considered a pattern for matching file names; the ‘ % ’ can match any nonempty substring, while other characters match only themselves.


1 Answers

Another approach would be to define the following function expand in your makefile:

expand = devices/$1/$1.c

Then, having secondary expansion enabled, you can call that function for determining the prerequisites of the target based on the stem, which is in $*:

.SECONDEXPANSION:
obj/%.o: $$(call expand,$$*)
    $(CC) $(CFLAGS) $< -o $@
like image 79
ネロク・ゴ Avatar answered Oct 13 '22 07:10

ネロク・ゴ