CMakeLists.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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" "${CMAKE_CURRENT_BINARY_DIR}/curl_config.h")
  28. # Get 'CSOURCES', 'HHEADERS' variables
  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(
  36. "${CMAKE_CURRENT_BINARY_DIR}"
  37. "${CMAKE_CURRENT_SOURCE_DIR}"
  38. )
  39. if(USE_ARES)
  40. include_directories(${CARES_INCLUDE_DIRS})
  41. endif()
  42. if(BUILD_TESTING)
  43. add_library(
  44. curlu # special libcurlu library just for unittests
  45. STATIC
  46. EXCLUDE_FROM_ALL
  47. ${HHEADERS} ${CSOURCES}
  48. )
  49. target_compile_definitions(curlu PUBLIC "UNITTESTS" "CURL_STATICLIB")
  50. target_link_libraries(curlu PRIVATE ${CURL_LIBS})
  51. endif()
  52. if(ENABLE_CURLDEBUG)
  53. # We must compile these sources separately to avoid memdebug.h redefinitions
  54. # applying to them.
  55. set_source_files_properties("memdebug.c" "curl_multibyte.c" PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON)
  56. endif()
  57. ## Library definition
  58. # Add "_imp" as a suffix before the extension to avoid conflicting with
  59. # the statically linked "libcurl.lib" (typically with MSVC)
  60. if(WIN32 AND
  61. NOT IMPORT_LIB_SUFFIX AND
  62. CMAKE_STATIC_LIBRARY_SUFFIX STREQUAL CMAKE_IMPORT_LIBRARY_SUFFIX)
  63. set(IMPORT_LIB_SUFFIX "_imp")
  64. endif()
  65. # Whether to do a single compilation pass for libcurl sources and reuse these
  66. # objects to generate both static and shared target.
  67. if(NOT DEFINED SHARE_LIB_OBJECT)
  68. # Enable it by default on platforms where PIC is the default for both shared
  69. # and static and there is a way to tell the linker which libcurl symbols it
  70. # should export (vs. marking these symbols exportable at compile-time).
  71. if(WIN32)
  72. set(SHARE_LIB_OBJECT ON)
  73. else()
  74. # On other platforms, make it an option disabled by default
  75. set(SHARE_LIB_OBJECT OFF)
  76. endif()
  77. endif()
  78. if(SHARE_LIB_OBJECT)
  79. set(LIB_OBJECT "libcurl_object")
  80. add_library(${LIB_OBJECT} OBJECT ${HHEADERS} ${CSOURCES})
  81. if(WIN32)
  82. # Define CURL_STATICLIB always, to disable __declspec(dllexport) for
  83. # exported libcurl symbols. We handle exports via libcurl.def instead.
  84. # Except with symbol hiding disabled or debug mode enabled, when we export
  85. # _all_ symbols from libcurl DLL, without using libcurl.def.
  86. set_property(TARGET ${LIB_OBJECT} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_STATICLIB")
  87. endif()
  88. target_link_libraries(${LIB_OBJECT} PRIVATE ${CURL_LIBS})
  89. set_target_properties(${LIB_OBJECT} PROPERTIES
  90. POSITION_INDEPENDENT_CODE ON)
  91. if(CURL_HIDES_PRIVATE_SYMBOLS)
  92. set_property(TARGET ${LIB_OBJECT} APPEND PROPERTY COMPILE_FLAGS "${CURL_CFLAG_SYMBOLS_HIDE}")
  93. set_property(TARGET ${LIB_OBJECT} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_HIDDEN_SYMBOLS")
  94. endif()
  95. if(CURL_HAS_LTO)
  96. set_target_properties(${LIB_OBJECT} PROPERTIES
  97. INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
  98. INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
  99. endif()
  100. target_include_directories(${LIB_OBJECT} INTERFACE
  101. "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
  102. "$<BUILD_INTERFACE:${CURL_SOURCE_DIR}/include>")
  103. set(LIB_SOURCE $<TARGET_OBJECTS:${LIB_OBJECT}>)
  104. else()
  105. set(LIB_SOURCE ${HHEADERS} ${CSOURCES})
  106. endif()
  107. # We want it to be called libcurl on all platforms
  108. if(BUILD_STATIC_LIBS)
  109. list(APPEND libcurl_export ${LIB_STATIC})
  110. add_library(${LIB_STATIC} STATIC ${LIB_SOURCE})
  111. add_library(${PROJECT_NAME}::${LIB_STATIC} ALIAS ${LIB_STATIC})
  112. if(WIN32)
  113. set_property(TARGET ${LIB_OBJECT} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_STATICLIB")
  114. endif()
  115. target_link_libraries(${LIB_STATIC} PRIVATE ${CURL_LIBS})
  116. # Remove the "lib" prefix since the library is already named "libcurl".
  117. set_target_properties(${LIB_STATIC} PROPERTIES
  118. PREFIX "" OUTPUT_NAME "${LIBCURL_OUTPUT_NAME}"
  119. SUFFIX "${STATIC_LIB_SUFFIX}${CMAKE_STATIC_LIBRARY_SUFFIX}"
  120. INTERFACE_COMPILE_DEFINITIONS "CURL_STATICLIB")
  121. if(CURL_HIDES_PRIVATE_SYMBOLS)
  122. set_property(TARGET ${LIB_STATIC} APPEND PROPERTY COMPILE_FLAGS "${CURL_CFLAG_SYMBOLS_HIDE}")
  123. set_property(TARGET ${LIB_STATIC} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_HIDDEN_SYMBOLS")
  124. endif()
  125. if(CURL_HAS_LTO)
  126. set_target_properties(${LIB_STATIC} PROPERTIES
  127. INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
  128. INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
  129. endif()
  130. target_include_directories(${LIB_STATIC} INTERFACE
  131. "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
  132. "$<BUILD_INTERFACE:${CURL_SOURCE_DIR}/include>")
  133. endif()
  134. if(BUILD_SHARED_LIBS)
  135. list(APPEND libcurl_export ${LIB_SHARED})
  136. add_library(${LIB_SHARED} SHARED ${LIB_SOURCE})
  137. add_library(${PROJECT_NAME}::${LIB_SHARED} ALIAS ${LIB_SHARED})
  138. if(WIN32 OR CYGWIN)
  139. if(CYGWIN)
  140. # For Cygwin always compile dllmain.c as a separate unit since it
  141. # includes windows.h, which should not be included in other units.
  142. set_source_files_properties("dllmain.c" PROPERTIES
  143. SKIP_UNITY_BUILD_INCLUSION ON)
  144. endif()
  145. set_property(TARGET ${LIB_SHARED} APPEND PROPERTY SOURCES "dllmain.c")
  146. endif()
  147. if(WIN32)
  148. set_property(TARGET ${LIB_SHARED} APPEND PROPERTY SOURCES "libcurl.rc")
  149. if(CURL_HIDES_PRIVATE_SYMBOLS)
  150. set_property(TARGET ${LIB_SHARED} APPEND PROPERTY SOURCES "${CURL_SOURCE_DIR}/lib/libcurl.def")
  151. endif()
  152. endif()
  153. target_link_libraries(${LIB_SHARED} PRIVATE ${CURL_LIBS})
  154. # Remove the "lib" prefix since the library is already named "libcurl".
  155. set_target_properties(${LIB_SHARED} PROPERTIES
  156. PREFIX "" OUTPUT_NAME "${LIBCURL_OUTPUT_NAME}"
  157. IMPORT_PREFIX "" IMPORT_SUFFIX "${IMPORT_LIB_SUFFIX}${CMAKE_IMPORT_LIBRARY_SUFFIX}"
  158. POSITION_INDEPENDENT_CODE ON)
  159. if(CURL_HIDES_PRIVATE_SYMBOLS)
  160. set_property(TARGET ${LIB_SHARED} APPEND PROPERTY COMPILE_FLAGS "${CURL_CFLAG_SYMBOLS_HIDE}")
  161. set_property(TARGET ${LIB_SHARED} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_HIDDEN_SYMBOLS")
  162. endif()
  163. if(CURL_HAS_LTO)
  164. set_target_properties(${LIB_SHARED} PROPERTIES
  165. INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
  166. INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
  167. endif()
  168. target_include_directories(${LIB_SHARED} INTERFACE
  169. "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
  170. "$<BUILD_INTERFACE:${CURL_SOURCE_DIR}/include>")
  171. if(CMAKE_DLL_NAME_WITH_SOVERSION OR
  172. CYGWIN OR
  173. APPLE OR
  174. CMAKE_SYSTEM_NAME STREQUAL "AIX" OR
  175. CMAKE_SYSTEM_NAME STREQUAL "Linux" OR
  176. CMAKE_SYSTEM_NAME STREQUAL "SunOS" OR
  177. CMAKE_SYSTEM_NAME STREQUAL "Haiku" OR
  178. CMAKE_SYSTEM_NAME STREQUAL "GNU/kFreeBSD" OR
  179. # FreeBSD comes with the a.out and ELF flavours but a.out was supported
  180. # up to v3.x and ELF from v3.x. I cannot imagine someone running CMake
  181. # on those ancient systems.
  182. CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
  183. set(_soversion_default TRUE)
  184. else()
  185. set(_soversion_default FALSE)
  186. endif()
  187. option(CURL_LIBCURL_SOVERSION "Enable libcurl SOVERSION" ${_soversion_default})
  188. option(CURL_LIBCURL_VERSIONED_SYMBOLS "Enable libcurl versioned symbols" OFF)
  189. if(CURL_LIBCURL_SOVERSION OR CURL_LIBCURL_VERSIONED_SYMBOLS)
  190. # Get 'VERSIONCHANGE', 'VERSIONADD', 'VERSIONDEL', 'VERSIONINFO' variables
  191. transform_makefile_inc("Makefile.soname" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.soname.cmake")
  192. include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.soname.cmake")
  193. math(EXPR _cmakesoname "${VERSIONCHANGE} - ${VERSIONDEL}")
  194. set(_cmakeversion "${_cmakesoname}.${VERSIONDEL}.${VERSIONADD}")
  195. endif()
  196. if(CURL_LIBCURL_SOVERSION)
  197. set_target_properties(${LIB_SHARED} PROPERTIES
  198. VERSION "${_cmakeversion}" SOVERSION "${_cmakesoname}")
  199. endif()
  200. ## Versioned symbols
  201. if(CURL_LIBCURL_VERSIONED_SYMBOLS)
  202. if(NOT DEFINED CURL_LIBCURL_VERSIONED_SYMBOLS_PREFIX)
  203. # Default to prefixes used by autotools
  204. if(CURL_WITH_MULTI_SSL)
  205. set(CURL_LIBCURL_VERSIONED_SYMBOLS_PREFIX "MULTISSL_")
  206. elseif(CURL_USE_OPENSSL)
  207. set(CURL_LIBCURL_VERSIONED_SYMBOLS_PREFIX "OPENSSL_")
  208. elseif(CURL_USE_MBEDTLS)
  209. set(CURL_LIBCURL_VERSIONED_SYMBOLS_PREFIX "MBEDTLS_")
  210. elseif(CURL_USE_BEARSSL)
  211. set(CURL_LIBCURL_VERSIONED_SYMBOLS_PREFIX "BEARSSL_")
  212. elseif(CURL_USE_WOLFSSL)
  213. set(CURL_LIBCURL_VERSIONED_SYMBOLS_PREFIX "WOLFSSL_")
  214. elseif(CURL_USE_GNUTLS)
  215. set(CURL_LIBCURL_VERSIONED_SYMBOLS_PREFIX "GNUTLS_")
  216. endif()
  217. endif()
  218. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/libcurl.vers" "
  219. HIDDEN {};
  220. CURL_${CURL_LIBCURL_VERSIONED_SYMBOLS_PREFIX}${_cmakesoname}
  221. {
  222. global: curl_*;
  223. local: *;
  224. };")
  225. include(CheckCSourceCompiles)
  226. set(CMAKE_REQUIRED_LINK_OPTIONS "-Wl,--version-script=${CMAKE_CURRENT_BINARY_DIR}/libcurl.vers")
  227. check_c_source_compiles("int main(void) { return 0; }" HAVE_VERSIONED_SYMBOLS)
  228. if(HAVE_VERSIONED_SYMBOLS)
  229. # Superseded by LINK_OPTIONS in CMake 3.13 and later.
  230. set_target_properties(${LIB_SHARED} PROPERTIES LINK_FLAGS "${CMAKE_REQUIRED_LINK_OPTIONS}")
  231. else()
  232. message(WARNING "Versioned symbols requested, but not supported by the toolchain.")
  233. endif()
  234. unset(CMAKE_REQUIRED_LINK_OPTIONS)
  235. endif()
  236. endif()
  237. add_library(${LIB_NAME} ALIAS ${LIB_SELECTED})
  238. add_library(${PROJECT_NAME}::${LIB_NAME} ALIAS ${LIB_SELECTED})
  239. if(CURL_ENABLE_EXPORT_TARGET)
  240. if(BUILD_STATIC_LIBS)
  241. install(TARGETS ${LIB_STATIC}
  242. EXPORT ${TARGETS_EXPORT_NAME}
  243. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  244. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  245. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  246. )
  247. endif()
  248. if(BUILD_SHARED_LIBS)
  249. install(TARGETS ${LIB_SHARED}
  250. EXPORT ${TARGETS_EXPORT_NAME}
  251. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  252. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  253. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  254. )
  255. endif()
  256. export(TARGETS ${libcurl_export}
  257. FILE "${PROJECT_BINARY_DIR}/libcurl-target.cmake"
  258. NAMESPACE ${PROJECT_NAME}::
  259. )
  260. endif()