# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

if (enable_tests)
  add_subdirectory(util/test)
endif ()

add_library(
  thrift-core

  Thrift.cpp
  ../cpp2/FieldRef.cpp
)
target_include_directories(
  thrift-core
  INTERFACE
    $<INSTALL_INTERFACE:${INCLUDE_INSTALL_DIR}>
)
target_link_libraries(
  thrift-core
  PUBLIC
    fmt::fmt
    Folly::folly
)

add_library(
  concurrency

  concurrency/InitThreadFactory.cpp
  concurrency/PosixThreadFactory.cpp
  concurrency/ThreadManager.cpp
)
target_link_libraries(
  concurrency
  PUBLIC
    Folly::folly
    ${LIBGFLAGS_LIBRARY}
    ${GLOG_LIBRARIES}
)

# transport pulls in rocket/CompressionManager via THeader.cpp, which
# lives in thriftcpp2. Build it only when the RPC stack is enabled.
if (THRIFT_RPC)
  add_library(
    transport

    transport/TTransportException.cpp
    transport/TFDTransport.cpp
    transport/THttpTransport.cpp
    transport/THttpClient.cpp
    transport/THttpServer.cpp
    transport/TSocket.cpp
    transport/TBufferTransports.cpp
    transport/THeader.cpp
    transport/TZlibTransport.cpp
    ../cpp2/transport/rocket/framing/Util.cpp
    ../cpp2/Flags.cpp
    util/PausableTimer.cpp
    util/THttpParser.cpp
    util/VarintUtils.cpp
  )
  add_dependencies(
    transport
    rpcmetadata
  )
  target_link_libraries(
    transport
    PUBLIC
      concurrency
      thrift-core
      rpcmetadata
      # We need to link to libthriftcpp2.a after libtransport.a because
      # libtransport.a uses
      # apache::thrift::rocket::CompressionManager::uncompressBuffer()
      # that is defined in libthriftcpp2.a.
      #
      # We don't need this for libtransport.so because libthriftcpp2.so
      # and libtransport.so are cyclic dependencies and libthriftcpp2.so
      # has libtransport.so dependency. We can't link to
      # libthriftcpp2.so from libtransport.so. It's not allowed.
      $<$<AND:$<NOT:$<BOOL:${BUILD_SHARED_LIBS}>>,$<BOOL:${THRIFT_RPC}>>:$<INSTALL_INTERFACE:FBThrift::thriftcpp2>>
      Folly::folly
      ZLIB::ZLIB
      ${OPENSSL_LIBRARIES}
      ${ZSTD_LIBRARIES}
  )
endif ()

add_library(
  runtime

  ../cpp2/runtime/Init.cpp
)
target_link_libraries(
  runtime
  PUBLIC
    Folly::folly
)

if (THRIFT_RPC)
  add_library(
    async

    ContextStack.cpp
    EventHandlerBase.cpp
    server/TServerObserver.cpp
    ../cpp2/detail/EventHandlerRuntime.cpp
    ../cpp2/async/ClientInterceptorBase.cpp
    ../cpp2/async/InterceptorFlags.cpp
    ../cpp2/async/InterceptorFrameworkMetadata.cpp
  )
  if (
    CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR
    CMAKE_CXX_COMPILER_ID STREQUAL "GNU"
  )
    target_compile_options(
      async
      PUBLIC
        $<$<COMPILE_LANGUAGE:CXX>:-fsized-deallocation>
    )
  endif()
  target_link_libraries(
    async
    PUBLIC
      concurrency
      runtime
      transport
      Boost::boost
      Folly::folly
      wangle::wangle
      ${GLOG_LIBRARIES}
      ${OPENSSL_LIBRARIES}
  )

  add_library(
    thrift
    INTERFACE
  )
  target_link_libraries(
    thrift
    INTERFACE
      async
      concurrency
      thriftprotocol
      transport
      Folly::folly
      ${GLOG_LIBRARIES}
  )
endif () # THRIFT_RPC

set(THRIFT1_HEADER_DIRS
  async
  concurrency
  protocol
  server
  transport
)
foreach(dir ${THRIFT1_HEADER_DIRS})
  install(DIRECTORY ${dir} DESTINATION ${INCLUDE_INSTALL_DIR}/thrift/lib/cpp
          FILES_MATCHING PATTERN "*.h")
  install(DIRECTORY ${dir} DESTINATION ${INCLUDE_INSTALL_DIR}/thrift/lib/cpp
          FILES_MATCHING PATTERN "*.tcc")
endforeach()

if (BUILD_SHARED_LIBS)
  # all but thrift since it's an interface
  set_target_properties(thrift-core concurrency
    PROPERTIES VERSION ${PACKAGE_VERSION}
    )
  if (THRIFT_RPC)
    set_target_properties(async transport
      PROPERTIES VERSION ${PACKAGE_VERSION}
      )
  endif ()
endif()

install(
  TARGETS
    thrift-core
    concurrency
    runtime
  EXPORT fbthrift-exports
  DESTINATION ${LIB_INSTALL_DIR}
)

if (THRIFT_RPC)
  install(
    TARGETS
      transport
      async
      thrift
    EXPORT fbthrift-exports
    DESTINATION ${LIB_INSTALL_DIR}
  )
endif ()
