rapids_json_parse#

Added in version v27.02.00.

Extract the members of a JSON object into CMake variables.

rapids_json_parse(<prefix>
                  (FILE <file> | STRING <json>)
                  [KEY <member|index>...]
                  [BASE_DIR <dir>]
                  [VALUES <key>...]
                  [LISTS <key>...]
                  [PATHS <key>...]
                  [OBJECTS <key>...]
)

Reads a JSON object and stores the members that the caller has asked for in variables named <prefix>_<key>. The layout of the JSON document is described by the keywords given to the call, in the same way cmake_parse_arguments describes the layout of command line arguments. The members of each record of an array can be parsed by giving the record to this function as STRING, after extracting it with string(JSON ... GET_RAW).

<prefix>

Prefix of the variables that are set in the calling scope. All variables that could be set by this call are unset first, so it is safe to re-use a <prefix> for multiple invocations.

FILE

Path to the JSON file to read. If relative, it is evaluated relative to CMAKE_CURRENT_LIST_DIR. The file is added to the CMAKE_CONFIGURE_DEPENDS directory property so that editing it re-runs the CMake configuration. Mutually exclusive with STRING.

STRING

String containing the JSON document to read. Mutually exclusive with FILE.

KEY

Sequence of object members and array indices, in the same form as accepted by string(JSON), that locates the object to parse. When not provided the document itself is parsed.

BASE_DIR

An absolute path of the directory that relative entries of PATHS are evaluated relative to.

VALUES

Members that hold a single value. The value is stored as is.

LISTS

Members that hold an array. The array is stored as a CMake list with an entry for each element of the array.

Only supports string, number, or boolean elements.

PATHS

Members that hold a path, or an array of paths. A member that is neither a string nor an array is an error. The result is always a CMake list of absolute paths, which has a single entry when the member is a string. Absolute paths are used as is, and relative paths are made absolute by:

  • BASE_DIR when provided.

  • When FILE is used, the directory containing the JSON file.

  • When STRING is used, the CMAKE_CURRENT_LIST_DIR of the caller.

Paths are not normalized, so entries such as ../a.cpp keep their .. component.

OBJECTS

Members that hold an object. The object is stored as a JSON string that can be given to another call of rapids_json_parse as the STRING argument.

A member can be declared by a single keyword only. Members that exist in the JSON object, but are not declared, are ignored. All declared members are optional, callers that need a member should check that its variable is defined.

Example on how to properly use rapids_json_parse():

# kernel.json:
# {
#   "name": "scan_@type@",
#   "input": "scan.cu.in",
#   "headers": [ "<common.hpp>", "<scan.hpp>" ],
#   "output_dir": "generated"
# }
rapids_json_parse(_kernel FILE kernel.json
                  VALUES name output_dir
                  PATHS input
                  LISTS headers
)
message(STATUS "input is ${_kernel_input}")
message(STATUS "headers are ${_kernel_headers}")

Result Variables#

<prefix>_<key>

Set for each declared member that exists in the JSON object. Members that don’t exist leave the variable unset.

Limitations#

A ; is supported in the STRING document and in VALUES and OBJECTS members. It is not supported in the elements of LISTS and PATHS members, since they are returned as CMake lists.