From 7670613a3e868c208ec91eadfd266f314116befd Mon Sep 17 00:00:00 2001
From: "Joachim Wuttke (o)" <j.wuttke@fz-juelich.de>
Date: Thu, 10 Sep 2015 15:42:00 +0200
Subject: [PATCH] FindYamlCpp05, asserts include file version, compiles and
 runs test. (suggested for upload at
 https://github.com/jbeder/yaml-cpp/issues/336).

---
 .gitignore                                   |   1 +
 pub/CMakeLists.txt                           |   2 +-
 pub/cmake/modules/CheckCppIncludeFile.cmake  | 100 +++++++++++++++++++
 pub/cmake/modules/CheckCppIncludeFile.cpp.in |  13 +++
 pub/cmake/modules/FindYamlCpp.cmake          |  68 -------------
 pub/cmake/modules/FindYamlCpp05.cmake        |  86 ++++++++++++++++
 6 files changed, 201 insertions(+), 69 deletions(-)
 create mode 100644 pub/cmake/modules/CheckCppIncludeFile.cmake
 create mode 100644 pub/cmake/modules/CheckCppIncludeFile.cpp.in
 delete mode 100644 pub/cmake/modules/FindYamlCpp.cmake
 create mode 100644 pub/cmake/modules/FindYamlCpp05.cmake

diff --git a/.gitignore b/.gitignore
index 877230c1..e4b89a63 100644
--- a/.gitignore
+++ b/.gitignore
@@ -22,6 +22,7 @@ frida
 external_ci
 gcc
 clang
+build
 qtbuild
 podstyle.css
 pub/src/xax_yacc.hpp
diff --git a/pub/CMakeLists.txt b/pub/CMakeLists.txt
index 6e3d3e80..d4d83306 100644
--- a/pub/CMakeLists.txt
+++ b/pub/CMakeLists.txt
@@ -41,7 +41,7 @@ find_package(BISON)
 find_package(FLEX)
 include(FindGSL)
 include(FindFFTW)
-include(FindYamlCpp)
+include(FindYamlCpp05)
 include(FindFridalibs)
 include(FindReadline)
 
diff --git a/pub/cmake/modules/CheckCppIncludeFile.cmake b/pub/cmake/modules/CheckCppIncludeFile.cmake
new file mode 100644
index 00000000..b430c03d
--- /dev/null
+++ b/pub/cmake/modules/CheckCppIncludeFile.cmake
@@ -0,0 +1,100 @@
+#.rst:
+# CheckCppIncludeFile
+# -------------------
+#
+# macro which checks the C++ include file exists.
+#
+# CHECK_INCLUDE_FILE(INCLUDE VARIABLE)
+#
+# ::
+#
+#   INCLUDE  - name of include file
+#   VARIABLE - variable to return result
+#              Will be created as an internal cache variable.
+#
+#
+#
+# an optional third argument is the CFlags to add to the compile line or
+# you can use CMAKE_REQUIRED_FLAGS
+#
+# The following variables may be set before calling this macro to modify
+# the way the check is run:
+#
+# ::
+#
+#   CMAKE_REQUIRED_FLAGS = string of compile command line flags
+#   CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
+#   CMAKE_REQUIRED_INCLUDES = list of include directories
+#   CMAKE_REQUIRED_QUIET = execute quietly without messages
+
+#=============================================================================
+#
+# Copyright 2015 Joachim Wuttke, based on cmake 3.2 CheckIncludeFile.cmake
+#
+# Copyright 2002-2009 Kitware, Inc.
+#
+# Distributed under the OSI-approved BSD License (the "License");
+# see accompanying file Copyright.txt for details.
+#
+# This software is distributed WITHOUT ANY WARRANTY; without even the
+# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+# See the License for more information.
+#=============================================================================
+# (To distribute this file outside of CMake, substitute the full
+#  License text for the above reference.)
+
+macro(CHECK_INCLUDE_FILE INCLUDE VARIABLE)
+    if(DEFINED "${VARIABLE}")
+        message(WARNING "using ${VARIABLE} as cached: ${${VARIABLE}}")
+        return()
+    endif()        
+    if(CMAKE_REQUIRED_INCLUDES)
+        set(CHECK_INCLUDE_FILE_C_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${CMAKE_REQUIRED_INCLUDES}")
+    else()
+        set(CHECK_INCLUDE_FILE_C_INCLUDE_DIRS)
+    endif()
+    set(MACRO_CHECK_INCLUDE_FILE_FLAGS ${CMAKE_REQUIRED_FLAGS})
+    set(CHECK_INCLUDE_FILE_VAR ${INCLUDE})
+    configure_file(${CMAKE_SOURCE_DIR}/cmake/modules/CheckCppIncludeFile.cpp.in
+        ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckCppIncludeFile.cpp)
+    if(NOT CMAKE_REQUIRED_QUIET)
+        message(STATUS "Looking for ${INCLUDE}")
+    endif()
+    if(${ARGC} EQUAL 3)
+        set(CMAKE_C_FLAGS_SAVE ${CMAKE_C_FLAGS})
+        set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${ARGV2}")
+    endif()
+
+    try_compile(${VARIABLE}
+        ${CMAKE_BINARY_DIR}
+        ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckCppIncludeFile.cpp
+        COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
+        CMAKE_FLAGS
+        -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_INCLUDE_FILE_FLAGS}
+        "${CHECK_INCLUDE_FILE_C_INCLUDE_DIRS}"
+        OUTPUT_VARIABLE OUTPUT)
+
+    if(${ARGC} EQUAL 3)
+        set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS_SAVE})
+    endif()
+
+    if(${VARIABLE})
+        if(NOT CMAKE_REQUIRED_QUIET)
+            message(STATUS "Looking for ${INCLUDE} - found")
+        endif()
+        set(${VARIABLE} 1 CACHE INTERNAL "Have include ${INCLUDE}")
+        file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
+            "Determining if the include file ${INCLUDE} "
+            "exists passed with the following output:\n"
+            "${OUTPUT}\n\n")
+    else()
+        if(NOT CMAKE_REQUIRED_QUIET)
+            message(STATUS "Looking for ${INCLUDE} - not found")
+        endif()
+        set(${VARIABLE} "" CACHE INTERNAL "Have include ${INCLUDE}")
+        file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
+            "Determining if the include file ${INCLUDE} "
+            "exists failed with the following output:\n"
+            "${OUTPUT}\n\n")
+    endif()
+endmacro()
diff --git a/pub/cmake/modules/CheckCppIncludeFile.cpp.in b/pub/cmake/modules/CheckCppIncludeFile.cpp.in
new file mode 100644
index 00000000..ddfbee8b
--- /dev/null
+++ b/pub/cmake/modules/CheckCppIncludeFile.cpp.in
@@ -0,0 +1,13 @@
+#include <${CHECK_INCLUDE_FILE_VAR}>
+
+#ifdef __CLASSIC_C__
+int main()
+{
+  return 0;
+}
+#else
+int main(void)
+{
+  return 0;
+}
+#endif
diff --git a/pub/cmake/modules/FindYamlCpp.cmake b/pub/cmake/modules/FindYamlCpp.cmake
deleted file mode 100644
index e7f31e6b..00000000
--- a/pub/cmake/modules/FindYamlCpp.cmake
+++ /dev/null
@@ -1,68 +0,0 @@
-message( "FindYamlCpp.cmake begin" )
-
-# Locate yaml-cpp
-#
-# from http://code.google.com/p/yaml-cpp/issues/attachmentText?id=127&aid=1270002000&name=FindYamlCpp.cmake&token=59a4fbf54107b93367255c9d36464e81
-# also at https://github.com/supercollider/supercollider/blob/master/cmake_modules/FindYamlCpp.cmake
-#
-# This module defines
-# YAMLCPP_FOUND, if false, do not try to link to yaml-cpp
-# YAMLCPP_LIBRARY, where to find yaml-cpp
-# YAMLCPP_INCLUDE_DIR, where to find yaml.h
-#
-# By default, the dynamic libraries of yaml-cpp will be found. To find the static ones instead,
-# you must set the YAMLCPP_STATIC_LIBRARY variable to TRUE before calling find_package(YamlCpp ...).
-#
-# If yaml-cpp is not installed in a standard path, you can use the YAMLCPP_DIR CMake variable
-# to tell CMake where yaml-cpp is.
-
-# attempt to find static library first if this is set
-if(YAMLCPP_STATIC_LIBRARY)
-  set(YAMLCPP_STATIC libyaml-cpp.a)
-endif()
-
-# find the yaml-cpp include directory
-find_path(YAMLCPP_INCLUDE_DIR yaml-cpp/yaml.h
-PATH_SUFFIXES include
-PATHS
-~/Library/Frameworks/yaml-cpp/include/
-/Library/Frameworks/yaml-cpp/include/
-/usr/local/include/yaml-cpp/
-/usr/local/include/
-/usr/include/yaml-cpp/
-/usr/include/
-/sw/yaml-cpp/ # Fink
-/opt/local/yaml-cpp/ # DarwinPorts
-/opt/csw/yaml-cpp/ # Blastwave
-/opt/yaml-cpp/
-${YAMLCPP_DIR}/include/)
-
-# find the yaml-cpp library
-find_library(YAMLCPP_LIBRARY
-NAMES ${YAMLCPP_STATIC} yaml-cpp
-PATH_SUFFIXES lib64 lib
-PATHS ~/Library/Frameworks
-/Library/Frameworks
-/usr/local
-/usr
-/sw
-/opt/local
-/opt/csw
-/opt
-${YAMLCPP_DIR}/lib)
-
-# handle the QUIETLY and REQUIRED arguments and set YAMLCPP_FOUND to TRUE if all listed variables are TRUE
-include(FindPackageHandleStandardArgs)
-FIND_PACKAGE_HANDLE_STANDARD_ARGS(YAMLCPP DEFAULT_MSG YAMLCPP_INCLUDE_DIR YAMLCPP_LIBRARY)
-mark_as_advanced(YAMLCPP_INCLUDE_DIR YAMLCPP_LIBRARY)
-
-if( YAMLCPP_FOUND )
-#  if( NOT YAMLCPP_FIND_QUIETLY )
-    message( STATUS "FindYAMLCPP: Found both YAMLCPP headers and library" )
-#  endif( NOT YAMLCPP_FIND_QUIETLY )
-else( YAMLCPP_FOUND )
-#  if( YAMLCPP_FIND_REQUIRED )
-    message( FATAL_ERROR "FindYAMLCPP: Could not find YAMLCPP headers or library" )
-#  endif( YAMLCPP_FIND_REQUIRED )
-endif( YAMLCPP_FOUND )
-message( "FindYamlCpp.cmake end" )
diff --git a/pub/cmake/modules/FindYamlCpp05.cmake b/pub/cmake/modules/FindYamlCpp05.cmake
new file mode 100644
index 00000000..99a52853
--- /dev/null
+++ b/pub/cmake/modules/FindYamlCpp05.cmake
@@ -0,0 +1,86 @@
+message( "FindYamlCpp05.cmake begin" )
+
+# Locate yaml-cpp with API version 0.5
+#
+# This module defines
+# YAMLCPP_FOUND, if false, do not try to link to yaml-cpp
+# YAMLCPP_LIBRARY, where to find yaml-cpp
+# YAMLCPP_INCLUDE_DIR, where to find yaml.h
+#
+# By default, the dynamic libraries of yaml-cpp will be found. To find the static ones instead,
+# you must set the YAMLCPP_STATIC_LIBRARY variable to TRUE before calling find_package(YamlCpp ...).
+#
+# If yaml-cpp is not installed in a standard path, you can use the YAMLCPP_DIR CMake variable
+# to tell CMake where yaml-cpp is.
+
+include (CheckCppIncludeFile)
+include (CheckCXXSourceRuns)
+
+# find the yaml-cpp include directory
+find_path(YAMLCPP_INCLUDE_DIR yaml-cpp/yaml.h
+    PATH_SUFFIXES include
+    PATHS
+    ~/Library/Frameworks/yaml-cpp/include/
+    /Library/Frameworks/yaml-cpp/include/
+    /usr/local/include/yaml-cpp/
+    /usr/local/include/
+    /usr/include/yaml-cpp/
+    /usr/include/
+    /sw/yaml-cpp/ # Fink
+    /opt/local/yaml-cpp/ # DarwinPorts
+    /opt/csw/yaml-cpp/ # Blastwave
+    /opt/yaml-cpp/
+    ${YAMLCPP_DIR}/include/)
+
+set(CMAKE_REQUIRED_INCLUDES ${YAMLCPP_INCLUDE_DIR})
+set(CMAKE_REQUIRED_QUIET True)
+
+# first look for outdated yaml-cpp0.3 include files
+unset(YAMLCPP_FOUND_03 CACHE)
+check_include_file("yaml-cpp/aliasmanager.h" YAMLCPP_FOUND_03)
+if(${YAMLCPP_FOUND_03})
+    message(WARNING "Found include file for libyaml-cpp0.3. Most probably this precludes libyaml-cpp0.5 from being properly installed")
+endif()
+
+# now look for needed yaml-cpp0.5 include files
+unset(YAMLCPP_FOUND_05 CACHE)
+check_include_file("yaml-cpp/node/detail/iterator_fwd.h" YAMLCPP_FOUND_05)
+if(${YAMLCPP_FOUND_05})
+else()
+    message(FATAL_ERROR "Include file for libyaml-cpp0.5 not found")
+endif()
+
+# attempt to find static library first if this is set
+if(YAMLCPP_STATIC_LIBRARY)
+    set(YAMLCPP_STATIC libyaml-cpp.a)
+endif()
+
+# find the yaml-cpp library
+find_library(YAMLCPP_LIBRARY
+    NAMES ${YAMLCPP_STATIC} yaml-cpp
+    PATH_SUFFIXES lib64 lib
+    PATHS ~/Library/Frameworks
+    /Library/Frameworks
+    /usr/local
+    /usr
+    /sw
+    /opt/local
+    /opt/csw
+    /opt
+    ${YAMLCPP_DIR}/lib)
+
+# try to compile, link, and run a test program
+unset(YAMLCPP_RUNS CACHE)
+set(CMAKE_REQUIRED_LIBRARIES yaml-cpp)
+check_cxx_source_runs("#include \"yaml-cpp/yaml.h\"\n#include <assert.h>\nint main() {\n    YAML::Node node = YAML::Load(\"[1, 2, 3]\");\n    assert(node.IsSequence());\n}" YAMLCPP_RUNS)
+if(${YAMLCPP_RUNS})
+else()
+    message(FATAL_ERROR "Test of libyaml-cpp0.5 failed")
+endif()
+
+# handle the QUIETLY and REQUIRED arguments and set YAMLCPP_FOUND to TRUE if all listed variables are TRUE
+include(FindPackageHandleStandardArgs)
+find_package_handle_standard_args(YAMLCPP DEFAULT_MSG YAMLCPP_INCLUDE_DIR YAMLCPP_LIBRARY)
+mark_as_advanced(YAMLCPP_INCLUDE_DIR YAMLCPP_LIBRARY)
+
+message( "FindYamlCpp05.cmake end" )
-- 
GitLab