113498266Sopenharmony_ci#***************************************************************************
213498266Sopenharmony_ci#                                  _   _ ____  _
313498266Sopenharmony_ci#  Project                     ___| | | |  _ \| |
413498266Sopenharmony_ci#                             / __| | | | |_) | |
513498266Sopenharmony_ci#                            | (__| |_| |  _ <| |___
613498266Sopenharmony_ci#                             \___|\___/|_| \_\_____|
713498266Sopenharmony_ci#
813498266Sopenharmony_ci# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
913498266Sopenharmony_ci#
1013498266Sopenharmony_ci# This software is licensed as described in the file COPYING, which
1113498266Sopenharmony_ci# you should have received as part of this distribution. The terms
1213498266Sopenharmony_ci# are also available at https://curl.se/docs/copyright.html.
1313498266Sopenharmony_ci#
1413498266Sopenharmony_ci# You may opt to use, copy, modify, merge, publish, distribute and/or sell
1513498266Sopenharmony_ci# copies of the Software, and permit persons to whom the Software is
1613498266Sopenharmony_ci# furnished to do so, under the terms of the COPYING file.
1713498266Sopenharmony_ci#
1813498266Sopenharmony_ci# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
1913498266Sopenharmony_ci# KIND, either express or implied.
2013498266Sopenharmony_ci#
2113498266Sopenharmony_ci# SPDX-License-Identifier: curl
2213498266Sopenharmony_ci#
2313498266Sopenharmony_ci###########################################################################
2413498266Sopenharmony_ciset(LIB_NAME libcurl)
2513498266Sopenharmony_ciset(LIBCURL_OUTPUT_NAME libcurl CACHE STRING "Basename of the curl library")
2613498266Sopenharmony_ciadd_definitions(-DBUILDING_LIBCURL)
2713498266Sopenharmony_ci
2813498266Sopenharmony_ciconfigure_file(curl_config.h.cmake
2913498266Sopenharmony_ci  ${CMAKE_CURRENT_BINARY_DIR}/curl_config.h)
3013498266Sopenharmony_ci
3113498266Sopenharmony_citransform_makefile_inc("Makefile.inc" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake")
3213498266Sopenharmony_ciinclude(${CMAKE_CURRENT_BINARY_DIR}/Makefile.inc.cmake)
3313498266Sopenharmony_ci
3413498266Sopenharmony_cilist(APPEND HHEADERS
3513498266Sopenharmony_ci  ${CMAKE_CURRENT_BINARY_DIR}/curl_config.h
3613498266Sopenharmony_ci  )
3713498266Sopenharmony_ci
3813498266Sopenharmony_ci# The rest of the build
3913498266Sopenharmony_ci
4013498266Sopenharmony_ciinclude_directories(${CMAKE_CURRENT_BINARY_DIR}/../include)
4113498266Sopenharmony_ciinclude_directories(${CMAKE_CURRENT_SOURCE_DIR}/..)
4213498266Sopenharmony_ciinclude_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include)
4313498266Sopenharmony_ciinclude_directories(${CMAKE_CURRENT_BINARY_DIR}/..)
4413498266Sopenharmony_ciinclude_directories(${CMAKE_CURRENT_SOURCE_DIR})
4513498266Sopenharmony_ciinclude_directories(${CMAKE_CURRENT_BINARY_DIR})
4613498266Sopenharmony_ciif(USE_ARES)
4713498266Sopenharmony_ci  include_directories(${CARES_INCLUDE_DIR})
4813498266Sopenharmony_ciendif()
4913498266Sopenharmony_ci
5013498266Sopenharmony_ciif(BUILD_TESTING)
5113498266Sopenharmony_ci  add_library(
5213498266Sopenharmony_ci    curlu # special libcurlu library just for unittests
5313498266Sopenharmony_ci    STATIC
5413498266Sopenharmony_ci    EXCLUDE_FROM_ALL
5513498266Sopenharmony_ci    ${HHEADERS} ${CSOURCES}
5613498266Sopenharmony_ci  )
5713498266Sopenharmony_ci  target_compile_definitions(curlu PUBLIC UNITTESTS CURL_STATICLIB)
5813498266Sopenharmony_ciendif()
5913498266Sopenharmony_ci
6013498266Sopenharmony_ciif(ENABLE_CURLDEBUG)
6113498266Sopenharmony_ci  # We must compile these sources separately to avoid memdebug.h redefinitions
6213498266Sopenharmony_ci  # applying to them.
6313498266Sopenharmony_ci  set_source_files_properties(memdebug.c curl_multibyte.c PROPERTIES SKIP_UNITY_BUILD_INCLUSION ON)
6413498266Sopenharmony_ciendif()
6513498266Sopenharmony_ci
6613498266Sopenharmony_ciif(BUILD_TESTING)
6713498266Sopenharmony_ci  target_link_libraries(curlu PRIVATE ${CURL_LIBS})
6813498266Sopenharmony_ciendif()
6913498266Sopenharmony_ci
7013498266Sopenharmony_citransform_makefile_inc("Makefile.soname" "${CMAKE_CURRENT_BINARY_DIR}/Makefile.soname.cmake")
7113498266Sopenharmony_ciinclude(${CMAKE_CURRENT_BINARY_DIR}/Makefile.soname.cmake)
7213498266Sopenharmony_ci
7313498266Sopenharmony_ciif(CMAKE_SYSTEM_NAME STREQUAL "AIX" OR
7413498266Sopenharmony_ci  CMAKE_SYSTEM_NAME STREQUAL "Linux" OR
7513498266Sopenharmony_ci  CMAKE_SYSTEM_NAME STREQUAL "Darwin" OR
7613498266Sopenharmony_ci  CMAKE_SYSTEM_NAME STREQUAL "SunOS" OR
7713498266Sopenharmony_ci  CMAKE_SYSTEM_NAME STREQUAL "GNU/kFreeBSD" OR
7813498266Sopenharmony_ci
7913498266Sopenharmony_ci  # FreeBSD comes with the a.out and elf flavours
8013498266Sopenharmony_ci  # but a.out was supported up to version 3.x and
8113498266Sopenharmony_ci  # elf from 3.x. I cannot imagine someone running
8213498266Sopenharmony_ci  # CMake on those ancient systems
8313498266Sopenharmony_ci  CMAKE_SYSTEM_NAME STREQUAL "FreeBSD" OR
8413498266Sopenharmony_ci
8513498266Sopenharmony_ci  CMAKE_SYSTEM_NAME STREQUAL "Haiku")
8613498266Sopenharmony_ci
8713498266Sopenharmony_ci  math(EXPR CMAKESONAME "${VERSIONCHANGE} - ${VERSIONDEL}")
8813498266Sopenharmony_ci  set(CMAKEVERSION "${CMAKESONAME}.${VERSIONDEL}.${VERSIONADD}")
8913498266Sopenharmony_cielse()
9013498266Sopenharmony_ci  unset(CMAKESONAME)
9113498266Sopenharmony_ciendif()
9213498266Sopenharmony_ci
9313498266Sopenharmony_ci## Library definition
9413498266Sopenharmony_ci
9513498266Sopenharmony_ci# Add "_imp" as a suffix before the extension to avoid conflicting with
9613498266Sopenharmony_ci# the statically linked "libcurl.lib" (typically with MSVC)
9713498266Sopenharmony_ciif(WIN32 AND
9813498266Sopenharmony_ci   NOT IMPORT_LIB_SUFFIX AND
9913498266Sopenharmony_ci   CMAKE_STATIC_LIBRARY_SUFFIX STREQUAL CMAKE_IMPORT_LIBRARY_SUFFIX)
10013498266Sopenharmony_ci  set(IMPORT_LIB_SUFFIX "_imp")
10113498266Sopenharmony_ciendif()
10213498266Sopenharmony_ci
10313498266Sopenharmony_ci# Whether to do a single compilation pass for libcurl sources and reuse these
10413498266Sopenharmony_ci# objects to generate both static and shared target.
10513498266Sopenharmony_ciif(NOT DEFINED SHARE_LIB_OBJECT)
10613498266Sopenharmony_ci  # Enable it by default on platforms where PIC is the default for both shared
10713498266Sopenharmony_ci  # and static and there is a way to tell the linker which libcurl symbols it
10813498266Sopenharmony_ci  # should export (vs. marking these symbols exportable at compile-time).
10913498266Sopenharmony_ci  if(WIN32)
11013498266Sopenharmony_ci    set(SHARE_LIB_OBJECT ON)
11113498266Sopenharmony_ci  else()
11213498266Sopenharmony_ci    # On other platforms, make it an option disabled by default
11313498266Sopenharmony_ci    set(SHARE_LIB_OBJECT OFF)
11413498266Sopenharmony_ci  endif()
11513498266Sopenharmony_ciendif()
11613498266Sopenharmony_ci
11713498266Sopenharmony_ciif(WIN32)
11813498266Sopenharmony_ci  # Define CURL_STATICLIB always, to disable __declspec(dllexport) for exported
11913498266Sopenharmony_ci  # libcurl symbols. We handle exports via libcurl.def instead. Except with
12013498266Sopenharmony_ci  # symbol hiding disabled or debug mode enabled, when we export _all_ symbols
12113498266Sopenharmony_ci  # from libcurl DLL, without using libcurl.def.
12213498266Sopenharmony_ci  add_definitions("-DCURL_STATICLIB")
12313498266Sopenharmony_ciendif()
12413498266Sopenharmony_ci
12513498266Sopenharmony_ciif(SHARE_LIB_OBJECT)
12613498266Sopenharmony_ci  set(LIB_OBJECT "libcurl_object")
12713498266Sopenharmony_ci  add_library(${LIB_OBJECT} OBJECT ${HHEADERS} ${CSOURCES})
12813498266Sopenharmony_ci  target_link_libraries(${LIB_OBJECT} PRIVATE ${CURL_LIBS})
12913498266Sopenharmony_ci  set_target_properties(${LIB_OBJECT} PROPERTIES
13013498266Sopenharmony_ci    POSITION_INDEPENDENT_CODE ON)
13113498266Sopenharmony_ci  if(HIDES_CURL_PRIVATE_SYMBOLS)
13213498266Sopenharmony_ci    set_property(TARGET ${LIB_OBJECT} APPEND PROPERTY COMPILE_FLAGS "${CURL_CFLAG_SYMBOLS_HIDE}")
13313498266Sopenharmony_ci    set_property(TARGET ${LIB_OBJECT} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_HIDDEN_SYMBOLS")
13413498266Sopenharmony_ci  endif()
13513498266Sopenharmony_ci  if(CURL_HAS_LTO)
13613498266Sopenharmony_ci    set_target_properties(${LIB_OBJECT} PROPERTIES
13713498266Sopenharmony_ci      INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
13813498266Sopenharmony_ci      INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
13913498266Sopenharmony_ci  endif()
14013498266Sopenharmony_ci
14113498266Sopenharmony_ci  target_include_directories(${LIB_OBJECT} INTERFACE
14213498266Sopenharmony_ci    $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
14313498266Sopenharmony_ci    $<BUILD_INTERFACE:${CURL_SOURCE_DIR}/include>)
14413498266Sopenharmony_ci
14513498266Sopenharmony_ci  set(LIB_SOURCE $<TARGET_OBJECTS:${LIB_OBJECT}>)
14613498266Sopenharmony_cielse()
14713498266Sopenharmony_ci  set(LIB_SOURCE ${HHEADERS} ${CSOURCES})
14813498266Sopenharmony_ciendif()
14913498266Sopenharmony_ci
15013498266Sopenharmony_ci# we want it to be called libcurl on all platforms
15113498266Sopenharmony_ciif(BUILD_STATIC_LIBS)
15213498266Sopenharmony_ci  list(APPEND libcurl_export ${LIB_STATIC})
15313498266Sopenharmony_ci  add_library(${LIB_STATIC} STATIC ${LIB_SOURCE})
15413498266Sopenharmony_ci  add_library(${PROJECT_NAME}::${LIB_STATIC} ALIAS ${LIB_STATIC})
15513498266Sopenharmony_ci  target_link_libraries(${LIB_STATIC} PRIVATE ${CURL_LIBS})
15613498266Sopenharmony_ci  # Remove the "lib" prefix since the library is already named "libcurl".
15713498266Sopenharmony_ci  set_target_properties(${LIB_STATIC} PROPERTIES
15813498266Sopenharmony_ci    PREFIX "" OUTPUT_NAME "${LIBCURL_OUTPUT_NAME}"
15913498266Sopenharmony_ci    SUFFIX "${STATIC_LIB_SUFFIX}${CMAKE_STATIC_LIBRARY_SUFFIX}"
16013498266Sopenharmony_ci    INTERFACE_COMPILE_DEFINITIONS "CURL_STATICLIB")
16113498266Sopenharmony_ci  if(HIDES_CURL_PRIVATE_SYMBOLS)
16213498266Sopenharmony_ci    set_property(TARGET ${LIB_STATIC} APPEND PROPERTY COMPILE_FLAGS "${CURL_CFLAG_SYMBOLS_HIDE}")
16313498266Sopenharmony_ci    set_property(TARGET ${LIB_STATIC} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_HIDDEN_SYMBOLS")
16413498266Sopenharmony_ci  endif()
16513498266Sopenharmony_ci  if(CURL_HAS_LTO)
16613498266Sopenharmony_ci    set_target_properties(${LIB_STATIC} PROPERTIES
16713498266Sopenharmony_ci      INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
16813498266Sopenharmony_ci      INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
16913498266Sopenharmony_ci  endif()
17013498266Sopenharmony_ci  if(CMAKEVERSION AND CMAKESONAME)
17113498266Sopenharmony_ci    set_target_properties(${LIB_STATIC} PROPERTIES
17213498266Sopenharmony_ci      VERSION ${CMAKEVERSION} SOVERSION ${CMAKESONAME})
17313498266Sopenharmony_ci  endif()
17413498266Sopenharmony_ci
17513498266Sopenharmony_ci  target_include_directories(${LIB_STATIC} INTERFACE
17613498266Sopenharmony_ci    $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
17713498266Sopenharmony_ci    $<BUILD_INTERFACE:${CURL_SOURCE_DIR}/include>)
17813498266Sopenharmony_ciendif()
17913498266Sopenharmony_ci
18013498266Sopenharmony_ciif(BUILD_SHARED_LIBS)
18113498266Sopenharmony_ci  list(APPEND libcurl_export ${LIB_SHARED})
18213498266Sopenharmony_ci  add_library(${LIB_SHARED} SHARED ${LIB_SOURCE})
18313498266Sopenharmony_ci  add_library(${PROJECT_NAME}::${LIB_SHARED} ALIAS ${LIB_SHARED})
18413498266Sopenharmony_ci  if(WIN32)
18513498266Sopenharmony_ci    set_property(TARGET ${LIB_SHARED} APPEND PROPERTY SOURCES libcurl.rc)
18613498266Sopenharmony_ci    if(HIDES_CURL_PRIVATE_SYMBOLS)
18713498266Sopenharmony_ci      set_property(TARGET ${LIB_SHARED} APPEND PROPERTY SOURCES "${CURL_SOURCE_DIR}/libcurl.def")
18813498266Sopenharmony_ci    endif()
18913498266Sopenharmony_ci  endif()
19013498266Sopenharmony_ci  target_link_libraries(${LIB_SHARED} PRIVATE ${CURL_LIBS})
19113498266Sopenharmony_ci  # Remove the "lib" prefix since the library is already named "libcurl".
19213498266Sopenharmony_ci  set_target_properties(${LIB_SHARED} PROPERTIES
19313498266Sopenharmony_ci    PREFIX "" OUTPUT_NAME "${LIBCURL_OUTPUT_NAME}"
19413498266Sopenharmony_ci    IMPORT_PREFIX "" IMPORT_SUFFIX "${IMPORT_LIB_SUFFIX}${CMAKE_IMPORT_LIBRARY_SUFFIX}"
19513498266Sopenharmony_ci    POSITION_INDEPENDENT_CODE ON)
19613498266Sopenharmony_ci  if(HIDES_CURL_PRIVATE_SYMBOLS)
19713498266Sopenharmony_ci    set_property(TARGET ${LIB_SHARED} APPEND PROPERTY COMPILE_FLAGS "${CURL_CFLAG_SYMBOLS_HIDE}")
19813498266Sopenharmony_ci    set_property(TARGET ${LIB_SHARED} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_HIDDEN_SYMBOLS")
19913498266Sopenharmony_ci  endif()
20013498266Sopenharmony_ci  if(CURL_HAS_LTO)
20113498266Sopenharmony_ci    set_target_properties(${LIB_SHARED} PROPERTIES
20213498266Sopenharmony_ci      INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE
20313498266Sopenharmony_ci      INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
20413498266Sopenharmony_ci  endif()
20513498266Sopenharmony_ci  if(CMAKEVERSION AND CMAKESONAME)
20613498266Sopenharmony_ci    set_target_properties(${LIB_SHARED} PROPERTIES
20713498266Sopenharmony_ci      VERSION ${CMAKEVERSION} SOVERSION ${CMAKESONAME})
20813498266Sopenharmony_ci  endif()
20913498266Sopenharmony_ci
21013498266Sopenharmony_ci  target_include_directories(${LIB_SHARED} INTERFACE
21113498266Sopenharmony_ci    $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
21213498266Sopenharmony_ci    $<BUILD_INTERFACE:${CURL_SOURCE_DIR}/include>)
21313498266Sopenharmony_ciendif()
21413498266Sopenharmony_ci
21513498266Sopenharmony_ciadd_library(${LIB_NAME} ALIAS ${LIB_SELECTED})
21613498266Sopenharmony_ciadd_library(${PROJECT_NAME}::${LIB_NAME} ALIAS ${LIB_SELECTED})
21713498266Sopenharmony_ci
21813498266Sopenharmony_ciif(CURL_ENABLE_EXPORT_TARGET)
21913498266Sopenharmony_ci  if(BUILD_STATIC_LIBS)
22013498266Sopenharmony_ci    install(TARGETS ${LIB_STATIC}
22113498266Sopenharmony_ci      EXPORT ${TARGETS_EXPORT_NAME}
22213498266Sopenharmony_ci      ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
22313498266Sopenharmony_ci      LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
22413498266Sopenharmony_ci      RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
22513498266Sopenharmony_ci    )
22613498266Sopenharmony_ci  endif()
22713498266Sopenharmony_ci  if(BUILD_SHARED_LIBS)
22813498266Sopenharmony_ci    install(TARGETS ${LIB_SHARED}
22913498266Sopenharmony_ci      EXPORT ${TARGETS_EXPORT_NAME}
23013498266Sopenharmony_ci      ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
23113498266Sopenharmony_ci      LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
23213498266Sopenharmony_ci      RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
23313498266Sopenharmony_ci    )
23413498266Sopenharmony_ci  endif()
23513498266Sopenharmony_ci
23613498266Sopenharmony_ci  export(TARGETS ${libcurl_export}
23713498266Sopenharmony_ci         FILE ${PROJECT_BINARY_DIR}/libcurl-target.cmake
23813498266Sopenharmony_ci         NAMESPACE ${PROJECT_NAME}::
23913498266Sopenharmony_ci  )
24013498266Sopenharmony_ciendif()
241