3 # Find the FlatBuffers schema headers
5 # This will define the following target:
7 # ${APP_NAME_LC}::FlatBuffers - The flatbuffers headers
9 find_package(FlatC REQUIRED)
11 if(NOT TARGET ${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME})
13 include(cmake/scripts/common/ModuleHelpers.cmake)
15 macro(buildflatbuffers)
16 # Override build type detection and always build as release
17 set(FLATBUFFERS_BUILD_TYPE Release)
19 set(CMAKE_ARGS -DFLATBUFFERS_CODE_COVERAGE=OFF
20 -DFLATBUFFERS_BUILD_TESTS=OFF
21 -DFLATBUFFERS_INSTALL=ON
22 -DFLATBUFFERS_BUILD_FLATLIB=OFF
23 -DFLATBUFFERS_BUILD_FLATC=OFF
24 -DFLATBUFFERS_BUILD_FLATHASH=OFF
25 -DFLATBUFFERS_BUILD_GRPCTEST=OFF
26 -DFLATBUFFERS_BUILD_SHAREDLIB=OFF
28 set(BUILD_BYPRODUCTS ${DEPENDS_PATH}/include/flatbuffers/flatbuffers.h)
33 set(MODULE_LC flatbuffers)
37 find_package(flatbuffers CONFIG
38 HINTS ${DEPENDS_PATH}/lib/cmake
39 ${${CORE_PLATFORM_NAME_LC}_SEARCH_CONFIG})
41 # Check for existing Flatbuffers. If version >= FLATBUFFERS-VERSION file version, dont build
42 # A corner case, but if a linux/freebsd user WANTS to build internal flatbuffers, build anyway
43 if((flatbuffers_VERSION VERSION_LESS ${${MODULE}_VER} AND ENABLE_INTERNAL_FLATBUFFERS) OR
44 ((CORE_SYSTEM_NAME STREQUAL linux OR CORE_SYSTEM_NAME STREQUAL freebsd) AND ENABLE_INTERNAL_FLATBUFFERS))
48 find_path(FLATBUFFERS_INCLUDE_DIR NAMES flatbuffers/flatbuffers.h
49 HINTS ${DEPENDS_PATH}/include
50 ${${CORE_PLATFORM_LC}_SEARCH_CONFIG}
54 include(FindPackageHandleStandardArgs)
55 find_package_handle_standard_args(FlatBuffers
56 REQUIRED_VARS FLATBUFFERS_INCLUDE_DIR
57 VERSION_VAR FLATBUFFERS_VER)
61 add_library(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} INTERFACE IMPORTED)
62 set_target_properties(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} PROPERTIES
63 INTERFACE_INCLUDE_DIRECTORIES "${FLATBUFFERS_INCLUDE_DIR}")
65 add_dependencies(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} flatbuffers::flatc)
67 if(TARGET flatbuffers)
68 add_dependencies(${APP_NAME_LC}::${CMAKE_FIND_PACKAGE_NAME} flatbuffers)
71 # Add internal build target when a Multi Config Generator is used
72 # We cant add a dependency based off a generator expression for targeted build types,
73 # https://gitlab.kitware.com/cmake/cmake/-/issues/19467
74 # therefore if the find heuristics only find the library, we add the internal build
75 # target to the project to allow user to manually trigger for any build type they need
76 # in case only a specific build type is actually available (eg Release found, Debug Required)
77 # This is mainly targeted for windows who required different runtime libs for different
78 # types, and they arent compatible
79 if(_multiconfig_generator)
80 if(NOT TARGET flatbuffers)
82 set_target_properties(flatbuffers PROPERTIES EXCLUDE_FROM_ALL TRUE)
84 add_dependencies(build_internal_depends flatbuffers)
87 if(FlatBuffers_FIND_REQUIRED)
88 message(FATAL_ERROR "Flatbuffer schema headers were not found. You may want to try -DENABLE_INTERNAL_FLATBUFFERS=ON to build the internal headers package")