utils.cmake 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. # This function comes from https://blog.csdn.net/yindongjie1221/article/details/90614261
  2. function(redefine_file_macro targetname)
  3. get_target_property(source_files "${targetname}" SOURCES)
  4. foreach(sourcefile ${source_files})
  5. get_property(defs SOURCE "${sourcefile}"
  6. PROPERTY COMPILE_DEFINITIONS)
  7. get_filename_component(filepath "${sourcefile}" ABSOLUTE)
  8. string(REPLACE ${PROJECT_SOURCE_DIR}/ "" relpath ${filepath})
  9. list(APPEND defs "__REL_FILE__=\"${relpath}\"")
  10. set_property(
  11. SOURCE "${sourcefile}"
  12. PROPERTY COMPILE_DEFINITIONS ${defs}
  13. )
  14. endforeach()
  15. endfunction()
  16. function(download_and_decompress url filename decompress_dir)
  17. if(NOT EXISTS ${filename})
  18. message("Downloading file from ${url} to ${filename} ...")
  19. file(DOWNLOAD ${url} "${filename}.tmp" SHOW_PROGRESS)
  20. file(RENAME "${filename}.tmp" ${filename})
  21. endif()
  22. if(NOT EXISTS ${decompress_dir})
  23. file(MAKE_DIRECTORY ${decompress_dir})
  24. endif()
  25. message("Decompress file ${filename} ...")
  26. execute_process(COMMAND ${CMAKE_COMMAND} -E tar -xf ${filename} WORKING_DIRECTORY ${decompress_dir})
  27. endfunction()
  28. function(get_openvino_libs OPENVINO_RUNTIME_DIR)
  29. set(LIB_LIST "")
  30. find_library(OPENVINO_LIB openvino PATHS ${OPENVINO_RUNTIME_DIR}/lib/ ${OPENVINO_RUNTIME_DIR}/lib/intel64 NO_DEFAULT_PATH)
  31. list(APPEND LIB_LIST ${OPENVINO_LIB})
  32. set(TBB_DIR ${OPENVINO_RUNTIME_DIR}/3rdparty/tbb/lib/cmake)
  33. message(STATUS "TBB_DIR: ${TBB_DIR}")
  34. find_package(TBB PATHS ${TBB_DIR})
  35. if (TBB_FOUND)
  36. # 2024.10.22(zhangyue): Use openvino with tbb on linux
  37. set(TBB_LIB "${OPENVINO_RUNTIME_DIR}/3rdparty/tbb/lib/libtbb.so")
  38. list(APPEND LIB_LIST ${TBB_LIB})
  39. else()
  40. # TODO(zhoushunjie): Use openvino with tbb on linux in future.
  41. set(OMP_LIB "${OPENVINO_RUNTIME_DIR}/3rdparty/omp/lib/libiomp5.so")
  42. list(APPEND LIB_LIST ${OMP_LIB})
  43. endif()
  44. set(OPENVINO_LIBS ${LIB_LIST} PARENT_SCOPE)
  45. endfunction()
  46. function(remove_duplicate_libraries libraries)
  47. list(LENGTH ${libraries} lib_length)
  48. set(libraries_temp "")
  49. set(full_libraries "")
  50. foreach(lib_path ${${libraries}})
  51. get_filename_component(lib_name ${lib_path} NAME)
  52. list(FIND libraries_temp ${lib_name} lib_idx)
  53. if (${lib_idx} EQUAL -1)
  54. list(APPEND libraries_temp ${lib_name})
  55. list(APPEND full_libraries ${lib_path})
  56. endif()
  57. endforeach()
  58. set(${libraries} ${full_libraries} PARENT_SCOPE)
  59. endfunction()
  60. function(get_windows_path win_path origin_path)
  61. STRING(REGEX REPLACE "/" "\\\\" _win_path ${origin_path})
  62. set(${win_path} ${_win_path} PARENT_SCOPE)
  63. endfunction()
  64. function(get_osx_architecture)
  65. if (CMAKE_OSX_ARCHITECTURES STREQUAL "arm64")
  66. set(CURRENT_OSX_ARCH "arm64" PARENT_SCOPE)
  67. elseif(CMAKE_OSX_ARCHITECTURES STREQUAL "x86_64")
  68. set(CURRENT_OSX_ARCH "x86_64" PARENT_SCOPE)
  69. else()
  70. set(CURRENT_OSX_ARCH ${CMAKE_HOST_SYSTEM_PROCESSOR} PARENT_SCOPE)
  71. endif()
  72. endfunction()
  73. # A fake target to include all the libraries and tests the ultra_infer module depends.
  74. add_custom_target(fd_compile_deps COMMAND echo 1)
  75. # A function to grep LINK_ONLY dependencies from INTERFACE_LINK_LIBRARIES
  76. function(regrex_link_only_libraries OUTPUT_DEPS PUBLIC_DEPS)
  77. string(JOIN "#" _public_deps ${PUBLIC_DEPS})
  78. string(REPLACE "$<LINK_ONLY:" "" _public_deps ${_public_deps})
  79. string(REPLACE ">" "" _public_deps ${_public_deps})
  80. string(REPLACE "#" ";" _public_deps ${_public_deps})
  81. set(${OUTPUT_DEPS} ${_public_deps} PARENT_SCOPE)
  82. endfunction()
  83. # Bundle several static libraries into one. This function is modified from Paddle Lite.
  84. # reference: https://github.com/PaddlePaddle/Paddle-Lite/blob/develop/cmake/lite.cmake#L252
  85. function(bundle_static_library tgt_name bundled_tgt_name fake_target)
  86. list(APPEND static_libs ${tgt_name})
  87. add_dependencies(fd_compile_deps ${fake_target})
  88. # Set redundant static libs here, protobuf is already available
  89. # in the Paddle Lite static library. So, we don't need protobuf
  90. # in opencv. And there is no need for opencv_dnn, opencv_ml,
  91. # opencv_flann and some other modules. Therefore, we chose
  92. # to discard these redundant modules.
  93. set(REDUNDANT_STATIC_LIBS opencv_dnn opencv_calib3d opencv_photo
  94. opencv_flann opencv_objdetect opencv_stitching opencv_gapi
  95. opencv_ml libprotobuf)
  96. function(_recursively_collect_dependencies input_target)
  97. list(FIND REDUNDANT_STATIC_LIBS ${input_target} _input_redunant_id)
  98. if(${_input_redunant_id} GREATER 0)
  99. return()
  100. endif()
  101. set(_input_link_libraries LINK_LIBRARIES)
  102. # https://cmake.org/cmake/help/latest/prop_tgt/TYPE.html
  103. get_target_property(_input_type ${input_target} TYPE)
  104. # In OpenCVModules.cmake, they set the deps of modules
  105. # (opencv_core,...) as INTERFACE_LINK_LIBRARIES. The
  106. # 'Type' of opencv static lib is set as 'STATIC_LIBRARY'.
  107. if ((${_input_type} STREQUAL "INTERFACE_LIBRARY")
  108. OR (${_input_type} STREQUAL "STATIC_LIBRARY"))
  109. set(_input_link_libraries INTERFACE_LINK_LIBRARIES)
  110. endif()
  111. get_target_property(_public_dependencies ${input_target} ${_input_link_libraries})
  112. regrex_link_only_libraries(public_dependencies "${_public_dependencies}")
  113. foreach(dependency IN LISTS public_dependencies)
  114. if(TARGET ${dependency})
  115. get_target_property(alias ${dependency} ALIASED_TARGET)
  116. if (TARGET ${alias})
  117. set(dependency ${alias})
  118. endif()
  119. get_target_property(_type ${dependency} TYPE)
  120. list(FIND REDUNDANT_STATIC_LIBS ${dependency} _deps_redunant_id)
  121. if (${_type} STREQUAL "STATIC_LIBRARY" AND
  122. (NOT (${_deps_redunant_id} GREATER 0)))
  123. list(APPEND static_libs ${dependency})
  124. endif()
  125. get_property(library_already_added
  126. GLOBAL PROPERTY _${tgt_name}_static_bundle_${dependency})
  127. if (NOT library_already_added)
  128. set_property(GLOBAL PROPERTY _${tgt_name}_static_bundle_${dependency} ON)
  129. if(NOT (${_deps_redunant_id} GREATER 0))
  130. _recursively_collect_dependencies(${dependency})
  131. endif()
  132. endif()
  133. endif()
  134. endforeach()
  135. set(static_libs ${static_libs} PARENT_SCOPE)
  136. endfunction()
  137. _recursively_collect_dependencies(${tgt_name})
  138. list(REMOVE_DUPLICATES static_libs)
  139. list(REMOVE_ITEM static_libs ${REDUNDANT_STATIC_LIBS})
  140. message(STATUS "WITH_STATIC_LIB=${WITH_STATIC_LIB}, Found all needed static libs from dependency tree: ${static_libs}")
  141. message(STATUS "Exclude some redundant static libs: ${REDUNDANT_STATIC_LIBS}")
  142. set(bundled_tgt_full_name
  143. ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${bundled_tgt_name}${CMAKE_STATIC_LIBRARY_SUFFIX})
  144. message(STATUS "Use bundled_tgt_full_name: ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${bundled_tgt_name}${CMAKE_STATIC_LIBRARY_SUFFIX}")
  145. if(WIN32)
  146. message(FATAL_ERROR "Not support UltraInfer static lib for windows now.")
  147. endif()
  148. add_custom_target(${fake_target} ALL COMMAND ${CMAKE_COMMAND} -E echo "Building fake_target ${fake_target}")
  149. add_dependencies(${fake_target} ${tgt_name})
  150. # add_dependencies(${fake_target} fastdelpoy_dummy)
  151. if(NOT IOS AND NOT APPLE)
  152. file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/${bundled_tgt_name}.ar.in
  153. "CREATE ${bundled_tgt_full_name}\n" )
  154. foreach(tgt IN LISTS static_libs)
  155. file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/${bundled_tgt_name}.ar.in
  156. "ADDLIB $<TARGET_FILE:${tgt}>\n")
  157. endforeach()
  158. file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/${bundled_tgt_name}.ar.in "SAVE\n")
  159. file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/${bundled_tgt_name}.ar.in "END\n")
  160. file(GENERATE
  161. OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${bundled_tgt_name}.ar
  162. INPUT ${CMAKE_CURRENT_BINARY_DIR}/${bundled_tgt_name}.ar.in)
  163. set(ar_tool ${CMAKE_AR})
  164. if (CMAKE_INTERPROCEDURAL_OPTIMIZATION)
  165. set(ar_tool ${CMAKE_CXX_COMPILER_AR})
  166. endif()
  167. message(STATUS "Found ar_tool: ${ar_tool}")
  168. add_custom_command(
  169. TARGET ${fake_target} PRE_BUILD
  170. COMMAND rm -f ${bundled_tgt_full_name}
  171. COMMAND ${ar_tool} -M < ${CMAKE_CURRENT_BINARY_DIR}/${bundled_tgt_name}.ar
  172. COMMENT "Bundling ${bundled_tgt_name}"
  173. COMMAND ${CMAKE_STRIP} --strip-unneeded ${CMAKE_CURRENT_BINARY_DIR}/lib${bundled_tgt_name}.a
  174. COMMENT "Stripped unneeded debug symbols in ${bundled_tgt_name}"
  175. DEPENDS ${tgt_name}
  176. VERBATIM)
  177. else()
  178. foreach(lib ${static_libs})
  179. set(libfiles ${libfiles} $<TARGET_FILE:${lib}>)
  180. endforeach()
  181. add_custom_command(
  182. TARGET ${fake_target} PRE_BUILD
  183. COMMAND rm -f ${bundled_tgt_full_name}
  184. COMMAND /usr/bin/libtool -static -o ${bundled_tgt_full_name} ${libfiles}
  185. COMMENT "Bundling ${bundled_tgt_name}"
  186. COMMAND ${CMAKE_STRIP} -S ${CMAKE_CURRENT_BINARY_DIR}/lib${bundled_tgt_name}.a
  187. COMMENT "Stripped unneeded debug symbols in ${bundled_tgt_name}"
  188. DEPENDS ${tgt_name}
  189. )
  190. endif()
  191. add_library(${bundled_tgt_name} STATIC IMPORTED GLOBAL)
  192. set_property(TARGET ${bundled_tgt_name} PROPERTY IMPORTED_LOCATION
  193. ${bundled_tgt_full_name})
  194. add_dependencies(${bundled_tgt_name} ${fake_target})
  195. add_dependencies(${bundled_tgt_name} ${tgt_name})
  196. endfunction()