]> git.draconx.ca Git - dxcommon.git/blob - m4/md5.m4
DX_C_ALIGNAS: Work around bash-5 parsing bug.
[dxcommon.git] / m4 / md5.m4
1 dnl Copyright © 2021-2022 Nick Bowler
2 dnl
3 dnl License WTFPL2: Do What The Fuck You Want To Public License, version 2.
4 dnl This is free software: you are free to do what the fuck you want to.
5 dnl There is NO WARRANTY, to the extent permitted by law.
6
7 dnl DX_PROG_MD5
8 dnl
9 dnl Search PATH for a command-line md5 digest utility, such as GNU md5sum,
10 dnl FreeBSD md5 or openssl md5.  If found, the MD5 variable (which is
11 dnl substituted by AC_SUBST) is set to the result and the cache variable
12 dnl dx_cv_md5_works is set to "yes".  Otherwise, dx_cv_md5_works is set to
13 dnl "no".
14 dnl
15 dnl The utilities that can be found have slightly different options and
16 dnl output formats.  For example, GNU md5sum prints each line with the
17 dnl digest followed by the filename, whereas openssl md5 prints the filename
18 dnl followed by the digest.  The MD5 setting will include the -r option, when
19 dnl supported, to prefer a command that prints the digest first as this is
20 dnl easier to parse by shell code.  The cache variable dx_cv_md5_outpos will
21 dnl be set to "front" if the digest appears at the beginning of the line,
22 dnl and "back" if the digest appears at the end of the line.  Portable usage
23 dnl should handle both scenarios.
24 AC_DEFUN([DX_PROG_MD5], [AC_PREREQ([2.62])dnl
25 AC_ARG_VAR([MD5], [MD5 digest command])dnl
26 eval ': >conftest.in'
27 AC_CACHE_CHECK([for utility to compute MD5 digests], [ac_cv_path_MD5],
28 [dx_cv_md5_found=:
29 AC_PATH_PROGS_FEATURE_CHECK([MD5], [md5sum md5 openssl],
30 [DX_BASENAME([md5_relcmd], ["$ac_path_MD5"])
31 set x '-r' ''; shift
32 AS_CASE([$md5_relcmd], [openssl], [ac_path_MD5="$ac_path_MD5 md5"],
33                        [md5sum], [shift])
34 eval ': >conftest.in'
35 for md5_arg
36 do
37 _DX_MD5_DO_TEST([$ac_path_MD5 $md5_arg],
38   [ac_path_MD5="$ac_path_MD5 $md5_arg"; break])
39 done
40 AS_IF([test x"$dx_cv_md5_works" = x"yes"],
41 [md5_bypath=`{ command -v "$md5_relcmd"; } 2>/dev/null` #''
42 ac_cv_path_MD5=$ac_path_MD5
43 set x $ac_path_MD5; shift
44 if test x"$md5_bypath" = x"$[1]"; then
45   shift; set x $md5_relcmd "$[@]"; shift;
46   ac_cv_path_MD5=$[*]
47 fi
48 ac_path_MD5_found=:])], [ac_cv_path_MD5='none found' dx_cv_md5_found=false])])
49 AS_IF([$dx_cv_md5_found],
50 [MD5=$ac_cv_path_MD5
51 AC_CACHE_CHECK([for $MD5 digest position], [dx_cv_md5_outpos],
52 [_DX_MD5_DO_TEST([$MD5])])])
53 rm -f conftest.in conftest.out])
54
55 AC_DEFUN([_DX_MD5_DO_TEST],
56 [AS_IF([$1 conftest.in >conftest.out 2>&AS_MESSAGE_LOG_FD],
57 [read a b <conftest.out
58 AS_IF([test x"$a" = x"d41d8cd98f00b204e9800998ecf8427e"],
59   [dx_cv_md5_works=yes dx_cv_md5_outpos=front
60 $2],
61 [set x $b; shift; eval "a=\$$[#]"
62 AS_IF([test x"$a" = x"d41d8cd98f00b204e9800998ecf8427e"],
63   [dx_cv_md5_works=yes dx_cv_md5_outpos=back
64 $2], [dx_cv_md5_works=no dx_cv_md5_outpos='not found'])])],
65 [dx_cv_md5_works=no dx_cv_md5_outpos='not found'])])