From e75c8a45ad2624637cf7d32925d31ada990af7e2 Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Wed, 16 Dec 2020 18:32:22 +0100 Subject: [PATCH 01/21] cmake: use add_custom_command(OUTPUTS xx.h) + add_executable(.. xx.h) to track dependencies --- samples/monster/CMakeLists.txt | 7 +++--- samples/reflection/CMakeLists.txt | 8 +++--- test/cgen_test/CMakeLists.txt | 10 +++++--- test/emit_test/CMakeLists.txt | 7 +++--- test/flatc_compat/CMakeLists.txt | 10 +++++--- test/json_test/CMakeLists.txt | 30 +++++++++++++---------- test/load_test/CMakeLists.txt | 13 ++++++---- test/monster_test/CMakeLists.txt | 15 ++++++++---- test/monster_test_concat/CMakeLists.txt | 6 ++--- test/monster_test_cpp/CMakeLists.txt | 10 ++++---- test/monster_test_prefix/CMakeLists.txt | 6 ++--- test/monster_test_solo/CMakeLists.txt | 6 ++--- test/optional_scalars_test/CMakeLists.txt | 10 +++++--- test/reflection_test/CMakeLists.txt | 1 - 14 files changed, 75 insertions(+), 64 deletions(-) diff --git a/samples/monster/CMakeLists.txt b/samples/monster/CMakeLists.txt index a14e10990..33420206c 100644 --- a/samples/monster/CMakeLists.txt +++ b/samples/monster/CMakeLists.txt @@ -6,15 +6,14 @@ set(FBS_DIR "${CMAKE_CURRENT_SOURCE_DIR}") include_directories("${GEN_DIR}" "${INC_DIR}") -add_custom_target(gen_monster_fbs ALL) add_custom_command ( - TARGET gen_monster_fbs + OUTPUT "${GEN_DIR}/flatbuffers_common_builder.h" "${GEN_DIR}/flatbuffers_common_reader.h" + "${GEN_DIR}/monster_builder.h" "${GEN_DIR}/monster_reader.h" "${GEN_DIR}/monster_verifier.h" COMMAND cmake -E make_directory "${GEN_DIR}" COMMAND flatcc_cli -a -o "${GEN_DIR}" "${FBS_DIR}/monster.fbs" DEPENDS flatcc_cli "${FBS_DIR}/monster.fbs" ) -add_executable(monster monster.c) -add_dependencies(monster gen_monster_fbs) +add_executable(monster monster.c "${GEN_DIR}/monster_builder.h") target_link_libraries(monster flatccrt) if (FLATCC_TEST) diff --git a/samples/reflection/CMakeLists.txt b/samples/reflection/CMakeLists.txt index db5b75445..553c49d42 100644 --- a/samples/reflection/CMakeLists.txt +++ b/samples/reflection/CMakeLists.txt @@ -15,15 +15,17 @@ set(FBS_DIR "${PROJECT_SOURCE_DIR}/samples/monster") include_directories("${GEN_DIR}" "${INC_DIR}") -add_custom_target(gen_monster_bfbs ALL) add_custom_command ( - TARGET gen_monster_bfbs + OUTPUT "${GEN_DIR}/monster.bfbs" COMMAND cmake -E make_directory "${GEN_DIR}" COMMAND flatcc_cli --schema -o "${GEN_DIR}" "${FBS_DIR}/monster.fbs" DEPENDS flatcc_cli "${FBS_DIR}/monster.fbs" ) +add_custom_target(build_moster_bfbs ALL + DEPENDS "${GEN_DIR}/monster.bfbs" +) + add_executable(bfbs2json bfbs2json.c) -add_dependencies(bfbs2json gen_monster_bfbs) target_link_libraries(bfbs2json flatccrt) if (FLATCC_TEST) diff --git a/test/cgen_test/CMakeLists.txt b/test/cgen_test/CMakeLists.txt index 2edc040c6..50ba920b8 100644 --- a/test/cgen_test/CMakeLists.txt +++ b/test/cgen_test/CMakeLists.txt @@ -12,7 +12,7 @@ target_link_libraries(cgen_test flatcc ) -add_test(cgen_test cgen_test${CMAKE_EXECUTABLE_SUFFIX}) +add_test(cgen_test cgen_test) # Compilation of the generated code tests many import edge cases @@ -24,10 +24,12 @@ add_test(cgen_test cgen_test${CMAKE_EXECUTABLE_SUFFIX}) # see also http://stackoverflow.com/questions/9870162/avoid-quoting-in-cmake-add-custom-command separate_arguments(CUSTOM_C_FLAGS UNIX_COMMAND "${CMAKE_C_FLAGS}") -add_custom_target(test_generated - COMMAND ./cgen_test${CMAKE_EXECUTABLE_SUFFIX} > test_generated${CMAKE_EXECUTABLE_SUFFIX}.c +add_custom_target(test_generated + COMMAND cgen_test > test_generated${CMAKE_EXECUTABLE_SUFFIX}.c COMMAND ${CMAKE_C_COMPILER} ${CUSTOM_C_FLAGS} test_generated${CMAKE_EXECUTABLE_SUFFIX}.c -c - -I${CMAKE_SOURCE_DIR}/include WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/test/cgen_test + -I${CMAKE_SOURCE_DIR}/include + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/test/cgen_test + BYPRODUCTS test_generated${CMAKE_EXECUTABLE_SUFFIX}.c ) add_dependencies(test_generated cgen_test) diff --git a/test/emit_test/CMakeLists.txt b/test/emit_test/CMakeLists.txt index aac5fb790..6d46f3dd4 100644 --- a/test/emit_test/CMakeLists.txt +++ b/test/emit_test/CMakeLists.txt @@ -6,15 +6,14 @@ set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/emit_test") include_directories("${GEN_DIR}" "${INC_DIR}") -add_custom_target(gen_emit_test ALL) add_custom_command ( - TARGET gen_emit_test + OUTPUT "${GEN_DIR}/emit_test_builder.h" "${GEN_DIR}/emit_test_reader.h" "${GEN_DIR}/emit_test_verifier.h" + "${GEN_DIR}/flatbuffers_common_builder.h" "${GEN_DIR}/flatbuffers_common_reader.h" COMMAND cmake -E make_directory "${GEN_DIR}" COMMAND flatcc_cli -a -o "${GEN_DIR}" "${FBS_DIR}/emit_test.fbs" DEPENDS flatcc_cli "${FBS}" ) -add_executable(emit_test emit_test.c) -add_dependencies(emit_test gen_emit_test) +add_executable(emit_test emit_test.c "${GEN_DIR}/emit_test_builder.h") target_link_libraries(emit_test flatccrt) add_test(emit_test emit_test${CMAKE_EXECUTABLE_SUFFIX}) diff --git a/test/flatc_compat/CMakeLists.txt b/test/flatc_compat/CMakeLists.txt index 6a9a4a3f6..3e934378d 100644 --- a/test/flatc_compat/CMakeLists.txt +++ b/test/flatc_compat/CMakeLists.txt @@ -6,16 +6,18 @@ set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") include_directories("${GEN_DIR}" "${INC_DIR}") -add_custom_target(gen_flatc_compat ALL) add_custom_command ( - TARGET gen_flatc_compat + OUTPUT "${GEN_DIR}/attributes_builder.h" "${GEN_DIR}/attributes_reader.h" "${GEN_DIR}/attributes_verifier.h" + "${GEN_DIR}/flatbuffers_common_builder.h" "${GEN_DIR}/flatbuffers_common_reader.h" + "${GEN_DIR}/include_test1_builder.h" "${GEN_DIR}/include_test1_reader.h" "${GEN_DIR}/include_test1_verifier.h" + "${GEN_DIR}/include_test2_builder.h" "${GEN_DIR}/include_test2_reader.h" "${GEN_DIR}/include_test2_verifier.h" + "${GEN_DIR}/monster_test_builder.h" "${GEN_DIR}/monster_test_reader.h" "${GEN_DIR}/monster_test_verifier.h" COMMAND cmake -E make_directory "${GEN_DIR}" COMMAND cmake -E copy "${CMAKE_CURRENT_SOURCE_DIR}/monsterdata_test.mon" ${CMAKE_CURRENT_BINARY_DIR} COMMAND flatcc_cli -a -o "${GEN_DIR}" "${FBS_DIR}/monster_test.fbs" DEPENDS flatcc_cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" ) -add_executable(flatc_compat flatc_compat.c) -add_dependencies(flatc_compat gen_flatc_compat) +add_executable(flatc_compat flatc_compat.c "${GEN_DIR}/monster_test_reader.h" "${GEN_DIR}/monster_test_verifier.h") target_link_libraries(flatc_compat flatccrt) add_test(flatc_compat flatc_compat${CMAKE_EXECUTABLE_SUFFIX}) diff --git a/test/json_test/CMakeLists.txt b/test/json_test/CMakeLists.txt index fec6c7c1a..f99ac0e88 100644 --- a/test/json_test/CMakeLists.txt +++ b/test/json_test/CMakeLists.txt @@ -9,9 +9,18 @@ set(DATA_SRC "${PROJECT_SOURCE_DIR}/test/flatc_compat") include_directories("${GEN_DIR}" "${INC_DIR}") -add_custom_target(gen_monster_test_json ALL) add_custom_command ( - TARGET gen_monster_test_json + OUTPUT "${GEN_DIR}/attributes_builder.h" "${GEN_DIR}/attributes_json_parser.h" + "${GEN_DIR}/attributes_json_printer.h" "${GEN_DIR}/attributes_reader.h" "${GEN_DIR}/attributes_verifier.h" + "${GEN_DIR}/flatbuffers_common_builder.h" "${GEN_DIR}/flatbuffers_common_reader.h" + "${GEN_DIR}/include_test1_builder.h" "${GEN_DIR}/include_test1_json_parser.h" + "${GEN_DIR}/include_test1_json_printer.h" "${GEN_DIR}/include_test1_reader.h" + "${GEN_DIR}/include_test1_verifier.h" "${GEN_DIR}/include_test2_builder.h" + "${GEN_DIR}/include_test2_json_parser.h" "${GEN_DIR}/include_test2_json_printer.h" + "${GEN_DIR}/include_test2_reader.h" "${GEN_DIR}/include_test2_verifier.h" + "${GEN_DIR}/monster_test_builder.h" "${GEN_DIR}/monster_test_json_parser.h" + "${GEN_DIR}/monster_test_json_printer.h" "${GEN_DIR}/monster_test_reader.h" + "${GEN_DIR}/monster_test_verifier.h" COMMAND cmake -E make_directory "${GEN_DIR}" COMMAND cmake -E copy "${DATA_SRC}/monsterdata_test.golden" "${DATA_DST}" COMMAND cmake -E copy "${DATA_SRC}/monsterdata_test.mon" "${DATA_DST}" @@ -20,14 +29,9 @@ add_custom_command ( ) add_executable(test_basic_parse test_basic_parse.c) -add_executable(test_json_parser test_json_parser.c) -add_executable(test_json_printer test_json_printer.c) -add_executable(test_json test_json.c) - -add_dependencies(test_basic_parse gen_monster_test_json) -add_dependencies(test_json_parser gen_monster_test_json) -add_dependencies(test_json_printer gen_monster_test_json) -add_dependencies(test_json gen_monster_test_json) +add_executable(test_json_parser test_json_parser.c "${GEN_DIR}/monster_test_reader.h" "${GEN_DIR}/monster_test_json_parser.h") +add_executable(test_json_printer test_json_printer.c "${GEN_DIR}/monster_test_json_parser.h") +add_executable(test_json test_json.c "${GEN_DIR}/monster_test_json_parser.h" "${GEN_DIR}/monster_test_json_printer.h") target_link_libraries(test_basic_parse flatccrt) target_link_libraries(test_json_parser flatccrt) @@ -51,9 +55,9 @@ set(RTSRC ) macro(jstest trg flags) - add_executable(${trg} test_json.c ${RTSRC}) - add_dependencies(${trg} gen_monster_test_json) - add_test(${trg} ${trg}${CMAKE_EXECUTABLE_SUFFIX}) + add_executable(${trg} test_json.c ${RTSRC} "${GEN_DIR}/monster_test_json_parser.h" + "${GEN_DIR}/monster_test_json_printer.h" "${GEN_DIR}/monster_test_verifier.h") + add_test(${trg} ${trg}) set_target_properties(${trg} PROPERTIES COMPILE_FLAGS ${flags}) endmacro() diff --git a/test/load_test/CMakeLists.txt b/test/load_test/CMakeLists.txt index 6d444a547..9ca007626 100644 --- a/test/load_test/CMakeLists.txt +++ b/test/load_test/CMakeLists.txt @@ -6,15 +6,18 @@ set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") include_directories("${GEN_DIR}" "${INC_DIR}") -add_custom_target(gen_load_test ALL) -add_custom_command ( - TARGET gen_load_test +add_custom_command( + OUTPUT "${GEN_DIR}/attributes_builder.h" "${GEN_DIR}/attributes_reader.h" "${GEN_DIR}/attributes_verifier.h" + "${GEN_DIR}/flatbuffers_common_builder.h" "${GEN_DIR}/flatbuffers_common_reader.h" + "${GEN_DIR}/include_test1_builder.h" "${GEN_DIR}/include_test1_reader.h" "${GEN_DIR}/include_test1_verifier.h" + "${GEN_DIR}/include_test2_builder.h" "${GEN_DIR}/include_test2_reader.h" "${GEN_DIR}/include_test2_verifier.h" + "${GEN_DIR}/monster_test_builder.h" "${GEN_DIR}/monster_test_reader.h" "${GEN_DIR}/monster_test_verifier.h" COMMAND cmake -E make_directory "${GEN_DIR}" COMMAND flatcc_cli -a -o "${GEN_DIR}" "${FBS_DIR}/monster_test.fbs" DEPENDS flatcc_cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" ) -add_executable(load_test load_test.c) -add_dependencies(load_test gen_load_test) +add_executable(load_test load_test.c "${GEN_DIR}/monster_test_builder.h") target_link_libraries(load_test flatccrt) add_test(load_test load_test${CMAKE_EXECUTABLE_SUFFIX}) + diff --git a/test/monster_test/CMakeLists.txt b/test/monster_test/CMakeLists.txt index 818292725..28bd896eb 100644 --- a/test/monster_test/CMakeLists.txt +++ b/test/monster_test/CMakeLists.txt @@ -6,15 +6,20 @@ set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") include_directories("${GEN_DIR}" "${INC_DIR}") -add_custom_target(gen_monster_test ALL) add_custom_command ( - TARGET gen_monster_test + OUTPUT "${GEN_DIR}/attributes_builder.h" "${GEN_DIR}/attributes_reader.h" "${GEN_DIR}/attributes_verifier.h" + "${GEN_DIR}/flatbuffers_common_builder.h" "${GEN_DIR}/flatbuffers_common_reader.h" + "${GEN_DIR}/include_test1_builder.h" "${GEN_DIR}/include_test1_reader.h" + "${GEN_DIR}/include_test1_verifier.h" "${GEN_DIR}/include_test2_builder.h" + "${GEN_DIR}/include_test2_reader.h" "${GEN_DIR}/include_test2_verifier.h" + "${GEN_DIR}/monster_test_builder.h" "${GEN_DIR}/monster_test_reader.h" + "${GEN_DIR}/monster_test_verifier.h" COMMAND cmake -E make_directory "${GEN_DIR}" COMMAND flatcc_cli -a -o "${GEN_DIR}" "${FBS_DIR}/monster_test.fbs" - DEPENDS flatcc_cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" + DEPENDS flatcc_cli "${FBS_DIR}/attributes.fbs" "${FBS_DIR}/monster_test.fbs" + "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" ) -add_executable(monster_test monster_test.c) -add_dependencies(monster_test gen_monster_test) +add_executable(monster_test monster_test.c "${GEN_DIR}/monster_test_builder.h" "${GEN_DIR}/monster_test_verifier.h") target_link_libraries(monster_test flatccrt) add_test(monster_test monster_test${CMAKE_EXECUTABLE_SUFFIX}) diff --git a/test/monster_test_concat/CMakeLists.txt b/test/monster_test_concat/CMakeLists.txt index 836ae09e1..1657ce447 100644 --- a/test/monster_test_concat/CMakeLists.txt +++ b/test/monster_test_concat/CMakeLists.txt @@ -6,16 +6,14 @@ set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") include_directories("${GEN_DIR}" "${INC_DIR}") -add_custom_target(gen_monster_test_concat ALL) add_custom_command ( - TARGET gen_monster_test_concat + OUTPUT "${GEN_DIR}/monster_test.h" COMMAND cmake -E make_directory "${GEN_DIR}" # We could also use the recursive -r option, but this tests adding files manually to the output file. COMMAND flatcc_cli -cwv --reader -o "${GEN_DIR}" "--outfile=monster_test.h" "${FBS_DIR}/attributes.fbs" "${FBS_DIR}/include_test2.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/monster_test.fbs" DEPENDS flatcc_cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" "${FBS_DIR}/attributes.fbs" ) include_directories("${GEN_DIR}" "${INC_DIR}") -add_executable(monster_test_concat monster_test_concat.c) -add_dependencies(monster_test_concat gen_monster_test_concat) +add_executable(monster_test_concat monster_test_concat.c "${GEN_DIR}/monster_test.h") target_link_libraries(monster_test_concat flatccrt) add_test(monster_test_concat monster_test_concat${CMAKE_EXECUTABLE_SUFFIX}) diff --git a/test/monster_test_cpp/CMakeLists.txt b/test/monster_test_cpp/CMakeLists.txt index 22247a394..704bb5d39 100644 --- a/test/monster_test_cpp/CMakeLists.txt +++ b/test/monster_test_cpp/CMakeLists.txt @@ -9,16 +9,16 @@ set(FBS_DIR "${PROJECT_SOURCE_DIR}/samples/monster") include_directories("${GEN_DIR}" "${INC_DIR}") -add_custom_target(gen_monster_test_cpp ALL) -add_custom_command ( - TARGET gen_monster_test_cpp +add_custom_command( + OUTPUT "${GEN_DIR}/flatbuffers_common_builder.h" "${GEN_DIR}/flatbuffers_common_reader.h" + "${GEN_DIR}/monster_builder.h" "${GEN_DIR}/monster_reader.h" "${GEN_DIR}/monster_verifier.h" COMMAND cmake -E make_directory "${GEN_DIR}" COMMAND flatcc_cli -a -o "${GEN_DIR}" "${FBS_DIR}/monster.fbs" DEPENDS flatcc_cli "${FBS_DIR}/monster.fbs" ) -add_executable(monster_test_cpp monster_test.cpp) -add_dependencies(monster_test_cpp gen_monster_test_cpp) +add_executable(monster_test_cpp monster_test.cpp "${GEN_DIR}/monster_builder.h") target_link_libraries(monster_test_cpp flatccrt) add_test(monster_test_cpp monster_test_cpp${CMAKE_EXECUTABLE_SUFFIX}) + diff --git a/test/monster_test_prefix/CMakeLists.txt b/test/monster_test_prefix/CMakeLists.txt index 13461ec36..498816bdb 100644 --- a/test/monster_test_prefix/CMakeLists.txt +++ b/test/monster_test_prefix/CMakeLists.txt @@ -6,15 +6,13 @@ set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") include_directories("${GEN_DIR}" "${INC_DIR}") -add_custom_target(gen_monster_test_prefix ALL) add_custom_command ( - TARGET gen_monster_test_prefix + OUTPUT "${GEN_DIR}/zzz_monster_test.h" COMMAND cmake -E make_directory "${GEN_DIR}" COMMAND flatcc_cli -a --prefix=zzz_ --stdout "${FBS_DIR}/monster_test.fbs" > "${GEN_DIR}/zzz_monster_test.h" DEPENDS flatcc_cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" ) -add_executable(monster_test_prefix monster_test_prefix.c) -add_dependencies(monster_test_prefix gen_monster_test_prefix) +add_executable(monster_test_prefix monster_test_prefix.c "${GEN_DIR}/zzz_monster_test.h") target_link_libraries(monster_test_prefix flatccrt) add_test(monster_test_prefix monster_test_prefix${CMAKE_EXECUTABLE_SUFFIX}) diff --git a/test/monster_test_solo/CMakeLists.txt b/test/monster_test_solo/CMakeLists.txt index b6294b85f..080900c78 100644 --- a/test/monster_test_solo/CMakeLists.txt +++ b/test/monster_test_solo/CMakeLists.txt @@ -6,16 +6,14 @@ set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") include_directories("${GEN_DIR}" "${INC_DIR}") -add_custom_target(gen_monster_test_solo ALL) add_custom_command ( - TARGET gen_monster_test_solo + OUTPUT "${GEN_DIR}/monster_test.h" COMMAND cmake -E make_directory "${GEN_DIR}" COMMAND flatcc_cli -cwv --reader --stdout "${FBS_DIR}/attributes.fbs" "${FBS_DIR}/include_test2.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/monster_test.fbs" > "${GEN_DIR}/monster_test.h" DEPENDS flatcc_cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" "${FBS_DIR}/attributes.fbs" ) include_directories("${GEN_DIR}" "${INC_DIR}") -add_executable(monster_test_solo monster_test_solo.c) -add_dependencies(monster_test_solo gen_monster_test_solo) +add_executable(monster_test_solo monster_test_solo.c "${GEN_DIR}/monster_test.h") target_link_libraries(monster_test_solo flatccrt) add_test(monster_test_solo monster_test_solo${CMAKE_EXECUTABLE_SUFFIX}) diff --git a/test/optional_scalars_test/CMakeLists.txt b/test/optional_scalars_test/CMakeLists.txt index 61a00c71d..f7d92e5ca 100644 --- a/test/optional_scalars_test/CMakeLists.txt +++ b/test/optional_scalars_test/CMakeLists.txt @@ -6,14 +6,16 @@ set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/optional_scalars_test") include_directories("${GEN_DIR}" "${INC_DIR}") -add_custom_target(gen_optional_scalars_test ALL) add_custom_command ( - TARGET gen_optional_scalars_test + OUTPUT "${GEN_DIR}/flatbuffers_common_builder.h" "${GEN_DIR}/flatbuffers_common_reader.h" + "${GEN_DIR}/optional_scalars_test_builder.h" "${GEN_DIR}/optional_scalars_test_json_parser.h" + "${GEN_DIR}/optional_scalars_test_json_printer.h" "${GEN_DIR}/optional_scalars_test_reader.h" + "${GEN_DIR}/optional_scalars_test_verifier.h" COMMAND cmake -E make_directory "${GEN_DIR}" COMMAND flatcc_cli -a --json -o "${GEN_DIR}" "${FBS_DIR}/optional_scalars_test.fbs" ) -add_executable(optional_scalars_test optional_scalars_test.c) -add_dependencies(optional_scalars_test gen_optional_scalars_test) +add_executable(optional_scalars_test optional_scalars_test.c "${GEN_DIR}/optional_scalars_test_builder.h" + "${GEN_DIR}/optional_scalars_test_json_printer.h" "${GEN_DIR}/optional_scalars_test_json_parser.h") target_link_libraries(optional_scalars_test flatccrt) add_test(optional_scalars_test optional_scalars_test${CMAKE_EXECUTABLE_SUFFIX}) diff --git a/test/reflection_test/CMakeLists.txt b/test/reflection_test/CMakeLists.txt index fa1e4b3f5..675c6bd02 100644 --- a/test/reflection_test/CMakeLists.txt +++ b/test/reflection_test/CMakeLists.txt @@ -14,7 +14,6 @@ add_custom_command ( DEPENDS flatcc_cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" ) add_executable(reflection_test reflection_test.c) -add_dependencies(reflection_test gen_reflection_test) target_link_libraries(reflection_test flatccrt) add_test(reflection_test reflection_test${CMAKE_EXECUTABLE_SUFFIX}) From e2316443af501b7e7edacda1400c409fdea40788 Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Wed, 16 Dec 2020 18:32:48 +0100 Subject: [PATCH 02/21] cmake: install cmake targets --- CMakeLists.txt | 4 +++- src/cli/CMakeLists.txt | 7 +++++-- src/compiler/CMakeLists.txt | 7 ++++++- src/runtime/CMakeLists.txt | 8 +++++++- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 40dee3b3c..130eedaa0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -346,5 +346,7 @@ set_target_properties(${dist_targets} if (FLATCC_INSTALL) install(DIRECTORY include/flatcc DESTINATION include) -endif() + install(EXPORT flatcc_exports NAMESPACE flatcc:: DESTINATION ${lib_dir}/cmake/flatcc FILE flatcc-targets.cmake) + include(CMakePackageConfigHelpers) +endif() diff --git a/src/cli/CMakeLists.txt b/src/cli/CMakeLists.txt index 40facacbb..de6e8e65d 100644 --- a/src/cli/CMakeLists.txt +++ b/src/cli/CMakeLists.txt @@ -6,6 +6,7 @@ include_directories ( add_executable(flatcc_cli flatcc_cli.c ) +add_executable(flatcc::cli ALIAS flatcc_cli) target_link_libraries(flatcc_cli flatcc @@ -13,8 +14,10 @@ target_link_libraries(flatcc_cli # Rename because the libflatcc library and the flatcc executable would # conflict if they had the same target name `flatcc`. -set_target_properties(flatcc_cli PROPERTIES OUTPUT_NAME flatcc) +set_target_properties(flatcc_cli PROPERTIES + OUTPUT_NAME flatcc + EXPORT_NAME cli) if (FLATCC_INSTALL) - install(TARGETS flatcc_cli DESTINATION bin) + install(TARGETS flatcc_cli EXPORT flatcc_exports DESTINATION bin) endif() diff --git a/src/compiler/CMakeLists.txt b/src/compiler/CMakeLists.txt index ce31819d9..399f149ee 100644 --- a/src/compiler/CMakeLists.txt +++ b/src/compiler/CMakeLists.txt @@ -37,7 +37,12 @@ if (FLATCC_REFLECTION) endif(FLATCC_REFLECTION) add_library(flatcc ${SOURCES}) +add_library(flatcc::libflatcc ALIAS flatcc) + +target_include_directories(flatcc PUBLIC $) + +set_target_properties(flatcc PROPERTIES EXPORT_NAME libflatcc) if (FLATCC_INSTALL) - install(TARGETS flatcc DESTINATION ${lib_dir}) + install(TARGETS flatcc EXPORT flatcc_exports DESTINATION ${lib_dir}) endif() diff --git a/src/runtime/CMakeLists.txt b/src/runtime/CMakeLists.txt index 127e2a47f..5d638551c 100644 --- a/src/runtime/CMakeLists.txt +++ b/src/runtime/CMakeLists.txt @@ -10,7 +10,13 @@ add_library(flatccrt json_parser.c json_printer.c ) +add_library(flatcc::runtime ALIAS flatccrt) + +target_include_directories(flatccrt PUBLIC $) + +set_target_properties(flatccrt PROPERTIES + EXPORT_NAME runtime) if (FLATCC_INSTALL) - install(TARGETS flatccrt DESTINATION ${lib_dir}) + install(TARGETS flatccrt EXPORT flatcc_exports DESTINATION ${lib_dir}) endif() From 17b346a9e4f7a25296dd17ac0299a9a3c4864568 Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Wed, 16 Dec 2020 19:14:52 +0100 Subject: [PATCH 03/21] cmake: use alias'ed flatcc::(cli|libflatcc|runtime) --- samples/monster/CMakeLists.txt | 11 +++++------ samples/reflection/CMakeLists.txt | 11 +++++------ src/compiler/CMakeLists.txt | 3 +-- src/runtime/CMakeLists.txt | 6 +----- test/cgen_test/CMakeLists.txt | 6 +----- test/emit_test/CMakeLists.txt | 11 +++++------ test/flatc_compat/CMakeLists.txt | 11 +++++------ test/json_test/CMakeLists.txt | 24 +++++++++++------------ test/load_test/CMakeLists.txt | 11 +++++------ test/monster_test/CMakeLists.txt | 11 +++++------ test/monster_test_concat/CMakeLists.txt | 13 ++++++------ test/monster_test_cpp/CMakeLists.txt | 11 +++++------ test/monster_test_prefix/CMakeLists.txt | 11 +++++------ test/monster_test_solo/CMakeLists.txt | 13 ++++++------ test/optional_scalars_test/CMakeLists.txt | 10 +++++----- test/reflection_test/CMakeLists.txt | 11 +++++------ 16 files changed, 79 insertions(+), 95 deletions(-) diff --git a/samples/monster/CMakeLists.txt b/samples/monster/CMakeLists.txt index 33420206c..a9b9fde72 100644 --- a/samples/monster/CMakeLists.txt +++ b/samples/monster/CMakeLists.txt @@ -1,21 +1,20 @@ include(CTest) -set(INC_DIR "${PROJECT_SOURCE_DIR}/include") set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${CMAKE_CURRENT_SOURCE_DIR}") -include_directories("${GEN_DIR}" "${INC_DIR}") +include_directories("${GEN_DIR}") add_custom_command ( OUTPUT "${GEN_DIR}/flatbuffers_common_builder.h" "${GEN_DIR}/flatbuffers_common_reader.h" "${GEN_DIR}/monster_builder.h" "${GEN_DIR}/monster_reader.h" "${GEN_DIR}/monster_verifier.h" COMMAND cmake -E make_directory "${GEN_DIR}" - COMMAND flatcc_cli -a -o "${GEN_DIR}" "${FBS_DIR}/monster.fbs" - DEPENDS flatcc_cli "${FBS_DIR}/monster.fbs" + COMMAND flatcc::cli -a -o "${GEN_DIR}" "${FBS_DIR}/monster.fbs" + DEPENDS flatcc::cli "${FBS_DIR}/monster.fbs" ) add_executable(monster monster.c "${GEN_DIR}/monster_builder.h") -target_link_libraries(monster flatccrt) +target_link_libraries(monster flatcc::runtime) if (FLATCC_TEST) - add_test(monster monster${CMAKE_EXECUTABLE_SUFFIX}) + add_test(monster monster) endif() diff --git a/samples/reflection/CMakeLists.txt b/samples/reflection/CMakeLists.txt index 553c49d42..8e91798fe 100644 --- a/samples/reflection/CMakeLists.txt +++ b/samples/reflection/CMakeLists.txt @@ -9,25 +9,24 @@ include(CTest) # sample, and the actual C source of this project. # -set(INC_DIR "${PROJECT_SOURCE_DIR}/include") set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/samples/monster") -include_directories("${GEN_DIR}" "${INC_DIR}") +include_directories("${GEN_DIR}") add_custom_command ( OUTPUT "${GEN_DIR}/monster.bfbs" COMMAND cmake -E make_directory "${GEN_DIR}" - COMMAND flatcc_cli --schema -o "${GEN_DIR}" "${FBS_DIR}/monster.fbs" - DEPENDS flatcc_cli "${FBS_DIR}/monster.fbs" + COMMAND flatcc::cli --schema -o "${GEN_DIR}" "${FBS_DIR}/monster.fbs" + DEPENDS flatcc::cli "${FBS_DIR}/monster.fbs" ) add_custom_target(build_moster_bfbs ALL DEPENDS "${GEN_DIR}/monster.bfbs" ) add_executable(bfbs2json bfbs2json.c) -target_link_libraries(bfbs2json flatccrt) +target_link_libraries(bfbs2json flatcc::runtime) if (FLATCC_TEST) - add_test(bfbs2json bfbs2json${CMAKE_EXECUTABLE_SUFFIX} ${GEN_DIR}/monster.bfbs) + add_test(bfbs2json bfbs2json "${GEN_DIR}/monster.bfbs") endif() diff --git a/src/compiler/CMakeLists.txt b/src/compiler/CMakeLists.txt index 399f149ee..2663259c9 100644 --- a/src/compiler/CMakeLists.txt +++ b/src/compiler/CMakeLists.txt @@ -1,6 +1,5 @@ include_directories ( "${PROJECT_SOURCE_DIR}/external" - "${PROJECT_SOURCE_DIR}/include" "${PROJECT_SOURCE_DIR}/config" ) @@ -39,7 +38,7 @@ endif(FLATCC_REFLECTION) add_library(flatcc ${SOURCES}) add_library(flatcc::libflatcc ALIAS flatcc) -target_include_directories(flatcc PUBLIC $) +target_include_directories(flatcc PUBLIC $ $) set_target_properties(flatcc PROPERTIES EXPORT_NAME libflatcc) diff --git a/src/runtime/CMakeLists.txt b/src/runtime/CMakeLists.txt index 5d638551c..f1aecef44 100644 --- a/src/runtime/CMakeLists.txt +++ b/src/runtime/CMakeLists.txt @@ -1,7 +1,3 @@ -include_directories ( - "${PROJECT_SOURCE_DIR}/include" -) - add_library(flatccrt builder.c emitter.c @@ -12,7 +8,7 @@ add_library(flatccrt ) add_library(flatcc::runtime ALIAS flatccrt) -target_include_directories(flatccrt PUBLIC $) +target_include_directories(flatccrt PUBLIC $ $) set_target_properties(flatccrt PROPERTIES EXPORT_NAME runtime) diff --git a/test/cgen_test/CMakeLists.txt b/test/cgen_test/CMakeLists.txt index 50ba920b8..d307c31d6 100644 --- a/test/cgen_test/CMakeLists.txt +++ b/test/cgen_test/CMakeLists.txt @@ -1,15 +1,11 @@ include(CTest) -include_directories ( - "${PROJECT_SOURCE_DIR}/include" -) - add_executable(cgen_test cgen_test.c ) target_link_libraries(cgen_test - flatcc + flatcc::libflatcc ) add_test(cgen_test cgen_test) diff --git a/test/emit_test/CMakeLists.txt b/test/emit_test/CMakeLists.txt index 6d46f3dd4..1337ce073 100644 --- a/test/emit_test/CMakeLists.txt +++ b/test/emit_test/CMakeLists.txt @@ -1,19 +1,18 @@ include(CTest) -set(INC_DIR "${PROJECT_SOURCE_DIR}/include") set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/emit_test") -include_directories("${GEN_DIR}" "${INC_DIR}") +include_directories("${GEN_DIR}") add_custom_command ( OUTPUT "${GEN_DIR}/emit_test_builder.h" "${GEN_DIR}/emit_test_reader.h" "${GEN_DIR}/emit_test_verifier.h" "${GEN_DIR}/flatbuffers_common_builder.h" "${GEN_DIR}/flatbuffers_common_reader.h" COMMAND cmake -E make_directory "${GEN_DIR}" - COMMAND flatcc_cli -a -o "${GEN_DIR}" "${FBS_DIR}/emit_test.fbs" - DEPENDS flatcc_cli "${FBS}" + COMMAND flatcc::cli -a -o "${GEN_DIR}" "${FBS_DIR}/emit_test.fbs" + DEPENDS flatcc::cli "${FBS}" ) add_executable(emit_test emit_test.c "${GEN_DIR}/emit_test_builder.h") -target_link_libraries(emit_test flatccrt) +target_link_libraries(emit_test flatcc::runtime) -add_test(emit_test emit_test${CMAKE_EXECUTABLE_SUFFIX}) +add_test(emit_test emit_test) diff --git a/test/flatc_compat/CMakeLists.txt b/test/flatc_compat/CMakeLists.txt index 3e934378d..603c7c83c 100644 --- a/test/flatc_compat/CMakeLists.txt +++ b/test/flatc_compat/CMakeLists.txt @@ -1,10 +1,9 @@ include(CTest) -set(INC_DIR "${PROJECT_SOURCE_DIR}/include") set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") -include_directories("${GEN_DIR}" "${INC_DIR}") +include_directories("${GEN_DIR}") add_custom_command ( OUTPUT "${GEN_DIR}/attributes_builder.h" "${GEN_DIR}/attributes_reader.h" "${GEN_DIR}/attributes_verifier.h" @@ -14,10 +13,10 @@ add_custom_command ( "${GEN_DIR}/monster_test_builder.h" "${GEN_DIR}/monster_test_reader.h" "${GEN_DIR}/monster_test_verifier.h" COMMAND cmake -E make_directory "${GEN_DIR}" COMMAND cmake -E copy "${CMAKE_CURRENT_SOURCE_DIR}/monsterdata_test.mon" ${CMAKE_CURRENT_BINARY_DIR} - COMMAND flatcc_cli -a -o "${GEN_DIR}" "${FBS_DIR}/monster_test.fbs" - DEPENDS flatcc_cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" + COMMAND flatcc::cli -a -o "${GEN_DIR}" "${FBS_DIR}/monster_test.fbs" + DEPENDS flatcc::cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" ) add_executable(flatc_compat flatc_compat.c "${GEN_DIR}/monster_test_reader.h" "${GEN_DIR}/monster_test_verifier.h") -target_link_libraries(flatc_compat flatccrt) +target_link_libraries(flatc_compat flatcc::runtime) -add_test(flatc_compat flatc_compat${CMAKE_EXECUTABLE_SUFFIX}) +add_test(flatc_compat flatc_compat) diff --git a/test/json_test/CMakeLists.txt b/test/json_test/CMakeLists.txt index f99ac0e88..eab036c9d 100644 --- a/test/json_test/CMakeLists.txt +++ b/test/json_test/CMakeLists.txt @@ -1,13 +1,12 @@ include(CTest) -set(INC_DIR "${PROJECT_SOURCE_DIR}/include") set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") set(DATA_DST "${CMAKE_CURRENT_BINARY_DIR}") set(DATA_SRC "${PROJECT_SOURCE_DIR}/test/flatc_compat") -include_directories("${GEN_DIR}" "${INC_DIR}") +include_directories("${GEN_DIR}") add_custom_command ( OUTPUT "${GEN_DIR}/attributes_builder.h" "${GEN_DIR}/attributes_json_parser.h" @@ -24,8 +23,8 @@ add_custom_command ( COMMAND cmake -E make_directory "${GEN_DIR}" COMMAND cmake -E copy "${DATA_SRC}/monsterdata_test.golden" "${DATA_DST}" COMMAND cmake -E copy "${DATA_SRC}/monsterdata_test.mon" "${DATA_DST}" - COMMAND flatcc_cli -av --json -o "${GEN_DIR}" "${FBS_DIR}/monster_test.fbs" - DEPENDS flatcc_cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" + COMMAND flatcc::cli -av --json -o "${GEN_DIR}" "${FBS_DIR}/monster_test.fbs" + DEPENDS flatcc::cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" ) add_executable(test_basic_parse test_basic_parse.c) @@ -33,15 +32,15 @@ add_executable(test_json_parser test_json_parser.c "${GEN_DIR}/monster_test_read add_executable(test_json_printer test_json_printer.c "${GEN_DIR}/monster_test_json_parser.h") add_executable(test_json test_json.c "${GEN_DIR}/monster_test_json_parser.h" "${GEN_DIR}/monster_test_json_printer.h") -target_link_libraries(test_basic_parse flatccrt) -target_link_libraries(test_json_parser flatccrt) -target_link_libraries(test_json_printer flatccrt) -target_link_libraries(test_json flatccrt) +target_link_libraries(test_basic_parse PRIVATE flatcc::runtime) +target_link_libraries(test_json_parser PRIVATE flatcc::runtime) +target_link_libraries(test_json_printer PRIVATE flatcc::runtime) +target_link_libraries(test_json PRIVATE flatcc::runtime) -add_test(test_basic_parse test_basic_parse${CMAKE_EXECUTABLE_SUFFIX}) -add_test(test_json_parser test_json_parser${CMAKE_EXECUTABLE_SUFFIX}) -add_test(test_json_printer test_json_printer${CMAKE_EXECUTABLE_SUFFIX}) -add_test(test_json test_json${CMAKE_EXECUTABLE_SUFFIX}) +add_test(test_basic_parse test_basic_parse) +add_test(test_json_parser test_json_parser) +add_test(test_json_printer test_json_printer) +add_test(test_json test_json) # Compile without default library in order to test various runtime flags set(RTPATH "${PROJECT_SOURCE_DIR}/src/runtime") @@ -57,6 +56,7 @@ set(RTSRC macro(jstest trg flags) add_executable(${trg} test_json.c ${RTSRC} "${GEN_DIR}/monster_test_json_parser.h" "${GEN_DIR}/monster_test_json_printer.h" "${GEN_DIR}/monster_test_verifier.h") + target_include_directories(${trg} PRIVATE ${PROJECT_SOURCE_DIR}/include) add_test(${trg} ${trg}) set_target_properties(${trg} PROPERTIES COMPILE_FLAGS ${flags}) endmacro() diff --git a/test/load_test/CMakeLists.txt b/test/load_test/CMakeLists.txt index 9ca007626..138d5a95d 100644 --- a/test/load_test/CMakeLists.txt +++ b/test/load_test/CMakeLists.txt @@ -1,10 +1,9 @@ include(CTest) -set(INC_DIR "${PROJECT_SOURCE_DIR}/include") set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") -include_directories("${GEN_DIR}" "${INC_DIR}") +include_directories("${GEN_DIR}") add_custom_command( OUTPUT "${GEN_DIR}/attributes_builder.h" "${GEN_DIR}/attributes_reader.h" "${GEN_DIR}/attributes_verifier.h" @@ -13,11 +12,11 @@ add_custom_command( "${GEN_DIR}/include_test2_builder.h" "${GEN_DIR}/include_test2_reader.h" "${GEN_DIR}/include_test2_verifier.h" "${GEN_DIR}/monster_test_builder.h" "${GEN_DIR}/monster_test_reader.h" "${GEN_DIR}/monster_test_verifier.h" COMMAND cmake -E make_directory "${GEN_DIR}" - COMMAND flatcc_cli -a -o "${GEN_DIR}" "${FBS_DIR}/monster_test.fbs" - DEPENDS flatcc_cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" + COMMAND flatcc::cli -a -o "${GEN_DIR}" "${FBS_DIR}/monster_test.fbs" + DEPENDS flatcc::cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" ) add_executable(load_test load_test.c "${GEN_DIR}/monster_test_builder.h") -target_link_libraries(load_test flatccrt) +target_link_libraries(load_test flatcc::runtime) -add_test(load_test load_test${CMAKE_EXECUTABLE_SUFFIX}) +add_test(load_test load_test) diff --git a/test/monster_test/CMakeLists.txt b/test/monster_test/CMakeLists.txt index 28bd896eb..67a5f6ed4 100644 --- a/test/monster_test/CMakeLists.txt +++ b/test/monster_test/CMakeLists.txt @@ -1,10 +1,9 @@ include(CTest) -set(INC_DIR "${PROJECT_SOURCE_DIR}/include") set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") -include_directories("${GEN_DIR}" "${INC_DIR}") +include_directories("${GEN_DIR}") add_custom_command ( OUTPUT "${GEN_DIR}/attributes_builder.h" "${GEN_DIR}/attributes_reader.h" "${GEN_DIR}/attributes_verifier.h" @@ -15,11 +14,11 @@ add_custom_command ( "${GEN_DIR}/monster_test_builder.h" "${GEN_DIR}/monster_test_reader.h" "${GEN_DIR}/monster_test_verifier.h" COMMAND cmake -E make_directory "${GEN_DIR}" - COMMAND flatcc_cli -a -o "${GEN_DIR}" "${FBS_DIR}/monster_test.fbs" - DEPENDS flatcc_cli "${FBS_DIR}/attributes.fbs" "${FBS_DIR}/monster_test.fbs" + COMMAND flatcc::cli -a -o "${GEN_DIR}" "${FBS_DIR}/monster_test.fbs" + DEPENDS flatcc::cli "${FBS_DIR}/attributes.fbs" "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" ) add_executable(monster_test monster_test.c "${GEN_DIR}/monster_test_builder.h" "${GEN_DIR}/monster_test_verifier.h") -target_link_libraries(monster_test flatccrt) +target_link_libraries(monster_test flatcc::runtime) -add_test(monster_test monster_test${CMAKE_EXECUTABLE_SUFFIX}) +add_test(monster_test monster_test) diff --git a/test/monster_test_concat/CMakeLists.txt b/test/monster_test_concat/CMakeLists.txt index 1657ce447..482344fb8 100644 --- a/test/monster_test_concat/CMakeLists.txt +++ b/test/monster_test_concat/CMakeLists.txt @@ -1,19 +1,20 @@ include(CTest) -set(INC_DIR "${PROJECT_SOURCE_DIR}/include") set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") -include_directories("${GEN_DIR}" "${INC_DIR}") +include_directories("${GEN_DIR}") add_custom_command ( OUTPUT "${GEN_DIR}/monster_test.h" COMMAND cmake -E make_directory "${GEN_DIR}" # We could also use the recursive -r option, but this tests adding files manually to the output file. - COMMAND flatcc_cli -cwv --reader -o "${GEN_DIR}" "--outfile=monster_test.h" "${FBS_DIR}/attributes.fbs" "${FBS_DIR}/include_test2.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/monster_test.fbs" DEPENDS flatcc_cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" "${FBS_DIR}/attributes.fbs" + COMMAND flatcc::cli -cwv --reader -o "${GEN_DIR}" "--outfile=monster_test.h" "${FBS_DIR}/attributes.fbs" + "${FBS_DIR}/include_test2.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/monster_test.fbs" + DEPENDS flatcc::cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" + "${FBS_DIR}/include_test2.fbs" "${FBS_DIR}/attributes.fbs" ) -include_directories("${GEN_DIR}" "${INC_DIR}") add_executable(monster_test_concat monster_test_concat.c "${GEN_DIR}/monster_test.h") -target_link_libraries(monster_test_concat flatccrt) +target_link_libraries(monster_test_concat flatcc::runtime) -add_test(monster_test_concat monster_test_concat${CMAKE_EXECUTABLE_SUFFIX}) +add_test(monster_test_concat monster_test_concat) diff --git a/test/monster_test_cpp/CMakeLists.txt b/test/monster_test_cpp/CMakeLists.txt index 704bb5d39..346c72233 100644 --- a/test/monster_test_cpp/CMakeLists.txt +++ b/test/monster_test_cpp/CMakeLists.txt @@ -2,23 +2,22 @@ include(CTest) # Note: This re-uses the samples/monster fbs and .c file. -set(INC_DIR "${PROJECT_SOURCE_DIR}/include") # We use our own separate gen dir so we don't clash with the real monster sample. set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated/monster_test_cpp") set(FBS_DIR "${PROJECT_SOURCE_DIR}/samples/monster") -include_directories("${GEN_DIR}" "${INC_DIR}") +include_directories("${GEN_DIR}") add_custom_command( OUTPUT "${GEN_DIR}/flatbuffers_common_builder.h" "${GEN_DIR}/flatbuffers_common_reader.h" "${GEN_DIR}/monster_builder.h" "${GEN_DIR}/monster_reader.h" "${GEN_DIR}/monster_verifier.h" COMMAND cmake -E make_directory "${GEN_DIR}" - COMMAND flatcc_cli -a -o "${GEN_DIR}" "${FBS_DIR}/monster.fbs" - DEPENDS flatcc_cli "${FBS_DIR}/monster.fbs" + COMMAND flatcc::cli -a -o "${GEN_DIR}" "${FBS_DIR}/monster.fbs" + DEPENDS flatcc::cli "${FBS_DIR}/monster.fbs" ) add_executable(monster_test_cpp monster_test.cpp "${GEN_DIR}/monster_builder.h") -target_link_libraries(monster_test_cpp flatccrt) +target_link_libraries(monster_test_cpp flatcc::runtime) -add_test(monster_test_cpp monster_test_cpp${CMAKE_EXECUTABLE_SUFFIX}) +add_test(monster_test_cpp monster_test_cpp) diff --git a/test/monster_test_prefix/CMakeLists.txt b/test/monster_test_prefix/CMakeLists.txt index 498816bdb..2993b73ee 100644 --- a/test/monster_test_prefix/CMakeLists.txt +++ b/test/monster_test_prefix/CMakeLists.txt @@ -1,18 +1,17 @@ include(CTest) -set(INC_DIR "${PROJECT_SOURCE_DIR}/include") set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") -include_directories("${GEN_DIR}" "${INC_DIR}") +include_directories("${GEN_DIR}") add_custom_command ( OUTPUT "${GEN_DIR}/zzz_monster_test.h" COMMAND cmake -E make_directory "${GEN_DIR}" - COMMAND flatcc_cli -a --prefix=zzz_ --stdout "${FBS_DIR}/monster_test.fbs" > "${GEN_DIR}/zzz_monster_test.h" - DEPENDS flatcc_cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" + COMMAND flatcc::cli -a --prefix=zzz_ --stdout "${FBS_DIR}/monster_test.fbs" > "${GEN_DIR}/zzz_monster_test.h" + DEPENDS flatcc::cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" ) add_executable(monster_test_prefix monster_test_prefix.c "${GEN_DIR}/zzz_monster_test.h") -target_link_libraries(monster_test_prefix flatccrt) +target_link_libraries(monster_test_prefix flatcc::runtime) -add_test(monster_test_prefix monster_test_prefix${CMAKE_EXECUTABLE_SUFFIX}) +add_test(monster_test_prefix monster_test_prefix) diff --git a/test/monster_test_solo/CMakeLists.txt b/test/monster_test_solo/CMakeLists.txt index 080900c78..ef4da0ea2 100644 --- a/test/monster_test_solo/CMakeLists.txt +++ b/test/monster_test_solo/CMakeLists.txt @@ -1,19 +1,20 @@ include(CTest) -set(INC_DIR "${PROJECT_SOURCE_DIR}/include") set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") -include_directories("${GEN_DIR}" "${INC_DIR}") +include_directories("${GEN_DIR}") add_custom_command ( OUTPUT "${GEN_DIR}/monster_test.h" COMMAND cmake -E make_directory "${GEN_DIR}" - COMMAND flatcc_cli -cwv --reader --stdout "${FBS_DIR}/attributes.fbs" "${FBS_DIR}/include_test2.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/monster_test.fbs" > "${GEN_DIR}/monster_test.h" DEPENDS flatcc_cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" "${FBS_DIR}/attributes.fbs" + COMMAND flatcc::cli -cwv --reader --stdout "${FBS_DIR}/attributes.fbs" "${FBS_DIR}/include_test2.fbs" + "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/monster_test.fbs" > "${GEN_DIR}/monster_test.h" + DEPENDS flatcc::cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" + "${FBS_DIR}/include_test2.fbs" "${FBS_DIR}/attributes.fbs" ) -include_directories("${GEN_DIR}" "${INC_DIR}") add_executable(monster_test_solo monster_test_solo.c "${GEN_DIR}/monster_test.h") -target_link_libraries(monster_test_solo flatccrt) +target_link_libraries(monster_test_solo flatcc::runtime) -add_test(monster_test_solo monster_test_solo${CMAKE_EXECUTABLE_SUFFIX}) +add_test(monster_test_solo monster_test_solo) diff --git a/test/optional_scalars_test/CMakeLists.txt b/test/optional_scalars_test/CMakeLists.txt index f7d92e5ca..216e9e54a 100644 --- a/test/optional_scalars_test/CMakeLists.txt +++ b/test/optional_scalars_test/CMakeLists.txt @@ -1,10 +1,9 @@ include(CTest) -set(INC_DIR "${PROJECT_SOURCE_DIR}/include") set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/optional_scalars_test") -include_directories("${GEN_DIR}" "${INC_DIR}") +include_directories("${GEN_DIR}") add_custom_command ( OUTPUT "${GEN_DIR}/flatbuffers_common_builder.h" "${GEN_DIR}/flatbuffers_common_reader.h" @@ -12,10 +11,11 @@ add_custom_command ( "${GEN_DIR}/optional_scalars_test_json_printer.h" "${GEN_DIR}/optional_scalars_test_reader.h" "${GEN_DIR}/optional_scalars_test_verifier.h" COMMAND cmake -E make_directory "${GEN_DIR}" - COMMAND flatcc_cli -a --json -o "${GEN_DIR}" "${FBS_DIR}/optional_scalars_test.fbs" + COMMAND flatcc::cli -a --json -o "${GEN_DIR}" "${FBS_DIR}/optional_scalars_test.fbs" + DEPENDS flatcc::cli "${FBS_DIR}/optional_scalars_test.fbs" ) add_executable(optional_scalars_test optional_scalars_test.c "${GEN_DIR}/optional_scalars_test_builder.h" "${GEN_DIR}/optional_scalars_test_json_printer.h" "${GEN_DIR}/optional_scalars_test_json_parser.h") -target_link_libraries(optional_scalars_test flatccrt) +target_link_libraries(optional_scalars_test flatcc::runtime) -add_test(optional_scalars_test optional_scalars_test${CMAKE_EXECUTABLE_SUFFIX}) +add_test(optional_scalars_test optional_scalars_test) diff --git a/test/reflection_test/CMakeLists.txt b/test/reflection_test/CMakeLists.txt index 675c6bd02..db455b9e5 100644 --- a/test/reflection_test/CMakeLists.txt +++ b/test/reflection_test/CMakeLists.txt @@ -1,19 +1,18 @@ include(CTest) -set(INC_DIR "${PROJECT_SOURCE_DIR}/include") set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") -include_directories("${GEN_DIR}" "${INC_DIR}") +include_directories("${GEN_DIR}") add_custom_target(gen_reflection_test ALL) add_custom_command ( TARGET gen_reflection_test COMMAND cmake -E make_directory "${GEN_DIR}" - COMMAND flatcc_cli --schema -o "${GEN_DIR}" "${FBS_DIR}/monster_test.fbs" - DEPENDS flatcc_cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" + COMMAND flatcc::cli --schema -o "${GEN_DIR}" "${FBS_DIR}/monster_test.fbs" + DEPENDS flatcc::cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" ) add_executable(reflection_test reflection_test.c) -target_link_libraries(reflection_test flatccrt) +target_link_libraries(reflection_test flatcc::runtime) -add_test(reflection_test reflection_test${CMAKE_EXECUTABLE_SUFFIX}) +add_test(reflection_test reflection_test) From 7d836bb6ab1c2953d747748245b1875ac99cb684 Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Wed, 16 Dec 2020 19:19:36 +0100 Subject: [PATCH 04/21] cmake: generate + install flatcc-config.cmake --- CMakeLists.txt | 3 +++ flatcc-config.cmake.in | 3 +++ 2 files changed, 6 insertions(+) create mode 100644 flatcc-config.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 130eedaa0..644aa9301 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -349,4 +349,7 @@ if (FLATCC_INSTALL) install(EXPORT flatcc_exports NAMESPACE flatcc:: DESTINATION ${lib_dir}/cmake/flatcc FILE flatcc-targets.cmake) include(CMakePackageConfigHelpers) + configure_package_config_file(flatcc-config.cmake.in flatcc-config.cmake + INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/flatcc") + install(FILES "${CMAKE_CURRENT_BINARY_DIR}/flatcc-config.cmake" DESTINATION "lib/cmake/flatcc") endif() diff --git a/flatcc-config.cmake.in b/flatcc-config.cmake.in new file mode 100644 index 000000000..6cec0493e --- /dev/null +++ b/flatcc-config.cmake.in @@ -0,0 +1,3 @@ +@PACKAGE_INIT@ + +include("${CMAKE_CURRENT_LIST_DIR}/flatcc-targets.cmake") From 30bfbaff50c3d359e2a6b50b27f8141efbf52d8c Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Wed, 16 Dec 2020 21:49:52 +0100 Subject: [PATCH 05/21] cmake: use add_Test(NAME .. COMMAND ..) to support commands with executable target --- external/hash/CMakeLists.txt | 10 +++++----- samples/monster/CMakeLists.txt | 2 +- samples/reflection/CMakeLists.txt | 2 +- test/cgen_test/CMakeLists.txt | 4 ++-- test/emit_test/CMakeLists.txt | 2 +- test/flatc_compat/CMakeLists.txt | 2 +- test/json_test/CMakeLists.txt | 10 +++++----- test/load_test/CMakeLists.txt | 2 +- test/monster_test/CMakeLists.txt | 2 +- test/monster_test_concat/CMakeLists.txt | 2 +- test/monster_test_cpp/CMakeLists.txt | 2 +- test/monster_test_prefix/CMakeLists.txt | 2 +- test/monster_test_solo/CMakeLists.txt | 2 +- test/optional_scalars_test/CMakeLists.txt | 2 +- test/reflection_test/CMakeLists.txt | 2 +- 15 files changed, 24 insertions(+), 24 deletions(-) diff --git a/external/hash/CMakeLists.txt b/external/hash/CMakeLists.txt index 7b7d990dd..a00f660b5 100644 --- a/external/hash/CMakeLists.txt +++ b/external/hash/CMakeLists.txt @@ -29,10 +29,10 @@ add_executable (load_test_d_rh load_test.c ptr_set.c cmetrohash64.c) target_compile_definitions(load_test_rh PRIVATE -DPTR_SET_RH) -add_test(hash_test hash_test) -add_test(hash_test_32 hash_test_32) -add_test(hash_test_rh hash_test_rh) -add_test(load_test load_test) -add_test(load_test_rh load_test_rh) +add_test(NAME hash_test COMMAND hash_test) +add_test(NAME hash_test_32 COMMAND hash_test_32) +add_test(NAME hash_test_rh COMMAND hash_test_rh) +add_test(NAME load_test COMMAND load_test) +add_test(NAME load_test_rh COMMAND load_test_rh) enable_testing() diff --git a/samples/monster/CMakeLists.txt b/samples/monster/CMakeLists.txt index a9b9fde72..369461905 100644 --- a/samples/monster/CMakeLists.txt +++ b/samples/monster/CMakeLists.txt @@ -16,5 +16,5 @@ add_executable(monster monster.c "${GEN_DIR}/monster_builder.h") target_link_libraries(monster flatcc::runtime) if (FLATCC_TEST) - add_test(monster monster) + add_test(NAME monster COMMAND monster) endif() diff --git a/samples/reflection/CMakeLists.txt b/samples/reflection/CMakeLists.txt index 8e91798fe..d0d7c9293 100644 --- a/samples/reflection/CMakeLists.txt +++ b/samples/reflection/CMakeLists.txt @@ -28,5 +28,5 @@ add_executable(bfbs2json bfbs2json.c) target_link_libraries(bfbs2json flatcc::runtime) if (FLATCC_TEST) - add_test(bfbs2json bfbs2json "${GEN_DIR}/monster.bfbs") + add_test(NAME bfbs2json COMMAND bfbs2json "${GEN_DIR}/monster.bfbs") endif() diff --git a/test/cgen_test/CMakeLists.txt b/test/cgen_test/CMakeLists.txt index d307c31d6..ffe2ffd1a 100644 --- a/test/cgen_test/CMakeLists.txt +++ b/test/cgen_test/CMakeLists.txt @@ -8,7 +8,7 @@ target_link_libraries(cgen_test flatcc::libflatcc ) -add_test(cgen_test cgen_test) +add_test(NAME cgen_test COMMAND cgen_test) # Compilation of the generated code tests many import edge cases @@ -37,5 +37,5 @@ if(${CMAKE_MAKE_PROGRAM} MATCHES make) # visible so build fails on not found in the custom build # stage where CMAKE_C_COMPILER uses a compiler call that has this # behavior -#add_test(test_generated ${CMAKE_MAKE_PROGRAM} test_generated) +#add_test(NAME test_generated COMMAND ${CMAKE_MAKE_PROGRAM} test_generated) endif(${CMAKE_MAKE_PROGRAM} MATCHES make) diff --git a/test/emit_test/CMakeLists.txt b/test/emit_test/CMakeLists.txt index 1337ce073..efb3d5487 100644 --- a/test/emit_test/CMakeLists.txt +++ b/test/emit_test/CMakeLists.txt @@ -15,4 +15,4 @@ add_custom_command ( add_executable(emit_test emit_test.c "${GEN_DIR}/emit_test_builder.h") target_link_libraries(emit_test flatcc::runtime) -add_test(emit_test emit_test) +add_test(NAME emit_test COMMAND emit_test) diff --git a/test/flatc_compat/CMakeLists.txt b/test/flatc_compat/CMakeLists.txt index 603c7c83c..b1e5762ca 100644 --- a/test/flatc_compat/CMakeLists.txt +++ b/test/flatc_compat/CMakeLists.txt @@ -19,4 +19,4 @@ add_custom_command ( add_executable(flatc_compat flatc_compat.c "${GEN_DIR}/monster_test_reader.h" "${GEN_DIR}/monster_test_verifier.h") target_link_libraries(flatc_compat flatcc::runtime) -add_test(flatc_compat flatc_compat) +add_test(NAME flatc_compat COMMAND flatc_compat) diff --git a/test/json_test/CMakeLists.txt b/test/json_test/CMakeLists.txt index eab036c9d..023f66826 100644 --- a/test/json_test/CMakeLists.txt +++ b/test/json_test/CMakeLists.txt @@ -37,10 +37,10 @@ target_link_libraries(test_json_parser PRIVATE flatcc::runtime) target_link_libraries(test_json_printer PRIVATE flatcc::runtime) target_link_libraries(test_json PRIVATE flatcc::runtime) -add_test(test_basic_parse test_basic_parse) -add_test(test_json_parser test_json_parser) -add_test(test_json_printer test_json_printer) -add_test(test_json test_json) +add_test(NAME test_basic_parse COMMAND test_basic_parse) +add_test(NAME test_json_parser COMMAND test_json_parser) +add_test(NAME test_json_printer COMMAND test_json_printer) +add_test(NAME test_json COMMAND test_json) # Compile without default library in order to test various runtime flags set(RTPATH "${PROJECT_SOURCE_DIR}/src/runtime") @@ -57,7 +57,7 @@ macro(jstest trg flags) add_executable(${trg} test_json.c ${RTSRC} "${GEN_DIR}/monster_test_json_parser.h" "${GEN_DIR}/monster_test_json_printer.h" "${GEN_DIR}/monster_test_verifier.h") target_include_directories(${trg} PRIVATE ${PROJECT_SOURCE_DIR}/include) - add_test(${trg} ${trg}) + add_test(NAME ${trg} COMMAND ${trg}) set_target_properties(${trg} PROPERTIES COMPILE_FLAGS ${flags}) endmacro() diff --git a/test/load_test/CMakeLists.txt b/test/load_test/CMakeLists.txt index 138d5a95d..3d66e8ca6 100644 --- a/test/load_test/CMakeLists.txt +++ b/test/load_test/CMakeLists.txt @@ -18,5 +18,5 @@ add_custom_command( add_executable(load_test load_test.c "${GEN_DIR}/monster_test_builder.h") target_link_libraries(load_test flatcc::runtime) -add_test(load_test load_test) +add_test(NAME load_test COMMAND load_test) diff --git a/test/monster_test/CMakeLists.txt b/test/monster_test/CMakeLists.txt index 67a5f6ed4..9b1b2605e 100644 --- a/test/monster_test/CMakeLists.txt +++ b/test/monster_test/CMakeLists.txt @@ -21,4 +21,4 @@ add_custom_command ( add_executable(monster_test monster_test.c "${GEN_DIR}/monster_test_builder.h" "${GEN_DIR}/monster_test_verifier.h") target_link_libraries(monster_test flatcc::runtime) -add_test(monster_test monster_test) +add_test(NAME monster_test COMMAND monster_test) diff --git a/test/monster_test_concat/CMakeLists.txt b/test/monster_test_concat/CMakeLists.txt index 482344fb8..5bb831e06 100644 --- a/test/monster_test_concat/CMakeLists.txt +++ b/test/monster_test_concat/CMakeLists.txt @@ -17,4 +17,4 @@ add_custom_command ( add_executable(monster_test_concat monster_test_concat.c "${GEN_DIR}/monster_test.h") target_link_libraries(monster_test_concat flatcc::runtime) -add_test(monster_test_concat monster_test_concat) +add_test(NAME monster_test_concat COMMAND monster_test_concat) diff --git a/test/monster_test_cpp/CMakeLists.txt b/test/monster_test_cpp/CMakeLists.txt index 346c72233..134f0f20c 100644 --- a/test/monster_test_cpp/CMakeLists.txt +++ b/test/monster_test_cpp/CMakeLists.txt @@ -19,5 +19,5 @@ add_custom_command( add_executable(monster_test_cpp monster_test.cpp "${GEN_DIR}/monster_builder.h") target_link_libraries(monster_test_cpp flatcc::runtime) -add_test(monster_test_cpp monster_test_cpp) +add_test(NAME monster_test_cpp COMMAND monster_test_cpp) diff --git a/test/monster_test_prefix/CMakeLists.txt b/test/monster_test_prefix/CMakeLists.txt index 2993b73ee..2f44a0baa 100644 --- a/test/monster_test_prefix/CMakeLists.txt +++ b/test/monster_test_prefix/CMakeLists.txt @@ -14,4 +14,4 @@ add_custom_command ( add_executable(monster_test_prefix monster_test_prefix.c "${GEN_DIR}/zzz_monster_test.h") target_link_libraries(monster_test_prefix flatcc::runtime) -add_test(monster_test_prefix monster_test_prefix) +add_test(NAME monster_test_prefix COMMAND monster_test_prefix) diff --git a/test/monster_test_solo/CMakeLists.txt b/test/monster_test_solo/CMakeLists.txt index ef4da0ea2..4b604cd5e 100644 --- a/test/monster_test_solo/CMakeLists.txt +++ b/test/monster_test_solo/CMakeLists.txt @@ -17,4 +17,4 @@ add_custom_command ( add_executable(monster_test_solo monster_test_solo.c "${GEN_DIR}/monster_test.h") target_link_libraries(monster_test_solo flatcc::runtime) -add_test(monster_test_solo monster_test_solo) +add_test(NAME monster_test_solo COMMAND monster_test_solo) diff --git a/test/optional_scalars_test/CMakeLists.txt b/test/optional_scalars_test/CMakeLists.txt index 216e9e54a..e472071cd 100644 --- a/test/optional_scalars_test/CMakeLists.txt +++ b/test/optional_scalars_test/CMakeLists.txt @@ -18,4 +18,4 @@ add_executable(optional_scalars_test optional_scalars_test.c "${GEN_DIR}/optiona "${GEN_DIR}/optional_scalars_test_json_printer.h" "${GEN_DIR}/optional_scalars_test_json_parser.h") target_link_libraries(optional_scalars_test flatcc::runtime) -add_test(optional_scalars_test optional_scalars_test) +add_test(NAME optional_scalars_test COMMAND optional_scalars_test) diff --git a/test/reflection_test/CMakeLists.txt b/test/reflection_test/CMakeLists.txt index db455b9e5..686d22934 100644 --- a/test/reflection_test/CMakeLists.txt +++ b/test/reflection_test/CMakeLists.txt @@ -15,4 +15,4 @@ add_custom_command ( add_executable(reflection_test reflection_test.c) target_link_libraries(reflection_test flatcc::runtime) -add_test(reflection_test reflection_test) +add_test(NAME reflection_test COMMAND reflection_test) From c7d53027f49ebaf32878142974eff9e27c0d75f7 Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Wed, 16 Dec 2020 22:48:31 +0100 Subject: [PATCH 06/21] cmake: add custom target for parallel MSVC builds --- test/json_test/CMakeLists.txt | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/test/json_test/CMakeLists.txt b/test/json_test/CMakeLists.txt index 023f66826..13ffa1ca5 100644 --- a/test/json_test/CMakeLists.txt +++ b/test/json_test/CMakeLists.txt @@ -8,6 +8,24 @@ set(DATA_SRC "${PROJECT_SOURCE_DIR}/test/flatc_compat") include_directories("${GEN_DIR}") +# FIXME: possible improvement if cmake_minimum_required >= 3.1: +# use add_library(... INTERFACE) + add_custom_command + target_sources(INTERFACE) + target_link_libraries() + +# Use add_custom_target + add_custom_command + add_dependencies because a parallel MSVC build will cause +# add_custom_command to be run multiple times in parallel which causes errors opening file for write. +# This problem does not pose when generated sources are used by only one target. +add_custom_target (gen_json_test + DEPENDS "${GEN_DIR}/attributes_builder.h" "${GEN_DIR}/attributes_json_parser.h" + "${GEN_DIR}/attributes_json_printer.h" "${GEN_DIR}/attributes_reader.h" "${GEN_DIR}/attributes_verifier.h" + "${GEN_DIR}/flatbuffers_common_builder.h" "${GEN_DIR}/flatbuffers_common_reader.h" + "${GEN_DIR}/include_test1_builder.h" "${GEN_DIR}/include_test1_json_parser.h" + "${GEN_DIR}/include_test1_json_printer.h" "${GEN_DIR}/include_test1_reader.h" + "${GEN_DIR}/include_test1_verifier.h" "${GEN_DIR}/include_test2_builder.h" + "${GEN_DIR}/include_test2_json_parser.h" "${GEN_DIR}/include_test2_json_printer.h" + "${GEN_DIR}/include_test2_reader.h" "${GEN_DIR}/include_test2_verifier.h" + "${GEN_DIR}/monster_test_builder.h" "${GEN_DIR}/monster_test_json_parser.h" + "${GEN_DIR}/monster_test_json_printer.h" "${GEN_DIR}/monster_test_reader.h" + "${GEN_DIR}/monster_test_verifier.h") add_custom_command ( OUTPUT "${GEN_DIR}/attributes_builder.h" "${GEN_DIR}/attributes_json_parser.h" "${GEN_DIR}/attributes_json_printer.h" "${GEN_DIR}/attributes_reader.h" "${GEN_DIR}/attributes_verifier.h" @@ -28,9 +46,14 @@ add_custom_command ( ) add_executable(test_basic_parse test_basic_parse.c) -add_executable(test_json_parser test_json_parser.c "${GEN_DIR}/monster_test_reader.h" "${GEN_DIR}/monster_test_json_parser.h") -add_executable(test_json_printer test_json_printer.c "${GEN_DIR}/monster_test_json_parser.h") -add_executable(test_json test_json.c "${GEN_DIR}/monster_test_json_parser.h" "${GEN_DIR}/monster_test_json_printer.h") +add_executable(test_json_parser test_json_parser.c) +add_executable(test_json_printer test_json_printer.c) +add_executable(test_json test_json.c) + +add_dependencies(test_basic_parse gen_json_test) +add_dependencies(test_json_parser gen_json_test) +add_dependencies(test_json_printer gen_json_test) +add_dependencies(test_json gen_json_test) target_link_libraries(test_basic_parse PRIVATE flatcc::runtime) target_link_libraries(test_json_parser PRIVATE flatcc::runtime) @@ -54,8 +77,8 @@ set(RTSRC ) macro(jstest trg flags) - add_executable(${trg} test_json.c ${RTSRC} "${GEN_DIR}/monster_test_json_parser.h" - "${GEN_DIR}/monster_test_json_printer.h" "${GEN_DIR}/monster_test_verifier.h") + add_executable(${trg} test_json.c ${RTSRC}) + add_dependencies(${trg} gen_json_test) target_include_directories(${trg} PRIVATE ${PROJECT_SOURCE_DIR}/include) add_test(NAME ${trg} COMMAND ${trg}) set_target_properties(${trg} PROPERTIES COMPILE_FLAGS ${flags}) From cb6563ec34644c290478301a766bc6507003dfcf Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Wed, 16 Dec 2020 22:49:26 +0100 Subject: [PATCH 07/21] Enable test_generated test --- test/cgen_test/CMakeLists.txt | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/test/cgen_test/CMakeLists.txt b/test/cgen_test/CMakeLists.txt index ffe2ffd1a..f014c6621 100644 --- a/test/cgen_test/CMakeLists.txt +++ b/test/cgen_test/CMakeLists.txt @@ -26,16 +26,7 @@ add_custom_target(test_generated -I${CMAKE_SOURCE_DIR}/include WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/test/cgen_test BYPRODUCTS test_generated${CMAKE_EXECUTABLE_SUFFIX}.c + DEPENDS cgen_test ) -add_dependencies(test_generated cgen_test) -# Might be related to: -# https://cmake.org/Bug/view.php?id=14963#c37230 -# https://github.com/ninja-build/ninja/issues/760 -if(${CMAKE_MAKE_PROGRAM} MATCHES make) -# this is now also broken for make - the system include path is not -# visible so build fails on not found in the custom build -# stage where CMAKE_C_COMPILER uses a compiler call that has this -# behavior -#add_test(NAME test_generated COMMAND ${CMAKE_MAKE_PROGRAM} test_generated) -endif(${CMAKE_MAKE_PROGRAM} MATCHES make) +add_test(NAME test_generated COMMAND ${CMAKE_COMMAND} --build "${PROJECT_BINARY_DIR}" --target test_generated --config $) From 6fdd281b11a00759f949cd87e47a2e917fdff83f Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Wed, 16 Dec 2020 23:57:25 +0100 Subject: [PATCH 08/21] Create cmake subfolder --- CMakeLists.txt | 2 +- flatcc-config.cmake.in => cmake/flatcc-config.cmake.in | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename flatcc-config.cmake.in => cmake/flatcc-config.cmake.in (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 644aa9301..9ad4fe4d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -349,7 +349,7 @@ if (FLATCC_INSTALL) install(EXPORT flatcc_exports NAMESPACE flatcc:: DESTINATION ${lib_dir}/cmake/flatcc FILE flatcc-targets.cmake) include(CMakePackageConfigHelpers) - configure_package_config_file(flatcc-config.cmake.in flatcc-config.cmake + configure_package_config_file(cmake/flatcc-config.cmake.in flatcc-config.cmake INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/flatcc") install(FILES "${CMAKE_CURRENT_BINARY_DIR}/flatcc-config.cmake" DESTINATION "lib/cmake/flatcc") endif() diff --git a/flatcc-config.cmake.in b/cmake/flatcc-config.cmake.in similarity index 100% rename from flatcc-config.cmake.in rename to cmake/flatcc-config.cmake.in From 519ed9aac192e214424a7a731c11005d216c5704 Mon Sep 17 00:00:00 2001 From: Winfried Dobbe Date: Wed, 16 Dec 2020 13:27:07 +0100 Subject: [PATCH 09/21] #168: add cmake helper function to generate flatbuffer code --- CMakeLists.txt | 6 ++- README.md | 37 +++++++++++++ cmake/FlatccGenerateSources.cmake | 86 +++++++++++++++++++++++++++++++ cmake/flatcc-config.cmake.in | 4 ++ 4 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 cmake/FlatccGenerateSources.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ad4fe4d5..d55094a0e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -351,5 +351,9 @@ if (FLATCC_INSTALL) include(CMakePackageConfigHelpers) configure_package_config_file(cmake/flatcc-config.cmake.in flatcc-config.cmake INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/flatcc") - install(FILES "${CMAKE_CURRENT_BINARY_DIR}/flatcc-config.cmake" DESTINATION "lib/cmake/flatcc") + install( + FILES + "${CMAKE_CURRENT_BINARY_DIR}/flatcc-config.cmake" + cmake/FlatccGenerateSources.cmake + DESTINATION "lib/cmake/flatcc") endif() diff --git a/README.md b/README.md index 4308350a4..81d89e1ed 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ executable also handle optional json parsing or printing in less than 2 us for a * [Online Forums](#online-forums) * [Introduction](#introduction) * [Project Details](#project-details) +* [Use in CMake build](#use-in-cmake-build) * [Poll on Meson Build](#poll-on-meson-build) * [Reporting Bugs](#reporting-bugs) * [Status](#status) @@ -186,6 +187,42 @@ their place. **NOTE: Big-endian platforms are only supported as of release 0.4.0.** +## Use in CMake build + +If your project uses the CMake build system, you can include cmake module +`cmake/FlatccGenerateSources.cmake` that provides the following cmake +function: + + flatcc_generate_sources(GENERATED_SOURCE_DIRECTORY + GENERATE_BUILDER + GENERATE_VERIFIER + EXPECTED_OUTPUT_FILES + DEFINITION_FILES +) + +`GENERATE_BUILDER` and `GENERATE_VERIFIER` are boolean options. When specified +they will instruct flatcc to generate builder / verifier source code. + +Optionally you can let cmake know the directory where the flatcc executable +is located in environment variable `FLATCC_BUILD_BIN_PATH`. This is especially +usefull when cross-compiling. In that case you should provide the directory +where the build arch flatcc compiler executable is located. + +The flatcc_generate_sources function will create a cmake custom command to +generate the output files during just before compilation (so in the build +step, not the configure step). + +Example: + + include(FlatccGenerateSources) + flatcc_generate_sources(GENERATED_SOURCE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/datadef + GENERATE_BUILDER + GENERATE_VERIFIER + EXPECTED_OUTPUT_FILES datadef/seclif_protocol_reader.h + datadef/seclif_protocol_builder.h + datadef/seclif_protocol_verifier.h + DEFINITION_FILES ${CMAKE_CURRENT_SOURCE_DIR}/datadef/seclif_protocol.fbs + ) ## Poll on Meson Build diff --git a/cmake/FlatccGenerateSources.cmake b/cmake/FlatccGenerateSources.cmake new file mode 100644 index 000000000..4141dacd4 --- /dev/null +++ b/cmake/FlatccGenerateSources.cmake @@ -0,0 +1,86 @@ + cmake_minimum_required(VERSION 3.5) + +# Use the following function to generate C source files from flatbuffer definition files: +# +# flatcc_generate_sources(GENERATED_SOURCE_DIRECTORY +# GENERATE_BUILDER +# GENERATE_VERIFIER +# EXPECTED_OUTPUT_FILES +# DEFINITION_FILES +# ) +# +# GENERATE_BUILDER and GENERATE_VERIFIER are boolean options. When specified they will instruct +# flatcc to generate builder / verifier source code. +# +# With cross-compiling you should provide the directory where the flatcc compiler executable is located +# in environment variable FLATCC_BUILD_BIN_PATH. If you use Conan and add flatcc as a build requirement +# this will be done automatically. + + +function(flatcc_generate_sources) + + # parse function arguments + set(OUTPREFIX "FLATCC") #variables created by 'cmake_parse_arguments' will be prefixed with this + set(NO_VAL_ARGS GENERATE_BUILDER GENERATE_VERIFIER) + set(SINGLE_VAL_ARGS GENERATED_SOURCE_DIRECTORY) + set(MULTI_VAL_ARGS DEFINITION_FILES EXPECTED_OUTPUT_FILES CC_OPTIONS) + + cmake_parse_arguments(${OUTPREFIX} + "${NO_VAL_ARGS}" + "${SINGLE_VAL_ARGS}" + "${MULTI_VAL_ARGS}" + ${ARGN} + ) + if (GENERATED_SOURCE_DIRECTORY IN_LIST FLATCC_KEYWORDS_MISSING_VALUES) + message(FATAL_ERROR "No directory provided after GENERATED_SOURCE_DIRECTORY keyword") + endif() + if (NOT FLATCC_GENERATED_SOURCE_DIRECTORY) + set(FLATCC_GENERATED_SOURCE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + endif() + message(STATUS "Flatcc sources will be generated in directory ${FLATCC_GENERATED_SOURCE_DIRECTORY}") + + if (FLATCC_GENERATE_BUILDER) + list(APPEND FLATCC_CC_OPTIONS --builder) + endif() + if (FLATCC_GENERATE_VERIFIER) + list(APPEND FLATCC_CC_OPTIONS --verifier) + endif() + + if (FLATCC_DEFINITION_FILES) + if (NOT EXISTS ${FLATCC_GENERATED_SOURCE_DIRECTORY}) + file(MAKE_DIRECTORY ${FLATCC_GENERATED_SOURCE_DIRECTORY}) + endif() + + message(VERBOSE "Executing command ${FLATCC_COMPILER} ${FLATCC_CC_OPTIONS} -o ${FLATCC_GENERATED_SOURCE_DIRECTORY} ${FLATCC_DEFINITION_FILES}") + add_custom_command(OUTPUT ${FLATCC_EXPECTED_OUTPUT_FILES} + COMMAND ${FLATCC_COMPILER} ${FLATCC_CC_OPTIONS} -o ${FLATCC_GENERATED_SOURCE_DIRECTORY} ${FLATCC_DEFINITION_FILES} + WORKING_DIRECTORY ${FLATCC_GENERATED_SOURCE_DIRECTORY}) + else() + message(WARNING "No flatbuffer definition files provided, no sources will be generated") + endif() + +endfunction() + + +#### Main #### + +#When cross-compiling user can provide location of the flatbuffers to C compiler in build arch via +#environment variable FLATCC_BUILD_BIN_PATH +set(FLATCC_BIN_PATH "$ENV{FLATCC_BUILD_BIN_PATH}") +if (FLATCC_BIN_PATH) + #user provided location where asn1c compiler executable is installed + find_program(FLATCC_COMPILER flatcc + PATHS ${FLATCC_BIN_PATH} + NO_DEFAULT_PATH + NO_SYSTEM_ENVIRONMENT_PATH + NO_CMAKE_SYSTEM_PATH +) +else() + #Find compiler exe + find_program(FLATCC_COMPILER flatcc) +endif() + + +if (NOT FLATCC_COMPILER) + message(FATAL_ERROR "Could not locate the flatcc compiler executable") +endif() diff --git a/cmake/flatcc-config.cmake.in b/cmake/flatcc-config.cmake.in index 6cec0493e..f31d30439 100644 --- a/cmake/flatcc-config.cmake.in +++ b/cmake/flatcc-config.cmake.in @@ -1,3 +1,7 @@ @PACKAGE_INIT@ +# Make available all imported targets include("${CMAKE_CURRENT_LIST_DIR}/flatcc-targets.cmake") + +# Make available the cmake scripts in the current folder +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}") From 5f03ecd2af40aedc39261912d19430ef3fbddba4 Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Thu, 17 Dec 2020 03:55:02 +0100 Subject: [PATCH 10/21] Use flatcc_generate_source to generate headers + track dependencies --- CMakeLists.txt | 4 + cmake/FlatccGenerateSources.cmake | 213 +++++++++++++++------- samples/monster/CMakeLists.txt | 11 +- samples/reflection/CMakeLists.txt | 14 +- src/cli/CMakeLists.txt | 2 +- test/cgen_test/CMakeLists.txt | 2 - test/emit_test/CMakeLists.txt | 11 +- test/flatc_compat/CMakeLists.txt | 22 +-- test/json_test/CMakeLists.txt | 38 +--- test/load_test/CMakeLists.txt | 16 +- test/monster_test/CMakeLists.txt | 19 +- test/monster_test_concat/CMakeLists.txt | 16 +- test/monster_test_cpp/CMakeLists.txt | 10 +- test/monster_test_prefix/CMakeLists.txt | 13 +- test/monster_test_solo/CMakeLists.txt | 2 - test/optional_scalars_test/CMakeLists.txt | 13 +- test/reflection_test/CMakeLists.txt | 14 +- 17 files changed, 204 insertions(+), 216 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d55094a0e..996fc7a78 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,10 @@ else() project (FlatCC C) endif() +# Make the cmake scripts inside the cmake subfolder available +list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake") +include(FlatccGenerateSources) + # # NOTE: when changing build options, clean the build using on of: # diff --git a/cmake/FlatccGenerateSources.cmake b/cmake/FlatccGenerateSources.cmake index 4141dacd4..c4dc26cec 100644 --- a/cmake/FlatccGenerateSources.cmake +++ b/cmake/FlatccGenerateSources.cmake @@ -1,86 +1,177 @@ - cmake_minimum_required(VERSION 3.5) - # Use the following function to generate C source files from flatbuffer definition files: # -# flatcc_generate_sources(GENERATED_SOURCE_DIRECTORY -# GENERATE_BUILDER -# GENERATE_VERIFIER -# EXPECTED_OUTPUT_FILES +# flatcc_generate_sources(OUTPUT_DIR +# BUILDER +# VERIFIER # DEFINITION_FILES # ) # -# GENERATE_BUILDER and GENERATE_VERIFIER are boolean options. When specified they will instruct +# BUILDER and VERIFIER are boolean options. When specified they will instruct # flatcc to generate builder / verifier source code. # # With cross-compiling you should provide the directory where the flatcc compiler executable is located # in environment variable FLATCC_BUILD_BIN_PATH. If you use Conan and add flatcc as a build requirement # this will be done automatically. +include(CMakeParseArguments) -function(flatcc_generate_sources) +function(flatcc_generate_sources) # parse function arguments - set(OUTPREFIX "FLATCC") #variables created by 'cmake_parse_arguments' will be prefixed with this - set(NO_VAL_ARGS GENERATE_BUILDER GENERATE_VERIFIER) - set(SINGLE_VAL_ARGS GENERATED_SOURCE_DIRECTORY) - set(MULTI_VAL_ARGS DEFINITION_FILES EXPECTED_OUTPUT_FILES CC_OPTIONS) - - cmake_parse_arguments(${OUTPREFIX} - "${NO_VAL_ARGS}" - "${SINGLE_VAL_ARGS}" - "${MULTI_VAL_ARGS}" - ${ARGN} - ) - if (GENERATED_SOURCE_DIRECTORY IN_LIST FLATCC_KEYWORDS_MISSING_VALUES) - message(FATAL_ERROR "No directory provided after GENERATED_SOURCE_DIRECTORY keyword") + set(output_options SCHEMA COMMON COMMON_READER COMMON_BUILDER BUILDER READER VERIFIER JSON_PARSER JSON_PRINTER JSON) + # TODO: missing RECURSIVE option + set(NO_VAL_ARGS ALL ${output_options}) + set(SINGLE_VAL_ARGS OUTPUT_DIR OUTFILE PREFIX TARGET) + set(MULTI_VAL_ARGS DEFINITION_FILES COMPILE_FLAGS PATHS) + + cmake_parse_arguments(FLATCC "${NO_VAL_ARGS}" "${SINGLE_VAL_ARGS}" "${MULTI_VAL_ARGS}" ${ARGN}) + + if (NOT FLATCC_DEFINITION_FILES) + message(FATAL_ERROR "No flatbuffer definition files provided") endif() - if (NOT FLATCC_GENERATED_SOURCE_DIRECTORY) - set(FLATCC_GENERATED_SOURCE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + + if (NOT FLATCC_OUTPUT_DIR) + set(FLATCC_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}") endif() - message(STATUS "Flatcc sources will be generated in directory ${FLATCC_GENERATED_SOURCE_DIRECTORY}") - if (FLATCC_GENERATE_BUILDER) - list(APPEND FLATCC_CC_OPTIONS --builder) + list(APPEND FLATCC_COMPILE_FLAGS -o "${FLATCC_OUTPUT_DIR}") + + foreach(path ${FLATCC_PATHS}) + list(APPEND FLATCC_COMPILE_FLAGS -I "${path}") + endforeach() + + # if no option is passed, the default READER is used + set(default_option_reader ON) + foreach(output_option ${output_options}) + if(output_option) + set(default_option_reader OFF) + endif() + endforeach() + if(default_option_reader) + set(FLATCC_READER ON) endif() - if (FLATCC_GENERATE_VERIFIER) - list(APPEND FLATCC_CC_OPTIONS --verifier) + + # Add flatcc options + + if(FLATCC_PREFIX) + list(APPEND FLATCC_COMPILE_FLAGS "--prefix=${FLATCC_PREFIX}") endif() - if (FLATCC_DEFINITION_FILES) - if (NOT EXISTS ${FLATCC_GENERATED_SOURCE_DIRECTORY}) - file(MAKE_DIRECTORY ${FLATCC_GENERATED_SOURCE_DIRECTORY}) - endif() + # Handle reader option first as other option encompass reader + if (FLATCC_READER) + list(APPEND FLATCC_COMPILE_FLAGS --reader) + endif() + if (FLATCC_COMMON_READER) + list(APPEND FLATCC_COMPILE_FLAGS --common) + endif() + if (FLATCC_COMMON_BUILDER) + list(APPEND FLATCC_COMPILE_FLAGS --common) + endif() + if (FLATCC_BUILDER) + list(APPEND FLATCC_COMPILE_FLAGS --builder) + # Builder also generates reader + set(FLATCC_READER ON) + endif() + if (FLATCC_VERIFIER) + list(APPEND FLATCC_COMPILE_FLAGS --verifier) + # verifier also generates reader + set(FLATCC_READER ON) + endif() + if (FLATCC_JSON_PARSER) + list(APPEND FLATCC_COMPILE_FLAGS --json-parser) + endif() + if (FLATCC_JSON_PRINTER) + list(APPEND FLATCC_COMPILE_FLAGS --json-printer) + endif() + if (FLATCC_SCHEMA) + list(APPEND FLATCC_COMPILE_FLAGS --schema) + endif() + # TODO: missing recursive option + # if (FLATCC_RECURSIVE) + # list(APPEND FLATCC_COMPILE_FLAGS --recursive) + # endif() - message(VERBOSE "Executing command ${FLATCC_COMPILER} ${FLATCC_CC_OPTIONS} -o ${FLATCC_GENERATED_SOURCE_DIRECTORY} ${FLATCC_DEFINITION_FILES}") - add_custom_command(OUTPUT ${FLATCC_EXPECTED_OUTPUT_FILES} - COMMAND ${FLATCC_COMPILER} ${FLATCC_CC_OPTIONS} -o ${FLATCC_GENERATED_SOURCE_DIRECTORY} ${FLATCC_DEFINITION_FILES} - WORKING_DIRECTORY ${FLATCC_GENERATED_SOURCE_DIRECTORY}) - else() - message(WARNING "No flatbuffer definition files provided, no sources will be generated") + # handle 'all', 'common', 'json' last as they encompass other options + if (FLATCC_COMMON) + list(APPEND FLATCC_COMPILE_FLAGS --common) + set(FLATCC_COMMON_READER ON) + set(FLATCC_COMMON_BUILDER ON) + endif() + if (FLATCC_JSON) + list(APPEND FLATCC_COMPILE_FLAGS --json) + set(FLATCC_JSON_PARSER ON) + set(FLATCC_JSON_PRINTER ON) + endif() + if (FLATCC_ALL) + list(APPEND FLATCC_COMPILE_FLAGS -a) + set(FLATCC_READER ON) + set(FLATCC_COMMON_BUILDER ON) + set(FLATCC_COMMON_READER ON) + set(FLATCC_BUILDER ON) + set(FLATCC_VERIFIER ON) + # TODO: missing recursive option + # set(FLATCC_RECURSIVE ON) endif() -endfunction() + # Calculate suffixes of output files. + + set(GENERATED_FILE_SUFFIXES) + if(FLATCC_READER) + list(APPEND GENERATED_FILE_SUFFIXES _reader.h) + endif() + if(FLATCC_BUILDER) + list(APPEND GENERATED_FILE_SUFFIXES _builder.h) + endif() + if(FLATCC_VERIFIER) + list(APPEND GENERATED_FILE_SUFFIXES _verifier.h) + endif() + if(FLATCC_JSON_PARSER) + list(APPEND GENERATED_FILE_SUFFIXES _json_parser.h) + endif() + if(FLATCC_JSON_PRINTER) + list(APPEND GENERATED_FILE_SUFFIXES _json_printer.h) + endif() + if(FLATCC_SCHEMA) + list(APPEND GENERATED_FILE_SUFFIXES .bfbs) + endif() + + set(ABSOLUTE_DEFINITION_FILES) + set(OUTPUT_FILES) + if(FLATCC_OUTFILE) + list(APPEND FLATCC_COMPILE_FLAGS "--outfile=${FLATCC_OUTPUT_DIR}/${FLATCC_OUTFILE}") + list(APPEND OUTPUT_FILES "${FLATCC_OUTPUT_DIR}/${FLATCC_OUTFILE}") + else() + foreach(definition_file ${FLATCC_DEFINITION_FILES}) + get_filename_component(def_name_we "${definition_file}" NAME_WLE) + foreach(suffix ${GENERATED_FILE_SUFFIXES}) + list(APPEND OUTPUT_FILES "${FLATCC_OUTPUT_DIR}/${def_name_we}${suffix}") + endforeach() + endforeach() + + if(FLATCC_COMMON_READER) + list(APPEND OUTPUT_FILES "${FLATCC_OUTPUT_DIR}/flatbuffers_common_reader.h") + endif() + if(FLATCC_COMMON_BUILDER) + list(APPEND OUTPUT_FILES "${FLATCC_OUTPUT_DIR}/flatbuffers_common_builder.h") + endif() + endif() + foreach(definition_file ${FLATCC_DEFINITION_FILES}) + file(REAL_PATH "${definition_file}" absolute_def_file) + list(APPEND ABSOLUTE_DEFINITION_FILES "${absolute_def_file}") + endforeach() + message(VERBOSE "Flatcc output directory: ${FLATCC_OUTPUT_DIR}") + message(VERBOSE "Flatcc output files: ${OUTPUT_FILES}") + message(VERBOSE "Flatcc execute: flatcc;${FLATCC_COMPILE_FLAGS};${ABSOLUTE_DEFINITION_FILES}") + add_custom_command(OUTPUT ${OUTPUT_FILES} + COMMAND "${CMAKE_COMMAND}" -E make_directory "${FLATCC_OUTPUT_DIR}" + COMMAND flatcc::cli ${FLATCC_COMPILE_FLAGS} ${ABSOLUTE_DEFINITION_FILES} + DEPENDS flatcc::cli ${FLATCC_DEFINITION_FILES} + ) + if(FLATCC_TARGET) + # FIXME: possible improvement if cmake_minimum_required >= 3.1: + # use add_library(... INTERFACE) + add_custom_command + target_sources(INTERFACE) + target_link_libraries() + add_custom_target("${FLATCC_TARGET}" DEPENDS ${OUTPUT_FILES}) + endif() -#### Main #### - -#When cross-compiling user can provide location of the flatbuffers to C compiler in build arch via -#environment variable FLATCC_BUILD_BIN_PATH -set(FLATCC_BIN_PATH "$ENV{FLATCC_BUILD_BIN_PATH}") -if (FLATCC_BIN_PATH) - #user provided location where asn1c compiler executable is installed - find_program(FLATCC_COMPILER flatcc - PATHS ${FLATCC_BIN_PATH} - NO_DEFAULT_PATH - NO_SYSTEM_ENVIRONMENT_PATH - NO_CMAKE_SYSTEM_PATH -) -else() - #Find compiler exe - find_program(FLATCC_COMPILER flatcc) -endif() - - -if (NOT FLATCC_COMPILER) - message(FATAL_ERROR "Could not locate the flatcc compiler executable") -endif() +endfunction() diff --git a/samples/monster/CMakeLists.txt b/samples/monster/CMakeLists.txt index 369461905..3e620207c 100644 --- a/samples/monster/CMakeLists.txt +++ b/samples/monster/CMakeLists.txt @@ -1,17 +1,10 @@ -include(CTest) - set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${CMAKE_CURRENT_SOURCE_DIR}") include_directories("${GEN_DIR}") -add_custom_command ( - OUTPUT "${GEN_DIR}/flatbuffers_common_builder.h" "${GEN_DIR}/flatbuffers_common_reader.h" - "${GEN_DIR}/monster_builder.h" "${GEN_DIR}/monster_reader.h" "${GEN_DIR}/monster_verifier.h" - COMMAND cmake -E make_directory "${GEN_DIR}" - COMMAND flatcc::cli -a -o "${GEN_DIR}" "${FBS_DIR}/monster.fbs" - DEPENDS flatcc::cli "${FBS_DIR}/monster.fbs" -) +flatcc_generate_sources(ALL OUTPUT_DIR "${GEN_DIR}" DEFINITION_FILES monster.fbs) + add_executable(monster monster.c "${GEN_DIR}/monster_builder.h") target_link_libraries(monster flatcc::runtime) diff --git a/samples/reflection/CMakeLists.txt b/samples/reflection/CMakeLists.txt index d0d7c9293..d988d026c 100644 --- a/samples/reflection/CMakeLists.txt +++ b/samples/reflection/CMakeLists.txt @@ -1,5 +1,3 @@ -include(CTest) - # # This projects depends headers generated from reflection.fbs but these # are pre-generated in `include/flatcc/reflection` so we don't need to @@ -14,17 +12,9 @@ set(FBS_DIR "${PROJECT_SOURCE_DIR}/samples/monster") include_directories("${GEN_DIR}") -add_custom_command ( - OUTPUT "${GEN_DIR}/monster.bfbs" - COMMAND cmake -E make_directory "${GEN_DIR}" - COMMAND flatcc::cli --schema -o "${GEN_DIR}" "${FBS_DIR}/monster.fbs" - DEPENDS flatcc::cli "${FBS_DIR}/monster.fbs" -) -add_custom_target(build_moster_bfbs ALL - DEPENDS "${GEN_DIR}/monster.bfbs" -) +flatcc_generate_sources(SCHEMA OUTPUT_DIR "${GEN_DIR}" DEFINITION_FILES "${FBS_DIR}/monster.fbs") -add_executable(bfbs2json bfbs2json.c) +add_executable(bfbs2json bfbs2json.c "${GEN_DIR}/monster.bfbs") target_link_libraries(bfbs2json flatcc::runtime) if (FLATCC_TEST) diff --git a/src/cli/CMakeLists.txt b/src/cli/CMakeLists.txt index de6e8e65d..fe9b06f7f 100644 --- a/src/cli/CMakeLists.txt +++ b/src/cli/CMakeLists.txt @@ -15,7 +15,7 @@ target_link_libraries(flatcc_cli # Rename because the libflatcc library and the flatcc executable would # conflict if they had the same target name `flatcc`. set_target_properties(flatcc_cli PROPERTIES - OUTPUT_NAME flatcc + OUTPUT_NAME flatcc$<$:_d> EXPORT_NAME cli) if (FLATCC_INSTALL) diff --git a/test/cgen_test/CMakeLists.txt b/test/cgen_test/CMakeLists.txt index f014c6621..74d08d5e6 100644 --- a/test/cgen_test/CMakeLists.txt +++ b/test/cgen_test/CMakeLists.txt @@ -1,5 +1,3 @@ -include(CTest) - add_executable(cgen_test cgen_test.c ) diff --git a/test/emit_test/CMakeLists.txt b/test/emit_test/CMakeLists.txt index efb3d5487..a4a4258f9 100644 --- a/test/emit_test/CMakeLists.txt +++ b/test/emit_test/CMakeLists.txt @@ -1,17 +1,10 @@ -include(CTest) - set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/emit_test") include_directories("${GEN_DIR}") -add_custom_command ( - OUTPUT "${GEN_DIR}/emit_test_builder.h" "${GEN_DIR}/emit_test_reader.h" "${GEN_DIR}/emit_test_verifier.h" - "${GEN_DIR}/flatbuffers_common_builder.h" "${GEN_DIR}/flatbuffers_common_reader.h" - COMMAND cmake -E make_directory "${GEN_DIR}" - COMMAND flatcc::cli -a -o "${GEN_DIR}" "${FBS_DIR}/emit_test.fbs" - DEPENDS flatcc::cli "${FBS}" -) +flatcc_generate_sources(ALL OUTPUT_DIR "${GEN_DIR}" DEFINITION_FILES "${FBS_DIR}/emit_test.fbs") + add_executable(emit_test emit_test.c "${GEN_DIR}/emit_test_builder.h") target_link_libraries(emit_test flatcc::runtime) diff --git a/test/flatc_compat/CMakeLists.txt b/test/flatc_compat/CMakeLists.txt index b1e5762ca..ec98cb488 100644 --- a/test/flatc_compat/CMakeLists.txt +++ b/test/flatc_compat/CMakeLists.txt @@ -1,22 +1,18 @@ -include(CTest) - set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") include_directories("${GEN_DIR}") -add_custom_command ( - OUTPUT "${GEN_DIR}/attributes_builder.h" "${GEN_DIR}/attributes_reader.h" "${GEN_DIR}/attributes_verifier.h" - "${GEN_DIR}/flatbuffers_common_builder.h" "${GEN_DIR}/flatbuffers_common_reader.h" - "${GEN_DIR}/include_test1_builder.h" "${GEN_DIR}/include_test1_reader.h" "${GEN_DIR}/include_test1_verifier.h" - "${GEN_DIR}/include_test2_builder.h" "${GEN_DIR}/include_test2_reader.h" "${GEN_DIR}/include_test2_verifier.h" - "${GEN_DIR}/monster_test_builder.h" "${GEN_DIR}/monster_test_reader.h" "${GEN_DIR}/monster_test_verifier.h" - COMMAND cmake -E make_directory "${GEN_DIR}" - COMMAND cmake -E copy "${CMAKE_CURRENT_SOURCE_DIR}/monsterdata_test.mon" ${CMAKE_CURRENT_BINARY_DIR} - COMMAND flatcc::cli -a -o "${GEN_DIR}" "${FBS_DIR}/monster_test.fbs" - DEPENDS flatcc::cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" -) +# TODO: use recursive +flatcc_generate_sources(ALL OUTPUT_DIR "${GEN_DIR}" + DEFINITION_FILES "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs") + add_executable(flatc_compat flatc_compat.c "${GEN_DIR}/monster_test_reader.h" "${GEN_DIR}/monster_test_verifier.h") target_link_libraries(flatc_compat flatcc::runtime) +add_custom_command ( + TARGET flatc_compat POST_BUILD + COMMAND "${CMAKE_COMMAND}" -E copy "${CMAKE_CURRENT_SOURCE_DIR}/monsterdata_test.mon" "${CMAKE_CURRENT_BINARY_DIR}" +) + add_test(NAME flatc_compat COMMAND flatc_compat) diff --git a/test/json_test/CMakeLists.txt b/test/json_test/CMakeLists.txt index 13ffa1ca5..5140c9e18 100644 --- a/test/json_test/CMakeLists.txt +++ b/test/json_test/CMakeLists.txt @@ -1,5 +1,3 @@ -include(CTest) - set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") @@ -11,38 +9,14 @@ include_directories("${GEN_DIR}") # FIXME: possible improvement if cmake_minimum_required >= 3.1: # use add_library(... INTERFACE) + add_custom_command + target_sources(INTERFACE) + target_link_libraries() -# Use add_custom_target + add_custom_command + add_dependencies because a parallel MSVC build will cause -# add_custom_command to be run multiple times in parallel which causes errors opening file for write. -# This problem does not pose when generated sources are used by only one target. -add_custom_target (gen_json_test - DEPENDS "${GEN_DIR}/attributes_builder.h" "${GEN_DIR}/attributes_json_parser.h" - "${GEN_DIR}/attributes_json_printer.h" "${GEN_DIR}/attributes_reader.h" "${GEN_DIR}/attributes_verifier.h" - "${GEN_DIR}/flatbuffers_common_builder.h" "${GEN_DIR}/flatbuffers_common_reader.h" - "${GEN_DIR}/include_test1_builder.h" "${GEN_DIR}/include_test1_json_parser.h" - "${GEN_DIR}/include_test1_json_printer.h" "${GEN_DIR}/include_test1_reader.h" - "${GEN_DIR}/include_test1_verifier.h" "${GEN_DIR}/include_test2_builder.h" - "${GEN_DIR}/include_test2_json_parser.h" "${GEN_DIR}/include_test2_json_printer.h" - "${GEN_DIR}/include_test2_reader.h" "${GEN_DIR}/include_test2_verifier.h" - "${GEN_DIR}/monster_test_builder.h" "${GEN_DIR}/monster_test_json_parser.h" - "${GEN_DIR}/monster_test_json_printer.h" "${GEN_DIR}/monster_test_reader.h" - "${GEN_DIR}/monster_test_verifier.h") -add_custom_command ( - OUTPUT "${GEN_DIR}/attributes_builder.h" "${GEN_DIR}/attributes_json_parser.h" - "${GEN_DIR}/attributes_json_printer.h" "${GEN_DIR}/attributes_reader.h" "${GEN_DIR}/attributes_verifier.h" - "${GEN_DIR}/flatbuffers_common_builder.h" "${GEN_DIR}/flatbuffers_common_reader.h" - "${GEN_DIR}/include_test1_builder.h" "${GEN_DIR}/include_test1_json_parser.h" - "${GEN_DIR}/include_test1_json_printer.h" "${GEN_DIR}/include_test1_reader.h" - "${GEN_DIR}/include_test1_verifier.h" "${GEN_DIR}/include_test2_builder.h" - "${GEN_DIR}/include_test2_json_parser.h" "${GEN_DIR}/include_test2_json_printer.h" - "${GEN_DIR}/include_test2_reader.h" "${GEN_DIR}/include_test2_verifier.h" - "${GEN_DIR}/monster_test_builder.h" "${GEN_DIR}/monster_test_json_parser.h" - "${GEN_DIR}/monster_test_json_printer.h" "${GEN_DIR}/monster_test_reader.h" - "${GEN_DIR}/monster_test_verifier.h" - COMMAND cmake -E make_directory "${GEN_DIR}" + +flatcc_generate_sources(ALL VERIFIER JSON OUTPUT_DIR "${GEN_DIR}" TARGET gen_json_test + DEFINITION_FILES "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs") + +add_custom_command (TARGET gen_json_test POST_BUILD COMMAND cmake -E copy "${DATA_SRC}/monsterdata_test.golden" "${DATA_DST}" COMMAND cmake -E copy "${DATA_SRC}/monsterdata_test.mon" "${DATA_DST}" - COMMAND flatcc::cli -av --json -o "${GEN_DIR}" "${FBS_DIR}/monster_test.fbs" - DEPENDS flatcc::cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" + DEPENDS "${DATA_SRC}/monsterdata_test.golden" "${DATA_SRC}/monsterdata_test.mon" ) add_executable(test_basic_parse test_basic_parse.c) diff --git a/test/load_test/CMakeLists.txt b/test/load_test/CMakeLists.txt index 3d66e8ca6..18c66f49c 100644 --- a/test/load_test/CMakeLists.txt +++ b/test/load_test/CMakeLists.txt @@ -1,20 +1,12 @@ -include(CTest) - set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") include_directories("${GEN_DIR}") -add_custom_command( - OUTPUT "${GEN_DIR}/attributes_builder.h" "${GEN_DIR}/attributes_reader.h" "${GEN_DIR}/attributes_verifier.h" - "${GEN_DIR}/flatbuffers_common_builder.h" "${GEN_DIR}/flatbuffers_common_reader.h" - "${GEN_DIR}/include_test1_builder.h" "${GEN_DIR}/include_test1_reader.h" "${GEN_DIR}/include_test1_verifier.h" - "${GEN_DIR}/include_test2_builder.h" "${GEN_DIR}/include_test2_reader.h" "${GEN_DIR}/include_test2_verifier.h" - "${GEN_DIR}/monster_test_builder.h" "${GEN_DIR}/monster_test_reader.h" "${GEN_DIR}/monster_test_verifier.h" - COMMAND cmake -E make_directory "${GEN_DIR}" - COMMAND flatcc::cli -a -o "${GEN_DIR}" "${FBS_DIR}/monster_test.fbs" - DEPENDS flatcc::cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" -) +# TODO: use recursive +flatcc_generate_sources(ALL OUTPUT_DIR "${GEN_DIR}" + DEFINITION_FILES "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs") + add_executable(load_test load_test.c "${GEN_DIR}/monster_test_builder.h") target_link_libraries(load_test flatcc::runtime) diff --git a/test/monster_test/CMakeLists.txt b/test/monster_test/CMakeLists.txt index 9b1b2605e..225b66b39 100644 --- a/test/monster_test/CMakeLists.txt +++ b/test/monster_test/CMakeLists.txt @@ -1,23 +1,12 @@ -include(CTest) - set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") include_directories("${GEN_DIR}") -add_custom_command ( - OUTPUT "${GEN_DIR}/attributes_builder.h" "${GEN_DIR}/attributes_reader.h" "${GEN_DIR}/attributes_verifier.h" - "${GEN_DIR}/flatbuffers_common_builder.h" "${GEN_DIR}/flatbuffers_common_reader.h" - "${GEN_DIR}/include_test1_builder.h" "${GEN_DIR}/include_test1_reader.h" - "${GEN_DIR}/include_test1_verifier.h" "${GEN_DIR}/include_test2_builder.h" - "${GEN_DIR}/include_test2_reader.h" "${GEN_DIR}/include_test2_verifier.h" - "${GEN_DIR}/monster_test_builder.h" "${GEN_DIR}/monster_test_reader.h" - "${GEN_DIR}/monster_test_verifier.h" - COMMAND cmake -E make_directory "${GEN_DIR}" - COMMAND flatcc::cli -a -o "${GEN_DIR}" "${FBS_DIR}/monster_test.fbs" - DEPENDS flatcc::cli "${FBS_DIR}/attributes.fbs" "${FBS_DIR}/monster_test.fbs" - "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" -) +# TODO: use recursive +flatcc_generate_sources(ALL OUTPUT_DIR "${GEN_DIR}" + DEFINITION_FILES "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs") + add_executable(monster_test monster_test.c "${GEN_DIR}/monster_test_builder.h" "${GEN_DIR}/monster_test_verifier.h") target_link_libraries(monster_test flatcc::runtime) diff --git a/test/monster_test_concat/CMakeLists.txt b/test/monster_test_concat/CMakeLists.txt index 5bb831e06..80024bb01 100644 --- a/test/monster_test_concat/CMakeLists.txt +++ b/test/monster_test_concat/CMakeLists.txt @@ -1,19 +1,13 @@ -include(CTest) - set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") include_directories("${GEN_DIR}") -add_custom_command ( - OUTPUT "${GEN_DIR}/monster_test.h" - COMMAND cmake -E make_directory "${GEN_DIR}" - # We could also use the recursive -r option, but this tests adding files manually to the output file. - COMMAND flatcc::cli -cwv --reader -o "${GEN_DIR}" "--outfile=monster_test.h" "${FBS_DIR}/attributes.fbs" - "${FBS_DIR}/include_test2.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/monster_test.fbs" - DEPENDS flatcc::cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" - "${FBS_DIR}/include_test2.fbs" "${FBS_DIR}/attributes.fbs" -) +flatcc_generate_sources(ALL + OUTPUT_DIR "${GEN_DIR}" + OUTFILE "monster_test.h" + DEFINITION_FILES "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs") + add_executable(monster_test_concat monster_test_concat.c "${GEN_DIR}/monster_test.h") target_link_libraries(monster_test_concat flatcc::runtime) diff --git a/test/monster_test_cpp/CMakeLists.txt b/test/monster_test_cpp/CMakeLists.txt index 134f0f20c..2dc017742 100644 --- a/test/monster_test_cpp/CMakeLists.txt +++ b/test/monster_test_cpp/CMakeLists.txt @@ -1,5 +1,3 @@ -include(CTest) - # Note: This re-uses the samples/monster fbs and .c file. # We use our own separate gen dir so we don't clash with the real monster sample. @@ -8,12 +6,8 @@ set(FBS_DIR "${PROJECT_SOURCE_DIR}/samples/monster") include_directories("${GEN_DIR}") -add_custom_command( - OUTPUT "${GEN_DIR}/flatbuffers_common_builder.h" "${GEN_DIR}/flatbuffers_common_reader.h" - "${GEN_DIR}/monster_builder.h" "${GEN_DIR}/monster_reader.h" "${GEN_DIR}/monster_verifier.h" - COMMAND cmake -E make_directory "${GEN_DIR}" - COMMAND flatcc::cli -a -o "${GEN_DIR}" "${FBS_DIR}/monster.fbs" - DEPENDS flatcc::cli "${FBS_DIR}/monster.fbs" +flatcc_generate_sources(ALL OUTPUT_DIR "${GEN_DIR}" + DEFINITION_FILES "${FBS_DIR}/monster.fbs" ) add_executable(monster_test_cpp monster_test.cpp "${GEN_DIR}/monster_builder.h") diff --git a/test/monster_test_prefix/CMakeLists.txt b/test/monster_test_prefix/CMakeLists.txt index 2f44a0baa..fae1bed5f 100644 --- a/test/monster_test_prefix/CMakeLists.txt +++ b/test/monster_test_prefix/CMakeLists.txt @@ -1,16 +1,13 @@ -include(CTest) - set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") include_directories("${GEN_DIR}") -add_custom_command ( - OUTPUT "${GEN_DIR}/zzz_monster_test.h" - COMMAND cmake -E make_directory "${GEN_DIR}" - COMMAND flatcc::cli -a --prefix=zzz_ --stdout "${FBS_DIR}/monster_test.fbs" > "${GEN_DIR}/zzz_monster_test.h" - DEPENDS flatcc::cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" -) +flatcc_generate_sources(ALL PREFIX zzz_ + OUTPUT_DIR "${GEN_DIR}" + OUTFILE "zzz_monster_test.h" + DEFINITION_FILES "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs") + add_executable(monster_test_prefix monster_test_prefix.c "${GEN_DIR}/zzz_monster_test.h") target_link_libraries(monster_test_prefix flatcc::runtime) diff --git a/test/monster_test_solo/CMakeLists.txt b/test/monster_test_solo/CMakeLists.txt index 4b604cd5e..7633637d4 100644 --- a/test/monster_test_solo/CMakeLists.txt +++ b/test/monster_test_solo/CMakeLists.txt @@ -1,5 +1,3 @@ -include(CTest) - set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") diff --git a/test/optional_scalars_test/CMakeLists.txt b/test/optional_scalars_test/CMakeLists.txt index e472071cd..7b95d78b9 100644 --- a/test/optional_scalars_test/CMakeLists.txt +++ b/test/optional_scalars_test/CMakeLists.txt @@ -1,19 +1,12 @@ -include(CTest) - set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/optional_scalars_test") include_directories("${GEN_DIR}") -add_custom_command ( - OUTPUT "${GEN_DIR}/flatbuffers_common_builder.h" "${GEN_DIR}/flatbuffers_common_reader.h" - "${GEN_DIR}/optional_scalars_test_builder.h" "${GEN_DIR}/optional_scalars_test_json_parser.h" - "${GEN_DIR}/optional_scalars_test_json_printer.h" "${GEN_DIR}/optional_scalars_test_reader.h" - "${GEN_DIR}/optional_scalars_test_verifier.h" - COMMAND cmake -E make_directory "${GEN_DIR}" - COMMAND flatcc::cli -a --json -o "${GEN_DIR}" "${FBS_DIR}/optional_scalars_test.fbs" - DEPENDS flatcc::cli "${FBS_DIR}/optional_scalars_test.fbs" +flatcc_generate_sources(ALL JSON OUTPUT_DIR "${GEN_DIR}" + DEFINITION_FILES "${FBS_DIR}/optional_scalars_test.fbs" ) + add_executable(optional_scalars_test optional_scalars_test.c "${GEN_DIR}/optional_scalars_test_builder.h" "${GEN_DIR}/optional_scalars_test_json_printer.h" "${GEN_DIR}/optional_scalars_test_json_parser.h") target_link_libraries(optional_scalars_test flatcc::runtime) diff --git a/test/reflection_test/CMakeLists.txt b/test/reflection_test/CMakeLists.txt index 686d22934..5a1869e26 100644 --- a/test/reflection_test/CMakeLists.txt +++ b/test/reflection_test/CMakeLists.txt @@ -1,18 +1,10 @@ -include(CTest) - set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") -include_directories("${GEN_DIR}") +flatcc_generate_sources(SCHEMA OUTPUT_DIR "${GEN_DIR}" + DEFINITION_FILES "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs") -add_custom_target(gen_reflection_test ALL) -add_custom_command ( - TARGET gen_reflection_test - COMMAND cmake -E make_directory "${GEN_DIR}" - COMMAND flatcc::cli --schema -o "${GEN_DIR}" "${FBS_DIR}/monster_test.fbs" - DEPENDS flatcc::cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" -) -add_executable(reflection_test reflection_test.c) +add_executable(reflection_test reflection_test.c "${GEN_DIR}/monster_test.bfbs") target_link_libraries(reflection_test flatcc::runtime) add_test(NAME reflection_test COMMAND reflection_test) From 90202ba003f7aa853ee7aab0e4e8068cc611f83e Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Thu, 17 Dec 2020 04:11:24 +0100 Subject: [PATCH 11/21] cmake: don't use file(REAL_PATH) --- cmake/FlatccGenerateSources.cmake | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/cmake/FlatccGenerateSources.cmake b/cmake/FlatccGenerateSources.cmake index c4dc26cec..b8d179ef3 100644 --- a/cmake/FlatccGenerateSources.cmake +++ b/cmake/FlatccGenerateSources.cmake @@ -136,6 +136,17 @@ function(flatcc_generate_sources) endif() set(ABSOLUTE_DEFINITION_FILES) + + foreach(definition_file ${FLATCC_DEFINITION_FILES}) + # TODO: file(REAL_PATH) if cmake_minimum_Version >= 3.19 + if(IS_ABSOLUTE "${definition_file}") + set(absolute_def_file "${definition_file}") + else() + set(absolute_def_file "${CMAKE_CURRENT_SOURCE_DIR}/${definition_file}") + endif() + list(APPEND ABSOLUTE_DEFINITION_FILES "${absolute_def_file}") + endforeach() + set(OUTPUT_FILES) if(FLATCC_OUTFILE) list(APPEND FLATCC_COMPILE_FLAGS "--outfile=${FLATCC_OUTPUT_DIR}/${FLATCC_OUTFILE}") @@ -155,10 +166,6 @@ function(flatcc_generate_sources) list(APPEND OUTPUT_FILES "${FLATCC_OUTPUT_DIR}/flatbuffers_common_builder.h") endif() endif() - foreach(definition_file ${FLATCC_DEFINITION_FILES}) - file(REAL_PATH "${definition_file}" absolute_def_file) - list(APPEND ABSOLUTE_DEFINITION_FILES "${absolute_def_file}") - endforeach() message(VERBOSE "Flatcc output directory: ${FLATCC_OUTPUT_DIR}") message(VERBOSE "Flatcc output files: ${OUTPUT_FILES}") From a40d6c3e237709ca26a9a637f8c0b620bdcf9c55 Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Thu, 17 Dec 2020 05:36:51 +0100 Subject: [PATCH 12/21] ci: output message when test fails --- scripts/test.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/test.sh b/scripts/test.sh index 0728e1660..5e46aa146 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -26,11 +26,11 @@ echo "building before tests ..." $ROOT/scripts/build.sh $DEBUG echo "running test in debug build ..." -cd $DBGDIR && ctest $ROOT +cd $DBGDIR && ctest $ROOT --output-on-failure if [ "$DEBUG" != "--debug" ]; then echo "running test in release build ..." -cd $RELDIR && ctest $ROOT +cd $RELDIR && ctest $ROOT --output-on-failure echo "TEST PASSED" else echo "DEBUG TEST PASSED" From 199bcac4fec3eb75f76e15374816be6b01447c40 Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Thu, 17 Dec 2020 05:50:05 +0100 Subject: [PATCH 13/21] cmake: apply regex on sources to track dependencies --- cmake/FlatccGenerateSources.cmake | 108 ++++++++++++++++++------ test/flatc_compat/CMakeLists.txt | 3 +- test/json_test/CMakeLists.txt | 6 +- test/load_test/CMakeLists.txt | 3 +- test/monster_test/CMakeLists.txt | 3 +- test/monster_test_concat/CMakeLists.txt | 2 +- test/monster_test_prefix/CMakeLists.txt | 2 +- test/reflection_test/CMakeLists.txt | 2 +- 8 files changed, 91 insertions(+), 38 deletions(-) diff --git a/cmake/FlatccGenerateSources.cmake b/cmake/FlatccGenerateSources.cmake index b8d179ef3..6bd2725a9 100644 --- a/cmake/FlatccGenerateSources.cmake +++ b/cmake/FlatccGenerateSources.cmake @@ -19,8 +19,7 @@ include(CMakeParseArguments) function(flatcc_generate_sources) # parse function arguments set(output_options SCHEMA COMMON COMMON_READER COMMON_BUILDER BUILDER READER VERIFIER JSON_PARSER JSON_PRINTER JSON) - # TODO: missing RECURSIVE option - set(NO_VAL_ARGS ALL ${output_options}) + set(NO_VAL_ARGS ALL RECURSIVE ${output_options}) set(SINGLE_VAL_ARGS OUTPUT_DIR OUTFILE PREFIX TARGET) set(MULTI_VAL_ARGS DEFINITION_FILES COMPILE_FLAGS PATHS) @@ -36,9 +35,35 @@ function(flatcc_generate_sources) list(APPEND FLATCC_COMPILE_FLAGS -o "${FLATCC_OUTPUT_DIR}") + # Add current source directory for finding dependencies + set(absolute_flatcc_paths "${CMAKE_CURRENT_SOURCE_DIR}") + # Also add directory of definition files + foreach(definition_file FLATCC_DEFINITION_FILES) + endforeach() + + set(ABSOLUTE_DEFINITION_FILES) + foreach(definition_file ${FLATCC_DEFINITION_FILES}) + # TODO: file(REAL_PATH) if cmake_minimum_required_version >= 3.19 + if(IS_ABSOLUTE "${definition_file}") + set(absolute_def_file "${definition_file}") + else() + set(absolute_def_file "${CMAKE_CURRENT_SOURCE_DIR}/${definition_file}") + endif() + list(APPEND ABSOLUTE_DEFINITION_FILES "${absolute_def_file}") + + get_filename_component(absolute_def_directory "${absolute_def_file}" DIRECTORY) + list(APPEND absolute_flatcc_paths "${absolute_def_directory}") + endforeach() + foreach(path ${FLATCC_PATHS}) + # TODO: file(REAL_PATH) if cmake_minimum_required_version >= 3.19 + if(NOT IS_ABSOLUTE "${path}") + set(path "${CMAKE_CURRENT_SOURCE_DIR}/${path}") + endif() + list(APPEND absolute_flatcc_paths "${path}") list(APPEND FLATCC_COMPILE_FLAGS -I "${path}") endforeach() + list(REMOVE_DUPLICATES absolute_flatcc_paths) # if no option is passed, the default READER is used set(default_option_reader ON) @@ -86,10 +111,9 @@ function(flatcc_generate_sources) if (FLATCC_SCHEMA) list(APPEND FLATCC_COMPILE_FLAGS --schema) endif() - # TODO: missing recursive option - # if (FLATCC_RECURSIVE) - # list(APPEND FLATCC_COMPILE_FLAGS --recursive) - # endif() + if (FLATCC_RECURSIVE) + list(APPEND FLATCC_COMPILE_FLAGS --recursive) + endif() # handle 'all', 'common', 'json' last as they encompass other options if (FLATCC_COMMON) @@ -109,8 +133,6 @@ function(flatcc_generate_sources) set(FLATCC_COMMON_READER ON) set(FLATCC_BUILDER ON) set(FLATCC_VERIFIER ON) - # TODO: missing recursive option - # set(FLATCC_RECURSIVE ON) endif() # Calculate suffixes of output files. @@ -135,24 +157,58 @@ function(flatcc_generate_sources) list(APPEND GENERATED_FILE_SUFFIXES .bfbs) endif() - set(ABSOLUTE_DEFINITION_FILES) - - foreach(definition_file ${FLATCC_DEFINITION_FILES}) - # TODO: file(REAL_PATH) if cmake_minimum_Version >= 3.19 - if(IS_ABSOLUTE "${definition_file}") - set(absolute_def_file "${definition_file}") - else() - set(absolute_def_file "${CMAKE_CURRENT_SOURCE_DIR}/${definition_file}") + # grep each definition file recursively for includes, convert them to absolute paths and add them to a list + set(ABSOLUTE_DEFINITIONS_DEPENDENCIES) + set(absolute_definition_files_todo ${ABSOLUTE_DEFINITION_FILES}) + while(absolute_definition_files_todo) + list(GET absolute_definition_files_todo 0 current_deffile) + # TODO: use if(absolute_definition_files_todo IN_LIST ABSOLUTE_DEFINITIONS_DEPENDENCIES) if cmake_minimum_required_version >= 3.3 + list(FIND ABSOLUTE_DEFINITIONS_DEPENDENCIES "${current_deffile}" todo_index) + if(todo_index LESS 0) + list(APPEND ABSOLUTE_DEFINITIONS_DEPENDENCIES "${current_deffile}") + file(READ "${current_deffile}" contents_deffile) + set(include_regex "(^|\n)include[ \t]+\"([^\"]+)\"") + string(REGEX MATCHALL "${include_regex}" includes_contents_match "${contents_deffile}") + foreach(include_match ${includes_contents_match}) + string(REGEX MATCH "${include_regex}" _m "${include_match}") + set(include_def_file "${CMAKE_MATCH_2}") + if(IS_ABSOLUTE "${include_def_file}") + set(abs_include_def_file "${include_def_file}") + else() + set(abs_include_def_file) + foreach(absolute_flatcc_path ${absolute_flatcc_paths}) + if(NOT abs_include_def_file) + if(EXISTS "${absolute_flatcc_path}/${include_def_file}") + set(abs_include_def_file "${absolute_flatcc_path}/${include_def_file}") + endif() + endif() + endforeach() + if(NOT abs_include_def_file) + message(WARNING "${current_deffile} includes ${include_def_file}, but cannot be found in search path") + endif() + endif() + if(abs_include_def_file) + list(APPEND absolute_definition_files_todo "${abs_include_def_file}") + endif() + endforeach() endif() - list(APPEND ABSOLUTE_DEFINITION_FILES "${absolute_def_file}") - endforeach() + list(REMOVE_AT absolute_definition_files_todo 0) + endwhile() + + list(REMOVE_DUPLICATES ABSOLUTE_DEFINITIONS_DEPENDENCIES) + list(REMOVE_DUPLICATES ABSOLUTE_DEFINITION_FILES) set(OUTPUT_FILES) if(FLATCC_OUTFILE) list(APPEND FLATCC_COMPILE_FLAGS "--outfile=${FLATCC_OUTPUT_DIR}/${FLATCC_OUTFILE}") list(APPEND OUTPUT_FILES "${FLATCC_OUTPUT_DIR}/${FLATCC_OUTFILE}") else() - foreach(definition_file ${FLATCC_DEFINITION_FILES}) + if(FLATCC_RECURSIVE) + set(definition_files ${ABSOLUTE_DEFINITIONS_DEPENDENCIES}) + else() + set(definition_files ${ABSOLUTE_DEFINITION_FILES}) + endif() + foreach(definition_file ${definition_files}) get_filename_component(def_name_we "${definition_file}" NAME_WLE) foreach(suffix ${GENERATED_FILE_SUFFIXES}) list(APPEND OUTPUT_FILES "${FLATCC_OUTPUT_DIR}/${def_name_we}${suffix}") @@ -167,16 +223,20 @@ function(flatcc_generate_sources) endif() endif() - message(VERBOSE "Flatcc output directory: ${FLATCC_OUTPUT_DIR}") - message(VERBOSE "Flatcc output files: ${OUTPUT_FILES}") - message(VERBOSE "Flatcc execute: flatcc;${FLATCC_COMPILE_FLAGS};${ABSOLUTE_DEFINITION_FILES}") + message(VERBOSE "---- flatcc start ----") + message(VERBOSE "output directory: ${FLATCC_OUTPUT_DIR}") + message(VERBOSE "output files: ${OUTPUT_FILES}") + message(VERBOSE "execute: flatcc;${FLATCC_COMPILE_FLAGS};${ABSOLUTE_DEFINITION_FILES}") + message(VERBOSE "dependencies: ${ABSOLUTE_DEFINITIONS_DEPENDENCIES}") + message(VERBOSE "----- flatcc end -----") + add_custom_command(OUTPUT ${OUTPUT_FILES} COMMAND "${CMAKE_COMMAND}" -E make_directory "${FLATCC_OUTPUT_DIR}" COMMAND flatcc::cli ${FLATCC_COMPILE_FLAGS} ${ABSOLUTE_DEFINITION_FILES} - DEPENDS flatcc::cli ${FLATCC_DEFINITION_FILES} + DEPENDS flatcc::cli ${ABSOLUTE_DEFINITIONS_DEPENDENCIES} ) if(FLATCC_TARGET) - # FIXME: possible improvement if cmake_minimum_required >= 3.1: + # TODO: possible improvement if cmake_minimum_required_version >= 3.1: # use add_library(... INTERFACE) + add_custom_command + target_sources(INTERFACE) + target_link_libraries() add_custom_target("${FLATCC_TARGET}" DEPENDS ${OUTPUT_FILES}) endif() diff --git a/test/flatc_compat/CMakeLists.txt b/test/flatc_compat/CMakeLists.txt index ec98cb488..c593d24fc 100644 --- a/test/flatc_compat/CMakeLists.txt +++ b/test/flatc_compat/CMakeLists.txt @@ -3,9 +3,8 @@ set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") include_directories("${GEN_DIR}") -# TODO: use recursive flatcc_generate_sources(ALL OUTPUT_DIR "${GEN_DIR}" - DEFINITION_FILES "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs") + DEFINITION_FILES "${FBS_DIR}/monster_test.fbs") add_executable(flatc_compat flatc_compat.c "${GEN_DIR}/monster_test_reader.h" "${GEN_DIR}/monster_test_verifier.h") target_link_libraries(flatc_compat flatcc::runtime) diff --git a/test/json_test/CMakeLists.txt b/test/json_test/CMakeLists.txt index 5140c9e18..e53e4a517 100644 --- a/test/json_test/CMakeLists.txt +++ b/test/json_test/CMakeLists.txt @@ -6,12 +6,8 @@ set(DATA_SRC "${PROJECT_SOURCE_DIR}/test/flatc_compat") include_directories("${GEN_DIR}") -# FIXME: possible improvement if cmake_minimum_required >= 3.1: -# use add_library(... INTERFACE) + add_custom_command + target_sources(INTERFACE) + target_link_libraries() - - flatcc_generate_sources(ALL VERIFIER JSON OUTPUT_DIR "${GEN_DIR}" TARGET gen_json_test - DEFINITION_FILES "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs") + DEFINITION_FILES "${FBS_DIR}/monster_test.fbs") add_custom_command (TARGET gen_json_test POST_BUILD COMMAND cmake -E copy "${DATA_SRC}/monsterdata_test.golden" "${DATA_DST}" diff --git a/test/load_test/CMakeLists.txt b/test/load_test/CMakeLists.txt index 18c66f49c..4174dbc71 100644 --- a/test/load_test/CMakeLists.txt +++ b/test/load_test/CMakeLists.txt @@ -3,9 +3,8 @@ set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") include_directories("${GEN_DIR}") -# TODO: use recursive flatcc_generate_sources(ALL OUTPUT_DIR "${GEN_DIR}" - DEFINITION_FILES "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs") + DEFINITION_FILES "${FBS_DIR}/monster_test.fbs") add_executable(load_test load_test.c "${GEN_DIR}/monster_test_builder.h") target_link_libraries(load_test flatcc::runtime) diff --git a/test/monster_test/CMakeLists.txt b/test/monster_test/CMakeLists.txt index 225b66b39..b9cf67f0a 100644 --- a/test/monster_test/CMakeLists.txt +++ b/test/monster_test/CMakeLists.txt @@ -3,9 +3,8 @@ set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") include_directories("${GEN_DIR}") -# TODO: use recursive flatcc_generate_sources(ALL OUTPUT_DIR "${GEN_DIR}" - DEFINITION_FILES "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs") + DEFINITION_FILES "${FBS_DIR}/monster_test.fbs") add_executable(monster_test monster_test.c "${GEN_DIR}/monster_test_builder.h" "${GEN_DIR}/monster_test_verifier.h") target_link_libraries(monster_test flatcc::runtime) diff --git a/test/monster_test_concat/CMakeLists.txt b/test/monster_test_concat/CMakeLists.txt index 80024bb01..637bcbe05 100644 --- a/test/monster_test_concat/CMakeLists.txt +++ b/test/monster_test_concat/CMakeLists.txt @@ -6,7 +6,7 @@ include_directories("${GEN_DIR}") flatcc_generate_sources(ALL OUTPUT_DIR "${GEN_DIR}" OUTFILE "monster_test.h" - DEFINITION_FILES "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs") + DEFINITION_FILES "${FBS_DIR}/monster_test.fbs") add_executable(monster_test_concat monster_test_concat.c "${GEN_DIR}/monster_test.h") target_link_libraries(monster_test_concat flatcc::runtime) diff --git a/test/monster_test_prefix/CMakeLists.txt b/test/monster_test_prefix/CMakeLists.txt index fae1bed5f..8a0bf1d6a 100644 --- a/test/monster_test_prefix/CMakeLists.txt +++ b/test/monster_test_prefix/CMakeLists.txt @@ -6,7 +6,7 @@ include_directories("${GEN_DIR}") flatcc_generate_sources(ALL PREFIX zzz_ OUTPUT_DIR "${GEN_DIR}" OUTFILE "zzz_monster_test.h" - DEFINITION_FILES "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs") + DEFINITION_FILES "${FBS_DIR}/monster_test.fbs") add_executable(monster_test_prefix monster_test_prefix.c "${GEN_DIR}/zzz_monster_test.h") target_link_libraries(monster_test_prefix flatcc::runtime) diff --git a/test/reflection_test/CMakeLists.txt b/test/reflection_test/CMakeLists.txt index 5a1869e26..d115dc82f 100644 --- a/test/reflection_test/CMakeLists.txt +++ b/test/reflection_test/CMakeLists.txt @@ -2,7 +2,7 @@ set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") flatcc_generate_sources(SCHEMA OUTPUT_DIR "${GEN_DIR}" - DEFINITION_FILES "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs") + DEFINITION_FILES "${FBS_DIR}/monster_test.fbs") add_executable(reflection_test reflection_test.c "${GEN_DIR}/monster_test.bfbs") target_link_libraries(reflection_test flatcc::runtime) From 40174a7ca4ce07b0baf150b7a848f2d8de6d2da9 Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Thu, 17 Dec 2020 06:04:53 +0100 Subject: [PATCH 14/21] cmake: install flatcc-config-version.cmake to allow version discrimination when using find_package --- CMakeLists.txt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 996fc7a78..6fdf5c1d2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -352,12 +352,24 @@ if (FLATCC_INSTALL) install(DIRECTORY include/flatcc DESTINATION include) install(EXPORT flatcc_exports NAMESPACE flatcc:: DESTINATION ${lib_dir}/cmake/flatcc FILE flatcc-targets.cmake) + file(READ include/flatcc/flatcc_version.h FLATCC_VERSION_H_CONTENT) + string(REGEX MATCH "#[ \t]*define[ \t]+FLATCC_VERSION_TEXT[ \t]+\"([^\"]+)\"" version_match "${FLATCC_VERSION_H_CONTENT}") + if(NOT version_match) + message(FATAL_ERROR "Could not parse the version from include/flatcc/flatcc_version.h") + endif() + set(FLATCC_VERSION "${CMAKE_MATCH_1}") + message(VERBOSE "Extracted version from include/flatcc/flatcc_version.h: ${FLATCC_VERSION}") + include(CMakePackageConfigHelpers) configure_package_config_file(cmake/flatcc-config.cmake.in flatcc-config.cmake INSTALL_DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/flatcc") + write_basic_package_version_file(flatcc-config-version.cmake + VERSION "${FLATCC_VERSION}" + COMPATIBILITY AnyNewerVersion) install( FILES "${CMAKE_CURRENT_BINARY_DIR}/flatcc-config.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/flatcc-config-version.cmake" cmake/FlatccGenerateSources.cmake DESTINATION "lib/cmake/flatcc") endif() From 1f0ab8f01ba816603c0f675db6f51cf7d02a7269 Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Thu, 17 Dec 2020 07:17:37 +0100 Subject: [PATCH 15/21] cgen_test: buill test code as library (to make use of cmake build system passing correct flags) --- test/cgen_test/CMakeLists.txt | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/test/cgen_test/CMakeLists.txt b/test/cgen_test/CMakeLists.txt index 74d08d5e6..e1e700337 100644 --- a/test/cgen_test/CMakeLists.txt +++ b/test/cgen_test/CMakeLists.txt @@ -16,15 +16,24 @@ add_test(NAME cgen_test COMMAND cgen_test) # expansion of flags results in quotes the compiler won't eat, # separating arguments should fix this, but not sure how portable it is. # see also http://stackoverflow.com/questions/9870162/avoid-quoting-in-cmake-add-custom-command -separate_arguments(CUSTOM_C_FLAGS UNIX_COMMAND "${CMAKE_C_FLAGS}") -add_custom_target(test_generated - COMMAND cgen_test > test_generated${CMAKE_EXECUTABLE_SUFFIX}.c - COMMAND ${CMAKE_C_COMPILER} ${CUSTOM_C_FLAGS} test_generated${CMAKE_EXECUTABLE_SUFFIX}.c -c - -I${CMAKE_SOURCE_DIR}/include +add_custom_COMMAND(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/test_generated${CMAKE_EXECUTABLE_SUFFIX}.c" + COMMAND cgen_test > "${CMAKE_CURRENT_BINARY_DIR}/test_generated${CMAKE_EXECUTABLE_SUFFIX}.c" WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/test/cgen_test - BYPRODUCTS test_generated${CMAKE_EXECUTABLE_SUFFIX}.c DEPENDS cgen_test - ) +) + +if(NOT MSVC) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=unused-const-variable") +endif() +add_library(test_generated_objects STATIC EXCLUDE_FROM_ALL + "${CMAKE_CURRENT_BINARY_DIR}/test_generated${CMAKE_EXECUTABLE_SUFFIX}.c" +) +target_include_directories(test_generated_objects PRIVATE "${PROJECT_SOURCE_DIR}/include") + +add_custom_target(test_generated + COMMAND "${CMAKE_COMMAND}" -E rm -f $ + COMMAND "${CMAKE_COMMAND}" --build "${PROJECT_BINARY_DIR}" --target test_generated_objects --config $ --verbose +) add_test(NAME test_generated COMMAND ${CMAKE_COMMAND} --build "${PROJECT_BINARY_DIR}" --target test_generated --config $) From d2322c822ef44a2d937f736fcbcfaf9bc9517989 Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Thu, 17 Dec 2020 06:29:30 +0100 Subject: [PATCH 16/21] ci: add github actions script --- .github/workflows/ci.yml | 63 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 000000000..d073284f3 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,63 @@ +on: + - push + - pull_request +jobs: + test_unix: + name: 'Test Unix with CMake' + strategy: + matrix: + os: ['macos-latest', 'ubuntu-latest'] + config: ['Release', 'Debug'] + cmake_generator: ['Unix Makefiles', 'Ninja'] + runs-on: ${{ matrix.os }} + steps: + - name: 'Checkout repo' + uses: actions/checkout@v2 + - name: 'Install meson (Ubuntu)' + if: ${{ contains(matrix.os, 'ubuntu') && (matrix.cmake_generator == 'Ninja') }} + run: | + sudo apt-get install ninja-build + - name: 'Install meson (Macos)' + if: ${{ contains(matrix.os, 'macos') && (matrix.cmake_generator == 'Ninja') }} + run: | + brew install meson + - name: 'CMake configure' + run: | + mkdir build + cd build + cmake .. -G "${{ matrix.cmake_generator }}" -DCMAKE_BUILD_TYPE=${{ matrix.config }} -DFLATCC_INSTALL=ON -DCMAKE_INSTALL_PREFIX=prefix + - name: 'CMake build' + run: | + cmake --build build --parallel + - name: 'CTest' + run: | + cd build + ctest -V -C ${{ matrix.config }} + - name: 'Install' + run: | + cmake --build build --target install + test_windows: + name: 'Test Windows with CMake' + strategy: + matrix: + config: ['Release', 'Debug'] + arch: ['Win32', 'x64'] + runs-on: 'windows-latest' + steps: + - name: 'Checkout repo' + uses: actions/checkout@v2 + - name: 'CMake configure' + run: | + mkdir build + cd build + cmake .. -A ${{ matrix.arch }} -DFLATCC_INSTALL=ON -DCMAKE_INSTALL_PREFIX=prefix + - name: 'CMake build' + run: | + cmake --build build --parallel --config ${{ matrix.config }} --verbose + - name: 'CTest' + run: | + cd build + ctest -V -C ${{ matrix.config }} + - name: 'Install' + run: | + cmake --build build --target install --config ${{ matrix.config }} From 41b1a5f02a97df1bfb70a8112e389bec351e145d Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Thu, 17 Dec 2020 07:42:34 +0100 Subject: [PATCH 17/21] fix test_generated --- test/cgen_test/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cgen_test/CMakeLists.txt b/test/cgen_test/CMakeLists.txt index e1e700337..1d85117a5 100644 --- a/test/cgen_test/CMakeLists.txt +++ b/test/cgen_test/CMakeLists.txt @@ -32,7 +32,7 @@ add_library(test_generated_objects STATIC EXCLUDE_FROM_ALL target_include_directories(test_generated_objects PRIVATE "${PROJECT_SOURCE_DIR}/include") add_custom_target(test_generated - COMMAND "${CMAKE_COMMAND}" -E rm -f $ + COMMAND "${CMAKE_COMMAND}" -E remove -f "${CMAKE_CURRENT_BINARY_DIR}/test_generated${CMAKE_EXECUTABLE_SUFFIX}.c" $ COMMAND "${CMAKE_COMMAND}" --build "${PROJECT_BINARY_DIR}" --target test_generated_objects --config $ --verbose ) From 38acb79853f1e7118847008ab62261025fa6616b Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Sat, 27 Mar 2021 15:41:58 +0100 Subject: [PATCH 18/21] remove lib/libtest* libraries in scripts/cleanall.sh --- scripts/cleanall.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/cleanall.sh b/scripts/cleanall.sh index a91d58cfa..d8027a175 100755 --- a/scripts/cleanall.sh +++ b/scripts/cleanall.sh @@ -11,6 +11,7 @@ rm -rf release rm -f bin/flatcc* rm -f bin/bfbs2json* rm -f lib/libflatcc* +rm -f lib/libtest* if [ -d bin ]; then rmdir bin fi From 10f6a9ebae4f6b73c5488aeda0f8815a3223b31c Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Sat, 27 Mar 2021 17:01:41 +0100 Subject: [PATCH 19/21] ci: add cross building github action --- .github/workflows/ci.yml | 134 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 127 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d073284f3..b3e7cd03d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,14 +1,38 @@ on: - - push - - pull_request + push: + branches-ignore: + - 'ci-more' + pull_request: jobs: test_unix: name: 'Test Unix with CMake' strategy: matrix: - os: ['macos-latest', 'ubuntu-latest'] - config: ['Release', 'Debug'] - cmake_generator: ['Unix Makefiles', 'Ninja'] + include: + - os: 'ubuntu-latest' + config: 'Release' + cmake_generator: 'Unix Makefiles' + shared: 'OFF' + - os: 'ubuntu-latest' + config: 'Debug' + cmake_generator: 'Ninja' + shared: 'OFF' + - os: 'ubuntu-latest' + config: 'Release' + cmake_generator: 'Unix Makefiles' + shared: 'ON' + - os: 'macos-latest' + config: 'Release' + cmake_generator: 'Unix Makefiles' + shared: 'OFF' + - os: 'macos-latest' + config: 'Debug' + cmake_generator: 'Ninja' + shared: 'OFF' + - os: 'macos-latest' + config: 'Release' + cmake_generator: 'Ninja' + shared: 'ON' runs-on: ${{ matrix.os }} steps: - name: 'Checkout repo' @@ -25,7 +49,9 @@ jobs: run: | mkdir build cd build - cmake .. -G "${{ matrix.cmake_generator }}" -DCMAKE_BUILD_TYPE=${{ matrix.config }} -DFLATCC_INSTALL=ON -DCMAKE_INSTALL_PREFIX=prefix + cmake .. -DBUILD_SHARED_LIBS=${{ matrix.shared }} -G "${{ matrix.cmake_generator }}" \ + -DCMAKE_BUILD_TYPE=${{ matrix.config }} -DFLATCC_INSTALL=ON \ + -DCMAKE_INSTALL_PREFIX=${{ github.workspace }}/prefix - name: 'CMake build' run: | cmake --build build --parallel @@ -36,6 +62,12 @@ jobs: - name: 'Install' run: | cmake --build build --target install + - name: 'Test installed flatcc prefix' + run: | + mkdir user && cd user + cmake "${{ github.workspace }}/test/install_test" -DCMAKE_PREFIX_PATH="${{ github.workspace }}/prefix" + cmake --build . + ctest . test_windows: name: 'Test Windows with CMake' strategy: @@ -50,7 +82,7 @@ jobs: run: | mkdir build cd build - cmake .. -A ${{ matrix.arch }} -DFLATCC_INSTALL=ON -DCMAKE_INSTALL_PREFIX=prefix + cmake .. -A ${{ matrix.arch }} -DFLATCC_INSTALL=ON -DCMAKE_INSTALL_PREFIX=${{ github.workspace }}/prefix - name: 'CMake build' run: | cmake --build build --parallel --config ${{ matrix.config }} --verbose @@ -61,3 +93,91 @@ jobs: - name: 'Install' run: | cmake --build build --target install --config ${{ matrix.config }} + - name: 'Test installed flatcc prefix' + run: | + mkdir user && cd user + cmake "${{ github.workspace }}/test/install_test" -A ${{ matrix.arch }} ` + -DCMAKE_PREFIX_PATH="${{ github.workspace }}/prefix" + cmake --build . --config ${{ matrix.config }} + ctest . -C ${{ matrix.config }} + test_mingw_on_linux: + name: 'Test cross building to Windows with CMake' + strategy: + matrix: + config: ['Release'] + arch: ['x64'] + shared_build: ['ON'] + config_build: ['Release'] + shared_host: ['ON'] + config_host: ['Release'] + runs-on: 'ubuntu-latest' + steps: + - name: 'Install mingw' + run: | + sudo apt-get install mingw-w64 + - name: 'Checkout repo' + uses: actions/checkout@v2 + - name: 'CMake configure/build/install build flatcc (native)' + run: | + mkdir build_native + cd build_native + cmake .. -DBUILD_SHARED_LIBS=${{ matrix.shared_build }} -DCMAKE_BUILD_TYPE=${{ matrix.config_build }} \ + -DFLATCC_INSTALL=ON -DCMAKE_INSTALL_PREFIX=${{ github.workspace }}/prefix_build + cmake --build . --parallel + cmake --build . --target install + - name: 'CMake configure/build/install host flatcc (cross)' + run: | + mkdir build_mingw + cd build_mingw + cmake .. -DCMAKE_TOOLCHAIN_FILE=../test/cmake/x86_64-w64-mingw32-toolchain.cmake \ + -DBUILD_SHARED_LIBS=${{ matrix.shared_host }} -DFLATCC_INSTALL=ON \ + -DCMAKE_BUILD_TYPE=${{ matrix.config_host }} \ + -DCMAKE_INSTALL_PREFIX=${{ github.workspace }}/prefix_host \ + -DFLATCC_ROOT_FOR_BUILD=${{ github.workspace }}/prefix_build + cmake --build . --parallel + cmake --build . --target install + - name: 'Build a flatbuffers project with both native and host flatcc' + run: | + mkdir project + cd project + cmake ../test/install_test -DCMAKE_TOOLCHAIN_FILE=../test/cmake/x86_64-w64-mingw32-toolchain.cmake \ + -DCMAKE_PREFIX_PATH=${{ github.workspace }}/prefix_host \ + -DFLATCC_ROOT_FOR_BUILD=${{ github.workspace }}/prefix_build + cmake --build . + test_mingw_on_windows: + name: 'Test mingw on Windows' + strategy: + matrix: + shared: ['ON'] + config: ['Release'] + runs-on: 'windows-latest' + steps: + - name: 'Checkout repo' + uses: actions/checkout@v2 + - name: 'CMake configure' + run: | + mkdir build + cd build + cmake .. -DBUILD_SHARED_LIBS=${{ matrix.shared }} -DCMAKE_BUILD_TYPE=${{ matrix.config }} ` + -DFLATCC_INSTALL=ON -DCMAKE_INSTALL_PREFIX="${{ github.workspace }}/prefix" ` + -G "MinGW Makefiles" + - name: 'CMake build' + run: | + cmake --build build --parallel + - name: 'CTest' + run: | + # FIXME: try using https://cmake.org/cmake/help/latest/prop_test/ENVIRONMENT.html on each test + $env:Path += ";${{ github.workspace }}/bin" + cd build + ctest -V -C ${{ matrix.config }} + - name: 'Install' + run: | + cmake --build build --target install + - name: 'Test installed flatcc prefix' + run: | + $env:Path += ";${{ github.workspace }}/prefix/bin" + mkdir user && cd user + cmake "${{ github.workspace }}/test/install_test" ` + -DCMAKE_PREFIX_PATH="${{ github.workspace }}/prefix" -G "MinGW Makefiles" + cmake --build . + ctest . From c1f1fd917439a7fd2928ad0dada5f0595392cbe4 Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Sat, 27 Mar 2021 17:05:39 +0100 Subject: [PATCH 20/21] Add .editorconfig --- .editorconfig | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 000000000..652d34735 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +root = true + +[{*.c, *.h, *.cpp, *.fbs, *.sh, *.cmake, *.cmake.in, *.txt}] +indent_style = space +indent_size = 4 +insert_final_newline = true +trim_trailing_whitespace = true + +[{*.yml, *.json}] +indent_style = space +indent_size = 2 +insert_final_newline = true +trim_trailing_whitespace = true From 9808df588bf3b651a6b3a67c635f7e5b1a395f21 Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Sat, 27 Mar 2021 17:19:25 +0100 Subject: [PATCH 21/21] Restored changed after botched force push --- .github/workflows/ci-more.yml | 52 ++++ .gitignore | 1 + CMakeLists.txt | 64 +++-- README.md | 181 ++++++++----- appveyor.yml | 3 +- cmake/FindFlatCCNative.cmake | 39 +++ cmake/FlatccGenerateSources.cmake | 238 ++++++++++++------ cmake/flatcc-config.cmake.in | 11 + samples/CMakeLists.txt | 2 + samples/monster/CMakeLists.txt | 18 +- samples/reflection/CMakeLists.txt | 17 +- src/cli/CMakeLists.txt | 23 +- src/compiler/CMakeLists.txt | 26 +- src/runtime/CMakeLists.txt | 8 +- test/CMakeLists.txt | 4 +- test/cgen_test/CMakeLists.txt | 64 ++--- test/cmake/x86_64-w64-mingw32-toolchain.cmake | 12 + test/emit_test/CMakeLists.txt | 15 +- test/flatc_compat/CMakeLists.txt | 14 +- test/install_test/CMakeLists.txt | 41 +++ test/json_test/CMakeLists.txt | 33 ++- test/load_test/CMakeLists.txt | 16 +- test/monster_test/CMakeLists.txt | 16 +- test/monster_test_concat/CMakeLists.txt | 13 +- test/monster_test_cpp/CMakeLists.txt | 13 +- test/monster_test_prefix/CMakeLists.txt | 14 +- test/monster_test_solo/CMakeLists.txt | 7 +- test/optional_scalars_test/CMakeLists.txt | 15 +- test/reflection_test/CMakeLists.txt | 12 +- 29 files changed, 664 insertions(+), 308 deletions(-) create mode 100644 .github/workflows/ci-more.yml create mode 100644 cmake/FindFlatCCNative.cmake create mode 100644 test/cmake/x86_64-w64-mingw32-toolchain.cmake create mode 100644 test/install_test/CMakeLists.txt diff --git a/.github/workflows/ci-more.yml b/.github/workflows/ci-more.yml new file mode 100644 index 000000000..90a4d3036 --- /dev/null +++ b/.github/workflows/ci-more.yml @@ -0,0 +1,52 @@ +on: + schedule: + # Run this script every saturday, so you have the whole weekend to fix problems :evil-grin: + - cron: '0 0 * * 6' +jobs: + merge_into_ci_more: + # Only enable this job on dividelabs' repo to avoid running this job on forks + if: ${{ github.repository_owner == 'dividelabs' }} + runs-on: 'ubuntu-latest' + steps: + - name: 'Checkout repo' + uses: actions/checkout@v2 + with: + fetch-depth: 0 + - name: 'Configure git' + run: | + git config --global user.name "ci-more cron job" + git config --global user.email "ci-more-cron-job@example.com" + - name: 'Checkout ci-more' + run: | + git checkout ci-more + - name: 'Backup .travis.yml and appveyor.yml' + run: | + cp .travis.yml .travis.yml.backup + cp appveyor.yml appveyor.yml.backup + - name: 'Checkout .travis.yml and appveyor.yml from master' + run: | + git checkout origin/master -- .travis.yml appveyor.yml + git add .travis.yml appveyor.yml + git commit -m "Temporarily add .travis.yml and appveyor.yml from master" + - name: 'Merge master into ci-more' + run: | + git merge origin/master -m "Merge master into ci-more" + - name: 'Restore .travis.yml and appveyor.yml' + run: | + mv .travis.yml.backup .travis.yml + mv appveyor.yml.backup appveyor.yml + git add -- .travis.yml appveyor.yml + git commit -m "Restore .travis.yml and appveyor.yml" + - name: 'Check whether the working directory is clean' + run: | + git diff --exit-code + - name: 'Check whether only .travis.yml and appveyor.yml differ with master' + run: | + if test $(git diff origin/master --name-only | wc -l) != 2; then + echo "ci-more should diverge from origin/master with exactly 2 files (.travis.yml and appveyor.yml)"; + git diff origin/master --exit-code; # origin/master + exit 1; + fi + - name: 'Push ci-more to server' + run: | + git push origin ci-more diff --git a/.gitignore b/.gitignore index eba83e37c..e698fe9bd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ build/* bin/* lib/* +lib64/* release/* scripts/build.cfg diff --git a/CMakeLists.txt b/CMakeLists.txt index 6fdf5c1d2..07922146f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ #cmake_minimum_required (VERSION 2.8.12.2) # Centos 7 #cmake_minimum_required (VERSION 2.8.11) -cmake_minimum_required (VERSION 2.8) +cmake_minimum_required (VERSION 2.8.12) # Disable build of tests and samples. Due to custom build step # dependency on flatcc tool, some custom build configurations may @@ -12,11 +12,12 @@ option(FLATCC_TEST "enable tests" ON) # Only active if FLATCC_TEST is active. Used to ensure that C++ users # can include generatd C source. Old GCC pre 4.7 won't compile C++ test # project. -option(FLATCC_CXX_TEST "enable C++ tests" ON) +include(CMakeDependentOption) +cmake_dependent_option(FLATCC_CXX_TEST "enable C++ tests" ON FLATCC_TEST OFF) # Conditionally set project languages based on FLATCC_TEST, as C++ is # only necessary if building the tests. -if (FLATCC_TEST AND FLATCC_CXX_TEST) +if (FLATCC_CXX_TEST) project (FlatCC C CXX) else() project (FlatCC C) @@ -24,7 +25,6 @@ endif() # Make the cmake scripts inside the cmake subfolder available list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake") -include(FlatccGenerateSources) # # NOTE: when changing build options, clean the build using on of: @@ -111,10 +111,6 @@ if (FLATCC_RTONLY) set(FLATCC_TEST off) endif() -if (FLATCC_TEST) - enable_testing() -endif() - if (NOT FLATCC_TEST) set(FLATCC_COVERAGE off) endif() @@ -155,10 +151,9 @@ if (FLATCC_FAST_DOUBLE) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DGRISU3_PARSE_ALLOW_ERROR -DFLATCC_USE_GRISU3=1") endif() -if (NOT DEFINED FLATCC_INSTALL_LIB) - set(lib_dir lib) -else() - set(lib_dir ${FLATCC_INSTALL_LIB}) +include(GNUInstallDirs) +if (FLATCC_INSTALL_LIB) + set(CMAKE_INSTALL_LIBDIR ${FLATCC_INSTALL_LIB}) endif() # The folder of this directory, as apposed to CMAKE_BINARY_DIR @@ -167,13 +162,14 @@ set (dist_dir "${PROJECT_SOURCE_DIR}") # set (dist_dir "${CMAKE_BINARY_DIR}") message(STATUS "dist install dir ${dist_dir}") -message(STATUS "lib install dir ${dist_dir}/${lib_dir}") +message(STATUS "lib install dir ${dist_dir}/${CMAKE_INSTALL_LIBDIR}") # Note: for compiling generated C code, warnings of unused functions # and constants should be turned off - those are plentiful. They are # silenced for Clang, GCC and MSVC in generated headers.headers. if (CMAKE_C_COMPILER_ID MATCHES "Clang") + set(CLANG_VERSION "${CMAKE_C_COMPILER_VERSION}") # Clang or AppleClang message(STATUS "Setting Clang compiler options") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wstrict-prototypes") @@ -195,8 +191,7 @@ if (CMAKE_C_COMPILER_ID MATCHES "Clang") # set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -save-temps") elseif (CMAKE_C_COMPILER_ID STREQUAL "GNU") - execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion - OUTPUT_VARIABLE GCC_VERSION) + set(GCC_VERSION "${CMAKE_C_COMPILER_VERSION}") if (GCC_VERSION VERSION_LESS 4.7) message(STATUS "Setting older GNU C compiler options with FLATCC_PORTABLE") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra") @@ -278,7 +273,7 @@ elseif (MSVC) # using STREQUAL here conflicts with string interpretation changes set (FLATCC_NEED_C89_VAR_DECLS true) endif() set(FLATCC_PORTABLE true) - elseif (CMAKE_C_COMPILER_ID STREQUAL "XL") +elseif (CMAKE_C_COMPILER_ID STREQUAL "XL") # IBM's native XLC C compiler in extended C99 mode message(STATUS "Setting IBM XL C compiler options") @@ -294,6 +289,11 @@ if (FLATCC_PORTABLE) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DFLATCC_PORTABLE") endif() +if(MINGW) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-pedantic-ms-format") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-pedantic-ms-format") +endif() + if (CLANG_VERSION) message(STATUS "CLANG_VERSION: ${CLANG_VERSION}") endif() @@ -302,7 +302,7 @@ if (GCC_VERSION) endif() message(STATUS "Configured C_FLAGS: ${CMAKE_C_FLAGS}") -set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/${lib_dir}) +set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/${CMAKE_INSTALL_LIBDIR}) set(CMAKE_DEBUG_POSTFIX "_d") @@ -310,6 +310,14 @@ if (CMAKE_BUILD_TYPE MATCHES "Debug") set(CMAKE_EXECUTABLE_SUFFIX "_d${CMAKE_EXECUTABLE_SUFFIX}") endif() +if(BUILD_SHARED_LIBS) + if(APPLE) + set(CMAKE_INSTALL_RPATH "@executable_path/../${CMAKE_INSTALL_LIBDIR}") + set(CMAKE_MACOSX_RPATH 1) + elseif(UNIX) + set(CMAKE_INSTALL_RPATH "$ORIGIN/../${CMAKE_INSTALL_LIBDIR}") + endif() +endif() if (FLATCC_RTONLY) # The targets we copy to bin and lib directories, i.e. not tests. @@ -329,8 +337,18 @@ else() add_subdirectory(src/cli) endif() +set_target_properties(${dist_targets} PROPERTIES + ARCHIVE_OUTPUT_DIRECTORY "${dist_dir}/${CMAKE_INSTALL_LIBDIR}" + LIBRARY_OUTPUT_DIRECTORY "${dist_dir}/${CMAKE_INSTALL_LIBDIR}" + RUNTIME_OUTPUT_DIRECTORY "${dist_dir}/${CMAKE_INSTALL_BINDIR}" +) + +# FindFlatCCNative.cmake creates the flatcc::cli_native target +find_package(FlatCCNative REQUIRED) + # disabled by FLATCC_RTONLY if (FLATCC_TEST) + enable_testing() add_subdirectory(test) add_subdirectory(samples) endif() @@ -341,16 +359,9 @@ if (FLATCC_COVERAGE) COMMAND genhtml coverage.info --output-directory coverage) endif() -set_target_properties(${dist_targets} - PROPERTIES - ARCHIVE_OUTPUT_DIRECTORY "${dist_dir}/${lib_dir}" - LIBRARY_OUTPUT_DIRECTORY "${dist_dir}/${lib_dir}" - RUNTIME_OUTPUT_DIRECTORY "${dist_dir}/bin" -) - if (FLATCC_INSTALL) install(DIRECTORY include/flatcc DESTINATION include) - install(EXPORT flatcc_exports NAMESPACE flatcc:: DESTINATION ${lib_dir}/cmake/flatcc FILE flatcc-targets.cmake) + install(EXPORT flatcc_exports NAMESPACE flatcc:: DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/flatcc FILE flatcc-targets.cmake) file(READ include/flatcc/flatcc_version.h FLATCC_VERSION_H_CONTENT) string(REGEX MATCH "#[ \t]*define[ \t]+FLATCC_VERSION_TEXT[ \t]+\"([^\"]+)\"" version_match "${FLATCC_VERSION_H_CONTENT}") @@ -371,5 +382,6 @@ if (FLATCC_INSTALL) "${CMAKE_CURRENT_BINARY_DIR}/flatcc-config.cmake" "${CMAKE_CURRENT_BINARY_DIR}/flatcc-config-version.cmake" cmake/FlatccGenerateSources.cmake - DESTINATION "lib/cmake/flatcc") + cmake/FindFlatCCNative.cmake + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/flatcc") endif() diff --git a/README.md b/README.md index 81d89e1ed..3b5bd7519 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,6 @@ executable also handle optional json parsing or printing in less than 2 us for a * [Online Forums](#online-forums) * [Introduction](#introduction) * [Project Details](#project-details) -* [Use in CMake build](#use-in-cmake-build) * [Poll on Meson Build](#poll-on-meson-build) * [Reporting Bugs](#reporting-bugs) * [Status](#status) @@ -35,6 +34,7 @@ executable also handle optional json parsing or printing in less than 2 us for a * [Use of Macros in Generated Code](#use-of-macros-in-generated-code) * [Extracting Documentation](#extracting-documentation) * [Using flatcc](#using-flatcc) + * [Using flatcc in CMake projects](#using-flatcc-in-cmake-projects) * [Trouble Shooting](#trouble-shooting) * [Quickstart](#quickstart) * [Reading a Buffer](#reading-a-buffer) @@ -155,13 +155,13 @@ The project includes: See also: -- [Reporting Bugs](https://github.com/dvidelabs/flatcc#reporting-bugs) +- [Reporting Bugs](#reporting-bugs) - [Google FlatBuffers](http://google.github.io/flatbuffers/) -- [Build Instructions](https://github.com/dvidelabs/flatcc#building) +- [Build Instructions](#building) -- [Quickstart](https://github.com/dvidelabs/flatcc#quickstart) +- [Quickstart](#quickstart) - [Builder Interface Reference] @@ -187,42 +187,6 @@ their place. **NOTE: Big-endian platforms are only supported as of release 0.4.0.** -## Use in CMake build - -If your project uses the CMake build system, you can include cmake module -`cmake/FlatccGenerateSources.cmake` that provides the following cmake -function: - - flatcc_generate_sources(GENERATED_SOURCE_DIRECTORY - GENERATE_BUILDER - GENERATE_VERIFIER - EXPECTED_OUTPUT_FILES - DEFINITION_FILES -) - -`GENERATE_BUILDER` and `GENERATE_VERIFIER` are boolean options. When specified -they will instruct flatcc to generate builder / verifier source code. - -Optionally you can let cmake know the directory where the flatcc executable -is located in environment variable `FLATCC_BUILD_BIN_PATH`. This is especially -usefull when cross-compiling. In that case you should provide the directory -where the build arch flatcc compiler executable is located. - -The flatcc_generate_sources function will create a cmake custom command to -generate the output files during just before compilation (so in the build -step, not the configure step). - -Example: - - include(FlatccGenerateSources) - flatcc_generate_sources(GENERATED_SOURCE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/datadef - GENERATE_BUILDER - GENERATE_VERIFIER - EXPECTED_OUTPUT_FILES datadef/seclif_protocol_reader.h - datadef/seclif_protocol_builder.h - datadef/seclif_protocol_verifier.h - DEFINITION_FILES ${CMAKE_CURRENT_SOURCE_DIR}/datadef/seclif_protocol.fbs - ) ## Poll on Meson Build @@ -445,7 +409,7 @@ uses C99 style code to better follow the C++ version. The build option `FLATCC_TEST` can be used to disable all tests which might make flatcc compile on platforms that are otherwise problematic. -The buld option `FLATCC_CXX_TEST` can be disabled specifically for C++ +The build option `FLATCC_CXX_TEST` can be disabled specifically for C++ tests (a simple C++ file that includes generated C code). ### Platforms reported to work by users @@ -792,6 +756,81 @@ JSON printer and parser can be generated using the --json flag or some certain runtime library compile time flags that can optimize out printing symbolic enums, but these can also be disabled at runtime. +## Using flatcc in CMake projects + +If your project uses the CMake build system, you can include the cmake module +`FlatccGenerateSources.cmake` that provides the following cmake function: + + flatcc_generate_sources( + NAME + SCHEMA_FILES [ [...]] + [OUTPUT_DIR ] + [ALL] [BINARY_SCHEMA] [COMMON] [COMMON_READER] [COMMON_BUILDER] [BUILDER] + [READER] [VERIFIER] [JSON_PARSER] [JSON_PRINTER] [JSON] [RECURSIVE] + [OUTFILE ] [PREFIX ] [TARGET ] + [PATHS [ [...]]] + [EXTRA_ARGS [ [...]]] + ) + +This function sets the following targets and variables: + +- `flatcc_generated::` target: linking to this target will automatically +call flatcc for re-generating the sources when the input definition files +have changed + +- `_GENERATED_SOURCES` variable: list of all sources that are generated by flatcc. +These can be used by adding they to `add_library`/`add_executable`. + +- `flatcc_generated_` target: this target can be used as target of your +make program to re-generate the flatcc headers when an input source has changed. + +This function accepts the following arguments: + +- NAME `` is a unique name on which all output variables/targets are based. + +- `SCHEMA_FILES` is a required argument. As argument, it expects +a list of flatbuffer definition files. These sources must be available when +running this function as they are parsed for dependencies. If the definition files +are generated dynamically, then you should make use +[`add_custom_command`](https://cmake.org/cmake/help/latest/command/add_custom_command.html). +See [test/monster_test_solo] for an example on how to do this. + +- `ALL`, `BINARY_SCHEMA`, `COMMON`, `COMMON_READER`, `COMMON_BUILDER`, `BUILDER`, +`READER`, `VERIFIER`, `JSON_PARSER`, `JSON_PRINTER` and `JSON` are boolean +options that instruct flatcc what types of source/binary to generate. +Documentation of all arguments can be found at the top of +`FlatccGEnerateSources.cmake`. + +Note: normally COMMON should also be specified along with BUILDER unless common files +are generated by another build step. For simple configurations ALL is likely the best choice. + +This function uses CMake's `add_custom command` internally. This means that +the generated headers are only available in the build step, not the configure +step. + +Example: + + find_package(flatcc REQUIRED) + + include(FlatccGenerateSources) + flatcc_generate_sources( + NAME seclif_protocol + SCHEMA_FILES "datadef/seclif_protocol.fbs" + OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/datadef" + BUILDER + VERIFIER + ) + + add_executable(my_main main.c) + target_link_libraries(seclif_protocol PRIVATE flatcc::runtime flatcc_generated::seclif_protocol) + +Cross building with flatbuffers on cmake is possible by providing the install +location of the build architecture flatcc package via the cmake variable `FLATCC_ROOT_FOR_BUILD` +or environment variable `FLATCC_ROOT_FOR_BUILD`. The flatcc_generate_sources +function will then use the *build* architecture flatcc cli executable to +generate the header files and link your target to the *host* architecture +libflatccrt library. See section [Cross-compilation](#cross-compilation) for an example. + ## Trouble Shooting Make sure to link with `libflatccrt` (rt for runtime) and not `libflatcc` (the schema compiler), otherwise the builder will not be available. Also make sure to have the 'include' of the flatcc project root in the include path. @@ -837,12 +876,12 @@ including JSON printing. ## Quickstart -After [building](https://github.com/dvidelabs/flatcc#building) the `flatcc tool`, +After [building](#building) the `flatcc tool`, binaries are located in the `bin` and `lib` directories under the `flatcc` source tree. You can either jump directly to the [monster -example](https://github.com/dvidelabs/flatcc/tree/master/samples/monster) +example](samples/monster) that follows [Googles FlatBuffers Tutorial](https://google.github.io/flatbuffers/flatbuffers_guide_tutorial.html), or you can read along the quickstart guide below. If you follow the monster tutorial, you may want to clone and build flatcc and copy @@ -867,6 +906,10 @@ files](https://google.github.io/flatbuffers/flatbuffers_guide_writing_schema.htm The [Builder Interface Reference] may be useful after studying the monster sample and quickstart below. +If you're using CMake as a build system, a utility function exists to easily integrate +flatcc. See [the documentation of flatcc_generate_sources](#use-in-cmake-build) +for a short overview + example. + When looking for advanced examples such as sorting vectors and finding elements by a key, you should find these in the [`test/monster_test`](https://github.com/dvidelabs/flatcc/tree/master/test/monster_test) project. @@ -1391,7 +1434,7 @@ Likewise, type hashes are always tested in native (host) endian format. The -[`flatcc/flatcc_identifier.h`](https://github.com/dvidelabs/flatcc/blob/master/include/flatcc/flatcc_identifier.h) +[`flatcc/flatcc_identifier.h`](include/flatcc/flatcc_identifier.h) file contains an implementation of the FNV-1a hash used. The hash was chosen for simplicity, availability, and collision resistance. For better distribution, and for internal use only, a dispersion function is @@ -2304,6 +2347,9 @@ In Visual Studio: that `include\flatcc\portable\pwarnings.h` disable certain warnings for warning level -W3.* +*Building the tests require at least CMake 3.3. Passing `-DFLATCC_TEST=OFF` +to cmake disables building them. + ### Docker Docker image: @@ -2313,33 +2359,38 @@ Docker image: ### Cross-compilation -Users have been reporting some degree of success using cross compiles -from Linux x86 host to embedded ARM Linux devices. +Cross-compilation is possible when using the CMake build system. First you have +to build and install flatcc for the build architecture. Then when cross-compiling +provide the install location of the build architecture flatcc package via +environment variable `FLATCC_ROOT_FOR_BUILD` or via cmake variable `FLATCC_ROOT_FOR_BUILD`. -For this to work, `FLATCC_TEST` option should be disabled in part -because cross-compilation cannot run the cross-compiled flatcc tool, and -in part because there appears to be some issues with CMake custom build -steps needed when building test and sample projects. +It is possible to build the tests for your target system, but not to run them on the build system, unless you've configured +[`CMAKE_CROSSCOMPILING_EMULATOR`](https://cmake.org/cmake/help/latest/variable/CMAKE_CROSSCOMPILING_EMULATOR.html). +The option `FLATCC_RTONLY` will disable tests and only build the runtime library. +It is highly recommended to at least run the `tests/monster_test` project on your target platform. -The option `FLATCC_RTONLY` will disable tests and only build the runtime -library. -The following is not well tested, but may be a starting point: +The following may be a starting point: - mkdir -p build/xbuild - cd build/xbuild - cmake ../.. -DBUILD_SHARED_LIBS=on -DFLATCC_RTONLY=on \ - -DCMAKE_BUILD_TYPE=Release + mkdir -p build/buildarch && cd build/buildarch + cmake ../.. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/my/install/dir/flatcc_x86_64 -DFLATCC_INSTALL=ON + cmake --build . + cmake --build . --target install + cd .. + + + mkdir hostarch && cd hostarch + cmake ../.. -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE= -DCMAKE_INSTALL_PREFIX=/my/install/dir/flatcc_cortexa9 -DFLATCC_ROOT_FOR_BUILD=/my/install/dir/flatcc_x86_64 -DFLATCC_INSTALL=ON + cmake --build . + cmake --build . --target install -Overall, it may be simpler to create a separate Makefile and just -compile the few `src/runtime/*.c` into a library and distribute the -headers as for other platforms, unless `flatcc` is also required for the -target. Or to simply include the runtime source and header files in the user -project. + cd + mkdir -p build/cortexa9 && cd build/cortexa9 + cmake ../.. -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE= -DCMAKE_PREFIX_PATH=/my/install/dir/flatcc_cortexa9 -DFLATCC_ROOT_FOR_BUILD=/my/install/dir/flatcc_x86_64 + cmake --build . -Note that no tests will be built nor run with `FLATCC_RTONLY` enabled. -It is highly recommended to at least run the `tests/monster_test` -project on a new platform. +Note that flatcc is also available as a [Conan](https://conan.io) package in [conan-center](https://conan.io/center). This can simplify cross- +compiling even more. ### Custom Allocation diff --git a/appveyor.yml b/appveyor.yml index 87499a961..55c32818e 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -12,7 +12,7 @@ shallow_clone: true clone_folder: c:\projects\flatcc platform: - - Win32 + - Win32 - x64 configuration: @@ -35,4 +35,3 @@ build: test_script: - ctest -C "%configuration%" -VV - diff --git a/cmake/FindFlatCCNative.cmake b/cmake/FindFlatCCNative.cmake new file mode 100644 index 000000000..24fa157bd --- /dev/null +++ b/cmake/FindFlatCCNative.cmake @@ -0,0 +1,39 @@ +if(TARGET flatcc::cli_native) + message(WARNING "flatcc:::cli_native is already defined") +else() + set(__find_program_flatcc OFF) + if(CMAKE_CROSSCOMPILING) + set(__find_program_flatcc ON) + else() + if(NOT TARGET flatcc::cli AND NOT DEFINED flatcc_FOUND) + find_package(flatcc REQUIRED) + endif() + if(TARGET flatcc::cli) + get_property(_flatcc_cli_alias TARGET flatcc::cli PROPERTY ALIASED_TARGET) + if(_flatcc_cli_alias) + add_executable(flatcc::cli_native ALIAS ${_flatcc_cli_alias}) + else() + add_executable(flatcc::cli_native ALIAS flatcc::cli) + endif() + else() + set(__find_program_flatcc ON) + endif() + endif() + + if(__find_program_flatcc) + find_program(FLATCC_BIN + NAMES flatcc flatcc_d + PATHS "${FLATCC_ROOT_FOR_BUILD}" ENV FLATCC_ROOT_FOR_BUILD + PATH_SUFFIXES bin + DOC "Path of native flatcc executable" + NO_DEFAULT_PATH + ) + if(NOT FLATCC_BIN) + message(FATAL_ERROR "Could not find native flatcc executable.") + endif() + add_executable(flatcc::cli_native IMPORTED) + set_property(TARGET flatcc::cli_native PROPERTY IMPORTED_LOCATION "${FLATCC_BIN}") + endif() +endif() + +set(FlatCCNative_FOUND ON) diff --git a/cmake/FlatccGenerateSources.cmake b/cmake/FlatccGenerateSources.cmake index 6bd2725a9..06d48ab21 100644 --- a/cmake/FlatccGenerateSources.cmake +++ b/cmake/FlatccGenerateSources.cmake @@ -1,55 +1,139 @@ -# Use the following function to generate C source files from flatbuffer definition files: +# Use `flatcc_generate_sources` to generate C source files from flatbuffer schema files. +# The sources will be generated at build time, and only if some target has the output target +# as a dependency or adds the generated sources. # -# flatcc_generate_sources(OUTPUT_DIR -# BUILDER -# VERIFIER -# DEFINITION_FILES -# ) +# flatcc_generate_sources( +# NAME +# SCHEMA_FILES [ [...]] +# [ALL] [BINARY_SCHEMA] [COMMON] [COMMON_READER] [COMMON_BUILDER] [BUILDER] [READER] +# [OUTPUT_DIR ] +# [VERIFIER] [JSON_PARSER] [JSON_PRINTER] [JSON] [RECURSIVE] +# [OUTFILE ] [PREFIX ] +# [PATHS [ [...]]] +# [EXTRA_ARGS [ [...]]] +# ) # -# BUILDER and VERIFIER are boolean options. When specified they will instruct -# flatcc to generate builder / verifier source code. +# The following things are returned: +# +# _GENERATED_SOURCES +# +# Variable containing a list of all generated sources. +# This variable can be added to `add_library`/`add_executable`. +# +# flatcc_generated:: +# +# Target that contains all headers. Users must link to it. +# When linking to it, the headers will be generated. +# The folder containing these headers will also be added to the include path. +# +# Required arguments: +# +# NAME +# +# Unique name. It is used for output variable(s) and target(s). +# +# SCHEMA_FILES [ [...]] +# +# Flatcc will generate sources for all schema files listed here. +# This function also track included files, so it is not needed to list all dependent files here. +# Because the search for included files happens at configure time, +# the schema files must be available before calling this function. +# +# Optional arguments: +# +# OUTPUT_DIR +# +# All generated sources will be written in the `output-directory` folder. +# +# ALL +# BINARY_SCHEMA +# COMMON +# COMMON_READER +# COMMON_BUILDER +# BUILDER +# READER +# VERIFIER +# JSON_PARSER +# JSON_PRINTER +# JSON +# RECURSIVE +# +# When specified, these will instruct flatcc to generate a specific type of source code. +# It is preferable to use these options instead of adding flatcc arguments to COMPILE_DEFINITIONS. +# This is because the generated sources will have correct dependency information. +# When none of these are specified, the default is READER. +# These options can be combined. +# +# OUTFILE +# +# Write all source into one file named `output-file`. +# +# PREFIX +# +# Prefix all symbols with `prefix` +# +# PATHS [ [...]] +# +# Add extra include search paths where flatcc should look for included defintion files. +# +# EXTRA_ARGS [ [...]] +# +# Add extra arguments to flatcc. +# Use the arguments `ALL`/`COMMON`/`READER`/`JSON` instead of specifying `-a`/`-c`/`--reader`/`--json` here. # -# With cross-compiling you should provide the directory where the flatcc compiler executable is located -# in environment variable FLATCC_BUILD_BIN_PATH. If you use Conan and add flatcc as a build requirement -# this will be done automatically. -include(CMakeParseArguments) +cmake_minimum_required(VERSION 3.3) +# TODO: not needed when cmake_minimum_required_version >= 3.4 +include(CMakeParseArguments) function(flatcc_generate_sources) # parse function arguments - set(output_options SCHEMA COMMON COMMON_READER COMMON_BUILDER BUILDER READER VERIFIER JSON_PARSER JSON_PRINTER JSON) + set(output_options BINARY_SCHEMA COMMON COMMON_READER COMMON_BUILDER BUILDER READER VERIFIER JSON_PARSER JSON_PRINTER JSON) set(NO_VAL_ARGS ALL RECURSIVE ${output_options}) - set(SINGLE_VAL_ARGS OUTPUT_DIR OUTFILE PREFIX TARGET) - set(MULTI_VAL_ARGS DEFINITION_FILES COMPILE_FLAGS PATHS) + set(SINGLE_VAL_ARGS NAME OUTPUT_DIR OUTFILE PREFIX) + set(MULTI_VAL_ARGS SCHEMA_FILES EXTRA_ARGS PATHS) cmake_parse_arguments(FLATCC "${NO_VAL_ARGS}" "${SINGLE_VAL_ARGS}" "${MULTI_VAL_ARGS}" ${ARGN}) + + # TODO: add following check when cmake_minimum_required_version >= 3.15 + #if (FLATCC_KEYWORDS_MISSING_VALUES) + # message(FATAL_ERROR "The following keywords in call to flatcc_generate_sources have no value: ${FLATCC_KEYWORDS_MISSING_VALUES}") + #endif() - if (NOT FLATCC_DEFINITION_FILES) - message(FATAL_ERROR "No flatbuffer definition files provided") + if (FLATCC_UNPARSED_ARGUMENTS) + message(WARNING "The following unknown keywords in call to flatcc_generate_sources are ignored: ${FLATCC_UNPARSED_ARGUMENTS}") + endif() + + if (NOT FLATCC_NAME) + message(FATAL_ERROR "NAME not provided in call to flatcc_generate_sources") + endif() + + if (NOT FLATCC_SCHEMA_FILES) + message(FATAL_ERROR "No flatbuffer schema files provided") endif() if (NOT FLATCC_OUTPUT_DIR) set(FLATCC_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}") endif() + if (NOT IS_ABSOLUTE "${FLATCC_OUTPUT_DIR}") + set(FLATCC_OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/${FLATCC_OUTPUT_DIR}") + endif() - list(APPEND FLATCC_COMPILE_FLAGS -o "${FLATCC_OUTPUT_DIR}") + list(APPEND FLATCC_ARGS -o "${FLATCC_OUTPUT_DIR}") # Add current source directory for finding dependencies set(absolute_flatcc_paths "${CMAKE_CURRENT_SOURCE_DIR}") - # Also add directory of definition files - foreach(definition_file FLATCC_DEFINITION_FILES) - endforeach() - set(ABSOLUTE_DEFINITION_FILES) - foreach(definition_file ${FLATCC_DEFINITION_FILES}) + set(ABSOLUTE_SCHEMA_FILES) + foreach(schema_file ${FLATCC_SCHEMA_FILES}) # TODO: file(REAL_PATH) if cmake_minimum_required_version >= 3.19 - if(IS_ABSOLUTE "${definition_file}") - set(absolute_def_file "${definition_file}") + if(IS_ABSOLUTE "${schema_file}") + set(absolute_def_file "${schema_file}") else() - set(absolute_def_file "${CMAKE_CURRENT_SOURCE_DIR}/${definition_file}") + set(absolute_def_file "${CMAKE_CURRENT_SOURCE_DIR}/${schema_file}") endif() - list(APPEND ABSOLUTE_DEFINITION_FILES "${absolute_def_file}") + list(APPEND ABSOLUTE_SCHEMA_FILES "${absolute_def_file}") get_filename_component(absolute_def_directory "${absolute_def_file}" DIRECTORY) list(APPEND absolute_flatcc_paths "${absolute_def_directory}") @@ -61,7 +145,7 @@ function(flatcc_generate_sources) set(path "${CMAKE_CURRENT_SOURCE_DIR}/${path}") endif() list(APPEND absolute_flatcc_paths "${path}") - list(APPEND FLATCC_COMPILE_FLAGS -I "${path}") + list(APPEND FLATCC_ARGS -I "${path}") endforeach() list(REMOVE_DUPLICATES absolute_flatcc_paths) @@ -79,64 +163,64 @@ function(flatcc_generate_sources) # Add flatcc options if(FLATCC_PREFIX) - list(APPEND FLATCC_COMPILE_FLAGS "--prefix=${FLATCC_PREFIX}") + list(APPEND FLATCC_ARGS "--prefix=${FLATCC_PREFIX}") endif() # Handle reader option first as other option encompass reader if (FLATCC_READER) - list(APPEND FLATCC_COMPILE_FLAGS --reader) + list(APPEND FLATCC_ARGS --reader) endif() if (FLATCC_COMMON_READER) - list(APPEND FLATCC_COMPILE_FLAGS --common) + list(APPEND FLATCC_ARGS --common_reader) endif() if (FLATCC_COMMON_BUILDER) - list(APPEND FLATCC_COMPILE_FLAGS --common) + list(APPEND FLATCC_ARGS --common_builder) endif() if (FLATCC_BUILDER) - list(APPEND FLATCC_COMPILE_FLAGS --builder) + list(APPEND FLATCC_ARGS --builder) # Builder also generates reader set(FLATCC_READER ON) endif() if (FLATCC_VERIFIER) - list(APPEND FLATCC_COMPILE_FLAGS --verifier) + list(APPEND FLATCC_ARGS --verifier) # verifier also generates reader set(FLATCC_READER ON) endif() if (FLATCC_JSON_PARSER) - list(APPEND FLATCC_COMPILE_FLAGS --json-parser) + list(APPEND FLATCC_ARGS --json-parser) endif() if (FLATCC_JSON_PRINTER) - list(APPEND FLATCC_COMPILE_FLAGS --json-printer) + list(APPEND FLATCC_ARGS --json-printer) endif() - if (FLATCC_SCHEMA) - list(APPEND FLATCC_COMPILE_FLAGS --schema) + if (FLATCC_BINARY_SCHEMA) + list(APPEND FLATCC_ARGS --schema) endif() if (FLATCC_RECURSIVE) - list(APPEND FLATCC_COMPILE_FLAGS --recursive) + list(APPEND FLATCC_ARGS --recursive) endif() # handle 'all', 'common', 'json' last as they encompass other options if (FLATCC_COMMON) - list(APPEND FLATCC_COMPILE_FLAGS --common) + list(APPEND FLATCC_ARGS --common) set(FLATCC_COMMON_READER ON) set(FLATCC_COMMON_BUILDER ON) endif() if (FLATCC_JSON) - list(APPEND FLATCC_COMPILE_FLAGS --json) + list(APPEND FLATCC_ARGS --json) set(FLATCC_JSON_PARSER ON) set(FLATCC_JSON_PRINTER ON) endif() if (FLATCC_ALL) - list(APPEND FLATCC_COMPILE_FLAGS -a) - set(FLATCC_READER ON) + list(APPEND FLATCC_ARGS -a) set(FLATCC_COMMON_BUILDER ON) set(FLATCC_COMMON_READER ON) set(FLATCC_BUILDER ON) set(FLATCC_VERIFIER ON) + set(FLATCC_READER ON) + set(FLATCC_RECURSIVE ON) endif() # Calculate suffixes of output files. - set(GENERATED_FILE_SUFFIXES) if(FLATCC_READER) list(APPEND GENERATED_FILE_SUFFIXES _reader.h) @@ -153,16 +237,16 @@ function(flatcc_generate_sources) if(FLATCC_JSON_PRINTER) list(APPEND GENERATED_FILE_SUFFIXES _json_printer.h) endif() - if(FLATCC_SCHEMA) + if(FLATCC_BINARY_SCHEMA) list(APPEND GENERATED_FILE_SUFFIXES .bfbs) endif() - # grep each definition file recursively for includes, convert them to absolute paths and add them to a list + # grep each schema file recursively for includes, convert them to absolute paths and add them to a list set(ABSOLUTE_DEFINITIONS_DEPENDENCIES) - set(absolute_definition_files_todo ${ABSOLUTE_DEFINITION_FILES}) - while(absolute_definition_files_todo) - list(GET absolute_definition_files_todo 0 current_deffile) - # TODO: use if(absolute_definition_files_todo IN_LIST ABSOLUTE_DEFINITIONS_DEPENDENCIES) if cmake_minimum_required_version >= 3.3 + set(absolute_schema_files_todo ${ABSOLUTE_SCHEMA_FILES}) + while(absolute_schema_files_todo) + list(GET absolute_schema_files_todo 0 current_deffile) + # TODO: use if(absolute_schema_files_todo IN_LIST ABSOLUTE_DEFINITIONS_DEPENDENCIES) if cmake_minimum_required_version >= 3.3 list(FIND ABSOLUTE_DEFINITIONS_DEPENDENCIES "${current_deffile}" todo_index) if(todo_index LESS 0) list(APPEND ABSOLUTE_DEFINITIONS_DEPENDENCIES "${current_deffile}") @@ -188,28 +272,29 @@ function(flatcc_generate_sources) endif() endif() if(abs_include_def_file) - list(APPEND absolute_definition_files_todo "${abs_include_def_file}") + list(APPEND absolute_schema_files_todo "${abs_include_def_file}") endif() endforeach() endif() - list(REMOVE_AT absolute_definition_files_todo 0) + list(REMOVE_AT absolute_schema_files_todo 0) endwhile() list(REMOVE_DUPLICATES ABSOLUTE_DEFINITIONS_DEPENDENCIES) - list(REMOVE_DUPLICATES ABSOLUTE_DEFINITION_FILES) + list(REMOVE_DUPLICATES ABSOLUTE_SCHEMA_FILES) set(OUTPUT_FILES) if(FLATCC_OUTFILE) - list(APPEND FLATCC_COMPILE_FLAGS "--outfile=${FLATCC_OUTPUT_DIR}/${FLATCC_OUTFILE}") + list(APPEND FLATCC_ARGS "--outfile=${FLATCC_OUTPUT_DIR}/${FLATCC_OUTFILE}") list(APPEND OUTPUT_FILES "${FLATCC_OUTPUT_DIR}/${FLATCC_OUTFILE}") else() if(FLATCC_RECURSIVE) - set(definition_files ${ABSOLUTE_DEFINITIONS_DEPENDENCIES}) + set(SCHEMA_FILES ${ABSOLUTE_DEFINITIONS_DEPENDENCIES}) else() - set(definition_files ${ABSOLUTE_DEFINITION_FILES}) + set(SCHEMA_FILES ${ABSOLUTE_SCHEMA_FILES}) endif() - foreach(definition_file ${definition_files}) - get_filename_component(def_name_we "${definition_file}" NAME_WLE) + foreach(schema_file ${SCHEMA_FILES}) + # TODO: should be NAME_WLE, but not supported in cmake 2.8 + get_filename_component(def_name_we "${schema_file}" NAME_WE) foreach(suffix ${GENERATED_FILE_SUFFIXES}) list(APPEND OUTPUT_FILES "${FLATCC_OUTPUT_DIR}/${def_name_we}${suffix}") endforeach() @@ -223,22 +308,33 @@ function(flatcc_generate_sources) endif() endif() - message(VERBOSE "---- flatcc start ----") - message(VERBOSE "output directory: ${FLATCC_OUTPUT_DIR}") - message(VERBOSE "output files: ${OUTPUT_FILES}") - message(VERBOSE "execute: flatcc;${FLATCC_COMPILE_FLAGS};${ABSOLUTE_DEFINITION_FILES}") - message(VERBOSE "dependencies: ${ABSOLUTE_DEFINITIONS_DEPENDENCIES}") - message(VERBOSE "----- flatcc end -----") + list(APPEND FLATCC_ARGS ${FLATCC_EXTRA_ARGS}) + + # TODO: VERBOSE was added in cmake 3.15. + if(NOT (CMAKE_VERSION VERSION_LESS 3.15)) + message(VERBOSE "---- flatcc info start ----") + message(VERBOSE "output directory: ${FLATCC_OUTPUT_DIR}") + message(VERBOSE "output files: ${OUTPUT_FILES}") + message(VERBOSE "execute: flatcc;${FLATCC_ARGS};${ABSOLUTE_SCHEMA_FILES}") + message(VERBOSE "dependencies: ${ABSOLUTE_DEFINITIONS_DEPENDENCIES}") + message(VERBOSE "----- flatcc info end -----") + endif() + + file(MAKE_DIRECTORY "${FLATCC_OUTPUT_DIR}") add_custom_command(OUTPUT ${OUTPUT_FILES} COMMAND "${CMAKE_COMMAND}" -E make_directory "${FLATCC_OUTPUT_DIR}" - COMMAND flatcc::cli ${FLATCC_COMPILE_FLAGS} ${ABSOLUTE_DEFINITION_FILES} - DEPENDS flatcc::cli ${ABSOLUTE_DEFINITIONS_DEPENDENCIES} + COMMAND flatcc::cli_native ${FLATCC_ARGS} ${ABSOLUTE_SCHEMA_FILES} + DEPENDS flatcc::cli_native ${ABSOLUTE_DEFINITIONS_DEPENDENCIES} ) - if(FLATCC_TARGET) - # TODO: possible improvement if cmake_minimum_required_version >= 3.1: - # use add_library(... INTERFACE) + add_custom_command + target_sources(INTERFACE) + target_link_libraries() - add_custom_target("${FLATCC_TARGET}" DEPENDS ${OUTPUT_FILES}) - endif() + add_custom_target("flatcc_generated_${FLATCC_NAME}" + DEPENDS ${OUTPUT_FILES}) + + add_library("__flatcc_generated_${FLATCC_NAME}" INTERFACE) + target_include_directories("__flatcc_generated_${FLATCC_NAME}" INTERFACE "${FLATCC_OUTPUT_DIR}") + add_dependencies("__flatcc_generated_${FLATCC_NAME}" "flatcc_generated_${FLATCC_NAME}") + + add_library("flatcc_generated::${FLATCC_NAME}" ALIAS "__flatcc_generated_${FLATCC_NAME}") + set("${FLATCC_NAME}_GENERATED_SOURCES" ${OUTPUT_FILES} PARENT_SCOPE) endfunction() diff --git a/cmake/flatcc-config.cmake.in b/cmake/flatcc-config.cmake.in index f31d30439..b32c12176 100644 --- a/cmake/flatcc-config.cmake.in +++ b/cmake/flatcc-config.cmake.in @@ -1,7 +1,18 @@ @PACKAGE_INIT@ +set(FLATCC_RTONLY @FLATCC_RTONLY@) +set(FLATCC_REFLECTION @FLATCC_REFLECTION@) + +cmake_minimum_required(VERSION 3.3) + +@PACKAGE_INIT@ + # Make available all imported targets include("${CMAKE_CURRENT_LIST_DIR}/flatcc-targets.cmake") # Make available the cmake scripts in the current folder list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}") + +find_package(FlatCCNative REQUIRED) + +check_required_components(flatcc) diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index c05a45017..81eb5d369 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -1,3 +1,5 @@ +include(FlatccGenerateSources) + if (FLATCC_NEED_C89_VAR_DECLS) MESSAGE( STATUS "Disabling monster sample: needed C99 style variable declarations not supported by target compiler") else() diff --git a/samples/monster/CMakeLists.txt b/samples/monster/CMakeLists.txt index 3e620207c..d2c6fa5b6 100644 --- a/samples/monster/CMakeLists.txt +++ b/samples/monster/CMakeLists.txt @@ -1,13 +1,15 @@ set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") -set(FBS_DIR "${CMAKE_CURRENT_SOURCE_DIR}") -include_directories("${GEN_DIR}") +# Don't use FBS_DIR to test whether flatcc_generate_sources knows to use the correct file. -flatcc_generate_sources(ALL OUTPUT_DIR "${GEN_DIR}" DEFINITION_FILES monster.fbs) +flatcc_generate_sources( + NAME monster_sample + SCHEMA_FILES monster.fbs + ALL + OUTPUT_DIR "${GEN_DIR}" +) -add_executable(monster monster.c "${GEN_DIR}/monster_builder.h") -target_link_libraries(monster flatcc::runtime) +add_executable(monster monster.c) +target_link_libraries(monster PRIVATE flatcc::runtime flatcc_generated::monster_sample) -if (FLATCC_TEST) - add_test(NAME monster COMMAND monster) -endif() +add_test(NAME monster COMMAND monster) diff --git a/samples/reflection/CMakeLists.txt b/samples/reflection/CMakeLists.txt index d988d026c..fea42fd81 100644 --- a/samples/reflection/CMakeLists.txt +++ b/samples/reflection/CMakeLists.txt @@ -10,13 +10,14 @@ set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/samples/monster") -include_directories("${GEN_DIR}") +flatcc_generate_sources( + NAME reflection_sample + SCHEMA_FILES "${FBS_DIR}/monster.fbs" + BINARY_SCHEMA + OUTPUT_DIR "${GEN_DIR}" +) -flatcc_generate_sources(SCHEMA OUTPUT_DIR "${GEN_DIR}" DEFINITION_FILES "${FBS_DIR}/monster.fbs") +add_executable(bfbs2json bfbs2json.c) +target_link_libraries(bfbs2json PRIVATE flatcc::runtime flatcc_generated::reflection_sample) -add_executable(bfbs2json bfbs2json.c "${GEN_DIR}/monster.bfbs") -target_link_libraries(bfbs2json flatcc::runtime) - -if (FLATCC_TEST) - add_test(NAME bfbs2json COMMAND bfbs2json "${GEN_DIR}/monster.bfbs") -endif() +add_test(NAME bfbs2json COMMAND bfbs2json "${GEN_DIR}/monster.bfbs") diff --git a/src/cli/CMakeLists.txt b/src/cli/CMakeLists.txt index fe9b06f7f..87175c6b4 100644 --- a/src/cli/CMakeLists.txt +++ b/src/cli/CMakeLists.txt @@ -1,23 +1,24 @@ -include_directories ( - "${PROJECT_SOURCE_DIR}/include" - "${PROJECT_SOURCE_DIR}/config" -) - add_executable(flatcc_cli flatcc_cli.c ) add_executable(flatcc::cli ALIAS flatcc_cli) -target_link_libraries(flatcc_cli - flatcc -) +target_link_libraries(flatcc_cli PRIVATE flatcc) +target_include_directories(flatcc_cli PRIVATE "${PROJECT_SOURCE_DIR}/config") # Rename because the libflatcc library and the flatcc executable would # conflict if they had the same target name `flatcc`. set_target_properties(flatcc_cli PROPERTIES - OUTPUT_NAME flatcc$<$:_d> - EXPORT_NAME cli) + OUTPUT_NAME flatcc + EXPORT_NAME cli +) +if(NOT (CMAKE_VERSION VERSION_LESS 3.4)) + # TODO: cmake versions <3.4 do not support generator expressions in OUTPUT_NAME + set_target_properties(flatcc_cli PROPERTIES + OUTPUT_NAME flatcc$<$:_d> + ) +endif() if (FLATCC_INSTALL) - install(TARGETS flatcc_cli EXPORT flatcc_exports DESTINATION bin) + install(TARGETS flatcc_cli EXPORT flatcc_exports DESTINATION "${CMAKE_INSTALL_BINDIR}") endif() diff --git a/src/compiler/CMakeLists.txt b/src/compiler/CMakeLists.txt index 2663259c9..d71cdf066 100644 --- a/src/compiler/CMakeLists.txt +++ b/src/compiler/CMakeLists.txt @@ -1,8 +1,3 @@ -include_directories ( - "${PROJECT_SOURCE_DIR}/external" - "${PROJECT_SOURCE_DIR}/config" -) - set (SOURCES ${PROJECT_SOURCE_DIR}/external/hash/cmetrohash64.c ${PROJECT_SOURCE_DIR}/external/hash/str_set.c @@ -32,16 +27,27 @@ set (SOURCES ) if (FLATCC_REFLECTION) - set (SOURCES ${SOURCES} codegen_schema.c) + list(APPEND SOURCES codegen_schema.c) endif(FLATCC_REFLECTION) add_library(flatcc ${SOURCES}) -add_library(flatcc::libflatcc ALIAS flatcc) +add_library(flatcc::compiler ALIAS flatcc) -target_include_directories(flatcc PUBLIC $ $) +target_include_directories(flatcc + PUBLIC + "$" + $ + PRIVATE + "${PROJECT_SOURCE_DIR}/external" + "${PROJECT_SOURCE_DIR}/config" +) -set_target_properties(flatcc PROPERTIES EXPORT_NAME libflatcc) +set_target_properties(flatcc PROPERTIES EXPORT_NAME compiler) if (FLATCC_INSTALL) - install(TARGETS flatcc EXPORT flatcc_exports DESTINATION ${lib_dir}) + install(TARGETS flatcc EXPORT flatcc_exports + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + ) endif() diff --git a/src/runtime/CMakeLists.txt b/src/runtime/CMakeLists.txt index f1aecef44..6aab059a9 100644 --- a/src/runtime/CMakeLists.txt +++ b/src/runtime/CMakeLists.txt @@ -8,11 +8,15 @@ add_library(flatccrt ) add_library(flatcc::runtime ALIAS flatccrt) -target_include_directories(flatccrt PUBLIC $ $) +target_include_directories(flatccrt PUBLIC $ $) set_target_properties(flatccrt PROPERTIES EXPORT_NAME runtime) if (FLATCC_INSTALL) - install(TARGETS flatccrt EXPORT flatcc_exports DESTINATION ${lib_dir}) + install(TARGETS flatccrt EXPORT flatcc_exports + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + ) endif() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 59a61323b..6a3c876f4 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,7 +1,8 @@ +include(FlatccGenerateSources) + # Note: some files under source control may be tested with binary comparison. # Under git such files are protected with the `.gitattributes` file. # Incorrect line endings may lead to failed tests. -if (FLATCC_TEST) if (FLATCC_CXX_TEST) # This is tests is primarly for making sure C++ users can use # generated FlatCC code. It fails for pre GCC 4.7 C++ because both @@ -24,4 +25,3 @@ add_subdirectory(optional_scalars_test) if (FLATCC_REFLECTION) add_subdirectory(reflection_test) endif() -endif() diff --git a/test/cgen_test/CMakeLists.txt b/test/cgen_test/CMakeLists.txt index 1d85117a5..be8b08eb6 100644 --- a/test/cgen_test/CMakeLists.txt +++ b/test/cgen_test/CMakeLists.txt @@ -3,37 +3,45 @@ add_executable(cgen_test ) target_link_libraries(cgen_test - flatcc::libflatcc + flatcc::compiler ) add_test(NAME cgen_test COMMAND cgen_test) -# Compilation of the generated code tests many import edge cases -# in the parser and code generator but due to CMake limitations, -# custom target dependencies only work for Make build targets. -# -# expansion of flags results in quotes the compiler won't eat, -# separating arguments should fix this, but not sure how portable it is. -# see also http://stackoverflow.com/questions/9870162/avoid-quoting-in-cmake-add-custom-command - -add_custom_COMMAND(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/test_generated${CMAKE_EXECUTABLE_SUFFIX}.c" - COMMAND cgen_test > "${CMAKE_CURRENT_BINARY_DIR}/test_generated${CMAKE_EXECUTABLE_SUFFIX}.c" - WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/test/cgen_test - DEPENDS cgen_test -) - -if(NOT MSVC) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=unused-const-variable") +if(CMAKE_VERSION VERSION_LESS 3.10) + message(WARNING "generator test disabled due to old cmake version") +else() + # TODO: the use of generator expressions requires at least cmake versions 3.10 (3.5 does not, 3.10 does) + + # Compilation of the generated code tests many import edge cases + # in the parser and code generator but due to CMake limitations, + # custom target dependencies only work for Make build targets. + # + # expansion of flags results in quotes the compiler won't eat, + # separating arguments should fix this, but not sure how portable it is. + # see also http://stackoverflow.com/questions/9870162/avoid-quoting-in-cmake-add-custom-command + + add_custom_command( + OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/test_generated${CMAKE_EXECUTABLE_SUFFIX}.c" + COMMAND cgen_test > "${CMAKE_CURRENT_BINARY_DIR}/test_generated${CMAKE_EXECUTABLE_SUFFIX}.c" + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/test/cgen_test + DEPENDS cgen_test + ) + + if(NOT MSVC) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error=unused-const-variable") + endif() + add_library(test_generated_objects OBJECT + "${CMAKE_CURRENT_BINARY_DIR}/test_generated${CMAKE_EXECUTABLE_SUFFIX}.c" + ) + target_include_directories(test_generated_objects PRIVATE "${PROJECT_SOURCE_DIR}/include") + set_target_properties(test_generated_objects PROPERTIES EXCLUDE_FROM_ALL ON) + + add_custom_target(test_generated + COMMAND "${CMAKE_COMMAND}" -E remove -f "${CMAKE_CURRENT_BINARY_DIR}/test_generated${CMAKE_EXECUTABLE_SUFFIX}.c" $ + COMMAND "${CMAKE_COMMAND}" --build "${PROJECT_BINARY_DIR}" --target test_generated_objects --config $ --verbose + ) + + add_test(NAME test_generated COMMAND ${CMAKE_COMMAND} --build "${PROJECT_BINARY_DIR}" --target test_generated --config $) endif() -add_library(test_generated_objects STATIC EXCLUDE_FROM_ALL - "${CMAKE_CURRENT_BINARY_DIR}/test_generated${CMAKE_EXECUTABLE_SUFFIX}.c" -) -target_include_directories(test_generated_objects PRIVATE "${PROJECT_SOURCE_DIR}/include") - -add_custom_target(test_generated - COMMAND "${CMAKE_COMMAND}" -E remove -f "${CMAKE_CURRENT_BINARY_DIR}/test_generated${CMAKE_EXECUTABLE_SUFFIX}.c" $ - COMMAND "${CMAKE_COMMAND}" --build "${PROJECT_BINARY_DIR}" --target test_generated_objects --config $ --verbose -) - -add_test(NAME test_generated COMMAND ${CMAKE_COMMAND} --build "${PROJECT_BINARY_DIR}" --target test_generated --config $) diff --git a/test/cmake/x86_64-w64-mingw32-toolchain.cmake b/test/cmake/x86_64-w64-mingw32-toolchain.cmake new file mode 100644 index 000000000..4bf4b2b74 --- /dev/null +++ b/test/cmake/x86_64-w64-mingw32-toolchain.cmake @@ -0,0 +1,12 @@ +set(CMAKE_SYSTEM_NAME "Windows") +set(CMAKE_SYSTEM_PROCESSOR "x86_64") + +set(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc) +set(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++) +set(CMAKE_C_FLAGS_INIT "-m64") +set(CMAKE_CXX_FLAGS_INIT "-m64") + +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) diff --git a/test/emit_test/CMakeLists.txt b/test/emit_test/CMakeLists.txt index a4a4258f9..6d7760fa0 100644 --- a/test/emit_test/CMakeLists.txt +++ b/test/emit_test/CMakeLists.txt @@ -1,11 +1,14 @@ set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/emit_test") -include_directories("${GEN_DIR}") - -flatcc_generate_sources(ALL OUTPUT_DIR "${GEN_DIR}" DEFINITION_FILES "${FBS_DIR}/emit_test.fbs") - -add_executable(emit_test emit_test.c "${GEN_DIR}/emit_test_builder.h") -target_link_libraries(emit_test flatcc::runtime) +flatcc_generate_sources( + NAME emit_test + SCHEMA_FILES "${FBS_DIR}/emit_test.fbs" + ALL + OUTPUT_DIR "${GEN_DIR}" +) + +add_executable(emit_test emit_test.c) +target_link_libraries(emit_test PRIVATE flatcc::runtime flatcc_generated::emit_test) add_test(NAME emit_test COMMAND emit_test) diff --git a/test/flatc_compat/CMakeLists.txt b/test/flatc_compat/CMakeLists.txt index c593d24fc..fdf1e8aaf 100644 --- a/test/flatc_compat/CMakeLists.txt +++ b/test/flatc_compat/CMakeLists.txt @@ -1,13 +1,15 @@ set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") -include_directories("${GEN_DIR}") - -flatcc_generate_sources(ALL OUTPUT_DIR "${GEN_DIR}" - DEFINITION_FILES "${FBS_DIR}/monster_test.fbs") +flatcc_generate_sources( + NAME flatc_compat + SCHEMA_FILES "${FBS_DIR}/monster_test.fbs" + ALL + OUTPUT_DIR "${GEN_DIR}" +) -add_executable(flatc_compat flatc_compat.c "${GEN_DIR}/monster_test_reader.h" "${GEN_DIR}/monster_test_verifier.h") -target_link_libraries(flatc_compat flatcc::runtime) +add_executable(flatc_compat flatc_compat.c) +target_link_libraries(flatc_compat PRIVATE flatcc::runtime flatcc_generated::flatc_compat) add_custom_command ( TARGET flatc_compat POST_BUILD diff --git a/test/install_test/CMakeLists.txt b/test/install_test/CMakeLists.txt new file mode 100644 index 000000000..f7ec72474 --- /dev/null +++ b/test/install_test/CMakeLists.txt @@ -0,0 +1,41 @@ +# This file tests the installation of flatbuffers. It is not meant to be used as a subfolder of test. + +cmake_minimum_required(VERSION 3.3) +project(flatcc_install_test) + +# Fake the base of the project to be the root of flatcc +set(CMAKE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../..") +set(PROJECT_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../..") + +enable_testing() + +find_package(flatcc REQUIRED) + +if(NOT DEFINED FLATCC_RTONLY) + message(FATAL_ERROR "Variable FLATCC_RTONLY not defined") +endif() + +if(NOT TARGET flatcc::runtime) + message(FATAL_ERROR "flatcc::runtime target not available") +endif() + +if(NOT TARGET flatcc::cli_native) + message(FATAL_ERROR "flatcc::cli_native target not available") +endif() + +if(NOT FLATCC_RTONLY) + if(NOT TARGET flatcc::cli) + message(FATAL_ERROR "flatcc::cli target not available") + endif() + + if(NOT TARGET flatcc::compiler) + message(FATAL_ERROR "flatcc::compiler target not available") + endif() +endif() + +include(FlatccGenerateSources) + +# re-use flatcc monster_test and reflection_test + +add_subdirectory("${PROJECT_SOURCE_DIR}/test/monster_test" monster_test) +add_subdirectory("${PROJECT_SOURCE_DIR}/test/reflection_test" reflection_test) diff --git a/test/json_test/CMakeLists.txt b/test/json_test/CMakeLists.txt index e53e4a517..ae1f9368e 100644 --- a/test/json_test/CMakeLists.txt +++ b/test/json_test/CMakeLists.txt @@ -4,31 +4,30 @@ set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") set(DATA_DST "${CMAKE_CURRENT_BINARY_DIR}") set(DATA_SRC "${PROJECT_SOURCE_DIR}/test/flatc_compat") -include_directories("${GEN_DIR}") - -flatcc_generate_sources(ALL VERIFIER JSON OUTPUT_DIR "${GEN_DIR}" TARGET gen_json_test - DEFINITION_FILES "${FBS_DIR}/monster_test.fbs") +flatcc_generate_sources( + NAME json_test + SCHEMA_FILES "${FBS_DIR}/monster_test.fbs" + ALL VERIFIER JSON + OUTPUT_DIR "${GEN_DIR}" +) -add_custom_command (TARGET gen_json_test POST_BUILD - COMMAND cmake -E copy "${DATA_SRC}/monsterdata_test.golden" "${DATA_DST}" - COMMAND cmake -E copy "${DATA_SRC}/monsterdata_test.mon" "${DATA_DST}" +add_custom_command (OUTPUT "${DATA_DST}/monsterdata_test.golden" "${DATA_DST}/monsterdata_test.mon" + COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${DATA_SRC}/monsterdata_test.golden" "${DATA_DST}" + COMMAND "${CMAKE_COMMAND}" -E copy_if_different "${DATA_SRC}/monsterdata_test.mon" "${DATA_DST}" DEPENDS "${DATA_SRC}/monsterdata_test.golden" "${DATA_SRC}/monsterdata_test.mon" ) +add_custom_target(copy_json_test_inputs ALL + DEPENDS "${DATA_DST}/monsterdata_test.golden" "${DATA_DST}/monsterdata_test.mon") add_executable(test_basic_parse test_basic_parse.c) add_executable(test_json_parser test_json_parser.c) add_executable(test_json_printer test_json_printer.c) add_executable(test_json test_json.c) -add_dependencies(test_basic_parse gen_json_test) -add_dependencies(test_json_parser gen_json_test) -add_dependencies(test_json_printer gen_json_test) -add_dependencies(test_json gen_json_test) - -target_link_libraries(test_basic_parse PRIVATE flatcc::runtime) -target_link_libraries(test_json_parser PRIVATE flatcc::runtime) -target_link_libraries(test_json_printer PRIVATE flatcc::runtime) -target_link_libraries(test_json PRIVATE flatcc::runtime) +target_link_libraries(test_basic_parse PRIVATE flatcc::runtime flatcc_generated::json_test ) +target_link_libraries(test_json_parser PRIVATE flatcc::runtime flatcc_generated::json_test ) +target_link_libraries(test_json_printer PRIVATE flatcc::runtime flatcc_generated::json_test ) +target_link_libraries(test_json PRIVATE flatcc::runtime flatcc_generated::json_test ) add_test(NAME test_basic_parse COMMAND test_basic_parse) add_test(NAME test_json_parser COMMAND test_json_parser) @@ -48,7 +47,7 @@ set(RTSRC macro(jstest trg flags) add_executable(${trg} test_json.c ${RTSRC}) - add_dependencies(${trg} gen_json_test) + target_link_libraries(${trg} PRIVATE flatcc_generated::json_test) target_include_directories(${trg} PRIVATE ${PROJECT_SOURCE_DIR}/include) add_test(NAME ${trg} COMMAND ${trg}) set_target_properties(${trg} PROPERTIES COMPILE_FLAGS ${flags}) diff --git a/test/load_test/CMakeLists.txt b/test/load_test/CMakeLists.txt index 4174dbc71..a86317a36 100644 --- a/test/load_test/CMakeLists.txt +++ b/test/load_test/CMakeLists.txt @@ -1,13 +1,15 @@ set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") -include_directories("${GEN_DIR}") - -flatcc_generate_sources(ALL OUTPUT_DIR "${GEN_DIR}" - DEFINITION_FILES "${FBS_DIR}/monster_test.fbs") - -add_executable(load_test load_test.c "${GEN_DIR}/monster_test_builder.h") -target_link_libraries(load_test flatcc::runtime) +flatcc_generate_sources( + NAME load_test + SCHEMA_FILES "${FBS_DIR}/monster_test.fbs" + ALL + OUTPUT_DIR "${GEN_DIR}" +) + +add_executable(load_test load_test.c) +target_link_libraries(load_test PRIVATE flatcc::runtime flatcc_generated::load_test) add_test(NAME load_test COMMAND load_test) diff --git a/test/monster_test/CMakeLists.txt b/test/monster_test/CMakeLists.txt index b9cf67f0a..577d11fca 100644 --- a/test/monster_test/CMakeLists.txt +++ b/test/monster_test/CMakeLists.txt @@ -1,12 +1,14 @@ set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") -include_directories("${GEN_DIR}") - -flatcc_generate_sources(ALL OUTPUT_DIR "${GEN_DIR}" - DEFINITION_FILES "${FBS_DIR}/monster_test.fbs") - -add_executable(monster_test monster_test.c "${GEN_DIR}/monster_test_builder.h" "${GEN_DIR}/monster_test_verifier.h") -target_link_libraries(monster_test flatcc::runtime) +flatcc_generate_sources( + NAME monster_test + SCHEMA_FILES "${FBS_DIR}/monster_test.fbs" + ALL + OUTPUT_DIR "${GEN_DIR}" +) + +add_executable(monster_test monster_test.c) +target_link_libraries(monster_test PRIVATE flatcc::runtime flatcc_generated::monster_test) add_test(NAME monster_test COMMAND monster_test) diff --git a/test/monster_test_concat/CMakeLists.txt b/test/monster_test_concat/CMakeLists.txt index 637bcbe05..82e8581c6 100644 --- a/test/monster_test_concat/CMakeLists.txt +++ b/test/monster_test_concat/CMakeLists.txt @@ -1,14 +1,15 @@ set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") -include_directories("${GEN_DIR}") - -flatcc_generate_sources(ALL +flatcc_generate_sources( + NAME monster_test_concat + SCHEMA_FILES "${FBS_DIR}/monster_test.fbs" + ALL OUTPUT_DIR "${GEN_DIR}" OUTFILE "monster_test.h" - DEFINITION_FILES "${FBS_DIR}/monster_test.fbs") +) -add_executable(monster_test_concat monster_test_concat.c "${GEN_DIR}/monster_test.h") -target_link_libraries(monster_test_concat flatcc::runtime) +add_executable(monster_test_concat monster_test_concat.c) +target_link_libraries(monster_test_concat PRIVATE flatcc::runtime flatcc_generated::monster_test_concat) add_test(NAME monster_test_concat COMMAND monster_test_concat) diff --git a/test/monster_test_cpp/CMakeLists.txt b/test/monster_test_cpp/CMakeLists.txt index 2dc017742..6bf5d3c03 100644 --- a/test/monster_test_cpp/CMakeLists.txt +++ b/test/monster_test_cpp/CMakeLists.txt @@ -4,14 +4,15 @@ set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated/monster_test_cpp") set(FBS_DIR "${PROJECT_SOURCE_DIR}/samples/monster") -include_directories("${GEN_DIR}") - -flatcc_generate_sources(ALL OUTPUT_DIR "${GEN_DIR}" - DEFINITION_FILES "${FBS_DIR}/monster.fbs" +flatcc_generate_sources( + NAME monster_test_cpp + SCHEMA_FILES "${FBS_DIR}/monster.fbs" + ALL + OUTPUT_DIR "${GEN_DIR}" ) -add_executable(monster_test_cpp monster_test.cpp "${GEN_DIR}/monster_builder.h") -target_link_libraries(monster_test_cpp flatcc::runtime) +add_executable(monster_test_cpp monster_test.cpp) +target_link_libraries(monster_test_cpp PRIVATE flatcc::runtime flatcc_generated::monster_test_cpp) add_test(NAME monster_test_cpp COMMAND monster_test_cpp) diff --git a/test/monster_test_prefix/CMakeLists.txt b/test/monster_test_prefix/CMakeLists.txt index 8a0bf1d6a..41f4ef7b9 100644 --- a/test/monster_test_prefix/CMakeLists.txt +++ b/test/monster_test_prefix/CMakeLists.txt @@ -1,14 +1,16 @@ set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") -include_directories("${GEN_DIR}") - -flatcc_generate_sources(ALL PREFIX zzz_ +flatcc_generate_sources( + NAME monster_test_prefix + SCHEMA_FILES "${FBS_DIR}/monster_test.fbs" + ALL + PREFIX zzz_ OUTPUT_DIR "${GEN_DIR}" OUTFILE "zzz_monster_test.h" - DEFINITION_FILES "${FBS_DIR}/monster_test.fbs") +) -add_executable(monster_test_prefix monster_test_prefix.c "${GEN_DIR}/zzz_monster_test.h") -target_link_libraries(monster_test_prefix flatcc::runtime) +add_executable(monster_test_prefix monster_test_prefix.c) +target_link_libraries(monster_test_prefix PRIVATE flatcc::runtime flatcc_generated::monster_test_prefix) add_test(NAME monster_test_prefix COMMAND monster_test_prefix) diff --git a/test/monster_test_solo/CMakeLists.txt b/test/monster_test_solo/CMakeLists.txt index 7633637d4..9a8b2c447 100644 --- a/test/monster_test_solo/CMakeLists.txt +++ b/test/monster_test_solo/CMakeLists.txt @@ -1,18 +1,19 @@ set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") -include_directories("${GEN_DIR}") +# Don't use flatcc_generate_sources to test -stdout add_custom_command ( OUTPUT "${GEN_DIR}/monster_test.h" COMMAND cmake -E make_directory "${GEN_DIR}" - COMMAND flatcc::cli -cwv --reader --stdout "${FBS_DIR}/attributes.fbs" "${FBS_DIR}/include_test2.fbs" + COMMAND flatcc::cli_native -cwv --reader --stdout "${FBS_DIR}/attributes.fbs" "${FBS_DIR}/include_test2.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/monster_test.fbs" > "${GEN_DIR}/monster_test.h" - DEPENDS flatcc::cli "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" + DEPENDS flatcc::cli_native "${FBS_DIR}/monster_test.fbs" "${FBS_DIR}/include_test1.fbs" "${FBS_DIR}/include_test2.fbs" "${FBS_DIR}/attributes.fbs" ) add_executable(monster_test_solo monster_test_solo.c "${GEN_DIR}/monster_test.h") +target_include_directories(monster_test_solo PRIVATE "${GEN_DIR}") target_link_libraries(monster_test_solo flatcc::runtime) add_test(NAME monster_test_solo COMMAND monster_test_solo) diff --git a/test/optional_scalars_test/CMakeLists.txt b/test/optional_scalars_test/CMakeLists.txt index 7b95d78b9..fc3e69fb8 100644 --- a/test/optional_scalars_test/CMakeLists.txt +++ b/test/optional_scalars_test/CMakeLists.txt @@ -1,14 +1,15 @@ set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/optional_scalars_test") -include_directories("${GEN_DIR}") - -flatcc_generate_sources(ALL JSON OUTPUT_DIR "${GEN_DIR}" - DEFINITION_FILES "${FBS_DIR}/optional_scalars_test.fbs" +flatcc_generate_sources( + NAME optional_scalars_test + SCHEMA_FILES "${FBS_DIR}/optional_scalars_test.fbs" + ALL + JSON + OUTPUT_DIR "${GEN_DIR}" ) -add_executable(optional_scalars_test optional_scalars_test.c "${GEN_DIR}/optional_scalars_test_builder.h" - "${GEN_DIR}/optional_scalars_test_json_printer.h" "${GEN_DIR}/optional_scalars_test_json_parser.h") -target_link_libraries(optional_scalars_test flatcc::runtime) +add_executable(optional_scalars_test optional_scalars_test.c) +target_link_libraries(optional_scalars_test PRIVATE flatcc::runtime flatcc_generated::optional_scalars_test) add_test(NAME optional_scalars_test COMMAND optional_scalars_test) diff --git a/test/reflection_test/CMakeLists.txt b/test/reflection_test/CMakeLists.txt index d115dc82f..0ef810f1d 100644 --- a/test/reflection_test/CMakeLists.txt +++ b/test/reflection_test/CMakeLists.txt @@ -1,10 +1,14 @@ set(GEN_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") set(FBS_DIR "${PROJECT_SOURCE_DIR}/test/monster_test") -flatcc_generate_sources(SCHEMA OUTPUT_DIR "${GEN_DIR}" - DEFINITION_FILES "${FBS_DIR}/monster_test.fbs") +flatcc_generate_sources( + NAME reflection_test + SCHEMA_FILES "${FBS_DIR}/monster_test.fbs" + BINARY_SCHEMA + OUTPUT_DIR "${GEN_DIR}" +) -add_executable(reflection_test reflection_test.c "${GEN_DIR}/monster_test.bfbs") -target_link_libraries(reflection_test flatcc::runtime) +add_executable(reflection_test reflection_test.c) +target_link_libraries(reflection_test PRIVATE flatcc::runtime flatcc_generated::reflection_test) add_test(NAME reflection_test COMMAND reflection_test)