conan-io / conan-center-index

Recipes for the ConanCenter repository
https://conan.io/center
MIT License
945 stars 1.71k forks source link

[question] how to run flatbuffers' flatc executable in CMakeLists.txt? #9260

Open qh-huang opened 2 years ago

qh-huang commented 2 years ago

It's related to #8374.

As I have a conanfile.txt which includes flatbuffers/2.0.0 with option flatbuffers:flatc=True, and it can be installed correctly.

However, cmake complains about flatc is not found when I try to generate flatbuffer headers with build_flatbuffers macro.

(I tried flatbuffers:options_from_context=False after I read #8374 but it's not working)

What's the recommended way to use flatbuffer along with conan?

To make it more clear, following are my conanfile.txt and CMakeLists.txt. I'd like to know what it should be to use flatbuffers in my project. Thanks.

# conanfile.txt
[requires]
flatbuffers/2.0.0

[generators]
cmake

[options]
flatbuffers:flatc=True
flatbuffers:options_from_context=False
# CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(myproj
    VERSION 0.0.1
    LANGUAGES C CXX)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

set(CMAKE_CXX_STANDARD 14)

file(GLOB_RECURSE ALL_HDRS "examples/*.h" "examples/*.hpp" "examples/*.inc" "examples/*.ipp")
file(GLOB_RECURSE ALL_SRCS "examples/*.cc" "examples/*.cpp")
file(GLOB_RECURSE ALL_TEST_SRCS "examples/*_test.cc" "examples/*_test.cpp" tests/*.cc tests/*.cpp)
file(GLOB_RECURSE ALL_EXECUTABLE_SRCS "examples/*_main.cc" "examples/*_main.cpp")

list(REMOVE_ITEM ALL_SRCS ${ALL_EXECUTABLE_SRCS})
list(REMOVE_ITEM ALL_SRCS ${ALL_TEST_SRCS})

# flatbuffer schema
file(GLOB_RECURSE ALL_FBS "*.fbs")
set(ALL_FBS_SRCS)
set(ALL_FBS_HDRS)
foreach(ABS_FIL ${ALL_FBS})
  file(RELATIVE_PATH REL_FIL ${PROJECT_SOURCE_DIR} ${ABS_FIL})
  get_filename_component(DIR ${REL_FIL} DIRECTORY)
  get_filename_component(FIL_WE ${REL_FIL} NAME_WE)
  list(APPEND ALL_FBS_HDRS "${PROJECT_BINARY_DIR}/${DIR}/${FIL_WE}_generated.h")
  build_flatbuffers(
    ${ABS_FIL}
    ""
    ${FIL_WE}_generated.h
    ""
    ${PROJECT_BINARY_DIR}/${DIR}
    ""
    ""
  )
endforeach()
list(APPEND ALL_HDRS ${ALL_FBS_HDRS})

add_library(${PROJECT_NAME} STATIC ${ALL_HDRS} ${ALL_SRCS})
target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})

# build tests
foreach(ABS_FIL ${ALL_TEST_SRCS})
  file(RELATIVE_PATH REL_FIL ${PROJECT_SOURCE_DIR} ${ABS_FIL})
  get_filename_component(DIR ${REL_FIL} DIRECTORY)
  get_filename_component(FIL_WE ${REL_FIL} NAME_WE)

  add_executable(${TEST_TARGET_NAME} ${ABS_FIL})
  target_link_libraries("${TEST_TARGET_NAME}" PUBLIC ${PROJECT_NAME})
  add_test(${TEST_TARGET_NAME} ${TEST_TARGET_NAME})
endforeach()
ruurdadema commented 2 years ago

This is how I do it: (Note: these are only snippets, it's not a fully working example)

# This generates a conanfile.txt file, you can also specify this in a conanfile.txt itself.
conan_cmake_configure(
        REQUIRES
        flatbuffers/2.0.0
        GENERATORS cmake_find_package
        IMPORTS "bin, flatc -> ./bin" # macOS
        IMPORTS "bin, flatc.exe -> ./bin" # Windows
        OPTIONS flatbuffers:options_from_context=False
        OPTIONS flatbuffers:flatc=True
)

# Flatbuffers
if (WIN32)
    set(FLATBUFFERS_FLATC_EXECUTABLE ${CMAKE_CURRENT_BINARY_DIR}/bin/flatc.exe)
else ()
    set(FLATBUFFERS_FLATC_EXECUTABLE ${CMAKE_CURRENT_BINARY_DIR}/bin/flatc)
endif ()

flatbuffers_generate_headers(
        TARGET FlatbuffersGeneratedHeaders
        SCHEMAS SomeSchema.fbs
        FLAGS --gen-object-api --reflect-names --gen-name-strings --gen-compare
)
include_directories(${CMAKE_BINARY_DIR}/FlatbuffersGeneratedHeaders)