function(PACK_SCHEMAS SCHEMAS LIB)
  set(SCHEMAS_HEADER "${CMAKE_BINARY_DIR}/api/schemas.hpp")

  file(WRITE "${SCHEMAS_HEADER}.tmp"
       "\
// Auto-generated by cmake script.\n\
\n\
#include <nlohmann/json.hpp>\n\
\n\
namespace api\n\
{\n
   using json = nlohmann::json;\n")

  foreach(SCHEMA ${SCHEMAS})
    get_filename_component(SCHEMA ${SCHEMA} REALPATH)
    if(NOT EXISTS ${SCHEMA})
      message(SEND_ERROR "Unable to locate schema: ${SCHEMA}")
    endif()
    string(REGEX REPLACE "(.*)/([^/]+).json" "\\2" VAR ${SCHEMA})
    string(MAKE_C_IDENTIFIER "${VAR}" VAR)
    file(APPEND "${SCHEMAS_HEADER}.tmp" "   static json ${VAR} = R\"-auto-generated-(")
    file(READ "${SCHEMA}" RAW)
    file(APPEND "${SCHEMAS_HEADER}.tmp" "${RAW}")
    file(APPEND "${SCHEMAS_HEADER}.tmp" ")-auto-generated-\"_json;\n\n")
  endforeach()

  file(APPEND "${SCHEMAS_HEADER}.tmp" "}\n")

  configure_file("${SCHEMAS_HEADER}.tmp" "${SCHEMAS_HEADER}" COPYONLY)
  add_library(${LIB} "${SCHEMAS_HEADER}")
  target_include_directories(${LIB} PUBLIC "${CMAKE_BINARY_DIR}/api")
  target_link_libraries(${LIB} nlohmann_json::nlohmann_json)
  set_target_properties(${LIB} PROPERTIES LINKER_LANGUAGE CXX)
endfunction(PACK_SCHEMAS)
