Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding cmake target names, install pdb symbols on msvc compiler #976

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*.gcno
*.gcov

build/
/example
/example64
/examplesh
Expand Down
45 changes: 30 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,25 +167,37 @@ 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}" )
if(MSVC)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/$<TARGET_FILE_BASE_NAME:${ZLIB_SHARED_PROJECT_NAME}>.pdb
DESTINATION bin
CONFIGURATIONS Debug OR RelWithDebInfo
OPTIONAL
)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/$<TARGET_FILE_BASE_NAME:${ZLIB_STATIC_PROJECT_NAME}>.pdb
DESTINATION lib
CONFIGURATIONS Debug OR RelWithDebInfo
OPTIONAL
)
endif()
endif()
if(NOT SKIP_INSTALL_HEADERS AND NOT SKIP_INSTALL_ALL )
install(FILES ${ZLIB_PUBLIC_HDRS} DESTINATION "${INSTALL_INC_DIR}")
Expand All @@ -199,20 +214,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()