CMakeLists.txt 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. # On supported platforms, do a single compilation pass for libcurl sources
  96. # and reuse these objects to generate both static and shared target.
  97. # This is possible where PIC is the default for both shared and static and
  98. # there is a way to tell the linker which libcurl symbols it should export
  99. # (vs. marking these symbols exportable at compile-time).
  100. if(WIN32 AND NOT DEFINED SHARE_LIB_OBJECT)
  101. set(SHARE_LIB_OBJECT ON)
  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. COMPILE_DEFINITIONS "BUILDING_LIBCURL"
  109. INTERFACE_COMPILE_DEFINITIONS "CURL_STATICLIB")
  110. if(HIDES_CURL_PRIVATE_SYMBOLS)
  111. set_target_properties(${LIB_OBJECT} PROPERTIES
  112. COMPILE_DEFINITIONS "CURL_HIDDEN_SYMBOLS"
  113. COMPILE_FLAGS "${CURL_CFLAG_SYMBOLS_HIDE}")
  114. endif()
  115. if(CURL_HAS_LTO)
  116. set_target_properties(${LIB_OBJECT} PROPERTIES
  117. INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
  118. INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
  119. endif()
  120. target_include_directories(${LIB_OBJECT} INTERFACE
  121. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  122. $<BUILD_INTERFACE:${CURL_SOURCE_DIR}/include>)
  123. set(LIB_SOURCE $<TARGET_OBJECTS:${LIB_OBJECT}>)
  124. else()
  125. set(LIB_SOURCE ${HHEADERS} ${CSOURCES})
  126. endif()
  127. # we want it to be called libcurl on all platforms
  128. if(BUILD_STATIC_LIBS)
  129. list(APPEND libcurl_export ${LIB_STATIC})
  130. add_library(${LIB_STATIC} STATIC ${LIB_SOURCE})
  131. add_library(${PROJECT_NAME}::${LIB_STATIC} ALIAS ${LIB_STATIC})
  132. target_link_libraries(${LIB_STATIC} PRIVATE ${CURL_LIBS})
  133. # Remove the "lib" prefix since the library is already named "libcurl".
  134. set_target_properties(${LIB_STATIC} PROPERTIES
  135. PREFIX "" OUTPUT_NAME "${LIBCURL_OUTPUT_NAME}"
  136. SUFFIX "${STATIC_LIB_SUFFIX}${CMAKE_STATIC_LIBRARY_SUFFIX}"
  137. COMPILE_DEFINITIONS "BUILDING_LIBCURL"
  138. INTERFACE_COMPILE_DEFINITIONS "CURL_STATICLIB")
  139. if(HIDES_CURL_PRIVATE_SYMBOLS)
  140. set_target_properties(${LIB_STATIC} PROPERTIES
  141. COMPILE_DEFINITIONS "CURL_HIDDEN_SYMBOLS"
  142. COMPILE_FLAGS "${CURL_CFLAG_SYMBOLS_HIDE}")
  143. endif()
  144. if(CURL_HAS_LTO)
  145. set_target_properties(${LIB_STATIC} PROPERTIES
  146. INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
  147. INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
  148. endif()
  149. if(CMAKEVERSION AND CMAKESONAME)
  150. set_target_properties(${LIB_STATIC} PROPERTIES
  151. VERSION ${CMAKEVERSION} SOVERSION ${CMAKESONAME})
  152. endif()
  153. target_include_directories(${LIB_STATIC} INTERFACE
  154. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  155. $<BUILD_INTERFACE:${CURL_SOURCE_DIR}/include>)
  156. endif()
  157. if(BUILD_SHARED_LIBS)
  158. list(APPEND libcurl_export ${LIB_SHARED})
  159. add_library(${LIB_SHARED} SHARED ${LIB_SOURCE})
  160. add_library(${PROJECT_NAME}::${LIB_SHARED} ALIAS ${LIB_SHARED})
  161. if(WIN32)
  162. set_property(TARGET ${LIB_SHARED} APPEND PROPERTY SOURCES libcurl.rc ${CURL_SOURCE_DIR}/libcurl.def)
  163. endif()
  164. target_link_libraries(${LIB_SHARED} PRIVATE ${CURL_LIBS})
  165. # Remove the "lib" prefix since the library is already named "libcurl".
  166. set_target_properties(${LIB_SHARED} PROPERTIES
  167. PREFIX "" OUTPUT_NAME "${LIBCURL_OUTPUT_NAME}"
  168. IMPORT_PREFIX "" IMPORT_SUFFIX "${IMPORT_LIB_SUFFIX}${CMAKE_IMPORT_LIBRARY_SUFFIX}"
  169. COMPILE_DEFINITIONS "BUILDING_LIBCURL"
  170. POSITION_INDEPENDENT_CODE ON)
  171. if(HIDES_CURL_PRIVATE_SYMBOLS)
  172. set_target_properties(${LIB_SHARED} PROPERTIES
  173. COMPILE_DEFINITIONS "CURL_HIDDEN_SYMBOLS"
  174. COMPILE_FLAGS "${CURL_CFLAG_SYMBOLS_HIDE}")
  175. endif()
  176. if(CURL_HAS_LTO)
  177. set_target_properties(${LIB_SHARED} PROPERTIES
  178. INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
  179. INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
  180. endif()
  181. if(CMAKEVERSION AND CMAKESONAME)
  182. set_target_properties(${LIB_SHARED} PROPERTIES
  183. VERSION ${CMAKEVERSION} SOVERSION ${CMAKESONAME})
  184. endif()
  185. target_include_directories(${LIB_SHARED} INTERFACE
  186. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  187. $<BUILD_INTERFACE:${CURL_SOURCE_DIR}/include>)
  188. endif()
  189. add_library(${LIB_NAME} ALIAS ${LIB_SELECTED})
  190. add_library(${PROJECT_NAME}::${LIB_NAME} ALIAS ${LIB_SELECTED})
  191. if(CURL_ENABLE_EXPORT_TARGET)
  192. if(BUILD_STATIC_LIBS)
  193. install(TARGETS ${LIB_STATIC}
  194. EXPORT ${TARGETS_EXPORT_NAME}
  195. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  196. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  197. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  198. )
  199. endif()
  200. if(BUILD_SHARED_LIBS)
  201. install(TARGETS ${LIB_SHARED}
  202. EXPORT ${TARGETS_EXPORT_NAME}
  203. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  204. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  205. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  206. )
  207. endif()
  208. export(TARGETS ${libcurl_export}
  209. FILE ${PROJECT_BINARY_DIR}/libcurl-target.cmake
  210. NAMESPACE ${PROJECT_NAME}::
  211. )
  212. endif()