Search code examples
gnu-makemd5gnu

How to create MD5 file using GNU Makefile?


I have this Makefile which converts pnm file to png file. pnm file is in the inputs directory and then converted file (png) should be in the outputs directory. My question is how I need to create this summary MD5 file that sums my png file in the outputs using GNU make? I tried writing this rule, but it still doesn't create any MD5 file, only outputs the png file.

$(MD5_FILE): $(OUTPUT_FILES)
    md5 $@ $^

This is my Makefile code

INPUT_DIR = inputs
OUTPUT_DIR = outputs

INPUT_FILES = $(wildcard $(INPUT_DIR)/*.pnm)
OUTPUT_FILES = $(INPUT_FILES:$(INPUT_DIR)/%.pnm=$(OUTPUT_DIR)/%.png)
MD5_file = $(OUTPUT_DIR)/md5-file.md5

.PHONY: all
all: $(OUTPUT_FILES) $(MD5_FILE)

$(MD5_FILE): $(OUTPUT_FILES)
    md5 $@ $^

$(OUTPUT_DIR)/%.png: $(INPUT_DIR)/%.pnm
    pnmtopng $<  > $@ 

.PHONY: clean distclean
clean: 
    rm -f $(OUTPUT_FILES)
distclean: clean

Solution

  • You forgot to specify an output file; you are asking it to print the MD5 for the previous (nonexistent, empty, or stale) output file as well as the input files.

    $(MD5_FILE): $(OUTPUT_FILES)
        md5 $^ >$@
    

    You'll also want to make sure you specify the variable consistently; here, you are calling it MD5_FILE whereas above in the assignment you used MD5_file.

    Finally, you'll also want to remove this file in the clean target.