Skip to content

Commit 713e254

Browse files
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
1 parent a57bb07 commit 713e254

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

lib/CMakeLists.txt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,20 @@ if (LINK_STATIC AND BUILD_STATIC_LIB)
106106
endwhile()
107107
list(REMOVE_ITEM LIBLIST "debug")
108108
list(REMOVE_ITEM LIBLIST "optimized")
109-
string(REPLACE ";" " " TEMP_OUT "${LIBLIST}")
109+
foreach (ITEM IN LISTS LIBLIST)
110+
list(APPEND TEMP_OUT ${ITEM})
111+
endforeach ()
110112
set(${OUTLIST} ${TEMP_OUT} PARENT_SCOPE)
111113
endfunction(remove_libtype)
112114

113115
add_library(pulsarStaticWithDeps STATIC ${PULSAR_SOURCES})
114116
target_include_directories(pulsarStaticWithDeps PRIVATE ${dlfcn-win32_INCLUDE_DIRS})
115-
remove_libtype("${COMMON_LIBS}" "optimized" DEBUG_STATIC_LIBS)
116-
remove_libtype("${COMMON_LIBS}" "debug" STATIC_LIBS)
117-
set_property(TARGET pulsarStaticWithDeps PROPERTY STATIC_LIBRARY_FLAGS_DEBUG ${DEBUG_STATIC_LIBS})
118-
set_property(TARGET pulsarStaticWithDeps PROPERTY STATIC_LIBRARY_FLAGS_RELEASE ${STATIC_LIBS})
117+
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
118+
remove_libtype("${COMMON_LIBS}" "optimized" STATIC_LIBS)
119+
else ()
120+
remove_libtype("${COMMON_LIBS}" "debug" STATIC_LIBS)
121+
endif ()
122+
set_property(TARGET pulsarStaticWithDeps PROPERTY STATIC_LIBRARY_OPTIONS ${STATIC_LIBS})
119123
set_property(TARGET pulsarStaticWithDeps PROPERTY OUTPUT_NAME ${LIB_NAME}WithDeps)
120124
set_property(TARGET pulsarStaticWithDeps PROPERTY VERSION ${LIBRARY_VERSION})
121125
install(TARGETS pulsarStaticWithDeps DESTINATION lib)

0 commit comments

Comments
 (0)