From d78ff4bd67b6bb944c3e06ff3a3f324f7b5eb4fe Mon Sep 17 00:00:00 2001
From: Florent Pruvost <florent.pruvost@inria.fr>
Date: Mon, 25 Jul 2022 16:23:28 +0200
Subject: [PATCH] Create a new library for chameleon blas/lapack api symbols to
 get things separated and clean. This avoids breaking users code using
 classical chameleon and cblas/lapacke beside (e.g. can cause redefinition of
 some cblas objects).

---
 CMakeLists.txt                                |   5 +
 cmake_modules/CHAMELEONConfig.cmake.in        |   1 +
 cmake_modules/GenPkgConfig.cmake              |   8 ++
 compute/CMakeLists.txt                        |  13 ---
 include/CMakeLists.txt                        |   1 -
 include/chameleon.h                           |   5 -
 lapack_api/CMakeLists.txt                     | 110 ++++++++++++++++++
 .../include}/chameleon/chameleon_zlapack.h    |   1 +
 lapack_api/include/chameleon_lapack.h         |  26 +++++
 .../include}/lapack_api_common.h              |   7 +-
 .../src}/lapack_api_common.c                  |   0
 .../src}/lapack_zgemm.c                       |   0
 lib/pkgconfig/chameleon_lapack.pc.in          |  23 ++++
 testing/CMakeLists.txt                        |   1 +
 testing/testings.h                            |   1 +
 15 files changed, 180 insertions(+), 22 deletions(-)
 create mode 100644 lapack_api/CMakeLists.txt
 rename {include => lapack_api/include}/chameleon/chameleon_zlapack.h (97%)
 create mode 100644 lapack_api/include/chameleon_lapack.h
 rename {compute/lapack_api => lapack_api/include}/lapack_api_common.h (83%)
 rename {compute/lapack_api => lapack_api/src}/lapack_api_common.c (100%)
 rename {compute/lapack_api => lapack_api/src}/lapack_zgemm.c (100%)
 create mode 100644 lib/pkgconfig/chameleon_lapack.pc.in

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4e2b1d40c..2c409a3fa 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -570,6 +570,11 @@ add_subdirectory(include)
 add_subdirectory(control)
 add_subdirectory(compute)
 
+###############################################################################
+# chameleon_lapack library #
+############################
+add_subdirectory(lapack_api)
+
 ###############################################################################
 # Executables and Tests #
 #########################
diff --git a/cmake_modules/CHAMELEONConfig.cmake.in b/cmake_modules/CHAMELEONConfig.cmake.in
index 4817a7aa0..c4b93d79f 100644
--- a/cmake_modules/CHAMELEONConfig.cmake.in
+++ b/cmake_modules/CHAMELEONConfig.cmake.in
@@ -86,3 +86,4 @@ if (@CHAMELEON_SCHED_STARPU@)
   include("${CMAKE_CURRENT_LIST_DIR}/chameleon_starpuTargets.cmake")
 endif()
 include("${CMAKE_CURRENT_LIST_DIR}/chameleonTargets.cmake")
+include("${CMAKE_CURRENT_LIST_DIR}/chameleon_lapackTargets.cmake")
diff --git a/cmake_modules/GenPkgConfig.cmake b/cmake_modules/GenPkgConfig.cmake
index 9488bb070..ba1fb73ee 100644
--- a/cmake_modules/GenPkgConfig.cmake
+++ b/cmake_modules/GenPkgConfig.cmake
@@ -80,29 +80,34 @@ ENDMACRO(CLEAN_LIB_LIST)
 MACRO(GENERATE_PKGCONFIG_FILE)
 
     # The definitions that should be given to users (change the API)
+    set(CHAMELEON_LAPACK_PKGCONFIG_DEFINITIONS "")
     set(CHAMELEON_PKGCONFIG_DEFINITIONS "")
     set(COREBLAS_PKGCONFIG_DEFINITIONS "")
     set(CUDABLAS_PKGCONFIG_DEFINITIONS "")
 
     # The link flags specific to this package and any required libraries
     # that don't support PkgConfig
