1# Copyright 2024 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14 15include($ENV{PW_ROOT}/pw_build/pigweed.cmake) 16 17# Generates a sensor library 18# 19# Args: 20# OUT_HEADER: The path/to/header.h to generate 21# SOURCES: YAML files defining sensors 22# OUT_INCLUDES: [optional] The include path to expose in the final library, if 23# not defined, the root of the 'out_header' will be used so including the 24# header will be done via '#include "path/to/header.h"' 25# INPUTS: [optional] YAML files included by the sensors, these will be 26# used to optimize re-building. 27# GENERATOR: [optional] Python generator script, if not set, the default 28# Pigweed generator will be used. 29# GENERATOR_ARGS: [optional] Command line arguments to pass to the generator. 30# GENERATOR_INCLUDES: [optional] Include paths to pass to the generator. These 31# are used to resolve the sensor dependencies. 32# PUBLIC_DEPS: [optional] Public dependencies to pass to the final generated 33# target. 34# 35# Example use: 36# pw_sensor_library(my_sensors 37# OUT_HEADER 38# ${CMAKE_BINARY_DIR}/generated/include/my/app/sensors.h 39# OUT_INCLUDES 40# ${CMAKE_BINARY_DIR}/generated/include 41# SOURCES 42# sensors/bma4xx.yaml 43# sensors/bmi160.yaml 44# INPUTS 45# sensors/attributes.yaml 46# sensors/channels.yaml 47# sensors/triggers.yaml 48# sensors/units.yaml 49# GENERATOR 50# scripts/sensor_header_generator.py 51# GENERATOR_ARGS 52# -v 53# -experimental 54# GENERATOR_INCLUDES 55# ${CMAKE_CURRENT_LIST_DIR} 56# PUBLIC_DEPS 57# pw_sensor.types 58# pw_containers 59# ) 60function(pw_sensor_library NAME) 61 pw_parse_arguments( 62 NUM_POSITIONAL_ARGS 63 1 64 MULTI_VALUE_ARGS 65 INPUTS 66 GENERATOR_INCLUDES 67 SOURCES 68 GENERATOR_ARGS 69 PUBLIC_DEPS 70 OUT_INCLUDES 71 ONE_VALUE_ARGS 72 OUT_HEADER 73 GENERATOR 74 REQUIRED_ARGS 75 GENERATOR_INCLUDES 76 SOURCES 77 OUT_HEADER 78 ) 79 80 if("${arg_GENERATOR}" STREQUAL "") 81 set(arg_GENERATOR "$ENV{PW_ROOT}/pw_sensor/py/pw_sensor/constants_generator.py") 82 83 if("${arg_GENERATOR_ARGS}" STREQUAL "") 84 set(arg_GENERATOR_ARGS --package pw.sensor) 85 endif() 86 endif() 87 88 if(IS_ABSOLUTE "${arg_OUT_HEADER}") 89 if(NOT ${arg_OUT_INCLUDES}) 90 message(FATAL_ERROR "Invalid absolute path OUT_HEADER=${arg_OUT_HEADER}, missing OUT_INCLUDES") 91 endif() 92 93 set(output_file "${arg_OUT_HEADER}") 94 else() 95 set(output_file "${CMAKE_CURRENT_BINARY_DIR}/${arg_OUT_HEADER}") 96 if("${arg_OUT_INCLUDES}" STREQUAL "") 97 set(arg_OUT_INCLUDES "${CMAKE_CURRENT_BINARY_DIR}") 98 endif() 99 endif() 100 101 string(REPLACE ";" " " generator_args "${arg_GENERATOR_ARGS}") 102 103 set(include_list) 104 105 foreach(item IN LISTS arg_GENERATOR_INCLUDES) 106 list(APPEND include_list "-I" "${item}") 107 endforeach() 108 109 add_custom_command( 110 OUTPUT ${output_file} 111 COMMAND python3 112 $ENV{PW_ROOT}/pw_sensor/py/pw_sensor/sensor_desc.py 113 ${include_list} 114 -g "python3 ${arg_GENERATOR} ${generator_args}" 115 -o ${output_file} 116 ${arg_SOURCES} 117 DEPENDS 118 ${arg_GENERATOR} 119 $ENV{PW_ROOT}/pw_sensor/py/pw_sensor/sensor_desc.py 120 ${arg_INPUTS} 121 ${arg_SOURCES} 122 ) 123 add_custom_target(${NAME}.__generate_constants 124 DEPENDS 125 ${output_file} 126 ) 127 add_library(${NAME} STATIC 128 ${output_file} 129 ) 130 target_link_libraries(${NAME} PUBLIC ${arg_PUBLIC_DEPS}) 131 target_include_directories(${NAME} PUBLIC 132 ${arg_OUT_INCLUDES} 133 ) 134 add_dependencies(${NAME} ${NAME}.__generate_constants) 135 set_target_properties(${NAME} PROPERTIES LINKER_LANGUAGE CXX) 136endfunction(pw_sensor_library) 137