CMakeLists.txt 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. endif()
  53. if(ENABLE_CURLDEBUG)
  54. # We must compile these sources separately to avoid memdebug.h redefinitions
  55. # applying to them.
  56. set_source_files_properties(memdebug.c curl_multibyte.c PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON)
  57. endif()
  58. if(BUILD_TESTING)
  59. target_link_libraries(curlu PRIVATE ${CURL_LIBS})
  60. endif()
  61. transform_makefile_inc("Makefile.soname" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.soname.cmake")
  62. include(${CMAKE_CURRENT_BINARY_DIR}/Makefile.soname.cmake)
  63. if(CMAKE_SYSTEM_NAME STREQUAL "AIX" OR
  64. CMAKE_SYSTEM_NAME STREQUAL "Linux" OR
  65. CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR
  66. CMAKE_SYSTEM_NAME STREQUAL "SunOS" OR
  67. CMAKE_SYSTEM_NAME STREQUAL "GNU/kFreeBSD" OR
  68. # FreeBSD comes with the a.out and elf flavours
  69. # but a.out was supported up to version 3.x and
  70. # elf from 3.x. I cannot imagine someone running
  71. # CMake on those ancient systems
  72. CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR
  73. CMAKE_SYSTEM_NAME STREQUAL "Haiku")
  74. math(EXPR CMAKESONAME "${VERSIONCHANGE} - ${VERSIONDEL}")
  75. set(CMAKEVERSION "${CMAKESONAME}.${VERSIONDEL}.${VERSIONADD}")
  76. else()
  77. unset(CMAKESONAME)
  78. endif()
  79. ## Library definition
  80. # Add "_imp" as a suffix before the extension to avoid conflicting with
  81. # the statically linked "libcurl.lib" (typically with MSVC)
  82. if(WIN32 AND
  83. NOT IMPORT_LIB_SUFFIX AND
  84. CMAKE_STATIC_LIBRARY_SUFFIX STREQUAL CMAKE_IMPORT_LIBRARY_SUFFIX)
  85. set(IMPORT_LIB_SUFFIX "_imp")
  86. endif()
  87. # Whether to do a single compilation pass for libcurl sources and reuse these
  88. # objects to generate both static and shared target.
  89. if(NOT DEFINED SHARE_LIB_OBJECT)
  90. # Enable it by default on platforms where PIC is the default for both shared
  91. # and static and there is a way to tell the linker which libcurl symbols it
  92. # should export (vs. marking these symbols exportable at compile-time).
  93. if(WIN32)
  94. set(SHARE_LIB_OBJECT ON)
  95. else()
  96. # On other platforms, make it an option disabled by default
  97. set(SHARE_LIB_OBJECT OFF)
  98. endif()
  99. endif()
  100. if(SHARE_LIB_OBJECT)
  101. set(LIB_OBJECT "libcurl_object")
  102. add_library(${LIB_OBJECT} OBJECT ${HHEADERS} ${CSOURCES})
  103. if(WIN32)
  104. # Define CURL_STATICLIB always, to disable __declspec(dllexport) for
  105. # exported libcurl symbols. We handle exports via libcurl.def instead.
  106. # Except with symbol hiding disabled or debug mode enabled, when we export
  107. # _all_ symbols from libcurl DLL, without using libcurl.def.
  108. set_property(TARGET ${LIB_OBJECT} APPEND
  109. PROPERTY COMPILE_DEFINITIONS "CURL_STATICLIB")
  110. endif()
  111. target_link_libraries(${LIB_OBJECT} PRIVATE ${CURL_LIBS})
  112. set_target_properties(${LIB_OBJECT} PROPERTIES
  113. POSITION_INDEPENDENT_CODE ON)
  114. if(HIDES_CURL_PRIVATE_SYMBOLS)
  115. set_property(TARGET ${LIB_OBJECT} APPEND PROPERTY COMPILE_FLAGS "${CURL_CFLAG_SYMBOLS_HIDE}")
  116. set_property(TARGET ${LIB_OBJECT} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_HIDDEN_SYMBOLS")
  117. endif()
  118. if(CURL_HAS_LTO)
  119. set_target_properties(${LIB_OBJECT} PROPERTIES
  120. INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
  121. INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
  122. endif()
  123. target_include_directories(${LIB_OBJECT} INTERFACE
  124. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  125. $<BUILD_INTERFACE:${CURL_SOURCE_DIR}/include>)
  126. set(LIB_SOURCE $<TARGET_OBJECTS:${LIB_OBJECT}>)
  127. else()
  128. set(LIB_SOURCE ${HHEADERS} ${CSOURCES})
  129. endif()
  130. # we want it to be called libcurl on all platforms
  131. if(BUILD_STATIC_LIBS)
  132. list(APPEND libcurl_export ${LIB_STATIC})
  133. add_library(${LIB_STATIC} STATIC ${LIB_SOURCE})
  134. add_library(${PROJECT_NAME}::${LIB_STATIC} ALIAS ${LIB_STATIC})
  135. if(WIN32)
  136. set_property(TARGET ${LIB_OBJECT} APPEND
  137. PROPERTY COMPILE_DEFINITIONS "CURL_STATICLIB")
  138. endif()
  139. target_link_libraries(${LIB_STATIC} PRIVATE ${CURL_LIBS})
  140. # Remove the "lib" prefix since the library is already named "libcurl".
  141. set_target_properties(${LIB_STATIC} PROPERTIES
  142. PREFIX "" OUTPUT_NAME "${LIBCURL_OUTPUT_NAME}"
  143. SUFFIX "${STATIC_LIB_SUFFIX}${CMAKE_STATIC_LIBRARY_SUFFIX}"
  144. INTERFACE_COMPILE_DEFINITIONS "CURL_STATICLIB")
  145. if(HIDES_CURL_PRIVATE_SYMBOLS)
  146. set_property(TARGET ${LIB_STATIC} APPEND PROPERTY COMPILE_FLAGS "${CURL_CFLAG_SYMBOLS_HIDE}")
  147. set_property(TARGET ${LIB_STATIC} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_HIDDEN_SYMBOLS")
  148. endif()
  149. if(CURL_HAS_LTO)
  150. set_target_properties(${LIB_STATIC} PROPERTIES
  151. INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
  152. INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
  153. endif()
  154. if(CMAKEVERSION AND CMAKESONAME)
  155. set_target_properties(${LIB_STATIC} PROPERTIES
  156. VERSION ${CMAKEVERSION} SOVERSION ${CMAKESONAME})
  157. endif()
  158. target_include_directories(${LIB_STATIC} INTERFACE
  159. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  160. $<BUILD_INTERFACE:${CURL_SOURCE_DIR}/include>)
  161. endif()
  162. if(BUILD_SHARED_LIBS)
  163. list(APPEND libcurl_export ${LIB_SHARED})
  164. add_library(${LIB_SHARED} SHARED ${LIB_SOURCE})
  165. add_library(${PROJECT_NAME}::${LIB_SHARED} ALIAS ${LIB_SHARED})
  166. if(WIN32 OR CYGWIN)
  167. if(CYGWIN)
  168. # For cygwin always compile dllmain.c as a separate unit since it
  169. # includes windows.h, which shouldn't be included in other units.
  170. set_source_files_properties(dllmain.c PROPERTIES
  171. SKIP_UNITY_BUILD_INCLUSION ON)
  172. endif()
  173. set_property(TARGET ${LIB_SHARED} APPEND PROPERTY SOURCES dllmain.c)
  174. endif()
  175. if(WIN32)
  176. set_property(TARGET ${LIB_SHARED} APPEND PROPERTY SOURCES libcurl.rc)
  177. if(HIDES_CURL_PRIVATE_SYMBOLS)
  178. set_property(TARGET ${LIB_SHARED} APPEND PROPERTY SOURCES "${CURL_SOURCE_DIR}/libcurl.def")
  179. endif()
  180. endif()
  181. target_link_libraries(${LIB_SHARED} PRIVATE ${CURL_LIBS})
  182. # Remove the "lib" prefix since the library is already named "libcurl".
  183. set_target_properties(${LIB_SHARED} PROPERTIES
  184. PREFIX "" OUTPUT_NAME "${LIBCURL_OUTPUT_NAME}"
  185. IMPORT_PREFIX "" IMPORT_SUFFIX "${IMPORT_LIB_SUFFIX}${CMAKE_IMPORT_LIBRARY_SUFFIX}"
  186. POSITION_INDEPENDENT_CODE ON)
  187. if(HIDES_CURL_PRIVATE_SYMBOLS)
  188. set_property(TARGET ${LIB_SHARED} APPEND PROPERTY COMPILE_FLAGS "${CURL_CFLAG_SYMBOLS_HIDE}")
  189. set_property(TARGET ${LIB_SHARED} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_HIDDEN_SYMBOLS")
  190. endif()
  191. if(CURL_HAS_LTO)
  192. set_target_properties(${LIB_SHARED} PROPERTIES
  193. INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
  194. INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
  195. endif()
  196. if(CMAKEVERSION AND CMAKESONAME)
  197. set_target_properties(${LIB_SHARED} PROPERTIES
  198. VERSION ${CMAKEVERSION} SOVERSION ${CMAKESONAME})
  199. endif()
  200. target_include_directories(${LIB_SHARED} INTERFACE
  201. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  202. $<BUILD_INTERFACE:${CURL_SOURCE_DIR}/include>)
  203. endif()
  204. add_library(${LIB_NAME} ALIAS ${LIB_SELECTED})
  205. add_library(${PROJECT_NAME}::${LIB_NAME} ALIAS ${LIB_SELECTED})
  206. if(CURL_ENABLE_EXPORT_TARGET)
  207. if(BUILD_STATIC_LIBS)
  208. install(TARGETS ${LIB_STATIC}
  209. EXPORT ${TARGETS_EXPORT_NAME}
  210. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  211. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  212. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  213. )
  214. endif()
  215. if(BUILD_SHARED_LIBS)
  216. install(TARGETS ${LIB_SHARED}
  217. EXPORT ${TARGETS_EXPORT_NAME}
  218. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  219. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  220. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  221. )
  222. endif()
  223. export(TARGETS ${libcurl_export}
  224. FILE ${PROJECT_BINARY_DIR}/libcurl-target.cmake
  225. NAMESPACE ${PROJECT_NAME}::
  226. )
  227. endif()