Clean up CMakeLists.txt

* Rename the target `png` to `png_shared`. The main targets are now
   named `png_shared`, `png_static` and `png_framework`.
 * Rename the function symbol_prefix() to find_symbol_prefix().
 * Rename the prefix `S` to `_SYM` in the function create_symlink().
 * Rewrite the precondition checks in the function create_symlink().
 * Add precondition checks to the function generate_copy().
 * Delete the vestigial property CLEAN_DIRECT_OUTPUT.
 * Delete the variables PNG_LIB_NAME_STATIC and PNG_LIB_NAME_FRAMEWORK.
 * Initialize and use PNG_LIB_TARGETS consistently as a list.
 * Move all include() commands to the top of the file.
 * Acknowledge a former contributor.
 * Reformat.
This commit is contained in:
Cosmin Truta 2023-02-08 19:44:31 +02:00
parent efa9c2e920
commit aab24fa1b3

View File

@ -5,6 +5,7 @@
# Written by Christian Ehrlicher, 2007 # Written by Christian Ehrlicher, 2007
# Revised by Roger Lowman, 2009-2010 # Revised by Roger Lowman, 2009-2010
# Revised by Clifford Yapp, 2011-2012,2017 # Revised by Clifford Yapp, 2011-2012,2017
# Revised by Claudio Bley, 2013
# Revised by Roger Leigh, 2016 # Revised by Roger Leigh, 2016
# Revised by Andreas Franek, 2016 # Revised by Andreas Franek, 2016
# Revised by Sam Serrels, 2017 # Revised by Sam Serrels, 2017
@ -34,6 +35,10 @@ cmake_policy(VERSION 3.1)
project(libpng C ASM) project(libpng C ASM)
enable_testing() enable_testing()
include(CMakeParseArguments)
include(CheckCSourceCompiles)
include(GNUInstallDirs)
set(PNGLIB_MAJOR 1) set(PNGLIB_MAJOR 1)
set(PNGLIB_MINOR 6) set(PNGLIB_MINOR 6)
set(PNGLIB_REVISION 40) set(PNGLIB_REVISION 40)
@ -44,8 +49,6 @@ set(PNGLIB_VERSION ${PNGLIB_MAJOR}.${PNGLIB_MINOR}.${PNGLIB_REVISION})
set(PNGLIB_SHARED_SOVERSION ${PNGLIB_MAJOR}${PNGLIB_MINOR}) set(PNGLIB_SHARED_SOVERSION ${PNGLIB_MAJOR}${PNGLIB_MINOR})
set(PNGLIB_SHARED_VERSION ${PNGLIB_SHARED_SOVERSION}.${PNGLIB_REVISION}.${PNGLIB_SUBREVISION}) set(PNGLIB_SHARED_VERSION ${PNGLIB_SHARED_SOVERSION}.${PNGLIB_REVISION}.${PNGLIB_SUBREVISION})
include(GNUInstallDirs)
# Allow users to specify location of zlib. # Allow users to specify location of zlib.
# Useful if zlib is being built alongside this as a sub-project. # Useful if zlib is being built alongside this as a sub-project.
option(PNG_BUILD_ZLIB "Custom zlib Location, else find_package is used" OFF) option(PNG_BUILD_ZLIB "Custom zlib Location, else find_package is used" OFF)
@ -229,13 +232,12 @@ set(PNG_LIB_NAME png${PNGLIB_MAJOR}${PNGLIB_MINOR})
# Distinguish between debug and release builds. # Distinguish between debug and release builds.
set(CMAKE_DEBUG_POSTFIX "d") set(CMAKE_DEBUG_POSTFIX "d")
include(CheckCSourceCompiles)
option(ld-version-script "Enable linker version script" ON) option(ld-version-script "Enable linker version script" ON)
if(ld-version-script AND NOT ANDROID AND NOT APPLE) if(ld-version-script AND NOT ANDROID AND NOT APPLE)
# Check if LD supports linker scripts. # Check if LD supports linker scripts.
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/conftest.map" " file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/conftest.map" "
VERS_1 { VERS_1 {
global: sym; global: sym1;
local: *; local: *;
}; };
@ -249,7 +251,8 @@ VERS_2 {
${CMAKE_REQUIRED_FLAGS} ${CMAKE_REQUIRED_FLAGS}
${CMAKE_SHARED_LIBRARY_C_FLAGS} ${CMAKE_SHARED_LIBRARY_C_FLAGS}
"-Wl,--version-script='${CMAKE_CURRENT_BINARY_DIR}/conftest.map'") "-Wl,--version-script='${CMAKE_CURRENT_BINARY_DIR}/conftest.map'")
check_c_source_compiles("void sym(void) {} check_c_source_compiles("
void sym1(void) {}
void sym2(void) {} void sym2(void) {}
int main(void) {return 0;} int main(void) {return 0;}
" HAVE_LD_VERSION_SCRIPT) " HAVE_LD_VERSION_SCRIPT)
@ -258,7 +261,8 @@ int main(void) {return 0;}
${CMAKE_REQUIRED_FLAGS_SAVE} ${CMAKE_REQUIRED_FLAGS_SAVE}
${CMAKE_SHARED_LIBRARY_C_FLAGS} ${CMAKE_SHARED_LIBRARY_C_FLAGS}
"-Wl,-M -Wl,${CMAKE_CURRENT_BINARY_DIR}/conftest.map") "-Wl,-M -Wl,${CMAKE_CURRENT_BINARY_DIR}/conftest.map")
check_c_source_compiles("void sym(void) {} check_c_source_compiles("
void sym1(void) {}
void sym2(void) {} void sym2(void) {}
int main(void) {return 0;} int main(void) {return 0;}
" HAVE_SOLARIS_LD_VERSION_SCRIPT) " HAVE_SOLARIS_LD_VERSION_SCRIPT)
@ -269,10 +273,10 @@ endif()
# Find symbol prefix. Likely obsolete and unnecessary with recent # Find symbol prefix. Likely obsolete and unnecessary with recent
# toolchains (it's not done in many other projects). # toolchains (it's not done in many other projects).
function(symbol_prefix) function(find_symbol_prefix)
set(SYMBOL_PREFIX) set(SYMBOL_PREFIX)
execute_process(COMMAND "${CMAKE_C_COMPILER}" "-E" "-" execute_process(COMMAND "${CMAKE_C_COMPILER}" -E -
INPUT_FILE /dev/null INPUT_FILE /dev/null
OUTPUT_VARIABLE OUT OUTPUT_VARIABLE OUT
RESULT_VARIABLE STATUS) RESULT_VARIABLE STATUS)
@ -299,7 +303,7 @@ function(symbol_prefix)
endfunction() endfunction()
if(UNIX) if(UNIX)
symbol_prefix() find_symbol_prefix()
endif() endif()
find_program(AWK NAMES gawk awk) find_program(AWK NAMES gawk awk)
@ -310,7 +314,7 @@ if(NOT AWK OR ANDROID OR IOS)
# No awk available to generate sources; use pre-built pnglibconf.h # No awk available to generate sources; use pre-built pnglibconf.h
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/scripts/pnglibconf.h.prebuilt configure_file(${CMAKE_CURRENT_SOURCE_DIR}/scripts/pnglibconf.h.prebuilt
${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h) ${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h)
add_custom_target(png_genfiles) # Dummy add_custom_target(png_genfiles)
else() else()
# Copy the awk scripts, converting their line endings to Unix (LF) # Copy the awk scripts, converting their line endings to Unix (LF)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/scripts/checksym.awk configure_file(${CMAKE_CURRENT_SOURCE_DIR}/scripts/checksym.awk
@ -328,7 +332,6 @@ else()
# Generate .chk from .out with awk: # Generate .chk from .out with awk:
# generate_chk(INPUT inputfile OUTPUT outputfile [DEPENDS dep1 [dep2...]]) # generate_chk(INPUT inputfile OUTPUT outputfile [DEPENDS dep1 [dep2...]])
include(CMakeParseArguments)
function(generate_chk) function(generate_chk)
set(options) set(options)
set(oneValueArgs INPUT OUTPUT) set(oneValueArgs INPUT OUTPUT)
@ -399,6 +402,13 @@ else()
set(oneValueArgs INPUT OUTPUT) set(oneValueArgs INPUT OUTPUT)
set(multiValueArgs DEPENDS) set(multiValueArgs DEPENDS)
cmake_parse_arguments(_GCO "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) cmake_parse_arguments(_GCO "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(NOT _GCO_INPUT)
message(FATAL_ERROR "generate_copy: Missing INPUT argument")
endif()
if(NOT _GCO_OUTPUT)
message(FATAL_ERROR "generate_copy: Missing OUTPUT argument")
endif()
add_custom_command(OUTPUT "${_GCO_OUTPUT}" add_custom_command(OUTPUT "${_GCO_OUTPUT}"
COMMAND "${CMAKE_COMMAND}" COMMAND "${CMAKE_COMMAND}"
-E remove "${_GCO_OUTPUT}" -E remove "${_GCO_OUTPUT}"
@ -412,14 +422,16 @@ else()
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/scripts/pnglibconf.dfa" DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/scripts/pnglibconf.dfa"
"${CMAKE_CURRENT_BINARY_DIR}/scripts/options.awk" "${CMAKE_CURRENT_BINARY_DIR}/scripts/options.awk"
"${CMAKE_CURRENT_SOURCE_DIR}/pngconf.h") "${CMAKE_CURRENT_SOURCE_DIR}/pngconf.h")
add_custom_target(png_scripts_pnglibconf_c DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/scripts/pnglibconf.c") add_custom_target(png_scripts_pnglibconf_c
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/scripts/pnglibconf.c")
# Generate pnglibconf.c # Generate pnglibconf.c
generate_source(OUTPUT "pnglibconf.c" generate_source(OUTPUT "pnglibconf.c"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/scripts/pnglibconf.dfa" DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/scripts/pnglibconf.dfa"
"${CMAKE_CURRENT_BINARY_DIR}/scripts/options.awk" "${CMAKE_CURRENT_BINARY_DIR}/scripts/options.awk"
"${CMAKE_CURRENT_SOURCE_DIR}/pngconf.h") "${CMAKE_CURRENT_SOURCE_DIR}/pngconf.h")
add_custom_target(pnglibconf_c DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.c") add_custom_target(pnglibconf_c
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.c")
if(PNG_PREFIX) if(PNG_PREFIX)
set(PNGLIBCONF_H_EXTRA_DEPENDS set(PNGLIBCONF_H_EXTRA_DEPENDS
@ -432,49 +444,57 @@ else()
generate_out(INPUT "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.c" generate_out(INPUT "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.c"
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.out" OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.out"
DEPENDS pnglibconf_c) DEPENDS pnglibconf_c)
add_custom_target(pnglibconf_out DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.out") add_custom_target(pnglibconf_out
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.out")
# Generate pnglibconf.h # Generate pnglibconf.h
generate_source(OUTPUT "pnglibconf.h" generate_source(OUTPUT "pnglibconf.h"
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.out" pnglibconf_out DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.out" pnglibconf_out
${PNGLIBCONF_H_EXTRA_DEPENDS}) ${PNGLIBCONF_H_EXTRA_DEPENDS})
add_custom_target(pnglibconf_h DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h") add_custom_target(pnglibconf_h
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h")
generate_out(INPUT "${CMAKE_CURRENT_SOURCE_DIR}/scripts/intprefix.c" generate_out(INPUT "${CMAKE_CURRENT_SOURCE_DIR}/scripts/intprefix.c"
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/scripts/intprefix.out" OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/scripts/intprefix.out"
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h" pnglibconf_h) DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h" pnglibconf_h)
add_custom_target(png_scripts_intprefix_out DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/scripts/intprefix.out") add_custom_target(png_scripts_intprefix_out
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/scripts/intprefix.out")
generate_out(INPUT "${CMAKE_CURRENT_SOURCE_DIR}/scripts/prefix.c" generate_out(INPUT "${CMAKE_CURRENT_SOURCE_DIR}/scripts/prefix.c"
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/scripts/prefix.out" OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/scripts/prefix.out"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/png.h" DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/png.h"
"${CMAKE_CURRENT_SOURCE_DIR}/pngconf.h" "${CMAKE_CURRENT_SOURCE_DIR}/pngconf.h"
"${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.out" pnglibconf_out) "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.out" pnglibconf_out)
add_custom_target(png_scripts_prefix_out DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/scripts/prefix.out") add_custom_target(png_scripts_prefix_out
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/scripts/prefix.out")
# Generate pngprefix.h # Generate pngprefix.h
generate_source(OUTPUT "pngprefix.h" generate_source(OUTPUT "pngprefix.h"
DEPENDS ${PNGPREFIX_H_EXTRA_DEPENDS}) DEPENDS ${PNGPREFIX_H_EXTRA_DEPENDS})
add_custom_target(pngprefix_h DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/pngprefix.h") add_custom_target(pngprefix_h
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/pngprefix.h")
generate_out(INPUT "${CMAKE_CURRENT_SOURCE_DIR}/scripts/sym.c" generate_out(INPUT "${CMAKE_CURRENT_SOURCE_DIR}/scripts/sym.c"
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/scripts/sym.out" OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/scripts/sym.out"
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h" pnglibconf_h) DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h" pnglibconf_h)
add_custom_target(png_scripts_sym_out DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/scripts/sym.out") add_custom_target(png_scripts_sym_out
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/scripts/sym.out")
generate_out(INPUT "${CMAKE_CURRENT_SOURCE_DIR}/scripts/symbols.c" generate_out(INPUT "${CMAKE_CURRENT_SOURCE_DIR}/scripts/symbols.c"
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/scripts/symbols.out" OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/scripts/symbols.out"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/png.h" DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/png.h"
"${CMAKE_CURRENT_SOURCE_DIR}/pngconf.h" "${CMAKE_CURRENT_SOURCE_DIR}/pngconf.h"
"${CMAKE_CURRENT_SOURCE_DIR}/scripts/pnglibconf.h.prebuilt") "${CMAKE_CURRENT_SOURCE_DIR}/scripts/pnglibconf.h.prebuilt")
add_custom_target(png_scripts_symbols_out DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/scripts/symbols.out") add_custom_target(png_scripts_symbols_out
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/scripts/symbols.out")
generate_out(INPUT "${CMAKE_CURRENT_SOURCE_DIR}/scripts/vers.c" generate_out(INPUT "${CMAKE_CURRENT_SOURCE_DIR}/scripts/vers.c"
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/scripts/vers.out" OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/scripts/vers.out"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/png.h" DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/png.h"
"${CMAKE_CURRENT_SOURCE_DIR}/pngconf.h" "${CMAKE_CURRENT_SOURCE_DIR}/pngconf.h"
"${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h" pnglibconf_h) "${CMAKE_CURRENT_BINARY_DIR}/pnglibconf.h" pnglibconf_h)
add_custom_target(png_scripts_vers_out DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/scripts/vers.out") add_custom_target(png_scripts_vers_out
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/scripts/vers.out")
generate_chk(INPUT "${CMAKE_CURRENT_BINARY_DIR}/scripts/symbols.out" generate_chk(INPUT "${CMAKE_CURRENT_BINARY_DIR}/scripts/symbols.out"
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/scripts/symbols.chk" OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/scripts/symbols.chk"
@ -591,49 +611,48 @@ endif()
# Now build our target. # Now build our target.
include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${ZLIB_INCLUDE_DIRS}) include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${ZLIB_INCLUDE_DIRS})
unset(PNG_LIB_TARGETS) set(PNG_LIB_TARGETS "")
if(PNG_SHARED) if(PNG_SHARED)
add_library(png SHARED ${libpng_sources}) add_library(png_shared SHARED ${libpng_sources})
set(PNG_LIB_TARGETS png) add_dependencies(png_shared png_genfiles)
set_target_properties(png PROPERTIES OUTPUT_NAME ${PNG_LIB_NAME}) list(APPEND PNG_LIB_TARGETS png_shared)
add_dependencies(png png_genfiles) set_target_properties(png_shared PROPERTIES OUTPUT_NAME ${PNG_LIB_NAME})
if(MSVC) if(MSVC)
# MVC does not append 'lib'. Do it here, to have consistent name. # MSVC does not append 'lib'. Do it here, to have consistent names.
set_target_properties(png PROPERTIES PREFIX "lib") set_target_properties(png_shared PROPERTIES PREFIX "lib")
set_target_properties(png PROPERTIES IMPORT_PREFIX "lib") set_target_properties(png_shared PROPERTIES IMPORT_PREFIX "lib")
endif() endif()
target_link_libraries(png ${ZLIB_LIBRARIES} ${M_LIBRARY}) target_link_libraries(png_shared ${ZLIB_LIBRARIES} ${M_LIBRARY})
if(UNIX AND AWK) if(UNIX AND AWK)
if(HAVE_LD_VERSION_SCRIPT) if(HAVE_LD_VERSION_SCRIPT)
set_target_properties(png PROPERTIES set_target_properties(png_shared PROPERTIES
LINK_FLAGS "-Wl,--version-script='${CMAKE_CURRENT_BINARY_DIR}/libpng.vers'") LINK_FLAGS "-Wl,--version-script='${CMAKE_CURRENT_BINARY_DIR}/libpng.vers'")
elseif(HAVE_SOLARIS_LD_VERSION_SCRIPT) elseif(HAVE_SOLARIS_LD_VERSION_SCRIPT)
set_target_properties(png PROPERTIES set_target_properties(png_shared PROPERTIES
LINK_FLAGS "-Wl,-M -Wl,'${CMAKE_CURRENT_BINARY_DIR}/libpng.vers'") LINK_FLAGS "-Wl,-M -Wl,'${CMAKE_CURRENT_BINARY_DIR}/libpng.vers'")
endif() endif()
endif() endif()
if(WIN32)
set_target_properties(png_shared PROPERTIES DEFINE_SYMBOL PNG_BUILD_DLL)
endif()
endif() endif()
if(PNG_STATIC) if(PNG_STATIC)
# does not work without changing name
set(PNG_LIB_NAME_STATIC png_static)
add_library(png_static STATIC ${libpng_sources}) add_library(png_static STATIC ${libpng_sources})
add_dependencies(png_static png_genfiles) add_dependencies(png_static png_genfiles)
list(APPEND PNG_LIB_TARGETS png_static)
# MSVC doesn't use a different file extension for shared vs. static # MSVC doesn't use a different file extension for shared vs. static
# libs. We are able to change OUTPUT_NAME to remove the _static # libs. We are able to change OUTPUT_NAME to remove the _static
# for all other platforms. # for all other platforms.
if(NOT MSVC) if(NOT MSVC)
set_target_properties(png_static PROPERTIES set_target_properties(png_static PROPERTIES
OUTPUT_NAME "${PNG_LIB_NAME}" OUTPUT_NAME "${PNG_LIB_NAME}")
CLEAN_DIRECT_OUTPUT 1)
else() else()
set_target_properties(png_static PROPERTIES set_target_properties(png_static PROPERTIES
OUTPUT_NAME "${PNG_LIB_NAME}_static" OUTPUT_NAME "${PNG_LIB_NAME}_static")
CLEAN_DIRECT_OUTPUT 1)
endif() endif()
list(APPEND PNG_LIB_TARGETS png_static)
if(MSVC) if(MSVC)
# MSVC does not append 'lib'. Do it here, to have consistent name. # MSVC does not append 'lib'. Do it here, to have consistent name.
set_target_properties(png_static PROPERTIES PREFIX "lib") set_target_properties(png_static PROPERTIES PREFIX "lib")
@ -642,7 +661,6 @@ if(PNG_STATIC)
endif() endif()
if(PNG_FRAMEWORK) if(PNG_FRAMEWORK)
set(PNG_LIB_NAME_FRAMEWORK png_framework)
add_library(png_framework SHARED ${libpng_sources}) add_library(png_framework SHARED ${libpng_sources})
add_dependencies(png_framework png_genfiles) add_dependencies(png_framework png_genfiles)
list(APPEND PNG_LIB_TARGETS png_framework) list(APPEND PNG_LIB_TARGETS png_framework)
@ -661,12 +679,7 @@ endif()
if(NOT PNG_LIB_TARGETS) if(NOT PNG_LIB_TARGETS)
message(SEND_ERROR "No library variant selected to build. " message(SEND_ERROR "No library variant selected to build. "
"Please enable at least one of the following options: " "Please enable at least one of the following options: "
"PNG_STATIC, PNG_SHARED, PNG_FRAMEWORK") "PNG_SHARED, PNG_STATIC, PNG_FRAMEWORK")
endif()
if(PNG_SHARED AND WIN32)
set_target_properties(png PROPERTIES
DEFINE_SYMBOL PNG_BUILD_DLL)
endif() endif()
function(png_add_test) function(png_add_test)
@ -674,7 +687,6 @@ function(png_add_test)
set(oneValueArgs NAME COMMAND) set(oneValueArgs NAME COMMAND)
set(multiValueArgs OPTIONS FILES) set(multiValueArgs OPTIONS FILES)
cmake_parse_arguments(_PAT "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) cmake_parse_arguments(_PAT "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
if(NOT _PAT_NAME) if(NOT _PAT_NAME)
message(FATAL_ERROR "png_add_test: Missing NAME argument") message(FATAL_ERROR "png_add_test: Missing NAME argument")
endif() endif()
@ -690,7 +702,7 @@ function(png_add_test)
@ONLY) @ONLY)
add_test(NAME "${_PAT_NAME}" add_test(NAME "${_PAT_NAME}"
COMMAND "${CMAKE_COMMAND}" COMMAND "${CMAKE_COMMAND}"
"-DLIBPNG=$<TARGET_FILE:png>" "-DLIBPNG=$<TARGET_FILE:png_shared>"
"-DTEST_COMMAND=$<TARGET_FILE:${_PAT_COMMAND}>" "-DTEST_COMMAND=$<TARGET_FILE:${_PAT_COMMAND}>"
-P "${CMAKE_CURRENT_BINARY_DIR}/tests/${_PAT_NAME}.cmake") -P "${CMAKE_CURRENT_BINARY_DIR}/tests/${_PAT_NAME}.cmake")
endfunction() endfunction()
@ -706,14 +718,14 @@ if(PNG_TESTS AND PNG_SHARED)
set(PNGTEST_PNG "${CMAKE_CURRENT_SOURCE_DIR}/pngtest.png") set(PNGTEST_PNG "${CMAKE_CURRENT_SOURCE_DIR}/pngtest.png")
add_executable(pngtest ${pngtest_sources}) add_executable(pngtest ${pngtest_sources})
target_link_libraries(pngtest png) target_link_libraries(pngtest png_shared)
png_add_test(NAME pngtest png_add_test(NAME pngtest
COMMAND pngtest COMMAND pngtest
FILES "${PNGTEST_PNG}") FILES "${PNGTEST_PNG}")
add_executable(pngvalid ${pngvalid_sources}) add_executable(pngvalid ${pngvalid_sources})
target_link_libraries(pngvalid png) target_link_libraries(pngvalid png_shared)
png_add_test(NAME pngvalid-gamma-16-to-8 png_add_test(NAME pngvalid-gamma-16-to-8
COMMAND pngvalid COMMAND pngvalid
@ -759,7 +771,7 @@ if(PNG_TESTS AND PNG_SHARED)
OPTIONS --transform) OPTIONS --transform)
add_executable(pngstest ${pngstest_sources}) add_executable(pngstest ${pngstest_sources})
target_link_libraries(pngstest png) target_link_libraries(pngstest png_shared)
foreach(gamma_type 1.8 linear none sRGB) foreach(gamma_type 1.8 linear none sRGB)
foreach(alpha_type none alpha) foreach(alpha_type none alpha)
@ -814,7 +826,7 @@ if(PNG_TESTS AND PNG_SHARED)
endforeach() endforeach()
add_executable(pngunknown ${pngunknown_sources}) add_executable(pngunknown ${pngunknown_sources})
target_link_libraries(pngunknown png) target_link_libraries(pngunknown png_shared)
png_add_test(NAME pngunknown-discard png_add_test(NAME pngunknown-discard
COMMAND pngunknown COMMAND pngunknown
@ -846,7 +858,7 @@ if(PNG_TESTS AND PNG_SHARED)
FILES "${PNGTEST_PNG}") FILES "${PNGTEST_PNG}")
add_executable(pngimage ${pngimage_sources}) add_executable(pngimage ${pngimage_sources})
target_link_libraries(pngimage png) target_link_libraries(pngimage png_shared)
png_add_test(NAME pngimage-quick png_add_test(NAME pngimage-quick
COMMAND pngimage COMMAND pngimage
@ -860,7 +872,7 @@ endif()
if(PNG_SHARED AND PNG_EXECUTABLES) if(PNG_SHARED AND PNG_EXECUTABLES)
add_executable(pngfix ${pngfix_sources}) add_executable(pngfix ${pngfix_sources})
target_link_libraries(pngfix png) target_link_libraries(pngfix png_shared)
set(PNG_BIN_TARGETS pngfix) set(PNG_BIN_TARGETS pngfix)
add_executable(png-fix-itxt ${png_fix_itxt_sources}) add_executable(png-fix-itxt ${png_fix_itxt_sources})
@ -868,56 +880,53 @@ if(PNG_SHARED AND PNG_EXECUTABLES)
list(APPEND PNG_BIN_TARGETS png-fix-itxt) list(APPEND PNG_BIN_TARGETS png-fix-itxt)
endif() endif()
# Creates a symlink from src to dest (if possible), or, alternatively, # Create a symlink from src to dest (if possible), or, alternatively,
# copies src to dest if different. # copy src to dest if different.
include(CMakeParseArguments)
function(create_symlink DEST_FILE) function(create_symlink DEST_FILE)
cmake_parse_arguments(S "" "FILE;TARGET" "" ${ARGN}) cmake_parse_arguments(_SYM "" "FILE;TARGET" "" ${ARGN})
if(NOT _SYM_FILE AND NOT _SYM_TARGET)
if(NOT S_TARGET AND NOT S_FILE) message(FATAL_ERROR "create_symlink: Missing FILE or TARGET argument")
message(FATAL_ERROR "create_symlink: Missing TARGET or FILE argument")
endif() endif()
if(_SYM_FILE AND _SYM_TARGET)
if(S_TARGET AND S_FILE)
message(FATAL_ERROR "create_symlink: " message(FATAL_ERROR "create_symlink: "
"Both source file ${S_FILE} and build target ${S_TARGET} arguments are present; " "The arguments FILE (${_SYM_FILE}) and TARGET (${_SYM_TARGET}) "
"can only have one") "are mutually-exclusive")
endif() endif()
if(S_FILE) if(_SYM_FILE)
# If we don't need to symlink something that's coming from a build target, # If we don't need to symlink something that's coming from a build target,
# we can go ahead and symlink/copy at configure time. # we can go ahead and symlink/copy at configure time.
if(CMAKE_HOST_WIN32 AND NOT CYGWIN) if(CMAKE_HOST_WIN32 AND NOT CYGWIN)
execute_process(COMMAND "${CMAKE_COMMAND}" execute_process(COMMAND "${CMAKE_COMMAND}"
-E copy_if_different -E copy_if_different
${S_FILE} ${DEST_FILE} ${_SYM_FILE} ${DEST_FILE}
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}") WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
else() else()
execute_process(COMMAND "${CMAKE_COMMAND}" execute_process(COMMAND "${CMAKE_COMMAND}"
-E create_symlink -E create_symlink
${S_FILE} ${DEST_FILE} ${_SYM_FILE} ${DEST_FILE}
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}") WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
endif() endif()
endif() endif()
if(S_TARGET) if(_SYM_TARGET)
# We need to use generator expressions, which can be a bit tricky. # We need to use generator expressions, which can be a bit tricky.
# For simplicity, make the symlink a POST_BUILD step, and use the TARGET # For simplicity, make the symlink a POST_BUILD step, and use the TARGET
# signature of add_custom_command. # signature of add_custom_command.
if(CMAKE_HOST_WIN32 AND NOT CYGWIN) if(CMAKE_HOST_WIN32 AND NOT CYGWIN)
add_custom_command(TARGET ${S_TARGET} add_custom_command(TARGET ${_SYM_TARGET}
POST_BUILD POST_BUILD
COMMAND "${CMAKE_COMMAND}" COMMAND "${CMAKE_COMMAND}"
-E copy_if_different -E copy_if_different
$<TARGET_LINKER_FILE_NAME:${S_TARGET}> $<TARGET_LINKER_FILE_NAME:${_SYM_TARGET}>
$<TARGET_LINKER_FILE_DIR:${S_TARGET}>/${DEST_FILE}) $<TARGET_LINKER_FILE_DIR:${_SYM_TARGET}>/${DEST_FILE})
else() else()
add_custom_command(TARGET ${S_TARGET} add_custom_command(TARGET ${_SYM_TARGET}
POST_BUILD POST_BUILD
COMMAND "${CMAKE_COMMAND}" COMMAND "${CMAKE_COMMAND}"
-E create_symlink -E create_symlink
$<TARGET_LINKER_FILE_NAME:${S_TARGET}> $<TARGET_LINKER_FILE_NAME:${_SYM_TARGET}>
$<TARGET_LINKER_FILE_DIR:${S_TARGET}>/${DEST_FILE}) $<TARGET_LINKER_FILE_DIR:${_SYM_TARGET}>/${DEST_FILE})
endif() endif()
endif() endif()
endfunction() endfunction()
@ -960,10 +969,9 @@ endif()
# Set up links. # Set up links.
if(PNG_SHARED) if(PNG_SHARED)
set_target_properties(png PROPERTIES set_target_properties(png_shared PROPERTIES
VERSION ${PNGLIB_SHARED_VERSION} VERSION ${PNGLIB_SHARED_VERSION}
SOVERSION ${PNGLIB_SHARED_SOVERSION} SOVERSION ${PNGLIB_SHARED_SOVERSION})
CLEAN_DIRECT_OUTPUT 1)
endif() endif()
# Install. # Install.
@ -978,14 +986,14 @@ if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL)
if(PNG_SHARED) if(PNG_SHARED)
# Create a symlink for libpng.dll.a => libpng16.dll.a on Cygwin # Create a symlink for libpng.dll.a => libpng16.dll.a on Cygwin
if(CYGWIN OR MINGW) if(CYGWIN OR MINGW)
create_symlink(libpng${CMAKE_IMPORT_LIBRARY_SUFFIX} TARGET png) create_symlink(libpng${CMAKE_IMPORT_LIBRARY_SUFFIX} TARGET png_shared)
install(FILES $<TARGET_LINKER_FILE_DIR:png>/libpng${CMAKE_IMPORT_LIBRARY_SUFFIX} install(FILES $<TARGET_LINKER_FILE_DIR:png_shared>/libpng${CMAKE_IMPORT_LIBRARY_SUFFIX}
DESTINATION ${CMAKE_INSTALL_LIBDIR}) DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif() endif()
if(NOT WIN32) if(NOT WIN32)
create_symlink(libpng${CMAKE_SHARED_LIBRARY_SUFFIX} TARGET png) create_symlink(libpng${CMAKE_SHARED_LIBRARY_SUFFIX} TARGET png_shared)
install(FILES $<TARGET_LINKER_FILE_DIR:png>/libpng${CMAKE_SHARED_LIBRARY_SUFFIX} install(FILES $<TARGET_LINKER_FILE_DIR:png_shared>/libpng${CMAKE_SHARED_LIBRARY_SUFFIX}
DESTINATION ${CMAKE_INSTALL_LIBDIR}) DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif() endif()
endif() endif()