blob: a746a646aafc2c837f85e81a8b4a21f689eb6b22 [file] [log] [blame]
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -08001cmake_minimum_required(VERSION 2.8)
2
3project(FlatBuffers)
4
5# NOTE: Code coverage only works on Linux & OSX.
6option(FLATBUFFERS_CODE_COVERAGE "Enable the code coverage build option." OFF)
Stewart Miles3f851832014-09-10 16:01:13 -07007option(FLATBUFFERS_BUILD_TESTS "Enable the build of tests and samples." ON)
Zbigniew Mandziejewicz3f8700b2014-09-16 00:50:23 +08008option(FLATBUFFERS_INSTALL "Enable the installation of targets." ON)
Leander Bessa Beernaert3ec8d7f2015-01-02 13:51:31 +01009option(FLATBUFFERS_BUILD_FLATC "Enable the build of the flatbuffers compiler" ON)
Alex Amesd5753212015-02-13 15:58:29 -080010option(FLATBUFFERS_BUILD_FLATHASH "Enable the build of flathash" ON)
Leander Bessa Beernaert3ec8d7f2015-01-02 13:51:31 +010011
12if(NOT FLATBUFFERS_BUILD_FLATC AND FLATBUFFERS_BUILD_TESTS)
13 message(WARNING
14 "Cannot build tests without building the compiler. Tests will be disabled.")
15 set(FLATBUFFERS_BUILD_TESTS OFF)
16endif()
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -080017
18set(FlatBuffers_Compiler_SRCS
19 include/flatbuffers/flatbuffers.h
Alex Amesd5753212015-02-13 15:58:29 -080020 include/flatbuffers/hash.h
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -080021 include/flatbuffers/idl.h
22 include/flatbuffers/util.h
23 src/idl_parser.cpp
24 src/idl_gen_cpp.cpp
Wouter van Oortmerssen557c88c2014-09-16 17:37:17 -070025 src/idl_gen_general.cpp
rw74d5f372014-07-11 16:12:35 -070026 src/idl_gen_go.cpp
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -080027 src/idl_gen_text.cpp
Wouter van Oortmerssend38b9af2014-09-26 16:46:30 -070028 src/idl_gen_fbs.cpp
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -080029 src/flatc.cpp
30)
31
Alex Amesd5753212015-02-13 15:58:29 -080032set(FlatHash_SRCS
33 include/flatbuffers/hash.h
34 src/flathash.cpp
35)
36
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -080037set(FlatBuffers_Tests_SRCS
38 include/flatbuffers/flatbuffers.h
Alex Amesd5753212015-02-13 15:58:29 -080039 include/flatbuffers/hash.h
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -080040 include/flatbuffers/idl.h
41 include/flatbuffers/util.h
42 src/idl_parser.cpp
43 src/idl_gen_text.cpp
Wouter van Oortmerssend38b9af2014-09-26 16:46:30 -070044 src/idl_gen_fbs.cpp
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -080045 tests/test.cpp
46 # file generate by running compiler on tests/monster_test.fbs
Zbigniew Mandziejewicz3f8700b2014-09-16 00:50:23 +080047 ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -080048)
49
50set(FlatBuffers_Sample_Binary_SRCS
51 include/flatbuffers/flatbuffers.h
52 samples/sample_binary.cpp
Zbigniew Mandziejewicz3f8700b2014-09-16 00:50:23 +080053 # file generated by running compiler on samples/monster.fbs
54 ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -080055)
56
57set(FlatBuffers_Sample_Text_SRCS
58 include/flatbuffers/flatbuffers.h
Alex Amesd5753212015-02-13 15:58:29 -080059 include/flatbuffers/hash.h
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -080060 include/flatbuffers/idl.h
61 include/flatbuffers/util.h
62 src/idl_parser.cpp
63 src/idl_gen_text.cpp
64 samples/sample_text.cpp
Zbigniew Mandziejewicz3f8700b2014-09-16 00:50:23 +080065 # file generated by running compiler on samples/monster.fbs
66 ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -080067)
68
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -080069# source_group(Compiler FILES ${FlatBuffers_Compiler_SRCS})
70# source_group(Tests FILES ${FlatBuffers_Tests_SRCS})
71
Stefan Eilemann52f4f452014-07-26 13:12:56 +020072if(APPLE)
Wouter van Oortmerssenbc5fa9d2014-08-25 10:42:38 -070073 set(CMAKE_CXX_FLAGS
74 "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++ -Wall -pedantic -Werror -Wextra")
Stefan Eilemann52f4f452014-07-26 13:12:56 +020075elseif(CMAKE_COMPILER_IS_GNUCXX OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
Wouter van Oortmerssenbc5fa9d2014-08-25 10:42:38 -070076 set(CMAKE_CXX_FLAGS
77 "${CMAKE_CXX_FLAGS} -std=c++0x -Wall -pedantic -Werror -Wextra")
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -080078endif()
79
80if(FLATBUFFERS_CODE_COVERAGE)
Stefan Eilemann52f4f452014-07-26 13:12:56 +020081 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fprofile-arcs -ftest-coverage")
Wouter van Oortmerssen8f80fec2014-07-29 10:29:38 -070082 set(CMAKE_EXE_LINKER_FLAGS
83 "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -080084endif()
85
franramirez688da0f0962015-01-31 11:14:59 -050086if(BIICODE)
87 # Execute biicode building
88 include(CMake/biicode.cmake)
89 return()
90endif(BIICODE)
91
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -080092include_directories(include)
93
Leander Bessa Beernaert3ec8d7f2015-01-02 13:51:31 +010094if(FLATBUFFERS_BUILD_FLATC)
Alex Amesd5753212015-02-13 15:58:29 -080095 add_executable(flatc ${FlatBuffers_Compiler_SRCS})
96endif()
97
98if(FLATBUFFERS_BUILD_FLATC)
99 add_executable(flathash ${FlatHash_SRCS})
Leander Bessa Beernaert3ec8d7f2015-01-02 13:51:31 +0100100endif()
Zbigniew Mandziejewicz3f8700b2014-09-16 00:50:23 +0800101
102function(compile_flatbuffers_schema_to_cpp SRC_FBS)
Wouter van Oortmerssen4cdf3eb2014-10-24 14:26:29 -0700103 get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH)
Wouter van Oortmerssen7b805352014-09-23 11:55:42 -0700104 string(REGEX REPLACE "\\.fbs$" "_generated.h" GEN_HEADER ${SRC_FBS})
Zbigniew Mandziejewicz3f8700b2014-09-16 00:50:23 +0800105 add_custom_command(
106 OUTPUT ${GEN_HEADER}
107 COMMAND flatc -c -o "${SRC_FBS_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
108 DEPENDS flatc)
109endfunction()
110
Stewart Miles3f851832014-09-10 16:01:13 -0700111if(FLATBUFFERS_BUILD_TESTS)
Zbigniew Mandziejewicz3f8700b2014-09-16 00:50:23 +0800112 compile_flatbuffers_schema_to_cpp(tests/monster_test.fbs)
113 include_directories(${CMAKE_CURRENT_BINARY_DIR}/tests)
Stewart Miles3f851832014-09-10 16:01:13 -0700114 add_executable(flattests ${FlatBuffers_Tests_SRCS})
Zbigniew Mandziejewicz3f8700b2014-09-16 00:50:23 +0800115
116 compile_flatbuffers_schema_to_cpp(samples/monster.fbs)
117 include_directories(${CMAKE_CURRENT_BINARY_DIR}/samples)
Stewart Miles3f851832014-09-10 16:01:13 -0700118 add_executable(flatsamplebinary ${FlatBuffers_Sample_Binary_SRCS})
119 add_executable(flatsampletext ${FlatBuffers_Sample_Text_SRCS})
120endif()
Wouter van Oortmerssen26a30732014-01-27 16:52:49 -0800121
Zbigniew Mandziejewicz3f8700b2014-09-16 00:50:23 +0800122if(FLATBUFFERS_INSTALL)
123 install(DIRECTORY include/flatbuffers DESTINATION include)
Leander Bessa Beernaert3ec8d7f2015-01-02 13:51:31 +0100124 if(FLATBUFFERS_BUILD_FLATC)
125 install(TARGETS flatc DESTINATION bin)
126 endif()
Zbigniew Mandziejewicz3f8700b2014-09-16 00:50:23 +0800127endif()
Daniel Nachbaur7a99b3c2014-07-15 15:09:36 +0200128
Stewart Miles3f851832014-09-10 16:01:13 -0700129if(FLATBUFFERS_BUILD_TESTS)
Zbigniew Mandziejewicz3f8700b2014-09-16 00:50:23 +0800130 enable_testing()
131
Wouter van Oortmerssen7b805352014-09-23 11:55:42 -0700132 file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/tests" DESTINATION
133 "${CMAKE_CURRENT_BINARY_DIR}")
Zbigniew Mandziejewicz3f8700b2014-09-16 00:50:23 +0800134 add_test(NAME flattests COMMAND flattests)
Stewart Miles3f851832014-09-10 16:01:13 -0700135endif()