From 04686340c4cd375a17d60d31bf6943367cc33214 Mon Sep 17 00:00:00 2001
From: Khem Raj <raj.khem@gmail.com>
Date: Sat, 21 Jan 2023 03:09:08 -0800
Subject: [PATCH] cmake: Do not use -isystem

isystem dirs are searched before the regular system dirs
this exposes an interesting include ordering problem when using
clang + libc++, when including C++ headers like <cstdlib>

cstdlib includes stdlib.h and in case of libc++, this should be coming
from libc++ as well, which is then eventually including system stdlib.h

libc++ has added a check for checking this order recently, which means
if cstlib ends up including system stdlib.h before libc++ provided
stdlib.h it errors out

/mnt/b/yoe/master/build/tmp/work/riscv64-yoe-linux/thrift/0.17.0-r0/recipe-sysroot/usr/include/c++/v1/cstdlib:90:5: error: <cstdlib> tried including <stdlib.h> but didn't find libc++'s <stdlib.h> header.           This usually means that your header search paths are not configured properly.           The header search paths should contain the C++ Standard Library headers before           any C Standard Library, and you are probably using compiler flags that make that           not be the case.
    ^

The reason is that include_directories with SYSTEM property adds the
directory via -system and some of these directories point to sysroot
e.g. OPENSSL_INCLUDE_DIR which ends up adding -isystem
<sysroot>/usr/include and causes the system stdlib.h to included before
libc++ stdlib.h

A fix is to use -idirafter which preserved the effects of system headers
but instead of prepending, it will append to system headers and the
issue is addressed

Signed-off-by: Khem Raj <raj.khem@gmail.com>

---
Upstream-Status: Pending

 CMakeLists.txt         |  4 ++--
 glib/CMakeLists.txt    |  4 ++--
 qt5/src/CMakeLists.txt |  4 ++--
 qt6/src/CMakeLists.txt |  4 ++--
 test/CMakeLists.txt    |  6 +++---
 utils/CMakeLists.txt   | 10 +++++-----
 6 files changed, 16 insertions(+), 16 deletions(-)

