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 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/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 000000000..b3e7cd03d --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,183 @@ +on: + push: + branches-ignore: + - 'ci-more' + pull_request: +jobs: + test_unix: + name: 'Test Unix with CMake' + strategy: + matrix: + 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' + 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 .. -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 + - name: 'CTest' + run: | + cd build + ctest -V -C ${{ matrix.config }} + - 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: + 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=${{ github.workspace }}/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 }} + - 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 . 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 40dee3b3c..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,16 +12,20 @@ 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) endif() +# Make the cmake scripts inside the cmake subfolder available +list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake") + # # NOTE: when changing build options, clean the build using on of: # @@ -107,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() @@ -151,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 @@ -163,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") @@ -191,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") @@ -274,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") @@ -290,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() @@ -298,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") @@ -306,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. @@ -325,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() @@ -337,14 +359,29 @@ 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) -endif() + 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}") + 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 + cmake/FindFlatCCNative.cmake + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/flatcc") +endif() diff --git a/README.md b/README.md index 4308350a4..3b5bd7519 100644 --- a/README.md +++ b/README.md @@ -34,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) @@ -154,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] @@ -408,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 @@ -755,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. @@ -800,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 @@ -830,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. @@ -1354,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 @@ -2267,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: @@ -2276,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 new file mode 100644 index 000000000..06d48ab21 --- /dev/null +++ b/cmake/FlatccGenerateSources.cmake @@ -0,0 +1,340 @@ +# 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( +# 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 [ [...]]] +# ) +# +# 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. +# + +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 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 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 (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_ARGS -o "${FLATCC_OUTPUT_DIR}") + + # Add current source directory for finding dependencies + set(absolute_flatcc_paths "${CMAKE_CURRENT_SOURCE_DIR}") + + set(ABSOLUTE_SCHEMA_FILES) + foreach(schema_file ${FLATCC_SCHEMA_FILES}) + # TODO: file(REAL_PATH) if cmake_minimum_required_version >= 3.19 + if(IS_ABSOLUTE "${schema_file}") + set(absolute_def_file "${schema_file}") + else() + set(absolute_def_file "${CMAKE_CURRENT_SOURCE_DIR}/${schema_file}") + endif() + 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}") + 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_ARGS -I "${path}") + endforeach() + list(REMOVE_DUPLICATES absolute_flatcc_paths) + + # 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() + + # Add flatcc options + + if(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_ARGS --reader) + endif() + if (FLATCC_COMMON_READER) + list(APPEND FLATCC_ARGS --common_reader) + endif() + if (FLATCC_COMMON_BUILDER) + list(APPEND FLATCC_ARGS --common_builder) + endif() + if (FLATCC_BUILDER) + list(APPEND FLATCC_ARGS --builder) + # Builder also generates reader + set(FLATCC_READER ON) + endif() + if (FLATCC_VERIFIER) + list(APPEND FLATCC_ARGS --verifier) + # verifier also generates reader + set(FLATCC_READER ON) + endif() + if (FLATCC_JSON_PARSER) + list(APPEND FLATCC_ARGS --json-parser) + endif() + if (FLATCC_JSON_PRINTER) + list(APPEND FLATCC_ARGS --json-printer) + endif() + if (FLATCC_BINARY_SCHEMA) + list(APPEND FLATCC_ARGS --schema) + endif() + if (FLATCC_RECURSIVE) + list(APPEND FLATCC_ARGS --recursive) + endif() + + # handle 'all', 'common', 'json' last as they encompass other options + if (FLATCC_COMMON) + list(APPEND FLATCC_ARGS --common) + set(FLATCC_COMMON_READER ON) + set(FLATCC_COMMON_BUILDER ON) + endif() + if (FLATCC_JSON) + list(APPEND FLATCC_ARGS --json) + set(FLATCC_JSON_PARSER ON) + set(FLATCC_JSON_PRINTER ON) + endif() + if (FLATCC_ALL) + 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) + 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_BINARY_SCHEMA) + list(APPEND GENERATED_FILE_SUFFIXES .bfbs) + endif() + + # grep each schema file recursively for includes, convert them to absolute paths and add them to a list + set(ABSOLUTE_DEFINITIONS_DEPENDENCIES) + 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}") + 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_schema_files_todo "${abs_include_def_file}") + endif() + endforeach() + endif() + list(REMOVE_AT absolute_schema_files_todo 0) + endwhile() + + list(REMOVE_DUPLICATES ABSOLUTE_DEFINITIONS_DEPENDENCIES) + list(REMOVE_DUPLICATES ABSOLUTE_SCHEMA_FILES) + + set(OUTPUT_FILES) + if(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(SCHEMA_FILES ${ABSOLUTE_DEFINITIONS_DEPENDENCIES}) + else() + set(SCHEMA_FILES ${ABSOLUTE_SCHEMA_FILES}) + endif() + 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() + 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() + + 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_native ${FLATCC_ARGS} ${ABSOLUTE_SCHEMA_FILES} + DEPENDS flatcc::cli_native ${ABSOLUTE_DEFINITIONS_DEPENDENCIES} + ) + + 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 new file mode 100644 index 000000000..b32c12176 --- /dev/null +++ b/cmake/flatcc-config.cmake.in @@ -0,0 +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/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/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 a14e10990..d2c6fa5b6 100644 --- a/samples/monster/CMakeLists.txt +++ b/samples/monster/CMakeLists.txt @@ -1,22 +1,15 @@ -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}") +# Don't use FBS_DIR to test whether flatcc_generate_sources knows to use the correct file. -add_custom_target(gen_monster_fbs ALL) -add_custom_command ( - TARGET gen_monster_fbs - 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( + NAME monster_sample + SCHEMA_FILES monster.fbs + ALL + OUTPUT_DIR "${GEN_DIR}" ) + add_executable(monster monster.c) -add_dependencies(monster gen_monster_fbs) -target_link_libraries(monster flatccrt) +target_link_libraries(monster PRIVATE flatcc::runtime flatcc_generated::monster_sample) -if (FLATCC_TEST) - add_test(monster monster${CMAKE_EXECUTABLE_SUFFIX}) -endif() +add_test(NAME monster COMMAND monster) diff --git a/samples/reflection/CMakeLists.txt b/samples/reflection/CMakeLists.txt index db5b75445..fea42fd81 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 @@ -9,23 +7,17 @@ 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}") - -add_custom_target(gen_monster_bfbs ALL) -add_custom_command ( - TARGET gen_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" +flatcc_generate_sources( + NAME reflection_sample + SCHEMA_FILES "${FBS_DIR}/monster.fbs" + BINARY_SCHEMA + OUTPUT_DIR "${GEN_DIR}" ) + add_executable(bfbs2json bfbs2json.c) -add_dependencies(bfbs2json gen_monster_bfbs) -target_link_libraries(bfbs2json flatccrt) +target_link_libraries(bfbs2json PRIVATE flatcc::runtime flatcc_generated::reflection_sample) -if (FLATCC_TEST) - add_test(bfbs2json bfbs2json${CMAKE_EXECUTABLE_SUFFIX} ${GEN_DIR}/monster.bfbs) -endif() +add_test(NAME bfbs2json COMMAND bfbs2json "${GEN_DIR}/monster.bfbs") 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 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" diff --git a/src/cli/CMakeLists.txt b/src/cli/CMakeLists.txt index 40facacbb..87175c6b4 100644 --- a/src/cli/CMakeLists.txt +++ b/src/cli/CMakeLists.txt @@ -1,20 +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) +set_target_properties(flatcc_cli PROPERTIES + 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 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 ce31819d9..d71cdf066 100644 --- a/src/compiler/CMakeLists.txt +++ b/src/compiler/CMakeLists.txt @@ -1,9 +1,3 @@ -include_directories ( - "${PROJECT_SOURCE_DIR}/external" - "${PROJECT_SOURCE_DIR}/include" - "${PROJECT_SOURCE_DIR}/config" -) - set (SOURCES ${PROJECT_SOURCE_DIR}/external/hash/cmetrohash64.c ${PROJECT_SOURCE_DIR}/external/hash/str_set.c @@ -33,11 +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::compiler ALIAS flatcc) + +target_include_directories(flatcc + PUBLIC + "$" + $ + PRIVATE + "${PROJECT_SOURCE_DIR}/external" + "${PROJECT_SOURCE_DIR}/config" +) + +set_target_properties(flatcc PROPERTIES EXPORT_NAME compiler) if (FLATCC_INSTALL) - install(TARGETS flatcc 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 127e2a47f..6aab059a9 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 @@ -10,7 +6,17 @@ 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 + 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 2edc040c6..be8b08eb6 100644 --- a/test/cgen_test/CMakeLists.txt +++ b/test/cgen_test/CMakeLists.txt @@ -1,43 +1,47 @@ -include(CTest) - -include_directories ( - "${PROJECT_SOURCE_DIR}/include" -) - add_executable(cgen_test cgen_test.c ) target_link_libraries(cgen_test - flatcc + flatcc::compiler ) -add_test(cgen_test cgen_test${CMAKE_EXECUTABLE_SUFFIX}) +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 -separate_arguments(CUSTOM_C_FLAGS UNIX_COMMAND "${CMAKE_C_FLAGS}") +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) -add_custom_target(test_generated - COMMAND ./cgen_test${CMAKE_EXECUTABLE_SUFFIX} > 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 + # 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 ) -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(test_generated ${CMAKE_MAKE_PROGRAM} test_generated) -endif(${CMAKE_MAKE_PROGRAM} MATCHES make) + + 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() 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 aac5fb790..6d7760fa0 100644 --- a/test/emit_test/CMakeLists.txt +++ b/test/emit_test/CMakeLists.txt @@ -1,20 +1,14 @@ -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}") - -add_custom_target(gen_emit_test ALL) -add_custom_command ( - TARGET gen_emit_test - 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( + NAME emit_test + SCHEMA_FILES "${FBS_DIR}/emit_test.fbs" + ALL + OUTPUT_DIR "${GEN_DIR}" ) + add_executable(emit_test emit_test.c) -add_dependencies(emit_test gen_emit_test) -target_link_libraries(emit_test flatccrt) +target_link_libraries(emit_test PRIVATE flatcc::runtime flatcc_generated::emit_test) -add_test(emit_test emit_test${CMAKE_EXECUTABLE_SUFFIX}) +add_test(NAME emit_test COMMAND emit_test) diff --git a/test/flatc_compat/CMakeLists.txt b/test/flatc_compat/CMakeLists.txt index 6a9a4a3f6..fdf1e8aaf 100644 --- a/test/flatc_compat/CMakeLists.txt +++ b/test/flatc_compat/CMakeLists.txt @@ -1,21 +1,19 @@ -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}") +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) +target_link_libraries(flatc_compat PRIVATE flatcc::runtime flatcc_generated::flatc_compat) -add_custom_target(gen_flatc_compat ALL) add_custom_command ( - TARGET gen_flatc_compat - 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" + TARGET flatc_compat POST_BUILD + COMMAND "${CMAKE_COMMAND}" -E copy "${CMAKE_CURRENT_SOURCE_DIR}/monsterdata_test.mon" "${CMAKE_CURRENT_BINARY_DIR}" ) -add_executable(flatc_compat flatc_compat.c) -add_dependencies(flatc_compat gen_flatc_compat) -target_link_libraries(flatc_compat flatccrt) -add_test(flatc_compat flatc_compat${CMAKE_EXECUTABLE_SUFFIX}) +add_test(NAME flatc_compat COMMAND flatc_compat) 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 fec6c7c1a..ae1f9368e 100644 --- a/test/json_test/CMakeLists.txt +++ b/test/json_test/CMakeLists.txt @@ -1,43 +1,38 @@ -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}") +flatcc_generate_sources( + NAME json_test + SCHEMA_FILES "${FBS_DIR}/monster_test.fbs" + ALL VERIFIER JSON + OUTPUT_DIR "${GEN_DIR}" +) -add_custom_target(gen_monster_test_json ALL) -add_custom_command ( - TARGET gen_monster_test_json - 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" +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_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) - -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 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(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(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") @@ -52,8 +47,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}) + 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}) endmacro() diff --git a/test/load_test/CMakeLists.txt b/test/load_test/CMakeLists.txt index 6d444a547..a86317a36 100644 --- a/test/load_test/CMakeLists.txt +++ b/test/load_test/CMakeLists.txt @@ -1,20 +1,15 @@ -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}") - -add_custom_target(gen_load_test ALL) -add_custom_command ( - TARGET gen_load_test - 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" +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) -add_dependencies(load_test gen_load_test) -target_link_libraries(load_test flatccrt) +target_link_libraries(load_test PRIVATE flatcc::runtime flatcc_generated::load_test) + +add_test(NAME load_test COMMAND load_test) -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..577d11fca 100644 --- a/test/monster_test/CMakeLists.txt +++ b/test/monster_test/CMakeLists.txt @@ -1,20 +1,14 @@ -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}") - -add_custom_target(gen_monster_test ALL) -add_custom_command ( - TARGET gen_monster_test - 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" +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) -add_dependencies(monster_test gen_monster_test) -target_link_libraries(monster_test flatccrt) +target_link_libraries(monster_test PRIVATE flatcc::runtime flatcc_generated::monster_test) -add_test(monster_test monster_test${CMAKE_EXECUTABLE_SUFFIX}) +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 836ae09e1..82e8581c6 100644 --- a/test/monster_test_concat/CMakeLists.txt +++ b/test/monster_test_concat/CMakeLists.txt @@ -1,21 +1,15 @@ -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}") - -add_custom_target(gen_monster_test_concat ALL) -add_custom_command ( - TARGET gen_monster_test_concat - 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( + NAME monster_test_concat + SCHEMA_FILES "${FBS_DIR}/monster_test.fbs" + ALL + OUTPUT_DIR "${GEN_DIR}" + OUTFILE "monster_test.h" ) -include_directories("${GEN_DIR}" "${INC_DIR}") + add_executable(monster_test_concat monster_test_concat.c) -add_dependencies(monster_test_concat gen_monster_test_concat) -target_link_libraries(monster_test_concat flatccrt) +target_link_libraries(monster_test_concat PRIVATE flatcc::runtime flatcc_generated::monster_test_concat) -add_test(monster_test_concat monster_test_concat${CMAKE_EXECUTABLE_SUFFIX}) +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 22247a394..6bf5d3c03 100644 --- a/test/monster_test_cpp/CMakeLists.txt +++ b/test/monster_test_cpp/CMakeLists.txt @@ -1,24 +1,18 @@ -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}") - -add_custom_target(gen_monster_test_cpp ALL) -add_custom_command ( - TARGET gen_monster_test_cpp - 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( + NAME monster_test_cpp + SCHEMA_FILES "${FBS_DIR}/monster.fbs" + ALL + OUTPUT_DIR "${GEN_DIR}" ) add_executable(monster_test_cpp monster_test.cpp) -add_dependencies(monster_test_cpp gen_monster_test_cpp) -target_link_libraries(monster_test_cpp flatccrt) +target_link_libraries(monster_test_cpp PRIVATE flatcc::runtime flatcc_generated::monster_test_cpp) + +add_test(NAME monster_test_cpp COMMAND monster_test_cpp) -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..41f4ef7b9 100644 --- a/test/monster_test_prefix/CMakeLists.txt +++ b/test/monster_test_prefix/CMakeLists.txt @@ -1,20 +1,16 @@ -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}") - -add_custom_target(gen_monster_test_prefix ALL) -add_custom_command ( - TARGET gen_monster_test_prefix - 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( + NAME monster_test_prefix + SCHEMA_FILES "${FBS_DIR}/monster_test.fbs" + ALL + PREFIX zzz_ + OUTPUT_DIR "${GEN_DIR}" + OUTFILE "zzz_monster_test.h" ) + add_executable(monster_test_prefix monster_test_prefix.c) -add_dependencies(monster_test_prefix gen_monster_test_prefix) -target_link_libraries(monster_test_prefix flatccrt) +target_link_libraries(monster_test_prefix PRIVATE flatcc::runtime flatcc_generated::monster_test_prefix) -add_test(monster_test_prefix monster_test_prefix${CMAKE_EXECUTABLE_SUFFIX}) +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 b6294b85f..9a8b2c447 100644 --- a/test/monster_test_solo/CMakeLists.txt +++ b/test/monster_test_solo/CMakeLists.txt @@ -1,21 +1,19 @@ -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}") +# Don't use flatcc_generate_sources to test -stdout -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" + 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_native "${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) -target_link_libraries(monster_test_solo flatccrt) +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(monster_test_solo monster_test_solo${CMAKE_EXECUTABLE_SUFFIX}) +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 61a00c71d..fc3e69fb8 100644 --- a/test/optional_scalars_test/CMakeLists.txt +++ b/test/optional_scalars_test/CMakeLists.txt @@ -1,19 +1,15 @@ -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}") - -add_custom_target(gen_optional_scalars_test ALL) -add_custom_command ( - TARGET gen_optional_scalars_test - COMMAND cmake -E make_directory "${GEN_DIR}" - COMMAND flatcc_cli -a --json -o "${GEN_DIR}" "${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) -add_dependencies(optional_scalars_test gen_optional_scalars_test) -target_link_libraries(optional_scalars_test flatccrt) +target_link_libraries(optional_scalars_test PRIVATE flatcc::runtime flatcc_generated::optional_scalars_test) -add_test(optional_scalars_test optional_scalars_test${CMAKE_EXECUTABLE_SUFFIX}) +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 fa1e4b3f5..0ef810f1d 100644 --- a/test/reflection_test/CMakeLists.txt +++ b/test/reflection_test/CMakeLists.txt @@ -1,20 +1,14 @@ -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}") - -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" +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) -add_dependencies(reflection_test gen_reflection_test) -target_link_libraries(reflection_test flatccrt) +target_link_libraries(reflection_test PRIVATE flatcc::runtime flatcc_generated::reflection_test) -add_test(reflection_test reflection_test${CMAKE_EXECUTABLE_SUFFIX}) +add_test(NAME reflection_test COMMAND reflection_test)