英文:
how to assign a value from a yaml file to a variable inside makefile
问题
I have a config file called conf.yaml
inside a Makefile I am using yq to parse the conf file and assign values. Here is the makefile:
define extract_from_yaml
$(shell docker run -v ${PWD}:/workdir --rm -it mikefarah/yq ."" conf.yaml)
endef
define extract_from_yaml2
$(strip $(shell docker run -v ${PWD}:/workdir --rm -it mikefarah/yq ."" conf.yaml))
endef
MODEL_VERSION := $(call extract_from_yaml,run.model_version)
MODEL_PATH := src/sad/models/${MODEL_VERSION}/dir
MODEL_VERSION2 := $(strip $(call extract_from_yaml,run.model_version))
MODEL_PATH2 := src/sad/models/${MODEL_VERSION2}/dir
MODEL_VERSION3 := $(call extract_from_yaml,run.model_version)
MODEL_PATH3 := src/sad/models/${MODEL_VERSION3}/dir
mytest:
${MODEL_VERSION}
mytest2:
${MODEL_PATH}
mytest3:
${MODEL_PATH2}
mytest4:
${MODEL_PATH3}
If I do make mytest
, the output is as expected and will be v1.0.0
. However, if I do make mytest2
, the output is "src/sad/models/ v1.0.0/dir"
. I don't understand where those white spaces come from. It seems that mytest3
is the solution where I remove the white space using $(strip)
.
It is annoyingly unbelievable that if I move the $(strip)
inside the function, still those whitespaces stay. You can see that by running make mytest4
.
So I wonder what is the prettiest way to do so.
英文:
I have a config file called conf.yaml
run:
mode: test
model_version: v1.0.0
inside a Makefile I am using yq to parse the conf file and assign values
here is the makefile
define extract_from_yaml
$(shell docker run -v ${PWD}:/workdir --rm -it mikefarah/yq ."" conf.yaml)
endef
define extract_from_yaml2
$(strip $(shell docker run -v ${PWD}:/workdir --rm -it mikefarah/yq ."" conf.yaml))
endef
MODEL_VERSION := $(call extract_from_yaml,run.model_version)
MODEL_PATH := src/sad/models/${MODEL_VERSION}/dir
MODEL_VERSION2 := $(strip $(call extract_from_yaml,run.model_version))
MODEL_PATH2 := src/sad/models/${MODEL_VERSION2}/dir
MODEL_VERSION3 := $(call extract_from_yaml,run.model_version)
MODEL_PATH3 := src/sad/models/${MODEL_VERSION3}/dir
mytest:
${MODEL_VERSION}
mytest2:
${MODEL_PATH}
mytest3:
${MODEL_PATH2}
mytest4:
${MODEL_PATH3}
if I do make mytest
the output is as expected and will be v1.0.0
however if I do make mytest2
the output is "src/sad/models/ v1.0.0/dir"
I don't understand where those white spaces come from. so seems the mytest3 is the solution where I remove the white space using $(strip)
.
it is annoyingly unbelievable that if I move the $(strip)
inside the function still those whitespaces stay. you can see that by running make mytest4
so I wonder what is the pretties way to do so.
答案1
得分: 1
以下是翻译好的部分:
The problem lies in spaces in extract_from_yaml.
Try this:
define extract_from_yaml
$(shell docker run -v ${PWD}:/workdir --rm -it mikefarah/yq ."" conf.yaml)
endef
define extract_from_yaml2
$(strip $(shell docker run -v ${PWD}:/workdir --rm -it mikefarah/yq ."" conf.yaml))
endef
MODEL_VERSION:=$(call extract_from_yaml2,run.model_version)
MODEL_PATH:=src/sad/models/${MODEL_VERSION}/dir
MODEL_VERSION2 := $(strip $(call extract_from_yaml,run.model_version))
MODEL_PATH2 := src/sad/models/${MODEL_VERSION2}/dir
MODEL_VERSION3 := $(call extract_from_yaml,run.model_version)
MODEL_PATH3 := src/sad/models/${MODEL_VERSION3}/dir
mytest:
echo ${MODEL_VERSION}
mytest2:
echo ${MODEL_PATH}
mytest3:
echo ${MODEL_PATH2}
mytest4:
echo ${MODEL_PATH3}
英文:
The problem lies in spaces in extract_from_yaml.
Try this :
define extract_from_yaml
$(shell docker run -v ${PWD}:/workdir --rm -it mikefarah/yq ."" conf.yaml)
endef
define extract_from_yaml2
$(strip $(shell docker run -v ${PWD}:/workdir --rm -it mikefarah/yq ."" conf.yaml))
endef
MODEL_VERSION:=$(call extract_from_yaml2,run.model_version)
MODEL_PATH:=src/sad/models/${MODEL_VERSION}/dir
MODEL_VERSION2 := $(strip $(call extract_from_yaml,run.model_version))
MODEL_PATH2 := src/sad/models/${MODEL_VERSION2}/dir
MODEL_VERSION3 := $(call extract_from_yaml,run.model_version)
MODEL_PATH3 := src/sad/models/${MODEL_VERSION3}/dir
mytest:
echo ${MODEL_VERSION}
mytest2:
echo ${MODEL_PATH}
mytest3:
echo ${MODEL_PATH2}
mytest4:
echo ${MODEL_PATH3}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论