@@ -176,6 +176,36 @@ function(create_static_lib TARGET_NAME)
176176 endif ()
177177endfunction ()
178178
179+ function (create_dummy_static_lib TARGET_NAME)
180+ set (options "" )
181+ set (oneValueArgs "" )
182+ set (multiValueArgs LIBS DEPS LIMIT)
183+ cmake_parse_arguments (merge "${options} " "${oneValueArgs} " "${multiValueArgs} " ${ARGN} )
184+
185+ list (REMOVE_DUPLICATES merge_LIBS)
186+ set (index 1)
187+ set (offset 1)
188+ # the dummy target would be consisted of limit size libraries
189+ set (limit ${merge_LIMIT} )
190+ list (LENGTH merge_LIBS libs_len)
191+ foreach (lib ${merge_LIBS} )
192+ list (APPEND merge_list ${lib} )
193+ list (LENGTH merge_list listlen)
194+ if ((${listlen} GREATER ${limit} ) OR (${offset} EQUAL ${libs_len} ))
195+ message ("Merge and generate static library: ${TARGET_NAME} _static_${index} " )
196+ merge_static_libs(${TARGET_NAME} _static_${index} ${merge_list} )
197+ if (merge_DEPS)
198+ target_link_libraries (${TARGET_NAME} _static_${index} ${merge_DEPS} )
199+ endif ()
200+ set (merge_list)
201+ list (APPEND ${TARGET_NAME} _list ${TARGET_NAME} _static_${index} )
202+ MATH (EXPR index "${index} +1" )
203+ endif ()
204+ MATH (EXPR offset "${offset} +1" )
205+ endforeach ()
206+ cc_library(${TARGET_NAME} DEPS ${${TARGET_NAME} _list})
207+ endfunction ()
208+
179209function (merge_static_libs TARGET_NAME)
180210 set (libs ${ARGN} )
181211 list (REMOVE_DUPLICATES libs)
@@ -193,92 +223,61 @@ function(merge_static_libs TARGET_NAME)
193223 # also help to track dependencies.
194224 set (target_SRCS ${CMAKE_CURRENT_BINARY_DIR} /${TARGET_NAME} _dummy.c)
195225
196- if (APPLE ) # Use OSX's libtool to merge archives
197- # Make the generated dummy source file depended on all static input
198- # libs. If input lib changes,the source file is touched
199- # which causes the desired effect (relink).
200- add_custom_command (OUTPUT ${target_SRCS}
201- COMMAND ${CMAKE_COMMAND} -E touch ${target_SRCS}
202- DEPENDS ${libs} )
203-
204- # Generate dummy static lib
205- generate_dummy_static_lib(LIB_NAME ${TARGET_NAME} FILE_PATH ${target_SRCS} GENERATOR "generic.cmake:merge_static_libs" )
206-
207- target_link_libraries (${TARGET_NAME} ${libs_deps} )
226+ # Make the generated dummy source file depended on all static input
227+ # libs. If input lib changes,the source file is touched
228+ # which causes the desired effect (relink).
229+ add_custom_command (OUTPUT ${target_SRCS}
230+ COMMAND ${CMAKE_COMMAND} -E touch ${target_SRCS}
231+ DEPENDS ${libs} )
232+
233+ # Generate dummy staic lib
234+ generate_dummy_static_lib(LIB_NAME ${TARGET_NAME} FILE_PATH ${target_SRCS} GENERATOR "generic.cmake:merge_static_libs" )
235+ target_link_libraries (${TARGET_NAME} ${libs_deps} )
208236
237+ # OSX: use 'libtool' to merge archives
238+ if (APPLE )
209239 foreach (lib ${libs} )
210240 # Get the file names of the libraries to be merged
211241 set (libfiles ${libfiles} $<TARGET_FILE:${lib} >)
212242 endforeach ()
213243 add_custom_command (TARGET ${TARGET_NAME} POST_BUILD
244+ COMMENT "Merge and generate static lib: lib${TARGET_NAME} .a"
214245 COMMAND rm "${CMAKE_CURRENT_BINARY_DIR} /lib${TARGET_NAME} .a"
215246 COMMAND /usr/bin/libtool -static -o "${CMAKE_CURRENT_BINARY_DIR} /lib${TARGET_NAME} .a" ${libfiles}
216247 )
217- endif (APPLE )
218- if (LINUX) # general UNIX: use "ar" to extract objects and re-add to a common lib
219- set (target_DIR ${CMAKE_CURRENT_BINARY_DIR} /${TARGET_NAME} .dir)
220-
221- foreach (lib ${libs} )
222- set (objlistfile ${target_DIR} /${lib} .objlist) # list of objects in the input library
223- set (objdir ${target_DIR} /${lib} .objdir)
224-
225- add_custom_command (OUTPUT ${objdir}
226- COMMAND ${CMAKE_COMMAND} -E make_directory ${objdir}
227- DEPENDS ${lib} )
248+ endif ()
228249
229- add_custom_command ( OUTPUT ${objlistfile}
230- COMMAND ${CMAKE_AR} -x "$<TARGET_FILE: ${lib} >"
231- COMMAND ${CMAKE_AR} -t "$<TARGET_FILE: ${lib} >" > ${objlistfile}
232- DEPENDS ${lib} ${objdir}
233- WORKING_DIRECTORY ${objdir} )
250+ # LINUX: use "ar" to extract objects and re-add to a common lib
251+ if (LINUX)
252+ set (mri_file ${CMAKE_CURRENT_BINARY_DIR} / ${TARGET_NAME} .mri CACHE INTERNAL "phi_static.mri file" )
253+ get_property (ABS_MERGE_LIB_PATH TARGET ${TARGET_NAME} PROPERTY LOCATION )
254+ file (WRITE ${mri_file} "create ${ABS_MERGE_LIB_PATH} \n " )
234255
235- list (APPEND target_OBJS "${objlistfile} " )
256+ foreach (lib ${libs} )
257+ get_property (ABS_LIB_PATH TARGET ${lib} PROPERTY LOCATION )
258+ file (APPEND ${mri_file} "addlib ${ABS_LIB_PATH} \n " )
236259 endforeach ()
237-
238- # Make the generated dummy source file depended on all static input
239- # libs. If input lib changes,the source file is touched
240- # which causes the desired effect (relink).
241- add_custom_command (OUTPUT ${target_SRCS}
242- COMMAND ${CMAKE_COMMAND} -E touch ${target_SRCS}
243- DEPENDS ${libs} ${target_OBJS} )
244-
245- # Generate dummy staic lib
246- generate_dummy_static_lib(LIB_NAME ${TARGET_NAME} FILE_PATH ${target_SRCS} GENERATOR "generic.cmake:merge_static_libs" )
247-
248- target_link_libraries (${TARGET_NAME} ${libs_deps} )
249-
250- # Get the file name of the generated library
251- set (target_LIBNAME "$<TARGET_FILE:${TARGET_NAME} >" )
260+ file (APPEND ${mri_file} "save\n end\n " )
252261
253262 add_custom_command (TARGET ${TARGET_NAME} POST_BUILD
254- COMMAND ${CMAKE_AR} crs ${target_LIBNAME} `find ${target_DIR} -name '*.o'`
255- COMMAND ${CMAKE_RANLIB} ${target_LIBNAME}
256- WORKING_DIRECTORY ${target_DIR} )
257- endif (LINUX)
258- if (WIN32 ) # windows do not support gcc/nvcc combined compiling. Use msvc lib.exe to merge libs.
259- # Make the generated dummy source file depended on all static input
260- # libs. If input lib changes,the source file is touched
261- # which causes the desired effect (relink).
262- add_custom_command (OUTPUT ${target_SRCS}
263- COMMAND ${CMAKE_COMMAND} -E touch ${target_SRCS}
264- DEPENDS ${libs} )
265- # Generate dummy staic lib
266- generate_dummy_static_lib(LIB_NAME ${TARGET_NAME} FILE_PATH ${target_SRCS} GENERATOR "generic.cmake:merge_static_libs" )
267-
268- target_link_libraries (${TARGET_NAME} ${libs_deps} )
263+ COMMENT "Merge and generate static lib: lib${TARGET_NAME} .a"
264+ COMMAND ${CMAKE_AR} -M < ${mri_file}
265+ COMMAND ${CMAKE_RANLIB} "$<TARGET_FILE:${TARGET_NAME} >" )
266+ endif ()
269267
268+ # Windows do not support gcc/nvcc combined compiling. Use msvc 'lib.exe' to merge libs.
269+ if (WIN32 )
270270 foreach (lib ${libs} )
271- # Get the file names of the libraries to be merged
272271 set (libfiles ${libfiles} $<TARGET_FILE:${lib} >)
273272 endforeach ()
274- # msvc will put libarary in directory of "/Release/xxxlib" by default
275- # COMMAND cmake -E remove "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}/${TARGET_NAME}.lib"
273+ # msvc compiler will put libarary in directory of "/Release/xxxlib" by default
276274 add_custom_command (TARGET ${TARGET_NAME} POST_BUILD
275+ COMMENT "Merge and generate static lib: lib${TARGET_NAME} .lib"
277276 COMMAND cmake -E make_directory $<TARGET_FILE_DIR:${TARGET_NAME} >
278277 COMMAND lib /OUT:$<TARGET_FILE:${TARGET_NAME} > ${libfiles}
279278 )
280- endif (WIN32 )
281- endfunction (merge_static_libs )
279+ endif ()
280+ endfunction ()
282281
283282function (check_coverage_opt TARGET_NAME SRCS)
284283 if (WITH_COVERAGE AND WITH_INCREMENTAL_COVERAGE)
@@ -1076,4 +1075,3 @@ function(math_library TARGET)
10761075 cc_library(${TARGET} SRCS ${cc_srcs} DEPS ${math_library_DEPS} ${math_common_deps} )
10771076 endif ()
10781077endfunction ()
1079-
0 commit comments