Skip to content

Commit

Permalink
Allow overriding cmake target names.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
stevewgr committed May 30, 2024
1 parent 8e47fc4 commit 7d143e2
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ project(zlib C)

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)

set(INSTALL_BIN_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Installation directory for executables")
Expand Down Expand Up @@ -149,12 +152,12 @@ if(MINGW)
set(ZLIB_DLL_SRCS ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj)
endif(MINGW)

add_library(zlib 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})
add_library(zlibstatic STATIC ${ZLIB_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
target_include_directories(zlibstatic PUBLIC ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
set_target_properties(zlib PROPERTIES DEFINE_SYMBOL ZLIB_DLL)
set_target_properties(zlib PROPERTIES SOVERSION 1)
add_library(${ZLIB_SHARED_PROJECT_NAME} SHARED ${ZLIB_SRCS} ${ZLIB_DLL_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
target_include_directories(${ZLIB_SHARED_PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
add_library(${ZLIB_STATIC_PROJECT_NAME} STATIC ${ZLIB_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
target_include_directories(${ZLIB_STATIC_PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
set_target_properties(${ZLIB_SHARED_PROJECT_NAME} PROPERTIES DEFINE_SYMBOL ZLIB_DLL)
set_target_properties(${ZLIB_SHARED_PROJECT_NAME} PROPERTIES SOVERSION 1)

if(NOT CYGWIN)
# This property causes shared libraries on Linux to have the full version
Expand All @@ -164,22 +167,22 @@ if(NOT CYGWIN)
#
# This has no effect with MSVC, on that platform the version info for
# 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()

if(UNIX)
# 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))
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()
elseif(BUILD_SHARED_LIBS AND WIN32)
# 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()

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}"
ARCHIVE DESTINATION "${INSTALL_LIB_DIR}"
LIBRARY DESTINATION "${INSTALL_LIB_DIR}" )
Expand All @@ -199,20 +202,20 @@ endif()
#============================================================================
if(ZLIB_BUILD_EXAMPLES)
add_executable(example test/example.c)
target_link_libraries(example zlib)
target_link_libraries(example ${ZLIB_SHARED_PROJECT_NAME})
add_test(example example)

add_executable(minigzip test/minigzip.c)
target_link_libraries(minigzip zlib)
target_link_libraries(minigzip ${ZLIB_SHARED_PROJECT_NAME})

if(HAVE_OFF64_T)
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")
add_test(example64 example64)

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")
endif()
endif()

0 comments on commit 7d143e2

Please sign in to comment.