gpt4 book ai didi

makefile - 以下 Makefile 目标的 DRY 版本是什么?

转载 作者:行者123 更新时间:2023-12-02 01:22:17 25 4
gpt4 key购买 nike

我不知道如何执行存储为变量的命令,也不知道如何在目标内部使用 ifeq,所以我在时刻!

理想情况下,我希望只有一个目标(all),它可以在 Mac 上运行存储的命令,并在 Linux 上运行两次,一次使用 -m32,一次使用 -m64。

all:
echo PLEASE SELECT OS, e.g. make linux
exit 1

mac:
gcc $(SHARED_OPT) $(GENERAL_CFLAGS) $(PLATFORM_CFLAGS) -o $(BUILD_DIR)$(BUILD_NAME) $(SOURCE) $(LIBRARIES)

linux:
gcc $(SHARED_OPT) $(GENERAL_CFLAGS) $(PLATFORM_CFLAGS) -o $(BUILD_DIR)$(BUILD_NAME64) $(SOURCE) $(LIBRARIES64) -m64
gcc $(SHARED_OPT) $(GENERAL_CFLAGS) $(PLATFORM_CFLAGS) -o $(BUILD_DIR)$(BUILD_NAME) $(SOURCE) $(LIBRARIES) -m32

更新:这就是我在阅读了各种建议后得到的结果。 (是的,我知道我应该使用 autoconf...)感谢大家的帮助!

ifeq($(PLATFORM), Linux)
COMMON = -pthread -fPIC
PLATFORM_CFLAGS = $(COMMON) -m32
PLATFORM_CFLAGS64 = $(COMMON) -m64
endif

ifeq ($(PLATFORM), Darwin)
PLATFORM_CFLAGS = -arch i386 -arch ppc -arch x86_64 -mmacosx-version-min=10.5 -isysroot /Developer/SDKs/MacOSX10.5.sdk
endif

all: $(PLATFORM)_all

Darwin_all:
mkdir -p ../../../../tmp
gcc $(SHARED_OPT) $(GENERAL_CFLAGS) $(PLATFORM_CFLAGS) -o $(BUILD_DIR)$(BUILD_NAME) $(SOURCE) $(LIBRARIES)

Linux_all: Darwin_all
gcc $(SHARED_OPT) $(GENERAL_CFLAGS) $(PLATFORM_CFLAGS64) -o $(BUILD_DIR)$(BUILD_NAME64) $(SOURCE) $(LIBRARIES64)

最佳答案

您让宏完成大部分工作,请注意您应该使用 $(CC) 而不是 gcc。

BUILD_COMMAND = $(CC) $(SHARED_OPT) $(GENERAL_CFLAGS) $(PLATFORM_CFLAGS)
BUILD_NAME32 = $(BUILD_NAME)
TARGET_32 = $(BUILD_DIR)$(BUILD_NAME32)
TARGET_64 = $(BUILD_DIR)$(BUILD_NAME64)
LIBS_32 = $(LIBRARIES)
LIBS_64 = $(LIBRARIES64)
OPTS_32 = -m32
OPTS_64 = -m64

# We could do some fancy stuff here...
# Except that we will remove the commands momentarily
all:
echo PLEASE SELECT OS, e.g. make linux
exit 1

# Note that without a qualifier
# - MacOS X 10.5.x will build 32-bit
# - MacOS X 10.6.x will build 64-bit
# But why not build both anyway?
mac:
$(BUILD_COMMAND) -o $(TARGET_64) $(SOURCE) $(LIBS_64) $(OPTS_64)
$(BUILD_COMMAND) -o $(TARGET_32) $(SOURCE) $(LIBS_32) $(OPTS_32)

linux:
$(BUILD_COMMAND) -o $(TARGET_64) $(SOURCE) $(LIBS_64) $(OPTS_64)
$(BUILD_COMMAND) -o $(TARGET_32) $(SOURCE) $(LIBS_32) $(OPTS_32)

哦,看,现在 Linux 和 MacOS X 的命令是相同的......所以我们可以这样做:

BUILD_COMMAND = $(CC) $(SHARED_OPT) $(GENERAL_CFLAGS) $(PLATFORM_CFLAGS)
BUILD_NAME32 = $(BUILD_NAME)
TARGET_32 = $(BUILD_DIR)$(BUILD_NAME32)
TARGET_64 = $(BUILD_DIR)$(BUILD_NAME64)
LIBS_32 = $(LIBRARIES)
LIBS_64 = $(LIBRARIES64)
OPTS_32 = -m32
OPTS_64 = -m64

all:
$(BUILD_COMMAND) -o $(TARGET_64) $(SOURCE) $(LIBS_64) $(OPTS_64)
$(BUILD_COMMAND) -o $(TARGET_32) $(SOURCE) $(LIBS_32) $(OPTS_32)

天哪,像我通常在 makefile 中那样编写 $(XXX) 而不是 ${XXX} 是一项艰苦的工作。

基本上,我们应用 DRY(不要重复自己),使名称变得无聊系统化。 Makefile 不应该令人兴奋。

如果您仍然希望平台之间存在差异,那么您可以按照 Ivan Andrus 建议的方式进行操作。 GNU Make 允许您评估 shell 命令,因此:

BUILD_COMMAND = $(CC) $(SHARED_OPT) $(GENERAL_CFLAGS) $(PLATFORM_CFLAGS)
BUILD_NAME32 = $(BUILD_NAME)
TARGET_32 = $(BUILD_DIR)$(BUILD_NAME32)
TARGET_64 = $(BUILD_DIR)$(BUILD_NAME64)
LIBS_32 = $(LIBRARIES)
LIBS_64 = $(LIBRARIES64)
OPTS_32 = -m32
OPTS_64 = -m64

all: $(shell uname)

Linux:
$(BUILD_COMMAND) -o $(TARGET_64) $(SOURCE) $(LIBS_64) $(OPTS_64)
$(BUILD_COMMAND) -o $(TARGET_32) $(SOURCE) $(LIBS_32) $(OPTS_32)

Darwin:
$(BUILD_COMMAND) -o $(TARGET_32) $(SOURCE) $(LIBS_32)

如果您觉得不能依赖 GNU Make,那么:

BUILD_COMMAND = $(CC) $(SHARED_OPT) $(GENERAL_CFLAGS) $(PLATFORM_CFLAGS)
BUILD_NAME32 = $(BUILD_NAME)
TARGET_32 = $(BUILD_DIR)$(BUILD_NAME32)
TARGET_64 = $(BUILD_DIR)$(BUILD_NAME64)
LIBS_32 = $(LIBRARIES)
LIBS_64 = $(LIBRARIES64)
OPTS_32 = -m32
OPTS_64 = -m64
BUILD_32 = use_32_bit
BUILD_64 = use_64_bit
BUILD_TYPE = $(BUILD_32) $(BUILD_64)

.PHONEY: $(BUILD_32) $(BUILD_64)

all: $(BUILD_TYPE)

use_64_bit:
$(BUILD_COMMAND) -o $(TARGET_64) $(SOURCE) $(LIBS_64) $(OPTS_64)

use_32_bit:
$(BUILD_COMMAND) -o $(TARGET_32) $(SOURCE) $(LIBS_32) $(OPTS_32)

默认情况下,这将编译 32 位和 64 位版本。如果您只需要 32 位或仅 64 位,请运行以下两个中适当的一个:

make BUILD_TYPE=use_32_bit
make BUILD_TYPE=use_64_bit

关于makefile - 以下 Makefile 目标的 DRY 版本是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2572551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com