Skip to content

Commit 5df6581

Browse files
authored
merge_static_libs (#12936)
1 parent 593ac0f commit 5df6581

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

cmake/generic.cmake

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ function(merge_static_libs TARGET_NAME)
148148
COMMAND rm "${CMAKE_CURRENT_BINARY_DIR}/lib${TARGET_NAME}.a"
149149
COMMAND /usr/bin/libtool -static -o "${CMAKE_CURRENT_BINARY_DIR}/lib${TARGET_NAME}.a" ${libfiles}
150150
)
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
152153
set(target_DIR ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}.dir)
153154

154155
foreach(lib ${libs})
@@ -187,14 +188,47 @@ function(merge_static_libs TARGET_NAME)
187188
COMMAND ${CMAKE_AR} crs ${target_LIBNAME} `find ${target_DIR} -name '*.o'`
188189
COMMAND ${CMAKE_RANLIB} ${target_LIBNAME}
189190
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)
191221
endfunction(merge_static_libs)
192222

193223
function(cc_library TARGET_NAME)
194224
set(options STATIC static SHARED shared)
195225
set(oneValueArgs "")
196226
set(multiValueArgs SRCS DEPS)
197227
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)
198232
if(cc_library_SRCS)
199233
if(cc_library_SHARED OR cc_library_shared) # build *.so
200234
add_library(${TARGET_NAME} SHARED ${cc_library_SRCS})

cmake/inference_lib.cmake

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ if(WITH_MKLDNN)
101101
)
102102
endif()
103103

104+
if (NOT WIN32)
104105
if(NOT MOBILE_INFERENCE AND NOT RPI)
105106
set(dst_dir "${FLUID_INSTALL_DIR}/third_party/install/snappy")
106107
copy(snappy_lib
@@ -120,15 +121,23 @@ if(NOT MOBILE_INFERENCE AND NOT RPI)
120121
DSTS ${dst_dir} ${dst_dir}/lib
121122
DEPS zlib)
122123
endif()
124+
endif(NOT WIN32)
123125

124126
# paddle fluid module
125127
set(src_dir "${PADDLE_SOURCE_DIR}/paddle/fluid")
126128
set(dst_dir "${FLUID_INSTALL_DIR}/paddle/fluid")
127129
set(module "framework")
130+
if (NOT WIN32)
128131
copy(framework_lib DEPS framework_py_proto
129132
SRCS ${src_dir}/${module}/*.h ${src_dir}/${module}/details/*.h ${PADDLE_BINARY_DIR}/paddle/fluid/framework/framework.pb.h
130133
DSTS ${dst_dir}/${module} ${dst_dir}/${module}/details ${dst_dir}/${module}
131134
)
135+
else()
136+
copy(framework_lib
137+
SRCS ${src_dir}/${module}/*.h ${src_dir}/${module}/details/*.h ${PADDLE_BINARY_DIR}/paddle/fluid/framework/framework.pb.h
138+
DSTS ${dst_dir}/${module} ${dst_dir}/${module}/details ${dst_dir}/${module}
139+
)
140+
endif(NOT WIN32)
132141

133142
set(module "memory")
134143
copy(memory_lib

0 commit comments

Comments
 (0)