makefileで変数値を動的に変更するにはどうすればよいですか?マクロの値に応じて特定のターゲットを呼び出したい。
たとえば。
STATIC_LIB = TRUE
all: makelib $(var)
makelib:
ifeq (${STATIC_LIB}, TRUE)
var=staticlib
else
var=sharedlib
endif
ここでは、targetの値に応じてstaticlibtargetまたはsharedlibtargetのいずれかを呼び出します。
コード:
.SUFFIXES: .cpp .hpp
# Programs
SHELL = bash
CC = g++
LD = ld
RM = rm
ECHO = /bin/echo
CAT = cat
PRINTF = printf
SED = sed
DOXYGEN = doxygen
STATIC_LIB = TRUE
######################################
# Project Name (generate executable with this name)
TARGET = cs296_exe_28
# Project Paths
PROJECT_ROOT=$(HOME)/Desktop/cs296/cs296_base_code
EXTERNAL_ROOT=$(PROJECT_ROOT)/external
SRCDIR = $(PROJECT_ROOT)/src
OBJDIR = $(PROJECT_ROOT)/myobjs
BINDIR = $(PROJECT_ROOT)/mybins
LIBDIR = $(PROJECT_ROOT)/mylibs
DOCDIR = $(PROJECT_ROOT)/doc
# Library Paths
BOX2D_ROOT=$(EXTERNAL_ROOT)
GLUI_ROOT=/usr
GL_ROOT=/usr
#Libraries
LIBS = -lBox2D -lglui -lglut -lGLU -lGL
# Compiler and Linker flags
CPPFLAGS =-g -O3 -Wall
CPPFLAGS+=-I $(BOX2D_ROOT)/include -I $(GLUI_ROOT)/include
LDFLAGS+=-L $(BOX2D_ROOT)/lib -L $(GLUI_ROOT)/lib
######################################
NO_COLOR=\e[0m
OK_COLOR=\e[1;32m
ERR_COLOR=\e[1;31m
WARN_COLOR=\e[1;33m
MESG_COLOR=\e[1;34m
FILE_COLOR=\e[1;37m
OK_STRING="[OK]"
ERR_STRING="[ERRORS]"
WARN_STRING="[WARNINGS]"
OK_FMT="${OK_COLOR}%30s\n${NO_COLOR}"
ERR_FMT="${ERR_COLOR}%30s\n${NO_COLOR}"
WARN_FMT="${WARN_COLOR}%30s\n${NO_COLOR}"
######################################
SRCS := $(wildcard $(SRCDIR)/*.cpp)
INCS := $(wildcard $(SRCDIR)/*.hpp)
OBJS := $(SRCS:$(SRCDIR)/%.cpp=$(OBJDIR)/%.o)
OBJSNMAIN := $(filter-out $(OBJDIR)/main.o,$(OBJS))
.PHONY: all setup doc clean distclean
ifndef STATIC_LIB
makelib : sharedlib # line 5
else ifeq ( ${STATIC_LIB}, TRUE )
makelib : staticlib # line 7
else
makelib : sharedlib # line 9
endif
all: setup makelib exelib
setup:
@$(ECHO) "Setting up compilation..."
@mkdir -p myobjs
@mkdir -p mybins
@mkdir -p mylibs
$(BINDIR)/$(TARGET): $(OBJS)
@$(PRINTF) "$(MESG_COLOR)Building executable:$(NO_COLOR) $(FILE_COLOR) %16s$(NO_COLOR)" "$(notdir $@)"
@$(CC) -o $@ $(LDFLAGS) $(OBJS) $(LIBS) 2> temp.log || touch temp.err
@if test -e temp.err; \
then $(PRINTF) $(ERR_FMT) $(ERR_STRING) && $(CAT) temp.log; \
elif test -s temp.log; \
then $(PRINTF) $(WARN_FMT) $(WARN_STRING) && $(CAT) temp.log; \
else $(PRINTF) $(OK_FMT) $(OK_STRING); \
fi;
@$(RM) -f temp.log temp.err
-include $(OBJS:.o=.d)
$(OBJS): $(OBJDIR)/%.o : $(SRCDIR)/%.cpp
@$(PRINTF) "$(MESG_COLOR)Compiling: $(NO_COLOR) $(FILE_COLOR) %25s$(NO_COLOR)" "$(notdir $<)"
@$(CC) $(CPPFLAGS) -c $< -o $@ -MD 2> temp.log || touch temp.err
@if test -e temp.err; \
then $(PRINTF) $(ERR_FMT) $(ERR_STRING) && $(CAT) temp.log; \
elif test -s temp.log; \
then $(PRINTF) $(WARN_FMT) $(WARN_STRING) && $(CAT) temp.log; \
else printf "${OK_COLOR}%30s\n${NO_COLOR}" "[OK]"; \
fi;
@$(RM) -f temp.log temp.err
exe: $(BINDIR)/$(TARGET)
makelib:
ifeq (${STATIC_LIB}, TRUE)
LIB_TYPE = staticlib
else
LIB_TYPE = sharedlib
endif
staticlib:
@$(ECHO) "static"
@ar rcs $(LIBDIR)/libCS296.a $(OBJSNMAIN)
sharedlib:
@$(ECHO) "shared"
@$(CC) -shared -o $(LIBDIR)/libCS296.so $(OBJSNMAIN)
exelib:
$(CC) -L $(LIBDIR) $(LDFLAGS) -o cs296_exelib_28 $(OBJDIR)/main.o -lCS296 $(LIBS)
doc:
@$(ECHO) -n "Generating Doxygen Documentation ... "
@$(RM) -rf doc/html
@$(DOXYGEN) $(DOCDIR)/Doxyfile 2 > /dev/null
@$(ECHO) "Done"
clean:
@$(ECHO) -n "Cleaning up..."
@$(RM) -rf $(OBJDIR) *~ $(DEPS) $(SRCDIR)/*~ $(LIBDIR)
@$(ECHO) "Done"
distclean: clean
@$(RM) -rf $(BINDIR) $(DOCDIR)/html