From 92a445f74f9b29dc526ecfd9c619b49f5b50781f Mon Sep 17 00:00:00 2001
From: Markus Blatt <mblatt@dune-project.org>
Date: Fri, 27 Apr 2012 13:57:23 +0000
Subject: [PATCH] Hide complexity from toplevel CMakeLists.txt.

[[Imported from SVN: r6660]]
---
 CMakeLists.txt                       | 109 +-------------------------
 cmake/modules/DuneCommonMacros.cmake |  21 +++++
 cmake/modules/DuneMacros.cmake       | 110 +++++++++++++++++++++++++++
 3 files changed, 135 insertions(+), 105 deletions(-)
 create mode 100644 cmake/modules/DuneCommonMacros.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2164fb666..6785e146d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -4,95 +4,12 @@ cmake_minimum_required(VERSION 2.8)
 # make sure our own modules are found
 set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules")
 
-
-set(CMAKE_CXX_FLAGS_DEBUG "-g -O0")
-set(CMAKE_C_FLAGS_DEBUG "-g -O0")
-set(CMAKE_CXX_FLAGS_RELEASE "-funroll-loops -O3")
-set(CMAKE_C_FLAGS_RELEASE "-funroll-loops -O3")
-
+#include the dune macros
 include(DuneMacros)
 
+# start a dune project with information from dune.module
 dune_project()
 
