Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2315310
  • 博文数量: 527
  • 博客积分: 10343
  • 博客等级: 上将
  • 技术积分: 5565
  • 用 户 组: 普通用户
  • 注册时间: 2005-07-26 23:05
文章分类

全部博文(527)

文章存档

2014年(4)

2012年(13)

2011年(19)

2010年(91)

2009年(136)

2008年(142)

2007年(80)

2006年(29)

2005年(13)

我的朋友

分类: LINUX

2010-05-25 16:30:12

export SHELL:=/bin/bash

inter_files=to_remove1.txt to_remove2.txt
.INTERMEDIATE: $(inter_files)

create_file: $(inter_files) ; @true
remove_file: ; -@rm -f $(inter_files)
$(inter_files): ;  @touch $@

test: remove_file create_file test_tty
    echo [var from : $(origin var)]

test_tty:
    false

.PHONY: test test_tty remove_file create_file

如果Make文件中需要用到中间文件, 当然希望任务结束时删除这些中间文件, 如果把删除作为一个rm 命令来实现, 则在make失败时不会执行到这一行.

上面的借用make会自动删除 .INTERMEDIATE 目标产生的文件这一特点, 实现make无论成功失败时都会删除这些中间文件的功能:

1. 声明一个.PHONY 目标, 如remove_file, create_file
2. 把remove_file create_file依次添加到目标中, 如test
3. 为remove_file添加一个伪规则
remove_file: ; -@rm -f $(inter_files)

-@ 是必需的, 保证文件总是被删除, 不考虑权限不够等情况

4. 为create_file添加规则:
create_file: $(inter_files); @true

目的只是保证各个 $(inter_files)中的目标会被执行:

5. 为每个中间文件添加这样一个规则:
  $(inter_files): ; @touch $@

 touch命令是必需的, 而且必需保证中间文件是在此次make过程中新生成的, 只有这样make才会在最后删除这些它自己生成的中间文件.

通过
.INTERMEDIATE 指定的文件名, 必需作为一个目标被执行的过程中产生了该文件时, make才会删除.

上述实现保证, 即使在make之前已经存在 $(inter_files)中指定的文件, 也会将其删除, 因为make过程会先删除这些文件, 然后重新生成.
阅读(1819) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~