@@ -148,7 +148,8 @@ function(merge_static_libs TARGET_NAME)
148
148
COMMAND rm "${CMAKE_CURRENT_BINARY_DIR} /lib${TARGET_NAME} .a"
149
149
COMMAND /usr/bin/libtool -static -o "${CMAKE_CURRENT_BINARY_DIR} /lib${TARGET_NAME} .a" ${libfiles}
150
150
)
151
- else () # general UNIX: use "ar" to extract objects and re-add to a common lib
151
+ endif (APPLE )
152
+ if (LINUX ) # general UNIX: use "ar" to extract objects and re-add to a common lib
152
153
set (target_DIR ${CMAKE_CURRENT_BINARY_DIR} /${TARGET_NAME}.dir )
153
154
154
155
foreach (lib ${libs} )
@@ -187,14 +188,47 @@ function(merge_static_libs TARGET_NAME)
187
188
COMMAND ${CMAKE_AR} crs ${target_LIBNAME} `find ${target_DIR} -name '*.o'`
188
189
COMMAND ${CMAKE_RANLIB} ${target_LIBNAME}
189
190
WORKING_DIRECTORY ${target_DIR} )
190
- endif ()
191
+ endif (LINUX )
192
+ if (WIN32 ) # windows do not support gcc/nvcc combined compiling. Use msvc lib.exe to merge libs.
193
+ # Make the generated dummy source file depended on all static input
194
+ # libs. If input lib changes,the source file is touched
195
+ # which causes the desired effect (relink).
196
+ add_custom_command (OUTPUT ${target_SRCS}
197
+ COMMAND ${CMAKE_COMMAND} -E touch ${target_SRCS}
198
+ DEPENDS ${libs} )
199
+
200
+ # Generate dummy staic lib
201
+ file (WRITE ${target_SRCS} "const char *dummy_${TARGET_NAME} = \" ${target_SRCS} \" ;" )
202
+ add_library (${TARGET_NAME} STATIC ${target_SRCS} )
203
+ target_link_libraries (${TARGET_NAME} ${libs_deps} )
204
+
205
+ foreach (lib ${libs} )
206
+ # Get the file names of the libraries to be merged
207
+ #if(NOT $<TARGET_FILE:${lib}> MATCHES "lib.*\\.lib")
208
+ # message("library" ${lib})
209
+ # set(libfiles ${libfiles} lib$<TARGET_FILE:${lib}>)
210
+ #else()
211
+ set (libfiles ${libfiles} $< TARGET_FILE:${lib} > )
212
+ #endif()
213
+ endforeach ()
214
+
215
+ # windows cmd return error in clean env.
216
+ # COMMAND del "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}/${TARGET_NAME}.lib"
217
+ add_custom_command (TARGET ${TARGET_NAME} POST_BUILD
218
+ COMMAND lib /OUT:${CMAKE_CURRENT_BINARY_DIR}/lib${TARGET_NAME}.lib ${libfiles}
219
+ )
220
+ endif (WIN32 )
191
221
endfunction (merge_static_libs )
192
222
193
223
function (cc_library TARGET_NAME )
194
224
set (options STATIC static SHARED shared )
195
225
set (oneValueArgs "" )
196
226
set (multiValueArgs SRCS DEPS )
197
227
cmake_parse_arguments (cc_library "${options} " "${oneValueArgs} " "${multiValueArgs} " ${ARGN} )
228
+ if (WIN32 )
229
+ # add libxxx.lib prefix in windows
230
+ set (${TARGET_NAME} _LIB_NAME "${CMAKE_STATIC_LIBRARY_PREFIX}${TARGET_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX} " CACHE STRING "output library name for target ${TARGET_NAME} " )
231
+ endif (WIN32 )
198
232
if (cc_library_SRCS )
199
233
if (cc_library_SHARED OR cc_library_shared ) # build *.so
200
234
add_library (${TARGET_NAME} SHARED ${cc_library_SRCS} )
0 commit comments