This is an automated email from the ASF dual-hosted git repository.

xyz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/pulsar-client-cpp.git


The following commit(s) were added to refs/heads/main by this push:
     new 713e254  Fix the static library might failed to link on Windows (#251)
713e254 is described below

commit 713e2544f30e683ea3758a120eaac456464bd090
Author: Yunze Xu <xyzinfern...@163.com>
AuthorDate: Fri Apr 14 15:10:34 2023 +0800

    Fix the static library might failed to link on Windows (#251)
    
    ### Motivation
    
    Currently the CI in master is broken, here is an example failure:
    
https://github.com/apache/pulsar-client-cpp/actions/runs/4688588681/jobs/8309495018?pr=249
    
    The reason is that the latest Windows runner image is already integrated
    with the OpenSSL library to link, i.e. the `COMMON_LIBS` list variable
    might have an element like:
    
    ```
    debug;C:/Program Files/OpenSSL/lib/VC/libcrypto64MDd.lib
    ```
    
    The list above have two elements:
    1. `debug`
    2. `C:/Program Files/OpenSSL/lib/VC/libcrypto64MDd.lib`
    
    When building the static library, the list above will be converted to a
    space-separated string like:
    
    ```
    C:/Program Files/OpenSSL/lib/VC/libcrypto64MDd.lib
    ```
    
    i.e. the `debug` and `optimized` elements are removed, the rest elements
    that follows `debug` or `optimized` (determined by the build mode) will
    be retained.
    
    See 
https://github.com/apache/pulsar-client-cpp/blob/a57bb072ce6140757917353cc1d5a0007ddc353d/lib/CMakeLists.txt#L109
    
    However, since there is a blank in `Program Files`, the string above is
    unexpectedly treated as two elements:
    - `C:/Program`
    - `Files/OpenSSL/lib/VC/libcrypto64MDd.lib`
    
    Then, the CI failed when linking to `pulsarWithDeps.lib`:
    
    > fatal error LNK1181: cannot open input file 'C:\Program'
    
    ### Modifications
    
    Instead of setting the `STATIC_LIBRARY_FLAGS` property, set the
    `STATIC_LIBRARY_OPTIONS` property, which is a list rather than a string.
    So the blank in the list element won't affect. Then in `remove_libtype`,
    return a list instead of a string.
    
    References:
    - 
https://cmake.org/cmake/help/latest/prop_tgt/STATIC_LIBRARY_OPTIONS.html#prop_tgt:STATIC_LIBRARY_OPTIONS
    - 
https://cmake.org/cmake/help/latest/prop_tgt/STATIC_LIBRARY_FLAGS.html#prop_tgt:STATIC_LIBRARY_FLAGS
---
 lib/CMakeLists.txt | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index 8226251..eca7638 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -106,16 +106,20 @@ if (LINK_STATIC AND BUILD_STATIC_LIB)
             endwhile()
             list(REMOVE_ITEM LIBLIST "debug")
             list(REMOVE_ITEM LIBLIST "optimized")
-            string(REPLACE ";" " " TEMP_OUT "${LIBLIST}")
+            foreach (ITEM IN LISTS LIBLIST)
+                list(APPEND TEMP_OUT ${ITEM})
+            endforeach ()
             set(${OUTLIST} ${TEMP_OUT} PARENT_SCOPE)
         endfunction(remove_libtype)
 
         add_library(pulsarStaticWithDeps STATIC ${PULSAR_SOURCES})
         target_include_directories(pulsarStaticWithDeps PRIVATE 
${dlfcn-win32_INCLUDE_DIRS})
-        remove_libtype("${COMMON_LIBS}" "optimized" DEBUG_STATIC_LIBS)
-        remove_libtype("${COMMON_LIBS}" "debug" STATIC_LIBS)
-        set_property(TARGET pulsarStaticWithDeps PROPERTY 
STATIC_LIBRARY_FLAGS_DEBUG ${DEBUG_STATIC_LIBS})
-        set_property(TARGET pulsarStaticWithDeps PROPERTY 
STATIC_LIBRARY_FLAGS_RELEASE ${STATIC_LIBS})
+        if (CMAKE_BUILD_TYPE STREQUAL "Debug")
+            remove_libtype("${COMMON_LIBS}" "optimized" STATIC_LIBS)
+        else ()
+            remove_libtype("${COMMON_LIBS}" "debug" STATIC_LIBS)
+        endif ()
+        set_property(TARGET pulsarStaticWithDeps PROPERTY 
STATIC_LIBRARY_OPTIONS ${STATIC_LIBS})
         set_property(TARGET pulsarStaticWithDeps PROPERTY OUTPUT_NAME 
${LIB_NAME}WithDeps)
         set_property(TARGET pulsarStaticWithDeps PROPERTY VERSION 
${LIBRARY_VERSION})
         install(TARGETS pulsarStaticWithDeps DESTINATION lib)

Reply via email to