@@ -69,18 +69,33 @@ include(ExternalProject)
69
69
set (_program_schema__include_dir "${CMAKE_BINARY_DIR} /sdk/include" )
70
70
set (_bundled_schema__include_dir "${CMAKE_BINARY_DIR} /sdk/bundled_program" )
71
71
72
- # Add the host project
73
- # lint_cmake: -readability/wonkycase
74
- ExternalProject_Add(
75
- flatcc_project
76
- PREFIX ${CMAKE_BINARY_DIR} /_host_build
77
- SOURCE_DIR ${_flatcc_source_dir}
78
- BINARY_DIR ${CMAKE_BINARY_DIR} /_host_build
79
- CMAKE_CACHE_ARGS -DFLATCC_TEST:BOOL =OFF -DFLATCC_REFLECTION:BOOL =OFF
72
+ # TODO(dbort): Only enable this when cross-compiling. It can cause build race
73
+ # conditions (libflatcc.a errors) when enabled.
74
+ option (EXECUTORCH_SEPARATE_FLATCC_HOST_PROJECT
75
+ "Whether to build the flatcc commandline tool as a separate project" ON )
76
+
77
+ if (EXECUTORCH_SEPARATE_FLATCC_HOST_PROJECT)
78
+ # Add the host project. We build this separately so that we can generate
79
+ # headers on the host during the build, even if we're cross-compiling the
80
+ # flatcc runtime to a different architecture.
81
+
82
+ # lint_cmake: -readability/wonkycase
83
+ ExternalProject_Add(
84
+ flatcc_project
85
+ PREFIX ${CMAKE_BINARY_DIR} /_host_build
86
+ SOURCE_DIR ${_flatcc_source_dir}
87
+ BINARY_DIR ${CMAKE_BINARY_DIR} /_host_build
88
+ CMAKE_CACHE_ARGS
89
+ -DFLATCC_TEST:BOOL =OFF -DFLATCC_REFLECTION:BOOL =OFF
80
90
# See above comment about POSITION_INDEPENDENT_CODE.
81
91
-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL =ON
82
- INSTALL_COMMAND "" # Prevent the install step, modify as needed
83
- )
92
+ INSTALL_COMMAND "" # Prevent the install step
93
+ )
94
+ set (_etdump_schema_gen_dep flatcc_project)
95
+ else ()
96
+ # If we're not cross-compiling, we can just use the plain commandline target.
97
+ set (_etdump_schema_gen_dep flatcc_cli)
98
+ endif ()
84
99
85
100
set (_etdump_schema__outputs)
86
101
foreach (fbs_file ${_etdump_schema_names} )
@@ -114,33 +129,45 @@ file(MAKE_DIRECTORY ${_program_schema__include_dir}/executorch/sdk/etdump)
114
129
file (MAKE_DIRECTORY
115
130
${_program_schema__include_dir} /executorch/sdk/bundled_program)
116
131
117
- add_custom_command (
118
- OUTPUT ${_etdump_schema__outputs}
119
- COMMAND
120
- # Note that the flatcc project actually writes its outputs into the source
121
- # tree instead of under the binary directory, and there's no way to change
122
- # that behavior.
123
- ${_flatcc_source_dir} /bin/flatcc -cwr -o
124
- ${_program_schema__include_dir} /executorch/sdk/etdump
125
- ${_etdump_schema__srcs}
132
+ if (EXECUTORCH_SEPARATE_FLATCC_HOST_PROJECT)
133
+ # If we cross-compiling, we need to use the version of the commandline tool
134
+ # built for the host.
135
+ set (_etdump_schema_gen_dep flatcc_project)
136
+
126
137
# TODO(dbort): flatcc installs its files directly in its source directory
127
- # instead of under CMAKE_BINARY_DIR, and it has no options to avoid
128
- # doing this. We build flatcc twice in the executorch build: once to get
129
- # the `flatcc` host commandline tool, and once to get the (potentially
138
+ # instead of under CMAKE_BINARY_DIR, and it has no options to avoid doing
139
+ # this. We build flatcc twice in the executorch build: once to get the
140
+ # `flatcc` host commandline tool, and once to get the (potentially
130
141
# cross-compiled) target runtime library. The host build will put its outputs
131
- # in the source tree, making the cross-compiling target build think that
132
- # the outputs have already been built. It will then try to link against the
142
+ # in the source tree, making the cross-compiling target build think that the
143
+ # outputs have already been built. It will then try to link against the
133
144
# host-architecture libraries, failing when cross-compiling. To work around
134
145
# this, delete the host outputs after running this command (which only runs
135
146
# when setting up the cmake files, not when actually building). This leaves
136
147
# room for the target build to put its own files in the source tree. We should
137
148
# try to remove this hack, ideally by submitting an upstream PR that adds an
138
149
# option to change the installation location.
150
+ set (_etdump_schema_cleanup_paths ${_flatcc_source_dir} /bin/*
151
+ ${_flatcc_source_dir} /lib/*)
152
+ else ()
153
+ # If we're not cross-compiling we can use the plain commandline target, and we
154
+ # don't need to delete any files.
155
+ set (_etdump_schema_gen_dep flatcc_cli)
156
+ set (_etdump_schema_cleanup_paths "" )
157
+ endif ()
158
+
159
+ add_custom_command (
160
+ OUTPUT ${_etdump_schema__outputs}
139
161
COMMAND
140
- rm -f ${_flatcc_source_dir} /bin/*
141
- ${_flatcc_source_dir} /lib/*
162
+ # Note that the flatcc project actually writes its outputs into the source
163
+ # tree instead of under the binary directory, and there's no way to change
164
+ # that behavior.
165
+ ${_flatcc_source_dir} /bin/flatcc -cwr -o
166
+ ${_program_schema__include_dir} /executorch/sdk/etdump
167
+ ${_etdump_schema__srcs}
168
+ COMMAND rm -f ${_etdump_schema_cleanup_paths}
142
169
WORKING_DIRECTORY ${CMAKE_BINARY_DIR} /sdk
143
- DEPENDS flatcc_project
170
+ DEPENDS ${_etdump_schema_gen_dep}
144
171
COMMENT "Generating etdump headers"
145
172
VERBATIM )
146
173
0 commit comments