1# Copyright 2025 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. 14include_guard(GLOBAL) 15 16include($ENV{PW_ROOT}/pw_build/pigweed.cmake) 17 18# Path to flatc 19set(pw_flatbuffers_FLATC flatc CACHE STRING "Flatbuffers compiler") 20# Global args for flatc 21set(pw_flatbuffers_FLATC_FLAGS "" CACHE LIST "Global args to flatc") 22 23# If set, this is added to the generated library as a public dependency. Since 24# flatbuffers is a header only library, this should be a library containing all 25# the headers as HEADERS, then exposing the include path as a public include, 26# so anything depending on flatbuffers can properly find the headers. This is 27# intended for pigweed itself to use when working with the installed 28# flatbuffers package, but any client can use this to substitute their 29# flatbuffers implementation as well, if they are not relying on the pigweed 30# provided implementation. The user may also opt to add this dependency to the 31# DEPS argument instead. 32 33if ("${dir_pw_third_party_flatbuffers}" STREQUAL "") 34 set(pw_flatbuffers_LIBRARY "" CACHE STRING "Dependency name for flatbuffers headers") 35else() 36 set(pw_flatbuffers_LIBRARY pw_third_party.flatbuffers CACHE STRING "Dependency name for flatbuffers headers") 37endif() 38 39function(pw_flatbuffer_library NAME) 40 pw_parse_arguments( 41 NUM_POSITIONAL_ARGS 42 1 43 MULTI_VALUE_ARGS 44 SOURCES 45 DEPS 46 FLATC_FLAGS 47 REQUIRED_ARGS 48 SOURCES 49 ) 50 51 set(out_dir "${CMAKE_CURRENT_BINARY_DIR}/${NAME}") 52 set(out_sources_dir "${out_dir}/sources") 53 54 # Create a set of files with absolute paths which will be the files we 55 # mirror over to the build directory 56 pw_rebase_paths(files_to_mirror "${CMAKE_CURRENT_SOURCE_DIR}" 57 "${CMAKE_CURRENT_SOURCE_DIR}" "${arg_SOURCES}" "") 58 # Create the set of files in files_to_mirror, but replaced with the 59 # absolute destination path to the build directory 60 pw_rebase_paths(sources "${out_sources_dir}" 61 "${CMAKE_CURRENT_SOURCE_DIR}" "${arg_SOURCES}" "") 62 63 add_custom_command( 64 COMMAND 65 python3 66 "$ENV{PW_ROOT}/pw_build/py/pw_build/mirror_tree.py" 67 --source-root "${CMAKE_CURRENT_SOURCE_DIR}" 68 --directory "${out_sources_dir}" 69 ${files_to_mirror} 70 DEPENDS 71 "$ENV{PW_ROOT}/pw_build/py/pw_build/mirror_tree.py" 72 ${files_to_mirror} 73 OUTPUT 74 ${sources} 75 ) 76 add_custom_target("${NAME}._sources" DEPENDS ${sources}) 77 78 # Generate the C++ headers. 79 foreach(source IN LISTS sources) 80 cmake_path(GET source FILENAME source_filename) 81 82 cmake_path(RELATIVE_PATH source BASE_DIRECTORY "${out_sources_dir}" 83 OUTPUT_VARIABLE rel_source) 84 cmake_path(REMOVE_FILENAME rel_source OUTPUT_VARIABLE rel_source_dir) 85 86 cmake_path(REMOVE_EXTENSION source_filename OUTPUT_VARIABLE 87 generated_filename) 88 cmake_path(APPEND_STRING generated_filename "_generated.h") 89 90 cmake_path(SET generated_directory "${out_dir}") 91 cmake_path(APPEND generated_directory "cpp" ${rel_source_dir}) 92 93 cmake_path(SET output "${generated_directory}") 94 cmake_path(APPEND output ${generated_filename}) 95 96 add_custom_command( 97 COMMAND "${pw_flatbuffers_FLATC}" --cpp ${pw_flatbuffers_FLATC_FLAGS} ${arg_FLATC_FLAGS} 98 -o "${generated_directory}" ${source} 99 DEPENDS ${source} 100 OUTPUT ${output} 101 ) 102 list(APPEND outputs "${output}") 103 endforeach() 104 add_custom_target("${NAME}._generated_sources" DEPENDS ${outputs}) 105 106 # Add the ${NAME}.cpp library using the generated C++ headers. 107 pw_add_library_generic("${NAME}.cpp" INTERFACE 108 PUBLIC_INCLUDES 109 "${out_dir}/cpp" 110 PUBLIC_DEPS 111 ${pw_flatbuffers_LIBRARY} 112 ${arg_DEPS} 113 ) 114 add_dependencies("${NAME}.cpp" 115 "${NAME}._sources" "${NAME}._generated_sources") 116endfunction() 117