2
0

CMakeLists.txt 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 memdebug.c separately to avoid memdebug.h redefinitions
  53. # being applied to memdebug.c itself.
  54. set_source_files_properties(memdebug.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. if(NOT WIN32 AND NOT CMAKE_CROSSCOMPILING)
  76. # on not-Windows and not-crosscompiling, check for writable argv[]
  77. include(CheckCSourceRuns)
  78. check_c_source_runs("
  79. int main(int argc, char **argv)
  80. {
  81. (void)argc;
  82. argv[0][0] = ' ';
  83. return (argv[0][0] == ' ')?0:1;
  84. }"
  85. HAVE_WRITABLE_ARGV)
  86. endif()
  87. ## Library definition
  88. # Add "_imp" as a suffix before the extension to avoid conflicting with
  89. # the statically linked "libcurl.lib" (typically with MSVC)
  90. if(WIN32 AND
  91. NOT IMPORT_LIB_SUFFIX AND
  92. CMAKE_STATIC_LIBRARY_SUFFIX STREQUAL CMAKE_IMPORT_LIBRARY_SUFFIX)
  93. set(IMPORT_LIB_SUFFIX "_imp")
  94. endif()
  95. # Whether to do a single compilation pass for libcurl sources and reuse these
  96. # objects to generate both static and shared target.
  97. if(NOT DEFINED SHARE_LIB_OBJECT)
  98. # Enable it by default on platforms where PIC is the default for both shared
  99. # and static and there is a way to tell the linker which libcurl symbols it
  100. # should export (vs. marking these symbols exportable at compile-time).
  101. if(WIN32)
  102. set(SHARE_LIB_OBJECT ON)
  103. else()
  104. # On other platforms, make it an option disabled by default
  105. set(SHARE_LIB_OBJECT OFF)
  106. endif()
  107. endif()
  108. if(SHARE_LIB_OBJECT)
  109. set(LIB_OBJECT "libcurl_object")
  110. add_library(${LIB_OBJECT} OBJECT ${HHEADERS} ${CSOURCES})
  111. target_link_libraries(${LIB_OBJECT} PRIVATE ${CURL_LIBS})
  112. set_target_properties(${LIB_OBJECT} PROPERTIES
  113. COMPILE_DEFINITIONS "BUILDING_LIBCURL"
  114. INTERFACE_COMPILE_DEFINITIONS "CURL_STATICLIB"
  115. POSITION_INDEPENDENT_CODE ON)
  116. if(HIDES_CURL_PRIVATE_SYMBOLS)
  117. set_target_properties(${LIB_OBJECT} PROPERTIES
  118. COMPILE_DEFINITIONS "CURL_HIDDEN_SYMBOLS"
  119. COMPILE_FLAGS "${CURL_CFLAG_SYMBOLS_HIDE}")
  120. endif()
  121. if(CURL_HAS_LTO)
  122. set_target_properties(${LIB_OBJECT} PROPERTIES
  123. INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
  124. INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
  125. endif()
  126. target_include_directories(${LIB_OBJECT} INTERFACE
  127. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  128. $<BUILD_INTERFACE:${CURL_SOURCE_DIR}/include>)
  129. set(LIB_SOURCE $<TARGET_OBJECTS:${LIB_OBJECT}>)
  130. else()
  131. set(LIB_SOURCE ${HHEADERS} ${CSOURCES})
  132. endif()
  133. # we want it to be called libcurl on all platforms
  134. if(BUILD_STATIC_LIBS)
  135. list(APPEND libcurl_export ${LIB_STATIC})
  136. add_library(${LIB_STATIC} STATIC ${LIB_SOURCE})
  137. add_library(${PROJECT_NAME}::${LIB_STATIC} ALIAS ${LIB_STATIC})
  138. target_link_libraries(${LIB_STATIC} PRIVATE ${CURL_LIBS})
  139. # Remove the "lib" prefix since the library is already named "libcurl".
  140. set_target_properties(${LIB_STATIC} PROPERTIES
  141. PREFIX "" OUTPUT_NAME "${LIBCURL_OUTPUT_NAME}"
  142. SUFFIX "${STATIC_LIB_SUFFIX}${CMAKE_STATIC_LIBRARY_SUFFIX}"
  143. COMPILE_DEFINITIONS "BUILDING_LIBCURL"
  144. INTERFACE_COMPILE_DEFINITIONS "CURL_STATICLIB")
  145. if(HIDES_CURL_PRIVATE_SYMBOLS)
  146. set_target_properties(${LIB_STATIC} PROPERTIES
  147. COMPILE_DEFINITIONS "CURL_HIDDEN_SYMBOLS"
  148. COMPILE_FLAGS "${CURL_CFLAG_SYMBOLS_HIDE}")
  149. endif()
  150. if(CURL_HAS_LTO)
  151. set_target_properties(${LIB_STATIC} PROPERTIES
  152. INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
  153. INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
  154. endif()
  155. if(CMAKEVERSION AND CMAKESONAME)
  156. set_target_properties(${LIB_STATIC} PROPERTIES
  157. VERSION ${CMAKEVERSION} SOVERSION ${CMAKESONAME})
  158. endif()
  159. target_include_directories(${LIB_STATIC} INTERFACE
  160. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  161. $<BUILD_INTERFACE:${CURL_SOURCE_DIR}/include>)
  162. endif()
  163. if(BUILD_SHARED_LIBS)
  164. list(APPEND libcurl_export ${LIB_SHARED})
  165. add_library(${LIB_SHARED} SHARED ${LIB_SOURCE})
  166. add_library(${PROJECT_NAME}::${LIB_SHARED} ALIAS ${LIB_SHARED})
  167. if(WIN32)
  168. set_property(TARGET ${LIB_SHARED} APPEND PROPERTY SOURCES libcurl.rc ${CURL_SOURCE_DIR}/libcurl.def)
  169. endif()
  170. target_link_libraries(${LIB_SHARED} PRIVATE ${CURL_LIBS})
  171. # Remove the "lib" prefix since the library is already named "libcurl".
  172. set_target_properties(${LIB_SHARED} PROPERTIES
  173. PREFIX "" OUTPUT_NAME "${LIBCURL_OUTPUT_NAME}"
  174. IMPORT_PREFIX "" IMPORT_SUFFIX "${IMPORT_LIB_SUFFIX}${CMAKE_IMPORT_LIBRARY_SUFFIX}"
  175. COMPILE_DEFINITIONS "BUILDING_LIBCURL"
  176. POSITION_INDEPENDENT_CODE ON)
  177. if(HIDES_CURL_PRIVATE_SYMBOLS)
  178. set_target_properties(${LIB_SHARED} PROPERTIES
  179. COMPILE_DEFINITIONS "CURL_HIDDEN_SYMBOLS"
  180. COMPILE_FLAGS "${CURL_CFLAG_SYMBOLS_HIDE}")
  181. endif()
  182. if(CURL_HAS_LTO)
  183. set_target_properties(${LIB_SHARED} PROPERTIES
  184. INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
  185. INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
  186. endif()
  187. if(CMAKEVERSION AND CMAKESONAME)
  188. set_target_properties(${LIB_SHARED} PROPERTIES
  189. VERSION ${CMAKEVERSION} SOVERSION ${CMAKESONAME})
  190. endif()
  191. target_include_directories(${LIB_SHARED} INTERFACE
  192. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  193. $<BUILD_INTERFACE:${CURL_SOURCE_DIR}/include>)
  194. endif()
  195. add_library(${LIB_NAME} ALIAS ${LIB_SELECTED})
  196. add_library(${PROJECT_NAME}::${LIB_NAME} ALIAS ${LIB_SELECTED})
  197. if(CURL_ENABLE_EXPORT_TARGET)
  198. if(BUILD_STATIC_LIBS)
  199. install(TARGETS ${LIB_STATIC}
  200. EXPORT ${TARGETS_EXPORT_NAME}
  201. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  202. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  203. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  204. )
  205. endif()
  206. if(BUILD_SHARED_LIBS)
  207. install(TARGETS ${LIB_SHARED}
  208. EXPORT ${TARGETS_EXPORT_NAME}
  209. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  210. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  211. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  212. )
  213. endif()
  214. export(TARGETS ${libcurl_export}
  215. FILE ${PROJECT_BINARY_DIR}/libcurl-target.cmake
  216. NAMESPACE ${PROJECT_NAME}::
  217. )
  218. endif()