简体   繁体   中英

MD5SUM in a Makefile

I'm trying to generate a MD5 checksum of a file in a Makefile. In my Makefile i have something like;

CHECKSUM=md5sum $(myfile)

But the variable CHECKSUM is always empty

Can anybody tell me what's wrong here?

Here is a slightly different example, where I set the expected md5 value as a make variable, and then check against it inside the shell commands in my recipe. In this case, I wanted to download a specific version of Anaconda and check its md5sum before installing it.

Makefile:

SHELL:=/bin/bash
ANACONDA_MD5:=c989ecc8b648ab8a64731aaee9ed2e7e

none:

Anaconda3-5.0.1-Linux-x86_64.sh: 
    wget https://repo.continuum.io/archive/Anaconda3-5.0.1-Linux-x86_64.sh

download: Anaconda3-5.0.1-Linux-x86_64.sh

verify: download
    AnacondaMD5="$$(md5sum Anaconda3-5.0.1-Linux-x86_64.sh | cut -d ' ' -f1)" && \
    if [ "$$AnacondaMD5" == '$(ANACONDA_MD5)' ]; then echo "md5sum matches"; fi

Output:

$ make verify
wget https://repo.continuum.io/archive/Anaconda3-5.0.1-Linux-x86_64.sh
--2018-01-16 18:11:50--  https://repo.continuum.io/archive/Anaconda3-5.0.1-Linux-x86_64.sh
Resolving repo.continuum.io... 104.16.18.10, 104.16.19.10, 2400:cb00:2048:1::6810:130a, ...
Connecting to repo.continuum.io|104.16.18.10|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 550796553 (525M) [application/x-sh]
Saving to: `Anaconda3-5.0.1-Linux-x86_64.sh'

100%[====================================================================================================================>] 550,796,553  103M/s   in 6.8s

2018-01-16 18:11:59 (77.0 MB/s) - `Anaconda3-5.0.1-Linux-x86_64.sh' saved [550796553/550796553]

AnacondaMD5="$(md5sum Anaconda3-5.0.1-Linux-x86_64.sh | cut -d ' ' -f1)" && \
    if [ "$AnacondaMD5" == 'c989ecc8b648ab8a64731aaee9ed2e7e' ]; then echo "md5sum matches"; fi
md5sum matches

Note the usage of $$AnacondaMD5 to fill in the in-line bash variable vs. the usage of $(ANACONDA_MD5) to fill in the make variable

Version:

$ make --version
GNU Make 3.81
Copyright (C) 2006  Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

This program built for x86_64-redhat-linux-gnu

This works for me:

NOW=$$(date)
print-now:
    @echo $(NOW)
md5sum:
    @SUM=$$(md5sum file.txt | cut -d' ' -f 1); \
    echo $$SUM; \
    cp file.txt file.$${SUM}.txt; \

Now run it with make md5sum .

You should get file file.<sum>.txt .

If you copy code from above, remember to use tab for indent or get the file from repo https://github.com/rofrol/makefile-md5sum .

As Chris says, you need something like:

CHECKSUM=$(md5sum $(myfile))

In case you didn't know, CHECKSUM will only available on that line. ie the following will output a blank link:

test:
    CHECKSUM=$(md5sum $(myfile))
    echo $$CHECKSUM

The following will do what you need:

test:
    CHECKSUM=$(md5sum $(myfile)); echo $$CHECKSUM

Or, if you need it over multiple lines

test:
    CHECKSUM=$(md5sum $(myfile)); \
    echo $$CHECKSUM; \
    echo $$CHECKSUM;

If you cut n paste the above, you need to insert tabs.

Do you need the result of a command outside the commands-part in makefile ?
Then, if your make is GNU-make , $(shell) function is available.
For example:

CHECKSUM := $(shell md5sum $(myfile))

For a list of files you can have following in Makefile

FILES=foo/bar/image.svg \
    foo/bar3/somejs.js \
    foo/bar1/someimage.svg \
    foo/bar2/anotherjs.js \
    foo/somestyle.css \
    html/block/somepng.png

checksum:
    for f in $(FILES); do \
    echo "file: $$f"; \
    SUM=$$(md5sum $$f | cut -d' ' -f 1); \
    echo "CHECKSUM: $$SUM"; \
    done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM