libplatsupport: Add tools to gen device headers The new functions allow the user to input a YAML file that contains a list of sub-devices of a particular device (GPIO pins, clocks on a board etc), and output a C header '#define' enumeration.
diff --git a/Findutil_libs.cmake b/Findutil_libs.cmake index 24e5df9..73ba91f 100644 --- a/Findutil_libs.cmake +++ b/Findutil_libs.cmake
@@ -12,11 +12,21 @@ set(UTIL_LIBS_DIR "${CMAKE_CURRENT_LIST_DIR}" CACHE STRING "") set(LWIP_HELPERS "${CMAKE_CURRENT_LIST_DIR}/liblwip/lwip_helpers.cmake" CACHE STRING "") -mark_as_advanced(UTIL_LIBS_DIR LWIP_HELPERS) +set( + PLATSUPPORT_HELPERS "${CMAKE_CURRENT_LIST_DIR}/libplatsupport/tools/helpers.cmake" + CACHE STRING "" +) +mark_as_advanced(UTIL_LIBS_DIR LWIP_HELPERS PLATSUPPORT_HELPERS) macro(util_libs_import_libraries) add_subdirectory(${UTIL_LIBS_DIR} util_libs) endmacro() include(FindPackageHandleStandardArgs) -FIND_PACKAGE_HANDLE_STANDARD_ARGS(util_libs DEFAULT_MSG UTIL_LIBS_DIR LWIP_HELPERS) +FIND_PACKAGE_HANDLE_STANDARD_ARGS( + util_libs + DEFAULT_MSG + UTIL_LIBS_DIR + LWIP_HELPERS + PLATSUPPORT_HELPERS +)
diff --git a/libplatsupport/CMakeLists.txt b/libplatsupport/CMakeLists.txt index a6d845b..f1520fc 100644 --- a/libplatsupport/CMakeLists.txt +++ b/libplatsupport/CMakeLists.txt
@@ -14,6 +14,8 @@ project(libplatsupport C) +include(${PLATSUPPORT_HELPERS}) + set(configure_string "") config_choice(
diff --git a/libplatsupport/tools/device_header_gen.py b/libplatsupport/tools/device_header_gen.py new file mode 100644 index 0000000..fe603a7 --- /dev/null +++ b/libplatsupport/tools/device_header_gen.py
@@ -0,0 +1,83 @@ +#!/usr/bin/env python3 + +# +# Copyright 2020, Data61 +# Commonwealth Scientific and Industrial Research Organisation (CSIRO) +# ABN 41 687 119 230. +# +# This software may be distributed and modified according to the terms of +# the BSD 2-Clause license. Note that NO WARRANTY is provided. +# See "LICENSE_BSD2.txt" for details. +# +# @TAG(DATA61_BSD) +# + +import builtins +import os +import argparse +import yaml +import logging + +from jinja2 import Environment, BaseLoader + +HEADER_TEMPLATE = '''/* + * Copyright 2020, Data61 + * Commonwealth Scientific and Industrial Research Organisation (CSIRO) + * ABN 41 687 119 230. + * + * This software may be distributed and modified according to the terms of + * the BSD 2-Clause license. Note that NO WARRANTY is provided. + * See "LICENSE_BSD2.txt" for details. + * + * @TAG(DATA61_BSD) + */ + +/* This file is auto-generated by device_header_gen.py in libplatsupport. */ + +#pragma once + +{% for dev in devices %} +#define {{ dev }} {{ loop.index0 }} + {% if loop.last %} +#define MAX_{{ device_type.upper() }}_ID {{ loop.index0 }} +{% endif %} +{% endfor %} +''' + + +def main(args: argparse.Namespace): + try: + with open(args.device_list) as device_list: + parsed_dev_dict = yaml.safe_load(device_list) + list_key = '{0}_list'.format(args.device_type) + devices = parsed_dev_dict[list_key] + template = Environment(loader=BaseLoader, trim_blocks=True, + lstrip_blocks=True).from_string(HEADER_TEMPLATE) + template_args = dict(builtins.__dict__, **{ + 'device_type': args.device_type, + 'devices': devices, + }) + header_contents = template.render(template_args) + args.header_out.write(header_contents) + args.header_out.close() + except: + logging.fatal("Failed to parse the device list file {0}".format(args.device_list.name)) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description='transform sub-device (GPIO etc.) list to libplatsupport build artefacts' + ) + + # Inputs + parser.add_argument('--device-list', help='sub-device list to parse for generation', + required=True, type=str) + parser.add_argument('--device-type', help='device type of the input type', required=True) + + # Outputs + parser.add_argument('--header-out', help='output location of the resultant C header', + required=True, type=argparse.FileType('w')) + + args = parser.parse_args() + + main(args)
diff --git a/libplatsupport/tools/helpers.cmake b/libplatsupport/tools/helpers.cmake new file mode 100644 index 0000000..7515957 --- /dev/null +++ b/libplatsupport/tools/helpers.cmake
@@ -0,0 +1,69 @@ +# +# Copyright 2020, Data61 +# Commonwealth Scientific and Industrial Research Organisation (CSIRO) +# ABN 41 687 119 230. +# +# This software may be distributed and modified according to the terms of +# the BSD 2-Clause license. Note that NO WARRANTY is provided. +# See "LICENSE_BSD2.txt" for details. +# +# @TAG(DATA61_BSD) +# + +cmake_minimum_required(VERSION 3.8.2) + +function(get_device_list var device_type platform) + get_filename_component(platsupport_tools ${PLATSUPPORT_HELPERS} DIRECTORY) + find_file( + ${platform}_${device_type}_LIST "${platform}.yaml" + PATHS "${platsupport_tools}/device_lists/${device_type}" + CMAKE_FIND_ROOT_PATH_BOTH + ) + if("${${platform}_${device_type}_LIST}" STREQUAL "${platform}_${device_type}_LIST-NOTFOUND") + message(WARNING "Could not find device list for device type ${device_type} for ${platform}") + endif() + set(${var} ${${platform}_${device_type}_LIST} PARENT_SCOPE) +endfunction() + +# List of helper CMake functions for libplatsupport +function(gen_device_header device_type platform) + get_filename_component(platsupport_tools ${PLATSUPPORT_HELPERS} DIRECTORY) + get_device_list(dev_list_file ${device_type} ${platform}) + if("${dev_list_file}" STREQUAL "${platform}_${device_type}_LIST-NOTFOUND") + # Exit silently... + return() + endif() + set(header_dir "${CMAKE_CURRENT_BINARY_DIR}/${device_type}/platsupport") + set(header_file "${header_dir}/${device_type}_list.h") + set(DEVICE_GEN_PATH "${platsupport_tools}/device_header_gen.py") + set(dev_header_deps ${DEVICE_GEN_PATH} ${dev_list_file}) + check_outfile_stale( + regen + ${header_file} + dev_header_deps + "${CMAKE_CURRENT_BINARY_DIR}/${device_type}.cmd" + ) + if(regen) + file(MAKE_DIRECTORY ${header_dir}) + execute_process( + COMMAND + ${PYTHON3} "${DEVICE_GEN_PATH}" --device-list "${dev_list_file}" --device-type + "${device_type}" --header-out "${header_file}" + INPUT_FILE /dev/stdin + OUTPUT_FILE /dev/stdout + ERROR_FILE /dev/stderr + RESULT_VARIABLE error + ) + if(error) + message(FATAL_ERROR "Failed to gen GPIO header: ${header_file}") + endif() + endif() + add_custom_target(${device_type}_list_gen DEPENDS "${dev_list_file}") + add_library(${device_type}_list INTERFACE) + target_include_directories( + ${device_type}_list + INTERFACE "${CMAKE_CURRENT_BINARY_DIR}/${device_type}" + ) + add_dependencies(${device_type}_list ${device_type}_list_gen ${header_file}) + # NOTE: Add this header to the CONFIG_LIBRARIES and GENERATED_FILES properties? +endfunction(gen_device_header)