1#
2# Copyright 2020, Data61
3# Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4# ABN 41 687 119 230.
5#
6# This software may be distributed and modified according to the terms of
7# the BSD 2-Clause license. Note that NO WARRANTY is provided.
8# See "LICENSE_BSD2.txt" for details.
9#
10# @TAG(DATA61_BSD)
11#
12
13cmake_minimum_required(VERSION 3.8.2)
14
15function(get_device_list var device_type platform)
16    get_filename_component(platsupport_tools ${PLATSUPPORT_HELPERS} DIRECTORY)
17    find_file(
18        ${platform}_${device_type}_LIST "${platform}.yaml"
19        PATHS "${platsupport_tools}/device_lists/${device_type}"
20        CMAKE_FIND_ROOT_PATH_BOTH
21    )
22    set(${var} ${${platform}_${device_type}_LIST} PARENT_SCOPE)
23endfunction()
24
25# List of helper CMake functions for libplatsupport
26function(gen_device_header device_type platform)
27    get_filename_component(platsupport_tools ${PLATSUPPORT_HELPERS} DIRECTORY)
28    get_device_list(dev_list_file ${device_type} ${platform})
29    if("${dev_list_file}" STREQUAL "${platform}_${device_type}_LIST-NOTFOUND")
30        # Exit silently...
31        return()
32    endif()
33    set(header_dir "${CMAKE_CURRENT_BINARY_DIR}/${device_type}/platsupport")
34    set(header_file "${header_dir}/${device_type}_list.h")
35    set(DEVICE_GEN_PATH "${platsupport_tools}/device_header_gen.py")
36    set(dev_header_deps ${DEVICE_GEN_PATH} ${dev_list_file})
37    check_outfile_stale(
38        regen
39        ${header_file}
40        dev_header_deps
41        "${CMAKE_CURRENT_BINARY_DIR}/${device_type}.cmd"
42    )
43    if(regen)
44        file(MAKE_DIRECTORY ${header_dir})
45        execute_process(
46            COMMAND
47                ${PYTHON3} "${DEVICE_GEN_PATH}" --device-list "${dev_list_file}" --device-type
48                "${device_type}" --header-out "${header_file}"
49            INPUT_FILE /dev/stdin
50            OUTPUT_FILE /dev/stdout
51            ERROR_FILE /dev/stderr
52            RESULT_VARIABLE error
53        )
54        if(error)
55            message(FATAL_ERROR "Failed to gen GPIO header: ${header_file}")
56        endif()
57    endif()
58    add_custom_target(${device_type}_list_gen DEPENDS "${dev_list_file}")
59    add_library(${device_type}_list INTERFACE)
60    target_include_directories(
61        ${device_type}_list
62        INTERFACE "${CMAKE_CURRENT_BINARY_DIR}/${device_type}"
63    )
64    add_dependencies(${device_type}_list ${device_type}_list_gen ${header_file})
65    # NOTE: Add this header to the CONFIG_LIBRARIES and GENERATED_FILES properties?
66endfunction(gen_device_header)
67