From d909c6d89d382773152d0c5320babb98d665296a Mon Sep 17 00:00:00 2001
From: Benjamin Fovet <45510885+bfovet@users.noreply.github.com>
Date: Fri, 27 Mar 2020 20:29:49 +0100
Subject: [PATCH] Add new package CMinpack (#15606)

* Add new package CMinpack

* Add link to CMinpack PR#21
---
 .../cminpack/link_with_blas_pr_21.patch       | 267 ++++++++++++++++++
 .../builtin/packages/cminpack/package.py      |  39 +++
 2 files changed, 306 insertions(+)
 create mode 100644 var/spack/repos/builtin/packages/cminpack/link_with_blas_pr_21.patch
 create mode 100644 var/spack/repos/builtin/packages/cminpack/package.py

diff --git a/var/spack/repos/builtin/packages/cminpack/link_with_blas_pr_21.patch b/var/spack/repos/builtin/packages/cminpack/link_with_blas_pr_21.patch
new file mode 100644
index 0000000000..a28d67ea0f
--- /dev/null
+++ b/var/spack/repos/builtin/packages/cminpack/link_with_blas_pr_21.patch
@@ -0,0 +1,267 @@
+From 3b386a0ed507a9923f942098a4dcf2df2bcde7d7 Mon Sep 17 00:00:00 2001
+From: Michel Zou <xantares09@hotmail.com>
+Date: Thu, 6 Dec 2018 23:38:07 +0100
+Subject: [PATCH] cmake to link to cblas
+
+---
+ CMakeLists.txt        |  30 ++++---
+ cmake/CMakeLists.txt  |   2 +-
+ cmake/FindCBLAS.cmake | 180 ++++++++++++++++++++++++++++++++++++++++++
+ 3 files changed, 201 insertions(+), 11 deletions(-)
+ create mode 100644 cmake/FindCBLAS.cmake
+
+diff --git a/CMakeLists.txt b/CMakeLists.txt
+index 6c8d16b..f22039f 100644
+--- a/CMakeLists.txt
++++ b/CMakeLists.txt
+@@ -5,6 +5,8 @@ cmake_minimum_required (VERSION 2.8.9)
+ project (CMINPACK)
+ string(TOLOWER ${PROJECT_NAME} PROJECT_NAME_LOWER)
+ 
++set (CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
++
+ include(${PROJECT_SOURCE_DIR}/cmake/cminpack_utils.cmake)
+ # Set version and OS-specific settings
+ set(CMINPACK_VERSION 1.3.6 CACHE STRING "CMinpack version")
+@@ -34,7 +36,7 @@ else ()
+   endif(WIN32)
+ endif ()
+ 
+-option(USE_BLAS "Compile cminpack using a blas library if possible" ON)
++option(USE_BLAS "Compile cminpack using cblas library if possible" ON)
+ 
+ #set(CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR}/../build)
+ 
+@@ -61,16 +63,24 @@ if (${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")
+   TARGET_LINK_LIBRARIES(cminpack m)
+ endif()
+ 
+-# Link with a BLAS library if requested
+-if (USE_BLAS)
+-  if (NOT BUILD_SHARED_LIBS)
+-    set(BLA_STATIC True)
++
++include (CheckLibraryExists)
++include (CheckFunctionExists)
++check_function_exists (sqrt HAVE_SQRT_NO_LIBM)
++if (NOT HAVE_SQRT_NO_LIBM)
++  check_library_exists ("m" sqrt "" HAVE_LIBM)
++  if (HAVE_LIBM)
++    target_link_libraries(cminpack PUBLIC m)
+   endif()
+-  find_package(BLAS)
+-  if (BLAS_FOUND)
+-    target_link_libraries(cminpack PUBLIC ${BLAS_LIBRARIES})
+-    set_target_properties(cminpack PROPERTIES LINK_FLAGS "${BLAS_LINKER_FLAGS}")
+-    target_compile_definitions(cminpack PUBLIC -DUSE_CBLAS)
++endif ()
++
++# Link with CBLAS library if requested
++if (USE_BLAS)
++  find_package (CBLAS)
++  if (CBLAS_FOUND)
++    target_link_libraries(cminpack PUBLIC ${CBLAS_LIBRARIES})
++    set_target_properties(cminpack PROPERTIES LINK_FLAGS "${CBLAS_LINKER_FLAGS}")
++    target_compile_definitions(cminpack PUBLIC USE_CBLAS)
+   endif()
+ endif()
+ 
+diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt
+index 058a89b..d8277bc 100644
+--- a/cmake/CMakeLists.txt
++++ b/cmake/CMakeLists.txt
+@@ -1,7 +1,7 @@
+ set(PKG_DESC "CMinPack")
+ set(PKG_EXTERNAL_DEPS "")
+ set(pkg_conf_file ${CMAKE_CURRENT_BINARY_DIR}/cminpack.pc)
+-if (USE_BLAS AND BLAS_FOUND)
++if (USE_BLAS AND CBLAS_FOUND)
+     set(PC_CMINPACK_CFLAGS "-DUSE_CBLAS")
+     if (NOT "${BLAS_LIBRARIES}" STREQUAL "")
+         string(REPLACE ";" " -l" PC_CMINPACK_LIBRARIES "${BLAS_LIBRARIES}")
+diff --git a/cmake/FindCBLAS.cmake b/cmake/FindCBLAS.cmake
+new file mode 100644
+index 0000000..e4fb422
+--- /dev/null
++++ b/cmake/FindCBLAS.cmake
+@@ -0,0 +1,180 @@
++# - Find CBLAS library
++#
++# This module finds an installed fortran library that implements the CBLAS 
++# linear-algebra interface (see http://www.netlib.org/blas/), with CBLAS
++# interface.
++#
++# This module sets the following variables:
++#  CBLAS_FOUND - set to true if a library implementing the CBLAS interface is found
++#  CBLAS_LIBRARIES - list of libraries (using full path name) to link against to use CBLAS
++#  CBLAS_INCLUDE_DIR - path to includes
++#  CBLAS_INCLUDE_FILE - the file to be included to use CBLAS
++#
++
++SET(CBLAS_LIBRARIES)
++SET(CBLAS_INCLUDE_DIR)
++SET(CBLAS_INCLUDE_FILE)
++
++# CBLAS in Intel mkl
++FIND_PACKAGE(MKL)
++IF (MKL_FOUND AND NOT CBLAS_LIBRARIES)
++  SET(CBLAS_LIBRARIES ${MKL_LIBRARIES})
++  SET(CBLAS_INCLUDE_DIR ${MKL_INCLUDE_DIR})
++  SET(CBLAS_INCLUDE_FILE "mkl_cblas.h")
++ENDIF (MKL_FOUND AND NOT CBLAS_LIBRARIES)
++
++# Old CBLAS search 
++SET(_verbose TRUE)
++INCLUDE(CheckFunctionExists)
++INCLUDE(CheckIncludeFile)
++
++MACRO(CHECK_ALL_LIBRARIES LIBRARIES _prefix _name _flags _list _include _search_include)
++  # This macro checks for the existence of the combination of fortran libraries
++  # given by _list.  If the combination is found, this macro checks (using the 
++  # Check_Fortran_Function_Exists macro) whether can link against that library
++  # combination using the name of a routine given by _name using the linker
++  # flags given by _flags.  If the combination of libraries is found and passes
++  # the link test, LIBRARIES is set to the list of complete library paths that
++  # have been found.  Otherwise, LIBRARIES is set to FALSE.
++  # N.B. _prefix is the prefix applied to the names of all cached variables that
++  # are generated internally and marked advanced by this macro.
++  SET(__list)
++  FOREACH(_elem ${_list})
++    IF(__list)
++      SET(__list "${__list} - ${_elem}")
++    ELSE(__list)
++      SET(__list "${_elem}")
++    ENDIF(__list)
++  ENDFOREACH(_elem)
++  IF(_verbose)
++    MESSAGE(STATUS "Checking for [${__list}]")
++  ENDIF(_verbose)
++  SET(_libraries_work TRUE)
++  SET(${LIBRARIES})
++  SET(_combined_name)
++  SET(_paths)
++  FOREACH(_library ${_list})
++    SET(_combined_name ${_combined_name}_${_library})
++    # did we find all the libraries in the _list until now?
++    # (we stop at the first unfound one)
++    IF(_libraries_work)      
++      IF(APPLE) 
++        FIND_LIBRARY(${_prefix}_${_library}_LIBRARY
++          NAMES ${_library}
++          PATHS /usr/local/lib /usr/lib /usr/local/lib64 /usr/lib64 ENV 
++          DYLD_LIBRARY_PATH 
++          )
++      ELSE(APPLE)
++        FIND_LIBRARY(${_prefix}_${_library}_LIBRARY
++          NAMES ${_library}
++          PATHS /usr/local/lib /usr/lib /usr/local/lib64 /usr/lib64 ENV 
++          LD_LIBRARY_PATH 
++          )
++      ENDIF(APPLE)
++      MARK_AS_ADVANCED(${_prefix}_${_library}_LIBRARY)
++      IF(${_prefix}_${_library}_LIBRARY)
++        GET_FILENAME_COMPONENT(_path ${${_prefix}_${_library}_LIBRARY} PATH)
++        LIST(APPEND _paths ${_path}/../include ${_path}/../../include)
++      ENDIF(${_prefix}_${_library}_LIBRARY)
++      SET(${LIBRARIES} ${${LIBRARIES}} ${${_prefix}_${_library}_LIBRARY})
++      SET(_libraries_work ${${_prefix}_${_library}_LIBRARY})
++    ENDIF(_libraries_work)
++  ENDFOREACH(_library ${_list})
++  # Test include
++  SET(_bug_search_include ${_search_include}) #CMAKE BUG!!! SHOULD NOT BE THAT
++  IF(_bug_search_include)
++    FIND_PATH(${_prefix}${_combined_name}_INCLUDE ${_include} ${_paths})
++    MARK_AS_ADVANCED(${_prefix}${_combined_name}_INCLUDE)
++    IF(${_prefix}${_combined_name}_INCLUDE)
++      IF (_verbose)
++        MESSAGE(STATUS "Includes found")
++      ENDIF (_verbose)
++      SET(${_prefix}_INCLUDE_DIR ${${_prefix}${_combined_name}_INCLUDE})
++      SET(${_prefix}_INCLUDE_FILE ${_include})
++    ELSE(${_prefix}${_combined_name}_INCLUDE)
++      SET(_libraries_work FALSE)
++    ENDIF(${_prefix}${_combined_name}_INCLUDE)
++  ELSE(_bug_search_include)
++    SET(${_prefix}_INCLUDE_DIR)
++    SET(${_prefix}_INCLUDE_FILE ${_include})
++  ENDIF(_bug_search_include)
++  # Test this combination of libraries.
++  IF(_libraries_work)
++    SET(CMAKE_REQUIRED_LIBRARIES ${_flags} ${${LIBRARIES}})
++    CHECK_FUNCTION_EXISTS(${_name} ${_prefix}${_combined_name}_WORKS)
++    SET(CMAKE_REQUIRED_LIBRARIES)
++    MARK_AS_ADVANCED(${_prefix}${_combined_name}_WORKS)
++    SET(_libraries_work ${${_prefix}${_combined_name}_WORKS})
++    IF(_verbose AND _libraries_work)
++      MESSAGE(STATUS "Libraries found")
++    ENDIF(_verbose AND _libraries_work)
++  ENDIF(_libraries_work)
++  # Fin
++  IF(NOT _libraries_work)
++    SET(${LIBRARIES} NOTFOUND)
++  ENDIF(NOT _libraries_work)
++ENDMACRO(CHECK_ALL_LIBRARIES)
++
++# Generic CBLAS library
++IF(NOT CBLAS_LIBRARIES)
++  CHECK_ALL_LIBRARIES(
++    CBLAS_LIBRARIES
++    CBLAS
++    cblas_dgemm
++    ""
++    "cblas"
++    "cblas.h"
++    TRUE )
++ENDIF()
++
++# CBLAS in ATLAS library? (http://math-atlas.sourceforge.net/)
++IF(NOT CBLAS_LIBRARIES)
++  CHECK_ALL_LIBRARIES(
++    CBLAS_LIBRARIES
++    CBLAS
++    cblas_dgemm
++    ""
++    "cblas;atlas"
++    "cblas.h"
++    TRUE )
++ENDIF()
++
++# CBLAS in BLAS library
++IF(NOT CBLAS_LIBRARIES)
++  CHECK_ALL_LIBRARIES(
++    CBLAS_LIBRARIES
++    CBLAS
++    cblas_dgemm
++    ""
++    "blas"
++    "cblas.h"
++    TRUE )
++ENDIF()
++
++# Apple CBLAS library?
++IF(NOT CBLAS_LIBRARIES)
++  CHECK_ALL_LIBRARIES(
++    CBLAS_LIBRARIES
++    CBLAS
++    cblas_dgemm
++    ""
++    "Accelerate"
++    "Accelerate/Accelerate.h"
++    FALSE )
++ENDIF()
++
++IF( NOT CBLAS_LIBRARIES )
++  CHECK_ALL_LIBRARIES(
++    CBLAS_LIBRARIES
++    CBLAS
++    cblas_dgemm
++    ""
++    "vecLib"
++    "vecLib/vecLib.h"
++    FALSE )
++ENDIF()
++
++include ( FindPackageHandleStandardArgs )
++find_package_handle_standard_args ( CBLAS DEFAULT_MSG CBLAS_LIBRARIES
++)
++
diff --git a/var/spack/repos/builtin/packages/cminpack/package.py b/var/spack/repos/builtin/packages/cminpack/package.py
new file mode 100644
index 0000000000..e269accabf
--- /dev/null
+++ b/var/spack/repos/builtin/packages/cminpack/package.py
@@ -0,0 +1,39 @@
+# Copyright 2013-2020 Lawrence Livermore National Security, LLC and other
+# Spack Project Developers. See the top-level COPYRIGHT file for details.
+#
+# SPDX-License-Identifier: (Apache-2.0 OR MIT)
+
+from spack import *
+
+
+class Cminpack(CMakePackage):
+    """This is a C version of the minpack minimization package.
+    Minpack includes software for solving nonlinear equations
+    and nonlinear least squares problems.
+    """
+
+    homepage = "http://devernay.free.fr/hacks/cminpack"
+    url      = "https://github.com/devernay/cminpack/archive/v1.3.6.tar.gz"
+    git      = 'https://github.com/devernay/cminpack.git'
+
+    version('master', branch='master')
+    version('1.3.6', sha256='3c07fd21308c96477a2c900032e21d937739c233ee273b4347a0d4a84a32d09f')
+
+    variant('shared', default=False, description='Build shared libraries')
+    variant('blas', default=True, description='Compile with BLAS')
+
+    depends_on('blas', when='+blas')
+
+    # Backport a pull request for correctly linking blas.
+    # See https://github.com/devernay/cminpack/pull/21
+    patch('link_with_blas_pr_21.patch', when='@:1.3.6')
+
+    def cmake_args(self):
+        args = [
+            '-DBUILD_SHARED_LIBS=%s' % (
+                'ON' if '+shared' in self.spec else 'OFF'),
+            '-DUSE_BLAS=%s' % (
+                'ON' if 'blas' in self.spec else 'OFF')
+        ]
+
+        return args
-- 
GitLab