From c258d3f84481bc493425f385114607e8114638e2 Mon Sep 17 00:00:00 2001
From: Soeren Peters <peters@irmb.tu-bs.de>
Date: Wed, 21 Oct 2020 10:13:43 +0200
Subject: [PATCH] Add a USE_OPENMP option to the toplevel cmake list.

---
 3rdParty/MuParser/CMakeLists.txt     | 8 +-------
 CMake/3rd/OpenMP.cmake               | 9 ++++++---
 CMakeLists.txt                       | 2 ++
 src/gpu/GksGpu/CMakeLists.txt        | 4 +++-
 src/gpu/GridGenerator/CMakeLists.txt | 4 ++--
 5 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/3rdParty/MuParser/CMakeLists.txt b/3rdParty/MuParser/CMakeLists.txt
index 49aa3ac25..a2c5742f7 100644
--- a/3rdParty/MuParser/CMakeLists.txt
+++ b/3rdParty/MuParser/CMakeLists.txt
@@ -17,12 +17,6 @@ option(ENABLE_SAMPLES "Build the samples" OFF)
 option(ENABLE_OPENMP "Enable OpenMP for multithreading" ON)
 #option(BUILD_SHARED_LIBS "Build shared/static libs" ON)
 
-if(ENABLE_OPENMP)
-    find_package(OpenMP REQUIRED)
-    set(CMAKE_CXX_FLAGS "${OpenMP_CXX_FLAGS} ${CMAKE_CXX_FLAGS}")
-    set(CMAKE_SHARED_LIBRARY_CXX_FLAGS "${OpenMP_CXX_FLAGS} ${CMAKE_SHARED_LIBRARY_CXX_FLAGS}")
-endif()
-
 
 # Credit: https://stackoverflow.com/questions/2368811/how-to-set-warning-level-in-cmake/3818084
 if(MSVC)
@@ -50,7 +44,7 @@ add_library(muparser
         src/muParserTokenReader.cpp
         )
 
-target_link_libraries(muparser PUBLIC OpenMP::OpenMP_CXX)
+linkOpenMP(muparser)
 target_include_directories(muparser PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
 
 # this compiles the "DLL" interface (C API)
diff --git a/CMake/3rd/OpenMP.cmake b/CMake/3rd/OpenMP.cmake
index bd7781b95..45465ba4b 100644
--- a/CMake/3rd/OpenMP.cmake
+++ b/CMake/3rd/OpenMP.cmake
@@ -1,10 +1,13 @@
-function (linkOpenMP)
+function (linkOpenMP targetName)
+
+	if(NOT USE_OPENMP)
+		return()
+	endif()
 
 	find_package(OpenMP REQUIRED)
 
 	if(OpenMP_CXX_FOUND)
-		vf_get_library_name(library_name)
-		target_link_libraries(${library_name} PUBLIC OpenMP::OpenMP_CXX)
+		target_link_libraries(${targetName} PUBLIC OpenMP::OpenMP_CXX)
 	endif()
 
 endfunction()
\ No newline at end of file
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3ca84ca3f..c644b9786 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -42,6 +42,8 @@ option(BUILD_VF_CLANG_TIDY "Add the clang tidy checks to the targets" OFF)
 
 option(BUILD_SHARED_LIBS "" ON)
 
+option(USE_OPENMP "Include OpenMP support" ON)
+
 #################################################################################
 #  CMAKE POLICIES
 #################################################################################
diff --git a/src/gpu/GksGpu/CMakeLists.txt b/src/gpu/GksGpu/CMakeLists.txt
index 3bb439da1..88df905d3 100644
--- a/src/gpu/GksGpu/CMakeLists.txt
+++ b/src/gpu/GksGpu/CMakeLists.txt
@@ -3,4 +3,6 @@ vf_add_library(BUILDTYPE shared PRIVATE_LINK basics GksMeshAdapter)
 
 linkCUDA()
 linkMPI()
-linkOpenMP()
+
+vf_get_library_name(library_name)
+linkOpenMP(${library_name})
diff --git a/src/gpu/GridGenerator/CMakeLists.txt b/src/gpu/GridGenerator/CMakeLists.txt
index aee5018a9..aaabd6d5d 100644
--- a/src/gpu/GridGenerator/CMakeLists.txt
+++ b/src/gpu/GridGenerator/CMakeLists.txt
@@ -1,9 +1,9 @@
 
 
 vf_add_library(BUILDTYPE shared PRIVATE_LINK basics)
+vf_get_library_name(library_name)
 
 linkCUDA()
-linkOpenMP()
+linkOpenMP(${library_name})
 
-vf_get_library_name(library_name)
 set_target_properties(${library_name} PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
\ No newline at end of file
-- 
GitLab