2
0

CMakeLists.txt 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #***************************************************************************
  2. # _ _ ____ _
  3. # Project ___| | | | _ \| |
  4. # / __| | | | |_) | |
  5. # | (__| |_| | _ <| |___
  6. # \___|\___/|_| \_\_____|
  7. #
  8. # Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. #
  10. # This software is licensed as described in the file COPYING, which
  11. # you should have received as part of this distribution. The terms
  12. # are also available at https://curl.se/docs/copyright.html.
  13. #
  14. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. # copies of the Software, and permit persons to whom the Software is
  16. # furnished to do so, under the terms of the COPYING file.
  17. #
  18. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. # KIND, either express or implied.
  20. #
  21. # SPDX-License-Identifier: curl
  22. #
  23. ###########################################################################
  24. set(LIB_NAME libcurl)
  25. set(LIBCURL_OUTPUT_NAME libcurl CACHE STRING "Basename of the curl library")
  26. add_definitions(-DBUILDING_LIBCURL)
  27. configure_file(curl_config.h.cmake
  28. ${CMAKE_CURRENT_BINARY_DIR}/curl_config.h)
  29. transform_makefile_inc("Makefile.inc" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
  30. include(${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake)
  31. # DllMain is added later for DLL builds only.
  32. list(REMOVE_ITEM CSOURCES dllmain.c)
  33. list(APPEND HHEADERS ${CMAKE_CURRENT_BINARY_DIR}/curl_config.h)
  34. # The rest of the build
  35. include_directories(${CMAKE_CURRENT_BINARY_DIR}/../include)
  36. include_directories(${CMAKE_CURRENT_SOURCE_DIR}/..)
  37. include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include)
  38. include_directories(${CMAKE_CURRENT_BINARY_DIR}/..)
  39. include_directories(${CMAKE_CURRENT_SOURCE_DIR})
  40. include_directories(${CMAKE_CURRENT_BINARY_DIR})
  41. if(USE_ARES)
  42. include_directories(${CARES_INCLUDE_DIR})
  43. endif()
  44. if(BUILD_TESTING)
  45. add_library(
  46. curlu # special libcurlu library just for unittests
  47. STATIC
  48. EXCLUDE_FROM_ALL
  49. ${HHEADERS} ${CSOURCES}
  50. )
  51. target_compile_definitions(curlu PUBLIC UNITTESTS CURL_STATICLIB)
  52. target_link_libraries(curlu PRIVATE ${CURL_LIBS})
  53. endif()
  54. if(ENABLE_CURLDEBUG)
  55. # We must compile these sources separately to avoid memdebug.h redefinitions
  56. # applying to them.
  57. set_source_files_properties(memdebug.c curl_multibyte.c PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON)
  58. endif()
  59. ## Library definition
  60. # Add "_imp" as a suffix before the extension to avoid conflicting with
  61. # the statically linked "libcurl.lib" (typically with MSVC)
  62. if(WIN32 AND
  63. NOT IMPORT_LIB_SUFFIX AND
  64. CMAKE_STATIC_LIBRARY_SUFFIX STREQUAL CMAKE_IMPORT_LIBRARY_SUFFIX)
  65. set(IMPORT_LIB_SUFFIX "_imp")
  66. endif()
  67. # Whether to do a single compilation pass for libcurl sources and reuse these
  68. # objects to generate both static and shared target.
  69. if(NOT DEFINED SHARE_LIB_OBJECT)
  70. # Enable it by default on platforms where PIC is the default for both shared
  71. # and static and there is a way to tell the linker which libcurl symbols it
  72. # should export (vs. marking these symbols exportable at compile-time).
  73. if(WIN32)
  74. set(SHARE_LIB_OBJECT ON)
  75. else()
  76. # On other platforms, make it an option disabled by default
  77. set(SHARE_LIB_OBJECT OFF)
  78. endif()
  79. endif()
  80. if(SHARE_LIB_OBJECT)
  81. set(LIB_OBJECT "libcurl_object")
  82. add_library(${LIB_OBJECT} OBJECT ${HHEADERS} ${CSOURCES})
  83. if(WIN32)
  84. # Define CURL_STATICLIB always, to disable __declspec(dllexport) for
  85. # exported libcurl symbols. We handle exports via libcurl.def instead.
  86. # Except with symbol hiding disabled or debug mode enabled, when we export
  87. # _all_ symbols from libcurl DLL, without using libcurl.def.
  88. set_property(TARGET ${LIB_OBJECT} APPEND
  89. PROPERTY COMPILE_DEFINITIONS "CURL_STATICLIB")
  90. endif()
  91. target_link_libraries(${LIB_OBJECT} PRIVATE ${CURL_LIBS})
  92. set_target_properties(${LIB_OBJECT} PROPERTIES
  93. POSITION_INDEPENDENT_CODE ON)
  94. if(HIDES_CURL_PRIVATE_SYMBOLS)
  95. set_property(TARGET ${LIB_OBJECT} APPEND PROPERTY COMPILE_FLAGS "${CURL_CFLAG_SYMBOLS_HIDE}")
  96. set_property(TARGET ${LIB_OBJECT} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_HIDDEN_SYMBOLS")
  97. endif()
  98. if(CURL_HAS_LTO)
  99. set_target_properties(${LIB_OBJECT} PROPERTIES
  100. INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
  101. INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
  102. endif()
  103. target_include_directories(${LIB_OBJECT} INTERFACE
  104. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  105. $<BUILD_INTERFACE:${CURL_SOURCE_DIR}/include>)
  106. set(LIB_SOURCE $<TARGET_OBJECTS:${LIB_OBJECT}>)
  107. else()
  108. set(LIB_SOURCE ${HHEADERS} ${CSOURCES})
  109. endif()
  110. # we want it to be called libcurl on all platforms
  111. if(BUILD_STATIC_LIBS)
  112. list(APPEND libcurl_export ${LIB_STATIC})
  113. add_library(${LIB_STATIC} STATIC ${LIB_SOURCE})
  114. add_library(${PROJECT_NAME}::${LIB_STATIC} ALIAS ${LIB_STATIC})
  115. if(WIN32)
  116. set_property(TARGET ${LIB_OBJECT} APPEND
  117. PROPERTY COMPILE_DEFINITIONS "CURL_STATICLIB")
  118. endif()
  119. target_link_libraries(${LIB_STATIC} PRIVATE ${CURL_LIBS})
  120. # Remove the "lib" prefix since the library is already named "libcurl".
  121. set_target_properties(${LIB_STATIC} PROPERTIES
  122. PREFIX "" OUTPUT_NAME "${LIBCURL_OUTPUT_NAME}"
  123. SUFFIX "${STATIC_LIB_SUFFIX}${CMAKE_STATIC_LIBRARY_SUFFIX}"
  124. INTERFACE_COMPILE_DEFINITIONS "CURL_STATICLIB")
  125. if(HIDES_CURL_PRIVATE_SYMBOLS)
  126. set_property(TARGET ${LIB_STATIC} APPEND PROPERTY COMPILE_FLAGS "${CURL_CFLAG_SYMBOLS_HIDE}")
  127. set_property(TARGET ${LIB_STATIC} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_HIDDEN_SYMBOLS")
  128. endif()
  129. if(CURL_HAS_LTO)
  130. set_target_properties(${LIB_STATIC} PROPERTIES
  131. INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
  132. INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
  133. endif()
  134. target_include_directories(${LIB_STATIC} INTERFACE
  135. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  136. $<BUILD_INTERFACE:${CURL_SOURCE_DIR}/include>)
  137. endif()
  138. if(BUILD_SHARED_LIBS)
  139. list(APPEND libcurl_export ${LIB_SHARED})
  140. add_library(${LIB_SHARED} SHARED ${LIB_SOURCE})
  141. add_library(${PROJECT_NAME}::${LIB_SHARED} ALIAS ${LIB_SHARED})
  142. if(WIN32 OR CYGWIN)
  143. if(CYGWIN)
  144. # For Cygwin always compile dllmain.c as a separate unit since it
  145. # includes windows.h, which should not be included in other units.
  146. set_source_files_properties(dllmain.c PROPERTIES
  147. SKIP_UNITY_BUILD_INCLUSION ON)
  148. endif()
  149. set_property(TARGET ${LIB_SHARED} APPEND PROPERTY SOURCES dllmain.c)
  150. endif()
  151. if(WIN32)
  152. set_property(TARGET ${LIB_SHARED} APPEND PROPERTY SOURCES libcurl.rc)
  153. if(HIDES_CURL_PRIVATE_SYMBOLS)
  154. set_property(TARGET ${LIB_SHARED} APPEND PROPERTY SOURCES "${CURL_SOURCE_DIR}/libcurl.def")
  155. endif()
  156. endif()
  157. target_link_libraries(${LIB_SHARED} PRIVATE ${CURL_LIBS})
  158. # Remove the "lib" prefix since the library is already named "libcurl".
  159. set_target_properties(${LIB_SHARED} PROPERTIES
  160. PREFIX "" OUTPUT_NAME "${LIBCURL_OUTPUT_NAME}"
  161. IMPORT_PREFIX "" IMPORT_SUFFIX "${IMPORT_LIB_SUFFIX}${CMAKE_IMPORT_LIBRARY_SUFFIX}"
  162. POSITION_INDEPENDENT_CODE ON)
  163. if(HIDES_CURL_PRIVATE_SYMBOLS)
  164. set_property(TARGET ${LIB_SHARED} APPEND PROPERTY COMPILE_FLAGS "${CURL_CFLAG_SYMBOLS_HIDE}")
  165. set_property(TARGET ${LIB_SHARED} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_HIDDEN_SYMBOLS")
  166. endif()
  167. if(CURL_HAS_LTO)
  168. set_target_properties(${LIB_SHARED} PROPERTIES
  169. INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
  170. INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
  171. endif()
  172. target_include_directories(${LIB_SHARED} INTERFACE
  173. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  174. $<BUILD_INTERFACE:${CURL_SOURCE_DIR}/include>)
  175. if(CMAKE_DLL_NAME_WITH_SOVERSION OR
  176. CYGWIN OR
  177. APPLE OR
  178. CMAKE_SYSTEM_NAME STREQUAL "AIX" OR
  179. CMAKE_SYSTEM_NAME STREQUAL "Linux" OR
  180. CMAKE_SYSTEM_NAME STREQUAL "SunOS" OR
  181. CMAKE_SYSTEM_NAME STREQUAL "Haiku" OR
  182. CMAKE_SYSTEM_NAME STREQUAL "GNU/kFreeBSD" OR
  183. # FreeBSD comes with the a.out and ELF flavours but a.out was supported
  184. # up to v3.x and ELF from v3.x. I cannot imagine someone running CMake
  185. # on those ancient systems.
  186. CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
  187. set(soversion_default TRUE)
  188. else()
  189. set(soversion_default FALSE)
  190. endif()
  191. option(CURL_LIBCURL_SOVERSION "Enable libcurl SOVERSION" ${soversion_default})
  192. if(CURL_LIBCURL_SOVERSION)
  193. transform_makefile_inc("Makefile.soname" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.soname.cmake")
  194. include(${CMAKE_CURRENT_BINARY_DIR}/Makefile.soname.cmake)
  195. math(EXPR CMAKESONAME "${VERSIONCHANGE} - ${VERSIONDEL}")
  196. set(CMAKEVERSION "${CMAKESONAME}.${VERSIONDEL}.${VERSIONADD}")
  197. set_target_properties(${LIB_SHARED} PROPERTIES
  198. VERSION "${CMAKEVERSION}" SOVERSION "${CMAKESONAME}")
  199. endif()
  200. endif()
  201. add_library(${LIB_NAME} ALIAS ${LIB_SELECTED})
  202. add_library(${PROJECT_NAME}::${LIB_NAME} ALIAS ${LIB_SELECTED})
  203. if(CURL_ENABLE_EXPORT_TARGET)
  204. if(BUILD_STATIC_LIBS)
  205. install(TARGETS ${LIB_STATIC}
  206. EXPORT ${TARGETS_EXPORT_NAME}
  207. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  208. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  209. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  210. )
  211. endif()
  212. if(BUILD_SHARED_LIBS)
  213. install(TARGETS ${LIB_SHARED}
  214. EXPORT ${TARGETS_EXPORT_NAME}
  215. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  216. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  217. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  218. )
  219. endif()
  220. export(TARGETS ${libcurl_export}
  221. FILE ${PROJECT_BINARY_DIR}/libcurl-target.cmake
  222. NAMESPACE ${PROJECT_NAME}::
  223. )
  224. endif()