@@ -19,8 +19,7 @@ include(CMakeParseArguments)
1919function (flatcc_generate_sources)
2020 # parse function arguments
2121 set (output_options SCHEMA COMMON COMMON_READER COMMON_BUILDER BUILDER READER VERIFIER JSON_PARSER JSON_PRINTER JSON)
22- # TODO: missing RECURSIVE option
23- set (NO_VAL_ARGS ALL ${output_options} )
22+ set (NO_VAL_ARGS ALL RECURSIVE ${output_options} )
2423 set (SINGLE_VAL_ARGS OUTPUT_DIR OUTFILE PREFIX TARGET )
2524 set (MULTI_VAL_ARGS DEFINITION_FILES COMPILE_FLAGS PATHS )
2625
@@ -36,9 +35,35 @@ function(flatcc_generate_sources)
3635
3736 list (APPEND FLATCC_COMPILE_FLAGS -o "${FLATCC_OUTPUT_DIR} " )
3837
38+ # Add current source directory for finding dependencies
39+ set (absolute_flatcc_paths "${CMAKE_CURRENT_SOURCE_DIR} " )
40+ # Also add directory of definition files
41+ foreach (definition_file FLATCC_DEFINITION_FILES)
42+ endforeach ()
43+
44+ set (ABSOLUTE_DEFINITION_FILES)
45+ foreach (definition_file ${FLATCC_DEFINITION_FILES} )
46+ # TODO: file(REAL_PATH) if cmake_minimum_required_version >= 3.19
47+ if (IS_ABSOLUTE "${definition_file} " )
48+ set (absolute_def_file "${definition_file} " )
49+ else ()
50+ set (absolute_def_file "${CMAKE_CURRENT_SOURCE_DIR} /${definition_file} " )
51+ endif ()
52+ list (APPEND ABSOLUTE_DEFINITION_FILES "${absolute_def_file} " )
53+
54+ get_filename_component (absolute_def_directory "${absolute_def_file} " DIRECTORY )
55+ list (APPEND absolute_flatcc_paths "${absolute_def_directory} " )
56+ endforeach ()
57+
3958 foreach (path ${FLATCC_PATHS} )
59+ # TODO: file(REAL_PATH) if cmake_minimum_required_version >= 3.19
60+ if (NOT IS_ABSOLUTE "${path} " )
61+ set (path "${CMAKE_CURRENT_SOURCE_DIR} /${path} " )
62+ endif ()
63+ list (APPEND absolute_flatcc_paths "${path} " )
4064 list (APPEND FLATCC_COMPILE_FLAGS -I "${path} " )
4165 endforeach ()
66+ list (REMOVE_DUPLICATES absolute_flatcc_paths)
4267
4368 # if no option is passed, the default READER is used
4469 set (default_option_reader ON )
@@ -86,10 +111,9 @@ function(flatcc_generate_sources)
86111 if (FLATCC_SCHEMA)
87112 list (APPEND FLATCC_COMPILE_FLAGS --schema)
88113 endif ()
89- # TODO: missing recursive option
90- # if (FLATCC_RECURSIVE)
91- # list(APPEND FLATCC_COMPILE_FLAGS --recursive)
92- # endif()
114+ if (FLATCC_RECURSIVE)
115+ list (APPEND FLATCC_COMPILE_FLAGS --recursive)
116+ endif ()
93117
94118 # handle 'all', 'common', 'json' last as they encompass other options
95119 if (FLATCC_COMMON)
@@ -109,8 +133,6 @@ function(flatcc_generate_sources)
109133 set (FLATCC_COMMON_READER ON )
110134 set (FLATCC_BUILDER ON )
111135 set (FLATCC_VERIFIER ON )
112- # TODO: missing recursive option
113- # set(FLATCC_RECURSIVE ON)
114136 endif ()
115137
116138 # Calculate suffixes of output files.
@@ -135,24 +157,58 @@ function(flatcc_generate_sources)
135157 list (APPEND GENERATED_FILE_SUFFIXES .bfbs)
136158 endif ()
137159
138- set (ABSOLUTE_DEFINITION_FILES)
139-
140- foreach (definition_file ${FLATCC_DEFINITION_FILES} )
141- # TODO: file(REAL_PATH) if cmake_minimum_Version >= 3.19
142- if (IS_ABSOLUTE "${definition_file} " )
143- set (absolute_def_file "${definition_file} " )
144- else ()
145- set (absolute_def_file "${CMAKE_CURRENT_SOURCE_DIR} /${definition_file} " )
160+ # grep each definition file recursively for includes, convert them to absolute paths and add them to a list
161+ set (ABSOLUTE_DEFINITIONS_DEPENDENCIES)
162+ set (absolute_definition_files_todo ${ABSOLUTE_DEFINITION_FILES} )
163+ while (absolute_definition_files_todo)
164+ list (GET absolute_definition_files_todo 0 current_deffile)
165+ # TODO: use if(absolute_definition_files_todo IN_LIST ABSOLUTE_DEFINITIONS_DEPENDENCIES) if cmake_minimum_required_version >= 3.3
166+ list (FIND ABSOLUTE_DEFINITIONS_DEPENDENCIES "${current_deffile} " todo_index)
167+ if (todo_index LESS 0)
168+ list (APPEND ABSOLUTE_DEFINITIONS_DEPENDENCIES "${current_deffile} " )
169+ file (READ "${current_deffile} " contents_deffile)
170+ set (include_regex "(^|\n )include[ \t ]+\" ([^\" ]+)\" " )
171+ string (REGEX MATCHALL "${include_regex} " includes_contents_match "${contents_deffile} " )
172+ foreach (include_match ${includes_contents_match} )
173+ string (REGEX MATCH "${include_regex} " _m "${include_match} " )
174+ set (include_def_file "${CMAKE_MATCH_2} " )
175+ if (IS_ABSOLUTE "${include_def_file} " )
176+ set (abs_include_def_file "${include_def_file} " )
177+ else ()
178+ set (abs_include_def_file)
179+ foreach (absolute_flatcc_path ${absolute_flatcc_paths} )
180+ if (NOT abs_include_def_file)
181+ if (EXISTS "${absolute_flatcc_path} /${include_def_file} " )
182+ set (abs_include_def_file "${absolute_flatcc_path} /${include_def_file} " )
183+ endif ()
184+ endif ()
185+ endforeach ()
186+ if (NOT abs_include_def_file)
187+ message (WARNING "${current_deffile} includes ${include_def_file} , but cannot be found in search path" )
188+ endif ()
189+ endif ()
190+ if (abs_include_def_file)
191+ list (APPEND absolute_definition_files_todo "${abs_include_def_file} " )
192+ endif ()
193+ endforeach ()
146194 endif ()
147- list (APPEND ABSOLUTE_DEFINITION_FILES "${absolute_def_file} " )
148- endforeach ()
195+ list (REMOVE_AT absolute_definition_files_todo 0)
196+ endwhile ()
197+
198+ list (REMOVE_DUPLICATES ABSOLUTE_DEFINITIONS_DEPENDENCIES)
199+ list (REMOVE_DUPLICATES ABSOLUTE_DEFINITION_FILES)
149200
150201 set (OUTPUT_FILES)
151202 if (FLATCC_OUTFILE)
152203 list (APPEND FLATCC_COMPILE_FLAGS "--outfile=${FLATCC_OUTPUT_DIR} /${FLATCC_OUTFILE} " )
153204 list (APPEND OUTPUT_FILES "${FLATCC_OUTPUT_DIR} /${FLATCC_OUTFILE} " )
154205 else ()
155- foreach (definition_file ${FLATCC_DEFINITION_FILES} )
206+ if (FLATCC_RECURSIVE)
207+ set (definition_files ${ABSOLUTE_DEFINITIONS_DEPENDENCIES} )
208+ else ()
209+ set (definition_files ${ABSOLUTE_DEFINITION_FILES} )
210+ endif ()
211+ foreach (definition_file ${definition_files} )
156212 get_filename_component (def_name_we "${definition_file} " NAME_WLE)
157213 foreach (suffix ${GENERATED_FILE_SUFFIXES} )
158214 list (APPEND OUTPUT_FILES "${FLATCC_OUTPUT_DIR} /${def_name_we}${suffix} " )
@@ -167,16 +223,20 @@ function(flatcc_generate_sources)
167223 endif ()
168224 endif ()
169225
170- message (VERBOSE "Flatcc output directory: ${FLATCC_OUTPUT_DIR} " )
171- message (VERBOSE "Flatcc output files: ${OUTPUT_FILES} " )
172- message (VERBOSE "Flatcc execute: flatcc;${FLATCC_COMPILE_FLAGS} ;${ABSOLUTE_DEFINITION_FILES} " )
226+ message (VERBOSE "---- flatcc start ----" )
227+ message (VERBOSE "output directory: ${FLATCC_OUTPUT_DIR} " )
228+ message (VERBOSE "output files: ${OUTPUT_FILES} " )
229+ message (VERBOSE "execute: flatcc;${FLATCC_COMPILE_FLAGS} ;${ABSOLUTE_DEFINITION_FILES} " )
230+ message (VERBOSE "dependencies: ${ABSOLUTE_DEFINITIONS_DEPENDENCIES} " )
231+ message (VERBOSE "----- flatcc end -----" )
232+
173233 add_custom_command (OUTPUT ${OUTPUT_FILES}
174234 COMMAND "${CMAKE_COMMAND} " -E make_directory "${FLATCC_OUTPUT_DIR} "
175235 COMMAND flatcc::cli ${FLATCC_COMPILE_FLAGS} ${ABSOLUTE_DEFINITION_FILES}
176- DEPENDS flatcc::cli ${FLATCC_DEFINITION_FILES }
236+ DEPENDS flatcc::cli ${ABSOLUTE_DEFINITIONS_DEPENDENCIES }
177237 )
178238 if (FLATCC_TARGET)
179- # FIXME : possible improvement if cmake_minimum_required >= 3.1:
239+ # TODO : possible improvement if cmake_minimum_required_version >= 3.1:
180240 # use add_library(... INTERFACE) + add_custom_command + target_sources(INTERFACE) + target_link_libraries()
181241 add_custom_target ("${FLATCC_TARGET} " DEPENDS ${OUTPUT_FILES} )
182242 endif ()
0 commit comments