CMakeLists.txt 9.4 KB

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