-
-option(DUNE_USE_ONLY_STATIC_LIBS "If set to ON, we will force static linkage everywhere" OFF)
-if(DUNE_USE_ONLY_STATIC_LIBS)
-  set(_default_enable_shared OFF)
-else(DUNE_USE_ONLY_STATIC_LIBS)
-  set(_default_enable_shared ON)
-endif(DUNE_USE_ONLY_STATIC_LIBS)
-option(BUILD_SHARED_LIBS "If set to ON, shared libs will be built" ${_default_enable_shared})
-
-if(DUNE_USE_ONLY_STATIC_LIBS)
-  # Use only static libraries.
-  # We do this by overriding the library suffixes.
-  set( BLA_STATIC 1)
-  set( _dune_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
-  if (WIN32)
-    set(CMAKE_FIND_LIBRARY_SUFFIXES .lib ${CMAKE_FIND_LIBRARY_SUFFIXES})
-  endif (WIN32)
-  if (APPLE)
-    set(CMAKE_FIND_LIBRARY_SUFFIXES .lib ${CMAKE_FIND_LIBRARY_SUFFIXES})
-  else (APPLE)
-    set(CMAKE_FIND_LIBRARY_SUFFIXES .a )
-  endif (APPLE)
-endif()
-
-include(DuneStreams)
-dune_set_minimal_debug_level()
-
-# include dune-common and current directory to include pathtest
-include_directories("${CMAKE_SOURCE_DIR}")
-link_directories("${CMAKE_SOURCE_DIR}/lib")
-
-# set required compiler flags for C++11 (former C++0x)
-find_package(CXX11Features)
-
-# search for headers
-include(CheckIncludeFile)
-include(CheckIncludeFileCXX)
-check_include_file("malloc.h" HAVE_MALLOC_H)
-check_include_file("stdint.h" HAVE_STDINT_H)
-check_include_file_cxx("memory" HAVE_MEMORY)
-set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -DHAVE_MEMORY=${HAVE_MEMORY}")
-
-include(DUNEMPI)
-
-find_package(Boost)
-set(HAVE_BOOST BOOST_FOUND)
-find_package(SharedPtr)
-if(Fortran_Works)
-  # search for lapack
-  find_package(LAPACK)
-  set(HAVE_LAPACK LAPACK_FOUND)
-  set(HAVE_BLAS BLAS_FOUND)
-  # make calling fortran routines from C/C++ possible
-  include(FortranCInterface)
-  FortranCInterface_VERIFY(CXX)
-  FortranCInterface_HEADER(FC.h MACRO_NAMESPACE "FC_")
-else(Fortran_Works)
-  set(HAVE_LAPACK Off)
-  set(HAVE_BLAS Off)
-  file(WRITE ${CMAKE_BINARY_DIR}/FC.h "")
-endif(Fortran_Works)
-
-
-# actually write the config.h file to disk
-configure_file(config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h)
-include_directories("${CMAKE_CURRENT_BINARY_DIR}")
-add_definitions(-DHAVE_CONFIG_H)
-
-# Create custom target for building the documentation
-# and provide macros for installing the docs and force
-# building them before.
-include(DUNEDoc)
-
-# activate testing the DUNE way
-include(DuneTests)
-
-# activate pkg-config
-include(DunePkgConfig)
-
 # add subdirectories to execute CMakeLists.txt there
 add_subdirectory("dune")
 add_subdirectory("bin")
@@ -102,23 +19,5 @@ add_subdirectory("doc")
 add_subdirectory("cmake/modules")
 add_subdirectory("cmake/scripts")
 
-
-#create cmake-config files
-configure_file(
-  ${PROJECT_SOURCE_DIR}/${DUNE_MOD_NAME}-config.cmake.in
-  ${PROJECT_BINARY_DIR}/${DUNE_MOD_NAME}-config.cmake @ONLY)
-
-configure_file(
-  ${PROJECT_SOURCE_DIR}/${DUNE_MOD_NAME}-version.cmake.in
-  ${PROJECT_BINARY_DIR}/${DUNE_MOD_NAME}-version.cmake @ONLY)
-
-#install dune.module file
-install(FILES dune.module DESTINATION lib/dunecontrol/${DUNE_MOD_NAME})
-
-#install cmake-config files
-install(FILES ${PROJECT_BINARY_DIR}/${DUNE_MOD_NAME}-config.cmake
-  ${PROJECT_BINARY_DIR}/${DUNE_MOD_NAME}-version.cmake
-  DESTINATION lib/cmake)
-
-
-export(PACKAGE dune-common)
+# finalize the dune project, e.g. generating config.h etc.
+finalize_dune_project()
diff --git a/cmake/modules/DuneCommonMacros.cmake b/cmake/modules/DuneCommonMacros.cmake
new file mode 100644
index 000000000..a9e1a4236
--- /dev/null
+++ b/cmake/modules/DuneCommonMacros.cmake
@@ -0,0 +1,21 @@
+# This cmake file holds test and directives that are executed
+# for the module dune-common and have to be executed by
+# all dependent modules
+#
+include(DuneStreams)
+dune_set_minimal_debug_level()
+
+find_package(Boost)
+set(HAVE_BOOST BOOST_FOUND)
+find_package(SharedPtr)
+if(Fortran_Works)
+  # search for lapack
+  find_package(LAPACK)
+  set(HAVE_LAPACK LAPACK_FOUND)
+  set(HAVE_BLAS BLAS_FOUND)
+else(Fortran_Works)
+  set(HAVE_LAPACK Off)
+  set(HAVE_BLAS Off)
+  file(WRITE ${CMAKE_BINARY_DIR}/FC.h "")
+endif(Fortran_Works)
+
diff --git a/cmake/modules/DuneMacros.cmake b/cmake/modules/DuneMacros.cmake
index 0f9c98491..08d542c63 100644
--- a/cmake/modules/DuneMacros.cmake
+++ b/cmake/modules/DuneMacros.cmake
@@ -60,6 +60,13 @@ macro(dune_module_information MODULE_DIR)
 endmacro(dune_module_information)
 
 macro(dune_project)
+
+  # Set the flags
+  set(CMAKE_CXX_FLAGS_DEBUG "-g -O0")
+  set(CMAKE_C_FLAGS_DEBUG "-g -O0")
+  set(CMAKE_CXX_FLAGS_RELEASE "-funroll-loops -O3")
+  set(CMAKE_C_FLAGS_RELEASE "-funroll-loops -O3")
+
   dune_module_information(${CMAKE_SOURCE_DIR})
   set(ProjectName            "${DUNE_MOD_NAME}")
   set(ProjectVersion         "${DUNE_MOD_VERSION}")
@@ -70,4 +77,107 @@ macro(dune_project)
   if(Fortran_Works)
     enable_language(Fortran OPTIONAL)
   endif(Fortran_Works)
+
+  option(DUNE_USE_ONLY_STATIC_LIBS "If set to ON, we will force static linkage everywhere" OFF)
+  if(DUNE_USE_ONLY_STATIC_LIBS)
+    set(_default_enable_shared OFF)
+  else(DUNE_USE_ONLY_STATIC_LIBS)
+    set(_default_enable_shared ON)
+  endif(DUNE_USE_ONLY_STATIC_LIBS)
+  option(BUILD_SHARED_LIBS "If set to ON, shared libs will be built" ${_default_enable_shared})
+
+  if(DUNE_USE_ONLY_STATIC_LIBS)
+    # Use only static libraries.
+    # We do this by overriding the library suffixes.
+    set( BLA_STATIC 1)
+    set( _dune_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
+    if (WIN32)
+      set(CMAKE_FIND_LIBRARY_SUFFIXES .lib ${CMAKE_FIND_LIBRARY_SUFFIXES})
+    endif (WIN32)
+    if (APPLE)
+      set(CMAKE_FIND_LIBRARY_SUFFIXES .lib ${CMAKE_FIND_LIBRARY_SUFFIXES})
+    else (APPLE)
+      set(CMAKE_FIND_LIBRARY_SUFFIXES .a )
+    endif (APPLE)
+  endif()
+
+  # set required compiler flags for C++11 (former C++0x)
+  find_package(CXX11Features)
+
+  # search for headers
+  include(CheckIncludeFile)
+  include(CheckIncludeFileCXX)
+  check_include_file("malloc.h" HAVE_MALLOC_H)
+  check_include_file("stdint.h" HAVE_STDINT_H)
+  check_include_file_cxx("memory" HAVE_MEMORY)
+  set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -DHAVE_MEMORY=${HAVE_MEMORY}")
+
+  # set include path and link path for the current project.
+  include_directories("${CMAKE_SOURCE_DIR}")
+  link_directories("${CMAKE_SOURCE_DIR}/lib")
+  include_directories("${CMAKE_CURRENT_BINARY_DIR}")
+  add_definitions(-DHAVE_CONFIG_H)
+
+  # Search for MPI and set the relevant variables.
+  include(DUNEMPI)
+
+
+  # Make calling fortran routines from C/C++ possible
+  if(Fortran_Works)
+    include(FortranCInterface)
+    FortranCInterface_VERIFY(CXX)
+    # Write FC.h header containing information about
+    # how to call fortran routined.
+    # It will be included in config.h
+    FortranCInterface_HEADER(FC.h MACRO_NAMESPACE "FC_")
+  else(Fortran_Works)
+    # Write empty FC.h header
+    file(WRITE ${CMAKE_BINARY_DIR}/FC.h "")
+  endif(Fortran_Works)
+
+  # Create custom target for building the documentation
+  # and provide macros for installing the docs and force
+  # building them before.
+  include(DUNEDoc)
+
+  # activate testing the DUNE way
+  include(DuneTests)
+
+  # activate pkg-config
+  include(DunePkgConfig)
+
+  # Search for a cmake files containing tests and directives
+  # specific to this module
+  find_file(_mod_cmake DuneCommonMacros.cmake ${CMAKE_MODULE_PATH}
+    NO_DEFAULT_PATH)
+  if(_mod_cmake)
+    include(DuneCommonMacros)
+  endif(_mod_cmake)
 endmacro(dune_project MODULE_DIR)
+
+# macro that should be called at the end of the top level CMakeLists.txt.
+# Namely it creates  config.h and the cmake-config files,
+# some install directives and export th module.
+MACRO(finalize_dune_project)
+  # actually write the config.h file to disk
+  configure_file(config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h)
+
+  #create cmake-config files
+  configure_file(
+    ${PROJECT_SOURCE_DIR}/${DUNE_MOD_NAME}-config.cmake.in
+    ${PROJECT_BINARY_DIR}/${DUNE_MOD_NAME}-config.cmake @ONLY)
+
+  configure_file(
+    ${PROJECT_SOURCE_DIR}/${DUNE_MOD_NAME}-version.cmake.in
+    ${PROJECT_BINARY_DIR}/${DUNE_MOD_NAME}-version.cmake @ONLY)
+
+  #install dune.module file
+  install(FILES dune.module DESTINATION lib/dunecontrol/${DUNE_MOD_NAME})
+
+  #install cmake-config files
+  install(FILES ${PROJECT_BINARY_DIR}/${DUNE_MOD_NAME}-config.cmake
+    ${PROJECT_BINARY_DIR}/${DUNE_MOD_NAME}-version.cmake
+    DESTINATION lib/cmake)
+
+  export(PACKAGE ${DUNE_MOD_NAME})
+ENDMACRO(finalize_dune_project)
-- 
GitLab