Makefile 可以只执行一次配置吗?

Makefile can I execute a configuration only once?

我正在尝试创建一个编译一些文件并创建一些输出的 Makefile,但首先我希望它只执行一次配置,下次我键入 make 时它不会重新执行配置,除非我更改参数例如前缀。

我尝试使用 touch、FORCE 和 if,在其他帖子中搜索了一下,但我是 gcc 和 Makefiles 的新手,所以我不能让它工作。

我现在的代码是(不包括其他规则,因为它们不影响配置):

XLEN := 32
RISCV_PREFIX := riscv$(XLEN)-unknown-elf-
RISCV_GCC := $(RISCV_PREFIX)gcc
CFLAGS := -O2
WORKING_DIR:= $(shell pwd)
LIBRARY_DIR:= $(abspath $(dir $(lastword $(MAKEFILE_LIST)))/..)
B := $(shell echo $(LIBRARY_DIR))
$(info $(B))

--->(只执行一次)---> CONFIGURATION := configure --prefix=$(LIBRARY_DIR) --with-arch=rv32if --with-abi=ilp32d

RISCV_TEST_DIR:=$(shell pwd)
SCRIPTDIR:=$(RISCV_TEST_DIR)/../../tools

RISCV_OPTIONS = -o  
RISCV_LINK = $(RISCV_GCC) $(PROGRAMS) $(RISCV_OPTIONS) $@ $(CFLAGS) #produces .elf file!
RISCV_OBJDUMP = $(RISCV_PREFIX)objdump -D                           #produces a dump file to see the assembly code!
RISCV_OBJCOPY = $(RISCV_PREFIX)objcopy -O binary                    #produces a bin file!

%.elf: %.c
    $(info Generating .elf file from files: $(PROGRAMS_NO_EX))
    $(RISCV_LINK)
    $(info Success!)
    $(info ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~)
%.dump: %.elf
    $(info Copying assembly to dump file $(PROGRAMS_NO_EX).dump)
    @$(RISCV_OBJDUMP) $< > $@
    $(info Success!)
    $(info ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~)
%.bin: %.elf
    $(info Generating bin file)
    @$(RISCV_OBJCOPY) $< $@
    $(info Success!)
    $(info ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~)
%.hex: %.bin 
    $(info Generating hex file)
    echo cd $(SCRIPTDIR)
    $(info Running binary to hex >>>)
    python $(SCRIPTDIR)/bin2hex.py $< -a 0x0 > $@ || exit -1
    $(info Hex Generation Successful!)
    $(info ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~)

all: $(PROGRAMS_TO_CREATE); if [ -a $(LIBRARY_DIR)/config.status ]; then cd $(LIBRARY_DIR) && $(CONFIGURATION); fi;

configure: config.status
    touch configure

config.status:
    cd $(LIBRARY_DIR) && $(CONFIGURATION);

.PHONY: all clean
clean:
    $(info Cleaning files...)
    @rm -rf *.elf *.hex *.map *.objdump *.i *.s *.bin *.dump
    $(info Done cleaning!)

提前致谢!

我认为您的配置语句的唯一问题是 config.statusconfigure 规则中的路径不正确,因为它们确实位于 $(LIBRARY_DIR) 中。当路径正确时,它只会正确 运行 配置一次:

$ cat Makefile
LIBRARY_DIR := library

all: $(LIBRARY_DIR)/config.status
        $(info Making $@)

$(LIBRARY_DIR)/config.status: $(LIBRARY_DIR)/configure
        cd $(<D) && ./$(<F)

输出:

$ make
cd library && ./configure
Making all

$ make            # <--- Invoking second time, no configure step
Making all
make: 'all' is up to date.

在@raspy 的帮助下设法解决了这样的问题:

all: $(PROGRAMS_TO_CREATE)
$(PROGRAMS_TO_CREATE):$(LIBRARY_DIR)/config.status

$(LIBRARY_DIR)/config.status: 
    cd $(LIBRARY_DIR) && $(CONFIGURATION)