# 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.

cmake_minimum_required(VERSION 3.24)

project(
  THRIFT
  VERSION 1.0.0
  LANGUAGES CXX)

# Build rules should still use $<TARGET_FILE...> instead of these paths:
# multi-config generators add a per-config subdirectory they do not carry.
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)

include(GNUInstallDirs)

# Not cached, so the default follows CMAKE_INSTALL_LIBDIR on a reconfigure.
if (NOT DEFINED THRIFT_INSTALL_CMAKEDIR)
  set(THRIFT_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/fbthrift")
endif ()

# Relative RPATHs keep the installation relocatable. Append, so a value from
# the command line or from fbcode_builder (which sets one on macOS) survives.
# Both entries go on everything: libraries resolve via the first, executables
# in bindir via the second, and the unused one is an empty search path.
if (NOT WIN32)
  if (APPLE)
    set(_rpath_origin "@loader_path")
  else ()
    set(_rpath_origin "$ORIGIN")
  endif ()
  file(RELATIVE_PATH _libdir_from_bindir "/${CMAKE_INSTALL_BINDIR}"
       "/${CMAKE_INSTALL_LIBDIR}")
  list(APPEND CMAKE_INSTALL_RPATH "${_rpath_origin}"
       "${_rpath_origin}/${_libdir_from_bindir}")
endif ()

# Thrift's libraries default to STATIC, and without PIC they cannot link into
# a consumer built with BUILD_SHARED_LIBS=ON, nor into the thrift-python
# extension .so files. PIC is cheap here, and keeping the archives static
# avoids the cross-dylib RTTI and undefined-symbol problems that building
# Thrift itself shared exposes.
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# The compile settings and the include directories for `#include <thrift/...>`
# and generated headers. PUBLIC targets also export the relocatable install
# path; PRIVATE ones do not.
function (thrift_configure_target target visibility)
  target_compile_features(${target} ${visibility} cxx_std_20)
  # GCC 10 needs this even under -std=c++20, and folly keys FOLLY_HAS_COROUTINES
  # off it. Inert from GCC 11 on.
  if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    target_compile_options(${target} ${visibility}
                           $<$<COMPILE_LANGUAGE:CXX>:-fcoroutines>)
  endif ()
  target_include_directories(
    ${target} ${visibility} $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
    $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>)
  if (NOT visibility STREQUAL "PRIVATE")
    target_include_directories(${target} ${visibility}
                               $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
  endif ()
endfunction ()

# Prepend, so entries from a parent project or the command line survive.
list(PREPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/thrift/cmake"
     "${CMAKE_CURRENT_SOURCE_DIR}/build/fbcode_builder/CMake")

option(
  THRIFT_RPC
  "Enable the RPC transport stack and its dependencies (fizz, wangle, mvfst)."
  ON)
option(THRIFT_TESTS "Enable tests (requires GTest)." OFF)
option(THRIFT_BENCHMARKS
       "Enable benchmarks (requires google/benchmark). Defaults to THRIFT_TESTS."
       ${THRIFT_TESTS})
option(THRIFT_PY_DEPRECATED
       "Enable the deprecated thrift-py library; use thrift-python instead" ON)
option(THRIFT_PYTHON
       "Enable the thrift-python library and extensions (requires Cython)" OFF)

# Records a dependency that exists only in this build tree. As in folly, an
# installed thrift would point at files that are not there, so install rules
# are turned off unless CMAKE_SKIP_INSTALL_RULES is set explicitly. A macro, so
# the setting reaches the subdirectories added after it.
set(thrift_build_local_deps "")
macro (thrift_build_local name)
  list(APPEND thrift_build_local_deps ${name})
  if (NOT DEFINED CMAKE_SKIP_INSTALL_RULES)
    set(CMAKE_SKIP_INSTALL_RULES ON)
    set(thrift_skipped_install ON)
  endif ()
endmacro ()

# Sets `out` to `dep` kept out of thrift's export, like folly_localize_deps.
# CMake before 3.26 has no $<BUILD_LOCAL_INTERFACE:>.
function (thrift_localize out dep)
  if (CMAKE_VERSION VERSION_LESS 3.26)
    set(${out}
        "${dep}"
        PARENT_SCOPE)
  else ()
    set(${out}
        "$<BUILD_LOCAL_INTERFACE:${dep}>"
        PARENT_SCOPE)
  endif ()
endfunction ()

# Fetch the dependency `name` from GitHub at the revision this repo is tested
# against, kept current by rev_sync. Extra arguments go to FetchContent_Declare.
function (thrift_fetch_github org name)
  set(path
      "${CMAKE_CURRENT_SOURCE_DIR}/build/deps/github_hashes/${org}/${name}-rev.txt"
  )
  file(READ "${path}" text)
  if (NOT text MATCHES "Subproject commit ([0-9a-f]+)")
    message(FATAL_ERROR "no ${name} revision in ${path}")
  endif ()
  message(STATUS "${name} not found, fetching ${CMAKE_MATCH_1}")
  include(FetchContent)
  FetchContent_Declare(
    ${name}
    GIT_REPOSITORY https://github.com/${org}/${name}.git
    GIT_TAG ${CMAKE_MATCH_1} ${ARGN})
  FetchContent_MakeAvailable(${name})
endfunction ()

# Homebrew's OpenSSL is keg-only, so CMake's FindOpenSSL doesn't search it.
# Set before folly, which also looks for OpenSSL.
if (APPLE AND NOT DEFINED OPENSSL_ROOT_DIR)
  execute_process(
    COMMAND brew --prefix openssl
    OUTPUT_VARIABLE OPENSSL_ROOT_DIR
    OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
endif ()

find_package(folly CONFIG QUIET)
if (NOT folly_FOUND)
  thrift_fetch_github(facebook folly)
endif ()

# Older folly installs export Threads::Threads without declaring it.
# Newer folly finds it itself, making this redundant.
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads)

# Find required dependencies for thrift/lib
find_package(Gflags REQUIRED)
find_package(Glog REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(ZLIB REQUIRED)
find_package(Zstd REQUIRED)

# folly lists here whatever it had to build into its own build tree. Such a
# dependency is absent from an installed folly, so it must not reach thrift's
# install interface either. The property is global, so it is readable here even
# though folly set it in the scope FetchContent added it in.
get_property(thrift_folly_build_local GLOBAL PROPERTY FOLLY_BUILD_LOCAL_TARGETS)
set(thrift_openssl_crypto OpenSSL::Crypto)
if ("OpenSSL::Crypto" IN_LIST thrift_folly_build_local)
  thrift_build_local(OpenSSL)
  thrift_localize(thrift_openssl_crypto OpenSSL::Crypto)
endif ()

# Fetch xxhash if it is not installed.
function (thrift_fetch_xxhash)
  set(path "${CMAKE_CURRENT_SOURCE_DIR}/build/fbcode_builder/manifests/xxhash")
  file(READ "${path}" text)
  if (NOT text MATCHES
      "url = (https://[^\r\n]+\\.tar\\.gz)[\r\n]+sha256 = ([0-9a-f]+)")
    message(FATAL_ERROR "no archive in ${path}")
  endif ()
  message(STATUS "xxhash not found, fetching ${CMAKE_MATCH_1}")
  set(XXHASH_BUILD_XXHSUM OFF)
  # Otherwise xxhash's option() caches BUILD_SHARED_LIBS=ON for all of thrift.
  if (NOT DEFINED BUILD_SHARED_LIBS)
    set(BUILD_SHARED_LIBS OFF)
  endif ()
  # xxhash FORCEs CMAKE_BUILD_TYPE to Release when it is unset.
  set(saved_build_type "${CMAKE_BUILD_TYPE}")
  include(FetchContent)
  FetchContent_Declare(
    xxhash
    URL "${CMAKE_MATCH_1}"
    URL_HASH SHA256=${CMAKE_MATCH_2}
    SOURCE_SUBDIR cmake_unofficial)
  FetchContent_MakeAvailable(xxhash)
  if (NOT saved_build_type)
    set(CMAKE_BUILD_TYPE
        ""
        CACHE STRING "Choose the type of build." FORCE)
  endif ()
  set_target_properties(xxhash PROPERTIES POSITION_INDEPENDENT_CODE ON)
  # The name an installed xxhash provides.
  add_library(Xxhash::xxhash INTERFACE IMPORTED GLOBAL)
  target_link_libraries(Xxhash::xxhash INTERFACE xxhash)
  FetchContent_GetProperties(xxhash SOURCE_DIR source_dir)
  set(Xxhash_INCLUDE_DIR
      "${source_dir}"
      PARENT_SCOPE)
  set(Xxhash_LIBRARY
      xxhash
      PARENT_SCOPE)
  set(Xxhash_FOUND
      ON
      PARENT_SCOPE)
endfunction ()

find_package(Xxhash)
set(thrift_xxhash Xxhash::xxhash)
if (NOT Xxhash_FOUND)
  # Bundled mode, which a fetched xxhash builds in, disables its install rules.
  thrift_build_local(xxhash)
  thrift_fetch_xxhash()
  thrift_localize(thrift_xxhash Xxhash::xxhash)
endif ()

# libsodium ships no CMakeLists.txt, so ExternalProject runs its configure
# script at build time, as folly does for OpenSSL.
function (thrift_build_sodium)
  set(path
      "${CMAKE_CURRENT_SOURCE_DIR}/build/fbcode_builder/manifests/libsodium")
  file(READ "${path}" text)
  if (NOT text MATCHES
      "url = (https://[^\r\n]+\\.tar\\.gz)[\r\n]+sha256 = ([0-9a-f]+)")
    message(FATAL_ERROR "no archive in ${path}")
  endif ()
  set(url "${CMAKE_MATCH_1}")
  set(sha "${CMAKE_MATCH_2}")
  if (WIN32)
    message(
      FATAL_ERROR
        "libsodium is not installed, and building it is not supported on "
        "Windows. Install libsodium and set sodium_DIR.")
  endif ()
  find_program(THRIFT_SODIUM_MAKE NAMES make)
  if (NOT THRIFT_SODIUM_MAKE)
    message(FATAL_ERROR "libsodium is not installed, and building it needs "
                        "make. Install make or libsodium.")
  endif ()
  message(STATUS "libsodium not found, building ${url}")
  set(prefix "${CMAKE_CURRENT_BINARY_DIR}/libsodium")
  set(lib "${prefix}/lib/libsodium.a")
  include(ExternalProject)
  ExternalProject_Add(
    libsodium
    URL "${url}"
    URL_HASH SHA256=${sha}
    DOWNLOAD_EXTRACT_TIMESTAMP FALSE
    BUILD_IN_SOURCE ON
    CONFIGURE_COMMAND <SOURCE_DIR>/configure --prefix=${prefix}
                      --libdir=${prefix}/lib --disable-shared --with-pic
    BUILD_COMMAND ${THRIFT_SODIUM_MAKE} -j
    INSTALL_COMMAND ${THRIFT_SODIUM_MAKE} install
    # Without this Ninja has no rule to produce the library.
    BUILD_BYPRODUCTS "${lib}")
  file(MAKE_DIRECTORY "${prefix}/include")
  # The target fizz links. add_dependencies() orders compiling against the
  # headers; BUILD_BYPRODUCTS only orders linking.
  add_library(sodium STATIC IMPORTED GLOBAL)
  set_target_properties(
    sodium PROPERTIES IMPORTED_LOCATION "${lib}" INTERFACE_INCLUDE_DIRECTORIES
                                                "${prefix}/include")
  add_dependencies(sodium libsodium)
  # Makes fizz's find_package(Sodium) resolve to this instead of FindSodium.
  file(WRITE "${CMAKE_FIND_PACKAGE_REDIRECTS_DIR}/sodium-config.cmake"
       "set(sodium_INCLUDE_DIR \"${prefix}/include\")\n")
endfunction ()

set(thrift_wangle wangle::wangle)
set(thrift_mvfst_server mvfst::mvfst_server_async_tran)
if (THRIFT_RPC)
  find_package(fizz CONFIG QUIET)
  find_package(wangle CONFIG QUIET)
  find_package(mvfst CONFIG QUIET)
  if (NOT (fizz_FOUND AND wangle_FOUND AND mvfst_FOUND))
    set(BUILD_TESTS OFF)
    set(BUILD_EXAMPLES OFF)
  endif ()
  if (NOT fizz_FOUND)
    # Not find_package(Sodium): FindSodium defines the target fizz links even
    # when it finds nothing.
    find_library(THRIFT_SODIUM_LIBRARY sodium)
    if (NOT THRIFT_SODIUM_LIBRARY)
      thrift_build_sodium()
    endif ()
    thrift_build_local(fizz)
    thrift_fetch_github(facebookincubator fizz SOURCE_SUBDIR fizz)
  endif ()
  if (NOT wangle_FOUND)
    # When folly fetched libevent, FindLibEvent's NOTFOUND results stay cached
    # and wangle would use them. folly's link interface carries libevent.
    foreach (var LIBEVENT_INCLUDE_DIR LIBEVENT_LIB)
      if (NOT ${var})
        set(${var} "")
      endif ()
    endforeach ()
    thrift_build_local(wangle)
    thrift_fetch_github(facebook wangle SOURCE_SUBDIR wangle)
    thrift_localize(thrift_wangle wangle::wangle)
  endif ()
  if (NOT mvfst_FOUND)
    thrift_build_local(mvfst)
    thrift_fetch_github(facebook mvfst)
    thrift_localize(thrift_mvfst_server mvfst::mvfst_server_async_tran)
  endif ()
endif ()

# Add the test dependencies.
if (THRIFT_TESTS)
  find_package(GTest REQUIRED)
  include(GoogleTest)
  enable_testing()
endif ()

if (thrift_skipped_install)
  list(JOIN thrift_build_local_deps ", " text)
  message(STATUS "Not installable, so install rules are off. Install these and "
                 "reconfigure for an installable thrift: ${text}")
endif ()

add_subdirectory(thrift)

# include(CPack) snapshots the CPACK_* variables where it is called, so keep it
# after every target, install rule and CPACK_* setting. A parent project that
# adds Thrift with add_subdirectory() or FetchContent does its own packaging.
if (PROJECT_IS_TOP_LEVEL)
  if (NOT DEFINED CPACK_GENERATOR)
    set(CPACK_GENERATOR RPM)
  endif ()
  set(CPACK_RPM_PACKAGE_PROVIDES thrift)
  include(CPack)
endif ()
