Jump to content
Aerosol

Some tricks which i got from linux makefile

Recommended Posts

Posted

I'm not superuser of make, but sometime i use it. My knowledge about the files usually end in the next things: I know that if I'll create Makefile file, write some simple rules which are in general form:

target:
command

And than execute make target, it executes command. Last time ago i started to learn some low-level stuffs as assembly programming, operating system theory and practice (as you can read and understand from previous blog posts about assembly programming). Some days ago i started to read Linux kernel Makefile and here i want to share some make syntax features which i learned from there. So this post is not for people who used make much time and knows much about it.

origin

origin - is a builtin function which returns information about variable. It's general form is:

$(origin var)

You can pass any $(variable) to it and it will return info about it. Return value of origin can be:

  • undefined - variable didn't defined
  • default - default var, like $(CC) and etc...
  • environemnt - from environment
  • environment override - return environemnt which was overridden with make -e
  • file - from makefile
  • command line - from make command line arguments
  • automatic - see bellow

So we can use it like this:

all:
ifeq ("$(origin A)", "command line")
ALL = $(A)
endif

@

Usually makefile prints every action after executing it, but sometimes we no need in it. We can use @ prefix for it, for example:

[CODE @Echo "Done." line, but just prints Done.

filter

filter function removes all space separated words from text, which doesn't match no one template. General view of it:

'$(filter template..., text)' 

For example, we have targets list and we need to check is there build target there or not:

targets := all clean  

findTargets: $(targets)
ifeq $($(filter build, $(targets),)

Check empty value

We can check empty value with following:

ifeq ($(VAR_CAN_BE_EMPTY)),)

Current directory

Previously when i was need to get current directory path, i used something like this:

curr = $(shell pwd)

Now i know about builtin variable:

$(CURDIR)

Errors, Warning and Info

There are 3 builtin functions for printing errors, warning and info:

ifeq $($(USE_VAR1),)
$(USER_VAR1 must be not empty)

ifeq $($(USE_VAR2), 1)
$(USE_VAR2 1)

ifneq $($(USE_VAR3),)
$(info $(USE_VAR1) is not empty)

VPATH

VPATH variable defines list of directories where template rules will search dependencies:

VPATH = src

%.o: %.c
gcc -c $<

Automatic variables

There are some special macros:

  • $| - Names of all the order-only prerequisites, with spaces between them
  • $@ - Filename of the target of the rule
  • $? - Names of all the prerequisites that are newer than the target
  • $< - Name of the first prerequisite

Source

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...