• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    list(APPEND arg_INPUTS "$ENV{PW_ROOT}/pw_sensor/py/pw_sensor/templates/cpp_constants.jinja")
83
84    if("${arg_GENERATOR_ARGS}" STREQUAL "")
85      set(arg_GENERATOR_ARGS --package pw.sensor)
86    endif()
87  endif()
88
89  if(IS_ABSOLUTE "${arg_OUT_HEADER}")
90    if(NOT ${arg_OUT_INCLUDES})
91      message(FATAL_ERROR "Invalid absolute path OUT_HEADER=${arg_OUT_HEADER}, missing OUT_INCLUDES")
92    endif()
93
94    set(output_file "${arg_OUT_HEADER}")
95  else()
96    set(output_file "${CMAKE_CURRENT_BINARY_DIR}/${arg_OUT_HEADER}")
97    if("${arg_OUT_INCLUDES}" STREQUAL "")
98      set(arg_OUT_INCLUDES "${CMAKE_CURRENT_BINARY_DIR}")
99    endif()
100  endif()
101
102  string(REPLACE ";" " " generator_args "${arg_GENERATOR_ARGS}")
103
104  set(include_list)
105
106  foreach(item IN LISTS arg_GENERATOR_INCLUDES)
107    list(APPEND include_list "-I" "${item}")
108  endforeach()
109
110  add_custom_command(
111    OUTPUT ${output_file}
112    COMMAND python3
113    $ENV{PW_ROOT}/pw_sensor/py/pw_sensor/sensor_desc.py
114      ${include_list}
115      -g "python3 ${arg_GENERATOR} ${generator_args}"
116      -o ${output_file}
117      ${arg_SOURCES}
118    DEPENDS
119      ${arg_GENERATOR}
120      $ENV{PW_ROOT}/pw_sensor/py/pw_sensor/sensor_desc.py
121      ${arg_INPUTS}
122      ${arg_SOURCES}
123  )
124  add_custom_target(${NAME}.__generate_constants
125    DEPENDS
126    ${output_file}
127  )
128  add_library(${NAME} STATIC
129      ${output_file}
130  )
131  target_link_libraries(${NAME} PUBLIC ${arg_PUBLIC_DEPS})
132  target_include_directories(${NAME} PUBLIC
133      ${arg_OUT_INCLUDES}
134  )
135  add_dependencies(${NAME} ${NAME}.__generate_constants)
136  set_target_properties(${NAME} PROPERTIES LINKER_LANGUAGE CXX)
137endfunction(pw_sensor_library)
138