← All tasks
cppopencv/opencv #28657Not a task: not reproduced

imgcodecs(png): Unexpected coexistence of BUILD_PNG=ON and PNG_FOUND=ON on macOS

envgap__opencv__opencv-28657

01 / FAILURE SIGNATURE

As reported upstream

@@ -13,6 +13,8 @@ FATAL: In-source builds are not allowed.
Not a benchmark task.
  • In a clean container the reported failure did not reproduce, or the known fix did not make the project run.

02 / ENVIRONMENT RECIPE

Base commit
9929b5ceb915fd48dd9281aef6e5f5cc68705188
Manifest
CMakeLists.txt
Reproduce
Awaiting issue-specific recipe
Run under trace
Awaiting a meaningful runtime command

03 / ORIGINAL ISSUE TEXT

opencv/opencv #28657 · read the original issue
### System Information

OpenCV version: 4.x 
Operating System / Platform: macOS
Compiler & compiler version: (maybe clang)

https://github.com/opencv/opencv/actions/runs/22807138466/job/66408557744?pr=28615#step:12:577
```
--     Host:                        Darwin 21.5.0 x86_64
--     CMake:                       3.20.1
--     C++ Compiler:                /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  (ver 12.0.5.12050022)
```

https://github.com/opencv/opencv/actions/runs/22807138466/job/66408557694?pr=28615#step:12:549
```
--     Host:                        Darwin 21.5.0 arm64
--     CMake:                       3.25.2
--     C++ Compiler:                /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  (ver 13.1.6.13160021)
```

### Detailed description

I'm sorry I have no macOS environments. This is only investigation.

I've observed a strange behavior on macOS where BUILD_PNG=ON and PNG_FOUND=ON coexist, which seems to contradict OpenCV's logic.

## Default Value
On macOS, BUILD_PNG defaults to ON.

https://github.com/opencv/opencv/blob/00833f98d0aed4e299a8ba5723d06456e6d077ee/CMakeLists.txt#L205

## Expected Logic: In OpenCVFindLibsGrfmt.cmake
if BUILD_PNG is ON, PNG_FOUND should be cleared using ocv_clear_vars(PNG_FOUND). And `find_package(PNG)` will not be called.

https://github.com/opencv/opencv/blob/00833f98d0aed4e299a8ba5723d06456e6d077ee/cmake/OpenCVFindLibsGrfmt.cmake#L332-L338


## Actual Issue
However, the final status report shows `PNG: libpng` without `build`. This means `PNG_FOUND = ON`. 
And `SIMD Support Resuest:` means `BUILD_PNG = ON`. It is strange condition.

```
--     PNG:                         libpng (ver 1.6.37)
--       SIMD Support Request:      YES
--       SIMD Support:              YES (Intel SSE)
```

https://github.com/opencv/opencv/blob/00833f98d0aed4e299a8ba5723d06456e6d077ee/CMakeLists.txt#L1540-L1545

## Expected reason
This suggests that some other module or dependency might be calling `find_package(PNG)` internally after the initial check, overwriting the PNG_FOUND variable.

For example, calling `find_package(PNG)` after `set(HAVE_PNG YES)` makes the same result.
```diff
   set(HAVE_PNG YES)
+
+  find_package(PNG) #debug
 endif()
```

```
--   Media I/O:
--     ZLib:                        /usr/lib/x86_64-linux-gnu/libz.so (ver 1.3)
--     JPEG:                        /usr/lib/x86_64-linux-gnu/libjpeg.so (ver 80)
--     WEBP:                        /usr/lib/x86_64-linux-gnu/libwebp.so (ver decoder: 0x0209, encoder: 0x020f, demux: 0x0107)
--     AVIF:                        avif (ver 1.3.0)
--     PNG:                         libpng (ver 1.6.43)
--       SIMD Support Request:      YES
--       SIMD Support:              YES (Intel SSE)
--     TIFF:                        /usr/lib/x86_64-linux-gnu/libtiff.so (ver 42 / 4.5.1)
--     JPEG 2000:                   OpenJPEG (ver 2.5.0)
--     OpenEXR:                     OpenEXR::OpenEXR (ver 3.4.4)
```

To identify the root cause, I suggest using variable_watch to trace the variable's state. By adding the following snippet to the top-level CMakeLists.txt, we can see exactly where and why PNG_FOUND is cleared/modified/read.

```
kmtr@kmtr-VMware-Virtual-Platform:~/work/opencv4$ git diff
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8e30518445..019cd7209c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -13,6 +13,8 @@ FATAL: In-source builds are not allowed.
 ")
 endif()

+variable_watch(PNG_FOUND)
+
 include(cmake/OpenCVMinDepVersions.cmake)

 if(CMAKE_SYSTEM_NAME MATCHES WindowsPhone OR CMAKE_SYSTEM_NAME MATCHES WindowsStore)
```

We can following trace logs.
```
CMake Debug Log at cmake/OpenCVUtils.cmake:415 (unset):
  Variable "PNG_FOUND" was accessed using REMOVED_ACCESS with value "".
Call Stack (most recent call first):
  cmake/OpenCVFindLibsGrfmt.cmake:334 (ocv_clear_vars)
  CMakeLists.txt:816 (include)


CMake Debug Log at cmake/OpenCVFindLibsGrfmt.cmake:340 (if):
  Variable "PNG_FOUND" was accessed using UNKNOWN_READ_ACCESS with value "".
Call Stack (most recent call first):
  CMakeLists.txt:816 (include)
```

### Steps to reproduce

This problem occurs in CI.

See https://github.com/opencv/opencv/actions/runs/22807138466/job/66408557694?pr=28615
See https://github.com/opencv/opencv/actions/runs/22807138466/job/66408557744?pr=28615

### Issue submission checklist

- [x] I report the issue, it's not a question
- [x] I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
- [x] I updated to the latest OpenCV version and the issue is still there
- [x] There is reproducer code and related data files (videos, images, onnx, etc)
Continue on GitHub ↗

04 / LABELS

Labels from the report text only; not yet run

No supported category has been assigned.

Label rules and the text that matched
[]