cmake: verify public header interface versions
authorMichael Wallner <mike@php.net>
Tue, 18 Aug 2020 07:31:43 +0000 (09:31 +0200)
committerMichael Wallner <mike@php.net>
Tue, 18 Aug 2020 07:31:43 +0000 (09:31 +0200)
CMake/InstallPublicHeaders.cmake
CMakeLists.txt

index 56ebb0c42247df0b8b8058711189021b181d3cf1..70cfe3d02c092864373daf4d63ce1076e11681ed 100644 (file)
@@ -1,4 +1,35 @@
 function(install_public_headers DIRECTORY)
+    # validate current directory
+    string(FIND ${CMAKE_CURRENT_SOURCE_DIR} /include/ INCDIR REVERSE)
+    string(FIND ${CMAKE_CURRENT_SOURCE_DIR} /src/ SRCDIR REVERSE)
+    if((INCDIR GREATER_EQUAL 0) OR (SRCDIR GREATER_EQUAL 0))
+        if(INCDIR GREATER_EQUAL 0)
+            math(EXPR POSITION "${INCDIR} + 9")
+        else()
+            math(EXPR POSITION "${SRCDIR} + 5")
+        endif()
+        string(SUBSTRING ${CMAKE_CURRENT_SOURCE_DIR} ${POSITION} -1 CHKDIR)
+        if(NOT CHKDIR STREQUAL DIRECTORY)
+            message(SEND_ERROR "install_public_headers() directories do not match: ${CHKDIR} != ${DIRECTORY}")
+            set(ENV{INVALID_CONFIGURATION} 1)
+        endif()
+    endif()
+
+    # validate public interface version
+    get_filename_component(TOP ${DIRECTORY} NAME)
+    string(FIND ${TOP} "-" DASH)
+    if(DASH GREATER 0)
+        string(SUBSTRING ${TOP} 0 ${DASH} LIBRARY)
+        string(TOUPPER ${LIBRARY} LIBRARY_UCASE)
+        math(EXPR DASH "${DASH} + 1")
+        string(SUBSTRING ${TOP} ${DASH} -1 VERSION)
+        if(NOT ${LIBRARY_UCASE}_VERSION_INC VERSION_EQUAL ${VERSION})
+            message(SEND_ERROR "${LIBRARY} public include directory version ${VERSION} != " ${${LIBRARY_UCASE}_VERSION_INC})
+            set(ENV{INVALID_CONFIGURATION} 1)
+        endif()
+    endif()
+
+    # change local includes to system includes
     foreach(HEADER IN LISTS ARGN)
         file(READ ${HEADER} HEADER_IN)
         string(REGEX REPLACE "include *\"([^\"]+)\"" "include <\\1>" HEADER_OUT "${HEADER_IN}")
index 2db4ab3a99f75bc6b90b33608b12fc6cd937b6c9..f559febd4e1796228d07c10297d4b77d031cc17a 100644 (file)
@@ -60,4 +60,6 @@ if(BUILD_DOCS OR BUILD_DOCSONLY)
     add_subdirectory(docs)
 endif()
 
-
+if(ENV{INVALID_CONFIGURATION})
+    message(FATAL_ERROR "invalid configuration -- giving up")
+endif()