1# Copyright (c) 2024 Arduino SA
2# SPDX-License-Identifier: Apache-2.0
3
4# Simple second stage filter for YAML generation, used when generator
5# expressions have been used for some of the data and the conversion to
6# YAML needs to happen after cmake has completed processing.
7#
8# This scripts expects as input:
9# - JSON_FILE: the name of the input file, in JSON format, that contains
10#              the expanded generator expressions.
11# - YAML_FILE: the name of the final output YAML file.
12# - TEMP_FILES: a list of temporary files that need to be removed after
13#               the conversion is done.
14#
15# This script loads the Zephyr yaml module and reuses its `to_yaml()`
16# function to convert the fully expanded JSON content to YAML, taking
17# into account the special format that was used to store lists.
18# Temporary files are then removed.
19
20cmake_minimum_required(VERSION 3.20.0)
21
22set(ZEPHYR_BASE ${CMAKE_CURRENT_LIST_DIR}/../)
23list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/modules")
24include(yaml)
25
26file(READ ${JSON_FILE} json_content)
27to_yaml("${json_content}" 0 yaml_out TRUE)
28file(WRITE ${YAML_FILE} "${yaml_out}")
29
30# Remove unused temporary files. JSON_FILE needs to be kept, or the
31# build system will complain there is no rule to rebuild it
32list(REMOVE_ITEM TEMP_FILES ${JSON_FILE})
33foreach(file ${TEMP_FILES})
34  file(REMOVE ${file})
35endforeach()
36