# Copyright (c) Facebook, Inc. and its 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.
project(Folly)
cmake_minimum_required(VERSION 3.14)

set(VELOX_FOLLY_BUILD_VERSION v2022.11.14.00)
set(VELOX_FOLLY_BUILD_SHA256_CHECKSUM
    b249436cb61b6dfd5288093565438d8da642b07ae021191a4042b221bc1bdc0e)
set(VELOX_FOLLY_SOURCE_URL
    "https://github.com/facebook/folly/archive/${VELOX_FOLLY_BUILD_VERSION}.tar.gz"
)

resolve_dependency_url(FOLLY)

message(STATUS "Building Folly from source")

if(gflags_SOURCE STREQUAL "BUNDLED")
  set(glog_patch && git apply ${CMAKE_CURRENT_LIST_DIR}/folly-gflags-glog.patch)
endif()

FetchContent_Declare(
  folly
  URL ${VELOX_FOLLY_SOURCE_URL}
  URL_HASH ${VELOX_FOLLY_BUILD_SHA256_CHECKSUM}
  PATCH_COMMAND git apply ${CMAKE_CURRENT_LIST_DIR}/folly-no-export.patch
                ${glog_patch})

if(ON_APPLE_M1)
  # folly will wrongly assume x86_64 if this is not set
  set(CMAKE_LIBRARY_ARCHITECTURE aarch64)
endif()
# Suppress all warnings
set(FOLLY_CXX_FLAGS -w)
# Enable INT128 support
set(FOLLY_HAVE_INT128_T ON)
FetchContent_MakeAvailable(folly)

# Folly::folly is not valid for FC but we want to match FindFolly
add_library(Folly::folly ALIAS folly)

# The folly target does not contain any include directories, they are propagated
# from folly_base. This marks them as system headers which should suppress
# warnigs generated by them when they are included else where.
get_target_property(_inc folly_base INTERFACE_INCLUDE_DIRECTORIES)
target_include_directories(folly_base SYSTEM INTERFACE ${_inc})

if(${gflags_SOURCE} STREQUAL "BUNDLED")
  add_dependencies(folly glog gflags)
endif()

set(FOLLY_BENCHMARK_STATIC_LIB
    ${folly_BINARY_DIR}/folly/libfollybenchmark${CMAKE_STATIC_LIBRARY_SUFFIX}
    PARENT_SCOPE)
