2
0

CMakeLists.txt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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(SYSTEM ${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 SKIP_UNITY_BUILD_INCLUSION ON)
  143. endif()
  144. set_property(TARGET ${LIB_SHARED} APPEND PROPERTY SOURCES "dllmain.c")
  145. endif()
  146. if(WIN32)
  147. set_property(TARGET ${LIB_SHARED} APPEND PROPERTY SOURCES "libcurl.rc")
  148. if(CURL_HIDES_PRIVATE_SYMBOLS)
  149. set_property(TARGET ${LIB_SHARED} APPEND PROPERTY SOURCES "${CURL_SOURCE_DIR}/lib/libcurl.def")
  150. endif()
  151. endif()
  152. target_link_libraries(${LIB_SHARED} PRIVATE ${CURL_LIBS})
  153. # Remove the "lib" prefix since the library is already named "libcurl".
  154. set_target_properties(${LIB_SHARED} PROPERTIES
  155. PREFIX "" OUTPUT_NAME "${LIBCURL_OUTPUT_NAME}"
  156. IMPORT_PREFIX "" IMPORT_SUFFIX "${IMPORT_LIB_SUFFIX}${CMAKE_IMPORT_LIBRARY_SUFFIX}"
  157. POSITION_INDEPENDENT_CODE ON)
  158. if(CURL_HIDES_PRIVATE_SYMBOLS)
  159. set_property(TARGET ${LIB_SHARED} APPEND PROPERTY COMPILE_FLAGS "${CURL_CFLAG_SYMBOLS_HIDE}")
  160. set_property(TARGET ${LIB_SHARED} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_HIDDEN_SYMBOLS")
  161. endif()
  162. if(CURL_HAS_LTO)
  163. set_target_properties(${LIB_SHARED} PROPERTIES
  164. INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
  165. INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
  166. endif()
  167. target_include_directories(${LIB_SHARED} INTERFACE
  168. "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
  169. "$<BUILD_INTERFACE:${CURL_SOURCE_DIR}/include>")
  170. if(CMAKE_DLL_NAME_WITH_SOVERSION OR
  171. CYGWIN OR
  172. APPLE OR
  173. CMAKE_SYSTEM_NAME STREQUAL "AIX" OR
  174. CMAKE_SYSTEM_NAME STREQUAL "Linux" OR
  175. CMAKE_SYSTEM_NAME STREQUAL "SunOS" OR
  176. CMAKE_SYSTEM_NAME STREQUAL "Haiku" OR
  177. CMAKE_SYSTEM_NAME STREQUAL "GNU/kFreeBSD" OR
  178. # FreeBSD comes with the a.out and ELF flavours but a.out was supported
  179. # up to v3.x and ELF from v3.x. I cannot imagine someone running CMake
  180. # on those ancient systems.
  181. CMAKE_SYSTEM_NAME STREQUAL "FreeBSD")
  182. set(_soversion_default TRUE)
  183. else()
  184. set(_soversion_default FALSE)
  185. endif()
  186. option(CURL_LIBCURL_SOVERSION "Enable libcurl SOVERSION" ${_soversion_default})
  187. option(CURL_LIBCURL_VERSIONED_SYMBOLS "Enable libcurl versioned symbols" OFF)
  188. if(CURL_LIBCURL_SOVERSION OR CURL_LIBCURL_VERSIONED_SYMBOLS)
  189. # Get 'VERSIONCHANGE', 'VERSIONADD', 'VERSIONDEL', 'VERSIONINFO' variables
  190. transform_makefile_inc("Makefile.soname" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.soname.cmake")
  191. include("${CMAKE_CURRENT_BINARY_DIR}/Makefile.soname.cmake")
  192. math(EXPR _cmakesoname "${VERSIONCHANGE} - ${VERSIONDEL}")
  193. set(_cmakeversion "${_cmakesoname}.${VERSIONDEL}.${VERSIONADD}")
  194. endif()
  195. if(CURL_LIBCURL_SOVERSION)
  196. set_target_properties(${LIB_SHARED} PROPERTIES
  197. VERSION "${_cmakeversion}" SOVERSION "${_cmakesoname}")
  198. endif()
  199. ## Versioned symbols
  200. if(CURL_LIBCURL_VERSIONED_SYMBOLS)
  201. if(NOT DEFINED CURL_LIBCURL_VERSIONED_SYMBOLS_PREFIX)
  202. # Default to prefixes used by autotools
  203. if(CURL_WITH_MULTI_SSL)
  204. set(CURL_LIBCURL_VERSIONED_SYMBOLS_PREFIX "MULTISSL_")
  205. elseif(CURL_USE_OPENSSL)
  206. set(CURL_LIBCURL_VERSIONED_SYMBOLS_PREFIX "OPENSSL_")
  207. elseif(CURL_USE_MBEDTLS)
  208. set(CURL_LIBCURL_VERSIONED_SYMBOLS_PREFIX "MBEDTLS_")
  209. elseif(CURL_USE_BEARSSL)
  210. set(CURL_LIBCURL_VERSIONED_SYMBOLS_PREFIX "BEARSSL_")
  211. elseif(CURL_USE_WOLFSSL)
  212. set(CURL_LIBCURL_VERSIONED_SYMBOLS_PREFIX "WOLFSSL_")
  213. elseif(CURL_USE_GNUTLS)
  214. set(CURL_LIBCURL_VERSIONED_SYMBOLS_PREFIX "GNUTLS_")
  215. elseif(CURL_USE_RUSTLS)
  216. set(CURL_LIBCURL_VERSIONED_SYMBOLS_PREFIX "RUSTLS_")
  217. endif()
  218. endif()
  219. # Generate version script for the linker, for versioned symbols.
  220. # Consumed variables:
  221. # CURL_LIBCURL_VERSIONED_SYMBOLS_PREFIX
  222. # CURL_LIBCURL_VERSIONED_SYMBOLS_SONAME
  223. set(CURL_LIBCURL_VERSIONED_SYMBOLS_SONAME ${_cmakesoname})
  224. configure_file(
  225. "${CMAKE_CURRENT_SOURCE_DIR}/libcurl.vers.in"
  226. "${CMAKE_CURRENT_BINARY_DIR}/libcurl.vers" @ONLY)
  227. include(CheckCSourceCompiles)
  228. set(CMAKE_REQUIRED_LINK_OPTIONS "-Wl,--version-script=${CMAKE_CURRENT_BINARY_DIR}/libcurl.vers")
  229. check_c_source_compiles("int main(void) { return 0; }" HAVE_VERSIONED_SYMBOLS)
  230. if(HAVE_VERSIONED_SYMBOLS)
  231. # Superseded by LINK_OPTIONS in CMake 3.13 and later.
  232. set_target_properties(${LIB_SHARED} PROPERTIES LINK_FLAGS "${CMAKE_REQUIRED_LINK_OPTIONS}")
  233. else()
  234. message(WARNING "Versioned symbols requested, but not supported by the toolchain.")
  235. endif()
  236. unset(CMAKE_REQUIRED_LINK_OPTIONS)
  237. endif()
  238. endif()
  239. add_library(${LIB_NAME} ALIAS ${LIB_SELECTED})
  240. add_library(${PROJECT_NAME}::${LIB_NAME} ALIAS ${LIB_SELECTED})
  241. if(CURL_ENABLE_EXPORT_TARGET)
  242. if(BUILD_STATIC_LIBS)
  243. install(TARGETS ${LIB_STATIC}
  244. EXPORT ${TARGETS_EXPORT_NAME}
  245. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  246. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  247. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  248. )
  249. endif()
  250. if(BUILD_SHARED_LIBS)
  251. install(TARGETS ${LIB_SHARED}
  252. EXPORT ${TARGETS_EXPORT_NAME}
  253. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  254. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  255. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  256. )
  257. endif()
  258. export(TARGETS ${libcurl_export}
  259. FILE "${PROJECT_BINARY_DIR}/libcurl-target.cmake"
  260. NAMESPACE ${PROJECT_NAME}::
  261. )
  262. endif()