From aac878345685413ca85e2d4d941406cf98386921 Mon Sep 17 00:00:00 2001 From: Davide Grohmann Date: Tue, 7 Nov 2023 09:47:55 +0100 Subject: Allow serialization_lib to use an external flatbuffers target Before this change the only way to build the serialization_lib was to have the flatbuffers lib pre-built before calling cmake or to allow serialization_lib to build its own flatbuffers library. This commit make sure that you can build serialization_lib as part of the cmake build that builds flatbuffers (not in advance) by checking that flatbuffers is in the TARGET list. In that case it is simply linked by the serialization_lib library. Signed-off-by: Davide Grohmann Change-Id: I5a9d5f263ccfb36273977d6e6a2c8083598b0afb --- CMakeLists.txt | 61 +++++++++++++++++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d373694..ac34b75 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,35 +36,40 @@ add_library(tosa_serialization_lib src/numpy_utils.cpp ) -# Verify we have a valid flatbuffers include path. -# We will explicitly exclude the system include directories and only -# accept either a user supplied value or the local third_party/flatbuffers. -find_path(FLATBUFFERS_INCLUDE_PATH flatbuffers/flatbuffers.h - NO_DEFAULT_PATH - HINTS ${FLATBUFFERS_ROOT} ./third_party/flatbuffers - PATH_SUFFIXES include) -message(STATUS "Flatbuffers include located at: ${FLATBUFFERS_INCLUDE_PATH}") -include_directories(${FLATBUFFERS_INCLUDE_PATH}) - -# Next is the library. -# We will explicitly exclude the system lib directories and only accept -# either a user supplied value or the local third_party/flatbuffers. -find_library(FLATBUFFERS_LIBRARY - NAMES libflatbuffers.a flatbuffers - NO_DEFAULT_PATH - HINTS ${FLATBUFFERS_ROOT} ./third_party/flatbuffers - PATH_SUFFIXES lib) - -if(FLATBUFFERS_LIBRARY) - message(STATUS "Flatbuffers library located at: ${FLATBUFFERS_LIBRARY}") - target_link_libraries(tosa_serialization_lib PRIVATE ${FLATBUFFERS_LIBRARY}) +# If flatbuffers is built externally just link it +if (TARGET flatbuffers) + target_link_libraries(tosa_serialization_lib PRIVATE flatbuffers) else() - # It's not there we treat third_party/flatbuffers as a sub project. - # In this case we'll need to build the downloaded source. - # Turn off unnecessary flatbuffers targets - set(FLATBUFFERS_BUILD_TESTS OFF) - add_subdirectory(third_party/flatbuffers) - target_link_libraries(tosa_serialization_lib PRIVATE flatbuffers) + # Verify we have a valid flatbuffers include path. + # We will explicitly exclude the system include directories and only + # accept either a user supplied value or the local third_party/flatbuffers. + find_path(FLATBUFFERS_INCLUDE_PATH flatbuffers/flatbuffers.h + NO_DEFAULT_PATH + HINTS ${FLATBUFFERS_ROOT} ./third_party/flatbuffers + PATH_SUFFIXES include) + message(STATUS "Flatbuffers include located at: ${FLATBUFFERS_INCLUDE_PATH}") + include_directories(${FLATBUFFERS_INCLUDE_PATH}) + + # Next is the library. + # We will explicitly exclude the system lib directories and only accept + # either a user supplied value or the local third_party/flatbuffers. + find_library(FLATBUFFERS_LIBRARY + NAMES libflatbuffers.a flatbuffers + NO_DEFAULT_PATH + HINTS ${FLATBUFFERS_ROOT} ./third_party/flatbuffers + PATH_SUFFIXES lib) + + if(FLATBUFFERS_LIBRARY) + message(STATUS "Flatbuffers library located at: ${FLATBUFFERS_LIBRARY}") + target_link_libraries(tosa_serialization_lib PRIVATE ${FLATBUFFERS_LIBRARY}) + else() + # It's not there we treat third_party/flatbuffers as a sub project. + # In this case we'll need to build the downloaded source. + # Turn off unnecessary flatbuffers targets + set(FLATBUFFERS_BUILD_TESTS OFF) + add_subdirectory(third_party/flatbuffers) + target_link_libraries(tosa_serialization_lib PRIVATE flatbuffers) + endif() endif() set(public_headers) -- cgit v1.2.1