Allow overriding cmake target names.

Note that this is useful in particular when you want to use consistent
library names across all platforms, because on windows both shared and
static libs use the same *.lib extension in comparison to unix systems
where *.so is shared and *.a is static or on macos dylib for dynamic
libraries.
Surely we can also configure target properties and set OUTPUT_NAME, but
that creates other questions about configurations that do not match with
the target name. This is considered a bad practice and hence the
approach of overriding the target names should also influence the output
names and other configurations that may depend upon it.
pull/976/head
Steve Williams 9 months ago
parent 8e47fc4b07
commit 7d143e233f
No known key found for this signature in database
GPG Key ID: 2C53D0219F0F29A2
  1. 33
      CMakeLists.txt

@ -5,6 +5,9 @@ project(zlib C)
set(VERSION "1.3.1") set(VERSION "1.3.1")
set(ZLIB_SHARED_PROJECT_NAME "${PROJECT_NAME}" CACHE STRING "Shared library target name")
set(ZLIB_STATIC_PROJECT_NAME "${ZLIB_SHARED_PROJECT_NAME}static" CACHE STRING "Static library target name")
option(ZLIB_BUILD_EXAMPLES "Enable Zlib Examples" ON) option(ZLIB_BUILD_EXAMPLES "Enable Zlib Examples" ON)
set(INSTALL_BIN_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Installation directory for executables") set(INSTALL_BIN_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Installation directory for executables")
@ -149,12 +152,12 @@ if(MINGW)
set(ZLIB_DLL_SRCS ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj) set(ZLIB_DLL_SRCS ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj)
endif(MINGW) endif(MINGW)
add_library(zlib SHARED ${ZLIB_SRCS} ${ZLIB_DLL_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS}) add_library(${ZLIB_SHARED_PROJECT_NAME} SHARED ${ZLIB_SRCS} ${ZLIB_DLL_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
target_include_directories(zlib PUBLIC ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}) target_include_directories(${ZLIB_SHARED_PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
add_library(zlibstatic STATIC ${ZLIB_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS}) add_library(${ZLIB_STATIC_PROJECT_NAME} STATIC ${ZLIB_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
target_include_directories(zlibstatic PUBLIC ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}) target_include_directories(${ZLIB_STATIC_PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
set_target_properties(zlib PROPERTIES DEFINE_SYMBOL ZLIB_DLL) set_target_properties(${ZLIB_SHARED_PROJECT_NAME} PROPERTIES DEFINE_SYMBOL ZLIB_DLL)
set_target_properties(zlib PROPERTIES SOVERSION 1) set_target_properties(${ZLIB_SHARED_PROJECT_NAME} PROPERTIES SOVERSION 1)
if(NOT CYGWIN) if(NOT CYGWIN)
# This property causes shared libraries on Linux to have the full version # This property causes shared libraries on Linux to have the full version
@ -164,22 +167,22 @@ if(NOT CYGWIN)
# #
# This has no effect with MSVC, on that platform the version info for # This has no effect with MSVC, on that platform the version info for
# the DLL comes from the resource file win32/zlib1.rc # the DLL comes from the resource file win32/zlib1.rc
set_target_properties(zlib PROPERTIES VERSION ${ZLIB_FULL_VERSION}) set_target_properties(${ZLIB_SHARED_PROJECT_NAME} PROPERTIES VERSION ${ZLIB_FULL_VERSION})
endif() endif()
if(UNIX) if(UNIX)
# On unix-like platforms the library is almost always called libz # On unix-like platforms the library is almost always called libz
set_target_properties(zlib zlibstatic PROPERTIES OUTPUT_NAME z) set_target_properties(${ZLIB_SHARED_PROJECT_NAME} ${ZLIB_STATIC_PROJECT_NAME} PROPERTIES OUTPUT_NAME z)
if(NOT APPLE AND NOT(CMAKE_SYSTEM_NAME STREQUAL AIX)) if(NOT APPLE AND NOT(CMAKE_SYSTEM_NAME STREQUAL AIX))
set_target_properties(zlib PROPERTIES LINK_FLAGS "-Wl,--version-script,\"${CMAKE_CURRENT_SOURCE_DIR}/zlib.map\"") set_target_properties(${ZLIB_SHARED_PROJECT_NAME} PROPERTIES LINK_FLAGS "-Wl,--version-script,\"${CMAKE_CURRENT_SOURCE_DIR}/zlib.map\"")
endif() endif()
elseif(BUILD_SHARED_LIBS AND WIN32) elseif(BUILD_SHARED_LIBS AND WIN32)
# Creates zlib1.dll when building shared library version # Creates zlib1.dll when building shared library version
set_target_properties(zlib PROPERTIES SUFFIX "1.dll") set_target_properties(${ZLIB_SHARED_PROJECT_NAME} PROPERTIES SUFFIX "1.dll")
endif() endif()
if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL ) if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL )
install(TARGETS zlib zlibstatic install(TARGETS ${ZLIB_SHARED_PROJECT_NAME} ${ZLIB_STATIC_PROJECT_NAME}
RUNTIME DESTINATION "${INSTALL_BIN_DIR}" RUNTIME DESTINATION "${INSTALL_BIN_DIR}"
ARCHIVE DESTINATION "${INSTALL_LIB_DIR}" ARCHIVE DESTINATION "${INSTALL_LIB_DIR}"
LIBRARY DESTINATION "${INSTALL_LIB_DIR}" ) LIBRARY DESTINATION "${INSTALL_LIB_DIR}" )
@ -199,20 +202,20 @@ endif()
#============================================================================ #============================================================================
if(ZLIB_BUILD_EXAMPLES) if(ZLIB_BUILD_EXAMPLES)
add_executable(example test/example.c) add_executable(example test/example.c)
target_link_libraries(example zlib) target_link_libraries(example ${ZLIB_SHARED_PROJECT_NAME})
add_test(example example) add_test(example example)
add_executable(minigzip test/minigzip.c) add_executable(minigzip test/minigzip.c)
target_link_libraries(minigzip zlib) target_link_libraries(minigzip ${ZLIB_SHARED_PROJECT_NAME})
if(HAVE_OFF64_T) if(HAVE_OFF64_T)
add_executable(example64 test/example.c) add_executable(example64 test/example.c)
target_link_libraries(example64 zlib) target_link_libraries(example64 ${ZLIB_SHARED_PROJECT_NAME})
set_target_properties(example64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64") set_target_properties(example64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64")
add_test(example64 example64) add_test(example64 example64)
add_executable(minigzip64 test/minigzip.c) add_executable(minigzip64 test/minigzip.c)
target_link_libraries(minigzip64 zlib) target_link_libraries(minigzip64 ${ZLIB_SHARED_PROJECT_NAME})
set_target_properties(minigzip64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64") set_target_properties(minigzip64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64")
endif() endif()
endif() endif()

Loading…
Cancel
Save