Skip to content

Commit

Permalink
Make flatcc cross-compile deterministic (#4312)
Browse files Browse the repository at this point in the history
Summary:
As mentioned in #4297, the original flow makes host / cross build happen concurrently. This change moves host build process into cmake configuring stage and refine related dependencies.

Pull Request resolved: #4312

Test Plan:
- cross-compile > Through running `backends/qualcomm/script/build.sh --release`, we could check if the compiling process successfully finished.
- native-compile > Run following to check:
```shell
cmake \
    -DCMAKE_BUILD_TYPE=RelWithDebInfo \
    -DQNN_SDK_ROOT=${QNN_SDK_ROOT} \
    -DEXECUTORCH_BUILD_QNN=ON \
    -DEXECUTORCH_BUILD_SDK=ON \
    -DPYTHON_EXECUTABLE=$PYTHON_EXECUTABLE \
    -S $EXECUTORCH_ROOT \
    -B $EXECUTORCH_ROOT/build_x86_64 \
```

Reviewed By: tarun292

Differential Revision: D60243701

Pulled By: dbort

fbshipit-source-id: ff8d8cb06f0cc296c7ef465596e7e3df367dd059
  • Loading branch information
haowhsu-quic authored and facebook-github-bot committed Aug 2, 2024
1 parent 1b6d5bb commit 15815dd
Showing 1 changed file with 27 additions and 46 deletions.
73 changes: 27 additions & 46 deletions sdk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,23 +87,37 @@ if(EXECUTORCH_SEPARATE_FLATCC_HOST_PROJECT)
# Add the host project. We build this separately so that we can generate
# headers on the host during the build, even if we're cross-compiling the
# flatcc runtime to a different architecture.

# lint_cmake: -readability/wonkycase
ExternalProject_Add(
flatcc_project
PREFIX ${CMAKE_BINARY_DIR}/_host_build
SOURCE_DIR ${_flatcc_source_dir}
BINARY_DIR ${CMAKE_BINARY_DIR}/_host_build
CMAKE_CACHE_ARGS
-DFLATCC_TEST:BOOL=OFF -DFLATCC_REFLECTION:BOOL=OFF
# See above comment about POSITION_INDEPENDENT_CODE.
-DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON
INSTALL_COMMAND "" # Prevent the install step
execute_process(
COMMAND ${CMAKE_COMMAND} ${_flatcc_source_dir}
-DFLATCC_TEST=OFF -DFLATCC_REFLECTION=OFF
# See above comment about POSITION_INDEPENDENT_CODE.
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
-B${CMAKE_BINARY_DIR}/_host_build
)
execute_process(
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR}/_host_build
)
set(_etdump_schema_gen_dep)
# TODO(dbort): flatcc installs its files directly in its source directory
# instead of under CMAKE_BINARY_DIR, and it has no options to avoid doing
# this. We build flatcc twice in the executorch build: once to get the
# `flatcc` host commandline tool, and once to get the (potentially
# cross-compiled) target runtime library. The host build will put its outputs
# in the source tree, making the cross-compiling target build think that the
# outputs have already been built. It will then try to link against the
# host-architecture libraries, failing when cross-compiling. To work around
# this, delete the host outputs after running this command (which only runs
# when setting up the cmake files, not when actually building). This leaves
# room for the target build to put its own files in the source tree. We should
# try to remove this hack, ideally by submitting an upstream PR that adds an
# option to change the installation location.
set(_etdump_schema_cleanup_paths ${_flatcc_source_dir}/bin/*
${_flatcc_source_dir}/lib/*
)
set(_etdump_schema_gen_dep flatcc_project)
else()
# If we're not cross-compiling, we can just use the plain commandline target.
set(_etdump_schema_gen_dep flatcc_cli)
set(_etdump_schema_cleanup_paths "")
endif()

set(_etdump_schema__outputs)
Expand Down Expand Up @@ -134,42 +148,11 @@ add_library(
bundled_program_schema INTERFACE ${_bundled_program_schema__outputs}
)

# Ensure the host tool is built before the main project
add_dependencies(etdump_schema flatcc_cli)

file(MAKE_DIRECTORY ${_program_schema__include_dir}/executorch/sdk/etdump)
file(MAKE_DIRECTORY
${_program_schema__include_dir}/executorch/sdk/bundled_program
)

if(EXECUTORCH_SEPARATE_FLATCC_HOST_PROJECT)
# If we cross-compiling, we need to use the version of the commandline tool
# built for the host.
set(_etdump_schema_gen_dep flatcc_project)

# TODO(dbort): flatcc installs its files directly in its source directory
# instead of under CMAKE_BINARY_DIR, and it has no options to avoid doing
# this. We build flatcc twice in the executorch build: once to get the
# `flatcc` host commandline tool, and once to get the (potentially
# cross-compiled) target runtime library. The host build will put its outputs
# in the source tree, making the cross-compiling target build think that the
# outputs have already been built. It will then try to link against the
# host-architecture libraries, failing when cross-compiling. To work around
# this, delete the host outputs after running this command (which only runs
# when setting up the cmake files, not when actually building). This leaves
# room for the target build to put its own files in the source tree. We should
# try to remove this hack, ideally by submitting an upstream PR that adds an
# option to change the installation location.
set(_etdump_schema_cleanup_paths ${_flatcc_source_dir}/bin/*
${_flatcc_source_dir}/lib/*
)
else()
# If we're not cross-compiling we can use the plain commandline target, and we
# don't need to delete any files.
set(_etdump_schema_gen_dep flatcc_cli)
set(_etdump_schema_cleanup_paths "")
endif()

add_custom_command(
OUTPUT ${_etdump_schema__outputs}
COMMAND
Expand All @@ -180,10 +163,8 @@ add_custom_command(
${_program_schema__include_dir}/executorch/sdk/etdump
${_etdump_schema__srcs}
COMMAND rm -f ${_etdump_schema_cleanup_paths}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/sdk
DEPENDS ${_etdump_schema_gen_dep}
COMMENT "Generating etdump headers"
VERBATIM
)

add_library(
Expand Down

0 comments on commit 15815dd

Please sign in to comment.