The open source OpenXR runtime
1# Copyright 2025, NVIDIA CORPORATION.
2# SPDX-License-Identifier: BSL-1.0
3
4# JSON merge function: merges multiple JSON files into one output file
5#
6# To use this function in your CMakeLists.txt:
7# include(${CMAKE_SOURCE_DIR}/scripts/CMakeLists.txt)
8#
9# Usage:
10# merge_json_files(
11# OUTPUT <output_file>
12# SOURCES <json_file1> <json_file2> ...
13# [ALLOW_OVERWRITE]
14# [IGNORE_SCHEMA]
15# )
16#
17# Arguments:
18# OUTPUT - Output file path (required)
19# SOURCES - List of JSON files to merge (required)
20# ALLOW_OVERWRITE - Optional flag to allow duplicate keys
21# IGNORE_SCHEMA - Optional flag to ignore "$schema" field in output
22#
23# The function automatically:
24# - Sorts input files alphabetically for consistent results
25# - Tracks dependencies so output is regenerated when inputs change
26# - Errors on duplicate keys unless ALLOW_OVERWRITE is specified
27#
28# Example:
29# merge_json_files(
30# OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/merged.json"
31# SOURCES file1.json file2.json file3.json
32# IGNORE_SCHEMA
33# )
34
35function(merge_json_files)
36 set(options ALLOW_OVERWRITE IGNORE_SCHEMA)
37 set(oneValueArgs OUTPUT)
38 set(multiValueArgs SOURCES)
39
40 cmake_parse_arguments(
41 MERGE_JSON "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}
42 )
43
44 if(NOT MERGE_JSON_OUTPUT)
45 message(FATAL_ERROR "merge_json_files: OUTPUT argument is required")
46 endif()
47
48 if(NOT MERGE_JSON_SOURCES)
49 message(FATAL_ERROR "merge_json_files: SOURCES argument is required")
50 endif()
51
52 # Build command arguments
53 set(MERGE_COMMAND ${PYTHON_EXECUTABLE}
54 ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/merge_json.py -o
55 "${MERGE_JSON_OUTPUT}"
56 )
57
58 if(MERGE_JSON_ALLOW_OVERWRITE)
59 list(APPEND MERGE_COMMAND --allow-overwrite)
60 endif()
61
62 if(MERGE_JSON_IGNORE_SCHEMA)
63 list(APPEND MERGE_COMMAND --ignore-schema)
64 endif()
65
66 list(APPEND MERGE_COMMAND ${MERGE_JSON_SOURCES})
67
68 # Create custom command with proper dependencies
69 add_custom_command(
70 OUTPUT "${MERGE_JSON_OUTPUT}"
71 COMMAND ${MERGE_COMMAND}
72 VERBATIM
73 DEPENDS ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/merge_json.py
74 ${MERGE_JSON_SOURCES}
75 COMMENT "Merging JSON files into ${MERGE_JSON_OUTPUT}"
76 )
77endfunction()