diff --git a/CMakeLists.txt b/CMakeLists.txt index 1edd5f53cd..1d49243093 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1540,7 +1540,11 @@ if(WITH_SPNG) status(" Metadata Support:" "EXIF XMP ICC") # SPNG does not support cICP chunk. elseif(WITH_PNG OR HAVE_PNG) - status(" PNG:" PNG_FOUND THEN "${PNG_LIBRARY} (ver ${PNG_VERSION_STRING})" ELSE "build (ver ${PNG_VERSION_STRING})") + if(BUILD_PNG) + status(" PNG:" FALSE THEN "" ELSE "build (ver ${PNG_VERSION_STRING})") + else() + status(" PNG:" PNG_FOUND THEN "${PNG_LIBRARY} (ver ${PNG_VERSION_STRING})" ELSE "build (ver ${PNG_VERSION_STRING})") + endif() if(BUILD_PNG AND PNG_HARDWARE_OPTIMIZATIONS) status(" SIMD Support Request:" "YES") if(PNG_INTEL_SSE) diff --git a/cmake/OpenCVFindLibsGrfmt.cmake b/cmake/OpenCVFindLibsGrfmt.cmake index c3c358f6fc..15e8a133cd 100644 --- a/cmake/OpenCVFindLibsGrfmt.cmake +++ b/cmake/OpenCVFindLibsGrfmt.cmake @@ -333,7 +333,7 @@ if(NOT HAVE_SPNG AND WITH_PNG) if(BUILD_PNG) ocv_clear_vars(PNG_FOUND) else() - ocv_clear_internal_cache_vars(PNG_LIBRARY PNG_INCLUDE_DIR) + ocv_clear_internal_cache_vars(PNG_LIBRARY PNG_INCLUDE_DIR PNG_PNG_INCLUDE_DIR) find_package(PNG QUIET) endif() @@ -348,6 +348,18 @@ if(NOT HAVE_SPNG AND WITH_PNG) ocv_parse_header_version(PNG "${PNG_INCLUDE_DIR}/png.h" PNG_LIBPNG_VER_STRING) endif() + if(BUILD_PNG) + # Downstream find_package(PNG) calls from transitive dependencies + # (included via include() in the same scope) may overwrite PNG_FOUND + # and related variables. PNG_LIBRARY is naturally protected by + # FindPNG's "if(NOT PNG_LIBRARY)" guard, but PNG_PNG_INCLUDE_DIR + # (searched via find_path without a guard) and its derived variables + # (PNG_INCLUDE_DIR, PNG_LIBRARIES, PNG_VERSION_STRING) are not. + # Lock PNG_PNG_INCLUDE_DIR so that find_path() respects the cached + # bundled path and skips the system search. + set(PNG_PNG_INCLUDE_DIR "${PNG_INCLUDE_DIR}" CACHE INTERNAL "PNG include dir (bundled)") + endif() + set(HAVE_PNG YES) endif()