我想使用相同的make文件生成多个构建。我指的是几个版本windows-release
,windows-debug
,linux-release
,linux-debug
等等。我的想法是根据我正在运行的构建设置变量,如下所示:
windows-release:
CC:=...
CFLAGS:=...
OUTDIR:=windows/release
OBJDIR:=$(OUTDIR)/obj
EXECUTABLE:=$(OUTDIR)/exe
# Would call $(EXECUTABLE) here, but it's not procedural
然后制定实际构建的规则:
# This will generate a static library
$(EXECUTABLE): $(OBJECTS)
$(AR) $(ARFLAGS) $(ARLIB) $(OBJECTS)
$(OBJDIR)/%.o: src/%.cpp
$(CC) $(CFLAGS) $< -o $@
我很抱歉这个不完整的例子,但我还没有makefile,我现在只是写它。
我可以使用makefile执行此操作吗?我更喜欢使用脚本吗?
答案 0 :(得分:2)
你绝对可以这样做。你想要的三件事是.PHONY
targets(所以make并不认为windows-release
等是真实的文件),Target-specific Variable Values(所以你可以根据目标是不同的设置变量运行)和Secondary Expansion因为否则对先决条件的评估发生在特定于目标的上下文之外,因此特定于目标的变量不起作用。
哪种组合可以得到这样的东西:
windows-release: CC:=...
windows-release: CFLAGS:=...
windows-release: OUTDIR:=windows/release
windows-release: OBJDIR:=$(OUTDIR)/obj
windows-release: EXECUTABLE:=$(OUTDIR)/exe
windows-debug: CC:=...
windows-debug: CFLAGS:=...
windows-debug: OUTDIR:=windows/debug
windows-debug: OBJDIR:=$(OUTDIR)/obj
windows-debug: EXECUTABLE:=$(OUTDIR)/exe
linux-release: CC:=...
linux-release: CFLAGS:=...
linux-release: OUTDIR:=linux/release
linux-release: OBJDIR:=$(OUTDIR)/obj
linux-release: EXECUTABLE:=$(OUTDIR)/bin
linux-debug: CC:=...
linux-debug: CFLAGS:=...
linux-debug: OUTDIR:=linux/debug
linux-debug: OBJDIR:=$(OUTDIR)/obj
linux-debug: EXECUTABLE:=$(OUTDIR)/bin
.SECONDEXPANSION:
windows-release windows-debug linux-release linux-debug: $$(EXECUTABLE)
话虽如此,你也可以使用普通的全局变量和条件来代替:
ifeq (windows-release,$(filter windows-release,$(MAKECMDGOALS)))
CC:=...
CFLAGS:=...
OUTDIR:=windows/release
OBJDIR:=$(OUTDIR)/obj
EXECUTABLE:=$(OUTDIR)/exe
else ifeq (windows-debug,$(filter windows-debug,$(MAKECMDGOALS)))
CC:=...
CFLAGS:=...
OUTDIR:=windows/debug
OBJDIR:=$(OUTDIR)/obj
EXECUTABLE:=$(OUTDIR)/exe
else ifeq (linux-release,$(filter linux-release,$(MAKECMDGOALS)))
CC:=...
CFLAGS:=...
OUTDIR:=linux/release
OBJDIR:=$(OUTDIR)/obj
EXECUTABLE:=$(OUTDIR)/bin
else ifeq (linux-debug,$(filter linux-debug,$(MAKECMDGOALS)))
CC:=...
CFLAGS:=...
OUTDIR:=linux/debug
OBJDIR:=$(OUTDIR)/obj
EXECUTABLE:=$(OUTDIR)/bin
endif
windows-release windows-debug linux-release linux-debug: $(EXECUTABLE)