mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-09 22:56:27 +00:00
279 lines
9.6 KiB
CMake
279 lines
9.6 KiB
CMake
cmake_minimum_required(VERSION 2.8)
|
|
|
|
project(FlatBuffers)
|
|
|
|
# NOTE: Code coverage only works on Linux & OSX.
|
|
option(FLATBUFFERS_CODE_COVERAGE "Enable the code coverage build option." OFF)
|
|
option(FLATBUFFERS_BUILD_TESTS "Enable the build of tests and samples." ON)
|
|
option(FLATBUFFERS_INSTALL "Enable the installation of targets." ON)
|
|
option(FLATBUFFERS_BUILD_FLATLIB "Enable the build of the flatbuffers library" ON)
|
|
option(FLATBUFFERS_BUILD_FLATC "Enable the build of the flatbuffers compiler" ON)
|
|
option(FLATBUFFERS_BUILD_FLATHASH "Enable the build of flathash" ON)
|
|
|
|
if(NOT FLATBUFFERS_BUILD_FLATC AND FLATBUFFERS_BUILD_TESTS)
|
|
message(WARNING
|
|
"Cannot build tests without building the compiler. Tests will be disabled.")
|
|
set(FLATBUFFERS_BUILD_TESTS OFF)
|
|
endif()
|
|
|
|
set(FlatBuffers_Library_SRCS
|
|
include/flatbuffers/flatbuffers.h
|
|
include/flatbuffers/hash.h
|
|
include/flatbuffers/idl.h
|
|
include/flatbuffers/util.h
|
|
include/flatbuffers/reflection.h
|
|
include/flatbuffers/reflection_generated.h
|
|
src/idl_parser.cpp
|
|
src/idl_gen_text.cpp
|
|
src/reflection.cpp
|
|
)
|
|
|
|
set(FlatBuffers_Compiler_SRCS
|
|
${FlatBuffers_Library_SRCS}
|
|
src/idl_gen_cpp.cpp
|
|
src/idl_gen_general.cpp
|
|
src/idl_gen_go.cpp
|
|
src/idl_gen_js.cpp
|
|
src/idl_gen_php.cpp
|
|
src/idl_gen_python.cpp
|
|
src/idl_gen_fbs.cpp
|
|
src/flatc.cpp
|
|
)
|
|
|
|
set(FlatHash_SRCS
|
|
include/flatbuffers/hash.h
|
|
src/flathash.cpp
|
|
)
|
|
|
|
set(FlatBuffers_Tests_SRCS
|
|
${FlatBuffers_Library_SRCS}
|
|
src/idl_gen_fbs.cpp
|
|
src/idl_gen_general.cpp
|
|
tests/test.cpp
|
|
# file generate by running compiler on tests/monster_test.fbs
|
|
${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h
|
|
)
|
|
|
|
set(FlatBuffers_Sample_Binary_SRCS
|
|
include/flatbuffers/flatbuffers.h
|
|
samples/sample_binary.cpp
|
|
# file generated by running compiler on samples/monster.fbs
|
|
${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h
|
|
)
|
|
|
|
set(FlatBuffers_Sample_Text_SRCS
|
|
include/flatbuffers/flatbuffers.h
|
|
include/flatbuffers/hash.h
|
|
include/flatbuffers/idl.h
|
|
include/flatbuffers/util.h
|
|
src/idl_parser.cpp
|
|
src/idl_gen_text.cpp
|
|
samples/sample_text.cpp
|
|
# file generated by running compiler on samples/monster.fbs
|
|
${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h
|
|
)
|
|
|
|
# source_group(Compiler FILES ${FlatBuffers_Compiler_SRCS})
|
|
# source_group(Tests FILES ${FlatBuffers_Tests_SRCS})
|
|
|
|
if(APPLE)
|
|
set(CMAKE_CXX_FLAGS
|
|
"${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++ -Wall -pedantic -Werror -Wextra")
|
|
elseif(CMAKE_COMPILER_IS_GNUCXX)
|
|
set(CMAKE_CXX_FLAGS
|
|
"${CMAKE_CXX_FLAGS} -std=c++0x -Wall -pedantic -Werror -Wextra")
|
|
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
|
|
set(CMAKE_CXX_FLAGS
|
|
"${CMAKE_CXX_FLAGS} -std=c++0x -stdlib=libc++ -Wall -pedantic -Werror -Wextra")
|
|
endif()
|
|
|
|
if(FLATBUFFERS_CODE_COVERAGE)
|
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fprofile-arcs -ftest-coverage")
|
|
set(CMAKE_EXE_LINKER_FLAGS
|
|
"${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
|
|
endif()
|
|
|
|
if(BIICODE)
|
|
include(biicode/cmake/biicode.cmake)
|
|
return()
|
|
endif()
|
|
|
|
include_directories(include)
|
|
|
|
if(FLATBUFFERS_BUILD_FLATLIB)
|
|
add_library(flatbuffers STATIC ${FlatBuffers_Library_SRCS})
|
|
endif()
|
|
|
|
if(FLATBUFFERS_BUILD_FLATC)
|
|
add_executable(flatc ${FlatBuffers_Compiler_SRCS})
|
|
endif()
|
|
|
|
if(FLATBUFFERS_BUILD_FLATHASH)
|
|
add_executable(flathash ${FlatHash_SRCS})
|
|
endif()
|
|
|
|
function(compile_flatbuffers_schema_to_cpp SRC_FBS)
|
|
get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH)
|
|
string(REGEX REPLACE "\\.fbs$" "_generated.h" GEN_HEADER ${SRC_FBS})
|
|
add_custom_command(
|
|
OUTPUT ${GEN_HEADER}
|
|
COMMAND flatc -c --no-includes --gen-mutable -o "${SRC_FBS_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
|
|
DEPENDS flatc)
|
|
endfunction()
|
|
|
|
function(compile_flatbuffers_schema_to_binary SRC_FBS)
|
|
get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH)
|
|
string(REGEX REPLACE "\\.fbs$" ".bfbs" GEN_BINARY_SCHEMA ${SRC_FBS})
|
|
add_custom_command(
|
|
OUTPUT ${GEN_BINARY_SCHEMA}
|
|
COMMAND flatc -b --schema -o "${SRC_FBS_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
|
|
DEPENDS flatc)
|
|
endfunction()
|
|
|
|
# General function to create FlatBuffer build rules for the given list of
|
|
# schemas.
|
|
#
|
|
# flatbuffers_schemas: A list of flatbuffer schema files to process.
|
|
#
|
|
# schema_include_dirs: A list of schema file include directories, which will be
|
|
# passed to flatc via the -I parameter.
|
|
#
|
|
# custom_target_name: The generated files will be added as dependencies for a
|
|
# new custom target with this name. You should add that target as a dependency
|
|
# for your main target to ensure these files are built. You can also retrieve
|
|
# various properties from this target, such as GENERATED_INCLUDES_DIR,
|
|
# BINARY_SCHEMAS_DIR, and COPY_TEXT_SCHEMAS_DIR.
|
|
#
|
|
# additional_dependencies: A list of additional dependencies that you'd like
|
|
# all generated files to depend on. Pass in a blank string if you have none.
|
|
#
|
|
# generated_includes_dir: Where to generate the C++ header files for these
|
|
# schemas. The generated includes directory will automatically be added to
|
|
# CMake's include directories, and will be where generated header files are
|
|
# placed. This parameter is optional; pass in empty string if you don't want to
|
|
# generate include files for these schemas.
|
|
#
|
|
# binary_schemas_dir: If you specify an optional binary schema directory, binary
|
|
# schemas will be generated for these schemas as well, and placed into the given
|
|
# directory.
|
|
#
|
|
# copy_text_schemas_dir: If you want all text schemas (including schemas from
|
|
# all schema include directories) copied into a directory (for example, if you
|
|
# need them within your project to build JSON files), you can specify that
|
|
# folder here. All text schemas will be copied to that folder.
|
|
#
|
|
# IMPORTANT: Make sure you quote all list arguments you pass to this function!
|
|
# Otherwise CMake will only pass in the first element.
|
|
# Example: build_flatbuffers("${fb_files}" "${include_dirs}" target_name ...)
|
|
function(build_flatbuffers flatbuffers_schemas
|
|
schema_include_dirs
|
|
custom_target_name
|
|
additional_dependencies
|
|
generated_includes_dir
|
|
binary_schemas_dir
|
|
copy_text_schemas_dir)
|
|
set(schema_glob "*.fbs")
|
|
# Generate the include files parameters.
|
|
set(include_params "")
|
|
set(all_generated_files "")
|
|
foreach (include_dir ${schema_include_dirs})
|
|
set(include_params -I ${include_dir} ${include_params})
|
|
if (NOT ${copy_text_schemas_dir} STREQUAL "")
|
|
# Copy text schemas from dependent folders.
|
|
file(GLOB_RECURSE dependent_schemas ${include_dir}/${schema_glob})
|
|
foreach (dependent_schema ${dependent_schemas})
|
|
file(COPY ${dependent_schema} DESTINATION ${copy_text_schemas_dir})
|
|
endforeach()
|
|
endif()
|
|
endforeach()
|
|
|
|
foreach(schema ${flatbuffers_schemas})
|
|
get_filename_component(filename ${schema} NAME_WE)
|
|
# For each schema, do the things we requested.
|
|
if (NOT ${generated_includes_dir} STREQUAL "")
|
|
set(generated_include ${generated_includes_dir}/${filename}_generated.h)
|
|
add_custom_command(
|
|
OUTPUT ${generated_include}
|
|
COMMAND flatc --gen-mutable
|
|
-o ${generated_includes_dir}
|
|
${include_params}
|
|
-c ${schema}
|
|
DEPENDS flatc ${schema} ${additional_dependencies})
|
|
list(APPEND all_generated_files ${generated_include})
|
|
endif()
|
|
|
|
if (NOT ${binary_schemas_dir} STREQUAL "")
|
|
set(binary_schema ${binary_schemas_dir}/${filename}.bfbs)
|
|
add_custom_command(
|
|
OUTPUT ${binary_schema}
|
|
COMMAND flatc -b --schema
|
|
-o ${binary_schemas_dir}
|
|
${include_params}
|
|
${schema}
|
|
DEPENDS flatc ${schema} ${additional_dependencies})
|
|
list(APPEND all_generated_files ${binary_schema})
|
|
endif()
|
|
|
|
if (NOT ${copy_text_schemas_dir} STREQUAL "")
|
|
file(COPY ${schema} DESTINATION ${copy_text_schemas_dir})
|
|
endif()
|
|
endforeach()
|
|
|
|
# Create a custom target that depends on all the generated files.
|
|
# This is the target that you can depend on to trigger all these
|
|
# to be built.
|
|
add_custom_target(${custom_target_name}
|
|
DEPENDS ${all_generated_files} ${additional_dependencies})
|
|
|
|
# Register the include directory we are using.
|
|
if (NOT ${generated_includes_dir} STREQUAL "")
|
|
include_directories(${generated_includes_dir})
|
|
set_property(TARGET ${custom_target_name}
|
|
PROPERTY GENERATED_INCLUDES_DIR
|
|
${generated_includes_dir})
|
|
endif()
|
|
|
|
# Register the binary schemas dir we are using.
|
|
if (NOT ${binary_schemas_dir} STREQUAL "")
|
|
set_property(TARGET ${custom_target_name}
|
|
PROPERTY BINARY_SCHEMAS_DIR
|
|
${binary_schemas_dir})
|
|
endif()
|
|
|
|
# Register the text schema copy dir we are using.
|
|
if (NOT ${copy_text_schemas_dir} STREQUAL "")
|
|
set_property(TARGET ${custom_target_name}
|
|
PROPERTY COPY_TEXT_SCHEMAS_DIR
|
|
${copy_text_schemas_dir})
|
|
endif()
|
|
endfunction()
|
|
|
|
if(FLATBUFFERS_BUILD_TESTS)
|
|
compile_flatbuffers_schema_to_cpp(tests/monster_test.fbs)
|
|
include_directories(${CMAKE_CURRENT_BINARY_DIR}/tests)
|
|
add_executable(flattests ${FlatBuffers_Tests_SRCS})
|
|
|
|
compile_flatbuffers_schema_to_cpp(samples/monster.fbs)
|
|
include_directories(${CMAKE_CURRENT_BINARY_DIR}/samples)
|
|
add_executable(flatsamplebinary ${FlatBuffers_Sample_Binary_SRCS})
|
|
add_executable(flatsampletext ${FlatBuffers_Sample_Text_SRCS})
|
|
endif()
|
|
|
|
if(FLATBUFFERS_INSTALL)
|
|
install(DIRECTORY include/flatbuffers DESTINATION include)
|
|
if(FLATBUFFERS_BUILD_FLATLIB)
|
|
install(TARGETS flatbuffers DESTINATION lib)
|
|
endif()
|
|
if(FLATBUFFERS_BUILD_FLATC)
|
|
install(TARGETS flatc DESTINATION bin)
|
|
endif()
|
|
endif()
|
|
|
|
if(FLATBUFFERS_BUILD_TESTS)
|
|
enable_testing()
|
|
|
|
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/tests" DESTINATION
|
|
"${CMAKE_CURRENT_BINARY_DIR}")
|
|
add_test(NAME flattests COMMAND flattests)
|
|
endif()
|