# Copyright (c) 2017 The Bitcoin developers

# This generates config.h which provides numerous defines
# about the state of the plateform we are building on.

include(CheckIncludeFiles)
include(CheckSymbolExists)

# Endianness
check_include_files("endian.h" HAVE_ENDIAN_H)
check_include_files("sys/endian.h" HAVE_SYS_ENDIAN_H)

if(HAVE_ENDIAN_H)
  set(ENDIAN_FILE "endian.h")
elseif(HAVE_SYS_ENDIAN_H)
  set(ENDIAN_FILE "sys/endian.h")
else()
endif()

if(ENDIAN_FILE)
  check_symbol_exists(htole16 ${ENDIAN_FILE} HAVE_DECL_HTOLE16)
  check_symbol_exists(htobe16 ${ENDIAN_FILE} HAVE_DECL_HTOBE16)
  check_symbol_exists(be16toh ${ENDIAN_FILE} HAVE_DECL_BE16TOH)
  check_symbol_exists(le16toh ${ENDIAN_FILE} HAVE_DECL_LE16TOH)
  check_symbol_exists(htobe32 ${ENDIAN_FILE} HAVE_DECL_HTOBE32)
  check_symbol_exists(htole32 ${ENDIAN_FILE} HAVE_DECL_HTOLE32)
  check_symbol_exists(be32toh ${ENDIAN_FILE} HAVE_DECL_BE32TOH)
  check_symbol_exists(le32toh ${ENDIAN_FILE} HAVE_DECL_LE32TOH)
  check_symbol_exists(htobe64 ${ENDIAN_FILE} HAVE_DECL_HTOBE64)
  check_symbol_exists(htole64 ${ENDIAN_FILE} HAVE_DECL_HTOLE64)
  check_symbol_exists(be64toh ${ENDIAN_FILE} HAVE_DECL_BE64TOH)
  check_symbol_exists(le64toh ${ENDIAN_FILE} HAVE_DECL_LE64TOH)
endif()

# Byte swap
check_include_files("byteswap.h" HAVE_BYTESWAP_H)

check_symbol_exists(bswap_16 "byteswap.h" HAVE_DECL_BSWAP_16)
check_symbol_exists(bswap_32 "byteswap.h" HAVE_DECL_BSWAP_32)
check_symbol_exists(bswap_64 "byteswap.h" HAVE_DECL_BSWAP_64)

# Bitmanip intrinsics
function(check_builtin_exist SYMBOL VARIABLE)
  set(
      SOURCE_FILE
      "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckBuiltinExists.c"
  )
  set(
      CMAKE_CONFIGURABLE_FILE_CONTENT
      "int main(int argc, char** argv) { (void)argv; return ${SYMBOL}(argc); }\n"
  )
  configure_file(
      "${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in"
      "${SOURCE_FILE}"
      @ONLY
  )
  if(NOT CMAKE_REQUIRED_QUIET)
    message(STATUS "Looking for ${SYMBOL}")
  endif()
  try_compile(${VARIABLE}
      ${CMAKE_BINARY_DIR}
      ${SOURCE_FILE}
      OUTPUT_VARIABLE OUTPUT
      )
  if(${VARIABLE})
    if(NOT CMAKE_REQUIRED_QUIET)
      message(STATUS "Looking for ${SYMBOL} - found")
    endif()
    set(${VARIABLE} 1 CACHE INTERNAL "Have symbol ${SYMBOL}" PARENT_SCOPE)
    file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
        "Determining if the ${SYMBOL} "
        "exist passed with the following output:\n"
        "${OUTPUT}\nFile ${SOURCEFILE}:\n"
        "${CMAKE_CONFIGURABLE_FILE_CONTENT}\n")
  else()
    if(NOT CMAKE_REQUIRED_QUIET)
      message(STATUS "Looking for ${SYMBOL} - not found")
    endif()
    set(${VARIABLE} "" CACHE INTERNAL "Have symbol ${SYMBOL}" PARENT_SCOPE)
    file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
        "Determining if the ${SYMBOL} "
        "exist failed with the following output:\n"
        "${OUTPUT}\nFile ${SOURCEFILE}:\n"
        "${CMAKE_CONFIGURABLE_FILE_CONTENT}\n")
  endif()
endfunction()

check_builtin_exist(__builtin_clz HAVE_DECL___BUILTIN_CLZ)
check_builtin_exist(__builtin_clzl HAVE_DECL___BUILTIN_CLZL)
check_builtin_exist(__builtin_clzll HAVE_DECL___BUILTIN_CLZLL)

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/bpool-config.h.in ${CMAKE_CURRENT_SOURCE_DIR}/bpool-config.h)

########################## Generate version number #############################

SET(BPOOL_VERSION_MAJOR "2")
SET(BPOOL_VERSION_MINOR "2")
SET(BPOOL_VERSION_PATCH "0")

# Get the current abbreviated commit hash of the working branch
if(EXISTS "${CMAKE_SOURCE_DIR}/.git")
  execute_process(
      COMMAND git log -1 --format=%h
      WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
      OUTPUT_VARIABLE BPOOL_GIT_COMMIT_HASH
      OUTPUT_STRIP_TRAILING_WHITESPACE
  )
endif()

if(BPOOL_GIT_COMMIT_HASH)
  set(BPOOL_VERSION_PATCH "${BPOOL_VERSION_PATCH}-${BPOOL_GIT_COMMIT_HASH}")
endif()

set(BPOOL_VERSION_STR "${BPOOL_VERSION_MAJOR}.${BPOOL_VERSION_MINOR}.${BPOOL_VERSION_PATCH}")

message("-- version string: ${BPOOL_VERSION_STR}")

configure_file(${CMAKE_CURRENT_SOURCE_DIR}/bpool-version.h.in ${CMAKE_CURRENT_SOURCE_DIR}/bpool-version.h)


# package version of cpack
SET(CPACK_PACKAGE_VERSION_MAJOR "${BPOOL_VERSION_MAJOR}" PARENT_SCOPE)
SET(CPACK_PACKAGE_VERSION_MINOR "${BPOOL_VERSION_MINOR}" PARENT_SCOPE)
SET(CPACK_PACKAGE_VERSION_PATCH "${BPOOL_VERSION_PATCH}" PARENT_SCOPE)
