最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Makefile中如何获得define自定义函数的返回值

makefile crifan 5645浏览 0评论

折腾:

【已解决】Makefile中实现自己的函数并调用

期间,需要搞清楚makefile中如何获得define的自定义函数的返回值。

makefile define function return value

shell – How can I pass and return a value from user defined function in MAKEFILE? – Stack Overflow

makefile – How to check return value from the shell directive – Stack Overflow

gnu make – Makefile call function. How to get all arguments – Stack Overflow

然后用:

<code>define getCurrentDirAndDirName
$(eval MAKEFILE_LIST_LASTWORD = $(lastword $(MAKEFILE_LIST)))
# MAKEFILE_LIST_LASTWORD = $(lastword $(MAKEFILE_LIST))
$(info MAKEFILE_LIST_LASTWORD=$(MAKEFILE_LIST_LASTWORD))
# MAKEFILE_PATH := $(abspath $(MAKEFILE_LIST_LASTWORD))
$(eval MAKEFILE_LIST_FIRSTWORD = $(firstword $(MAKEFILE_LIST)))
$(info MAKEFILE_LIST_FIRSTWORD=$(MAKEFILE_LIST_FIRSTWORD))
$(eval MAKEFILE_PATH := $(abspath $(MAKEFILE_LIST_FIRSTWORD)))
$(info MAKEFILE_PATH=$(MAKEFILE_PATH))
$(eval MAKEFILE_DIR := $(dir $(MAKEFILE_PATH)))
$(info MAKEFILE_DIR=$(MAKEFILE_DIR))
$(eval MAKEFILE_DIR_PATSUBST := $(patsubst %/,%,$(MAKEFILE_DIR)))
$(info MAKEFILE_DIR_PATSUBST=$(MAKEFILE_DIR_PATSUBST))
$(eval MAKEFILE_DIR_NOSLASH = $(MAKEFILE_DIR_PATSUBST))
$(info MAKEFILE_DIR_NOSLASH=$(MAKEFILE_DIR_NOSLASH))
$(eval CURRENT_DIR_WITH_SLASH = $(MAKEFILE_DIR))
$(info CURRENT_DIR_WITH_SLASH=$(CURRENT_DIR_WITH_SLASH))
$(eval CURRENT_DIR = $(MAKEFILE_DIR_NOSLASH))
$(info CURRENT_DIR=$(CURRENT_DIR))

$(info before para 1=$1)
$1 = $(CURRENT_DIR)
$(info after para 1=$1)

$(eval CURRENT_DIR_NAME := $(notdir $(MAKEFILE_DIR_PATSUBST)))
$(info CURRENT_DIR_NAME=$(CURRENT_DIR_NAME))

$(info before para 2=$2)
$2 = $(CURRENT_DIR_NAME)
$(info after para 2=$2)
endef

</code>

debug_makefile:

  $(eval $(call getCurrentDirAndDirName, CURRENT_DIR, CURRENT_DIR_NAME))

  $(info after define function call: CURRENT_DIR=$(CURRENT_DIR), CURRENT_DIR_NAME=$(CURRENT_DIR_NAME))

输出:

<code>➜  youdao_note_summary git:(master) ✗ make
MAKEFILE_LIST_LASTWORD=../Makefile_common
MAKEFILE_LIST_FIRSTWORD=Makefile
MAKEFILE_PATH=/Users/crifan/GitBook/Library/Import/youdao_note_summary/Makefile
MAKEFILE_DIR=/Users/crifan/GitBook/Library/Import/youdao_note_summary/
MAKEFILE_DIR_PATSUBST=/Users/crifan/GitBook/Library/Import/youdao_note_summary
MAKEFILE_DIR_NOSLASH=/Users/crifan/GitBook/Library/Import/youdao_note_summary
CURRENT_DIR_WITH_SLASH=/Users/crifan/GitBook/Library/Import/youdao_note_summary/
CURRENT_DIR=/Users/crifan/GitBook/Library/Import/youdao_note_summary
before para 1= CURRENT_DIR
after para 1= CURRENT_DIR
CURRENT_DIR_NAME=youdao_note_summary
before para 2= CURRENT_DIR_NAME
after para 2= CURRENT_DIR_NAME
after define function call: CURRENT_DIR=/Users/crifan/GitBook/Library/Import/youdao_note_summary, CURRENT_DIR_NAME=youdao_note_summary
</code>