--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -628,10 +628,10 @@ add_library(poppler ${poppler_SRCS} ${LI
 if (OpenJPEG_FOUND)
   # check if we can remove this when we depend on newer openjpeg versions, 2.5 seems fixed
   # target openjp2 may lack interface include directories
-  target_include_directories(poppler SYSTEM PRIVATE ${OPENJPEG_INCLUDE_DIRS})
+  target_include_directories(poppler  PRIVATE ${OPENJPEG_INCLUDE_DIRS})
 endif()
 if(USE_CMS)
-  target_include_directories(poppler SYSTEM PRIVATE ${LCMS2_INCLUDE_DIR})
+  target_include_directories(poppler  PRIVATE ${LCMS2_INCLUDE_DIR})
 endif()
 generate_export_header(poppler BASE_NAME poppler-private EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/poppler_private_export.h")
 set_target_properties(poppler PROPERTIES
--- a/glib/CMakeLists.txt
+++ b/glib/CMakeLists.txt
@@ -4,7 +4,7 @@ include_directories(
 )
 
 include_directories(
-  SYSTEM
+
   ${GLIB2_INCLUDE_DIRS}
   ${CAIRO_INCLUDE_DIRS}
 )
@@ -96,7 +96,7 @@ if(MINGW AND BUILD_SHARED_LIBS)
     set_target_properties(poppler-glib PROPERTIES SUFFIX "-${POPPLER_GLIB_SOVERSION}${CMAKE_SHARED_LIBRARY_SUFFIX}")
 endif()
 target_link_libraries(poppler-glib poppler PkgConfig::GLIB2 ${CAIRO_LIBRARIES} Freetype::Freetype)
-target_include_directories(poppler-glib SYSTEM PRIVATE ${CAIRO_INCLUDE_DIRS})
+target_include_directories(poppler-glib  PRIVATE ${CAIRO_INCLUDE_DIRS})
 install(TARGETS poppler-glib RUNTIME DESTINATION bin LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
 
 if (ENABLE_NSS3)
--- a/qt5/src/CMakeLists.txt
+++ b/qt5/src/CMakeLists.txt
@@ -45,11 +45,11 @@ if(MINGW AND BUILD_SHARED_LIBS)
 endif()
 target_link_libraries(poppler-qt5 poppler Qt5::Core Qt5::Gui Qt5::Xml Freetype::Freetype)
 if (ENABLE_NSS3)
-    target_include_directories(poppler-qt5 SYSTEM PRIVATE ${NSS3_INCLUDE_DIRS})
+    target_include_directories(poppler-qt5  PRIVATE ${NSS3_INCLUDE_DIRS})
 endif()
 if(USE_CMS)
     target_link_libraries(poppler-qt5 poppler ${LCMS2_LIBRARIES})
-    target_include_directories(poppler-qt5 SYSTEM PRIVATE ${LCMS2_INCLUDE_DIR})
+    target_include_directories(poppler-qt5  PRIVATE ${LCMS2_INCLUDE_DIR})
 endif()
 install(TARGETS poppler-qt5 RUNTIME DESTINATION bin LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
 
--- a/qt6/src/CMakeLists.txt
+++ b/qt6/src/CMakeLists.txt
@@ -45,11 +45,11 @@ if(MINGW AND BUILD_SHARED_LIBS)
 endif()
 target_link_libraries(poppler-qt6 poppler Qt6::Core Qt6::Gui Freetype::Freetype)
 if (ENABLE_NSS3)
-    target_include_directories(poppler-qt6 SYSTEM PRIVATE ${NSS3_INCLUDE_DIRS})
+    target_include_directories(poppler-qt6  PRIVATE ${NSS3_INCLUDE_DIRS})
 endif()
 if(USE_CMS)
     target_link_libraries(poppler-qt6 poppler ${LCMS2_LIBRARIES})
-    target_include_directories(poppler-qt6 SYSTEM PRIVATE ${LCMS2_INCLUDE_DIR})
+    target_include_directories(poppler-qt6  PRIVATE ${LCMS2_INCLUDE_DIR})
 endif()
 install(TARGETS poppler-qt6 RUNTIME DESTINATION bin LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
 
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -23,7 +23,7 @@ if (GTK_FOUND)
   )
   poppler_add_test(gtk-test BUILD_GTK_TESTS ${gtk_splash_test_SRCS})
   target_link_libraries(gtk-test ${CAIRO_LIBRARIES} poppler-glib PkgConfig::GTK3)
-  target_include_directories(gtk-test SYSTEM PRIVATE ${CAIRO_INCLUDE_DIRS})
+  target_include_directories(gtk-test  PRIVATE ${CAIRO_INCLUDE_DIRS})
 
   if (HAVE_CAIRO)
 
@@ -35,7 +35,7 @@ if (GTK_FOUND)
     )
     poppler_add_test(pdf-inspector BUILD_GTK_TESTS ${pdf_inspector_SRCS})
     target_link_libraries(pdf-inspector ${CAIRO_LIBRARIES} Freetype::Freetype ${common_libs} PkgConfig::GTK3 poppler)
-    target_include_directories(pdf-inspector SYSTEM PRIVATE ${CAIRO_INCLUDE_DIRS})
+    target_include_directories(pdf-inspector  PRIVATE ${CAIRO_INCLUDE_DIRS})
     target_compile_definitions(pdf-inspector PRIVATE -DSRC_DIR="${CMAKE_CURRENT_SOURCE_DIR}")
   endif ()
 
@@ -59,7 +59,7 @@ if (HAVE_CAIRO)
       )
     add_executable(cairo-thread-test ${cairo_thread_test_SRCS})
     target_link_libraries(cairo-thread-test ${CAIRO_LIBRARIES} Freetype::Freetype Threads::Threads poppler)
-    target_include_directories(cairo-thread-test SYSTEM PRIVATE ${CAIRO_INCLUDE_DIRS})
+    target_include_directories(cairo-thread-test  PRIVATE ${CAIRO_INCLUDE_DIRS})
   endif ()
 endif ()
 
--- a/utils/CMakeLists.txt
+++ b/utils/CMakeLists.txt
@@ -18,7 +18,7 @@ add_executable(pdftoppm ${pdftoppm_SOURC
 target_link_libraries(pdftoppm ${common_libs})
 if(LCMS2_FOUND)
   target_link_libraries(pdftoppm ${LCMS2_LIBRARIES})
-  target_include_directories(pdftoppm SYSTEM PRIVATE ${LCMS2_INCLUDE_DIR})
+  target_include_directories(pdftoppm  PRIVATE ${LCMS2_INCLUDE_DIR})
 endif()
 install(TARGETS pdftoppm DESTINATION bin)
 install(FILES pdftoppm.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
@@ -39,10 +39,10 @@ if (HAVE_CAIRO)
   add_definitions(${CAIRO_CFLAGS})
   add_executable(pdftocairo ${pdftocairo_SOURCES})
   target_link_libraries(pdftocairo ${CAIRO_LIBRARIES} Freetype::Freetype ${common_libs})
-  target_include_directories(pdftocairo SYSTEM PRIVATE ${CAIRO_INCLUDE_DIRS})
+  target_include_directories(pdftocairo  PRIVATE ${CAIRO_INCLUDE_DIRS})
   if(LCMS2_FOUND)
     target_link_libraries(pdftocairo ${LCMS2_LIBRARIES})
-    target_include_directories(pdftocairo SYSTEM PRIVATE ${LCMS2_INCLUDE_DIR})
+    target_include_directories(pdftocairo  PRIVATE ${LCMS2_INCLUDE_DIR})
   endif()
   install(TARGETS pdftocairo DESTINATION bin)
   install(FILES pdftocairo.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
@@ -109,7 +109,7 @@ if (ENABLE_SIGNATURES)
   add_executable(pdfsig ${pdfsig_SOURCES})
   target_link_libraries(pdfsig ${common_libs})
   if (ENABLE_NSS3)
-    target_include_directories(pdfsig SYSTEM PRIVATE ${NSS3_INCLUDE_DIRS})
+    target_include_directories(pdfsig PRIVATE ${NSS3_INCLUDE_DIRS})
   endif()
   if (Intl_FOUND)
     target_link_libraries(pdfsig Intl::Intl)
@@ -127,7 +127,7 @@ add_executable(pdftops ${pdftops_SOURCES
 target_link_libraries(pdftops ${common_libs})
 if(LCMS2_FOUND)
   target_link_libraries(pdftops ${LCMS2_LIBRARIES})
-  target_include_directories(pdftops SYSTEM PRIVATE ${LCMS2_INCLUDE_DIR})
+  target_include_directories(pdftops  PRIVATE ${LCMS2_INCLUDE_DIR})
 endif()
 install(TARGETS pdftops DESTINATION bin)
 install(FILES pdftops.1 DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
