首页 > 解决方案 > Makefile:修改目标中的变量

问题描述

我有一个包含文件列表的变量。它们将被传递到我的模拟环境。

VHDL_SOURCES += $(PWD)/common/ip/file1.vhd \
                $(PWD)/common/ip/file2.vhd \
                $(PWD)/common/ip/file3.vhd \
                $(PWD)/common/ip/file4.vhd \

其中一个目标创建了一堆新文件,它们应该被添加到列表中。

# sim target: Add requirement filelist.txt 
.PHONY: sim
sim: add_xilip_sources


# all other definitions are defined in the common makefile
include $(PWD)/scripts/cocotb/common_makefile/Makefile

.PHONY: add_xilip_sources
add_xilip_sources: $(BUILD_PWD)/filelist.txt
    XILIP_FILES = $(shell cat $<) # read the filelist into an variable
    # prepend the new filelist to the VHDL_SOURCES list. 

$(BUILD_PWD)/filelist.txt: $(SCRIPT_PWD)/make_switch_ip.tcl
    vivado -mode tcl -source $(SCRIPT_PWD)/make_switch_ip.tcl

如何修改目标中的变量?

标签: makefilegnu-make

解决方案


你不能。Make 是声明式的,或两步式的;首先指定构建环境,然后执行。您不能在执行时修改构建环境,例如变量,或者至少不能在不同的配方之间修改。

您可以做的是使用文件而不是变量:

# vhdl_sources.txt containing VHDL_SOURCES
# vhdl_sources.txt.full containing VHDL_SOURCES plus generated filelist.txt content
vhdl_sources.txt.full: $(BUILD_PWD)/filelist.txt vhdl_sources.txt
    cat $^ > $@

推荐阅读