From 9532e3d8ce2e0ca4a0e88a9bedf3a5609d75b34a Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Thu, 11 Jan 2018 15:45:56 +0100
Subject: [PATCH] [CMake] Header standalone compilation check working.

---
 BaseLib/CMakeLists.txt                     |  2 -
 CMakeLists.txt                             |  2 +
 GeoLib/CMakeLists.txt                      |  2 +
 scripts/cmake/CheckHeaderCompilation.cmake | 72 ++++++++++++++++++----
 4 files changed, 65 insertions(+), 13 deletions(-)

diff --git a/BaseLib/CMakeLists.txt b/BaseLib/CMakeLists.txt
index 79621f6ab4b..7feb6db73c2 100644
--- a/BaseLib/CMakeLists.txt
+++ b/BaseLib/CMakeLists.txt
@@ -41,5 +41,3 @@ endif()
 if(OGS_USE_PCH)
     cotire(BaseLib)
 endif()
-
-check_header_compilation()
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 78d86e92bc7..2b2543286e4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -296,5 +296,7 @@ endif() # OGS_BUILD_TESTS
 configure_file("${CMAKE_CURRENT_SOURCE_DIR}/BaseLib/BuildInfo.cpp.in"
     "${CMAKE_CURRENT_BINARY_DIR}/BaseLib/BuildInfo.cpp" @ONLY)
 
+check_header_compilation()
+
 include(scripts/cmake/MarkVariablesAdvanced.cmake)
 unset(PRE_INSTALL_RUN CACHE)
diff --git a/GeoLib/CMakeLists.txt b/GeoLib/CMakeLists.txt
index 957d6c9043f..142b4e21931 100644
--- a/GeoLib/CMakeLists.txt
+++ b/GeoLib/CMakeLists.txt
@@ -34,3 +34,5 @@ endif()
 
 include(${PROJECT_SOURCE_DIR}/scripts/cmake/packaging/InstallXmlSchemaFiles.cmake)
 InstallXmlSchemaFiles("IO/XmlIO/*.xsd")
+
+#check_header_compilation(GeoLib)
diff --git a/scripts/cmake/CheckHeaderCompilation.cmake b/scripts/cmake/CheckHeaderCompilation.cmake
index 761442d2786..d51f4c1055f 100644
--- a/scripts/cmake/CheckHeaderCompilation.cmake
+++ b/scripts/cmake/CheckHeaderCompilation.cmake
@@ -1,24 +1,51 @@
 # Supply include directories and compiler flags
 get_directory_property(INCLUDE_DIRS INCLUDE_DIRECTORIES)
-set(CMAKE_REQUIRED_INCLUDES ${INCLUDE_DIRS})
 set(CMAKE_REQUIRED_FLAGS "-std=gnu++14")
 set(CMAKE_REQUIRED_QUIET TRUE)
 
 # Checks header for standalone compilation
-function(check_header_compilation)
-    if(NOT OGS_CHECK_HEADER_COMPILATION)
-        return()
-    endif()
-    string(REPLACE "${PROJECT_SOURCE_DIR}/" "" DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
+function(_check_header_compilation TARGET)
+
+    get_target_property(SOURCE_FILES ${TARGET} SOURCES)
+    get_target_property(SOURCE_DIR ${TARGET} SOURCE_DIR)
+
+    get_directory_property(DEFS DIRECTORY ${SOURCE_DIR} COMPILE_DEFINITIONS)
+    foreach(DEF ${DEFS})
+        if(${DEF} MATCHES ".*[0-9]\\(.*")
+            continue()
+        endif()
+        list(APPEND DEFS_CLEANED "-D${DEF}")
+    endforeach()
+
+    get_target_property(INCLUDE_DIRS ${TARGET} INCLUDE_DIRECTORIES)
+    get_target_property(LINK_LIBS ${TARGET} LINK_LIBRARIES)
+    foreach(LIB ${LINK_LIBS})
+        if(NOT TARGET ${LIB}) # Ignore non-existing targets
+            continue()
+        endif()
+        get_target_property(TARGET_INCLUDE_DIRS ${LIB} INCLUDE_DIRECTORIES)
+        if(TARGET_INCLUDE_DIRS)
+            list(APPEND INCLUDE_DIRS ${TARGET_INCLUDE_DIRS})
+        endif()
+    endforeach()
+    list(REMOVE_DUPLICATES INCLUDE_DIRS)
+
+    string(REPLACE "${PROJECT_SOURCE_DIR}/" "" DIRECTORY ${SOURCE_DIR})
     message(STATUS "Checking header compilation for ${DIRECTORY} ...")
     include(CheckCXXSourceCompiles)
-    set(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR})
-    file(GLOB_RECURSE FILES *.h)
-    foreach(FILE ${FILES})
-        # Ignore *-impl.h files
-        if("${FILE}" MATCHES ".*-impl.h")
+
+    set(CMAKE_REQUIRED_INCLUDES ${INCLUDE_DIRS} ${SOURCE_DIR})
+    set(CMAKE_REQUIRED_DEFINITIONS ${DEFS_CLEANED})
+
+    foreach(FILE ${SOURCE_FILES})
+
+        if(NOT "${FILE}" MATCHES ".*\\.h") # Check only header files
+            continue()
+        endif()
+        if("${FILE}" MATCHES ".*-impl\\.h") # Ignore *-impl.h files
             continue()
         endif()
+
         check_cxx_source_compiles(
             "
             #include \"${FILE}\"
@@ -26,10 +53,33 @@ function(check_header_compilation)
             "
             COMPILES
         )
+
         if(NOT COMPILES)
+            set(HEADER_COMPILE_ERROR TRUE CACHE INTERNAL "")
             string(REPLACE "${PROJECT_SOURCE_DIR}/" "" FILE_SHORT ${FILE})
             message(STATUS "  Compilation failed for ${FILE_SHORT}")
         endif()
         unset(COMPILES CACHE)
+
     endforeach()
 endfunction()
+
+function(check_header_compilation)
+    if(NOT OGS_CHECK_HEADER_COMPILATION)
+        return()
+    endif()
+    set(HEADER_COMPILE_ERROR FALSE CACHE INTERNAL "")
+
+    _check_header_compilation(BaseLib)
+    _check_header_compilation(GeoLib)
+    _check_header_compilation(MaterialLib)
+    _check_header_compilation(MathLib)
+    _check_header_compilation(MeshGeoToolsLib)
+    _check_header_compilation(MeshLib)
+    _check_header_compilation(NumLib)
+    _check_header_compilation(ProcessLib)
+
+    if(HEADER_COMPILE_ERROR)
+        message(STATUS "... header compilation check failed, see CMakeFiles/CMakeError.log for details!")
+    endif()
+endfunction()
-- 
GitLab