开发者

Makefile two compilers issue

开发者 https://www.devze.com 2023-03-05 02:06 出处:网络
I am asked to write a Makefile which needs to selects between two compilers, and each of these compilers should support 3 build versions (debug, release, test).

I am asked to write a Makefile which needs to selects between two compilers, and each of these compilers should support 3 build versions (debug, release, test).

There are a lot of variables that change based on input (compiler, compiler options, output directory, include directories etc). My first option was to go through target-specific variables and configure variables according to target. Do you think this is good idea?

I am not extremely famil开发者_StackOverflow社区iar with those kind of variables. It seems to me that if I do something like this:

release: variable1=value1   #release is target
release: variable2=value2

release: 
      # some compilation rule

Only the variable1 will be configured. Am I right about this?


Update

Thank you for your reply. I am trying to deal with compiler selection issue through additional variable which would be configured according to target. But, here is the problem. I have the following lines:

release: CFLAGS = -DCORE_SW_VERSION='"$(CORE_SW_VERSION)"' -Wall 
release: CFLAGS += -fgnu89-inline -mno-volatile-cache $(INCLUDE)  
release: TARGET=release

After this lines, I do some ifeq sequence in which I decide which compiler to use (according to TARGET variable value). And CFLAGS is configured properly, but the TARGET variable is empty. This leads me to conclusion that you can configure only one target-specific variable. Am I right? If not, I am not aware what I am doing wrong. Could you please help me?


Target-specific variables are defined only when building that target and any prerequisites of that target. You can't use target-specific variables arbitrarily throughout the makefile (as it sounds like you're trying to do with ifeq). For that, you may want to look at $(MAKECMDGOALS). I don't believe there is any limit on the number of target-specific variables, certainly not a limit of one.

Needing either target-specific variables or $(MAKECMDGOALS) may be a warning that you're trying to do coerce make into doing something it wasn't meant to do.

It's not clear to me whether you want to build three versions (debug/test/release with a single compiler for each one), or six versions. Assuming three, here is a unix-y Makefile to build with different compilers and CFLAGS depending on the target. However, note that this could just as easily be coded with RELEASE_CFLAGS, RELEASE_CC, DEBUG_CFLAGS, etc... variables.

all: release debug test

release: CC=gcc
release: CFLAGS=
debug: CC=gcc
debug: CFLAGS=-g
test: CC=cc
test: CFLAGS=-Wall

.PHONY: release debug test
release: release/exe
debug: debug/exe
test: test/exe

OBJECTS := test.o

release/%.o: %.c
        $(CC) $(CLFAGS) -c -o $@ $<

debug/%.o: %.c
        $(CC) $(CLFAGS) -c -o $@ $<

test/%.o: %.c
        $(CC) $(CLFAGS) -c -o $@ $<

release/exe: $(OBJECTS:%=release/%)
        $(CC) $(CFLAGS) -o $@ $^

debug/exe: $(OBJECTS:%=debug/%)
        $(CC) $(CFLAGS) -o $@ $^

test/exe: $(OBJECTS:%=test/%)
        $(CC) $(CFLAGS) -o $@ $^
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号