Skip to content
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

#168: add cmake helper function to generate flatbuffer code #169

Closed
wants to merge 1 commit into from

Conversation

wdobbe
Copy link

@wdobbe wdobbe commented Dec 16, 2020

Resolves #168 .

Install a cmake module that provides cmake function flatcc_generate_sources.
This function simplifies generation of C code from flatbuffer definition file(s).

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}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs DEPENDS for dependency tracking of the input flatbuffer files.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link

Choose a reason for hiding this comment

The 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?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The add_custom_command documentation does mention ninja with multiple arguments, but not with DEPENDS btw.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link

Choose a reason for hiding this comment

The 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 flatcc-config.cmake.

This allows your users to use flatcc by doing find_package(flatcc REQUIRED) and call flatcc using flatcc::flatcc.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like on every make, all tests are re-built.
This is because the dependency tree is not correct.
I'll look into that too.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wdobbe
You can then re-use/rebase your FlatccGenerateSources.cmake script on my pr.

Copy link
Contributor

Choose a reason for hiding this comment

The 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:

#24 (comment)

Copy link
Author

Choose a reason for hiding this comment

The 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 flatcc-config.cmake.

This allows your users to use flatcc by doing find_package(flatcc REQUIRED) and call flatcc using flatcc::flatcc.

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.

Comment on lines +69 to +81
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()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's ok to do this with one find_program:

  • it's possible to override by doing -DFLATCC_COMPILER=xxx.
  • Using -DFLATCC_ROOT, you can set a prefix of flatxx
  • Using the FLATCC_ROOT environment variable, you can also set a prefix

I think the options NO_DEFAULT_PATH, NO_SYSTEM_ENVIRONMENT_PATH and NO_CMAKE_SYSTEM_PATH are not needed.

Suggested change
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()
#user provided location where asn1c compiler executable is installed
find_program(FLATCC_COMPILER flatcc
PATHS ${FLATCC_ROOT} ENV FLATCC_ROOT
PATH_SUFFIXES bin
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#user provided location where asn1c compiler executable is installed

asn1c?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, copy/paste error from a similar script for the ASN1C compiler.
I'll correct that.

@@ -0,0 +1,86 @@
cmake_minimum_required(VERSION 3.5)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would split this file in 2:

  • Export the flatcc target as flatcc::flatcc with install(TARGETS) and install(EXPORT).
  • Inside flatcc-config.cmake, do include("${CMAKE_CURRENT_LIST_DIR}/FlatccGenerateSources.cmake") which then only contains the function. Instead of the FLATCC_COMPILER, it can then use the imported flatcc::flatcc executable target.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that would be a better solution.

@mikkelfj
Copy link
Contributor

Can you provide an example within FlatCC's test cases that uses this module?

I'm not convinced I want to accept this PR because I'd like to keep CMake maintenance to a minimum. It is has been rather painful.

Copy link
Contributor

@mikkelfj mikkelfj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding minimum required version, there is reason why flatcc uses cmake_minimum_required (VERSION 2.8)

Otherwise it will not build on some older conservative platforms. If it is really necessary to use a more recent version and it only affects users own programs opting in to this feature then maybe OK, but otherwise it would interfere with portability which is important to FlatCC.


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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useful

Copy link
Author

@wdobbe wdobbe Dec 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding minimum required version, there is reason why flatcc uses cmake_minimum_required (VERSION 2.8)

Otherwise it will not build on some older conservative platforms. If it is really necessary to use a more recent version and it only affects users own programs opting in to this feature then maybe OK, but otherwise it would interfere with portability which is important to FlatCC.

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.

Copy link
Author

@wdobbe wdobbe Dec 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm suggesting to add an example of using this module within the FlatCC project, but perhaps not replacing existing since it could break portability with CMake version. I'm not sure if the module only works after CMake install and the README should probably also clarify how to make it available.

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.

Copy link
Contributor

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.

@mikkelfj
Copy link
Contributor

BTW: any idea why Travis build breaks on MacOS? Is it just Travis that hasn't upgraded its platform properly?

@wdobbe
Copy link
Author

wdobbe commented Dec 16, 2020

Can you provide an example within FlatCC's test cases that uses this module?

This is how I call it in one of our company cmake files:

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
)

