CMakeLists.txt 8.7 KB

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