Commit 0cf422d4 authored by Davis King's avatar Davis King

Have CMake build a little test program to see if the system copy of libpng really works.

parent 0268e274
......@@ -430,16 +430,8 @@ if (NOT TARGET dlib)
endif()
if (DLIB_PNG_SUPPORT)
# try to find libpng
find_package(PNG QUIET)
# Make sure there isn't something wrong with the version of LIBPNG
# installed on this system. Also never link to anything from anaconda
# since it's probably broken.
if (PNG_FOUND AND NOT ("${PNG_INCLUDE_DIR}" MATCHES "(.*)(Ana|ana|mini)conda(.*)") AND NOT BUILDING_PYTHON_IN_MSVC)
set(CMAKE_REQUIRED_LIBRARIES ${PNG_LIBRARIES})
CHECK_FUNCTION_EXISTS(png_create_read_struct LIBPNG_IS_GOOD)
endif()
if (PNG_FOUND AND LIBPNG_IS_GOOD)
include(cmake_utils/find_libpng.cmake)
if (PNG_FOUND)
include_directories(${PNG_INCLUDE_DIR})
set (dlib_needed_libraries ${dlib_needed_libraries} ${PNG_LIBRARIES})
set(REQUIRES_LIBS " libpng")
......
#This script just runs CMake's built in PNG finding tool. But it also checks that the
#copy of libpng that cmake finds actually builds and links.
cmake_minimum_required(VERSION 2.8.12)
# Don't rerun this script if its already been executed.
if (DEFINED PNG_FOUND)
return()
endif()
find_package(PNG QUIET)
if(PNG_FOUND)
try_compile(test_for_libpng_worked
${PROJECT_BINARY_DIR}/test_for_libpng_build
${CMAKE_CURRENT_LIST_DIR}/test_for_libpng
test_if_libpng_is_broken)
message (STATUS "Found system copy of libpng: ${PNG_LIBRARIES}")
if(NOT test_for_libpng_worked)
set(PNG_FOUND 0)
message (STATUS "System copy of libpng is broken. Will build our own libpng and use that instead.")
endif()
endif()
cmake_minimum_required(VERSION 2.8.12)
project(test_if_libpng_is_broken)
find_package(PNG)
add_executable(libpng_test libpng_test.cpp)
target_link_libraries(libpng_test ${PNG_LIBRARIES})
// Copyright (C) 2019 Davis E. King (davis@dlib.net), Nils Labugt
// License: Boost Software License See LICENSE.txt for the full license.
#include <png.h>
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
void png_loader_user_error_fn_silent(png_structp png_struct, png_const_charp )
{
longjmp(png_jmpbuf(png_struct),1);
}
void png_loader_user_warning_fn_silent(png_structp , png_const_charp )
{
}
// This code doesn't really make a lot of sense. It's just calling all the libpng functions to make
// sure they can be compiled and linked.
int main() {
png_bytep* row_pointers_;
png_structp png_ptr_;
png_infop info_ptr_;
png_infop end_info_;
FILE *fp = fopen( "whatever.png", "rb" );
png_byte sig[8];
fread( sig, 1, 8, fp );
png_sig_cmp( sig, 0, 8 );
png_ptr_ = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, &png_loader_user_error_fn_silent, &png_loader_user_warning_fn_silent );
png_get_header_ver(NULL);
info_ptr_ = png_create_info_struct( png_ptr_ );
end_info_ = png_create_info_struct( png_ptr_ );
setjmp(png_jmpbuf(png_ptr_));
png_set_palette_to_rgb(png_ptr_);
png_init_io( png_ptr_, fp );
png_set_sig_bytes( png_ptr_, 8 );
// flags force one byte per channel output
int png_transforms = PNG_TRANSFORM_PACKING;
png_read_png( png_ptr_, info_ptr_, png_transforms, NULL );
png_get_image_height( png_ptr_, info_ptr_ );
png_get_image_width( png_ptr_, info_ptr_ );
png_get_bit_depth( png_ptr_, info_ptr_ );
png_get_color_type( png_ptr_, info_ptr_ );
png_get_rows( png_ptr_, info_ptr_ );
fclose(fp);
png_destroy_read_struct(&png_ptr_, &info_ptr_, &end_info_);
std::cerr << "This program is just for build system testing. Don't actually run it." << std::endl;
abort();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment