This is still not working for me, even though I think I have done things correctly... (set PROPERTIES GENERATED TRUE on the generated files, created a custom *_TARGET, populated a new variable with each file generated into a new variable *_OUTPUTS that gets injected into the PARENT_SCOPE, use that variable to set OBJECT_DEPENDS on the source files in that component that use the generated files)ESP_Angus wrote:Yes, I see. I think CMake makes some internal distinction between "generated" and "not generated" files, so I can see this not working as expected. I think the approach you mention should work....p-rimes wrote: Doesn't seem to be the case for me. I have to run build 4 or 5 times to get all the files generated. As well, I am generating header files back into the component directory...
I still need to re-run the build a few times to get everything generated. Here is an example CMake function (it will generate a `Foobar_generated.h` for each `Foobar.fbs` file in the function args), and another snippet for how I use it:
Code: Select all
function(FLATBUFFERS_GENERATE_GENERATED_H schema_generated_h)
set(GENERATED_OUTPUTS)
foreach(FILE ${ARGN})
get_filename_component(SCHEMA ${FILE} NAME_WE)
set(OUT "${COMPONENT_PATH}/src/gen/${SCHEMA}_generated.h")
list(APPEND GENERATED_OUTPUTS ${OUT})
add_custom_command(
OUTPUT ${OUT}
COMMAND flatc
ARGS
--cpp -o "${COMPONENT_PATH}/src/gen/"
-I "${PROJECT_PATH}/esp32-network-lib/uuid"
--scoped-enums
--gen-mutable
--gen-name-strings
--reflect-types
--reflect-names
"${FILE}"
DEPENDS "${FILE}"
COMMENT "Building flatbuffers C++ header for ${FILE}"
WORKING_DIRECTORY "${COMPONENT_PATH}"
)
endforeach()
set_source_files_properties("${GENERATED_OUTPUTS}" PROPERTIES GENERATED TRUE)
set_property(
DIRECTORY "${COMPONENT_PATH}"
APPEND PROPERTY
ADDITIONAL_MAKE_CLEAN_FILES "${GENERATED_OUTPUTS}"
)
set(${schema_generated_h}_OUTPUTS ${GENERATED_OUTPUTS} PARENT_SCOPE)
add_custom_target(${schema_generated_h}_TARGET DEPENDS ${GENERATED_OUTPUTS})
endfunction()
Code: Select all
FLATBUFFERS_GENERATE_GENERATED_H(uuid_generated_h uuid.fbs)
set_property(
SOURCE src/uuid.cpp
APPEND PROPERTY
OBJECT_DEPENDS "${uuid_generated_h_OUTPUTS}"
)
add_dependencies(${COMPONENT_NAME} uuid_generated_h_TARGET)
Code: Select all
In file included from ../requests/src/request_manager.cpp:11:
../uuid/src/uuid.h:13:28: fatal error: uuid_generated.h: No such file or directory
compilation terminated.
Oh yeah! `-include ${COMPONENT_PATH}/src/foo.h` is an excellent approach! I've hunted for an option like that before, so that's another thing I learned from this thread!ESP_Angus wrote: Oh wow... that's an adventurous use of the C preprocessor!
Best alternatives I can think of are:...
I also have some additional CMake questions:
- Are the empty settings `set(COMPONENT_REQUIRES)` and/or `set(COMPONENT_SRCS)` required in every `CMakeLists.txt`, even when they are empty?
- How can we override the C++ std setting (e.g. to use `-std=c++14`)
- When is it necessary to prefix `${COMPONENT_PATH}/` to file paths, or are they always relative to the compiling component?