Conditionally updating a target in a Makefile

I have a tool that I use to generate C++ header files as part of the build. I only want to write out the generated headers if they changed from what is already on disk so that I don’t trigger a rebuild of the object files that depend on those headers. I’m trying to do this with GNU Make. My problem is, because of the conditional write, I end up in a situation where the prerequisites are always newer than the target.

Here’s a simplified example of my code:

$(TMP_GENERATED_FILE): $(SCRIPT_INPUT_FILES)
    $(SCRIPT) $< -o $@


$(GENERATED_FILE): $(TMP_GENERATED_FILE)
    cmp -s "$<" "$@" || cp -f "$<" "$@"

# Other rules for the .o files which depend on $(GENERATED_FILE)

The problem is $(TMP_GENERATED_FILE) will almost always be newer than $(GENERATED_FILE) because I only write out $(GENERATED_FILE) if it differs from $(TMP_GENERATED_FILE). This means the recipe for $(GENERATED_FILE) executes on every build. Is there any way to avoid this? The desired state is the subsequent runs of make would do nothing if nothing changed.

Edit: One other attempt was the following:

$(TEMP_GENERATED_FILE): $(SCRIPT_INPUT_FILES)
    $(SCRIPT) $< -o $@

# Get make to reread the Makefile after generating $(TEMP_GENERATED_FILE) so that the condition below works correctly
.PHONY: run_script
run_script: $(TEMP_GENERATED_FILE)
-include run_script

ifeq ($(shell cmp -s "$(TEMP_GENERATED_FILE)" "$(GENERATED_FILE)" && echo identical || echo different), different)
    $(GENERATED_FILE): $(TMP_GENERATED_FILE)
        cp -f "$<" "$@"
endif

but it seems like I run into the issue described here: ” To avoid this make will not attempt to remake makefiles which are marked phony”

New contributor

ryan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

7

The short answer is there’s no straightforward way to avoid it.

All versions of make use the fileystem as their “database” tracking what files are out of date or not. In particular they use the modification time of files to detect when something is out of date.

You can prevent make from rebuilding things that depend on the GENERATED_FILE by not updating the modification time of the GENERATED_FILE. But if you don’t update the modification time of the GENERATED_FILE then the next time you run make it will still be out of date with respect to its prerequisites.

There’s no way to change this algorithm.

The only way to avoid re-running the cmp recipe would be, if you discover that the GENERATED_FILE doesn’t need to be rebuilt, to reset the timestamps of your prerequisites (e.g., TMP_GENERATED_FILE and all the SCRIPT_INPUT_FILES) so they were older than GENERATED_FILE and the next time you run make it will not think they are outdated.

But, make is not the only tool that uses modification times of files for important decisions so that change could have other impacts. I personally would not advise it, and instead just live with the extra cmp being run every time. It’s not much of a burden IMO.

3

You have too complicated build of the header. Here is a working example:

# makefile
all: hello

hello: hello.o
    gcc $^ -o $@

hello.o: hello.c hello.h
    gcc -c $< -o $@

hello.h: hello.h.in1 hello.h.in2
    cat $^ > $@
// hello.c
#include "hello.h"

int main() {
    fputs("hellon", stdout);
    return 0;
}
// hello.h.in1
/* autogenerated header */
// hello.h.in2
#include <stdio.h>

Testing:

$ make
cat hello.h.in1 hello.h.in2 > hello.h
gcc -c hello.c -o hello.o
gcc hello.o -o hello
$ touch hello.h.in2
$ make
cat hello.h.in1 hello.h.in2 > hello.h
gcc -c hello.c -o hello.o
gcc hello.o -o hello
$ touch hello.c
$ make
gcc -c hello.c -o hello.o
gcc hello.o -o hello
$ make
make: Nothing to be done for 'all'.

Questions?

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật