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

CMake option to build/install only static library #359

Open
01e9 opened this issue May 22, 2018 · 11 comments
Open

CMake option to build/install only static library #359

01e9 opened this issue May 22, 2018 · 11 comments

Comments

@01e9
Copy link

01e9 commented May 22, 2018

zlib/CMakeLists.txt

Lines 186 to 187 in cacf7f1

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})

I use zlib as cmake external project

ExternalProject_Add(external_z
    PREFIX "/tmp/test-cmake-external-projects"
    GIT_REPOSITORY "https://github.com/madler/zlib.git"
    GIT_TAG "v1.2.11"
    CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=/tmp/test-cmake-external-projects/installed
    )

I would like an arg -DBUILD_SHARED_LIB=OFF

@01e9
Copy link
Author

01e9 commented May 22, 2018

Currently I delete all .so after install step

ExternalProject_Add_Step(external_z remove_shared_lib
    DEPENDEES install
    COMMAND bash -c "rm /tmp/test-cmake-external-projects/installed/${CMAKE_INSTALL_LIBDIR}/${CMAKE_SHARED_LIBRARY_PREFIX}z${CMAKE_SHARED_LIBRARY_SUFFIX}*"
    )

@henryborchers
Copy link

I have not found an eloquent way of handling this either. It's kinda annoying.

@jeking3
Copy link

jeking3 commented Jun 10, 2018

I did this back in November:

#315

Sadly, it looks like the owner is missing.

@01e9
Copy link
Author

01e9 commented Jun 12, 2018

His Wikipedia page says he is still alive.

@01e9
Copy link
Author

01e9 commented Jun 14, 2018

The development branch is active
https://github.com/madler/zlib/commits/develop

@zdenop
Copy link

zdenop commented Feb 27, 2019

Maybe this patch could help?

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 0fe939d..8d2f5f1 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -7,6 +7,7 @@ set(VERSION "1.2.11")
 
 option(ASM686 "Enable building i686 assembly implementation")
 option(AMD64 "Enable building amd64 assembly implementation")
+option(SKIP_BUILD_EXAMPLES "Skip build of the examples" OFF)
 
 set(INSTALL_BIN_DIR "${CMAKE_INSTALL_PREFIX}/bin" CACHE PATH "Installation directory for executables")
 set(INSTALL_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib" CACHE PATH "Installation directory for libraries")
@@ -211,7 +212,15 @@ elseif(BUILD_SHARED_LIBS AND WIN32)
 endif()
 
 if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL )
-    install(TARGETS zlib zlibstatic
+    if (BUILD_SHARED_LIBS)
+        set(ZLIB_TARGETS zlib)
+        set_target_properties(zlibstatic PROPERTIES EXCLUDE_FROM_ALL ON)
+    else()
+        set(ZLIB_TARGETS zlibstatic)
+        set_target_properties(zlib PROPERTIES EXCLUDE_FROM_ALL ON)
+    endif()
+
+    install(TARGETS ${ZLIB_TARGETS}
         RUNTIME DESTINATION "${INSTALL_BIN_DIR}"
         ARCHIVE DESTINATION "${INSTALL_LIB_DIR}"
         LIBRARY DESTINATION "${INSTALL_LIB_DIR}" )
@@ -230,6 +239,7 @@ endif()
 # Example binaries
 #============================================================================
 
+if (NOT SKIP_BUILD_EXAMPLES)
 add_executable(example test/example.c)
 target_link_libraries(example zlib)
 add_test(example example)
@@ -247,3 +257,4 @@ if(HAVE_OFF64_T)
     target_link_libraries(minigzip64 zlib)
     set_target_properties(minigzip64 PROPERTIES COMPILE_FLAGS "-D_FILE_OFFSET_BITS=64")
 endif()
+endif()

@jeking3
Copy link

jeking3 commented Feb 28, 2019

Again, I did this over a year ago in #315.

@rgpublic
Copy link

Hope this gets committed one day. Just stumbled over this and this is really extremely annoying, because zlib is used in so many projects and you always need a workaround/patch because static zlib isn't picked up causing linker errors in a static build.

@Slackadays
Copy link

Any progress regarding this? Just getting the static zlib is so hard, and I don't want to have to patch CMakeLists.txt to do it.

@bear24rw
Copy link

I also just hit this same issue, would love to get BUILD_SHARED_LIBS properly supported!

@zdenop
Copy link

zdenop commented Jan 27, 2023

I suggest using https://github.com/zlib-ng/ - it is better maintained.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants