|
| 1 | +# This file contains some useful macros for use by packages using |
| 2 | +# yggdrasil-rapidjson as a dependency via find_package |
| 3 | + |
| 4 | +macro(include_yggdrasil_rapidjson_macros) |
| 5 | + if(NOT COMMAND yggdrasil_rapidjson_options) |
| 6 | + set(YGGDRASIL_RAPIDJSON_REPO_DIR "" CACHE PATH "Existing directory containing the yggdrasil-rapidjson repository") |
| 7 | + set(YGGDRASIL_RAPIDJSON_REPO_BUILD_DIR "") |
| 8 | + set(YGGDRASIL_RAPIDJSON_MACROS_FILE "") |
| 9 | + |
| 10 | + if(YggdrasilRapidJSON_FOUND) |
| 11 | + message(FATAL_ERROR "YggdrasilRapidJSON_FOUND, but yggdrasil_rapidjson_options not defined") |
| 12 | + elseif(NOT YGGDRASIL_RAPIDJSON_REPO_DIR) |
| 13 | + find_file( |
| 14 | + YGGDRASIL_RAPIDJSON_CONFIG_FILE |
| 15 | + YggdrasilRapidJSONConfig.cmake |
| 16 | + PATHS "${YggdrasilRapidJSON_DIR}" |
| 17 | + ) |
| 18 | + if(YGGDRASIL_RAPIDJSON_CONFIG_FILE) |
| 19 | + message(DEBUG "Located YggdrasilRapidJSON config file at ${YGGDRASIL_RAPIDJSON_CONFIG_FILE}") |
| 20 | + cmake_path( |
| 21 | + GET YGGDRASIL_RAPIDJSON_CONFIG_FILE |
| 22 | + PARENT_PATH YGGDRASIL_RAPIDJSON_MACROS_FILE |
| 23 | + ) |
| 24 | + cmake_path( |
| 25 | + APPEND YGGDRASIL_RAPIDJSON_MACROS_FILE |
| 26 | + YggdrasilRapidJSONMacros.cmake |
| 27 | + ) |
| 28 | + else() |
| 29 | + find_file( |
| 30 | + YGGDRASIL_RAPIDJSON_CONFIG_FILE |
| 31 | + YggdrasilRapidJSONConfig.cmake.in |
| 32 | + PATHS "${YggdrasilRapidJSON_DIR}" |
| 33 | + ) |
| 34 | + if(YGGDRASIL_RAPIDJSON_CONFIG_FILE) |
| 35 | + message(DEBUG "Located YggdrasilRapidJSON config template file at ${YGGDRASIL_RAPIDJSON_CONFIG_FILE}") |
| 36 | + cmake_path( |
| 37 | + GET YGGDRASIL_RAPIDJSON_CONFIG_FILE |
| 38 | + PARENT_PATH YGGDRASIL_RAPIDJSON_REPO_DIR |
| 39 | + ) |
| 40 | + endif() |
| 41 | + endif() |
| 42 | + unset(YGGDRASIL_RAPIDJSON_CONFIG_FILE) |
| 43 | + endif() |
| 44 | + if(YGGDRASIL_RAPIDJSON_REPO_DIR) |
| 45 | + if(NOT EXISTS "${YGGDRASIL_RAPIDJSON_REPO_DIR}") |
| 46 | + message(FATAL_ERROR "Directory specified by option YGGDRASIL_RAPIDJSON_REPO_DIR=\"${YGGDRASIL_RAPIDJSON_REPO_DIR}\" does not exist") |
| 47 | + endif() |
| 48 | + if(NOT IS_ABSOLUTE "${YGGDRASIL_RAPIDJSON_REPO_DIR}") |
| 49 | + cmake_path( |
| 50 | + ABSOLUTE_PATH YGGDRASIL_RAPIDJSON_REPO_DIR |
| 51 | + NORMALIZE |
| 52 | + ) |
| 53 | + endif() |
| 54 | + include(FetchContent) |
| 55 | + FetchContent_Declare( |
| 56 | + YggdrasilRapidJSON |
| 57 | + SOURCE_DIR "${YGGDRASIL_RAPIDJSON_REPO_DIR}" |
| 58 | + EXCLUDE_FROM_ALL |
| 59 | + OVERRIDE_FIND_PACKAGE |
| 60 | + ) |
| 61 | + FetchContent_Populate(YggdrasilRapidJSON) |
| 62 | + cmake_path( |
| 63 | + APPEND YGGDRASIL_RAPIDJSON_REPO_DIR "_build_for_${PROJECT_NAME}" |
| 64 | + OUTPUT_VARIABLE YGGDRASIL_RAPIDJSON_REPO_BUILD_DIR |
| 65 | + ) |
| 66 | + elseif(NOT YGGDRASIL_RAPIDJSON_MACROS_FILE) |
| 67 | + message(WARNING "Could not locate the YggdrasilRapidJSON package via find_file (YggdrasilRapidJSON_DIR=${YggdrasilRapidJSON_DIR}), importing it from github as an external project...") |
| 68 | + include(FetchContent) |
| 69 | + FetchContent_Declare( |
| 70 | + YggdrasilRapidJSON |
| 71 | + GIT_REPOSITORY https://github.com/cropsinsilico/yggdrasil-rapidjson.git |
| 72 | + GIT_TAG origin/yggdrasil |
| 73 | + EXCLUDE_FROM_ALL |
| 74 | + OVERRIDE_FIND_PACKAGE |
| 75 | + ) |
| 76 | + FetchContent_Populate(YggdrasilRapidJSON) |
| 77 | + set(YGGDRASIL_RAPIDJSON_REPO_DIR "${yggdrasilrapidjson_SOURCE_DIR}") |
| 78 | + set(YGGDRASIL_RAPIDJSON_REPO_BUILD_DIR "${yggdrasilrapidjson_BUILD_DIR}") |
| 79 | + endif() |
| 80 | + if(NOT YGGDRASIL_RAPIDJSON_MACROS_FILE) |
| 81 | + cmake_path( |
| 82 | + APPEND YGGDRASIL_RAPIDJSON_REPO_DIR |
| 83 | + YggdrasilRapidJSONMacros.cmake |
| 84 | + OUTPUT_VARIABLE YGGDRASIL_RAPIDJSON_MACROS_FILE |
| 85 | + ) |
| 86 | + endif() |
| 87 | + message(STATUS "Including YggdrasilRapidJSON macros from \"${YGGDRASIL_RAPIDJSON_MACROS_FILE}\" (REPO_DIR=${YGGDRASIL_RAPIDJSON_REPO_DIR})") |
| 88 | + include("${YGGDRASIL_RAPIDJSON_MACROS_FILE}") |
| 89 | + endif() |
| 90 | +endmacro() |
| 91 | + |
| 92 | +macro(find_yggdrasil_rapidjson) |
| 93 | + include_yggdrasil_rapidjson_macros() |
| 94 | + if(YGGDRASIL_RAPIDJSON_REPO_DIR) |
| 95 | + foreach(suffix BUILD_EXAMPLES BUILD_TESTS BUILD_DOC) |
| 96 | + if(NOT YGGDRASIL_RAPIDJSON_${suffix}) |
| 97 | + set(YGGDRASIL_RAPIDJSON_${suffix} OFF) |
| 98 | + endif() |
| 99 | + endforeach() |
| 100 | + add_subdirectory( |
| 101 | + "${YGGDRASIL_RAPIDJSON_REPO_DIR}" |
| 102 | + "${YGGDRASIL_RAPIDJSON_REPO_BUILD_DIR}" |
| 103 | + EXCLUDE_FROM_ALL |
| 104 | + ) |
| 105 | + # Required for cmake <3.28 when EXCLUDE_FROM_ALL was added to |
| 106 | + # FetchContent_Declare |
| 107 | + set_property( |
| 108 | + DIRECTORY "${YGGDRASIL_RAPIDJSON_REPO_DIR}" |
| 109 | + PROPERTY EXCLUDE_FROM_ALL ON |
| 110 | + ) |
| 111 | + set(YggdrasilRapidJSON_VERSION "${YGGDRASIL_RAPIDJSON_VERSION}") |
| 112 | + else() |
| 113 | + find_package(YggdrasilRapidJSON REQUIRED) |
| 114 | + endif() |
| 115 | + if(YggdrasilRapidJSON_VERSION VERSION_LESS "1.1.0.6") |
| 116 | + yggdrasil_rapidjson_options_config(LOCAL) |
| 117 | + yggdrasil_rapidjson_target_config(YggdrasilRapidJSON INTERFACE LOCAL) |
| 118 | + endif() |
| 119 | + |
| 120 | +endmacro() |
0 commit comments