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

【已解决】Makefile中的target的末尾调用其他target

makefile crifan 5319浏览 0评论

已经写好了Makefile的deploy:

## Clean tmp folder

clean_tmp:

  -rm -rf $(TMP_PATH)

################################################################################

# Deploy to server

################################################################################

PASSWORD_FILE=sshpass_password.txt

REMOTE_USER=root

REMOTE_SERVER=42.123.123.254

REMOTE_PATH=/opt/cowfarm/farm_web

## copy generated build files to tmp folder

copy_build: create_folder_tmp

  cp -a $(BUILD_PATH)/* $(TMP_PATH)

## Deploy build files to remote server using rsync. create sshpass_password.txt file to contain password before use this

deploy: clean_tmp copy_build

  sshpass -f $(PASSWORD_FILE) rsync -avzh –progress –stats –delete –force $(TMP_PATH) $(REMOTE_USER)@$(REMOTE_SERVER):$(REMOTE_PATH)

现在希望:

deploy之后,再去调用clean_tmp去清除临时文件夹

即:makefile中,一个target的末尾,再去调用另外一个target

自己能想到的是:再去增加临时的targt去间接调用

makefile run target after

makefile execute another target – Stack Overflow

好像就是这个办法,没其他更好办法?

Using a make rule to call another – Unix & Linux Stack Exchange

makefile run target inside target

Re: How can I call one target from another

makefile   target include target

gnu make – How to use the include directive in a makefile for a specific target – Stack Overflow

Why Makefile include should be after all: target? – Stack Overflow

但是好像试了试:

## Deploy build files to remote server using rsync. Note: create sshpass_password.txt file to contain password before use this

deploy: clean_copy_deploy clean_tmp

结果貌似最后的clean_tmp没有执行?

去加上竖线|,表示右边顺序执行,试试:

deploy: | clean_copy_deploy clean_tmp

问题依旧。

然后直接试了试:

deploy: clean_tmp

是可以调用clean_tmp删除的。

然后注意到:

估计是:

clean_copy_deploy: clean_tmp copy_build

deploy: | clean_copy_deploy clean_tmp

deploy依赖中的clean_copy_deploy有了clean_tmp,所以后面的clean_tmp就多余了而无效了。

所以:

要么去把后面的clean_tmp改名,要么直接把药要执行的动作:

clean_tmp:

  -rm -rf $(TMP_PATH)

直接放到deploy或clean_copy_deploy下面。

makefile target dependencies same name

makefile   dependencies multiple same target

makefile – How to reuse a pattern rule for the same target in (GNU) make? – Stack Overflow

makefile中对于多个依赖,就是忽略掉的

-》作用是:避免重复依赖,避免可能的循环调用

-〉所以此处还是改名字吧

【总结】

最后用:

.DEFAULT_GOAL := deploy

.PHONY : debug_dir

.PHONY : help

.PHONY : create_folder_tmp copy_build

.PHONY : clean_tmp

.PHONY : clean_copy_deploy deploy

################################################################################

# Create folder

################################################################################

## Create folder for tmp folder

create_folder_tmp:

    mkdir -p $(TMP_PATH)

################################################################################

# Clean

################################################################################

## Clean tmp folder

clean_tmp:

    -rm -rf $(TMP_PATH)

## Clean tmp folder again

clean_tmp_2:

    -rm -rf $(TMP_PATH)

################################################################################

# Deploy to server

################################################################################

## copy generated build files to tmp folder

copy_build: create_folder_tmp

    cp -a $(BUILD_PATH)/* $(TMP_PATH)

## Clean and Copy and Deploy

clean_copy_deploy: clean_tmp copy_build

    sshpass -f $(PASSWORD_FILE) rsync -avzh –progress –stats –delete –force $(TMP_PATH) $(REMOTE_USER)@$(REMOTE_SERVER):$(REMOTE_PATH)

## Deploy build files to remote server using rsync. Note: create sshpass_password.txt file to contain password before use this

deploy: | clean_copy_deploy clean_tmp_2

实现了:

deploy期间,先去 clean_copy_deploy,期间会调用clean_tmp,最后再去调用功能同样的但是名字不同的clean_tmp_2

效果:

转载请注明:在路上 » 【已解决】Makefile中的target的末尾调用其他target

发表我的评论
取消评论

表情

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

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