1# Copyright (c) 2022 The Chromium OS Authors
2# SPDX-License-Identifier: Apache-2.0
3
4# Generates USB-C VIF policies in XML format from device tree.
5set(gen_vif_script ${ZEPHYR_BASE}/scripts/generate_usb_vif/generate_vif.py)
6set(dts_compatible usb-c-connector)
7set(vif_xml vif.xml)
8set(board_vif_xml ${BOARD}_${vif_xml})
9set(vif_out ${PROJECT_BINARY_DIR}/${vif_xml})
10
11set(cmd_gen_vif ${PYTHON_EXECUTABLE} ${gen_vif_script}
12        --edt-pickle ${EDT_PICKLE}
13        --compatible ${dts_compatible}
14        --vif-out ${vif_out}
15        )
16
17if (CONFIG_GENVIF_INPUT_VIF_XML_PATH)
18    if (IS_ABSOLUTE ${CONFIG_GENVIF_INPUT_VIF_XML_PATH})
19        if (EXISTS ${CONFIG_GENVIF_INPUT_VIF_XML_PATH})
20            set(vif_source_xml ${CONFIG_GENVIF_INPUT_VIF_XML_PATH})
21        endif ()
22    elseif (EXISTS ${APPLICATION_CONFIG_DIR}/${CONFIG_GENVIF_INPUT_VIF_XML_PATH})
23        set(vif_source_xml ${APPLICATION_CONFIG_DIR}/${CONFIG_GENVIF_INPUT_VIF_XML_PATH})
24    endif ()
25else ()
26    if (EXISTS ${APPLICATION_CONFIG_DIR}/boards/${board_vif_xml})
27        set(vif_source_xml ${APPLICATION_CONFIG_DIR}/boards/${board_vif_xml})
28    elseif (EXISTS ${APPLICATION_CONFIG_DIR}/${vif_xml})
29        set(vif_source_xml ${APPLICATION_CONFIG_DIR}/${vif_xml})
30    elseif (EXISTS ${BOARD_DIR}/${vif_xml})
31        set(vif_source_xml ${BOARD_DIR}/${vif_xml})
32    endif ()
33endif ()
34
35if (DEFINED vif_source_xml)
36    list(APPEND cmd_gen_vif --vif-source-xml ${vif_source_xml})
37else ()
38    if (CONFIG_GENVIF_INPUT_VIF_XML_PATH)
39        message(FATAL_ERROR "Incorrect VIF source XML file path. To fix specify"
40                " correct XML file path at 'CONFIG_GENVIF_INPUT_VIF_XML_PATH'.")
41    else ()
42        message(FATAL_ERROR "No VIF source XML file found. To fix, create"
43                " '${board_vif_xml}' in 'boards' directory of application"
44                " directory, or create '${vif_xml}' file in application"
45                " directory or board directory, or supply a custom XML VIF path"
46                " using 'CONFIG_GENVIF_INPUT_VIF_XML_PATH'.")
47    endif ()
48endif ()
49
50add_custom_command(
51        OUTPUT ${vif_xml}
52        DEPENDS ${EDT_PICKLE}
53        COMMENT "Generating XML file at zephyr/vif.xml"
54        COMMAND ${cmd_gen_vif}
55)
56
57add_custom_target(gen_vif ALL DEPENDS ${vif_xml})
58