windowscmakemsbuildlibpng

CMake find_package can't find libpng (16)


I'm not the first to run into libpng issues, especially when I want to link to self-built sources on Windows. I'm using the libpng1638 sources from https://github.com/glennrp/libpng. Semi official - the reason for this version is it has a CMake build/install script.

zlib is built/found and the library installs in c:\Program Files\libpng (I have set CMAKE_INSTALL_PREFIX for this)

The problem occurs when doing a find_package(PNG 16). With debug flag on, a number of search directories is listed, but ultimately it fails to find the PNG-config.cmake script that can link up to the installed paths and files.

It doesn't seem to help when I set CMAKE_PREFIX_PATH to the correct folder, and the reason is explained below.

This the command I used in the script (zlib is already found):

set(CMAKE_FIND_DEBUG_MODE TRUE)
find_package(PNG 16         PATHS "C:\\Program Files\\libpng\\lib\\libpng")
set(CMAKE_FIND_DEBUG_MODE FALSE)

This is the output of the debug trace:

...
find_package considered the following locations for the Config module:
  ...
  C:/Program Files/CMake/PNGConfig.cmake
  C:/Program Files/CMake/png-config.cmake
  C:/Program Files (x86)/PNGConfig.cmake
  C:/Program Files (x86)/png-config.cmake
  C:/Program Files/libpng/lib/libpng/PNGConfig.cmake
  C:/Program Files/libpng/lib/libpng/png-config.cmake

Indeed, no png-config.cmake file can be found anywhere on my system. In the source CMakeLists.txt of libpng, the section to create these XXX-config.cmake scripts has been deliberately disabled for Win_32 systems:

  # Install the pkg-config files.
  if(NOT CMAKE_HOST_WIN32 OR CYGWIN OR MINGW)
    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpng.pc
            DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
    install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/libpng-config
            DESTINATION ${CMAKE_INSTALL_BINDIR})
    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}.pc
            DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
    install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/${PNGLIB_NAME}-config
            DESTINATION ${CMAKE_INSTALL_BINDIR})
  endif()

Since I don't want to dabble in the source files (which ultimately are pulled from the web directly), I want to understand. Why would this be disabled for native Win32 builds? (most of the online info uses the Linux subsystem or a package manager.. )

Other packages like zlib have their XXX-config.cmake files properly copied to the install folders where FindXXX.cmake scripts can pick them up properly.

For completeness, here is the output of the libpng build (msvc)

  -- Install configuration: "Debug"
  -- Installing: C:/Program Files/libpng/lib/libpng16d.lib
  -- Installing: C:/Program Files/libpng/bin/libpng16d.dll
  -- Installing: C:/Program Files/libpng/lib/libpng16_staticd.lib
  -- Installing: C:/Program Files/libpng/include/png.h
  -- Installing: C:/Program Files/libpng/include/pngconf.h
  -- Installing: C:/Program Files/libpng/include/pnglibconf.h
  -- Installing: C:/Program Files/libpng/include/libpng16/png.h
  -- Installing: C:/Program Files/libpng/include/libpng16/pngconf.h
  -- Installing: C:/Program Files/libpng/include/libpng16/pnglibconf.h
  -- Installing: C:/Program Files/libpng/bin/pngfix.exe
  -- Installing: C:/Program Files/libpng/bin/png-fix-itxt.exe
  -- Installing: C:/Program Files/libpng/share/man/man3/libpng.3
  -- Installing: C:/Program Files/libpng/share/man/man3/libpngpf.3
  -- Installing: C:/Program Files/libpng/share/man/man5/png.5
  -- Installing: C:/Program Files/libpng/lib/libpng/libpng16.cmake
  -- Installing: C:/Program Files/libpng/lib/libpng/libpng16-debug.cmake

All insight greatly appreciated!

Update 1

After fiddling with the install commands, I have to conclude that libpng-libpng16.zip from https://github.com/glennrp/libpng is not maintained with Windows as build target in mind.

lpng1637.zip from https://sourceforge.net/projects/libpng/ has the same problem.

Update2 :

So far I have tried setting PNG_DIR, setting the CMAKE_PREFIX_PATH and a number of other things that failed. Just one seems to work, which is setting the environment PNG_ROOT variable to the correct installation folder. This is dead ugly, but it seems to be the only option that works.


Solution

  • So, for people who, like me, are trying to use freetype2 in a Windows project, here's how to build the dependencies:

    I did the same for dependencies Brotli and Harfbuzz, but no luck so far. Harfbuzz seems to have some strange chicken/egg relationship to freetype2.

    For setting up the build for freetype2 with CMake, after building the dependencies, these flags work:

    -DFT_DISABLE_HARFBUZZ=True 
    -DFT_REQUIRE_PNG=True 
    -DFT_DISABLE_BROTLI=True 
    -DFT_REQUIRE_BZIP2=True 
    -DCMAKE_PREFIX_PATH="C:\Program Files\libpng;C:\Program Files\bzip2;C:\Program Files\brotli;C:\Program Files\zlib;C:\Program Files\harfbuzz"
    

    Note that libpng pulls in zlib as dependency, so there's no real need to specify that flag imho, so I'm not touching it.