Skip to content

Commit

Permalink
Merge pull request #384 from Tarsnap/apisupport
Browse files Browse the repository at this point in the history
Import and use apisupport
  • Loading branch information
cperciva authored Apr 7, 2024
2 parents c3a97fc + 334db8e commit e584f2c
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
/libtool
/stamp-h1
# In-tree build, only top-level
/apisupport-config.h
/cpusupport-config.h
# In-tree build, all directories
.dirstamp
Expand Down
12 changes: 9 additions & 3 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ AM_CPPFLAGS= -I$(srcdir)/lib \
-I$(srcdir)/libcperciva/crypto \
-I$(srcdir)/libcperciva/util \
-DCPUSUPPORT_CONFIG_FILE=\"cpusupport-config.h\" \
-DAPISUPPORT_CONFIG_FILE=\"apisupport-config.h\" \
-D_POSIX_C_SOURCE=200809L \
-D_XOPEN_SOURCE=700 \
${CFLAGS_POSIX}
Expand All @@ -105,10 +106,15 @@ scrypt_LDADD= libcperciva_aesni.la libcperciva_rdrand.la \
${LDADD_POSIX}
scrypt_man_MANS= scrypt.1

# apisupport needs to access post-configure info: lib-platform/platform.h,
# config.h, and -DHAVE_CONFIG_H.
apisupport-config.h:
( export CC="${CC}"; export CFLAGS="-I${top_srcdir}/lib-platform -I${builddir} ${DEFS} ${CFLAGS}"; command -p sh $(srcdir)/libcperciva/apisupport/Build/apisupport.sh "$$PATH") > apisupport-config.h.tmp && command -p mv apisupport-config.h.tmp apisupport-config.h
cpusupport-config.h:
( export CC="${CC}"; export CFLAGS="${CFLAGS}"; command -p sh $(srcdir)/libcperciva/cpusupport/Build/cpusupport.sh "$$PATH") > cpusupport-config.h.tmp && command -p mv cpusupport-config.h.tmp cpusupport-config.h
BUILT_SOURCES= cpusupport-config.h
CLEANFILES= cpusupport-config.h cpusupport-config.h.tmp
BUILT_SOURCES= apisupport-config.h cpusupport-config.h
CLEANFILES= apisupport-config.h apisupport-config.h.tmp \
cpusupport-config.h cpusupport-config.h.tmp

# Libraries from libcperciva code.
noinst_LTLIBRARIES= libcperciva_aesni.la
Expand Down Expand Up @@ -151,7 +157,7 @@ libscrypt_sse2_la_CFLAGS=`. ./cpusupport-config.h; echo $${CFLAGS_X86_SSE2}`
noinst_LTLIBRARIES+= libscrypt_memlimit.la
libscrypt_memlimit_la_SOURCES= lib-platform/util/memlimit.c \
lib-platform/util/memlimit.h
libscrypt_memlimit_la_CFLAGS=-U_POSIX_C_SOURCE -U_XOPEN_SOURCE
libscrypt_memlimit_la_CFLAGS=`. ./apisupport-config.h; echo $${CFLAGS_NONPOSIX_MEMLIMIT}`

# Install libscrypt-kdf?
if LIBSCRYPT_KDF
Expand Down
19 changes: 19 additions & 0 deletions libcperciva/apisupport/Build/apisupport-NONPOSIX-MEMLIMIT.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "platform.h"

#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_SYS_SYSCTL_H
#include <sys/sysctl.h>
#endif
#ifdef HAVE_SYS_SYSINFO_H
#include <sys/sysinfo.h>
#endif

int
main(void)
{

/* Success! */
return (0);
}
85 changes: 85 additions & 0 deletions libcperciva/apisupport/Build/apisupport.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Should be sourced by `command -p sh path/to/apisupport.sh "$PATH"` from
# within a Makefile.
if ! [ "${PATH}" = "$1" ]; then
echo "WARNING: POSIX violation: ${SHELL}'s command -p resets \$PATH" 1>&2
PATH=$1
fi

# Standard output should be written to apisupport-config.h, which is both a
# C header file defining APISUPPORT_PLATFORM_FEATURE macros and sourceable sh
# code which sets CFLAGS_PLATFORM_FEATURE environment variables.
SRCDIR=$(command -p dirname "$0")

CFLAGS_HARDCODED="-D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700"

# Do we want to record stderr to a file?
if [ "${DEBUG:-0}" -eq "0" ]; then
outcc="/dev/null"
else
outcc="apisupport-stderr.log"
rm -f "${outcc}"
fi

feature() {
PLATFORM=$1
FEATURE=$2
EXTRALIB=$3
shift 3;

# Bail if we didn't include this feature in this source tree.
feature_filename="${SRCDIR}/apisupport-${PLATFORM}-${FEATURE}.c"
if ! [ -f "${feature_filename}" ]; then
return
fi

# Check if we can compile this feature (and any required arguments).
printf "Checking if compiler supports %s %s feature..." \
"${PLATFORM}" "${FEATURE}" 1>&2
for API_CFLAGS in "$@"; do
if ${CC} ${CPPFLAGS} ${CFLAGS} ${CFLAGS_HARDCODED} \
${API_CFLAGS} "${feature_filename}" ${LDADD_EXTRA} \
${EXTRALIB} 2>>"${outcc}"; then
rm -f a.out
break;
fi
API_CFLAGS=NOTSUPPORTED;
done
case ${API_CFLAGS} in
NOTSUPPORTED)
echo " no" 1>&2
;;
"")
echo " yes" 1>&2
echo "#define APISUPPORT_${PLATFORM}_${FEATURE} 1"
;;
*)
echo " yes, via ${API_CFLAGS}" 1>&2
echo "#define APISUPPORT_${PLATFORM}_${FEATURE} 1"
echo "#ifdef apisupport_dummy"
echo "export CFLAGS_${PLATFORM}_${FEATURE}=\"${API_CFLAGS}\""
echo "#endif"
;;
esac
}

if [ "$2" = "--all" ]; then
feature() {
PLATFORM=$1
FEATURE=$2
echo "#define APISUPPORT_${PLATFORM}_${FEATURE} 1"
}
fi

# Detect how to compile non-POSIX code.
feature NONPOSIX SETGROUPS "" "" \
"-U_POSIX_C_SOURCE -U_XOPEN_SOURCE" \
"-U_POSIX_C_SOURCE -U_XOPEN_SOURCE -Wno-reserved-id-macro"
feature NONPOSIX MEMLIMIT "" "" \
"-U_POSIX_C_SOURCE -U_XOPEN_SOURCE" \
"-U_POSIX_C_SOURCE -U_XOPEN_SOURCE -Wno-reserved-id-macro"

# Detect how to compile libssl and libcrypto code.
feature LIBSSL HOST_NAME "-lssl" "" \
"-Wno-cast-qual"
feature LIBCRYPTO LOW_LEVEL_AES "-lcrypto" "" \
"-Wno-deprecated-declarations"

0 comments on commit e584f2c

Please sign in to comment.