Skip to content

Commit 2d8467e

Browse files
authored
Merge pull request #4320 from Xreki/fix_android_linking_error
Fix bug in cc_library, when merging several libraries into one on Linux.
2 parents d35417e + dd2f477 commit 2d8467e

File tree

2 files changed

+35
-31
lines changed

2 files changed

+35
-31
lines changed

cmake/generic.cmake

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -106,22 +106,22 @@ function(merge_static_libs TARGET_NAME)
106106
endforeach()
107107
list(REMOVE_DUPLICATES libs_deps)
108108

109-
if(APPLE) # Use OSX's libtool to merge archives
110-
# To produce a library we need at least one source file.
111-
# It is created by add_custom_command below and will helps
112-
# also help to track dependencies.
113-
set(dummyfile ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}_dummy.c)
109+
# To produce a library we need at least one source file.
110+
# It is created by add_custom_command below and will helps
111+
# also help to track dependencies.
112+
set(target_SRCS ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}_dummy.c)
114113

114+
if(APPLE) # Use OSX's libtool to merge archives
115115
# Make the generated dummy source file depended on all static input
116116
# libs. If input lib changes,the source file is touched
117117
# which causes the desired effect (relink).
118-
add_custom_command(OUTPUT ${dummyfile}
119-
COMMAND ${CMAKE_COMMAND} -E touch ${dummyfile}
118+
add_custom_command(OUTPUT ${target_SRCS}
119+
COMMAND ${CMAKE_COMMAND} -E touch ${target_SRCS}
120120
DEPENDS ${libs})
121121

122122
# Generate dummy staic lib
123-
file(WRITE ${dummyfile} "const char * dummy = \"${dummyfile}\";")
124-
add_library(${TARGET_NAME} STATIC ${dummyfile})
123+
file(WRITE ${target_SRCS} "const char *dummy = \"${target_SRCS}\";")
124+
add_library(${TARGET_NAME} STATIC ${target_SRCS})
125125
target_link_libraries(${TARGET_NAME} ${libs_deps})
126126

127127
foreach(lib ${libs})
@@ -130,43 +130,47 @@ function(merge_static_libs TARGET_NAME)
130130
endforeach()
131131
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
132132
COMMAND rm "${CMAKE_CURRENT_BINARY_DIR}/lib${TARGET_NAME}.a"
133-
COMMAND /usr/bin/libtool -static -o "${CMAKE_CURRENT_BINARY_DIR}/lib${TARGET_NAME}.a" ${libfiles})
133+
COMMAND /usr/bin/libtool -static -o "${CMAKE_CURRENT_BINARY_DIR}/lib${TARGET_NAME}.a" ${libfiles}
134+
)
134135
else() # general UNIX: use "ar" to extract objects and re-add to a common lib
136+
set(target_DIR ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}.dir)
137+
135138
foreach(lib ${libs})
136-
set(objlistfile ${lib}.objlist) # list of objects in the input library
137-
set(objdir ${lib}.objdir)
139+
set(objlistfile ${target_DIR}/${lib}.objlist) # list of objects in the input library
140+
set(objdir ${target_DIR}/${lib}.objdir)
138141

139142
add_custom_command(OUTPUT ${objdir}
140143
COMMAND ${CMAKE_COMMAND} -E make_directory ${objdir}
141144
DEPENDS ${lib})
142145

143146
add_custom_command(OUTPUT ${objlistfile}
144147
COMMAND ${CMAKE_AR} -x "$<TARGET_FILE:${lib}>"
145-
COMMAND ${CMAKE_AR} -t "$<TARGET_FILE:${lib}>" > ../${objlistfile}
148+
COMMAND ${CMAKE_AR} -t "$<TARGET_FILE:${lib}>" > ${objlistfile}
146149
DEPENDS ${lib} ${objdir}
147150
WORKING_DIRECTORY ${objdir})
148151

149-
# Empty dummy source file that goes into merged library
150-
set(mergebase ${lib}.mergebase.c)
151-
add_custom_command(OUTPUT ${mergebase}
152-
COMMAND ${CMAKE_COMMAND} -E touch ${mergebase}
153-
DEPENDS ${objlistfile})
154-
155-
list(APPEND mergebases "${mergebase}")
152+
list(APPEND target_OBJS "${objlistfile}")
156153
endforeach()
157154

158-
add_library(${TARGET_NAME} STATIC ${mergebases})
155+
# Make the generated dummy source file depended on all static input
156+
# libs. If input lib changes,the source file is touched
157+
# which causes the desired effect (relink).
158+
add_custom_command(OUTPUT ${target_SRCS}
159+
COMMAND ${CMAKE_COMMAND} -E touch ${target_SRCS}
160+
DEPENDS ${libs} ${target_OBJS})
161+
162+
# Generate dummy staic lib
163+
file(WRITE ${target_SRCS} "const char *dummy = \"${target_SRCS}\";")
164+
add_library(${TARGET_NAME} STATIC ${target_SRCS})
159165
target_link_libraries(${TARGET_NAME} ${libs_deps})
160166

161167
# Get the file name of the generated library
162-
set(outlibfile "$<TARGET_FILE:${TARGET_NAME}>")
168+
set(target_LIBNAME "$<TARGET_FILE:${TARGET_NAME}>")
163169

164-
foreach(lib ${libs})
165-
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
166-
COMMAND ${CMAKE_AR} cr ${outlibfile} *.o
167-
COMMAND ${CMAKE_RANLIB} ${outlibfile}
168-
WORKING_DIRECTORY ${lib}.objdir)
169-
endforeach()
170+
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD
171+
COMMAND ${CMAKE_AR} crs ${target_LIBNAME} `find ${target_DIR} -name '*.o'`
172+
COMMAND ${CMAKE_RANLIB} ${target_LIBNAME}
173+
WORKING_DIRECTORY ${target_DIR})
170174
endif()
171175
endfunction(merge_static_libs)
172176

@@ -196,7 +200,7 @@ function(cc_library TARGET_NAME)
196200
add_style_check_target(${TARGET_NAME} ${cc_library_SRCS} ${cc_library_HEADERS})
197201

198202
else(cc_library_SRCS)
199-
if (cc_library_DEPS)
203+
if(cc_library_DEPS)
200204
merge_static_libs(${TARGET_NAME} ${cc_library_DEPS})
201205
else()
202206
message(FATAL "Please specify source file or library in cc_library.")

paddle/capi/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ if(ANDROID)
6262
LIBRARY DESTINATION lib/${ANDROID_ABI})
6363
execute_process(
6464
COMMAND ${GIT_EXECUTABLE} log --pretty=oneline -1
65+
WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}
6566
OUTPUT_VARIABLE GIT_COMMITS_LIST
6667
RESULT_VARIABLE GIT_COMMITS_LIST_RESULT
6768
ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
@@ -81,8 +82,7 @@ if(ANDROID)
8182
)"
8283
)
8384
else(ANDROID)
84-
install(TARGETS paddle_capi_whole
85-
ARCHIVE DESTINATION lib)
85+
install(TARGETS paddle_capi_whole ARCHIVE DESTINATION lib)
8686
if(NOT IOS)
8787
install(TARGETS paddle_capi_shared DESTINATION lib)
8888
endif()

0 commit comments

Comments
 (0)