CMakeLists.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. # ----------------------------------------------------------------------------
  2. # CMake file for libpng. See root CMakeLists.txt
  3. #
  4. # ----------------------------------------------------------------------------
  5. project(${PNG_LIBRARY} C)
  6. if(UNIX AND NOT APPLE AND NOT BEOS AND NOT HAIKU AND NOT EMSCRIPTEN)
  7. find_library(M_LIBRARY m)
  8. if(M_LIBRARY)
  9. set(M_LIBRARY m)
  10. else()
  11. set(M_LIBRARY "")
  12. endif()
  13. else()
  14. # libm is not available or not needed.
  15. endif()
  16. ocv_include_directories("${CMAKE_CURRENT_SOURCE_DIR}" ${ZLIB_INCLUDE_DIRS})
  17. file(GLOB lib_srcs *.c)
  18. file(GLOB lib_hdrs *.h)
  19. # CMake currently sets CMAKE_SYSTEM_PROCESSOR to one of x86_64 or arm64 on macOS,
  20. # based upon the OS architecture, not the target architecture. As such, we need
  21. # to check CMAKE_OSX_ARCHITECTURES to identify which hardware-specific flags to
  22. # enable. Note that this will fail if you attempt to build a universal binary in
  23. # a single CMake invocation.
  24. if(APPLE AND CMAKE_OSX_ARCHITECTURES)
  25. set(TARGET_ARCH ${CMAKE_OSX_ARCHITECTURES})
  26. else()
  27. set(TARGET_ARCH ${CMAKE_SYSTEM_PROCESSOR})
  28. endif()
  29. OCV_OPTION(PNG_HARDWARE_OPTIMIZATIONS "Enable Hardware Optimizations, if available for this platform" (NOT CV_DISABLE_OPTIMIZATION))
  30. if(PNG_HARDWARE_OPTIMIZATIONS)
  31. # Set definitions and sources for ARM.
  32. if(TARGET_ARCH MATCHES "^(ARM|arm|aarch)")
  33. if(TARGET_ARCH MATCHES "^(ARM64|arm64|aarch64)")
  34. set(PNG_ARM_NEON_POSSIBLE_VALUES on off)
  35. set(PNG_ARM_NEON "on"
  36. CACHE STRING "Enable ARM NEON optimizations: on|off; on is default")
  37. else()
  38. set(PNG_ARM_NEON_POSSIBLE_VALUES check on off)
  39. set(PNG_ARM_NEON "off"
  40. CACHE STRING "Enable ARM NEON optimizations: check|on|off; off is default")
  41. endif()
  42. set_property(CACHE PNG_ARM_NEON
  43. PROPERTY STRINGS ${PNG_ARM_NEON_POSSIBLE_VALUES})
  44. list(FIND PNG_ARM_NEON_POSSIBLE_VALUES ${PNG_ARM_NEON} index)
  45. if(index EQUAL -1)
  46. message(FATAL_ERROR "PNG_ARM_NEON must be one of [${PNG_ARM_NEON_POSSIBLE_VALUES}]")
  47. elseif(NOT PNG_ARM_NEON STREQUAL "off")
  48. list(APPEND lib_srcs arm/arm_init.c arm/filter_neon_intrinsics.c arm/palette_neon_intrinsics.c)
  49. if(NOT MSVC)
  50. if(CMAKE_SYSTEM_NAME STREQUAL "QNX")
  51. message(STATUS "Skipping arm/filter_neon.S on QNX")
  52. else()
  53. enable_language(ASM)
  54. list(APPEND lib_srcs arm/filter_neon.S)
  55. endif()
  56. endif()
  57. if(PNG_ARM_NEON STREQUAL "on")
  58. add_definitions(-DPNG_ARM_NEON_OPT=2)
  59. elseif(PNG_ARM_NEON STREQUAL "check")
  60. add_definitions(-DPNG_ARM_NEON_CHECK_SUPPORTED)
  61. endif()
  62. else()
  63. add_definitions(-DPNG_ARM_NEON_OPT=0) # NEON assembler is not supported
  64. endif()
  65. endif()
  66. # Set definitions and sources for PowerPC.
  67. if(TARGET_ARCH MATCHES "^(powerpc|ppc64)")
  68. if (CPU_VSX_SUPPORTED)
  69. set(CPU_VSX_SUPPORTED_STR "on")
  70. else()
  71. set(CPU_VSX_SUPPORTED_STR "off")
  72. endif()
  73. set(PNG_POWERPC_VSX_POSSIBLE_VALUES on off)
  74. set(PNG_POWERPC_VSX ${CPU_VSX_SUPPORTED_STR}
  75. CACHE STRING "Enable POWERPC VSX optimizations: on|off; on is default")
  76. set_property(CACHE PNG_POWERPC_VSX
  77. PROPERTY STRINGS ${PNG_POWERPC_VSX_POSSIBLE_VALUES})
  78. list(FIND PNG_POWERPC_VSX_POSSIBLE_VALUES ${PNG_POWERPC_VSX} index)
  79. if(index EQUAL -1)
  80. message(FATAL_ERROR "PNG_POWERPC_VSX must be one of [${PNG_POWERPC_VSX_POSSIBLE_VALUES}]")
  81. elseif(NOT PNG_POWERPC_VSX STREQUAL "off")
  82. list(APPEND lib_srcs powerpc/powerpc_init.c powerpc/filter_vsx_intrinsics.c)
  83. if(PNG_POWERPC_VSX STREQUAL "on")
  84. add_definitions(-DPNG_POWERPC_VSX_OPT=2)
  85. endif()
  86. else()
  87. add_definitions(-DPNG_POWERPC_VSX_OPT=0)
  88. endif()
  89. endif()
  90. # Set definitions and sources for Intel.
  91. if(TARGET_ARCH MATCHES "^(i[3-6]86|x86|AMD64)")
  92. set(PNG_INTEL_SSE_POSSIBLE_VALUES on off)
  93. set(PNG_INTEL_SSE "on"
  94. CACHE STRING "Enable INTEL_SSE optimizations: on|off; on is default")
  95. set_property(CACHE PNG_INTEL_SSE
  96. PROPERTY STRINGS ${PNG_INTEL_SSE_POSSIBLE_VALUES})
  97. list(FIND PNG_INTEL_SSE_POSSIBLE_VALUES ${PNG_INTEL_SSE} index)
  98. if(index EQUAL -1)
  99. message(FATAL_ERROR "PNG_INTEL_SSE must be one of [${PNG_INTEL_SSE_POSSIBLE_VALUES}]")
  100. elseif(NOT PNG_INTEL_SSE STREQUAL "off")
  101. list(APPEND lib_srcs intel/intel_init.c intel/filter_sse2_intrinsics.c)
  102. if(PNG_INTEL_SSE STREQUAL "on")
  103. add_definitions(-DPNG_INTEL_SSE_OPT=1)
  104. endif()
  105. else()
  106. add_definitions(-DPNG_INTEL_SSE_OPT=0)
  107. endif()
  108. endif()
  109. # Set definitions and sources for MIPS.
  110. if(TARGET_ARCH MATCHES "^(mipsel|mips64el)")
  111. if (CPU_MSA_SUPPORTED)
  112. set(CPU_MSA_SUPPORTED_STR "on")
  113. else()
  114. set(CPU_MSA_SUPPORTED_STR "off")
  115. endif()
  116. set(PNG_MIPS_MSA_POSSIBLE_VALUES on off)
  117. set(PNG_MIPS_MSA ${CPU_MSA_SUPPORTED_STR}
  118. CACHE STRING "Enable MIPS_MSA optimizations: on|off; on is default")
  119. set_property(CACHE PNG_MIPS_MSA
  120. PROPERTY STRINGS ${PNG_MIPS_MSA_POSSIBLE_VALUES})
  121. list(FIND PNG_MIPS_MSA_POSSIBLE_VALUES ${PNG_MIPS_MSA} index_msa)
  122. if(index_msa EQUAL -1)
  123. message(FATAL_ERROR "PNG_MIPS_MSA must be one of [${PNG_MIPS_MSA_POSSIBLE_VALUES}]")
  124. endif()
  125. set(PNG_MIPS_MMI_POSSIBLE_VALUES on off)
  126. set(PNG_MIPS_MMI "on"
  127. CACHE STRING "Enable MIPS_MMI optimizations: on|off; on is default")
  128. set_property(CACHE PNG_MIPS_MMI
  129. PROPERTY STRINGS ${PNG_MIPS_MMI_POSSIBLE_VALUES})
  130. list(FIND PNG_MIPS_MMI_POSSIBLE_VALUES ${PNG_MIPS_MMI} index_mmi)
  131. if(index_mmi EQUAL -1)
  132. message(FATAL_ERROR "PNG_MIPS_MMI must be one of [${PNG_MIPS_MMI_POSSIBLE_VALUES}]")
  133. endif()
  134. if(PNG_MIPS_MSA STREQUAL "on" AND PNG_MIPS_MMI STREQUAL "on")
  135. list(APPEND lib_srcs mips/mips_init.c mips/filter_msa_intrinsics.c mips/filter_mmi_inline_assembly.c)
  136. add_definitions(-DPNG_MIPS_MSA_OPT=2)
  137. add_definitions(-DPNG_MIPS_MMI_OPT=1)
  138. elseif(PNG_MIPS_MSA STREQUAL "on")
  139. list(APPEND lib_srcs mips/mips_init.c mips/filter_msa_intrinsics.c)
  140. add_definitions(-DPNG_MIPS_MSA_OPT=2)
  141. add_definitions(-DPNG_MIPS_MMI_OPT=0)
  142. elseif(PNG_MIPS_MMI STREQUAL "on")
  143. list(APPEND lib_srcs mips/mips_init.c mips/filter_mmi_inline_assembly.c)
  144. add_definitions(-DPNG_MIPS_MSA_OPT=0)
  145. add_definitions(-DPNG_MIPS_MMI_OPT=1)
  146. else()
  147. add_definitions(-DPNG_MIPS_MSA_OPT=0)
  148. add_definitions(-DPNG_MIPS_MMI_OPT=0)
  149. endif()
  150. endif()
  151. # Set definitions and sources for LoongArch.
  152. if(TARGET_ARCH MATCHES "^(loongarch)")
  153. if (CPU_LSX_SUPPORTED)
  154. set(CPU_LSX_SUPPORTED_STR "on")
  155. else()
  156. set(CPU_LSX_SUPPORTED_STR "off")
  157. endif()
  158. include(CheckCCompilerFlag)
  159. set(PNG_LOONGARCH_LSX_POSSIBLE_VALUES on off)
  160. set(PNG_LOONGARCH_LSX ${CPU_LSX_SUPPORTED_STR}
  161. CACHE STRING "Enable LOONGARCH_LSX optimizations: on|off; on is default")
  162. set_property(CACHE PNG_LOONGARCH_LSX
  163. PROPERTY STRINGS ${PNG_LOONGARCH_LSX_POSSIBLE_VALUES})
  164. list(FIND PNG_LOONGARCH_LSX_POSSIBLE_VALUES ${PNG_LOONGARCH_LSX} index)
  165. if(index EQUAL -1)
  166. message(FATAL_ERROR "PNG_LOONGARCH_LSX must be one of [${PNG_LOONGARCH_LSX_POSSIBLE_VALUES}]")
  167. elseif(NOT PNG_LOONGARCH_LSX STREQUAL "off")
  168. CHECK_C_COMPILER_FLAG("-mlsx" COMPILER_SUPPORTS_LSX)
  169. if(COMPILER_SUPPORTS_LSX)
  170. set(libpng_loongarch_sources
  171. loongarch/loongarch_lsx_init.c
  172. loongarch/filter_lsx_intrinsics.c)
  173. set_source_files_properties(${libpng_loongarch_sources}
  174. PROPERTIES
  175. COMPILE_FLAGS "-mlsx")
  176. list(APPEND lib_srcs ${libpng_loongarch_sources})
  177. add_definitions(-DPNG_LOONGARCH_LSX_OPT=1)
  178. else()
  179. message(FATAL_ERROR "Compiler does not support -mlsx option")
  180. endif()
  181. else()
  182. add_definitions(-DPNG_LOONGARCH_LSX_OPT=0)
  183. endif()
  184. endif()
  185. # Set definitions and sources for RISC-V.
  186. if(TARGET_ARCH MATCHES "^(riscv)")
  187. if (CPU_RVV_SUPPORTED)
  188. set(CPU_RVV_SUPPORTED_STR "on")
  189. else()
  190. set(CPU_RVV_SUPPORTED_STR "off")
  191. endif()
  192. include(CheckCCompilerFlag)
  193. set(PNG_RISCV_RVV_POSSIBLE_VALUES on off)
  194. set(PNG_RISCV_RVV ${CPU_RVV_SUPPORTED_STR}
  195. CACHE STRING "Enable RISC-V Vector optimizations: on|off; off is default")
  196. set_property(CACHE PNG_RISCV_RVV
  197. PROPERTY STRINGS ${PNG_RISCV_RVV_POSSIBLE_VALUES})
  198. list(FIND PNG_RISCV_RVV_POSSIBLE_VALUES ${PNG_RISCV_RVV} index)
  199. if(index EQUAL -1)
  200. message(FATAL_ERROR "PNG_RISCV_RVV must be one of [${PNG_RISCV_RVV_POSSIBLE_VALUES}]")
  201. elseif(NOT PNG_RISCV_RVV STREQUAL "off")
  202. check_c_source_compiles("
  203. #include <riscv_vector.h>
  204. int main() {
  205. const float src[] = { 0.0f, 0.0f, 0.0f, 0.0f };
  206. uint64_t ptr[2] = {0x0908060504020100, 0xFFFFFFFF0E0D0C0A};
  207. vuint8m1_t a = __riscv_vreinterpret_v_u64m1_u8m1(__riscv_vle64_v_u64m1(ptr, 2));
  208. vfloat32m1_t val = __riscv_vle32_v_f32m1((const float*)(src), 4);
  209. return (int)__riscv_vfmv_f_s_f32m1_f32(val);
  210. }" COMPILER_SUPPORTS_RVV)
  211. if(NOT COMPILER_SUPPORTS_RVV)
  212. message(FATAL_ERROR "Compiler does not support RISC-V Vector extension or its unable to detect it")
  213. endif()
  214. list(APPEND lib_srcs riscv/filter_rvv_intrinsics.c riscv/riscv_init.c)
  215. if(PNG_RISCV_RVV STREQUAL "on")
  216. add_definitions(-DPNG_RISCV_RVV_OPT=2)
  217. else()
  218. add_definitions(-DPNG_RISCV_RVV_OPT=0)
  219. endif()
  220. else()
  221. add_definitions(-DPNG_RISCV_RVV_OPT=0)
  222. endif()
  223. endif()
  224. else(PNG_HARDWARE_OPTIMIZATIONS)
  225. # Set definitions and sources for ARM.
  226. if(TARGET_ARCH MATCHES "^(ARM|arm|aarch)")
  227. add_definitions(-DPNG_ARM_NEON_OPT=0)
  228. endif()
  229. # Set definitions and sources for PowerPC.
  230. if(TARGET_ARCH MATCHES "^(powerpc|ppc64)")
  231. add_definitions(-DPNG_POWERPC_VSX_OPT=0)
  232. endif()
  233. # Set definitions and sources for Intel.
  234. if(TARGET_ARCH MATCHES "^(i[3-6]86|x86|AMD64)")
  235. add_definitions(-DPNG_INTEL_SSE_OPT=0)
  236. endif()
  237. # Set definitions and sources for MIPS.
  238. if(TARGET_ARCH MATCHES "^(mipsel|mips64el)")
  239. add_definitions(-DPNG_MIPS_MSA_OPT=0)
  240. endif()
  241. # Set definitions and sources for LoongArch.
  242. if(TARGET_ARCH MATCHES "^(loongarch)")
  243. add_definitions(-DPNG_LOONGARCH_LSX_OPT=0)
  244. endif()
  245. # Set definitions and sources for RISC-V.
  246. if(TARGET_ARCH MATCHES "^(riscv)")
  247. add_definitions(-DPNG_RISCV_RVV_OPT=0)
  248. endif()
  249. endif(PNG_HARDWARE_OPTIMIZATIONS)
  250. # ----------------------------------------------------------------------------------
  251. # Define the library target:
  252. # ----------------------------------------------------------------------------------
  253. if(MSVC)
  254. add_definitions(-D_CRT_SECURE_NO_DEPRECATE)
  255. endif(MSVC)
  256. add_library(${PNG_LIBRARY} STATIC ${OPENCV_3RDPARTY_EXCLUDE_FROM_ALL} ${lib_srcs} ${lib_hdrs})
  257. target_link_libraries(${PNG_LIBRARY} ${ZLIB_LIBRARIES})
  258. ocv_warnings_disable(CMAKE_C_FLAGS -Wundef -Wcast-align -Wimplicit-fallthrough -Wunused-parameter -Wsign-compare
  259. -Wmaybe-uninitialized
  260. -Wnull-pointer-subtraction # clang15
  261. -Wunused-but-set-variable # clang15
  262. )
  263. set_target_properties(${PNG_LIBRARY}
  264. PROPERTIES OUTPUT_NAME ${PNG_LIBRARY}
  265. DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
  266. COMPILE_PDB_NAME ${PNG_LIBRARY}
  267. COMPILE_PDB_NAME_DEBUG "${PNG_LIBRARY}${OPENCV_DEBUG_POSTFIX}"
  268. ARCHIVE_OUTPUT_DIRECTORY ${3P_LIBRARY_OUTPUT_PATH}
  269. )
  270. if(ENABLE_SOLUTION_FOLDERS)
  271. set_target_properties(${PNG_LIBRARY} PROPERTIES FOLDER "3rdparty")
  272. endif()
  273. if(NOT BUILD_SHARED_LIBS)
  274. ocv_install_target(${PNG_LIBRARY} EXPORT OpenCVModules ARCHIVE DESTINATION ${OPENCV_3P_LIB_INSTALL_PATH} COMPONENT dev OPTIONAL)
  275. endif()
  276. ocv_install_3rdparty_licenses(libpng LICENSE README)