Skip to content
88 changes: 88 additions & 0 deletions recipes/flatcc/all/FlatccGenerateSources.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you please point me to the cmake script from the flatbuffers repo at https://github.com/dvidelabs/flatcc
containing these functions/macros?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I wrote that cmake script because it simplifies using flatcc in cmake a lot. I am going to send it upstream as well.
Do you want me to add it to the flatcc package via the patches mechanism?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Edit: PR for flatcc cmake module submitted: dvidelabs/flatcc#169

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please do it as a patch, so when your pr gets accepted, this recipe can be adapted more easily.
In the mean time, maybe add a FIXME/TODO that this is experimental conan-only code?

# 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}
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 in current install location
find_program(FLATCC_COMPILER flatcc
NO_SYSTEM_ENVIRONMENT_PATH
NO_CMAKE_SYSTEM_PATH
)
endif()


if (NOT FLATCC_COMPILER)
message(FATAL_ERROR "Could not locate the flatcc compiler executable")
endif()
25 changes: 18 additions & 7 deletions recipes/flatcc/all/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class FlatccConan(ConanFile):
}
settings = "os", "arch", "compiler", "build_type"
generators = "cmake"
exports_sources = ["CMakeLists.txt"]
exports_sources = ["CMakeLists.txt", "FlatccGenerateSources.cmake"]

_cmake = None

Expand Down Expand Up @@ -89,7 +89,6 @@ def build(self):
cmake = self._configure_cmake()
cmake.build()


def package(self):
cmake = self._configure_cmake()
cmake.install()
Expand All @@ -99,12 +98,24 @@ def package(self):
os.path.join(self.package_folder, "bin", "flatcc"))
# Copy license file
self.copy("LICENSE", dst="licenses", src=self._source_subfolder)
# Copy file with cmake functions for end user
self.copy("FlatccGenerateSources.cmake", dst="cmake")

def package_info(self):
bin_path = os.path.join(self.package_folder, "bin")
self.output.info('Appending PATH environment variable: %s' % bin_path)
self.env_info.PATH.append(bin_path)
debug_suffix = "_d" if self.settings.build_type == "Debug" else ""
#flatcc package provides two components: the flatcc compiler binary and the runtime library
if not self.options.runtime_lib_only:
self.cpp_info.libs.append("flatcc%s" % debug_suffix)
self.cpp_info.libs.append("flatccrt%s" % debug_suffix)
self.cpp_info.components["flatcc_exe"].names["cmake_find_package"] = "flatcc_exe"
self.cpp_info.components["flatcc_exe"].libs = ["flatcc%s" % debug_suffix]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If flatcc_exe is the compiler binary, shouldn't .libs be empty then?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, I will add an extra component for the library that the compiler binary links to.

#Our FlatccGenerateSources.cmake should be included by the cmake_find_package generated file
self.cpp_info.components["flatcc_exe"].build_modules.append(os.path.join(self.package_folder, "cmake", "FlatccGenerateSources.cmake"))
bin_path = os.path.join(self.package_folder, "bin")
self.env_info.PATH.append(bin_path)
#When we are cross-compiling cmake needs to know the location of the flatbuffer compiler executable
#compiled for the build architecture. Provide it via environment variable.
settings_target = getattr(self, 'settings_target', None)
if settings_target != None:
self.env_info.FLATCC_BUILD_BIN_PATH = os.path.join(self.package_folder, "bin")
self.cpp_info.components["flatcc_rt"].names["cmake_find_package"] = "flatcc_rt"
self.cpp_info.components["flatcc_rt"].libs = ["flatccrt%s" % debug_suffix]
self.cpp_info.components["flatcc_rt"].includedirs.append(os.path.join("include", "flatcc", "reflection"))