Skip to content

Commit

Permalink
Pull request #133: Build zlib as either static or shared library
Browse files Browse the repository at this point in the history
Merge in OP/openkit-native from feature/APM-153398-zlib-build-improvements to main

* commit '67d029723e3293d106e3ae33e67a9994614c6dd0':
  Build zlib as either static or shared library

GitOrigin-RevId: 02cf2f1864f52a98f301fbba43291fe0173d5c37
  • Loading branch information
stefaneberl authored and openkitdt committed May 17, 2021
1 parent 2019a88 commit ae9cb81
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 40 deletions.
24 changes: 10 additions & 14 deletions 3rdparty/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,12 @@ fix_compiler_flags(USE_DEFAULT_WARNINGS_FLAGS)
######
# zlib
SET(SKIP_INSTALL_FILES TRUE)
set(BUILD_SHARED_LIBS_SAVED "${BUILD_SHARED_LIBS}")
if (NOT BUILD_SHARED_LIBS OR OPENKIT_MONOLITHIC_SHARED_LIB)
set(BUILD_SHARED_LIBS OFF)
endif()
add_subdirectory(zlib-1.2.11)
set_target_properties(zlib
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
set_target_properties(zlibstatic
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
set(BUILD_SHARED_LIBS "${BUILD_SHARED_LIBS_SAVED}")

######
# libcurl
Expand Down Expand Up @@ -113,8 +108,9 @@ set_target_properties(gmock_main PROPERTIES FOLDER 3rdparty/googletest)
set_target_properties(gtest PROPERTIES FOLDER 3rdparty/googletest)
set_target_properties(gtest_main PROPERTIES FOLDER 3rdparty/googletest)
set_target_properties(libcurl PROPERTIES FOLDER 3rdparty/libcurl)
set_target_properties(example PROPERTIES FOLDER 3rdparty/libcurl)
set_target_properties(uninstall PROPERTIES FOLDER 3rdparty/libcurl)
set_target_properties(minigzip PROPERTIES FOLDER 3rdparty/zlib)
set_target_properties(zlib PROPERTIES FOLDER 3rdparty/zlib)
set_target_properties(zlibstatic PROPERTIES FOLDER 3rdparty/zlib)
if (BUILD_SHARED_LIBS AND NOT OPENKIT_MONOLITHIC_SHARED_LIB)
set_target_properties(zlib PROPERTIES FOLDER 3rdparty/zlib)
else ()
set_target_properties(zlibstatic PROPERTIES FOLDER 3rdparty/zlib)
endif ()
65 changes: 39 additions & 26 deletions 3rdparty/zlib-1.2.11/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -183,35 +183,46 @@ if(MINGW)
set(ZLIB_DLL_SRCS ${CMAKE_CURRENT_BINARY_DIR}/zlib1rc.obj)
endif(MINGW)

add_library(zlib SHARED ${ZLIB_SRCS} ${ZLIB_ASMS} ${ZLIB_DLL_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
add_library(zlibstatic STATIC ${ZLIB_SRCS} ${ZLIB_ASMS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
set_target_properties(zlib PROPERTIES DEFINE_SYMBOL ZLIB_DLL)
set_target_properties(zlib PROPERTIES SOVERSION 1)

if(NOT CYGWIN)
if (BUILD_SHARED_LIBS)
add_library(zlib SHARED ${ZLIB_SRCS} ${ZLIB_ASMS} ${ZLIB_DLL_SRCS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
set_target_properties(zlib PROPERTIES DEFINE_SYMBOL ZLIB_DLL)
set_target_properties(zlib PROPERTIES SOVERSION 1)
set(ZLIB_LIBRARY zlib)
else ()
add_library(zlibstatic STATIC ${ZLIB_SRCS} ${ZLIB_ASMS} ${ZLIB_PUBLIC_HDRS} ${ZLIB_PRIVATE_HDRS})
set(ZLIB_LIBRARY zlibstatic)
endif ()

if(BUILD_SHARED_LIBS AND NOT CYGWIN)
# This property causes shared libraries on Linux to have the full version
# encoded into their final filename. We disable this on Cygwin because
# it causes cygz-${ZLIB_FULL_VERSION}.dll to be created when cygz.dll
# seems to be the default.
#
# 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_LIBRARY} 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)
if(NOT APPLE)
set_target_properties(zlib PROPERTIES LINK_FLAGS "-Wl,--version-script,\"${CMAKE_CURRENT_SOURCE_DIR}/zlib.map\"")
set_target_properties(${ZLIB_LIBRARY} PROPERTIES OUTPUT_NAME z)
if(BUILD_SHARED_LIBS AND NOT APPLE)
set_target_properties(${ZLIB_LIBRARY} 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_LIBRARY} PROPERTIES SUFFIX "1.dll")
endif()

set_target_properties(${ZLIB_LIBRARY}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")

if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL )
install(TARGETS zlib zlibstatic
install(TARGETS ${ZLIB_LIBRARY}
RUNTIME DESTINATION "${INSTALL_BIN_DIR}"
ARCHIVE DESTINATION "${INSTALL_LIB_DIR}"
LIBRARY DESTINATION "${INSTALL_LIB_DIR}" )
Expand All @@ -230,20 +241,22 @@ endif()
# Example binaries
#============================================================================

add_executable(example test/example.c)
target_link_libraries(example zlib)
add_test(example example)
if (ZLIB_BUILD_EXAMPLES)
add_executable(example test/example.c)
target_link_libraries(example ${ZLIB_LIBRARY})
add_test(example example)

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

if(HAVE_OFF64_T)
add_executable(example64 test/example.c)
target_link_libraries(example64 zlib)
set_target_properties(example64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64")
add_test(example64 example64)
if(HAVE_OFF64_T)
add_executable(example64 test/example.c)
target_link_libraries(example64 ${ZLIB_LIBRARY})
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)
set_target_properties(minigzip64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64")
endif()
add_executable(minigzip64 test/minigzip.c)
target_link_libraries(minigzip64 ${ZLIB_LIBRARY})
set_target_properties(minigzip64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64")
endif()
endif ()

0 comments on commit ae9cb81

Please sign in to comment.