From d2b51104c95716f45ca35d5d3f6c88f5382768bd Mon Sep 17 00:00:00 2001
From: Markus Blatt <mblatt@dune-project.org>
Date: Mon, 23 Apr 2012 18:12:31 +0000
Subject: [PATCH] Make toplevel CMakeLists.txt more modular to facilitate usage
 in other DUNE modules.

Check the dune.module file more rigorously! (Previously errors were
not apparent)

[[Imported from SVN: r6655]]
---
 CMakeLists.txt                    | 65 +++----------------------------
 bin/duneproject                   | 31 +++++++++++++++
 cmake/modules/DUNEDoc.cmake       |  3 ++
 cmake/modules/DuneMacros.cmake    | 55 ++++++++++++++++++++++++--
 cmake/modules/DunePkgConfig.cmake | 26 +++++++++++++
 cmake/modules/DuneTests.cmake     | 22 +++++++++++
 6 files changed, 140 insertions(+), 62 deletions(-)
 create mode 100644 cmake/modules/DunePkgConfig.cmake
 create mode 100644 cmake/modules/DuneTests.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0e0869bce..2164fb666 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -11,16 +11,8 @@ set(CMAKE_CXX_FLAGS_RELEASE "-funroll-loops -O3")
 set(CMAKE_C_FLAGS_RELEASE "-funroll-loops -O3")
 
 include(DuneMacros)
-dune_module_information(${CMAKE_SOURCE_DIR})
 
-set(ProjectName            "${DUNE_MOD_NAME}")
-set(ProjectVersion         "${DUNE_MOD_VERSION}")
-set(ProjectMaintainerEmail "${DUNE_MAINTAINER}")
-project(${ProjectName} C CXX)# Fortran)
-set(DUNE_COMMON_VERSION          "${DUNE_MOD_VERSION}")
-set(DUNE_COMMON_VERSION_MAJOR    "${DUNE_VERSION_MAJOR}")
-set(DUNE_COMMON_VERSION_MINOR    "${DUNE_VERSION_MINOR}")
-set(DUNE_COMMON_VERSION_REVISION "${DUNE_VERSION_REVISION}")
+dune_project()
 
 
 option(DUNE_USE_ONLY_STATIC_LIBS "If set to ON, we will force static linkage everywhere" OFF)
@@ -64,18 +56,12 @@ 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}")
 
-# search for packages
-find_package(PkgConfig)
-
 include(DUNEMPI)
 
 find_package(Boost)
 set(HAVE_BOOST BOOST_FOUND)
 find_package(SharedPtr)
-include(LanguageSupport)
-workaround_9220(Fortran Fortran_Works)
 if(Fortran_Works)
-  enable_language(Fortran OPTIONAL)
   # search for lapack
   find_package(LAPACK)
   set(HAVE_LAPACK LAPACK_FOUND)
@@ -96,32 +82,16 @@ configure_file(config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h)
 include_directories("${CMAKE_CURRENT_BINARY_DIR}")
 add_definitions(-DHAVE_CONFIG_H)
 
-include(DUNEDoxygen)
-
 # Create custom target for building the documentation
 # and provide macros for installing the docs and force
 # building them before.
 include(DUNEDoc)
 
-# Create a custom target for building the tests.
-# Thus they will not be built by make all any more.
-# Actually I wanted this target to be a dependency
-# of make test but that is currently not possible.
-# See http://public.kitware.com/Bug/view.php?id=8438
-# This really sucks!
-# Therefore currently make build_tests has to be called
-# before make test.
-add_custom_target(build_tests DEPENDS ${TESTPROGS})
-
-# mimic make check of the autotools
-add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} DEPENDS build_tests)
-
-# enable testing before adding the subdirectories.
-# Thus we can add the tests in the subdirectories
-# where they actually are.
-enable_testing()
-#add_custom_target(build_test)
-set_property(TEST APPEND PROPERTY DEPENDS build_tests)
+# activate testing the DUNE way
+include(DuneTests)
+
+# activate pkg-config
+include(DunePkgConfig)
 
 # add subdirectories to execute CMakeLists.txt there
 add_subdirectory("dune")
