# zhengrr
# 2019-06-05 – 2021-10-09
# Unlicense

#[=======================================================================[.rst:
rrIncludeScript
---------------

提供若干宏，以便利引入第三方 CMake 脚本。
#]=======================================================================]

cmake_minimum_required(VERSION 3.17)
cmake_policy(VERSION 3.17)

include_guard()  # 3.10


#[=======================================================================[.rst:
.. command:: rr_include_conan_script

  检查、下载并包含 `conan.cmake` 脚本。

  .. code-block:: cmake

    rr_include_conan_script()

  参见：

  - `cmake-conan <https://github.com/conan-io/cmake-conan>`_
#]=======================================================================]
macro(rr_include_conan_script)
  # 检查、下载并包含 conan.cmake 脚本
  if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
    message(CHECK_START "Downloading conan.cmake script")  # 3.17
    # https://github.com/conan-io/cmake-conan/releases
    file(
      DOWNLOAD      "https://raw.githubusercontent.com/conan-io/cmake-conan/0140fdbe363abfc1993f579e8d711067ac4742e1/conan.cmake"
                    "${CMAKE_BINARY_DIR}/conan.cmake"
      TLS_VERIFY    ON
      EXPECTED_HASH SHA256=ddf0fafacf48b5c4912ecce5701c252532437c40277734cad5f4a8084470adbc
      SHOW_PROGRESS
      STATUS        _rr_include_conan_script_zStatus)
    # https://emn178.github.io/online-tools/sha256_checksum.html
    list(GET _rr_include_conan_script_zStatus 0 _rr_include_conan_script_nStatusCode)
    if(NOT _rr_include_conan_script_nStatusCode EQUAL 0)
      file(REMOVE "${CMAKE_BINARY_DIR}/conan.cmake")
      list(GET _rr_include_conan_script_zStatus 1 _rr_include_conan_script_sStatusText)
      message(CHECK_FAIL "failed: ${_rr_include_conan_script_sStatusText}")
    else()
      message(CHECK_PASS "done")
    endif()
  endif()
  include("${CMAKE_BINARY_DIR}/conan.cmake")

  # 默认将 Conan 参数隐藏到“高级”
  mark_as_advanced(
    CONAN_CMD)
endmacro()


#[=======================================================================[.rst:
.. command:: rr_run_conan_wcon

  依据惯例，运行 Conan。
  
  注意，该命令所指的“惯例”以本人（作者）便利为准，随时可能改变，不应用于生产环境。

  .. code-block:: cmake

    rr_run_conan_wcon(
      <requires>...
      [DEBUG]
      [RELEASE]
      [RELWITHDEBINFO]
      [MINSIZEREL]
      [INHERITED])

#]=======================================================================]
macro(rr_run_conan_wcon)
  set(_rr_run_conan_wcon_zOptKws DEBUG
                                 RELEASE
                                 RELWITHDEBINFO
                                 MINSIZEREL
                                 INHERITED)
  cmake_parse_arguments("_rr_run_conan_wcon_ARG" "${_rr_run_conan_wcon_zOptKws}" "" "" ${ARGN})

  # 描述需要的依赖库和生成器，生成 conanfile.txt
  conan_cmake_configure(
    REQUIRES   ${_rr_run_conan_wcon_ARG_UNPARSED_ARGUMENTS}
    GENERATORS cmake_find_package_multi)

  # 编译 MinSizeRel 和 RelWithDebInfo 较少用到，默认关闭、按需开启，以提升配置速度
  if(NOT _rr_run_conan_wcon_ARG_DEBUG AND
     NOT _rr_run_conan_wcon_ARG_RELEASE AND
     NOT _rr_run_conan_wcon_ARG_RELWITHDEBINFO AND
     NOT _rr_run_conan_wcon_ARG_MINSIZEREL AND
     NOT _rr_run_conan_wcon_ARG_INHERITED)
    set(_rr_run_conan_wcon_zConfigs "Debug" "Release")

  elseif(_rr_run_conan_wcon_ARG_INHERITED)
    if(DEFINED CMAKE_BUILD_TYPE)
      set(_rr_run_conan_wcon_zConfigs "${CMAKE_BUILD_TYPE}")
    else()
      set(_rr_run_conan_wcon_zConfigs ${CMAKE_CONFIGURATION_TYPES})
    endif()
  
  else()
    set(_rr_run_conan_wcon_zConfigs)
    if(_rr_run_conan_wcon_ARG_DEBUG)
      list(APPEND _rr_run_conan_wcon_zConfigs "Debug")
    endif()
    if(_rr_run_conan_wcon_ARG_RELEASE)
      list(APPEND _rr_run_conan_wcon_zConfigs "Release")
    endif()
    if(_rr_run_conan_wcon_ARG_RELWITHDEBINFO)
      list(APPEND _rr_run_conan_wcon_zConfigs "RelWithDebInfo")
    endif()
    if(_rr_run_conan_wcon_ARG_MINSIZEREL)
      list(APPEND _rr_run_conan_wcon_zConfigs "MinSizeRel")
    endif()
  endif()

  foreach(sConfig IN LISTS _rr_run_conan_wcon_zConfigs)
    # 检测环境
    conan_cmake_autodetect(
      zSettings
      BUILD_TYPE "${sConfig}")
    # 执行安装
    conan_cmake_install(
      PATH_OR_REFERENCE .
      BUILD             missing
      REMOTE            conancenter
      SETTINGS          ${zSettings}
      OUTPUT_QUIET)
  endforeach()

  # 将生成的 *[Cc]onfig.cmake 引入 CMake
  if(NOT "${CMAKE_CURRENT_BINARY_DIR}" IN_LIST CMAKE_PREFIX_PATH)
    list(APPEND CMAKE_PREFIX_PATH "${CMAKE_CURRENT_BINARY_DIR}")
  endif()
