# 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 (THRIFT_TESTS OR THRIFT_BENCHMARKS)
  add_subdirectory(util/test)
endif ()

add_library(thrift-core Thrift.cpp ../cpp2/FieldRef.cpp)
thrift_configure_target(thrift-core PUBLIC)
target_link_libraries(thrift-core PUBLIC fmt::fmt Folly::folly)

add_library(
  concurrency concurrency/InitThreadFactory.cpp
              concurrency/PosixThreadFactory.cpp concurrency/ThreadManager.cpp)
thrift_configure_target(concurrency PUBLIC)
target_link_libraries(concurrency PUBLIC Folly::folly gflags glog::glog)

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

# transport pulls in rocket/CompressionManager via THeader.cpp, which
# lives in thriftcpp2, and async and thrift build on transport. Build them 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
    ../cpp2/transport/rocket/compression/CompressionAlgorithmSelector.cpp
    ../cpp2/transport/rocket/compression/CompressionManager.cpp
    util/PausableTimer.cpp
    util/THttpParser.cpp
    util/VarintUtils.cpp)
  add_dependencies(transport rpcmetadata)
  target_link_libraries(
    transport
    PUBLIC concurrency
           thrift-core
           rpcmetadata
           Folly::folly
           ZLIB::ZLIB
           ${thrift_openssl_crypto}
           zstd::zstd)

  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::headers
           Folly::folly
           ${thrift_wangle}
           glog::glog
           ${thrift_openssl_crypto})

  add_library(thrift-cpp INTERFACE)
  target_link_libraries(
    thrift-cpp INTERFACE async concurrency thriftprotocol transport
                         Folly::folly glog::glog)
endif () # THRIFT_RPC

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

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

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