Skip to content

Commit 518324f

Browse files
authored
Build flatcc for the host (#10855)
### Summary * Similar to what we did with `flatc`, let's build `flatcc_cli` for the host using [ExternalProject](https://cmake.org/cmake/help/latest/module/ExternalProject.html) * Move the `flatcc` definitions to be under `third-party/` * Move the `etdump` and `bundled_program` definitions to be under their respective folders * Clean up the build files and remove redundant things ### Test plan CI ``` $ ./install_executorch.sh $ ./scripts/build_apple_frameworks.sh $ rm -rf cmake-out \ && cmake --preset macos-arm64 \ && cmake --build cmake-out --parallel $ rm -rf cmake-out \ && cmake -DEXECUTORCH_BUILD_DEVTOOLS=ON -Dprotobuf_BUILD_TESTS=OFF -DEXECUTORCH_ENABLE_EVENT_TRACER=ON --preset macos-arm64 \ && cmake --build cmake-out --parallel ``` cc @larryliu0820
1 parent e13b086 commit 518324f

File tree

4 files changed

+173
-231
lines changed

4 files changed

+173
-231
lines changed

devtools/CMakeLists.txt

Lines changed: 3 additions & 231 deletions
Original file line numberDiff line numberDiff line change
@@ -4,236 +4,8 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7-
# Please this file formatted by running:
8-
# ~~~
9-
# cmake-format -i CMakeLists.txt
10-
# ~~~
11-
12-
cmake_minimum_required(VERSION 3.19)
13-
14-
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
15-
16-
set(_flatcc_source_dir ${CMAKE_CURRENT_SOURCE_DIR}/../third-party/flatcc)
17-
18-
if(NOT CMAKE_CXX_STANDARD)
19-
set(CMAKE_CXX_STANDARD 17)
20-
endif()
21-
22-
if(NOT FLATCC_EXECUTABLE)
23-
if(WIN32)
24-
set(FLATCC_EXECUTABLE ${_flatcc_source_dir}/bin/${CMAKE_BUILD_TYPE}/flatcc)
25-
else()
26-
set(FLATCC_EXECUTABLE ${_flatcc_source_dir}/bin/flatcc)
27-
endif()
28-
endif()
29-
30-
# Source root directory for executorch.
31-
if(NOT EXECUTORCH_ROOT)
32-
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/..)
33-
endif()
34-
35-
include(${EXECUTORCH_ROOT}/tools/cmake/Utils.cmake)
36-
37-
if(NOT PYTHON_EXECUTABLE)
38-
resolve_python_executable()
39-
endif()
40-
41-
# Paths to headers generated from the .fbs files. set(_etdump_schemas
42-
# etdump_schema_flatcc.fbs scalar_type.fbs)
43-
44-
set(_etdump_schema_names "etdump_schema_flatcc.fbs" "scalar_type.fbs")
45-
set(_bundled_input_schema_names "bundled_program_schema.fbs" "scalar_type.fbs")
46-
47-
foreach(schema_file ${_etdump_schema_names})
48-
list(APPEND _etdump_schema__srcs
49-
"${CMAKE_CURRENT_SOURCE_DIR}/etdump/${schema_file}"
50-
)
51-
endforeach()
52-
53-
foreach(schema_file ${_bundled_input_schema_names})
54-
list(APPEND _bundled_program_schema__srcs
55-
"${CMAKE_CURRENT_SOURCE_DIR}/bundled_program/schema/${schema_file}"
56-
)
57-
endforeach()
58-
59-
set(FLATCC_TEST
60-
OFF
61-
CACHE BOOL ""
62-
)
63-
set(FLATCC_REFLECTION
64-
OFF
65-
CACHE BOOL ""
66-
)
67-
set(FLATCC_DEBUG_CLANG_SANITIZE
68-
OFF
69-
CACHE BOOL ""
70-
)
71-
72-
add_subdirectory(${_flatcc_source_dir} ${CMAKE_BINARY_DIR}/third-party/flatcc)
73-
74-
# Fix for "relocation R_X86_64_32 against `.rodata' can not be used when making
75-
# a shared object; recompile with -fPIC" when building on some x86 linux
76-
# systems.
77-
set_property(TARGET flatccrt PROPERTY POSITION_INDEPENDENT_CODE ON)
78-
79-
# Assume we are cross-compiling and the CMAKE_TOOLCHAIN_FILE is set
80-
include(ExternalProject)
81-
827
# The include directory that will contain the generated schema headers.
83-
set(_program_schema__include_dir "${CMAKE_BINARY_DIR}/devtools/include")
84-
set(_bundled_schema__include_dir "${CMAKE_BINARY_DIR}/devtools/bundled_program")
85-
86-
# TODO(dbort): Only enable this when cross-compiling. It can cause build race
87-
# conditions (libflatcc.a errors) when enabled.
88-
option(EXECUTORCH_SEPARATE_FLATCC_HOST_PROJECT
89-
"Whether to build the flatcc commandline tool as a separate project" ON
90-
)
91-
92-
if(EXECUTORCH_SEPARATE_FLATCC_HOST_PROJECT)
93-
# Add the host project. We build this separately so that we can generate
94-
# headers on the host during the build, even if we're cross-compiling the
95-
# flatcc runtime to a different architecture.
96-
execute_process(
97-
COMMAND
98-
${CMAKE_COMMAND} ${_flatcc_source_dir} -DFLATCC_TEST=OFF
99-
-DFLATCC_REFLECTION=OFF
100-
# See above comment about POSITION_INDEPENDENT_CODE.
101-
-DCMAKE_POSITION_INDEPENDENT_CODE=ON -B${CMAKE_BINARY_DIR}/_host_build
102-
)
103-
execute_process(
104-
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/_host_build
105-
)
106-
set(_etdump_schema_gen_dep)
107-
# TODO(dbort): flatcc installs its files directly in its source directory
108-
# instead of under CMAKE_BINARY_DIR, and it has no options to avoid doing
109-
# this. We build flatcc twice in the executorch build: once to get the
110-
# `flatcc` host commandline tool, and once to get the (potentially
111-
# cross-compiled) target runtime library. The host build will put its outputs
112-
# in the source tree, making the cross-compiling target build think that the
113-
# outputs have already been built. It will then try to link against the
114-
# host-architecture libraries, failing when cross-compiling. To work around
115-
# this, delete the host outputs after running this command (which only runs
116-
# when setting up the cmake files, not when actually building). This leaves
117-
# room for the target build to put its own files in the source tree. We should
118-
# try to remove this hack, ideally by submitting an upstream PR that adds an
119-
# option to change the installation location.
120-
set(_etdump_schema_cleanup_paths ${_flatcc_source_dir}/bin/*
121-
${_flatcc_source_dir}/lib/*
122-
)
123-
else()
124-
# If we're not cross-compiling, we can just use the plain commandline target.
125-
set(_etdump_schema_gen_dep flatcc_cli)
126-
set(_etdump_schema_cleanup_paths "")
127-
endif()
128-
129-
set(_etdump_schema__outputs)
130-
foreach(fbs_file ${_etdump_schema_names})
131-
string(REGEX REPLACE "[.]fbs$" "_reader.h" generated "${fbs_file}")
132-
list(APPEND _etdump_schema__outputs
133-
"${_program_schema__include_dir}/executorch/devtools/etdump/${generated}"
134-
)
135-
string(REGEX REPLACE "[.]fbs$" "_builder.h" generated "${fbs_file}")
136-
list(APPEND _etdump_schema__outputs
137-
"${_program_schema__include_dir}/executorch/devtools/etdump/${generated}"
138-
)
139-
endforeach()
140-
141-
# lint_cmake: -linelength
142-
set(_bundled_program_schema__outputs)
143-
foreach(fbs_file ${_bundled_input_schema_names})
144-
string(REGEX REPLACE "[.]fbs$" "_generated.h" generated "${fbs_file}")
145-
list(
146-
APPEND
147-
_bundled_program_schema__outputs
148-
"${_bundled_schema__include_dir}/executorch/devtools/bundled_program/schema/${generated}"
149-
)
150-
endforeach()
151-
152-
add_library(etdump_schema INTERFACE ${_etdump_schema__outputs})
153-
add_library(
154-
bundled_program_schema INTERFACE ${_bundled_program_schema__outputs}
155-
)
156-
157-
file(MAKE_DIRECTORY ${_program_schema__include_dir}/executorch/devtools/etdump)
158-
file(MAKE_DIRECTORY
159-
${_program_schema__include_dir}/executorch/devtools/bundled_program
160-
)
161-
162-
if(WIN32)
163-
set(RM_COMMAND rmdir /s /q)
164-
else()
165-
set(RM_COMMAND rm -rf)
166-
endif()
167-
168-
add_custom_command(
169-
OUTPUT ${_etdump_schema__outputs}
170-
COMMAND
171-
# Note that the flatcc project actually writes its outputs into the source
172-
# tree instead of under the binary directory, and there's no way to change
173-
# that behavior.
174-
${FLATCC_EXECUTABLE} -cwr -o
175-
${_program_schema__include_dir}/executorch/devtools/etdump
176-
${_etdump_schema__srcs}
177-
COMMAND ${RM_COMMAND} ${_etdump_schema_cleanup_paths}
178-
DEPENDS ${_etdump_schema_gen_dep}
179-
COMMENT "Generating etdump headers"
180-
)
181-
182-
unset(RM_COMMAND)
183-
184-
add_library(
185-
etdump ${CMAKE_CURRENT_SOURCE_DIR}/etdump/etdump_flatcc.cpp
186-
${CMAKE_CURRENT_SOURCE_DIR}/etdump/emitter.cpp
187-
${CMAKE_CURRENT_SOURCE_DIR}/etdump/data_sinks/buffer_data_sink.cpp
188-
${CMAKE_CURRENT_SOURCE_DIR}/etdump/data_sinks/buffer_data_sink.h
189-
${CMAKE_CURRENT_SOURCE_DIR}/etdump/data_sinks/file_data_sink.cpp
190-
${CMAKE_CURRENT_SOURCE_DIR}/etdump/data_sinks/file_data_sink.h
191-
)
192-
193-
target_link_libraries(
194-
etdump
195-
PUBLIC etdump_schema flatccrt
196-
PRIVATE executorch
197-
)
198-
199-
add_custom_command(
200-
OUTPUT ${_bundled_program_schema__outputs}
201-
COMMAND
202-
flatc --cpp --cpp-std c++11 --gen-mutable --scoped-enums -o
203-
"${_bundled_schema__include_dir}/executorch/devtools/bundled_program/schema"
204-
${_bundled_program_schema__srcs}
205-
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/devtools
206-
DEPENDS flatc ${_bundled_program_schema__srcs}
207-
COMMENT "Generating bundled_program headers"
208-
VERBATIM
209-
)
210-
211-
# add_library(bundled_program INTERFACE ${_bundled_program_schema__outputs})
212-
add_library(
213-
bundled_program
214-
${CMAKE_CURRENT_SOURCE_DIR}/bundled_program/bundled_program.cpp
215-
)
216-
target_link_libraries(bundled_program executorch bundled_program_schema)
217-
218-
set_target_properties(bundled_program PROPERTIES LINKER_LANGUAGE CXX)
219-
target_include_directories(
220-
bundled_program PUBLIC ${_bundled_schema__include_dir}
221-
${EXECUTORCH_ROOT}/third-party/flatbuffers/include
222-
)
223-
224-
target_include_directories(
225-
etdump PUBLIC ${_program_schema__include_dir} ${_flatcc_source_dir}/include
226-
)
227-
228-
# Install libraries
229-
install(
230-
TARGETS bundled_program etdump flatccrt
231-
DESTINATION ${CMAKE_BINARY_DIR}/lib
232-
INCLUDES
233-
DESTINATION ${_common_include_directories}
234-
)
8+
set(DEVTOOLS_INCLUDE_DIR "${CMAKE_BINARY_DIR}/devtools/include")
2359

236-
if(BUILD_TESTING)
237-
# TODO: This is currently not working!
238-
# add_subdirectory(etdump/tests)
239-
endif()
10+
add_subdirectory(etdump)
11+
add_subdirectory(bundled_program)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
set(
8+
_schema_files
9+
bundled_program_schema.fbs
10+
scalar_type.fbs
11+
)
12+
13+
set(_schema_outputs)
14+
foreach(schema_file ${_schema_files})
15+
list(APPEND _bundled_program_schema__srcs "${CMAKE_CURRENT_SOURCE_DIR}/schema/${schema_file}")
16+
17+
string(REGEX REPLACE "[.]fbs$" "_generated.h" generated "${schema_file}")
18+
list(APPEND _schema_outputs "${DEVTOOLS_INCLUDE_DIR}/executorch/devtools/bundled_program/schema/${generated}")
19+
endforeach()
20+
21+
file(MAKE_DIRECTORY ${DEVTOOLS_INCLUDE_DIR}/executorch/devtools/bundled_program)
22+
add_custom_command(
23+
OUTPUT ${_schema_outputs}
24+
COMMAND
25+
flatc --cpp --cpp-std c++11 --gen-mutable --scoped-enums -o
26+
${DEVTOOLS_INCLUDE_DIR}/executorch/devtools/bundled_program/schema
27+
${_bundled_program_schema__srcs}
28+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/devtools
29+
DEPENDS flatc ${_bundled_program_schema__srcs}
30+
COMMENT "Generating bundled_program headers"
31+
VERBATIM
32+
)
33+
34+
add_library(
35+
bundled_program
36+
${_schema_outputs}
37+
${CMAKE_CURRENT_SOURCE_DIR}/bundled_program.cpp
38+
)
39+
target_link_libraries(
40+
bundled_program
41+
PUBLIC
42+
executorch
43+
)
44+
target_include_directories(
45+
bundled_program
46+
PUBLIC
47+
${DEVTOOLS_INCLUDE_DIR}
48+
${PROJECT_SOURCE_DIR}/third-party/flatbuffers/include
49+
)
50+
51+
install(
52+
TARGETS bundled_program
53+
DESTINATION ${CMAKE_BINARY_DIR}/lib
54+
INCLUDES
55+
DESTINATION ${_common_include_directories}
56+
)

devtools/etdump/CMakeLists.txt

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
set(
8+
_schema_files
9+
etdump_schema_flatcc.fbs
10+
scalar_type.fbs
11+
)
12+
13+
set(_schema_outputs)
14+
foreach(schema_file ${_schema_files})
15+
list(APPEND _etdump_schema__srcs "${CMAKE_CURRENT_SOURCE_DIR}/${schema_file}")
16+
17+
string(REGEX REPLACE "[.]fbs$" "_reader.h" generated_reader "${schema_file}")
18+
list(APPEND _schema_outputs "${DEVTOOLS_INCLUDE_DIR}/executorch/devtools/etdump/${generated_reader}")
19+
20+
string(REGEX REPLACE "[.]fbs$" "_builder.h" generated_builder "${schema_file}")
21+
list(APPEND _schema_outputs "${DEVTOOLS_INCLUDE_DIR}/executorch/devtools/etdump/${generated_builder}")
22+
endforeach()
23+
24+
file(MAKE_DIRECTORY ${DEVTOOLS_INCLUDE_DIR}/executorch/devtools/etdump)
25+
add_custom_command(
26+
OUTPUT ${_schema_outputs}
27+
COMMAND
28+
# Note that the flatcc project actually writes its outputs into the source
29+
# tree instead of under the binary directory, and there's no way to change
30+
# that behavior.
31+
flatcc_cli -cwr -o
32+
${DEVTOOLS_INCLUDE_DIR}/executorch/devtools/etdump
33+
${_etdump_schema__srcs}
34+
DEPENDS flatcc_cli ${_etdump_schema__srcs}
35+
COMMENT "Generating etdump headers"
36+
)
37+
38+
add_library(
39+
etdump
40+
${_schema_outputs}
41+
${CMAKE_CURRENT_SOURCE_DIR}/etdump_flatcc.cpp
42+
${CMAKE_CURRENT_SOURCE_DIR}/emitter.cpp
43+
${CMAKE_CURRENT_SOURCE_DIR}/data_sinks/buffer_data_sink.cpp
44+
${CMAKE_CURRENT_SOURCE_DIR}/data_sinks/buffer_data_sink.h
45+
${CMAKE_CURRENT_SOURCE_DIR}/data_sinks/file_data_sink.cpp
46+
${CMAKE_CURRENT_SOURCE_DIR}/data_sinks/file_data_sink.h
47+
)
48+
target_link_libraries(
49+
etdump
50+
PUBLIC
51+
flatccrt
52+
PRIVATE
53+
executorch
54+
)
55+
target_include_directories(
56+
etdump
57+
PUBLIC
58+
${DEVTOOLS_INCLUDE_DIR}
59+
${PROJECT_SOURCE_DIR}/third-party/flatcc/include
60+
)
61+
62+
install(
63+
TARGETS etdump
64+
DESTINATION ${CMAKE_BINARY_DIR}/lib
65+
INCLUDES
66+
DESTINATION ${_common_include_directories}
67+
)

0 commit comments

Comments
 (0)