|
| 1 | +# Copyright © 2024 Intel Corporation |
| 2 | +# SPDX-License-Identifier: Apache 2.0 |
| 3 | +# LEGAL NOTICE: Your use of this software and any required dependent software (the “Software Package”) |
| 4 | +# is subject to the terms and conditions of the software license agreements for the Software Package, |
| 5 | +# which may also include notices, disclaimers, or license terms for third party or open source software |
| 6 | +# included in or with the Software Package, and your use indicates your acceptance of all such terms. |
| 7 | +# Please refer to the “third-party-programs.txt” or other similarly-named text file included with the |
| 8 | +# Software Package for additional details. |
| 9 | + |
| 10 | +# It will automatically download and integrate the FlatBuffers library |
| 11 | +# set flags and target related to Flatbuffers that will be used |
| 12 | +# across different CMakeLists |
| 13 | + |
| 14 | +if(TARGET flatbuffers AND TARGET flatc) |
| 15 | + message(STATUS "FLATBUFFERS: flatbuffers and flatc targets already available") |
| 16 | + set(FLATC_COMMAND $<TARGET_FILE:flatc>) |
| 17 | + set(FLATC_TARGET flatc) |
| 18 | +endif() |
| 19 | + |
| 20 | +if(DEFINED FLATC_COMMAND AND DEFINED FLATC_TARGET) |
| 21 | + message(STATUS "FLATBUFFERS: Using flatc target from parent project") |
| 22 | +else() |
| 23 | + message(STATUS "FLATBUFFERS: Fetching FlatBuffers from source...") |
| 24 | + set(VPUNN_USING_INTERNAL_FLATBUFFERS TRUE) |
| 25 | + |
| 26 | + include(FetchContent) |
| 27 | + FetchContent_Declare( |
| 28 | + flatbuffers |
| 29 | + GIT_REPOSITORY https://github.com/google/flatbuffers.git |
| 30 | + GIT_TAG v24.3.25 |
| 31 | + ) |
| 32 | + |
| 33 | + set(FLATBUFFERS_BUILD_TESTS OFF CACHE BOOL "" FORCE) |
| 34 | + set(FLATBUFFERS_INSTALL OFF CACHE BOOL "" FORCE) |
| 35 | + set(FLATBUFFERS_BUILD_FLATC ON CACHE BOOL "" FORCE) |
| 36 | + |
| 37 | + if(JS_BUILD) |
| 38 | + # Populate sources (no build yet) |
| 39 | + FetchContent_GetProperties(flatbuffers) |
| 40 | + |
| 41 | + if(NOT flatbuffers_POPULATED) |
| 42 | + FetchContent_Populate(flatbuffers) |
| 43 | + endif() |
| 44 | + |
| 45 | + # Build host flatc (native) with ExternalProject |
| 46 | + include(ExternalProject) |
| 47 | + set(_FLATC_HOST_BINARY_DIR ${CMAKE_BINARY_DIR}/flatbuffers-host) |
| 48 | + set(_FLATC_HOST_BIN ${_FLATC_HOST_BINARY_DIR}/flatc) |
| 49 | + |
| 50 | + ExternalProject_Add(flatc_host_build |
| 51 | + SOURCE_DIR ${flatbuffers_SOURCE_DIR} |
| 52 | + BINARY_DIR ${_FLATC_HOST_BINARY_DIR} |
| 53 | + CMAKE_ARGS |
| 54 | + -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} |
| 55 | + -DCMAKE_CXX_COMPILER=g++ |
| 56 | + BUILD_COMMAND ${CMAKE_COMMAND} --build . --target flatc --parallel 8 |
| 57 | + INSTALL_COMMAND "" |
| 58 | + ) |
| 59 | + add_executable(flatc_host IMPORTED GLOBAL) |
| 60 | + add_dependencies(flatc_host flatc_host_build) |
| 61 | + set_target_properties(flatc_host PROPERTIES IMPORTED_LOCATION ${_FLATC_HOST_BIN}) |
| 62 | + |
| 63 | + # Provide a flatbuffers interface target for headers |
| 64 | + if(NOT TARGET flatbuffers) |
| 65 | + add_library(flatbuffers INTERFACE) |
| 66 | + target_include_directories(flatbuffers INTERFACE ${flatbuffers_SOURCE_DIR}/include) |
| 67 | + # Avoid expecting generated sources from a built library |
| 68 | + target_compile_definitions(flatbuffers INTERFACE FLATBUFFERS_HEADER_ONLY) |
| 69 | + endif() |
| 70 | + |
| 71 | + set(FLATC_COMMAND $<TARGET_FILE:flatc_host>) |
| 72 | + set(FLATC_TARGET flatc_host) |
| 73 | + message(STATUS "FLATBUFFERS: Host flatc will be built at ${_FLATC_HOST_BIN}") |
| 74 | + else() |
| 75 | + FetchContent_MakeAvailable(flatbuffers) |
| 76 | + |
| 77 | + if(NOT MSVC) |
| 78 | + # only for clang 13+ |
| 79 | + if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") |
| 80 | + # if clang version < 13, do nothing |
| 81 | + if ((CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 13)) |
| 82 | + target_compile_options(flatbuffers PRIVATE -Wno-unused-but-set-variable) |
| 83 | + target_compile_options(flatc PRIVATE -Wno-unused-but-set-variable) |
| 84 | + target_compile_options(flatbuffers PRIVATE -Wno-suggest-override) |
| 85 | + target_compile_options(flatc PRIVATE -Wno-suggest-override) |
| 86 | + endif() |
| 87 | + # For ther compilers that are not clang (GNU) |
| 88 | + else() |
| 89 | + target_compile_options(flatbuffers PRIVATE -Wno-suggest-override) |
| 90 | + target_compile_options(flatc PRIVATE -Wno-suggest-override) |
| 91 | + endif() |
| 92 | + endif() |
| 93 | + |
| 94 | + set(FLATC_COMMAND $<TARGET_FILE:flatc>) |
| 95 | + set(FLATC_TARGET flatc) |
| 96 | + |
| 97 | + message(STATUS "FLATBUFFERS: Fetched and configured") |
| 98 | + |
| 99 | + endif() |
| 100 | +endif() |
| 101 | + |
| 102 | +# Export variables to the parent scope |
| 103 | +set(FLATBUFFERS_SRC_DIR ${flatbuffers_SOURCE_DIR}) |
| 104 | +set(FLATBUFFERS_INCLUDE_DIRECTORIES ${flatbuffers_SOURCE_DIR}/include) |
0 commit comments