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