2
0

CMakeLists.txt 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. if(BUILD_SHARED_LIBS)
  28. set(CURL_STATICLIB NO)
  29. else()
  30. set(CURL_STATICLIB YES)
  31. endif()
  32. # Use:
  33. # * CURL_STATICLIB
  34. configure_file(curl_config.h.cmake
  35. ${CMAKE_CURRENT_BINARY_DIR}/curl_config.h)
  36. transform_makefile_inc("Makefile.inc" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
  37. include(${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake)
  38. list(APPEND HHEADERS
  39. ${CMAKE_CURRENT_BINARY_DIR}/curl_config.h
  40. )
  41. if(WIN32 AND NOT CURL_STATICLIB)
  42. list(APPEND CSOURCES libcurl.rc)
  43. endif()
  44. # The rest of the build
  45. include_directories(${CMAKE_CURRENT_BINARY_DIR}/../include)
  46. include_directories(${CMAKE_CURRENT_SOURCE_DIR}/..)
  47. include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include)
  48. include_directories(${CMAKE_CURRENT_BINARY_DIR}/..)
  49. include_directories(${CMAKE_CURRENT_SOURCE_DIR})
  50. include_directories(${CMAKE_CURRENT_BINARY_DIR})
  51. if(USE_ARES)
  52. include_directories(${CARES_INCLUDE_DIR})
  53. endif()
  54. add_library(
  55. ${LIB_NAME}
  56. ${HHEADERS} ${CSOURCES}
  57. )
  58. add_library(
  59. curlu # special libcurlu library just for unittests
  60. STATIC
  61. EXCLUDE_FROM_ALL
  62. ${HHEADERS} ${CSOURCES}
  63. )
  64. target_compile_definitions(curlu PUBLIC UNITTESTS CURL_STATICLIB)
  65. add_library(
  66. ${PROJECT_NAME}::${LIB_NAME}
  67. ALIAS ${LIB_NAME}
  68. )
  69. if(ENABLE_CURLDEBUG)
  70. # We must compile memdebug.c separately to avoid memdebug.h redefinitions
  71. # being applied to memdebug.c itself.
  72. set_source_files_properties(memdebug.c PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON)
  73. endif()
  74. if(NOT BUILD_SHARED_LIBS)
  75. set_target_properties(${LIB_NAME} PROPERTIES INTERFACE_COMPILE_DEFINITIONS CURL_STATICLIB)
  76. endif()
  77. target_link_libraries(${LIB_NAME} PRIVATE ${CURL_LIBS})
  78. target_link_libraries(curlu PRIVATE ${CURL_LIBS})
  79. transform_makefile_inc("Makefile.soname" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.soname.cmake")
  80. include(${CMAKE_CURRENT_BINARY_DIR}/Makefile.soname.cmake)
  81. set_target_properties(${LIB_NAME} PROPERTIES
  82. COMPILE_DEFINITIONS BUILDING_LIBCURL
  83. OUTPUT_NAME ${LIBCURL_OUTPUT_NAME}
  84. )
  85. if(CMAKE_SYSTEM_NAME STREQUAL "AIX" OR
  86. CMAKE_SYSTEM_NAME STREQUAL "Linux" OR
  87. CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR
  88. CMAKE_SYSTEM_NAME STREQUAL "SunOS" OR
  89. CMAKE_SYSTEM_NAME STREQUAL "GNU/kFreeBSD" OR
  90. # FreeBSD comes with the a.out and elf flavours
  91. # but a.out was supported up to version 3.x and
  92. # elf from 3.x. I cannot imagine someone running
  93. # CMake on those ancient systems
  94. CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR
  95. CMAKE_SYSTEM_NAME STREQUAL "Haiku")
  96. math(EXPR CMAKESONAME "${VERSIONCHANGE} - ${VERSIONDEL}")
  97. set(CMAKEVERSION "${CMAKESONAME}.${VERSIONDEL}.${VERSIONADD}")
  98. set_target_properties(${LIB_NAME} PROPERTIES
  99. VERSION ${CMAKEVERSION}
  100. SOVERSION ${CMAKESONAME}
  101. )
  102. endif()
  103. if(HIDES_CURL_PRIVATE_SYMBOLS)
  104. set_property(TARGET ${LIB_NAME} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_HIDDEN_SYMBOLS")
  105. set_property(TARGET ${LIB_NAME} APPEND PROPERTY COMPILE_FLAGS ${CURL_CFLAG_SYMBOLS_HIDE})
  106. endif()
  107. # Remove the "lib" prefix since the library is already named "libcurl".
  108. set_target_properties(${LIB_NAME} PROPERTIES PREFIX "")
  109. set_target_properties(${LIB_NAME} PROPERTIES IMPORT_PREFIX "")
  110. if(CURL_HAS_LTO)
  111. set_target_properties(${LIB_NAME} PROPERTIES
  112. INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
  113. INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
  114. endif()
  115. if(WIN32)
  116. if(BUILD_SHARED_LIBS)
  117. if(MSVC)
  118. # Add "_imp" as a suffix before the extension to avoid conflicting with
  119. # the statically linked "libcurl.lib"
  120. set_target_properties(${LIB_NAME} PROPERTIES IMPORT_SUFFIX "_imp.lib")
  121. endif()
  122. endif()
  123. elseif(NOT CMAKE_CROSSCOMPILING)
  124. # on not-Windows and not-crosscompiling, check for writable argv[]
  125. include(CheckCSourceRuns)
  126. check_c_source_runs("
  127. int main(int argc, char **argv)
  128. {
  129. (void)argc;
  130. argv[0][0] = ' ';
  131. return (argv[0][0] == ' ')?0:1;
  132. }"
  133. HAVE_WRITABLE_ARGV)
  134. endif()
  135. target_include_directories(${LIB_NAME} INTERFACE
  136. $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
  137. $<BUILD_INTERFACE:${CURL_SOURCE_DIR}/include>)
  138. if(CURL_ENABLE_EXPORT_TARGET)
  139. install(TARGETS ${LIB_NAME}
  140. EXPORT ${TARGETS_EXPORT_NAME}
  141. ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  142. LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  143. RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  144. )
  145. export(TARGETS ${LIB_NAME}
  146. FILE ${PROJECT_BINARY_DIR}/libcurl-target.cmake
  147. NAMESPACE ${PROJECT_NAME}::
  148. )
  149. endif()