Makefile 中的变量始终评估为 false

问题描述 投票:0回答:1

当 makefile 中存在文件时,我尝试执行脚本。 我尝试了下面的解决方案以及

ifdef DRP_FILE
和其他解决方案,但 ifeq 始终评估为 false(参见输出)。

我觉得我在空格(制表符/空格)上犯了错误,以 make 命令或 shell 命令执行代码 - 但我不知道哪里出了问题。搜索没有发现任何有用的信息。

# Makefile for testing
# Variables
BASE_DIR ?= $(shell pwd)/..
DRP_ROUTE_APP := app
BOARD:=sample
PLAT:=plat

# Targets
.PHONY: test
test: install-config
    @echo "Test passed!"

install-config:
ifneq ($(DRP_ROUTE_APP),)
    $(eval ROOTFS_OVERLAY:=/projects/$(PLAT)/$(BOARD)/rootfs_overlay)
    $(eval DRP_PATH:=$(shell find $(BASE_DIR)$(ROOTFS_OVERLAY)/etc/sfl/cfg/ -regextype posix-extended -regex '.*.txt'))
    $(eval DRP_FILE:=$(if $(DRP_PATH),$(shell basename $(realpath $(DRP_PATH)))))

    # Debug information
    @echo "DRP_PATH: $(DRP_PATH)"
    @echo "DRP_FILE: $(DRP_FILE)"

    ifeq ($(strip $(DRP_FILE)),)
        $(error "drp route file not found")
    else
        $(MAKE) run-drp-setup
    endif
endif

run-drp-setup:
    @echo "$(BASE_DIR)/./drp_setup.sh -f $(DRP_PATH) -s $(DRP_ROUTE_APP)"

输出make -f testmakefile测试

testmakefile:22: *** "drp route file not found".  Stop.

如有任何指点,我们将不胜感激。

variables makefile evaluate
1个回答
0
投票

你不能像这样混合搭配“shell 命令”和“make 命令”。 Make 没有内置 shell。它作为单独的进程运行 shell。而且 shell 处理和 makefile 处理在运行时无法来回通信。

make 调用配方的方式是,首先,扩展整个配方中的所有 make 结构。所有make变量、所有make函数等都首先展开。 然后 一旦完成并且所有值都被扩展,make 将运行一个 shell 并将生成的扩展输出作为一组要运行的命令提供给 shell。然后 make 等待 shell 退出并检查其退出代码。如果它是 0,那么命令成功并且 make 继续。如果退出代码不为 0,则 make 认为配方失败。

所以,你不能在食谱中组合诸如

$(eval ...)
$(error ...)
等内容,因为这些内容总是先扩展。

类似地,所有 make 结构(如

ifeq
等)都是 makefile 中的 preprocessor 语句。它们在读入 makefile 时进行处理,然后 make 决定哪些目标甚至需要重建,更少尝试运行配方。

简而言之,如果您需要做的事情依赖于作为配方一部分运行的 shell 命令,则必须使用 shell 语法编写整个内容。你不能使用 make 函数或 eval 等设置的变量。但在这里似乎不需要这些,它只会让你的 makefile 更加复杂。你为什么不直接使用:

# Makefile for testing
# Variables
BASE_DIR ?= $(shell pwd)/..
DRP_ROUTE_APP := app
BOARD := sample
PLAT := plat

ifneq ($(DRP_ROUTE_APP),)
  ROOTFS_OVERLAY := /projects/$(PLAT)/$(BOARD)/rootfs_overlay
  DRP_PATH := $(shell find $(BASE_DIR)$(ROOTFS_OVERLAY)/etc/sfl/cfg/ -regextype posix-extended -regex '.*.txt')
  DRP_FILE := $(if $(DRP_PATH),$(shell basename $(realpath $(DRP_PATH))))
endif

# Targets
.PHONY: test
test: install-config
        @echo "Test passed!"

install-config:
ifneq ($(DRP_ROUTE_APP),)
        @echo "DRP_PATH: $(DRP_PATH)"
        @echo "DRP_FILE: $(DRP_FILE)"
        if test -z '$(strip $(DRP_FILE))'; then \
            echo "drp route file not found"; exit 1; \
        else \
            $(MAKE) run-drp-setup; \
        fi
endif

run-drp-setup:
         @echo "$(BASE_DIR)/./drp_setup.sh -f $(DRP_PATH) -s $(DRP_ROUTE_APP)"
© www.soinside.com 2019 - 2024. All rights reserved.