I'm not convinced I want to accept this PR because I'd like to keep CMake maintenance to a minimum. It is has been rather painful.

  1. The module is optional, the end-user can choose whether to use it or not.
  2. I don't see the CMake maintenance problem. CMake has always been completely backwards-compatible, so once the function works it shouldn't require any maintenance.

@mikkelfj
Copy link
Contributor

I'm suggesting to add an example of using this module within the FlatCC project, but perhaps not replacing existing since it could break portability with CMake version. I'm not sure if the module only works after CMake install and the README should probably also clarify how to make it available.

@wdobbe
Copy link
Author

wdobbe commented Dec 16, 2020

BTW: any idea why Travis build breaks on MacOS? Is it just Travis that hasn't upgraded its platform properly?

When adding flatcc to conan-center-index we also had problems on MacOS because of a new security feature called Security Integrity Protection (SIP). That could be the case, however I currently don't see build logs for MacOS for this PR? And the only Mac I currently have access to (due to Covid-19 lockdown) is too old to have SIP.

@wdobbe
Copy link
Author

wdobbe commented Dec 16, 2020

I'm suggesting to add an example of using this module within the FlatCC project, but perhaps not replacing existing since it could break portability with CMake version. I'm not sure if the module only works after CMake install and the README should probably also clarify how to make it available.

Ok, I will do that once changes suggested by you and @madebr are implemented.

@mikkelfj
Copy link
Contributor

I'm developing flatcc on Mac Intel with Ninja. Travis has been building without issues before, except that it is often very slow. I think all travis builds use Make. It is unlikely that this PR triggered a build issue, but it is the first time that I see it.
I'm inclined to move everything (Windows, Mac, Linux) to GH Actions, but its not happening right away.

@mikkelfj
Copy link
Contributor

BTW: conan is different because it uses a binary dist, I think. Building from source should only give SIP issues if the core tools are broken (homebrew?, clang, make, cmake).

@wdobbe
Copy link
Author

wdobbe commented Dec 16, 2020

This is the comment I added for the workaround at that time (see here and here):

#On MacOS System Integrity Protection (SIP) will clear the DYLD_LIBRARY_PATH variable.
#As a result calling flatcc from cmake will currently not work if the flatcc executable
# is linked shared. As a workaround we generate the flatbuffer C files in the Conan recipe
# when on MacOS and flatcc option 'shared' is True.

So the issue I talked about only arises when the flatcc executable links dynamically to the flatcc library and flatcc is called from cmake.

@mikkelfj
Copy link
Contributor

DYLD, OK.

Build logs - follow details in the "Some checks were not successful"
https://travis-ci.org/github/dvidelabs/flatcc/jobs/750009859
The problem is homebrew trying to install ninja, it seems.
Nothing that I have changed in years.

@wdobbe
Copy link
Author

wdobbe commented Dec 16, 2020

I'm not too familiar with homebrew (I use conan nowadays) however in the build logs I don't see anything go particularly wrong, apart from the timeout at the end. Maybe the homebrew install was stuck or too slow?

@mikkelfj
Copy link
Contributor

mikkelfj commented Dec 17, 2020

@wdobbe I don't have the full overview here, but it seems that @madebr has already integrated most or all of your changes into his PR #171
If that PR passes review I'd probably squash it into master as a single commit. In that case you might not get listed as a contributor, so you should probably make a commit to that PR. But as I said, I don't have the full overview here.

@madebr
Copy link

madebr commented Dec 17, 2020

I have used @wdobbe 's commit and kept his name, so he should be mentioned as co-creator.

@mikkelfj
Copy link
Contributor

Should we close this PR then?

@mikkelfj
Copy link
Contributor

Sorry for not following up on this. We have several build PRs going and I need to narrow this down. If you are still interested, please comment on PR #258. The plan is to make a release soon, then introduce changes to the build.

@mikkelfj mikkelfj closed this Oct 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add cmake helper function to simplify code generation
3 participants