• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015 Google Inc. All rights reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15# General function to create FlatBuffer build rules for the given list of
16# schemas.
17#
18# flatbuffers_schemas: A list of flatbuffer schema files to process.
19#
20# schema_include_dirs: A list of schema file include directories, which will be
21# passed to flatc via the -I parameter.
22#
23# custom_target_name: The generated files will be added as dependencies for a
24# new custom target with this name. You should add that target as a dependency
25# for your main target to ensure these files are built. You can also retrieve
26# various properties from this target, such as GENERATED_INCLUDES_DIR,
27# BINARY_SCHEMAS_DIR, and COPY_TEXT_SCHEMAS_DIR.
28#
29# additional_dependencies: A list of additional dependencies that you'd like
30# all generated files to depend on. Pass in a blank string if you have none.
31#
32# generated_includes_dir: Where to generate the C++ header files for these
33# schemas. The generated includes directory will automatically be added to
34# CMake's include directories, and will be where generated header files are
35# placed. This parameter is optional; pass in empty string if you don't want to
36# generate include files for these schemas.
37#
38# binary_schemas_dir: If you specify an optional binary schema directory, binary
39# schemas will be generated for these schemas as well, and placed into the given
40# directory.
41#
42# copy_text_schemas_dir: If you want all text schemas (including schemas from
43# all schema include directories) copied into a directory (for example, if you
44# need them within your project to build JSON files), you can specify that
45# folder here. All text schemas will be copied to that folder.
46#
47# IMPORTANT: Make sure you quote all list arguments you pass to this function!
48# Otherwise CMake will only pass in the first element.
49# Example: build_flatbuffers("${fb_files}" "${include_dirs}" target_name ...)
50function(build_flatbuffers flatbuffers_schemas
51                           schema_include_dirs
52                           custom_target_name
53                           additional_dependencies
54                           generated_includes_dir
55                           binary_schemas_dir
56                           copy_text_schemas_dir)
57
58  # Test if including from FindFlatBuffers
59  if(FLATBUFFERS_FLATC_EXECUTABLE)
60    set(FLATC_TARGET "")
61    set(FLATC ${FLATBUFFERS_FLATC_EXECUTABLE})
62  else()
63    set(FLATC_TARGET flatc)
64    set(FLATC flatc)
65  endif()
66  set(FLATC_SCHEMA_ARGS --gen-mutable)
67  if(FLATBUFFERS_FLATC_SCHEMA_EXTRA_ARGS)
68    set(FLATC_SCHEMA_ARGS
69      ${FLATBUFFERS_FLATC_SCHEMA_EXTRA_ARGS}
70      ${FLATC_SCHEMA_ARGS}
71      )
72  endif()
73
74  set(schema_glob "*.fbs")
75  # Generate the include files parameters.
76  set(include_params "")
77  set(all_generated_files "")
78  foreach (include_dir ${schema_include_dirs})
79    set(include_params -I ${include_dir} ${include_params})
80    if (NOT ${copy_text_schemas_dir} STREQUAL "")
81      # Copy text schemas from dependent folders.
82      file(GLOB_RECURSE dependent_schemas ${include_dir}/${schema_glob})
83      foreach (dependent_schema ${dependent_schemas})
84        file(COPY ${dependent_schema} DESTINATION ${copy_text_schemas_dir})
85      endforeach()
86    endif()
87  endforeach()
88
89  foreach(schema ${flatbuffers_schemas})
90    get_filename_component(filename ${schema} NAME_WE)
91    # For each schema, do the things we requested.
92    if (NOT ${generated_includes_dir} STREQUAL "")
93      set(generated_include ${generated_includes_dir}/${filename}_generated.h)
94      add_custom_command(
95        OUTPUT ${generated_include}
96        COMMAND ${FLATC} ${FLATC_SCHEMA_ARGS}
97        -o ${generated_includes_dir}
98        ${include_params}
99        -c ${schema}
100        DEPENDS ${FLATC_TARGET} ${schema} ${additional_dependencies})
101      list(APPEND all_generated_files ${generated_include})
102    endif()
103
104    if (NOT ${binary_schemas_dir} STREQUAL "")
105      set(binary_schema ${binary_schemas_dir}/${filename}.bfbs)
106      add_custom_command(
107        OUTPUT ${binary_schema}
108        COMMAND ${FLATC} -b --schema
109        -o ${binary_schemas_dir}
110        ${include_params}
111        ${schema}
112        DEPENDS ${FLATC_TARGET} ${schema} ${additional_dependencies})
113      list(APPEND all_generated_files ${binary_schema})
114    endif()
115
116    if (NOT ${copy_text_schemas_dir} STREQUAL "")
117      file(COPY ${schema} DESTINATION ${copy_text_schemas_dir})
118    endif()
119  endforeach()
120
121  # Create a custom target that depends on all the generated files.
122  # This is the target that you can depend on to trigger all these
123  # to be built.
124  add_custom_target(${custom_target_name}
125                    DEPENDS ${all_generated_files} ${additional_dependencies})
126
127  # Register the include directory we are using.
128  if (NOT ${generated_includes_dir} STREQUAL "")
129    include_directories(${generated_includes_dir})
130    set_property(TARGET ${custom_target_name}
131      PROPERTY GENERATED_INCLUDES_DIR
132      ${generated_includes_dir})
133  endif()
134
135  # Register the binary schemas dir we are using.
136  if (NOT ${binary_schemas_dir} STREQUAL "")
137    set_property(TARGET ${custom_target_name}
138      PROPERTY BINARY_SCHEMAS_DIR
139      ${binary_schemas_dir})
140  endif()
141
142  # Register the text schema copy dir we are using.
143  if (NOT ${copy_text_schemas_dir} STREQUAL "")
144    set_property(TARGET ${custom_target_name}
145      PROPERTY COPY_TEXT_SCHEMAS_DIR
146      ${copy_text_schemas_dir})
147  endif()
148endfunction()
149