From 8546076c470f8af8014418e169819302cd7d1f66 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Del=20Pino?= <stephane.delpino44@gmail.com>
Date: Tue, 13 Mar 2018 00:40:09 +0100
Subject: [PATCH] CMake improvements - a bit of clean-up - now uses kokkos
 compiler's flag

In a (far?) future should work on deactivation of Kokkos and
eventually use Kokkos as a TPL
---
 CMakeLists.txt                     | 40 ++++++++++-----------------
 cmake/CheckNotInSources.cmake      | 28 +++++++++++++++++++
 cmake/GetKokkosCompilerFlags.cmake | 43 ++++++++++++++++++++++++++++++
 3 files changed, 85 insertions(+), 26 deletions(-)
 create mode 100644 cmake/CheckNotInSources.cmake
 create mode 100644 cmake/GetKokkosCompilerFlags.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 977985700..22b35dd8c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,38 +1,26 @@
 cmake_minimum_required (VERSION 3.4)
 
-if (${CMAKE_BINARY_DIR} MATCHES "^${CMAKE_SOURCE_DIR}")
-  if (NOT ${CMAKE_BINARY_DIR} MATCHES "^${CMAKE_SOURCE_DIR}/build")
-    message("")
-    message("##############################################")
-    message("     In-source building is not allowed!       ")
-    message("##############################################")
-    message("")
-    message(" Run cmake outside from source directory ")
-    message(" or from ${CMAKE_SOURCE_DIR}/build")
-    message("")
-    message("----------------------------------------------")
-    message(" warning: remaining generated files!")
-    message("   ${CMAKE_BINARY_DIR}/CMakeCache.txt")
-    message("   ${CMAKE_BINARY_DIR}/CMakeFiles")
-    message("----------------------------------------------")
-    message("")
-    message(" Please remove remaining generated files ")
-    message(" and run cmake from an appropriate location")
-    message("")
+# CMake utils
+list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
 
-    message(FATAL_ERROR  "\n** CMake aborted **")
-  endif()
-endif()
+# Forbids in-source builds
+include(CheckNotInSources)
 
-project (Pastis)
+#------------------------------------------------------
+#----------------- Main configuration -----------------
+#------------------------------------------------------
 
-list(APPEND CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -O3)
+project (Pastis)
 
+# Kokkos
 add_subdirectory(${CMAKE_SOURCE_DIR}/packages/kokkos)
-add_subdirectory(utils)
-
+include(GetKokkosCompilerFlags)
 include_directories(${Kokkos_INCLUDE_DIRS_RET})
+
+# Pastis utils
+add_subdirectory(utils)
 include_directories(utils)
 
+# Pastis binary
 add_executable(pastis main.cpp)
 target_link_libraries(pastis kokkos PastisUtils)
diff --git a/cmake/CheckNotInSources.cmake b/cmake/CheckNotInSources.cmake
new file mode 100644
index 000000000..ad43141c0
--- /dev/null
+++ b/cmake/CheckNotInSources.cmake
@@ -0,0 +1,28 @@
+# -----------------------------------------------------------------
+#            Checks we are not building in source-tree
+# -----------------------------------------------------------------
+
+# Explicitely authorize ${CMAKE_SOURCE_DIR}/build since some people
+if (${CMAKE_BINARY_DIR} MATCHES "^${CMAKE_SOURCE_DIR}")
+  if (NOT ${CMAKE_BINARY_DIR} MATCHES "^${CMAKE_SOURCE_DIR}/build")
+    message("")
+    message("##############################################")
+    message("     In-source building is not allowed!       ")
+    message("##############################################")
+    message("")
+    message(" Run cmake outside from source directory ")
+    message(" or from ${CMAKE_SOURCE_DIR}/build")
+    message("")
+    message("----------------------------------------------")
+    message(" warning: remaining generated files!")
+    message("   ${CMAKE_BINARY_DIR}/CMakeCache.txt")
+    message("   ${CMAKE_BINARY_DIR}/CMakeFiles")
+    message("----------------------------------------------")
+    message("")
+    message(" Please remove remaining generated files ")
+    message(" and run cmake from an appropriate location")
+    message("")
+
+    message(FATAL_ERROR  "\n** CMake aborted **\n")
+  endif()
+endif()
diff --git a/cmake/GetKokkosCompilerFlags.cmake b/cmake/GetKokkosCompilerFlags.cmake
new file mode 100644
index 000000000..63941c85a
--- /dev/null
+++ b/cmake/GetKokkosCompilerFlags.cmake
@@ -0,0 +1,43 @@
+# -----------------------------------------------------------------
+#       Let Kokkos do the job and find good compilier flags
+# -----------------------------------------------------------------
+
+#
+# This file is slightly adapted from Trilinos build system
+# (https://trilinos.org/)
+#
+
+set(KOKKOS_SRC_PATH ${Kokkos_SOURCE_DIR})
+set(KOKKOS_PATH ${KOKKOS_SRC_PATH})
+set(Kokkos_GEN_DIR ${CMAKE_BINARY_DIR})
+
+set_kokkos_cxx_compiler()
+set_kokkos_cxx_standard()
+
+include(${KOKKOS_SRC_PATH}/cmake/kokkos_options.cmake)
+
+#------------ COMPUTE KOKKOS_SETTINGS ----------------------------------------
+include(${KOKKOS_SRC_PATH}/cmake/kokkos_settings.cmake)
+
+#------------ GENERATE HEADER AND SOURCE FILES -------------------------------
+execute_process(
+  COMMAND ${KOKKOS_SETTINGS} make -f ${KOKKOS_SRC_PATH}/cmake/Makefile.generate_cmake_settings CXX=${CMAKE_CXX_COMPILER} generate_build_settings
+  WORKING_DIRECTORY "${Kokkos_GEN_DIR}"
+  OUTPUT_FILE ${Kokkos_GEN_DIR}/core_src_make.out
+  RESULT_VARIABLE GEN_SETTINGS_RESULT
+  )
+
+if (GEN_SETTINGS_RESULT)
+  message(FATAL_ERROR "Kokkos settings generation failed:\n"
+    "${KOKKOS_SETTINGS} make -f ${KOKKOS_SRC_PATH}/cmake/Makefile.generate_cmake_settings CXX=${CMAKE_CXX_COMPILER} generate_build_settings")
+endif()
+
+include(${Kokkos_GEN_DIR}/kokkos_generated_settings.cmake)
+
+#------------ GET KOKKOS COMPILER OPTIONS ------------------------------------
+
+set(CMAKE_CXX_FLAGSl "")
+foreach(opt ${KOKKOS_CXX_FLAGS})
+  set(CMAKE_CXX_FLAGSl "${CMAKE_CXX_FLAGSl} ${opt}")
+endforeach()
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGSl}")
-- 
GitLab