Skip to content

Commit

Permalink
Use CMake 3.5 features
Browse files Browse the repository at this point in the history
1. Set `cmake_minimum_required` to 3.5 (see https://cmake.org/cmake/help/latest/release/3.27.html#id17)
2. Specify the version of CMake project
3. `cmake_policy` is called implicitly by `cmake_minimum_required` (remove its call)
4. Use `CMAKE_INSTALL_BINDIR` and `CMAKE_INSTALL_INCLUDEDIR`
5. Use Zlib as a package (automatic linking of the library and add include directory)
6. Don't activate testing if it was not enabled
  • Loading branch information
theta682 committed Jul 3, 2023
1 parent f135775 commit 05703a0
Showing 1 changed file with 64 additions and 57 deletions.
121 changes: 64 additions & 57 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,7 @@
# For conditions of distribution and use, see the disclaimer
# and license in png.h

cmake_minimum_required(VERSION 3.1)
cmake_policy(VERSION 3.1)

project(libpng C ASM)
enable_testing()

include(CMakeParseArguments)
include(CheckCSourceCompiles)
include(GNUInstallDirs)
cmake_minimum_required(VERSION 3.5)

set(PNGLIB_MAJOR 1)
set(PNGLIB_MINOR 6)
Expand All @@ -49,6 +41,12 @@ set(PNGLIB_VERSION ${PNGLIB_MAJOR}.${PNGLIB_MINOR}.${PNGLIB_REVISION})
set(PNGLIB_ABI_VERSION ${PNGLIB_MAJOR}${PNGLIB_MINOR})
set(PNGLIB_SHARED_VERSION ${PNGLIB_ABI_VERSION}.${PNGLIB_REVISION}.${PNGLIB_SUBREVISION})

project(libpng VERSION ${PNGLIB_VERSION} LANGUAGES C ASM)

include(CMakeParseArguments)
include(CheckCSourceCompiles)
include(GNUInstallDirs)

# Allow the users to specify an application-specific API prefix for libpng
# vendoring purposes. A standard libpng build should have no such prefix.
set(PNG_PREFIX ""
Expand Down Expand Up @@ -79,7 +77,6 @@ option(PNG_HARDWARE_OPTIMIZATIONS "Enable hardware optimizations" ON)
option(PNG_BUILD_ZLIB "Custom zlib location, else find_package is used" OFF)
if(NOT PNG_BUILD_ZLIB)
find_package(ZLIB REQUIRED)
include_directories(${ZLIB_INCLUDE_DIRS})
endif()

if(UNIX AND NOT APPLE AND NOT BEOS AND NOT HAIKU AND NOT EMSCRIPTEN)
Expand Down Expand Up @@ -612,7 +609,6 @@ if(PNG_DEBUG)
endif()

# Now build our targets.
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${ZLIB_INCLUDE_DIRS})

# Initialize the list of libpng library targets.
set(PNG_LIBRARY_TARGETS "")
Expand Down Expand Up @@ -656,7 +652,12 @@ if(PNG_SHARED)
if(WIN32)
set_target_properties(png_shared PROPERTIES DEFINE_SYMBOL PNG_BUILD_DLL)
endif()
target_link_libraries(png_shared ${ZLIB_LIBRARIES} ${M_LIBRARY})
target_include_directories(png_shared
PUBLIC
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/libpng${PNGLIB_ABI_VERSION}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(png_shared PRIVATE ZLIB::ZLIB ${M_LIBRARY})
endif()

if(PNG_STATIC)
Expand All @@ -666,7 +667,12 @@ if(PNG_STATIC)
set_target_properties(png_static PROPERTIES
OUTPUT_NAME "${PNG_STATIC_OUTPUT_NAME}"
DEBUG_POSTFIX "${PNG_DEBUG_POSTFIX}")
target_link_libraries(png_static ${ZLIB_LIBRARIES} ${M_LIBRARY})
target_include_directories(png_static
PUBLIC
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/libpng${PNGLIB_ABI_VERSION}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(png_static PRIVATE ZLIB::ZLIB ${M_LIBRARY})
endif()

if(PNG_FRAMEWORK)
Expand All @@ -682,6 +688,11 @@ if(PNG_FRAMEWORK)
XCODE_ATTRIBUTE_INSTALL_PATH "@rpath"
PUBLIC_HEADER "${libpng_public_hdrs}"
OUTPUT_NAME "png")
target_include_directories(png_framework
PUBLIC
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/libpng${PNGLIB_ABI_VERSION}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(png_framework ${ZLIB_LIBRARIES} ${M_LIBRARY})
endif()

Expand All @@ -691,32 +702,34 @@ if(NOT PNG_LIBRARY_TARGETS)
"PNG_SHARED, PNG_STATIC, PNG_FRAMEWORK")
endif()

function(png_add_test)
set(options)
set(oneValueArgs NAME COMMAND)
set(multiValueArgs OPTIONS FILES)
cmake_parse_arguments(_PAT "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(NOT _PAT_NAME)
message(FATAL_ERROR "png_add_test: Missing NAME argument")
endif()
if(NOT _PAT_COMMAND)
message(FATAL_ERROR "png_add_test: Missing COMMAND argument")
endif()
if(PNG_TESTS AND PNG_SHARED)
enable_testing()

set(TEST_OPTIONS "${_PAT_OPTIONS}")
set(TEST_FILES "${_PAT_FILES}")
function(png_add_test)
set(options)
set(oneValueArgs NAME COMMAND)
set(multiValueArgs OPTIONS FILES)
cmake_parse_arguments(_PAT "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(NOT _PAT_NAME)
message(FATAL_ERROR "png_add_test: Missing NAME argument")
endif()
if(NOT _PAT_COMMAND)
message(FATAL_ERROR "png_add_test: Missing COMMAND argument")
endif()

configure_file("${CMAKE_CURRENT_SOURCE_DIR}/scripts/test.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/tests/${_PAT_NAME}.cmake"
@ONLY)
add_test(NAME "${_PAT_NAME}"
COMMAND "${CMAKE_COMMAND}"
"-DLIBPNG=$<TARGET_FILE:png_shared>"
"-DTEST_COMMAND=$<TARGET_FILE:${_PAT_COMMAND}>"
-P "${CMAKE_CURRENT_BINARY_DIR}/tests/${_PAT_NAME}.cmake")
endfunction()
set(TEST_OPTIONS "${_PAT_OPTIONS}")
set(TEST_FILES "${_PAT_FILES}")

configure_file("${CMAKE_CURRENT_SOURCE_DIR}/scripts/test.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/tests/${_PAT_NAME}.cmake"
@ONLY)
add_test(NAME "${_PAT_NAME}"
COMMAND "${CMAKE_COMMAND}"
"-DLIBPNG=$<TARGET_FILE:png_shared>"
"-DTEST_COMMAND=$<TARGET_FILE:${_PAT_COMMAND}>"
-P "${CMAKE_CURRENT_BINARY_DIR}/tests/${_PAT_NAME}.cmake")
endfunction()

if(PNG_TESTS AND PNG_SHARED)
# Find test PNG files by globbing, but sort lists to ensure
# consistency between different filesystems.
file(GLOB PNGSUITE_PNGS "${CMAKE_CURRENT_SOURCE_DIR}/contrib/pngsuite/*.png")
Expand All @@ -727,14 +740,14 @@ if(PNG_TESTS AND PNG_SHARED)
set(PNGTEST_PNG "${CMAKE_CURRENT_SOURCE_DIR}/pngtest.png")

add_executable(pngtest ${pngtest_sources})
target_link_libraries(pngtest png_shared)
target_link_libraries(pngtest PRIVATE png_shared)

png_add_test(NAME pngtest
COMMAND pngtest
FILES "${PNGTEST_PNG}")

add_executable(pngvalid ${pngvalid_sources})
target_link_libraries(pngvalid png_shared)
target_link_libraries(pngvalid PRIVATE png_shared)

png_add_test(NAME pngvalid-gamma-16-to-8
COMMAND pngvalid
Expand Down Expand Up @@ -780,7 +793,7 @@ if(PNG_TESTS AND PNG_SHARED)
OPTIONS --transform)

add_executable(pngstest ${pngstest_sources})
target_link_libraries(pngstest png_shared)
target_link_libraries(pngstest PRIVATE png_shared)

foreach(gamma_type 1.8 linear none sRGB)
foreach(alpha_type none alpha)
Expand Down Expand Up @@ -835,7 +848,7 @@ if(PNG_TESTS AND PNG_SHARED)
endforeach()

add_executable(pngunknown ${pngunknown_sources})
target_link_libraries(pngunknown png_shared)
target_link_libraries(pngunknown PRIVATE png_shared)

png_add_test(NAME pngunknown-discard
COMMAND pngunknown
Expand Down Expand Up @@ -867,7 +880,7 @@ if(PNG_TESTS AND PNG_SHARED)
FILES "${PNGTEST_PNG}")

add_executable(pngimage ${pngimage_sources})
target_link_libraries(pngimage png_shared)
target_link_libraries(pngimage PRIVATE png_shared)

png_add_test(NAME pngimage-quick
COMMAND pngimage
Expand All @@ -881,11 +894,11 @@ endif()

if(PNG_SHARED AND PNG_EXECUTABLES)
add_executable(pngfix ${pngfix_sources})
target_link_libraries(pngfix png_shared)
target_link_libraries(pngfix PRIVATE png_shared)
set(PNG_BIN_TARGETS pngfix)

add_executable(png-fix-itxt ${png_fix_itxt_sources})
target_link_libraries(png-fix-itxt ${ZLIB_LIBRARIES} ${M_LIBRARY})
target_link_libraries(png-fix-itxt PRIVATE ZLIB::ZLIB ${M_LIBRARY})
list(APPEND PNG_BIN_TARGETS png-fix-itxt)
endif()

Expand Down Expand Up @@ -980,20 +993,14 @@ endif()
if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL)
install(TARGETS ${PNG_LIBRARY_TARGETS}
EXPORT libpng
RUNTIME DESTINATION bin
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
FRAMEWORK DESTINATION ${CMAKE_INSTALL_LIBDIR})

if(PNG_SHARED)
# Create a symlink for libpng.dll.a => libpng16.dll.a on Cygwin
if(CYGWIN OR MINGW)
create_symlink(libpng${CMAKE_IMPORT_LIBRARY_SUFFIX} TARGET png_shared)
install(FILES $<TARGET_LINKER_FILE_DIR:png_shared>/libpng${CMAKE_IMPORT_LIBRARY_SUFFIX}
DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()

if(NOT WIN32)
if(NOT WIN32 OR CYGWIN OR MINGW)
create_symlink(libpng${CMAKE_SHARED_LIBRARY_SUFFIX} TARGET png_shared)
install(FILES $<TARGET_LINKER_FILE_DIR:png_shared>/libpng${CMAKE_SHARED_LIBRARY_SUFFIX}
DESTINATION ${CMAKE_INSTALL_LIBDIR})
Expand All @@ -1011,22 +1018,22 @@ endif()

if(NOT SKIP_INSTALL_HEADERS AND NOT SKIP_INSTALL_ALL)
install(FILES ${libpng_public_hdrs}
DESTINATION include)
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(FILES ${libpng_public_hdrs}
DESTINATION include/libpng${PNGLIB_ABI_VERSION})
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/libpng${PNGLIB_ABI_VERSION})
endif()
if(NOT SKIP_INSTALL_EXECUTABLES AND NOT SKIP_INSTALL_ALL)
if(NOT WIN32 OR CYGWIN OR MINGW)
install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/libpng-config
DESTINATION bin)
DESTINATION ${CMAKE_INSTALL_BINDIR})
install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/libpng${PNGLIB_ABI_VERSION}-config
DESTINATION bin)
DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
endif()

if(NOT SKIP_INSTALL_PROGRAMS AND NOT SKIP_INSTALL_ALL)
install(TARGETS ${PNG_BIN_TARGETS}
RUNTIME DESTINATION bin)
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()

if(NOT SKIP_INSTALL_FILES AND NOT SKIP_INSTALL_ALL)
Expand All @@ -1051,7 +1058,7 @@ endif()
# Create an export file that CMake users can include() to import our targets.
if(NOT SKIP_INSTALL_EXPORT AND NOT SKIP_INSTALL_ALL)
install(EXPORT libpng
DESTINATION lib/libpng
DESTINATION ${CMAKE_INSTALL_LIBDIR}/libpng
FILE libpng${PNGLIB_ABI_VERSION}.cmake)
endif()

Expand Down

0 comments on commit 05703a0

Please sign in to comment.