-
Notifications
You must be signed in to change notification settings - Fork 221
#168: add cmake helper function to generate flatbuffer code #169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,86 @@ | ||||||||||||||||||||||||||||||||||||||
| cmake_minimum_required(VERSION 3.5) | ||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would split this file in 2:
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that would be a better solution. |
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| # Use the following function to generate C source files from flatbuffer definition files: | ||||||||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||||||||
| # flatcc_generate_sources(GENERATED_SOURCE_DIRECTORY <directory where to write source files> | ||||||||||||||||||||||||||||||||||||||
| # GENERATE_BUILDER | ||||||||||||||||||||||||||||||||||||||
| # GENERATE_VERIFIER | ||||||||||||||||||||||||||||||||||||||
| # EXPECTED_OUTPUT_FILES <list of files that flatcc is supposed to generate> | ||||||||||||||||||||||||||||||||||||||
| # DEFINITION_FILES <list of flatbuffer definition files (.fbs)> | ||||||||||||||||||||||||||||||||||||||
| # ) | ||||||||||||||||||||||||||||||||||||||
| # | ||||||||||||||||||||||||||||||||||||||
| # 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} | ||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needs
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without reading carefully and from memory, just adding DEPENDS will break Ninja build because CMake doesn't handle dependecies properly, even if it works with Make. If you look at some of the examples that build with CMAKE, these are rather clumsy because of this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But without DEPENDS, the target sources don't get regenerated when the input sources get modified? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know because I am not a CMake expert. But I do know that I have spent several days trying to make it work (or something similar). Another who believed he could fix it had to admit he couldn't. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm looking into it now, I'll open a pr for installing This allows your users to use flatcc by doing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like on every There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @wdobbe
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, here is an old discussion about broken dependencies - I haven't reread it, but linked here for reference:
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, that is a good idea. I have plenty examples of how to generate the config files (correctly) so if you want me to do it, let me know. |
||||||||||||||||||||||||||||||||||||||
| 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() | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+69
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's ok to do this with one
I think the options
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #user provided location where asn1c compiler executable is installed asn1c?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, copy/paste error from a similar script for the ASN1C compiler. |
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| if (NOT FLATCC_COMPILER) | ||||||||||||||||||||||||||||||||||||||
| message(FATAL_ERROR "Could not locate the flatcc compiler executable") | ||||||||||||||||||||||||||||||||||||||
| endif() | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useful
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Edit: sorry was confused with PRs from Conan.
I had to update the cmake_minimum_required version from 2.8 to 3.1 in another recipe PR today because a conan-center hook rejects the PR if the cmake_minimum_required is not at least 3.1.In this case the minimum cmake version is 3.5 because from that version cmake function cmake_parse_arguments checks for value arguments that are miss their value, so it makes the function more fool-proof.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I will do that.
The only thing to do is that you have to do to make the module work is add the FlatCC cmake subdir to your CMAKE_MODULE_PATH.
Edit: when we also export a flatcc cmake config file as suggested by @madebr then appending CMAKE_MODULE_PATH is no longer necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But if it works the same for correct arguments on earlier versions then an earlier MIN version will still support the check for users with a recent CMake while not breaking the build for those who do not.