再去试试:

$(1)

<code>
define getCurrentDirAndDirName
$(eval MAKEFILE_LIST_LASTWORD = $(lastword $(MAKEFILE_LIST)))
# MAKEFILE_LIST_LASTWORD = $(lastword $(MAKEFILE_LIST))
$(info MAKEFILE_LIST_LASTWORD=$(MAKEFILE_LIST_LASTWORD))
# MAKEFILE_PATH := $(abspath $(MAKEFILE_LIST_LASTWORD))
$(eval MAKEFILE_LIST_FIRSTWORD = $(firstword $(MAKEFILE_LIST)))
$(info MAKEFILE_LIST_FIRSTWORD=$(MAKEFILE_LIST_FIRSTWORD))
$(eval MAKEFILE_PATH := $(abspath $(MAKEFILE_LIST_FIRSTWORD)))
$(info MAKEFILE_PATH=$(MAKEFILE_PATH))
$(eval MAKEFILE_DIR := $(dir $(MAKEFILE_PATH)))
$(info MAKEFILE_DIR=$(MAKEFILE_DIR))
$(eval MAKEFILE_DIR_PATSUBST := $(patsubst %/,%,$(MAKEFILE_DIR)))
$(info MAKEFILE_DIR_PATSUBST=$(MAKEFILE_DIR_PATSUBST))
$(eval MAKEFILE_DIR_NOSLASH = $(MAKEFILE_DIR_PATSUBST))
$(info MAKEFILE_DIR_NOSLASH=$(MAKEFILE_DIR_NOSLASH))
$(eval CURRENT_DIR_WITH_SLASH = $(MAKEFILE_DIR))
$(info CURRENT_DIR_WITH_SLASH=$(CURRENT_DIR_WITH_SLASH))
$(eval CURRENT_DIR = $(MAKEFILE_DIR_NOSLASH))
$(info CURRENT_DIR=$(CURRENT_DIR))

$(info before para 1=$(1))
$(1) = $(CURRENT_DIR)
$(info after para 1=$(1))

$(eval CURRENT_DIR_NAME := $(notdir $(MAKEFILE_DIR_PATSUBST)))
$(info CURRENT_DIR_NAME=$(CURRENT_DIR_NAME))

$(info before para 2=$(2))
$(2) = $(CURRENT_DIR_NAME)
$(info after para 2=$(2))
endef

</code>

debug_makefile:

  $(eval $(call getCurrentDirAndDirName, CURRENT_DIR, CURRENT_DIR_NAME))

  $(info after define function call: CURRENT_DIR=$(CURRENT_DIR), CURRENT_DIR_NAME=$(CURRENT_DIR_NAME))

结果:

和前面一样。

然后才注意到:

实际上此处的$1或$(1),根本没有实现变量传递,而是:

define函数内部的eval实现了变量:CURRENT_DIR和CURRENT_DIR_NAME的值的传递

