dnl CF_MAKE_AR_RULES version: 10 updated: 2025/09/28 13:52:37
dnl ----------------
dnl Check if the 'make' program knows how to interpret archive rules.  Though
dnl this has been common practice since the mid-80's, there were some holdouts
dnl noted in 1997 among the BSD-based Unix systems.  Twenty years later, some
dnl BSDs still fail to provide this POSIX feature.
dnl
dnl For reference, here is the 2004 edition:
dnl
dnl https://pubs.opengroup.org/onlinepubs/009695399/utilities/make.html
dnl
dnl If a target or prerequisite contains parentheses, it shall be treated as a
dnl member of an archive library.  For the lib( member .o) expression lib
dnl refers to the name of the archive library and member .o to the member name. 
dnl The application shall ensure that the member is an object file with the .o
dnl suffix.  The modification time of the expression is the modification time
dnl for the member as kept in the archive library; see ar.  The .a suffix shall
dnl refer to an archive library.  The .s2.a rule shall be used to update a
dnl member in the library from a file with a suffix .s2.
AC_DEFUN([CF_MAKE_AR_RULES],
[
AC_MSG_CHECKING(if ${MAKE:-make} program knows about archives)
AC_CACHE_VAL(cf_cv_ar_rules,[
cf_dir=subd$$
cf_cv_ar_rules=unknown
mkdir "$cf_dir"
cat >"$cf_dir/makefile" <<CF_EOF
SHELL = /bin/sh
AR = ar crv
CC = $CC

.SUFFIXES:
.SUFFIXES: .c .${OBJEXT} .a

all:  conf.a

.c.a:
	\$(CC) -c $<
	\$(AR) \$[]@ \$[]*.${OBJEXT}
conf.a : conf.a(conftest.${OBJEXT})
CF_EOF
cat >$cf_dir/conftest.c <<CF_EOF
#include <stdio.h>
extern int conftest(char *name);
int conftest(char *name)
{
	FILE *fp = fopen(name, "r");
	int rc = 0;
	if (fp != 0) {
		fclose(fp);
		rc = 1;
	}
	return rc;
}
CF_EOF
CDPATH=; export CDPATH
if ( cd "$cf_dir" && "${MAKE:-make}" 2>&AC_FD_CC >&AC_FD_CC && test -f conf.a )
then
	cf_cv_ar_rules=yes
else
	echo ... library-rule did not create an archive >&AC_FD_CC
	rm -f "$cf_dir/conftest.${OBJEXT}"
	cat >"$cf_dir/makefile" <<CF_EOF
SHELL = /bin/sh
AR = ar crv
CC = $CC

.SUFFIXES:
.SUFFIXES: .c .${OBJEXT}

all:  conf.a

.c.${OBJEXT}:
	\$(CC) -c $<

conf.a : conftest.${OBJEXT}
	\$(AR) \$[]@ \$?
CF_EOF
	CDPATH=; export CDPATH
	if ( cd "$cf_dir" && "${MAKE:-make}" 2>&AC_FD_CC >&AC_FD_CC && test -f conf.a )
	then
		cf_cv_ar_rules=no
	else
		AC_MSG_ERROR(I do not know how to construct a library)
	fi
fi
rm -rf "$cf_dir"
])
AC_MSG_RESULT($cf_cv_ar_rules)
])dnl
