From 0c442318196389d653ee21eba65d8c4f7beb72a0 Mon Sep 17 00:00:00 2001
From: Eli Schwartz <eschwartz@archlinux.org>
Date: Fri, 5 Oct 2018 15:52:17 +0000
Subject: [PATCH] Use a dedicated cblas library, that may or may not be in fact
 the blas one.

Openblas can be built with statically compiled convenience copies of
cblas, but if not, then the system libcblas.so should be used.
---
 Make.inc                         | 12 +++++++++++-
 Makefile                         |  3 +++
 base/Makefile                    |  4 ++++
 stdlib/LinearAlgebra/src/blas.jl | 15 +++++++++++++--
 4 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/Make.inc b/Make.inc
index b00a41b356d8..7bc6cd69e863 100644
--- a/Make.inc
+++ b/Make.inc
@@ -1059,6 +1059,7 @@ endif
 ifeq ($(USE_SYSTEM_BLAS), 1)
 ifeq ($(OS), Darwin)
 USE_BLAS64 := 0
+USE_SYSTEM_CBLAS := 0
 USE_SYSTEM_LAPACK := 0
 LIBBLAS := -L$(build_libdir) -lgfortblas
 LIBBLASNAME := libgfortblas
@@ -1071,12 +1072,21 @@ LIBBLAS := -L$(build_shlibdir) -lopenblas
 LIBBLASNAME := libopenblas
 endif
 
-# OpenBLAS builds LAPACK as part of its build.
+# OpenBLAS builds cblas/LAPACK as part of its build.
 # We only need to build LAPACK if we are not using OpenBLAS.
 ifeq ($(USE_SYSTEM_BLAS), 0)
+LIBCBLAS := $(LIBBLAS)
+LIBCBLASNAME := $(LIBBLASNAME)
 LIBLAPACK := $(LIBBLAS)
 LIBLAPACKNAME := $(LIBBLASNAME)
 else
+ifeq ($(USE_SYSTEM_CBLAS), 1)
+LIBCBLAS ?= -lcblas
+LIBCBLASNAME ?= libcblas
+else
+LIBCBLAS := -L$(build_shlibdir) -lcblas $(LIBBLAS)
+LIBCBLASNAME := libcblas
+endif
 ifeq ($(USE_SYSTEM_LAPACK), 1)
 LIBLAPACK ?= -llapack
 LIBLAPACKNAME ?= liblapack
diff --git a/Makefile b/Makefile
index 6063e79ae956..7df60b8170d6 100644
--- a/Makefile
+++ b/Makefile
@@ -199,6 +199,9 @@ endif
 endif
 
 JL_PRIVATE_LIBS-$(USE_SYSTEM_BLAS) += $(LIBBLASNAME)
+ifneq ($(LIBCBLASNAME),$(LIBBLASNAME))
+JL_PRIVATE_LIBS-$(USE_SYSTEM_CBLAS) += $(LIBCBLASNAME)
+endif
 ifneq ($(LIBLAPACKNAME),$(LIBBLASNAME))
 JL_PRIVATE_LIBS-$(USE_SYSTEM_LAPACK) += $(LIBLAPACKNAME)
 endif
diff --git a/base/Makefile b/base/Makefile
index 70e6da933d70..8ecfa6902b59 100644
--- a/base/Makefile
+++ b/base/Makefile
@@ -48,6 +48,7 @@ else
 endif
 	@echo "const libm_name = \"$(LIBMNAME)\"" >> $@
 	@echo "const libblas_name = \"$(LIBBLASNAME)\"" >> $@
+	@echo "const libcblas_name = \"$(LIBCBLASNAME)\"" >> $@
 	@echo "const liblapack_name = \"$(LIBLAPACKNAME)\"" >> $@
 ifeq ($(USE_BLAS64), 1)
 	@echo "const USE_BLAS64 = true" >> $@
@@ -224,6 +225,9 @@ endif
 $(eval $(call symlink_system_library,DSFMT,libdSFMT))
 $(eval $(call symlink_system_library,LIBBLASTRAMPOLINE,libblastrampoline))
 $(eval $(call symlink_system_library,BLAS,$(LIBBLASNAME)))
+ifneq ($(LIBCBLASNAME),$(LIBBLASNAME))
+$(eval $(call symlink_system_library,CBLAS,$(LIBCBLASNAME)))
+endif
 ifneq ($(LIBLAPACKNAME),$(LIBBLASNAME))
 $(eval $(call symlink_system_library,LAPACK,$(LIBLAPACKNAME)))
 endif
diff --git a/stdlib/LinearAlgebra/src/LinearAlgebra.jl b/stdlib/LinearAlgebra/src/LinearAlgebra.jl
index 9e1e751262..b1e5450241 100644
--- a/stdlib/LinearAlgebra/src/LinearAlgebra.jl
+++ b/stdlib/LinearAlgebra/src/LinearAlgebra.jl
@@ -565,12 +565,17 @@ end
 function __init__()
     try
         libblas_path = find_library_path(Base.libblas_name)
+        libcblas_path = find_library_path(Base.libcblas_name)
         liblapack_path = find_library_path(Base.liblapack_name)
         # We manually `dlopen()` these libraries here, so that we search with `libjulia-internal`'s
         # `RPATH` and not `libblastrampoline's`.  Once it's been opened, when LBT tries to open it,
         # it will find the library already loaded.
         libblas_path = Libdl.dlpath(Libdl.dlopen(libblas_path))
         BLAS.lbt_forward(libblas_path; clear=true)
+        if libcblas_path != libblas_path
+            libcblas_path = Libdl.dlpath(Libdl.dlopen(libcblas_path))
+            BLAS.lbt_forward(libcblas_path)
+        end
         if liblapack_path != libblas_path
             liblapack_path = Libdl.dlpath(Libdl.dlopen(liblapack_path))
             BLAS.lbt_forward(liblapack_path)