+    set(CHAMELEON_LAPACK_PKGCONFIG_LIBS "-lchameleon_lapack")
     set(CHAMELEON_PKGCONFIG_LIBS "-lchameleon")
     set(COREBLAS_PKGCONFIG_LIBS  "-lcoreblas")
     set(CUDABLAS_PKGCONFIG_LIBS  "-lcudablas")
 
     # The link flags for private libraries required by this package but not
     # exposed to applications
+    set(CHAMELEON_LAPACK_PKGCONFIG_LIBS_PRIVATE "")
     set(CHAMELEON_PKGCONFIG_LIBS_PRIVATE "")
     set(COREBLAS_PKGCONFIG_LIBS_PRIVATE  "")
     set(CUDABLAS_PKGCONFIG_LIBS_PRIVATE  "")
 
     # A list of packages required by this package
+    set(CHAMELEON_LAPACK_PKGCONFIG_REQUIRED "chameleon")
     set(CHAMELEON_PKGCONFIG_REQUIRED "hqr")
     set(COREBLAS_PKGCONFIG_REQUIRED  "")
     set(CUDABLAS_PKGCONFIG_REQUIRED  "")
 
     # A list of private packages required by this package but not exposed to
     # applications
+    set(CHAMELEON_LAPACK_PKGCONFIG_REQUIRED_PRIVATE "")
     set(CHAMELEON_PKGCONFIG_REQUIRED_PRIVATE "")
     set(COREBLAS_PKGCONFIG_REQUIRED_PRIVATE  "")
     set(CUDABLAS_PKGCONFIG_REQUIRED_PRIVATE  "")
@@ -157,6 +162,7 @@ MACRO(GENERATE_PKGCONFIG_FILE)
 
     # Create .pc file
     # ---------------
+    SET(_output_chameleon_lapack_file "${CMAKE_BINARY_DIR}/chameleon_lapack.pc")
     SET(_output_chameleon_file "${CMAKE_BINARY_DIR}/chameleon.pc")
     SET(_output_coreblas_file "${CMAKE_BINARY_DIR}/coreblas.pc")
     if(CHAMELEON_USE_CUDA)
@@ -164,6 +170,7 @@ MACRO(GENERATE_PKGCONFIG_FILE)
     endif()
 
     # TODO: add url of CHAMELEON releases in .pc file
+    CONFIGURE_FILE("${CMAKE_CURRENT_SOURCE_DIR}/lib/pkgconfig/chameleon_lapack.pc.in" "${_output_chameleon_lapack_file}" @ONLY)
     CONFIGURE_FILE("${CMAKE_CURRENT_SOURCE_DIR}/lib/pkgconfig/chameleon.pc.in" "${_output_chameleon_file}" @ONLY)
     CONFIGURE_FILE("${CMAKE_CURRENT_SOURCE_DIR}/lib/pkgconfig/coreblas.pc.in"  "${_output_coreblas_file}" @ONLY)
     if(CHAMELEON_USE_CUDA)
@@ -172,6 +179,7 @@ MACRO(GENERATE_PKGCONFIG_FILE)
 
     # installation
     # ------------
+    INSTALL(FILES ${_output_chameleon_lapack_file} DESTINATION lib/pkgconfig)
     INSTALL(FILES ${_output_chameleon_file} DESTINATION lib/pkgconfig)
     INSTALL(FILES ${_output_coreblas_file}  DESTINATION lib/pkgconfig)
     INSTALL(FILES ${_output_cudablas_file}  DESTINATION lib/pkgconfig)
diff --git a/compute/CMakeLists.txt b/compute/CMakeLists.txt
index cda965e6b..815eb60bf 100644
--- a/compute/CMakeLists.txt
+++ b/compute/CMakeLists.txt
@@ -264,22 +264,10 @@ precisions_rules_py(CONTROL_SRCS_GENERATED "${ZSRC}"
                     PRECISIONS "${CHAMELEON_PRECISION}"
                     TARGETDIR "control" )
 
