仅在目标目录上不存在的情况下下载并移动文件,否则不执行任何操作

时间:2019-01-29 10:31:21

标签: makefile gnu-make

我正在尝试通过下载zip文件来安装可执行命令(terraform),然后使用~/bin将其解压缩到Makefile目录中。

仅当terraform文件不存在时,我才想这样做。

根据GNU Make文档,我认为我应该对管道|https://www.gnu.org/software/make/manual/make.html#Prerequisite-Types)使用“仅订购先决条件”,尽管我并不完全了解该文档。

我编写了以下目标规则,目的是在文件make install-tf已经存在的情况下,以~/bin/terraform的身份运行它,然后什么也不做然后返回。但是,这不能按预期方式工作,无论文件是否已存在,我始终看到该目标规则的主体正在执行。

  • 以下目标有什么问题?
  • 仅当使用terraform的命令尚不存在时,如何安装Makefile

这是目标:

TERRAFORM_VERSION=0.11.11

install-tf: | ~/bin/terraform
    echo "Installing terraform version ${TERRAFORM_VERSION}"
    mkdir -p ~/tmp/
    wget https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip -P ~/tmp/
    mkdir -p ~/bin/
    unzip ~/tmp/terraform_${TERRAFORM_VERSION}_linux_amd64.zip -d ~/bin/
    echo "Done installing terraform version ${TERRAFORM_VERSION}"
    PATH=~/bin/:$$PATH terraform --version

1 个答案:

答案 0 :(得分:2)

  

以下目标有什么问题?

目标是install-tfmake install-tf将始终运行配方来完成此操作 如果没有install-tf这样的文件,请定位。配方永远不会创造 该文件。因此,配方将始终运行。

只有在以下情况下,您才需要配方才能制作文件$(HOME)/bin/terraform 不存在。大概是这样:

制作文件(1)

TERRAFORM_VERSION=0.11.11

$(HOME)/bin/terraform:
    echo "Installing terraform version ${TERRAFORM_VERSION}"
    mkdir -p $(HOME)/tmp/
    wget https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip -P $(HOME)/tmp/
    mkdir -p $(HOME)/bin/
    unzip ~/tmp/terraform_${TERRAFORM_VERSION}_linux_amd64.zip -d $(HOME)/bin/
    echo "Done installing terraform version ${TERRAFORM_VERSION}"
    PATH=$(HOME)/bin/:$$PATH terraform --version

第一次运行,就像:

$ make
echo "Installing terraform version 0.11.11"
Installing terraform version 0.11.11
mkdir -p /home/imk/tmp/
wget https://releases.hashicorp.com/terraform/0.11.11/terraform_0.11.11_linux_amd64.zip -P /home/imk/tmp/
--2019-01-29 11:09:11--  https://releases.hashicorp.com/terraform/0.11.11/terraform_0.11.11_linux_amd64.zip
Resolving releases.hashicorp.com (releases.hashicorp.com)... 151.101.17.183, 2a04:4e42:4::439
Connecting to releases.hashicorp.com (releases.hashicorp.com)|151.101.17.183|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 20971661 (20M) [application/zip]
Saving to: ‘/home/imk/tmp/terraform_0.11.11_linux_amd64.zip’

terraform_0.11.11_l 100%[===================>]  20.00M  8.86MB/s    in 2.3s

2019-01-29 11:09:14 (8.86 MB/s) - ‘/home/imk/tmp/terraform_0.11.11_linux_amd64.zip’ saved [20971661/20971661]

mkdir -p /home/imk/bin/
unzip ~/tmp/terraform_0.11.11_linux_amd64.zip -d /home/imk/bin/
Archive:  /home/imk/tmp/terraform_0.11.11_linux_amd64.zip
  inflating: /home/imk/bin/terraform
echo "Done installing terraform version 0.11.11"
Done installing terraform version 0.11.11
PATH=/home/imk/bin/:$PATH terraform --version
Terraform v0.11.11

第二次像:

$ make
make: '/home/imk/bin/terraform' is up to date.

还有一点改进:

制作文件(2)

TERRAFORM_VERSION := 0.11.11

.PHONY: all clean

all: $(HOME)/bin/terraform

$(HOME)/bin/terraform: | $(HOME)/tmp/ $(HOME)/bin/
    echo "Installing terraform version ${TERRAFORM_VERSION}"
    wget https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip -P $(HOME)/tmp/
    unzip ~/tmp/terraform_${TERRAFORM_VERSION}_linux_amd64.zip -d $(HOME)/bin/
    echo "Done installing terraform version ${TERRAFORM_VERSION}"
    PATH=$(HOME)/bin/:$$PATH terraform --version

$(HOME)/tmp/ $(HOME)/bin/:
    mkdir -p $@

clean:
    $(RM) $(HOME)/bin/terraform

运行方式:

$ make clean
rm -f /home/imk/bin/terraform

$ make
echo "Installing terraform version 0.11.11"
Installing terraform version 0.11.11
wget https://releases.hashicorp.com/terraform/0.11.11/terraform_0.11.11_linux_amd64.zip -P /home/imk/tmp/
--2019-01-29 11:38:32--  https://releases.hashicorp.com/terraform/0.11.11/terraform_0.11.11_linux_amd64.zip
Resolving releases.hashicorp.com (releases.hashicorp.com)... 151.101.17.183, 2a04:4e42:4::439
Connecting to releases.hashicorp.com (releases.hashicorp.com)|151.101.17.183|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 20971661 (20M) [application/zip]
Saving to: ‘/home/imk/tmp/terraform_0.11.11_linux_amd64.zip.1’

terraform_0.11.11_l 100%[===================>]  20.00M  8.88MB/s    in 2.3s    

2019-01-29 11:38:34 (8.88 MB/s) - ‘/home/imk/tmp/terraform_0.11.11_linux_amd64.zip.1’ saved [20971661/20971661]

unzip ~/tmp/terraform_0.11.11_linux_amd64.zip -d /home/imk/bin/
Archive:  /home/imk/tmp/terraform_0.11.11_linux_amd64.zip
  inflating: /home/imk/bin/terraform  
echo "Done installing terraform version 0.11.11"
Done installing terraform version 0.11.11
PATH=/home/imk/bin/:$PATH terraform --version
Terraform v0.11.11

$ make
make: Nothing to be done for 'all'.

请注意在以下情况下使用仅订购的先决条件:

$(HOME)/bin/terraform: | $(HOME)/tmp/ $(HOME)/bin/

那是说:在考虑目标$(HOME)/bin/terraform是否过时之前,先设置$(HOME)/tmp/$(HOME)/bin。 然后考虑$(HOME)/bin/terraform是否已过期,但不将$(HOME)/tmp/$(HOME)/bin视为前提条件。

另请参阅4.6 Phony Targets