@@ -132,23 +102,6 @@ add_subdirectory("doc")
 add_subdirectory("cmake/modules")
 add_subdirectory("cmake/scripts")
 
-# set some variables that are used in the pkg-config file
-set( CMAKE_FIND_LIBRARY_SUFFIXES ${_dune_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
-set( prefix ${CMAKE_INSTALL_PREFIX})
-set(exec_prefix "\${prefix}")
-set(libdir "\${exec_prefix}/lib")
-set(includedir "\${prefix}/include")
-set(PACKAGE_NAME ${DUNE_MOD_NAME})
-set(VERSION ${DUNE_MOD_VERSION})
-set(CC ${CMAKE_C_COMPILER})
-set(CXX ${CMAKE_CXX_COMPILER})
-
-#create pkg-config file
-configure_file(
-  ${PROJECT_SOURCE_DIR}/${DUNE_MOD_NAME}.pc.in
-  ${PROJECT_BINARY_DIR}/${DUNE_MOD_NAME}.pc
-  @ONLY
-)
 
 #create cmake-config files
 configure_file(
@@ -161,17 +114,11 @@ configure_file(
 
 #install dune.module file
 install(FILES dune.module DESTINATION lib/dunecontrol/${DUNE_MOD_NAME})
-# install pkgconfig file
-if(PKG_CONFIG_FOUND )
-  install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${DUNE_MOD_NAME}.pc
-    DESTINATION lib/pkgconfig)
-endif(PKG_CONFIG_FOUND)
 
 #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)
 
-include(CTest)
 
 export(PACKAGE dune-common)
diff --git a/bin/duneproject b/bin/duneproject
index 30056850d..257530a81 100755
--- a/bin/duneproject
+++ b/bin/duneproject
@@ -457,6 +457,37 @@ include \$(top_srcdir)/am/global-rules
 
 M_DELIM
 
+for i in $DEPENDENCIES; do
+    echo "find_packages($i)" >>$PROJECT/dependencies.cmake
+    echo "include_directories(\${$i_INCLUDE_DIRS})" >>$PROJECT/dependencies.cmake
+done
+echo "include_directories("${CMAKE_SOURCE_DIR}")"  >>$PROJECT/dependencies.cmake
+
+echo " - $PROJECT/CMakeLists.txt"
+cat> "$PROJECT/CMakeLists.txt" << M_DELIM
+cmake_minimum_required(VERSION 2.8)
+project($PROJECT CXX)
+include(dependencies.cmake)
+
+# for this tow work you will have to modify the CMAKE_MODULE_PATH
+#set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/../dune-common/cmake/modules")
+
+include(DuneMacros)
+
+include(DUNEDoc)
+
+#activate testing
+include(DuneTest)
+
+#activate pkg-config
+include(DunePkgConfig)
+
+add_subdirectory("src")
+add_subdirectory("m4")
+add_subdirectory("dune")
+add_subdirectory("doc")
+
+M_DELIM
 ################## STAMP-VC ##################
 echo "- $PROJECT/stamp-vc"
 echo 'A stamp file to signify that this directory comes from a version control system, not an unpacked tarball' > $PROJECT/stamp-vc
diff --git a/cmake/modules/DUNEDoc.cmake b/cmake/modules/DUNEDoc.cmake
index 3e82be816..b3faca377 100644
--- a/cmake/modules/DUNEDoc.cmake
+++ b/cmake/modules/DUNEDoc.cmake
@@ -17,3 +17,6 @@ MACRO(create_doc_install filename targetdir)
   install(CODE "execute_process(COMMAND \"${CMAKE_COMMAND}\" --build \"${CMAKE_BINARY_DIR}\" --target install_${targetfile} )
             LIST(APPEND CMAKE_INSTALL_MANIFEST_FILES ${CMAKE_INSTALL_PREFIX}/${targetdir}/${targetfile})")
 ENDMACRO(create_doc_install)
+
+# Support building documentation with doxygen.
+include(DUNEDoxygen)
diff --git a/cmake/modules/DuneMacros.cmake b/cmake/modules/DuneMacros.cmake
index 194f6f03e..0f9c98491 100644
--- a/cmake/modules/DuneMacros.cmake
+++ b/cmake/modules/DuneMacros.cmake
@@ -3,7 +3,12 @@ macro(dune_module_information MODULE_DIR)
   file(READ "${MODULE_DIR}/dune.module" DUNE_MODULE)
 
   # find version string
-  string(REGEX REPLACE ".*Version:[ ]*([^ \n]+).*" "\\1" DUNE_MOD_VERSION "${DUNE_MODULE}")
+  string(REGEX MATCH "Version:[ ]*[^ \n]+" MODULE_LINE "${DUNE_MODULE}")
+  if(NOT MODULE_LINE)
+    message("${MODULE_DIR}/dune.module is missing a version." FATAL_ERROR)
+  endif(NOT MODULE_LINE)
+
+  string(REGEX REPLACE ".*Version:[ ]*([^ \n]+).*" "\\1" MODULE_LINE "${DUNE_MODULE}")
   string(REGEX REPLACE "([0-9]).*" "\\1" DUNE_VERSION_MAJOR "${DUNE_MOD_VERSION}")
   string(REGEX REPLACE "[0-9]*\\.([0-9]).*" "\\1" DUNE_VERSION_MINOR "${DUNE_MOD_VERSION}")
   string(REGEX REPLACE "[0-9]*\\.[0-9]*\\.([0-9]).*" "\\1" DUNE_VERSION_REVISION "${DUNE_MOD_VERSION}")
@@ -19,6 +24,50 @@ macro(dune_module_information MODULE_DIR)
   endif(NON_NUMBER_CHARACTER)
 
   # find strings for module name, maintainer
-  string(REGEX REPLACE ".*Module:[ ]*([^ \n]+).*" "\\1" DUNE_MOD_NAME "${DUNE_MODULE}")
-  string(REGEX REPLACE ".*Maintainer:[ ]*([^ \n]+).*" "\\1" DUNE_MAINTAINER "${DUNE_MODULE}")
+  # 1. Check for line starting with Module
+  string(REGEX MATCH "Module:[ ]*[^ \n]+" MODULE_LINE "${DUNE_MODULE}")
+  if(NOT MODULE_LINE)
+    message("${MODULE_DIR}/dune.module is missing a module name." FATAL_ERROR)
+  endif(NOT MODULE_LINE)
+
+  # extract Module name
+  string(REGEX REPLACE ".*Module:[ ]*([^ \n]+).*" "\\1" DUNE_MOD_NAME "${MODULE_LINE}")
+
+  # 2. Check for line starting with Maintainer
+  string(REGEX MATCH "Maintainer:[ ]*[^ \n]+" MODULE_LINE "${DUNE_MODULE}")
+  if(NOT MODULE_LINE)
+    message("${MODULE_DIR}/dune.module is missing a maintainer." FATAL_ERROR)
+  endif(NOT MODULE_LINE)
+
+  # extract Maintainer
+  string(REGEX REPLACE ".*Maintainer:[ ]*([^ \n]+).*" "\\1" DUNE_MAINTAINER "${MODULE_LINE}")
+
+  # 3. Check for line starting with Depends
+  string(REGEX MATCH "Depends:[ ]*[^ \n]+" MODULE_LINE "${DUNE_MODULE}")
+
+  # extract dependencies if the line exists.
+  if(MODULE_LINE)
+    string(REGEX REPLACE ".*Depends:[ ]*([^ \n]+).*" "\\1" DUNE_DEPENDS "${DUNE_MODULE}")
+  endif(MODULE_LINE)
+
+  # set module version
+  set("${DUNE_MOD_NAME}_VERSION"          "${DUNE_MOD_VERSION}")
+  set("${DUNE_MOD_NAME}_VERSION_MAJOR"    "${DUNE_VERSION_MAJOR}")
+  set("${DUNE_MOD_NAME}_VERSION_MINOR"    "${DUNE_VERSION_MINOR}")
+  set("${DUNE_MOD_NAME}_VERSION_REVISION" "${DUNE_VERSION_REVISION}")
+
+  #foreach( module $DEPENDENCIES
 endmacro(dune_module_information)
+
+macro(dune_project)
+  dune_module_information(${CMAKE_SOURCE_DIR})
+  set(ProjectName            "${DUNE_MOD_NAME}")
+  set(ProjectVersion         "${DUNE_MOD_VERSION}")
+  set(ProjectMaintainerEmail "${DUNE_MAINTAINER}")
+  project(${ProjectName} C CXX)# Fortran)
+  include(LanguageSupport)
+  workaround_9220(Fortran Fortran_Works)
+  if(Fortran_Works)
+    enable_language(Fortran OPTIONAL)
+  endif(Fortran_Works)
+endmacro(dune_project MODULE_DIR)
diff --git a/cmake/modules/DunePkgConfig.cmake b/cmake/modules/DunePkgConfig.cmake
new file mode 100644
index 000000000..da5a4cd49
--- /dev/null
+++ b/cmake/modules/DunePkgConfig.cmake
@@ -0,0 +1,26 @@
+# search for packages
+find_package(PkgConfig)
+
+# set some variables that are used in the pkg-config file
+set( CMAKE_FIND_LIBRARY_SUFFIXES ${_dune_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
+set( prefix ${CMAKE_INSTALL_PREFIX})
+set(exec_prefix "\${prefix}")
+set(libdir "\${exec_prefix}/lib")
+set(includedir "\${prefix}/include")
+set(PACKAGE_NAME ${DUNE_MOD_NAME})
+set(VERSION ${DUNE_MOD_VERSION})
+set(CC ${CMAKE_C_COMPILER})
+set(CXX ${CMAKE_CXX_COMPILER})
+
+#create pkg-config file
+configure_file(
+  ${PROJECT_SOURCE_DIR}/${DUNE_MOD_NAME}.pc.in
+  ${PROJECT_BINARY_DIR}/${DUNE_MOD_NAME}.pc
+  @ONLY
+)
+
+# install pkgconfig file
+if(PKG_CONFIG_FOUND )
+  install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${DUNE_MOD_NAME}.pc
+    DESTINATION lib/pkgconfig)
+endif(PKG_CONFIG_FOUND)
diff --git a/cmake/modules/DuneTests.cmake b/cmake/modules/DuneTests.cmake
new file mode 100644
index 000000000..6dab7b57b
--- /dev/null
+++ b/cmake/modules/DuneTests.cmake
@@ -0,0 +1,22 @@
+# Create a custom target for building the tests.
+# Thus they will not be built by make all any more.
+# Actually I wanted this target to be a dependency
+# of make test but that is currently not possible.
+# See http://public.kitware.com/Bug/view.php?id=8438
+# This really sucks!
+# Therefore currently make build_tests has to be called
+# before make test.
+add_custom_target(build_tests DEPENDS ${TESTPROGS})
+
+# mimic make check of the autotools
+add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} DEPENDS build_tests)
+
+# enable testing before adding the subdirectories.
+# Thus we can add the tests in the subdirectories
+# where they actually are.
+enable_testing()
+#add_custom_target(build_test)
+set_property(TEST APPEND PROPERTY DEPENDS build_tests)
+
+#activate CTest
+include(CTest)
-- 
GitLab