Skip to content

Commit 20966d5

Browse files
committed
cmake: Add function to convert a yaml file to a config file
Add a way to convert a user supplied yaml file to a configuration file. For networking, a C header file is generated which can then be included to the application. Add a cmake function that calls the generate config macros with suitable values for networking needs. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 0f5e03f commit 20966d5

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

cmake/modules/extensions.cmake

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ include(CheckCXXCompilerFlag)
1616
# 1.2. zephyr_library_*
1717
# 1.2.1 zephyr_interface_library_*
1818
# 1.3. generate_inc_*
19+
# 1.3.1 generate_config_*
1920
# 1.4. board_*
2021
# 1.5. Misc.
2122
# 2. Kconfig-aware extensions
@@ -739,6 +740,79 @@ function(generate_inc_file_for_target
739740
generate_inc_file_for_gen_target(${target} ${source_file} ${generated_file} ${generated_target_name} ${ARGN})
740741
endfunction()
741742

743+
# 1.3.1 generate_config_*
744+
745+
# These functions are needed if a configuration file is generated
746+
# from a user supplied yaml file.
747+
#
748+
function(generate_config_file
749+
source_file # The yaml source file to be converted to config data
750+
generated_file # The generated file
751+
yaml_to_config_script # Script that generates the config
752+
)
753+
add_custom_command(
754+
OUTPUT ${generated_file}
755+
COMMAND
756+
${PYTHON_EXECUTABLE}
757+
${yaml_to_config_script}
758+
${ARGN} # Extra arguments are passed to the script
759+
< ${source_file}
760+
> ${generated_file}
761+
DEPENDS ${source_file}
762+
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
763+
)
764+
endfunction()
765+
766+
function(generate_config_file_for_gen_target
767+
target # The cmake target that depends on the generated file
768+
source_file # The yaml source file to be converted to config data
769+
generated_file # The generated file
770+
gen_target # The generated file target we depend on
771+
yaml_to_config_script # Script that generates the config
772+
# Any additional arguments are passed on to script
773+
)
774+
775+
generate_config_file(${source_file} ${generated_file} ${yaml_to_config_script} ${ARGN})
776+
777+
# Ensure 'generated_file' is generated before 'target' by creating a
778+
# dependency between the two targets
779+
780+
add_dependencies(${target} ${gen_target})
781+
endfunction()
782+
783+
function(generate_config_file_for_target
784+
target # The cmake target that depends on the generated file
785+
source_file # The yaml source file to be converted to config data
786+
generated_file # The generated file
787+
yaml_to_config_script # Script that generates the config
788+
# Any additional arguments are passed on to script
789+
)
790+
791+
# Ensure 'generated_file' is generated before 'target' by creating a
792+
# 'custom_target' for it and setting up a dependency between the two
793+
# targets
794+
795+
# But first create a unique name for the custom target
796+
generate_unique_target_name_from_filename(${generated_file} generated_target_name)
797+
798+
add_custom_target(${generated_target_name} DEPENDS ${generated_file})
799+
generate_config_file_for_gen_target(${target} ${source_file} ${generated_file}
800+
${generated_target_name} ${yaml_to_config_script} ${ARGN})
801+
endfunction()
802+
803+
function(network_generate_config_file_for_target
804+
target # The cmake target that depends on the generated file
805+
source_file # The yaml source file to be converted to config data
806+
)
807+
808+
set(generated_file ${ZEPHYR_BINARY_DIR}/include/generated/network_config.inc)
809+
set(yaml_to_config_script ${ZEPHYR_BASE}/scripts/net/net-yaml-config.py)
810+
set(yaml_schema_file ${ZEPHYR_BASE}/scripts/schemas/network-configuration-schema.yml)
811+
812+
generate_config_file_for_target(${target} ${source_file} ${generated_file}
813+
${yaml_to_config_script} ${yaml_schema_file})
814+
endfunction()
815+
742816
# 1.4. board_*
743817
#
744818
# This section is for extensions related to Zephyr board handling.

0 commit comments

Comments
 (0)