From 8e47fc4b075564b7508a56333d7b81cae09699bd Mon Sep 17 00:00:00 2001 From: Steve Williams <90905675+stevewgr@users.noreply.github.com> Date: Thu, 30 May 2024 14:55:24 -0400 Subject: [PATCH 1/3] Ignore build directory. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index bd44d84af..2c7b9c068 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ *.gcno *.gcov +build/ /example /example64 /examplesh From 7d143e233fa3c57c504cde51b9e24d0e17d0bb55 Mon Sep 17 00:00:00 2001 From: Steve Williams <90905675+stevewgr@users.noreply.github.com> Date: Thu, 30 May 2024 14:57:03 -0400 Subject: [PATCH 2/3] 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. --- CMakeLists.txt | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 15ceebe78..f456d9ff7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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") @@ -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 @@ -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}" ) @@ -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() From 1745729588a6b3ded4ba8b176e2519505225c412 Mon Sep 17 00:00:00 2001 From: Steve Williams <90905675+stevewgr@users.noreply.github.com> Date: Thu, 30 May 2024 15:01:44 -0400 Subject: [PATCH 3/3] Ensure we install *.pdb files for symbols on msvc as well. --- CMakeLists.txt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index f456d9ff7..93f5e1035 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -186,6 +186,18 @@ if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL ) RUNTIME DESTINATION "${INSTALL_BIN_DIR}" ARCHIVE DESTINATION "${INSTALL_LIB_DIR}" LIBRARY DESTINATION "${INSTALL_LIB_DIR}" ) + if(MSVC) + install(FILES ${CMAKE_CURRENT_BINARY_DIR}/$/$.pdb + DESTINATION bin + CONFIGURATIONS Debug OR RelWithDebInfo + OPTIONAL + ) + install(FILES ${CMAKE_CURRENT_BINARY_DIR}/$/$.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}")