1# SPDX-License-Identifier: Apache-2.0
2
3find_program(GEN_KOBJECT_LIST NAMES gen_kobject_list gen_kobject_list.py PATHS ${ZEPHYR_BASE}/scripts/build)
4message(STATUS "Found gen_kobject_list: ${GEN_KOBJECT_LIST}")
5if(GEN_KOBJECT_LIST MATCHES "\.py$")
6  set(GEN_KOBJECT_LIST_INTERPRETER ${PYTHON_EXECUTABLE})
7endif()
8
9# Invokes gen_kobject_list.py with the given SCRIPT_ARGS, creating a TARGET that depends on the
10# script's OUTPUTS
11function(gen_kobject_list)
12  cmake_parse_arguments(PARSE_ARGV 0 arg
13    ""
14    "TARGET"
15    "OUTPUTS;SCRIPT_ARGS;INCLUDES;DEPENDS"
16  )
17  foreach(include ${arg_INCLUDES})
18    list(APPEND arg_SCRIPT_ARGS --include-subsystem-list ${include})
19  endforeach()
20  add_custom_command(
21    OUTPUT ${arg_OUTPUTS}
22    COMMAND
23      ${GEN_KOBJECT_LIST_INTERPRETER}
24      ${GEN_KOBJECT_LIST}
25      ${arg_SCRIPT_ARGS}
26      $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
27    DEPENDS
28      ${arg_DEPENDS}
29      ${GEN_KOBJECT_LIST}
30    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
31    )
32  add_custom_target(${arg_TARGET} DEPENDS ${arg_OUTPUTS})
33endfunction()
34
35# Generates a gperf header file named OUTPUT using the symbols found in the KERNEL_TARGET's output
36# binary. INCLUDES is a list of JSON files defining kernel subsystems and sockets.
37function(gen_kobject_list_gperf)
38  cmake_parse_arguments(PARSE_ARGV 0 arg
39    ""
40    "TARGET;OUTPUT;KERNEL_TARGET"
41    "INCLUDES;DEPENDS"
42  )
43  gen_kobject_list(
44    TARGET ${arg_TARGET}
45    OUTPUTS ${arg_OUTPUT}
46    SCRIPT_ARGS
47      --kernel $<TARGET_FILE:${arg_KERNEL_TARGET}>
48      --gperf-output ${arg_OUTPUT}
49    INCLUDES ${arg_INCLUDES}
50    DEPENDS
51      ${arg_DEPENDS}
52      ${arg_KERNEL_TARGET}
53    )
54endfunction()
55
56# Generates header files describing the kernel subsystems defined by the JSON files in INCLUDES. The
57# variable named by GEN_DIR_OUT_VAR is set to the directory containing the header files.
58function(gen_kobject_list_headers)
59  cmake_parse_arguments(PARSE_ARGV 0 arg
60    ""
61    "GEN_DIR_OUT_VAR"
62    "INCLUDES;DEPENDS"
63  )
64  if (PROJECT_BINARY_DIR)
65    set(gen_dir ${PROJECT_BINARY_DIR}/include/generated/zephyr)
66  else ()
67    set(gen_dir ${CMAKE_BINARY_DIR}/include/generated/zephyr)
68  endif ()
69
70  set(KOBJ_TYPES ${gen_dir}/kobj-types-enum.h)
71  set(KOBJ_OTYPE ${gen_dir}/otype-to-str.h)
72  set(KOBJ_SIZE ${gen_dir}/otype-to-size.h)
73
74  file(MAKE_DIRECTORY ${gen_dir})
75
76  gen_kobject_list(
77    TARGET ${KOBJ_TYPES_H_TARGET}
78    OUTPUTS ${KOBJ_TYPES} ${KOBJ_OTYPE} ${KOBJ_SIZE}
79    SCRIPT_ARGS
80      --kobj-types-output ${KOBJ_TYPES}
81      --kobj-otype-output ${KOBJ_OTYPE}
82      --kobj-size-output ${KOBJ_SIZE}
83    INCLUDES ${arg_INCLUDES}
84    DEPENDS
85      ${arg_DEPENDS}
86      ${arg_KERNEL_TARGET}
87  )
88
89  if(arg_GEN_DIR_OUT_VAR)
90    cmake_path(GET gen_dir PARENT_PATH gen_dir)
91    set(${arg_GEN_DIR_OUT_VAR} ${gen_dir} PARENT_SCOPE)
92  endif()
93endfunction ()
94