1# SPDX-License-Identifier: Apache-2.0
2#
3# Copyright (c) 2023, Nordic Semiconductor ASA
4
5# This CMake module works together with the list_hardware.py script to obtain
6# all archs and SoC implementations defined in the Zephyr build system.
7#
8# The result from list_hardware.py is then used to generate Kconfig files for
9# the build system.
10#
11# The following files are generated in '<kconfig-binary-dir>/soc'
12# - Kconfig.defconfig: Contains references to SoC defconfig files for Zephyr integration.
13# - Kconfig: Contains references to regular SoC Kconfig files for Zephyr integration.
14# - Kconfig.soc: Contains references to generic SoC Kconfig files.
15# - Kconfig.sysbuild: Contains references to SoC sysbuild Kconfig files.
16#
17# The following file is generated in '<kconfig-binary-dir>/arch'
18# - Kconfig: Contains references to regular arch Kconfig files for Zephyr integration.
19
20include_guard(GLOBAL)
21
22if(NOT HWMv2)
23  return()
24endif()
25
26# Internal helper function for creation of Kconfig files.
27function(kconfig_gen bin_dir file dirs comment)
28  set(kconfig_header "# Load ${comment} descriptions.\n")
29  set(kconfig_file ${KCONFIG_BINARY_DIR}/${bin_dir}/${file})
30  file(WRITE ${kconfig_file} "${kconfig_header}")
31
32  foreach(dir ${dirs})
33    cmake_path(CONVERT "${dir}" TO_CMAKE_PATH_LIST dir)
34    file(APPEND ${kconfig_file} "osource \"${dir}/${file}\"\n")
35  endforeach()
36endfunction()
37
38# 'SOC_ROOT' and 'ARCH_ROOT' are prioritized lists of directories where their
39# implementations may be found. It always includes ${ZEPHYR_BASE}/[arch|soc]
40# at the lowest priority.
41list(APPEND SOC_ROOT ${ZEPHYR_BASE})
42list(APPEND ARCH_ROOT ${ZEPHYR_BASE})
43
44list(TRANSFORM ARCH_ROOT PREPEND "--arch-root=" OUTPUT_VARIABLE arch_root_args)
45list(TRANSFORM SOC_ROOT PREPEND "--soc-root=" OUTPUT_VARIABLE soc_root_args)
46
47execute_process(COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_BASE}/scripts/list_hardware.py
48                ${arch_root_args} ${soc_root_args}
49                --archs --socs
50                --cmakeformat={TYPE}\;{NAME}\;{DIR}\;{HWM}
51                OUTPUT_VARIABLE ret_hw
52                ERROR_VARIABLE err_hw
53                RESULT_VARIABLE ret_val
54)
55if(ret_val)
56  message(FATAL_ERROR "Error listing hardware.\nError message: ${err_hw}")
57endif()
58
59set(kconfig_soc_source_dir)
60
61while(TRUE)
62  string(FIND "${ret_hw}" "\n" idx REVERSE)
63  math(EXPR start "${idx} + 1")
64  string(SUBSTRING "${ret_hw}" ${start} -1 line)
65  string(SUBSTRING "${ret_hw}" 0 ${idx} ret_hw)
66
67  cmake_parse_arguments(HWM "" "TYPE" "" ${line})
68  if(HWM_TYPE STREQUAL "arch")
69    cmake_parse_arguments(ARCH_V2 "" "NAME;DIR" "" ${line})
70
71    list(APPEND kconfig_arch_source_dir "${ARCH_V2_DIR}")
72    list(APPEND ARCH_V2_NAME_LIST ${ARCH_V2_NAME})
73    string(TOUPPER "${ARCH_V2_NAME}" ARCH_V2_NAME_UPPER)
74    set(ARCH_V2_${ARCH_V2_NAME_UPPER}_DIR ${ARCH_V2_DIR})
75  elseif(HWM_TYPE MATCHES "^soc|^series|^family")
76    cmake_parse_arguments(SOC_V2 "" "NAME;HWM" "DIR" ${line})
77
78    list(APPEND kconfig_soc_source_dir "${SOC_V2_DIR}")
79    string(TOUPPER "${SOC_V2_NAME}" SOC_V2_NAME_UPPER)
80    string(TOUPPER "${HWM_TYPE}" HWM_TYPE_UPPER)
81
82    if(HWM_TYPE STREQUAL "soc")
83      # We support both SOC_foo_DIR and SOC_FOO_DIR.
84      set(SOC_${SOC_V2_NAME}_DIRECTORIES ${SOC_V2_DIR})
85      set(SOC_${SOC_V2_NAME_UPPER}_DIRECTORIES ${SOC_V2_DIR})
86      list(GET SOC_V2_DIR 0 SOC_${SOC_V2_NAME}_DIR)
87      list(GET SOC_V2_DIR 0 SOC_${SOC_V2_NAME_UPPER}_DIR)
88    else()
89      # We support both SOC_series_foo_DIR and SOC_SERIES_FOO_DIR (and family /  FAMILY).
90      set(SOC_${HWM_TYPE}_${SOC_V2_NAME}_DIR ${SOC_V2_DIR})
91      set(SOC_${HWM_TYPE_UPPER}_${SOC_V2_NAME_UPPER}_DIR ${SOC_V2_DIR})
92    endif()
93  endif()
94
95  if(idx EQUAL -1)
96    break()
97  endif()
98endwhile()
99list(REMOVE_DUPLICATES kconfig_soc_source_dir)
100
101# Support multiple ARCH_ROOT, SOC_ROOT and BOARD_ROOT
102kconfig_gen("arch" "Kconfig"             "${kconfig_arch_source_dir}" "Zephyr Arch Kconfig")
103kconfig_gen("soc"  "Kconfig.defconfig"   "${kconfig_soc_source_dir}"  "Zephyr SoC defconfig")
104kconfig_gen("soc"  "Kconfig"             "${kconfig_soc_source_dir}"  "Zephyr SoC Kconfig")
105kconfig_gen("soc"  "Kconfig.soc"         "${kconfig_soc_source_dir}"  "SoC Kconfig")
106kconfig_gen("soc"  "Kconfig.sysbuild"    "${kconfig_soc_source_dir}"  "Sysbuild SoC Kconfig")
107kconfig_gen("boards" "Kconfig.defconfig" "${BOARD_DIRECTORIES}"       "Zephyr board defconfig")
108kconfig_gen("boards" "Kconfig.${BOARD}"  "${BOARD_DIRECTORIES}"       "board Kconfig")
109kconfig_gen("boards" "Kconfig"           "${BOARD_DIRECTORIES}"       "Zephyr board Kconfig")
110kconfig_gen("boards" "Kconfig.sysbuild"  "${BOARD_DIRECTORIES}"       "Sysbuild board Kconfig")
111
112# Clear variables created by cmake_parse_arguments
113unset(SOC_V2_NAME)
114unset(SOC_V2_DIR)
115unset(SOC_V2_HWM)
116unset(ARCH_V2_NAME)
117unset(ARCH_V2_DIR)
118