# $NetBSD: Makefile.common,v 1.9 2024/02/17 10:13:19 adam Exp $
#
# used by math/openblas_pthread/Makefile
# used by math/openblas_openmp/Makefile
# used by math/openblas/Makefile
# used by math/openblas64_pthread/Makefile
# used by math/openblas64_openmp/Makefile
# used by math/openblas64/Makefile
#
# This is the common Makefile for OpenBLAS builds. It depends on
# OPENBLAS_VARIANT = (openblas|openblas_pthread|openblas_openmp)
# being set.

DISTNAME=	OpenBLAS-${OPENBLAS_VERSION}
PKGNAME=	${OPENBLAS_VARIANT}-${OPENBLAS_VERSION}
CATEGORIES=	math
MASTER_SITES=	${MASTER_SITE_GITHUB:=OpenMathLib/}
GITHUB_PROJECT=	OpenBLAS
GITHUB_RELEASE=	v${OPENBLAS_VERSION}

DISTINFO_FILE=	${.CURDIR}/../../math/openblas/distinfo
PATCHDIR=	${.CURDIR}/../../math/openblas/patches

MAINTAINER=	thomas.orgis@uni-hamburg.de
HOMEPAGE=	https://github.com/OpenMathLib/OpenBLAS
COMMENT=	Optimized BLAS library based on GotoBLAS2 (variant ${OPENBLAS_VARIANT})
LICENSE=	modified-bsd

# Untested on other platforms
ONLY_FOR_PLATFORM=	Darwin-*-* Linux-*-* NetBSD-*-*

USE_LANGUAGES=	c fortran
USE_TOOLS+=	gmake perl:build
NO_CONFIGURE=	yes
TEST_TARGET=	tests

# Also set compilers explicitly, overriding Makefile.rule
MAKE_FLAGS+=	CC=${CC:Q}
MAKE_FLAGS+=	CXX=${CXX:Q}
MAKE_FLAGS+=	FC=${FC:Q}

# You may override the autodetected thread count limit.
# Default is detected from build host, which may differ from run host.
BUILD_DEFS+=	OPENBLAS_THREAD_LIMIT

.include "../../mk/bsd.prefs.mk"

# Using conservative default of 32 threads as that matches the
# limit for related stack arrays in OpenBLAS. Might raise it
# in future, anyway. There some memory usage/efficiency tradeoff
# when this number is excessively high.
OPENBLAS_THREAD_LIMIT?=	32
.if !empty(OPENBLAS_THREAD_LIMIT)
MAKE_FLAGS+=		NUM_THREADS=${OPENBLAS_THREAD_LIMIT:Q}
.endif
BENCHMARK_MAXTHREADS?=	8

.if empty(LP64PLATFORMS:@.PLAT.@${MACHINE_PLATFORM:M${.PLAT.}}@)
MAKE_FLAGS+=		BINARY32=1
.  if !empty(OPENBLAS_VARIANT:M*64*)
PKG_FAIL_REASON+=	"${OPENBLAS_VARIANT} incompatible with non-64-bit platform"
.  endif
.else
MAKE_FLAGS+=		BINARY64=1
.endif

# Build with the C APIs, as do other optimized libraries.
#MAKE_FLAGS+=		NO_CBLAS=0 NO_LAPACKE=0

# Separate headers and cmake stuff.
MAKE_FLAGS+=	OPENBLAS_INCLUDE_DIR=${PREFIX}/include/${OPENBLAS_VARIANT}
MAKE_FLAGS+=	OPENBLAS_CMAKE_DIR=${PREFIX}/lib/cmake/${OPENBLAS_VARIANT}

# Settled on no sumbol suffix for 64 bit.
MAKE_FLAGS+=	FIXED_LIBNAME=1
.if ${OPENBLAS_VARIANT} == openblas || ${OPENBLAS_VARIANT} == openblas64
MAKE_FLAGS+=	USE_OPENMP=0 USE_THREAD=0 USE_LOCKING=1
BENCHMARK_ENV=	USE_SIMPLE_THREADED_LEVEL3=1
.elif !empty(OPENBLAS_VARIANT:Mopenblas*_pthread)
MAKE_FLAGS+=	LIBNAMESUFFIX=pthread
MAKE_FLAGS+=	USE_OPENMP=0 USE_THREAD=1
BENCHMARK_ENV=	OPENBLAS_NUM_THREADS=${BENCHMARK_MAXTHREADS}
.elif !empty(OPENBLAS_VARIANT:Mopenblas*_openmp)
MAKE_FLAGS+=	LIBNAMESUFFIX=openmp
MAKE_FLAGS+=	USE_OPENMP=1 NO_AFFINITY=1
BENCHMARK_ENV=	OMP_NUM_THREADS=${BENCHMARK_MAXTHREADS}
.endif

.if !empty(OPENBLAS_VARIANT:M*64*)
MAKE_FLAGS+=	INTERFACE64=1 LIBSONAMEBASE=openblas64
.endif

SUBST_CLASSES+=		threads
SUBST_STAGE.threads=	pre-build
SUBST_SED.threads+=	-e "s+OPENBLAS_NUM_THREADS+OMP_NUM_THREADS+g"
SUBST_FILES.threads+=	test/Makefile
SUBST_FILES.threads+=	ctest/Makefile

PLIST_SRC=	${.CURDIR}/../../math/openblas/PLIST
PLIST_SUBST+=	OPENBLAS_VARIANT=${OPENBLAS_VARIANT}

# This builds some benchmark programs you can call from ${WRKSRC} afterwards.
.PHONY: benchmark
benchmark: build
	(cd ${WRKSRC} && ${SETENV} ${MAKE_ENV} ${BENCHMARK_ENV} \
		${MAKE_PROGRAM} ${MAKE_FLAGS} hpl)
	(cd ${WRKSRC}/benchmark && ${SETENV} ${MAKE_ENV} \
		${BENCHMARK_ENV} ${MAKE_PROGRAM} ${MAKE_FLAGS})

.include "../../math/openblas/version.mk"

.include "options.mk"
