Subject: add pkgconfig checks
From: Julian Ospald <[email-masked]>
Date: Wed Mar 20 22:10:22 UTC 2013

https://bugs.gentoo.org/show_bug.cgi?id=462494

--- nvidia-texture-tools/cmake/FindOpenEXR.cmake
+++ nvidia-texture-tools/cmake/FindOpenEXR.cmake
@@ -7,11 +7,15 @@
 # OPENEXR_LIBRARIES = libraries that are needed to use OpenEXR.
 # 
 
-INCLUDE(FindZLIB)
+IF(UNIX)
+	INCLUDE(FindPkgConfig)
+	pkg_check_modules(_PKG_OPENEXR OpenEXR)
+ENDIF(UNIX)
 
+find_package(ZLIB REQUIRED)
 
-IF(ZLIB_FOUND)
-
+IF(NOT _PKG_OPENEXR_FOUND)
+	
 	SET(LIBRARY_PATHS 
 		/usr/lib
 		/usr/local/lib
@@ -46,10 +50,20 @@
 		NAMES IlmThread
 		PATHS ${LIBRARY_PATHS})
 
-ENDIF(ZLIB_FOUND)
+ENDIF(NOT _PKG_OPENEXR_FOUND)
 
 #MESSAGE(STATUS ${OPENEXR_IMATH_LIBRARY} ${OPENEXR_ILMIMF_LIBRARY} ${OPENEXR_IEX_LIBRARY} ${OPENEXR_HALF_LIBRARY} ${OPENEXR_ILMTHREAD_LIBRARY} ${ZLIB_LIBRARY})
 
+IF(_PKG_OPENEXR_FOUND)
+	SET(OPENEXR_FOUND TRUE)
+	foreach(i ${_PKG_OPENEXR_CFLAGS})
+		SET(OPENEXR_CFLAGS "${OPENEXR_CFLAGS} ${i}")
+	endforeach(i)
+	foreach(p ${_PKG_OPENEXR_LDFLAGS})
+		SET(OPENEXR_LDFLAGS "${OPENEXR_LDFLAGS} ${p}")
+	endforeach(p)
+	SET(OPENEXR_LDFLAGS "${OPENEXR_LDFLAGS} ${ZLIB_LIBRARY}")
+ELSE(_PKG_OPENEXR_FOUND)
 IF (OPENEXR_INCLUDE_PATH AND OPENEXR_IMATH_LIBRARY AND OPENEXR_ILMIMF_LIBRARY AND OPENEXR_IEX_LIBRARY AND OPENEXR_HALF_LIBRARY)
 	SET(OPENEXR_FOUND TRUE)
 	SET(OPENEXR_INCLUDE_PATHS ${OPENEXR_INCLUDE_PATH} CACHE STRING "The include paths needed to use OpenEXR")
@@ -65,6 +79,8 @@
 		MESSAGE(FATAL_ERROR "Could not find OpenEXR library")
 	ENDIF(OPENEXR_FIND_REQUIRED)
 ENDIF(OPENEXR_FOUND)
+ENDIF(_PKG_OPENEXR_FOUND)
+
 
 MARK_AS_ADVANCED(
 	OPENEXR_INCLUDE_PATHS
@@ -73,3 +89,4 @@
 	OPENEXR_IMATH_LIBRARY
 	OPENEXR_IEX_LIBRARY
 	OPENEXR_HALF_LIBRARY)
+
--- nvidia-texture-tools/src/nvimage/CMakeLists.txt
+++ nvidia-texture-tools/src/nvimage/CMakeLists.txt
@@ -44,10 +44,10 @@
 	INCLUDE_DIRECTORIES(${TIFF_INCLUDE_DIR})
 ENDIF(TIFF_FOUND)
 
-IF(OPENEXR_FOUND)
+IF(OPENEXR_FOUND AND NOT _PKG_OPENEXR_FOUND)
 	SET(LIBS ${LIBS} ${OPENEXR_LIBRARIES})
 	INCLUDE_DIRECTORIES(${OPENEXR_INCLUDE_PATHS})
-ENDIF(OPENEXR_FOUND)
+ENDIF(OPENEXR_FOUND AND NOT _PKG_OPENEXR_FOUND)
 
 # targets
 ADD_DEFINITIONS(-DNVIMAGE_EXPORTS)
@@ -61,6 +61,14 @@
 
 TARGET_LINK_LIBRARIES(nvimage ${LIBS} nvcore nvmath posh)
 
+IF(_PKG_OPENEXR_FOUND)
+	SET_TARGET_PROPERTIES(nvimage
+		PROPERTIES
+		COMPILE_FLAGS "${OPENEXR_CFLAGS}"
+		LINK_FLAGS "${OPENEXR_LDFLAGS}"
+	)
+ENDIF(_PKG_OPENEXR_FOUND)
+
 INSTALL(TARGETS nvimage
 	RUNTIME DESTINATION bin
 	LIBRARY DESTINATION lib