Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good way to do a "switch" in a Makefile

I'm experimenting with an updated build system at work; currently, I'm trying to find a good way to set compiler & flags depending on the target platform.

What I would like to do is something like

switch $(PLATFORM)_$(BUILD_TYPE)
  case "Linux_x86_release"
     CFLAGS = -O3
  case "Linux_x86_debug"
     CFLAGS = -O0 -g
  case "ARM_release"
     CC = armcc
     AR = armlink
     CFLAGS = -O2 -fx
     ...

which is not supported by GNU Make. Now, my first thought was to just do

-include $(PLATFORM)_$(BUILD_TYPE)

which is a pretty decent solution, however, it makes it hard to get an overview of what differs between files, not to mention that I'm looking forward to writing & maintaining a good 60-80 files, each containing a set of variable definitions.

Does anyone happen to know a better way to accomplish this? I.e. setting a set of flags and other options based on another variable?

like image 725
Christoffer Avatar asked Oct 14 '08 06:10

Christoffer


People also ask

What is $@ in makefile?

$@ is the name of the target being generated, and $< the first prerequisite (usually a source file). You can find a list of all these special variables in the GNU Make manual.

How can I get PWD in makefile?

You can use shell function: current_dir = $(shell pwd) . Or shell in combination with notdir , if you need not absolute path: current_dir = $(notdir $(shell pwd)) .

How do I link my makefile to .a files?

$(CC) $(CFLAGS) -I$(LIB_PATH) -L$(LIB_PATH) -o $(PROGRAM) main. c -l$(LIB) `pkg-config ...` Basically, you need set the include path to the . h file with -I, then -L for the lib path and -l to set the lib name.

What is Obj in makefile?

The third variable used in this file is OBJS. In this makefile, this variable specifies all the object files required to construct the main program.


3 Answers

How about:

CFLAGS_Linux_x86_release        = -O3
CFLAGS_Linux_x86_debug          = -O0 -g


CFLAGS  = ${CFLAGS_${PLATFORM}_${BUILD}}
like image 151
Martin York Avatar answered Sep 28 '22 15:09

Martin York


Configuring such parameters would be the task of a configure script.

That being said, you can look into the syntax for conditionals and conditional functions. For example, you could try the following:

ifeq ($(PLATFORM)_$(BUILD_TYPE),Linux_x86_release)
    CFLAGS = -O3
endif
ifeq ($(PLATFORM)_$(BUILD_TYPE),Linux_x86_debug)
    CFLAGS = -O0 -g
endif
like image 7
Bruno De Fraine Avatar answered Sep 28 '22 16:09

Bruno De Fraine


The Makefile used by git is a good example of a Makefile which does non-trivial configuration tasks in the Makefile itself (such as switching on the host type). It's actually quite readable and reasonably simple to use.

like image 5
JesperE Avatar answered Sep 28 '22 16:09

JesperE