问题描述
我手头上有一个源文件位于以下位置的任务:
I have a task at hand where my source file is under:
apps/$(APP_NAME)/$(APP_ENV)/$(APP_NAME).yml
然后我需要将这些文件处理为:
then I need to process those files into:
.output/$(APP_NAME)/$(APP_ENV).yml
我无法为我的APP_NAME是动态的并且来自的Makefile规则进行分类
I am failing to sort out the rules for such Makefile where my APP_NAME is dynamic and comes from
APP_NAMES=$(shell ls -1 $(APP_DIRS))
我能够做单个依赖关系没问题:
I am able to do singular dependencies no problem:
OUTPUT_DIR:=.output
APPS_DIR:=apps
$(OUTPUT_DIR)/app1/prod.yml: $(APPS_DIR)/app1/prod/app1.yml
$(eval APP_ENV=$(patsubst %.yml, %, $(patsubst $(OUTPUT_DIR)/%, %, $@)))
$(eval APP=$(dir $(APP_ENV)))
$(eval ENV=$(notdir $(APP_ENV)))
mkdir -p $(dir $@)
my_process --app=$(APP) --environment=$(ENV) --apps-dir=$(APPS_DIR) > $@
但是,制定隐式动态规则会让我逃脱.
however making an implicit dynamic rule escapes me.
推荐答案
模式规则只能出现一次%
,但您的情况需要两次.
Pattern rules can only have one occurence of %
but your case would require two.
我会尝试使用宏动态生成必要的规则,然后$(eval)
.如果我正确理解了您的要求,那么该makefile应该可以做到:
I would try generating the necessary rules on the fly with a macro and then $(eval)
them. If I understand your requirements correctly then this makefile should do it:
# first rule in makefile is the default
.PHONY: all
all:
OUTPUT_DIR := .output
APPS_DIR := apps
# set as a fixed list for the solution - can be dynamic
SOURCE_YMLS := \
$(APPS_DIR)/test1/prod/test1.yml \
$(APPS_DIR)/test1/env1/test1.yml \
$(APPS_DIR)/test1/env2/test1.yml \
$(APPS_DIR)/test2/stage/test2.yml \
$(APPS_DIR)/test2/env3/test2.yml \
$(APPS_DIR)/test2/env4/test2.yml \
OUTPUT_YMLS :=
OUTPUT_DIRS :=
# $(1): source file name $(APPS_DIR)/<app_name>/<app_env>/<app_name>.yml
define generate_yml_rule
_input_parts := $(subst /, ,$(patsubst $(APPS_DIR)/%,%,$(1)))
_app_name := $$(word 1,$$(_input_parts))
_app_env := $$(word 2,$$(_input_parts))
_output_dir := $(OUTPUT_DIR)/$$(_app_name)
_output_yml := $$(_output_dir)/$$(_app_env).yml
# target specific variables for recipe evaluation
$$(_output_yml): _app_name := $$(_app_name)
$$(_output_yml): _app_env := $$(_app_env)
# why does "myprocess" not use $$< directly ???
# NOTE: >$$@ commented out for testing only
$$(_output_yml): $(1) | $$(_output_dir)
@echo my_process --app=$$(_app_name) --environment=$$(_app_env) --apps-dir=$(APPS_DIR) # >$$@
OUTPUT_DIRS += $$(_output_dir)
OUTPUT_YMLS += $$(_output_yml)
_input_parts :=
_app_name :=
_app_env :=
_output_dir :=
_output_yml :=
endef
# generate rules for output yml files
$(eval \
$(foreach _f,$(SOURCE_YMLS), \
$(call generate_yml_rule,$(_f)) \
) \
)
# remove duplicate directories
OUTPUT_DIRS := $(sort $(OUTPUT_DIRS))
$(info OUTPUT_DIRS '$(OUTPUT_DIRS)')
$(info OUTPUT_YMLS '$(OUTPUT_YMLS)')
all: $(OUTPUT_YMLS)
@echo DONE
$(OUTPUT_DIRS): | $(OUTPUT_DIR)
$(OUTPUT_DIRS) $(OUTPUT_DIR):
mkdir -p $@
如果将info
放在eval
前面,则使生成的代码可见(仅摘录):
If we prepend info
in front of eval
then we make the generated code visible (excerpt only):
eval _input_parts := test1 prod test1.yml
_app_name := $(word 1,$(_input_parts))
_app_env := $(word 2,$(_input_parts))
_output_dir := .output/$(_app_name)
_output_yml := $(_output_dir)/$(_app_env).yml
# target specific variables for recipe evaluation
$(_output_yml): _app_name := $(_app_name)
$(_output_yml): _app_env := $(_app_env)
# why does "myprocess" not use $< directly ???
# NOTE: >$@ commented out for testing only
$(_output_yml): apps/test1/prod/test1.yml | $(_output_dir)
@echo my_process --app=$(_app_name) --environment=$(_app_env) --apps-dir=apps # >$@
OUTPUT_DIRS += $(_output_dir)
OUTPUT_YMLS += $(_output_yml)
_input_parts :=
_app_name :=
_app_env :=
_output_dir :=
_output_yml :=
_input_parts := test1 env1 test1.yml
_app_name := $(word 1,$(_input_parts))
_app_env := $(word 2,$(_input_parts))
_output_dir := .output/$(_app_name)
_output_yml := $(_output_dir)/$(_app_env).yml
# target specific variables for recipe evaluation
$(_output_yml): _app_name := $(_app_name)
$(_output_yml): _app_env := $(_app_env)
# why does "myprocess" not use $< directly ???
# NOTE: >$@ commented out for testing only
$(_output_yml): apps/test1/env1/test1.yml | $(_output_dir)
@echo my_process --app=$(_app_name) --environment=$(_app_env) --apps-dir=apps # >$@
OUTPUT_DIRS += $(_output_dir)
OUTPUT_YMLS += $(_output_yml)
_input_parts :=
... and so on for the 4 other source files...
试运行(myprocess
用echo
注释掉以显示正在发生的事情):
Test run (myprocess
commented out with echo
to show what is happening):
$ make
OUTPUT_DIRS '.output/test1 .output/test2'
OUTPUT_YMLS ' .output/test1/prod.yml .output/test1/env1.yml .output/test1/env2.yml .output/test2/stage.yml .output/test2/env3.yml .output/test2/env4.yml'
mkdir -p .output
mkdir -p .output/test1
my_process --app=test1 --environment=prod --apps-dir=apps
my_process --app=test1 --environment=env1 --apps-dir=apps
my_process --app=test1 --environment=env2 --apps-dir=apps
mkdir -p .output/test2
my_process --app=test2 --environment=stage --apps-dir=apps
my_process --app=test2 --environment=env3 --apps-dir=apps
my_process --app=test2 --environment=env4 --apps-dir=apps
DONE
这篇关于无法找出具有复杂依赖关系的Makefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!