<code>define getCurrentDirAndDirName
$(eval MAKEFILE_LIST_LASTWORD = $(lastword $(MAKEFILE_LIST)))
# MAKEFILE_LIST_LASTWORD = $(lastword $(MAKEFILE_LIST))
$(info MAKEFILE_LIST_LASTWORD=$(MAKEFILE_LIST_LASTWORD))
# MAKEFILE_PATH := $(abspath $(MAKEFILE_LIST_LASTWORD))
$(eval MAKEFILE_LIST_FIRSTWORD = $(firstword $(MAKEFILE_LIST)))
$(info MAKEFILE_LIST_FIRSTWORD=$(MAKEFILE_LIST_FIRSTWORD))
$(eval MAKEFILE_PATH := $(abspath $(MAKEFILE_LIST_FIRSTWORD)))
$(info MAKEFILE_PATH=$(MAKEFILE_PATH))
$(eval MAKEFILE_DIR := $(dir $(MAKEFILE_PATH)))
$(info MAKEFILE_DIR=$(MAKEFILE_DIR))
$(eval MAKEFILE_DIR_PATSUBST := $(patsubst %/,%,$(MAKEFILE_DIR)))
$(info MAKEFILE_DIR_PATSUBST=$(MAKEFILE_DIR_PATSUBST))
$(eval MAKEFILE_DIR_NOSLASH = $(MAKEFILE_DIR_PATSUBST))
$(info MAKEFILE_DIR_NOSLASH=$(MAKEFILE_DIR_NOSLASH))
$(eval CURRENT_DIR_WITH_SLASH = $(MAKEFILE_DIR))
$(info CURRENT_DIR_WITH_SLASH=$(CURRENT_DIR_WITH_SLASH))

$(eval CURRENT_DIR = $(MAKEFILE_DIR_NOSLASH))
$(info CURRENT_DIR=$(CURRENT_DIR))

$(eval CURRENT_DIR_NAME := $(notdir $(MAKEFILE_DIR_PATSUBST)))
$(info CURRENT_DIR_NAME=$(CURRENT_DIR_NAME))
endef

</code>

debug_makefile:

  $(eval $(call getCurrentDirAndDirName))

  $(info after define function call: CURRENT_DIR=$(CURRENT_DIR), CURRENT_DIR_NAME=$(CURRENT_DIR_NAME))

结果:

<code>➜  youdao_note_summary git:(master) ✗ make
MAKEFILE_LIST_LASTWORD=../Makefile_common
MAKEFILE_LIST_FIRSTWORD=Makefile
MAKEFILE_PATH=/Users/crifan/GitBook/Library/Import/youdao_note_summary/Makefile
MAKEFILE_DIR=/Users/crifan/GitBook/Library/Import/youdao_note_summary/
MAKEFILE_DIR_PATSUBST=/Users/crifan/GitBook/Library/Import/youdao_note_summary
MAKEFILE_DIR_NOSLASH=/Users/crifan/GitBook/Library/Import/youdao_note_summary
CURRENT_DIR_WITH_SLASH=/Users/crifan/GitBook/Library/Import/youdao_note_summary/
CURRENT_DIR=/Users/crifan/GitBook/Library/Import/youdao_note_summary
CURRENT_DIR_NAME=youdao_note_summary
after define function call: CURRENT_DIR=/Users/crifan/GitBook/Library/Import/youdao_note_summary, CURRENT_DIR_NAME=youdao_note_summary
make: Nothing to be done for `debug_makefile'.
</code>

makefile get custom function return value

shell – How can I pass and return a value from user defined function in MAKEFILE? – Stack Overflow

【总结】

最后是通过:

<code>define functioinName
...
$1 = $$(someVariable)
endef
$(eval $(call functioinName, return_para_1))

</code>

$(info after call function: return_para_1=$(return_para_1))

可以获得函数内部传递出来的值给return_para_1

其中:

$1 == $(1)

带括号,不带括号都可以。

比如:

<code>define getCurrentDirAndDirName
$(eval MAKEFILE_LIST_LASTWORD = $(lastword $(MAKEFILE_LIST)))
# MAKEFILE_LIST_LASTWORD = $(lastword $(MAKEFILE_LIST))
$(info MAKEFILE_LIST_LASTWORD=$(MAKEFILE_LIST_LASTWORD))
# MAKEFILE_PATH := $(abspath $(MAKEFILE_LIST_LASTWORD))
$(eval MAKEFILE_LIST_FIRSTWORD = $(firstword $(MAKEFILE_LIST)))
$(info MAKEFILE_LIST_FIRSTWORD=$(MAKEFILE_LIST_FIRSTWORD))
$(eval MAKEFILE_PATH := $(abspath $(MAKEFILE_LIST_FIRSTWORD)))
$(info MAKEFILE_PATH=$(MAKEFILE_PATH))
$(eval MAKEFILE_DIR := $(dir $(MAKEFILE_PATH)))
$(info MAKEFILE_DIR=$(MAKEFILE_DIR))
$(eval MAKEFILE_DIR_PATSUBST := $(patsubst %/,%,$(MAKEFILE_DIR)))
$(info MAKEFILE_DIR_PATSUBST=$(MAKEFILE_DIR_PATSUBST))
$(eval MAKEFILE_DIR_NOSLASH = $(MAKEFILE_DIR_PATSUBST))
$(info MAKEFILE_DIR_NOSLASH=$(MAKEFILE_DIR_NOSLASH))
$(eval CURRENT_DIR_WITH_SLASH = $(MAKEFILE_DIR))
$(info CURRENT_DIR_WITH_SLASH=$(CURRENT_DIR_WITH_SLASH))

$(eval CURRENT_DIR = $(MAKEFILE_DIR_NOSLASH))
$(info CURRENT_DIR=$(CURRENT_DIR))

$(eval CURRENT_DIR_NAME := $(notdir $(MAKEFILE_DIR_PATSUBST)))
$(info CURRENT_DIR_NAME=$(CURRENT_DIR_NAME))

$(info RETURN_CURRENT_DIR=$(RETURN_CURRENT_DIR))
$(1) := $$(CURRENT_DIR_NAME)
endef

</code>

debug_makefile:

  $(eval $(call getCurrentDirAndDirName, RETURN_CURRENT_DIR))

  $(info after define function call:)

  $(info RETURN_CURRENT_DIR=$(RETURN_CURRENT_DIR))

可以输出:

<code>➜  youdao_note_summary git:(master) ✗ make
CURRENT_DIR=
CURRENT_DIR_NAME=
MAKEFILE_LIST_LASTWORD=../Makefile_common
MAKEFILE_LIST_FIRSTWORD=Makefile
MAKEFILE_PATH=/Users/crifan/GitBook/Library/Import/youdao_note_summary/Makefile
MAKEFILE_DIR=/Users/crifan/GitBook/Library/Import/youdao_note_summary/
MAKEFILE_DIR_PATSUBST=/Users/crifan/GitBook/Library/Import/youdao_note_summary
MAKEFILE_DIR_NOSLASH=/Users/crifan/GitBook/Library/Import/youdao_note_summary
CURRENT_DIR_WITH_SLASH=/Users/crifan/GitBook/Library/Import/youdao_note_summary/
CURRENT_DIR=/Users/crifan/GitBook/Library/Import/youdao_note_summary
CURRENT_DIR_NAME=youdao_note_summary
RETURN_CURRENT_DIR=
after define function call:
RETURN_CURRENT_DIR=youdao_note_summary
make: Nothing to be done for `debug_makefile'.
</code>

当然,其实此处,由于define中已经eval去赋值了,所以也可以同时获得想要的:

CURRENT_DIR_NAME和CURRENT_DIR

<code>debug_makefile:
  $(eval $(call getCurrentDirAndDirName, RETURN_CURRENT_DIR))
  $(info after define function call:)
  $(info RETURN_CURRENT_DIR=$(RETURN_CURRENT_DIR))
  $(info CURRENT_DIR=$(CURRENT_DIR))
  $(info CURRENT_DIR_NAME=$(CURRENT_DIR_NAME))
</code>

同样可以输出要的值:

<code>after define function call:
RETURN_CURRENT_DIR=youdao_note_summary
CURRENT_DIR=/Users/crifan/GitBook/Library/Import/youdao_note_summary
CURRENT_DIR_NAME=youdao_note_summary
</code>

转载请注明:在路上 » 【已解决】Makefile中如何获得define自定义函数的返回值

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.199 seconds, using 22.17MB memory