endmacro()


#[=======================================================================[.rst:
.. command:: rr_include_hunter_gate_script

  检查、下载、包含并初始化 `HunterGate.cmake` 脚本。

  .. code-block:: cmake

    rr_include_hunter_gate_script()

  参见：

  - `Hunter <https://github.com/cpp-pm/hunter>`_
#]=======================================================================]
macro(rr_include_hunter_gate_script)
  # 检查、下载并包含 HungerGate.cmake 脚本
  if(NOT EXISTS "${CMAKE_BINARY_DIR}/HunterGate.cmake")
    message(CHECK_START "Downloading HunterGate.cmake script")
    file(
      DOWNLOAD "https://raw.githubusercontent.com/cpp-pm/gate/master/cmake/HunterGate.cmake"
               "${CMAKE_BINARY_DIR}/HunterGate.cmake"
               SHOW_PROGRESS
      STATUS   _rr_include_hunter_gate_script_zStatus)
    list(GET _rr_include_hunter_gate_script_zStatus 0 _rr_include_hunter_gate_script_nStatusCode)
    if(NOT _rr_include_hunter_gate_script_nStatusCode EQUAL 0)
      file(REMOVE "${CMAKE_BINARY_DIR}/HunterGate.cmake")
      list(GET _rr_include_hunter_gate_script_zStatus 1 _rr_include_hunter_gate_script_sStatusText)
      message(CHECK_FAIL "failed: ${_rr_include_hunter_gate_script_sStatusText}")
    else()
      message(CHECK_PASS "done")
    endif()
  endif()
  include("${CMAKE_BINARY_DIR}/HunterGate.cmake")

  # 允许在子目录的 project 指令前使用 HunterGate
  if(NOT CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR AND PROJECT_NAME STREQUAL CMAKE_PROJECT_NAME)
    set(PROJECT_NAME)
  endif()

  # 默认启用“不要重复计算工具链标识”
  option(HUNTER_NO_TOOLCHAIN_ID_RECALCULATION "No Toolchain-ID recalculation" ON)

  # https://github.com/cpp-pm/hunter/releases
  HunterGate(
    URL "https://github.com/cpp-pm/hunter/archive/v0.23.314.tar.gz"
    SHA1 "95c47c92f68edb091b5d6d18924baabe02a6962a"
  )

  # 默认将 Hunter 参数隐藏到“高级”
  mark_as_advanced(
    HUNTER_CONFIGURATION_TYPES
    HUNTER_ENABLED
    HUNTER_MSVC_VCVARSALL
    HUNTER_NO_TOOLCHAIN_ID_RECALCULATION
    HUNTER_STATUS_DEBUG
    HUNTER_STATUS_PRINT
    HUNTER_TLS_VERIFY)
endmacro()
