Makefile中的字典/地图/查找表 [英] Dictionaries/Maps/Lookup Tables in Makefiles
问题描述
我需要在Makefile中创建一个查找表/字典/映射,以查找键值信息.
I need to create a lookup table/dictionary/map in my Makefile to look up key-value information.
我一直在尝试使用ifeq
语句执行相同的操作,但是我的语句似乎失败了:
I have been trying to use ifeq
statements to do the same thing but my statements seem to fail:
# this gets the account id from the current user's ARN, you must have the AWS CLI and jq installed
AWS_ACCOUNT_ID:=$(shell aws iam get-user | jq -r '.User.Arn' | awk -F ':' '{print $$5;}')
# define a friendly account name for output
ifeq ($(AWS_ACCOUNT_ID), 123456)
AWS_ACCOUNT_FRIENDLY:=staging
endif
ifeq ($(AWS_ACCOUNT_ID), 789012)
AWS_ACCOUNT_FRIENDLY:=preprod
endif
ifeq ($(AWS_ACCOUNT_ID), 345678)
AWS_ACCOUNT_FRIENDLY:=production
endif
它似乎只能与第一个值123456
一起使用,而不能与其他值一起使用.
It seems to only work with the first value 123456
but not with others.
是否可以在Make中定义词典/地图,以通过帐户ID的键简单地查找帐户的友好名称?
Is there a way to define a dictionary/map in Make to simply look up the account friendly name by the key of the account id?
推荐答案
我无法解释为什么您看不到您期望的行为:我将验证AWS_ACCOUNT_ID
的值是否符合您的期望:也许您的Shell脚本没有执行您想要的操作.尝试添加类似的内容:
I can't explain why you don't see the behavior you expect: I would verify that the value of AWS_ACCOUNT_ID
is what you expect: maybe your shell script is not doing what you want. Try adding something like:
AWS_ACCOUNT_ID := $(shell ...)
$(info AWS_ACCOUNT_ID = '$(AWS_ACCOUNT_ID)')
看看你能得到什么.
不过,与您的一般性问题有关,我更喜欢使用构造的宏名称处理此类情况时,而不要使用许多ifeq
值:
However related to your more general question, I prefer to use constructed macro names when dealing with situations like this, instead of lots of ifeq
values:
AWS_123456_FRIENDLY := staging
AWS_789012_FRIENDLY := preprod
AWS_345678_FRIENDLY := production
AWS_ACCOUNT_ID := $(shell ...)
AWS_ACCOUNT_FRIENDLY := $(AWS_$(AWS_ACCOUNT_ID)_FRIENDLY)
这篇关于Makefile中的字典/地图/查找表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!