M Option in Make Command, Makefile

M Option in Make Command, Makefile
make -C ~/kernel-2.6 M=`pwd` modules

What is the meaning in M='pwd' in the line above ?

I could not understand the explanation :

The M= option causes that makefile to move back into your module source directory before trying to build the modules target.

Can you make this more clear ?

6 Answers

M is not an option for make. Note it lacks the hyphen. M is a variable assigned to the execution of make. If make executes a Makefile script, this script can read the variable M and use its contents.

In the example you provide, make will read Makefile in ~/kernel-2.6 and assign your present working directory to variable M. Typically, this will allow for make to return to your current directory after processing Makefile.

1

I had similar quiz with

make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

Here the make is called within my project's directory. -C is make option:

-C dir, --directory=dir Change to directory dir before reading the makefiles or doing anything else. If multiple -C options are specified, each is interpreted relative to the previous one: -C / -C etc is equivalent to -C /etc. This is typically used with recursive invocations of make.

M is not make option but argument passed to it. Since -C changes make directory we know that make will read make file in that directory. By inspection of make file in that directory I have discovered what it is going then to do with M:

From make file (named Makefile) in the directory pointed to by -C (btw it is kernel build directory):

# Use make M=dir to specify directory of external module to build
# Old syntax make ... SUBDIRS=$PWD is still supported
# Setting the environment variable KBUILD_EXTMOD takes precedence
ifdef SUBDIRS
  KBUILD_EXTMOD ?= $(SUBDIRS)
endif

Explanation from Linux Device Drivers, 3rd Edition, Jonathan Corbet et al.:

This command starts by changing its directory to the one provided with the -C option (that is, your kernel source directory). There it finds the kernel's top-level makefile. The M= option causes that makefile to move back into your module source directory before trying to build the modules target.

Read the manual for make:

-C dir, --directory=dir
            Change to directory dir before reading the makefiles or doing anything else.

Your invocation changes the directory to ~/kernel and effectively calls make there, i.e. reading the Makefile from that directory. With the M variable, the makefile knows where your actual project files are and can change back to that location.

The point is that you don't write your own makefile, but use a single, once-and-for-all version.

0

Other answers are mostly correct, but I want to tell you the way to learn it.

  1. M is not a option
  2. M is a variable in Makefile
  3. so we should read the Makefile in the directory that the make -C changes to.

Read the Makefile: there are few comments:

# Use make M=dir to specify directory of external module to build
# Old syntax make ... SUBDIRS=$PWD is still supported
# Setting the environment variable KBUILD_EXTMOD take precedence
ifdef SUBDIRS
  KBUILD_EXTMOD ?= $(SUBDIRS)
endif

ifeq ("$(origin M)", "command line")
  KBUILD_EXTMOD := $(M)
endif

You can search for KBUILD_EXTMOD:

VPATH       := $(srctree)$(if $(KBUILD_EXTMOD),:$(KBUILD_EXTMOD))
module-dirs := $(addprefix _module_,$(KBUILD_EXTMOD))

That's how the kernel's Makefile can build your out-of-tree sources.

The command to build an external module is:

    $ make -C <path_to_kernel_src> M=$PWD

The kbuild system knows that an external module is being built due to the "M=" option given in the command.

To build against the running kernel use:

    $ make -C /lib/modules/`uname -r`/build M=$PWD

In a Unix shell, writing `foobar` in the middle of a command means "run the command foobar and substitute its output here."

So including M=`pwd` in the make command means "run the pwd command to print the current working directory, and set the M variable to that value."

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.