From 6dfeabf416367c9443c0d482f62668627ed30852 Mon Sep 17 00:00:00 2001 From: Yunze Xu Date: Fri, 14 Apr 2023 13:07:17 +0800 Subject: [PATCH] Fix the static library might failed to link on Windows ### 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 82262515..eca76380 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)