-set(LAPACK_API_SRCS_GENERATED "")
-set(ZSRC
-  lapack_api/lapack_zgemm.c
-  )
-precisions_rules_py(LAPACK_API_SRCS_GENERATED "${ZSRC}"
-  PRECISIONS "${CHAMELEON_PRECISION}")
-set(LAPACK_API_SRCS
-  ${LAPACK_API_SRCS_GENERATED}
-  lapack_api/lapack_api_common.c
-  )
-
 set(CHAMELEON_SRCS
     ${CHAMELEON_CONTROL}
     ${CHAMELEON_SRCS_GENERATED}
     ${CONTROL_SRCS_GENERATED}
-    ${LAPACK_API_SRCS}
    )
 
 # Generate the chameleon fortran sources for all possible precisions
@@ -321,7 +309,6 @@ add_dependencies(chameleon
 )
 
 target_include_directories(chameleon PUBLIC
-  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/lapack_api>
   $<BUILD_INTERFACE:${CHAMELEON_SOURCE_DIR}/include>
   $<BUILD_INTERFACE:${CHAMELEON_BINARY_DIR}/include>
   $<BUILD_INTERFACE:${CHAMELEON_SOURCE_DIR}/control>
diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt
index 004ea4ab1..19dce3852 100644
--- a/include/CMakeLists.txt
+++ b/include/CMakeLists.txt
@@ -32,7 +32,6 @@ set(CHAMELEON_HDRS_GENERATED "")
 set(ZHDR
   chameleon/chameleon_z.h
   chameleon/chameleon_zc.h
-  chameleon/chameleon_zlapack.h
   chameleon/tasks_z.h
   chameleon/tasks_zc.h
 )
diff --git a/include/chameleon.h b/include/chameleon.h
index d3f41a16e..175cb73f7 100644
--- a/include/chameleon.h
+++ b/include/chameleon.h
@@ -68,11 +68,6 @@ typedef struct gepdf_info_s {
 #include "chameleon/chameleon_zc.h"
 #include "chameleon/chameleon_ds.h"
 
-#include "chameleon/chameleon_zlapack.h"
-#include "chameleon/chameleon_clapack.h"
-#include "chameleon/chameleon_dlapack.h"
-#include "chameleon/chameleon_slapack.h"
-
 BEGIN_C_DECLS
 
 /* ****************************************************************************
diff --git a/lapack_api/CMakeLists.txt b/lapack_api/CMakeLists.txt
new file mode 100644
index 000000000..9917a0399
--- /dev/null
+++ b/lapack_api/CMakeLists.txt
@@ -0,0 +1,110 @@
+###
+#
+# @file CMakeLists.txt
+#
+# @copyright 2022-2022 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria,
+#                      Univ. Bordeaux. All rights reserved.
+#
+###
+#
+#  @project CHAMELEON
+#  CHAMELEON is a software package provided by:
+#     Inria Bordeaux - Sud-Ouest,
+#     Univ. of Tennessee,
+#     King Abdullah Univesity of Science and Technology
+#     Univ. of California Berkeley,
+#     Univ. of Colorado Denver.
+#
+# @version 1.2.0
+#  @author Mathieu Faverge
+#  @author Florent Pruvost
+#  @date 2022-07-25
+#
+###
+
+# Generate the chameleon headers for all possible precisions
+# ----------------------------------------------------------
+set(CHAMELEON_LAPACK_HDRS_GENERATED "")
+set(ZHDR
+  include/chameleon/chameleon_zlapack.h
+)
+
+precisions_rules_py(CHAMELEON_LAPACK_HDRS_GENERATED "${ZHDR}"
+  TARGETDIR  "include/chameleon"
+  PRECISIONS "z;c;d;s" )
+
+set(CHAMELEON_LAPACK_HDRS "")
+foreach( hdr_file ${CHAMELEON_LAPACK_HDRS_GENERATED} )
+  list(APPEND CHAMELEON_LAPACK_HDRS ${CMAKE_CURRENT_BINARY_DIR}/${hdr_file})
+endforeach()
+
+# Force generation of headers
+# ---------------------------
+add_custom_target(chameleon_lapack_include
+  ALL SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/include/chameleon_lapack.h
+              ${CHAMELEON_LAPACK_HDRS})
+set(CHAMELEON_SOURCES_TARGETS "${CHAMELEON_SOURCES_TARGETS};chameleon_lapack_include" CACHE INTERNAL "List of targets of sources")
+
+# Installation of headers
+# -----------------------
+install( FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/chameleon_lapack.h
+         DESTINATION include )
+install( FILES ${CHAMELEON_LAPACK_HDRS}
+         DESTINATION include/chameleon )
+
+# Define the list of sources
+# --------------------------
+set(LAPACK_SRCS_GENERATED "")
+set(ZSRC
+  src/lapack_zgemm.c
+  )
+precisions_rules_py(LAPACK_SRCS_GENERATED "${ZSRC}"
+  PRECISIONS "${CHAMELEON_PRECISION}")
+set(CHAMELEON_LAPACK_SRCS
+  ${LAPACK_SRCS_GENERATED}
+  src/lapack_api_common.c
+  )
+
+# Force generation of sources
+# ---------------------------
+add_custom_target(chameleon_lapack_sources ALL SOURCES "${CHAMELEON_LAPACK_SRCS}")
+set(CHAMELEON_SOURCES_TARGETS "${CHAMELEON_SOURCES_TARGETS};chameleon_lapack_sources" CACHE INTERNAL "List of targets of sources")
+
+# Define library
+# --------------
+add_library(chameleon_lapack ${CHAMELEON_LAPACK_SRCS})
+set_target_properties(chameleon_lapack PROPERTIES VERSION ${CHAMELEON_VERSION})
+set_target_properties(chameleon_lapack PROPERTIES SOVERSION ${CHAMELEON_VERSION_MAJOR})
+
+# Define dependencies
+# -------------------
+target_include_directories(chameleon_lapack PUBLIC
+  $<BUILD_INTERFACE:${CHAMELEON_SOURCE_DIR}/lapack_api/include>
+  $<BUILD_INTERFACE:${CHAMELEON_BINARY_DIR}/lapack_api/include>
+  $<INSTALL_INTERFACE:include>)
+target_link_libraries(chameleon_lapack PUBLIC chameleon)
+if (CHAMELEON_SIMULATION)
+  target_include_directories(chameleon_lapack PUBLIC $<BUILD_INTERFACE:${CHAMELEON_SOURCE_DIR}/coreblas/include>)
+else()
+  target_link_libraries(chameleon_lapack PUBLIC coreblas)
+endif()
+set_property(TARGET chameleon_lapack PROPERTY INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/lib")
+
+# Export target
+# -------------
+install(EXPORT chameleon_lapackTargets
+        NAMESPACE CHAMELEON::
+        DESTINATION lib/cmake/chameleon
+        )
+
+# Installation of library and cmake target
+# ----------------------------------------
+install(TARGETS chameleon_lapack
+        EXPORT chameleon_lapackTargets
+        ARCHIVE DESTINATION lib
+        LIBRARY DESTINATION lib
+        )
+
+###
+### END CMakeLists.txt
+###
diff --git a/include/chameleon/chameleon_zlapack.h b/lapack_api/include/chameleon/chameleon_zlapack.h
similarity index 97%
rename from include/chameleon/chameleon_zlapack.h
rename to lapack_api/include/chameleon/chameleon_zlapack.h
index 761fed935..e201dbe44 100644
--- a/include/chameleon/chameleon_zlapack.h
+++ b/lapack_api/include/chameleon/chameleon_zlapack.h
@@ -19,6 +19,7 @@
 #ifndef _chameleon_zlapack_h_
 #define _chameleon_zlapack_h_
 
+#include "chameleon.h"
 #include "coreblas/cblas_wrapper.h"
 
 BEGIN_C_DECLS
diff --git a/lapack_api/include/chameleon_lapack.h b/lapack_api/include/chameleon_lapack.h
new file mode 100644
index 000000000..1df1c259a
--- /dev/null
+++ b/lapack_api/include/chameleon_lapack.h
@@ -0,0 +1,26 @@
+/**
+ *
+ * @file chameleon_lapack.h
+ *
+ * @copyright 2022-2022 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria,
+ *                      Univ. Bordeaux. All rights reserved.
+ *
+ ***
+ *
+ * @brief Chameleon blas/lapack and cblas/lapack api functions
+ *
+ * @version 1.2.0
+ * @author Mathieu Faverge
+ * @author Florent Pruvost
+ * @date 2022-07-25
+ *
+ */
+#ifndef _chameleon_lapack_h_
+#define _chameleon_lapack_h_
+
+#include "chameleon/chameleon_slapack.h"
+#include "chameleon/chameleon_dlapack.h"
+#include "chameleon/chameleon_clapack.h"
+#include "chameleon/chameleon_zlapack.h"
+
+#endif /* _chameleon_lapack_h_ */
diff --git a/compute/lapack_api/lapack_api_common.h b/lapack_api/include/lapack_api_common.h
similarity index 83%
rename from compute/lapack_api/lapack_api_common.h
rename to lapack_api/include/lapack_api_common.h
index 25eb831a0..347b085b6 100644
--- a/compute/lapack_api/lapack_api_common.h
+++ b/lapack_api/include/lapack_api_common.h
@@ -18,9 +18,10 @@
 #ifndef _lapack_api_common_h_
 #define _lapack_api_common_h_
 
-#include "chameleon.h"
-#include "chameleon/mangling.h"
-#include "control/auxiliary.h"
+#include <chameleon.h>
+#include <chameleon/mangling.h>
+#include <control/auxiliary.h>
+#include <coreblas/cblas_wrapper.h>
 
 /**
  *
diff --git a/compute/lapack_api/lapack_api_common.c b/lapack_api/src/lapack_api_common.c
similarity index 100%
rename from compute/lapack_api/lapack_api_common.c
rename to lapack_api/src/lapack_api_common.c
diff --git a/compute/lapack_api/lapack_zgemm.c b/lapack_api/src/lapack_zgemm.c
similarity index 100%
rename from compute/lapack_api/lapack_zgemm.c
rename to lapack_api/src/lapack_zgemm.c
diff --git a/lib/pkgconfig/chameleon_lapack.pc.in b/lib/pkgconfig/chameleon_lapack.pc.in
new file mode 100644
index 000000000..9dafd7379
--- /dev/null
+++ b/lib/pkgconfig/chameleon_lapack.pc.in
@@ -0,0 +1,23 @@
+#
+#  @file chameleon_lapack.pc
+#
+#  @copyright 2022-2022 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria,
+#                       Univ. Bordeaux. All rights reserved.
+#
+#  @version 1.2.0
+#  @author Florent Pruvost
+#  @date 2022-07-25
+#
+prefix=@CMAKE_INSTALL_PREFIX@
+exec_prefix=${prefix}
+libdir=${exec_prefix}/lib
+includedir=${exec_prefix}/include
+
+Name: chameleon_lapack
+Description: Dense Linear Algebra for Scalable Multi-core Architectures and GPGPUs
+Version: @CHAMELEON_VERSION@
+Cflags: @CHAMELEON_LAPACK_PKGCONFIG_DEFINITIONS@ -I${includedir}
+Libs: -L${libdir} @CHAMELEON_LAPACK_PKGCONFIG_LIBS@
+Libs.private: @CHAMELEON_LAPACK_PKGCONFIG_LIBS_PRIVATE@
+Requires: @CHAMELEON_LAPACK_PKGCONFIG_REQUIRED@
+Requires.private: @CHAMELEON_LAPACK_PKGCONFIG_REQUIRED_PRIVATE@
diff --git a/testing/CMakeLists.txt b/testing/CMakeLists.txt
index a2216fb2d..ae8d74f73 100644
--- a/testing/CMakeLists.txt
+++ b/testing/CMakeLists.txt
@@ -167,6 +167,7 @@ foreach(_precision ${CHAMELEON_PRECISION} )
     ${CHAMELEON_BINARY_DIR}/coreblas/include
     )
 
+  target_link_libraries(${__target_name} PRIVATE chameleon_lapack)
   target_link_libraries(${__target_name} PRIVATE chameleon)
   if(NOT CHAMELEON_SIMULATION)
     target_link_libraries(${__target_name} PRIVATE
diff --git a/testing/testings.h b/testing/testings.h
index 5326b1922..0b57e9249 100644
--- a/testing/testings.h
+++ b/testing/testings.h
@@ -18,6 +18,7 @@
 #ifndef _testings_h_
 #define _testings_h_
 
+#include <chameleon_lapack.h>
 #include <chameleon.h>
 #include <stdlib.h>
 #include <stdio.h>
-- 
GitLab