Tuesday, 27 August 2013

Target sensitive variables in make

Target sensitive variables in make

What is the best way to have some make variables be target sensitive in make?
I have a set of c files and depending the overall make target sometimes
they need to be build with g++ for other targets at other times they will
need to be built with gcc. In both cases the individual object file target
has the same name (thing.o)
The invocations of make are recursive with the appropriate $CC variable
being passed down...
I have a solution but it feels ugly and not the right way do to it so I'm
looking for something better
Thanks in advance
My "solution" stripped down:
# analyze the environment to choose a cc for c targets and another for
gtest targets
TARGET_ARCH_OVR32 = m32
TARGET_ARCH_OVR64 = m64
TARGET_ARCH_OVRRD = none
CC_prod_on64 = /usr/bin/gcc
CC_prod_on32 = gcc4
CC_gtest_on64 = /usr/bin/g++
CC_gtest_on32 = g++
PROC = $(shell uname -p)
TARGET_MACHN_ARCH := $(if $(findstring
64,$(PROC)),PLATFORM_64BIT,PLATFORM_32BIT)
CC_xrpc := $(if $(findstring
64,$(PROC)),$(CC_prod_on64),$(CC_prod_on32))
CC_gtest := $(if $(findstring
64,$(PROC)),$(CC_gtest_on64),$(CC_gtest_on32))
# this is the list of directores containing c files for the g++ build
# it is a superset of the PRODDIRS
TESTSUBDIRS = $(CHIPROOT)/open_source/google_test \
general general/testing \
$(CHIPROOT)/drv $(CHIPROOT)/drv/testing \
..
..
$ this is the list of directories containing C files for the gcc build
PRODDIRS = general \
$(CHIPROOT)/drv \
$(CHIPROOT)/dev \
..
..
CCx := $(if $(filter gtest,$(MAKECMDGOALS)),$(CC_gtest),$(CC_xrpc))
BUILDDIRx := $(if $(filter gtest,\
$(MAKECMDGOALS)),$(GTESTRESULTDIR),$(PRODTARGETDIR)/$(OSTYPE))
DIRSx := $(if $(filter gtest,$(MAKECMDGOALS)),$(TESTSUBDIRS),$(PRODDIRS))
TEST_MODE := $(if $(filter gtest,$(MAKECMDGOALS)),GTEST,NOUNIT_TEST)
DIRS = $(DIRSx)
BUILDDIR = $(BUILDDIRx)
CC = $(CCx)
objdir: $(CSPLATFORMDIR)/makebuilddir
$(CSPLATFORMDIR)/makebuilddir $(BUILDDIR)
# this is the target that actaully goes and compiles all the c files
$(DIRS): objdir
$(MAKE) CC=$(CCx) BUILDDIR=$(BUILDDIRx) -e -C $@
# this is one of the overall targets (it needs g++)
gtest: $(CSPLATFORMDIR)/makebuilddir $(DIRS) $(CHIPROOT)/irq
$(CHIPROOT)/irq/testing
$(CSPLATFORMDIR)/makebuilddir $(GTESTRESULTDIR)
g++ $(OBJS) -lpthread $(GTESTLIBDIR)/libgtest_main.a
$(GTESTLIBDIR)/libgtest.a -o\
$(GTESTRESULTDIR)/gtest
cp $(GTESTRESULTDIR)/gtest .
# this is another overall target (it needs gcc)
prod.a: $(CSPLATFORMDIR)/makebuilddir $(DIRS)
$(CSPLATFORMDIR)/makebuilddir $(BUILDDIR)
$(AR) -r $(TARGET) $(OBJS)
ls -l $(TARGET)

No comments:

Post a Comment